Implementation:Google deepmind Dm control MJCF Find Modify
| Metadata | |
|---|---|
| Knowledge Sources | dm_control |
| Domains | Physics Simulation, Robotics, Model Introspection |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for searching elements within an MJCF model tree by namespace and identifier via element.find() and element.find_all(), and modifying their attributes through Python attribute setters.
Description
element.find(namespace, identifier)
Locates a single element by its namespace (e.g., 'body', 'joint', 'geom') and string identifier within the subtree rooted at element. The implementation:
- Validates that both
namespaceandidentifierare strings and thatnamespaceis in the set of findable namespaces. - If
identifiercontains a/separator, the first segment is interpreted as the name of an attached model scope. The search delegates to that attached model'sfind()with the remainder of the identifier. - If
identifierhas no separator, the element's ownnamescopedictionary is queried. The result is verified to be an actual descendant of the calling element (by walking up the parent chain). If the found element is not within the subtree,Noneis returned.
element.find_all(namespace, immediate_children_only=False, exclude_attachments=False)
Returns a list of all elements matching the given namespace within the subtree. The implementation performs a depth-first traversal of the element's children. Elements match if their spec's namespace equals the query namespace, or if they are attachment frames when the query namespace is 'body'. The immediate_children_only flag limits the search to direct children. The exclude_attachments flag restricts traversal to own children (excluding elements from attached models).
Attribute modification
Once an element is found, attributes are read via element.attr_name and written via element.attr_name = value. Both operations go through the __getattr__/__setattr__ overrides on _ElementImpl, which delegate to the typed attribute objects. Attributes can be deleted with del element.attr_name, which calls __delattr__ and clears the attribute value (raising an error if the attribute is required). Repeated children can be cleared with element.child_type.clear().
The remove() method on any element removes it from the tree entirely (for repeated or on-demand elements) or clears all its attributes (for singleton children).
Usage
Use find() when the target element's name is known. Use find_all() when iterating over all elements of a type. Combine with attribute setters for post-hoc model modification.
Code Reference
| Property | Value |
|---|---|
| Source Location | dm_control/mjcf/element.py:L399-498 (find, find_all), dm_control/mjcf/element.py:L560-697 (get/set/del attributes, add, remove), dm_control/mjcf/attribute.py (attribute types)
|
| Signature (find) | find(namespace: str, identifier: str) -> Element or None
|
| Signature (find_all) | find_all(namespace: str, immediate_children_only=False, exclude_attachments=False) -> list[Element]
|
| Signature (remove) | remove(affect_attachments=False) -> None
|
| Import | from dm_control import mjcf
|
I/O Contract
Inputs (find):
| Parameter | Type | Description |
|---|---|---|
namespace |
str |
The MJCF namespace to search (e.g., 'body', 'joint', 'geom', 'actuator', 'site', 'sensor').
|
identifier |
str |
The name of the element. May include / to search into attached model scopes.
|
Inputs (find_all):
| Parameter | Type | Description |
|---|---|---|
namespace |
str |
The MJCF namespace to search. |
immediate_children_only |
bool |
If True, only direct children are returned (default False).
|
exclude_attachments |
bool |
If True, elements from attached models are excluded (default False).
|
Outputs:
| Output | Type | Description |
|---|---|---|
| return value (find) | mjcf.Element or None |
The matching element, or None if not found.
|
| return value (find_all) | list[mjcf.Element] |
All matching elements in depth-first order. |
Usage Examples
from dm_control import mjcf
# Parse a model
model = mjcf.from_path('/path/to/humanoid.xml')
# Find a specific body by name
torso = model.find('body', 'torso')
print(torso.pos) # Read position attribute
# Find a joint and modify its properties
right_hip = model.find('joint', 'right_hip')
if right_hip is not None:
right_hip.damping = 5.0
right_hip.range = [-1.57, 1.57]
# Find all geoms in the model
all_geoms = model.find_all('geom')
for geom in all_geoms:
geom.rgba = [0.5, 0.5, 0.5, 1.0] # Make all geoms grey
# Find all joints that are immediate children of a specific body
torso_joints = torso.find_all('joint', immediate_children_only=True)
# Search into an attached model by scoped identifier
# If 'robot' model was attached to 'arena':
robot_joint = model.find('joint', 'robot/elbow')
# Find all bodies excluding those from attached models
own_bodies = model.find_all('body', exclude_attachments=True)
# Remove an element from the tree
sensor = model.find('sensor', 'accelerometer')
if sensor is not None:
sensor.remove()
# Delete an optional attribute
torso = model.find('body', 'torso')
if torso is not None:
del torso.pos # Clears the pos attribute