google-deepmind / mujoco

Multi-Joint dynamics with Contact. A general purpose physics simulator.
https://mujoco.org
Apache License 2.0
7.51k stars 741 forks source link

Improve reflection capabilities in mjSpec API #1752

Closed juan-g-bonilla closed 1 day ago

juan-g-bonilla commented 1 week ago

Is your feature request related to a problem? Please describe. I am working on using mujoco as the multi-body dynamics engine for a larger simulation software. The future release of mjSpec will be extremely useful for our use case, which is users loading some spec (XML, URDF, etc), the program reflecting on said spec, and modifying it as needed. For example, we have wrappers for mujoco actuators so that the force/torque they produce can be set through models in our software.

We had a previous implementation working by reading and rewriting the original XML and reloading that (I know...), so an officially supported mjSpec wy will go a long way. I have been toying with the latest version in main, and while creating an mjSpec from scratch seems to work great, it is difficult to introspect on existing mjSpec. Particularly, I find it impossible to find and safely access existing elements using the API.

Describe the solution you'd like I'd like methods to safely cast a mjsElement* to the specific mjsXYZ* class:

mjsBody* mjs_asBody(mjsElement* element);

With these methods and the existing mjs_firstChild/mjs_nextChild, one should be able to traverse the body tree and access/modify all elements contained in it.

The other capability I'd like to see is a way to iterate over non-tree elements, like actuators or sensors.

Given that the API is a WIP, some of these features might be planned (or even already available, and I'm just not seeing them).

Thank you for all the good work on mujoco!

yuvaltassa commented 1 week ago

Yes, these features are planned, but we have not gotten to them yet.

Thanks for bringing this to our attention! Leaving open until this is resolved.

yuvaltassa commented 3 hours ago

Now added. Please let us know what other issues you encounter!