Page 1 of 1

API: Loading model states from an Inverse Kinematics motion file

Posted: Wed Jun 20, 2018 9:25 am
by navid.roux
I have an OpenSim model file and an accompanying motion file from a prior inverse kinematics calculation. Now I would like to programmatically "animate" the model such that I get access to transformations for every body in the bodyset in every time step.

Unfortunately, I fail to see how to link Model, Storage and State instances API-wise. My code can already output the transformation for the initial state, but how do I load a new state from a specific row of the motion file?

I already browsed the API documentation several times, but all I could find was a way to manually set the coordintes as per one of the Python bindings examples:
model.setStateVariableValue(state, 'joint/q/value', 0.25 * math.pi)

Surely, there must be a better and more automatic way, I think. OpenSim's GUI also loads motion files, hence I suppose this logic has already been implemented, but I was only unable to find it.

Code: Select all

import opensim as osim

model = osim.Model("model.osim")
model.buildSystem()
state = model.initializeState()

motion_storage = osim.Storage("ik.mot")

# Acquiring the transformation for a specific body in the bodyset already works
model.getBodySet().get(0).getTransformInGround(state)
Thanks in advance!

Re: API: Loading model states from an Inverse Kinematics motion file

Posted: Fri Jun 22, 2018 8:37 pm
by hkimpara
Hopefully, following example based on MATLAB may give you some hints:

Code: Select all

osimModel = Model('model.osim');
osimState = osimModel.initSystem();

% Read IK post data
Des_StoreData = Storage(IK_output_file);

Des_Time = ArrayDouble();
Des_Size = Des_StoreData.getTimeColumn (Des_Time);

for i = 1:num_Coordinates
    coordvalue = ArrayDouble();
    Des_StoreData.getDataColumn(char(Coordinates_Name{i,1}),coordvalue);
    coordvect = coordvalue.getAsVector;

    motType = model.getCoordinateSet().get(Coordinates_Name{i,1}).getMotionType();
    
    % Check unit for "Rotational" or "Coupled" coordinate in your case: rad or degree
    if strcmp(motType,'Rotational')
        Des_Data_Array(i,:) = deg2rad(osimVectorToArray(coordvect));
    else
        Des_Data_Array(i,:) = osimVectorToArray(coordvect);
    end
end

t = target_time;
for j = 1:num_Coordinates
	osimModel.updCoordinateSet.get(Coordinates_Name{j,1}).setValue(osimState,Des_Data_Array(j,t));
end


Re: API: Loading model states from an Inverse Kinematics motion file

Posted: Fri Jul 19, 2019 5:50 am
by mel_asfa
Hey Navid,

Did you manage to animate your model with your IK results ? I'm trying to do it, a little help would be great :D
Thanks in advance