Implementation:Google deepmind Dm control MJCF Export
| Metadata | |
|---|---|
| Knowledge Sources | dm_control |
| Domains | Physics Simulation, Robotics, Model Serialization |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for exporting MJCF model trees and their binary assets to directories or zip archives using export_with_assets(), export_with_assets_as_zip(), and mjcf_model.to_xml_string().
Description
The MJCF Export implementation provides three serialization paths:
1. mjcf_model.to_xml_string(*, precision=17, zero_threshold=0)
Serializes the in-memory MJCF tree to an XML string. The method:
- Calls
to_xml()on the root element, which recursively serialises every element and its attributes into anlxml.etreetree. - During serialization, identifiers are prefixed according to the name scope hierarchy (e.g.,
robot/elbow). Reference attributes are resolved to their fully qualified names. File attributes are replaced with content-addressed VFS filenames. - The etree is converted to a byte string via
etree.tostring()with optional pretty-printing. - The
precisionparameter controls the number of significant digits for floating-point values (default 17 for full double precision). Thezero_thresholdparameter snaps small values to zero.
2. export_with_assets(mjcf_model, out_dir, out_file_name=None, *, precision=17, zero_threshold=0)
Writes the model and all its assets as individual files in a directory:
- Determines the output file name (defaults to
mjcf_model.model + '.xml'). RaisesValueErrorif the name does not end with.xml. - Calls
mjcf_model.get_assets()to collect all binary assets. This method traverses allFileattributes in the model's namescope (and recursively in attached models) and returns a{vfs_filename: bytes}dictionary. - Adds the XML string to the assets dictionary.
- Creates
out_dirif it does not exist. - Writes each entry as a binary file.
3. export_with_assets_as_zip(mjcf_model, out_dir, model_name=None, *, precision=17, zero_threshold=0)
Writes the model and all its assets into a single zip file:
- Determines the model name (defaults to
mjcf_model.model). The zip file is namedmodel_name.zipand contains a directory namedmodel_name/. - Collects assets and the XML string in the same way as the directory export.
- Creates
out_dirif it does not exist. - Writes all files into the zip archive under the
model_name/prefix. - Returns the full path to the created zip file.
Usage
Use to_xml_string() for in-memory serialization. Use export_with_assets() to write a model to a directory for standalone use. Use export_with_assets_as_zip() to create distributable archives.
Code Reference
| Property | Value |
|---|---|
| Source Location | dm_control/mjcf/export_with_assets.py:L24-61, dm_control/mjcf/export_with_assets_as_zip.py:L23-67, dm_control/mjcf/element.py:L817-862 (to_xml_string)
|
| Signature (export_with_assets) | export_with_assets(mjcf_model, out_dir, out_file_name=None, *, precision=17, zero_threshold=0)
|
| Signature (export_with_assets_as_zip) | export_with_assets_as_zip(mjcf_model, out_dir, model_name=None, *, precision=17, zero_threshold=0)
|
| Signature (to_xml_string) | to_xml_string(self, prefix_root=None, self_only=False, pretty_print=True, *, precision=17, zero_threshold=0, filename_with_hash=True) -> str
|
| Import | from dm_control.mjcf import export_with_assets, from dm_control.mjcf import export_with_assets_as_zip
|
I/O Contract
Inputs (export_with_assets):
| Parameter | Type | Description |
|---|---|---|
mjcf_model |
mjcf.RootElement |
The model to export. |
out_dir |
str |
Directory to write the XML and assets. Created if it does not exist. |
out_file_name |
str or None |
Name of the output XML file. Defaults to model_name + '.xml'. Must end with .xml.
|
precision |
int |
Number of significant digits for float output (default 17). |
zero_threshold |
float |
Values with absolute value below this are written as zero (default 0). |
Inputs (export_with_assets_as_zip):
| Parameter | Type | Description |
|---|---|---|
mjcf_model |
mjcf.RootElement |
The model to export. |
out_dir |
str |
Directory for the output zip file. Created if it does not exist. |
model_name |
str or None |
Base name for the zip file, internal directory, and XML file. Defaults to mjcf_model.model.
|
precision |
int |
Number of significant digits for float output (default 17). |
zero_threshold |
float |
Values with absolute value below this are written as zero (default 0). |
Inputs (to_xml_string):
| Parameter | Type | Description |
|---|---|---|
precision |
int |
Significant digits for floats (default 17). |
zero_threshold |
float |
Zero-snap threshold (default 0). |
pretty_print |
bool |
Whether to indent the XML (default True).
|
Outputs:
| Output | Type | Description |
|---|---|---|
| return value (export_with_assets) | None |
Files are written to out_dir.
|
| return value (export_with_assets_as_zip) | str |
Full path to the created zip file. |
| return value (to_xml_string) | str |
The XML content as a string. |
Usage Examples
from dm_control import mjcf
from dm_control.mjcf import export_with_assets as ewa
from dm_control.mjcf import export_with_assets_as_zip as ewa_zip
# Build or load a model
model = mjcf.RootElement(model='my_scene')
body = model.worldbody.add('body', name='box')
body.add('geom', type='box', size=[0.1, 0.1, 0.1])
# 1. Export to XML string
xml_string = model.to_xml_string()
print(xml_string)
# 2. Export to XML string with reduced precision
xml_compact = model.to_xml_string(precision=5, zero_threshold=1e-10)
# 3. Export model and assets to a directory
ewa.export_with_assets(model, out_dir='/tmp/my_scene')
# Creates /tmp/my_scene/my_scene.xml and any asset files
# 4. Export with a custom file name
ewa.export_with_assets(model, out_dir='/tmp/my_scene',
out_file_name='scene_v2.xml')
# 5. Export model and assets as a zip archive
zip_path = ewa_zip.export_with_assets_as_zip(
model, out_dir='/tmp/archives')
print(zip_path) # /tmp/archives/my_scene.zip
# The zip contains: my_scene/my_scene.xml and my_scene/<asset files>
# 6. Export with a custom model name
zip_path = ewa_zip.export_with_assets_as_zip(
model, out_dir='/tmp/archives', model_name='release_v1')
print(zip_path) # /tmp/archives/release_v1.zip