Add bind() method to MjModel and MjData.

This method allows users to bind a spec to a model or data object, making it less verbose to access its arrays.

PiperOrigin-RevId: 699952211
Change-Id: I6b71a88d5a6968bc0f902bd49db7c568b46a4dfb
This commit is contained in:
Alessio Quaglino
2024-11-25 05:53:03 -08:00
committed by Copybara-Service
parent 13b6055098
commit c6ba8f27fd
6 changed files with 75 additions and 17 deletions
+2 -5
View File
@@ -6,11 +6,6 @@ Changelog
Upcoming version (not yet released)
-----------------------------------
General
^^^^^^^
- Removed id attribute from :ref:`mjSpec` objects in Python bindings. Using ids is error prone in scenarios of repeated
attachment and detachment. Python users are encouraged to use names for unique identification of model elements.
MJX
^^^
- Added muscle actuators.
@@ -18,6 +13,8 @@ MJX
Python bindings
^^^^^^^^^^^^^^^
- Provide prebuilt wheels for Python 3.13.
- Added ``bind`` method and removed id attribute from :ref:`mjSpec` objects. Using ids is error prone in scenarios of repeated attachment and
detachment. Python users are encouraged to use names for unique identification of model elements.
Bug fixes
^^^^^^^^^
+8 -7
View File
@@ -585,16 +585,17 @@ Model Editing
includes a reimplementation of the ``PyMJCF`` example in the ``dm_control``
`tutorial notebook <https://github.com/google-deepmind/dm_control/blob/main/dm_control/mjcf/tutorial.ipynb>`__.
``PyMJCF`` provides a notion of "binding", giving access to :ref:`mjModel` and :ref:`mjData` values via the constructing
elements. In the native API, this is done with object ids. For example, say we have multiple geoms containing the string
"torso" in their name. We want to get their Cartesian positions in the XY plane from ``mjData``. This can be done as
follows:
``PyMJCF`` provides a notion of "binding", giving access to :ref:`mjModel` and :ref:`mjData` values via a helper class.
In the native API, the helper class is not needed, so it is possible to directly bind an ``mjs`` object to
:ref:`mjModel` and :ref:`mjData`. This requires the objects to have a non-empty name. For example, say we have multiple
geoms containing the string "torso" in their name. We want to get their Cartesian positions in the XY plane from
``mjData``. This can be done as follows:
.. code-block:: python
torsos = [geom.id for geom in spec.geoms if 'torso' in geom.name]
pos_x = data.geom_xpos[torsos, 0]
pos_y = data.geom_xpos[torsos, 1]
torsos = [data.bind(geom) for geom in spec.geoms if 'torso' in geom.name]
pos_x = [torso.xpos[0] for torso in torsos]
pos_y = [torso.xpos[1] for torso in torsos]
Notes
-----