Page 1 of 1

getMultibodySystem().realize in python [SOLVED]

Posted: Mon Oct 22, 2018 12:03 pm
by pariterre
Hello!

I am rewriting the Static Optimization in Python. At some point (at the beginning of a new frame), I need to update the model according to the Q of the state. In a naive approach, I decided that I would create an "osim.State" and populate it with the required Q (something like that:

self.model = osim.Model(model_path)
self.state = osim.State()
self.Q = self.state.getQ() # Get a reference to the Q into that state
self.Q.resize(n_dof_of_the_model)
for i in range(n_dof_of_the_model):
self.Q.set(i, desired_values)

thereafter, I would have called:
self.model.getMultibodySystem().realize(self.state, DESIRED_KINEMATIC_FLAG)

But two problems arose. The first one is "realize" is not callable from the swig API. The second is (when the first will be solved) the FLAGS are not interfaced either.

So my question is: Is there a better way to update the kinematics of a model to a desired pose?

Thanks

Re: getMultibodySystem().realize in python

Posted: Mon Oct 22, 2018 1:50 pm
by aymanh
Hi Benjamin,

Thanks for taking this on, it will be extremely helpful to everybody using python to perform post-hoc analysis.

I'd highly recommend you check the python script under opensim-core (tBindings/Python/tests/test_states_trajectory.py) as it creates a stateTrajectory object and steps through it.

Please let us know if you run into problem locating it.

Best regards,
-Ayman

Re: getMultibodySystem().realize in python

Posted: Mon Oct 22, 2018 3:12 pm
by chrisdembia
Also, it's a good idea to use upQ() if you plan on editing the reference. Also, you should not resize the vectors stored within the State.

Re: getMultibodySystem().realize in python

Posted: Tue Oct 23, 2018 8:05 am
by pariterre
Thanks for your answer.

I was actually on the right track, but I got SegFaults. I thought I was doing things wrong and then started to resize and all. But it turns out, this was related to the other topic I opened which is now solved with a fresh environment.

I'll have a look at the examples! Thank again :)

Re: getMultibodySystem().realize in python

Posted: Tue Oct 23, 2018 8:37 am
by pariterre
Hello all!

For those who may get to this thread in the future (as it is not specifically done in the tests suggested earlier), there is actually an interface to realize:

Code: Select all

model = osim.Model(model_path)
state = self.model.initSystem()
Q = state.getQ()
{modify Q as needed}
model.realizePosition(state)
Thanks for your help