How to retrieve a "SimTK::Force::Gravity" object in Python scripting ?

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Jonathan SAVIN
Posts: 11
Joined: Wed Feb 10, 2021 12:52 am

How to retrieve a "SimTK::Force::Gravity" object in Python scripting ?

Post by Jonathan SAVIN » Mon Nov 28, 2022 7:23 am

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 :-)

Tags:

User avatar
Ayman Habib
Posts: 2248
Joined: Fri Apr 01, 2005 12:24 pm

Re: How to retrieve a "SimTK::Force::Gravity" object in Python scripting ?

Post by Ayman Habib » Mon Nov 28, 2022 1:00 pm

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

User avatar
Jonathan SAVIN
Posts: 11
Joined: Wed Feb 10, 2021 12:52 am

Re: How to retrieve a "SimTK::Force::Gravity" object in Python scripting ?

Post by Jonathan SAVIN » Fri Dec 09, 2022 3:33 am

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

User avatar
Jonathan SAVIN
Posts: 11
Joined: Wed Feb 10, 2021 12:52 am

Re: How to retrieve a "SimTK::Force::Gravity" object in Python scripting ?

Post by Jonathan SAVIN » Fri Feb 03, 2023 7:41 am

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

User avatar
Ayman Habib
Posts: 2248
Joined: Fri Apr 01, 2005 12:24 pm

Re: How to retrieve a "SimTK::Force::Gravity" object in Python scripting ?

Post by Ayman Habib » Thu Feb 23, 2023 12:43 pm

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

User avatar
Jonathan SAVIN
Posts: 11
Joined: Wed Feb 10, 2021 12:52 am

Re: How to retrieve a "SimTK::Force::Gravity" object in Python scripting ?

Post by Jonathan SAVIN » Fri Feb 24, 2023 9:14 am

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
Attachments
BuildGravityWrapper.tar.gz
(7.69 KiB) Downloaded 54 times

POST REPLY