Page 1 of 1
How to retrieve a "SimTK::Force::Gravity" object in Python scripting ?
Posted: Mon Nov 28, 2022 7:23 am
by jsn-74
Hi everyone,
I'd like to retrieve the "SimTK::Force::Gravity" object in a Python script from the current osim.Model.
Code: Select all
mdl = osim.Model(xxx)
state = mdl.initSystem()
gf = mdl.getGravityForce()
bf = gf.getBodyForces()
gf is correctly assigned : <Swig Object of type 'SimTK::Force::Gravity *' at 0x00000195939137E0>
but, when I try to retrieve the set of gravitational forces, I get the error : "AttributeError: 'SwigPyObject' object has no attribute 'getBodyForces'".
Would you please tell me what I'm doing wrong ? Shall I downcast the pointer/reference to another Opensim class ? Which one ?
Thanks in advance
Re: How to retrieve a "SimTK::Force::Gravity" object in Python scripting ?
Posted: Mon Nov 28, 2022 1:00 pm
by aymanh
Hello,
Most internal classes of simbody are not exposed to the scripting interface as they were rarely needed (if ever) by our users. Instead we expose methods that live on the OpenSim side/wrapper. Can you explain the use case for why you need access to the SimTK::Force::Gravity associated with the model?
Thank you,
-Ayman
Re: How to retrieve a "SimTK::Force::Gravity" object in Python scripting ?
Posted: Fri Dec 09, 2022 3:33 am
by jsn-74
Thank you very much Ayman for your message. Basically, I'd like to compensate for gravity torque in the residual torque capacity of a body. I hoped I could retrieve directly this gravity torque, without having to "manually" compute the relative position of its CoM and compute associated jacobians, etc. Is there another API which returns this quantity ? Thank you very much for your help, best regards
Re: How to retrieve a "SimTK::Force::Gravity" object in Python scripting ?
Posted: Fri Feb 03, 2023 7:41 am
by jsn-74
aymanh wrote: ↑Mon Nov 28, 2022 1:00 pm
Most internal classes of simbody are not exposed to the scripting interface as they were rarely needed (if ever) by our users. Instead we expose methods that live on the OpenSim side/wrapper.
Hi Mr Habib and OpenSim users.
Would you please explain me how to expose this simTK interface ? I guess it "only" consists in creating a new Swig interface file or adding a few lines in an existing one in the opensim folder "opensim-core-4.4\Bindings\SWIGSimTK" ? The needed simTK function is defined in the 2 files Force_Gravity.cpp (line 426) and Force_Gravity.h. Could someone guide me in this process ? Thanks in advance.
Best regards
JSn
Re: How to retrieve a "SimTK::Force::Gravity" object in Python scripting ?
Posted: Thu Feb 23, 2023 12:43 pm
by aymanh
Hi Jonathan,
Sorry it took us a while to get back to you on this.
The core issue is that exposing any class in the SimTK hierarchy to SWIG requires exposing all the base classes for it as well, so SimtK::ForceGravity requires SimTK::Force which in turn requires the PIMPL handle classes that are intended to be internal.
I think a more reasonable approach for you would be to piggyback on existing bindings/wrapping mechanism to inject new utility class (or extend the Model or other relevant C++ classes, not recommended) in the python_simulation.i file by adding C++ code that has access to these simbody internals without having to bring the whole class hierarchy across the native interface thus avoiding all the side-effects and cross-language compatibility issues that have been mostly addressed by SWIG. Adding a new class would be cleaner since you can continue to use the bindings with the GUI and other OpenSim tools/products without rebuilding it all from scratch and would generalize better.
Please let me know if you need help with that, or open an issue on github so that we can document the steps clearly for future community members in the same boat.
Best regards,
-Ayman
Re: How to retrieve a "SimTK::Force::Gravity" object in Python scripting ?
Posted: Fri Feb 24, 2023 9:14 am
by jsn-74
Dear Ayman,
thank you very much for your answer. I haven't been coding in C/C++ for years, so I'm not very comfortable in carrying out the task alone. Please enclosed find a tar.gz archive with minimum test code (no swig interface yet but it shall not be to complicated with such a light code). I have to remind out to configure the dependencies in the make process (including header files) so it is not usable yet but I feel keen on improving it, with your help if possible. Can we go on via private mails in order to go further without annoying the forum's readers ?
Best regards
J. Savin