Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Google deepmind Dm control MJCF Element Base

From Leeroopedia
Knowledge Sources
Domains MJCF, Object Model, Abstract Base Class
Last Updated 2026-02-15 04:00 GMT

Overview

The MJCF Element base module defines the abstract base class Element that establishes the interface contract for all MJCF element objects in the PyMJCF object model.

Description

The Element class is an abstract base class (using abc.ABCMeta) with __slots__ = [] that declares the complete API for MJCF element objects. It exists separately from the concrete implementation because actual element access in PyMJCF returns weakproxy-like proxies rather than direct references. This design ensures that when an element is removed from the MJCF tree, all outstanding references are automatically invalidated, preventing orphaned non-root elements.

The abstract interface covers five major categories of functionality. Traversal properties include parent, root, tag, spec, and namescope. Lookup methods include find(namespace, identifier) for locating specific elements by namespace and name, find_all(namespace) for collecting all elements of a given kind, and enter_scope(scope_identifier) for navigating to nested scopes. Mutation methods include add(element_name, **kwargs), remove(), set_attributes(), get_attributes(), and get_children(). XML generation methods include to_xml() and to_xml_string() with support for prefix roots, precision control, and zero thresholds. Debugging methods include get_init_stack() and get_last_modified_stacks_for_all_attributes() for tracking element provenance.

The is_same_as(other) method provides semantic equivalence checking between elements, comparing specifications, attribute values, and children recursively, where ordering of non-repeated children is permutation-insensitive.

Usage

Use this class primarily for isinstance checks against MJCF elements. The abstract interface is implemented by the concrete _Element class in element.py, which users interact with through proxy objects.

Code Reference

Source Location

Signature

class Element(metaclass=abc.ABCMeta):
    __slots__ = []

    # Traversal
    @property
    def tag(self): ...
    @property
    def spec(self): ...
    @property
    def parent(self): ...
    @property
    def namescope(self): ...
    @property
    def root(self): ...

    # Lookup
    def find(self, namespace, identifier): ...
    def find_all(self, namespace, immediate_children_only=False,
                 exclude_attachments=False): ...
    def enter_scope(self, scope_identifier): ...

    # Mutation
    def add(self, element_name, **kwargs): ...
    def remove(self, affect_attachments=False): ...
    def set_attributes(self, **kwargs): ...
    def get_attributes(self): ...
    def get_children(self, element_name): ...

    # XML generation
    def to_xml(self, prefix_root=None, debug_context=None, *,
               precision=constants.XML_DEFAULT_PRECISION, zero_threshold=0): ...
    def to_xml_string(self, prefix_root=None, self_only=False,
                      pretty_print=True, debug_context=None, *,
                      precision=constants.XML_DEFAULT_PRECISION,
                      zero_threshold=0): ...

    # Other
    def is_same_as(self, other): ...
    def resolve_references(self): ...
    def prefixed_identifier(self, prefix_root): ...
    @property
    def full_identifier(self): ...
    @property
    def is_removed(self): ...
    def all_children(self): ...

Import

from dm_control.mjcf.base import Element

I/O Contract

Inputs

Name Type Required Description
namespace str Yes (for find/find_all) The namespace to search (e.g., 'joint', 'body', 'actuator')
identifier str Yes (for find) The identifier string of the desired element
element_name str Yes (for add) The tag of the child element to add
prefix_root NameScope/None No Scope object for prefix calculation in XML generation

Outputs

Name Type Description
element Element/None Found element (from find) or None if not found
elements list[Element] List of matching elements (from find_all)
xml etree._Element Generated XML element tree (from to_xml)
xml_string str Generated XML string (from to_xml_string)

Usage Examples

from dm_control import mjcf
from dm_control.mjcf.base import Element

# Create an MJCF model
model = mjcf.RootElement()

# Check if an object is an MJCF Element
body = model.worldbody.add('body', name='my_body')
assert isinstance(body, Element)

# Use Element API methods
joint = body.add('joint', name='my_joint', type='hinge')
found = model.find('joint', 'my_joint')
all_joints = model.find_all('joint')

# Generate XML
xml_string = model.to_xml_string(pretty_print=True)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment