Implementation:Google deepmind Dm control MJCF Schema
| Knowledge Sources | |
|---|---|
| Domains | MJCF, Schema Validation, XML Parsing |
| Last Updated | 2026-02-15 04:00 GMT |
Overview
The MJCF Schema module parses MuJoCo's MJCF schema XML into a Python object representation of ElementSpec and AttributeSpec named tuples that drive the entire PyMJCF object model's structure, validation, and attribute typing.
Description
This module serves as the single source of truth for what elements, attributes, and types are valid in the MJCF format. At module load time, the parse_schema function reads schema.xml and recursively builds ElementSpec named tuples containing: element name, repeat/on-demand flags, identifier and namespace information, ordered dictionaries of AttributeSpec entries (mapping to typed attribute classes such as attribute.Identifier, attribute.Reference, attribute.Array, attribute.Keyword, and scalar types), and child element specs. The _parse_attribute helper maps schema type strings to concrete attribute classes and extracts type-specific keyword arguments like valid_values, array_size, reference_namespace, and path_namespace.
The module also constructs specialized attachment frame specs (ATTACHMENT_FRAME and WORLD_ATTACHMENT_FRAME) which are body-like elements without identifiers, used for model attachment in the Composer framework. The world attachment variant additionally permits freejoint children. FINDABLE_NAMESPACES is computed by collecting all namespaces from the parsed schema plus the attachment_frame namespace.
The override_schema function allows replacing the global schema at runtime, which is primarily used for testing purposes. It updates the module-level MUJOCO and FINDABLE_NAMESPACES globals.
Usage
This module is used internally by PyMJCF to validate element creation and attribute assignment. Users may interact with it when they need to inspect the schema programmatically or when overriding the schema for custom MuJoCo extensions.
Code Reference
Source Location
- Repository: Google_deepmind_Dm_control
- File: dm_control/mjcf/schema.py
- Lines: 1-262
Signature
ElementSpec = collections.namedtuple(
'ElementSpec', ('name', 'repeated', 'on_demand', 'identifier',
'namespace', 'attributes', 'children'))
AttributeSpec = collections.namedtuple(
'AttributeSpec', ('name', 'type', 'required', 'conflict_allowed',
'conflict_behavior', 'other_kwargs'))
def parse_schema(schema_path):
"""Parses the schema XML and returns an ElementSpec for the root element."""
def collect_namespaces(root_spec):
"""Constructs a set of namespaces in a given ElementSpec."""
def override_schema(schema_xml_path):
"""Override the schema with a custom xml."""
# Module-level constants
MUJOCO = parse_schema(_SCHEMA_XML_PATH)
FINDABLE_NAMESPACES = frozenset(...)
ATTACHMENT_FRAME = _attachment_frame_spec(is_world_attachment=False)
WORLD_ATTACHMENT_FRAME = _attachment_frame_spec(is_world_attachment=True)
Import
from dm_control.mjcf import schema
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| schema_path | str | Yes (for parse_schema) | Path to the schema XML file |
| root_spec | ElementSpec | Yes (for collect_namespaces) | Root element spec to collect namespaces from |
| schema_xml_path | str | Yes (for override_schema) | Path to a custom schema XML file |
Outputs
| Name | Type | Description |
|---|---|---|
| MUJOCO | ElementSpec | The parsed root element spec representing the complete MJCF schema |
| FINDABLE_NAMESPACES | frozenset | All valid namespaces for find/find_all operations |
| ATTACHMENT_FRAME | ElementSpec | Specialized body spec for non-world attachment frames |
| WORLD_ATTACHMENT_FRAME | ElementSpec | Specialized body spec for world-level attachment frames (includes freejoint) |
Usage Examples
from dm_control.mjcf import schema
# Inspect the root schema
root_spec = schema.MUJOCO
print("Root element:", root_spec.name)
print("Top-level children:", list(root_spec.children.keys()))
# Check available namespaces
print("Findable namespaces:", schema.FINDABLE_NAMESPACES)
# Inspect a specific element spec
body_spec = root_spec.children['worldbody'].children['body']
print("Body attributes:", list(body_spec.attributes.keys()))
print("Body children:", list(body_spec.children.keys()))
# Override schema for testing
schema.override_schema('/path/to/custom_schema.xml')