The scaling operation breaks the simSetConfigurationTree

Typically: "How do I... ", "How can I... " questions
Post Reply
zhengkz
Posts: 4
Joined: 16 Jul 2021, 20:58

The scaling operation breaks the simSetConfigurationTree

Post by zhengkz »

Hi, I confronted a problem that the scaling operation will break the sim.setConfigurationTree. To clarify, if I set init_state = sim.getConfigurationTree(item_handle) and then scaling some objects under the item. The sim.setConfigurationTree(init_state) will ignore the scaled objects, even after I scale them back to the init size.

So what is the best way to undo the scaling and come back to the init pose? I use coppeliasim 4.1. Thanks!

coppelia
Site Admin
Posts: 8893
Joined: 14 Dec 2012, 00:25

Re: The scaling operation breaks the simSetConfigurationTree

Post by coppelia »

Hello,

that API function will not touch scaling data, i.e. if will leave the object as it is.
Additionally, at simulation end, many parameters are restored, but not all of them. If you need a perfect restoration, best would be to copy the original object/model and/or save it to a buffer, like in following simple example of a customization script attached to a dummy, handling that task:

Code: Select all

function sysCall_init()
    model=sim.getObjectHandle("robotModel")
end

function sysCall_beforeSimulation()
    copy=sim.saveModel(model) -- memorize the original model
    print("Model data was saved")
end

function sysCall_afterSimulation()
    restoreIfNeeded()
end

function restoreIfNeeded()
    if copy then
        sim.removeModel(model) -- destroy the modified model
        model=sim.loadModel(copy) -- restore the original model
        print("Model was destroyed, and restored from the original data")
        copy=nil
    end
end

function sysCall_cleanup()
    restoreIfNeeded()
end
You can also keep scaled objects, and restore them to their initial pose something like following (scene is available in CoppeliaSim V4.3.0, scenes/teleportDynamicModel.ttm):

Code: Select all

function memorize()
    -- memorize the object poses, etc.:
    -----------------------------------------------
    local l=sim.setThreadAutomaticSwitch(false) -- in case this runs in a thread
    initialState={}
    local modelObjects=sim.getObjectsInTree(modelHandle,sim.handle_all,0)
    for i=1,#modelObjects,1 do
        local obj=modelObjects[i]
        local data={}
        data.localPose=sim.getObjectPose(obj,sim.handle_parent)
        -- e.g. you could also take into account the joints' initial states:
        --[[ 
        if sim.getObjectType(obj)==sim.object_joint_type then
            if sim.getJointType(obj)==sim.joint_spherical_subtype then
                data.jointM=sim.getJointMatrix(obj)
            else
                data.jointPos=sim.getJointPosition(obj)
            end
        end
        --]]
        initialState[obj]=data
    end
    sim.setThreadAutomaticSwitch(l)
    -----------------------------------------------
end

function restore()
    -- Restore the model to its original position/orientation:
    ----------------------------------------------------
    local l=sim.setThreadAutomaticSwitch(false) -- in case this runs in a thread
    for handle, data in pairs(initialState) do
        sim.setObjectPose(handle,sim.handle_parent,data.localPose)
        -- e.g. you could also take into account the joints' initial states:
        --[[
        if data.jointPos then
            sim.setJointPosition(handle,data.jointPos)
        end
        if data.jointM then
            sim.setSphericalJointMatrix(handle,data.jointM)
        end
        --]]
        sim.resetDynamicObject(handle)
    end
    sim.setThreadAutomaticSwitch(l)
    ----------------------------------------------------
end

function sysCall_init()
    modelHandle=sim.getObjectHandle("robotModel")
    memorize()
end
Cheers

Post Reply