Skip to content

Add named element accessors to mjx.Model#3034

Open
Shivam-Bhardwaj wants to merge 1 commit intogoogle-deepmind:mainfrom
Shivam-Bhardwaj:add-mjx-named-accessors
Open

Add named element accessors to mjx.Model#3034
Shivam-Bhardwaj wants to merge 1 commit intogoogle-deepmind:mainfrom
Shivam-Bhardwaj:add-mjx-named-accessors

Conversation

@Shivam-Bhardwaj
Copy link

Summary

Add convenience methods to mjx.Model for accessing elements by name, similar to mujoco.MjModel.

Closes #2806

Changes

  • Add BodyView, JointView, GeomView, SiteView, ActuatorView dataclasses
  • Add body(), joint(), geom(), site(), actuator() methods to Model
  • Add unit tests for all accessor methods

Example usage

import mujoco
from mujoco import mjx

model = mujoco.MjModel.from_xml_string(xml)
modelx = mjx.put_model(model)

# Access elements by name (similar to MjModel)
body = modelx.body("body1")
print(body.id)    # same as model.body("body1").id
print(body.name)  # "body1"

joint = modelx.joint("joint1")
geom = modelx.geom("geom1")
actuator = modelx.actuator("motor1")

Test plan

  • Added unit tests comparing results with mujoco.MjModel accessors
  • Tests verify KeyError is raised for non-existent elements
  • Ran pyink and isort for style compliance

Add convenience methods to mjx.Model for accessing elements by name,
similar to mujoco.MjModel:
- body(name) -> BodyView
- joint(name) -> JointView
- geom(name) -> GeomView
- site(name) -> SiteView
- actuator(name) -> ActuatorView

Each returns a view object with id and name properties. Raises KeyError
if the element is not found.

Closes google-deepmind#2806
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Named element accessors for mjx.Model

1 participant