Principle:Google deepmind Dm control MJCF Model Export
| Metadata | |
|---|---|
| Knowledge Sources | dm_control |
| Domains | Physics Simulation, Robotics, Model Serialization |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Serializing an in-memory physics model and its associated binary assets to portable file formats enables reproducible sharing, archival, and loading of simulation environments.
Description
MJCF Model Export is the final step in many model composition workflows. After a model has been created, populated, and composed from multiple sub-models, it often needs to be written to disk in a form that can be loaded by standard MuJoCo tools, shared with collaborators, or archived for reproducibility.
The export process must handle two distinct concerns:
- XML serialization: The in-memory MJCF object tree is converted to a well-formed XML string. During serialization, all identifiers are fully qualified with hierarchical scope prefixes so that the resulting XML is self-contained and unambiguous. Floating-point values are formatted to a configurable precision. Values below a configurable zero threshold are snapped to exactly zero to avoid numerical noise in the output.
- Asset collection: Binary assets (mesh files, texture images, heightfield data, skin files) referenced by the model are gathered into a dictionary keyed by their Virtual File System (VFS) filenames. The VFS filename is typically a content-addressed hash of the file's bytes, ensuring that identical assets are deduplicated and that filename collisions are impossible.
There are three export modalities:
- To XML string: Serialize the model to a string for in-memory use, passing to MuJoCo's compiler, or custom I/O.
- To directory: Write the XML file and all assets as individual files in a specified directory.
- To zip archive: Write the XML file and all assets into a single compressed zip file, with an internal directory structure.
All three modalities produce output that can be loaded directly by MuJoCo or re-imported using PyMJCF's parser functions.
Usage
Model export is used whenever a model needs to leave the Python process:
- Saving a composed model for later re-use.
- Sharing a model with a colleague or publishing it alongside a research paper.
- Exporting for visualization in MuJoCo's standalone viewer.
- Writing checkpoints of procedurally generated environments.
- Creating distributable model archives (zip files) that bundle all dependencies.
Theoretical Basis
FUNCTION export_to_directory(mjcf_model, out_dir, file_name):
IF file_name IS None:
file_name = mjcf_model.model + '.xml'
ASSERT file_name ends with '.xml'
assets = mjcf_model.get_assets() # {vfs_name: bytes}
assets[file_name] = mjcf_model.to_xml_string()
create_directory(out_dir)
FOR (name, contents) IN assets:
write_binary(out_dir / name, contents)
FUNCTION export_to_zip(mjcf_model, out_dir, model_name):
IF model_name IS None:
model_name = mjcf_model.model
xml_name = model_name + '.xml'
zip_name = model_name + '.zip'
files = mjcf_model.get_assets()
files[xml_name] = mjcf_model.to_xml_string()
create_directory(out_dir)
zip = new ZipFile(out_dir / zip_name)
FOR (name, contents) IN files:
zip.write(model_name / name, contents)
RETURN out_dir / zip_name
The key property is completeness: the exported output is self-contained. Every binary asset referenced in the XML is included in the output, and the XML references use the VFS filenames that match the exported asset files. This means the output can be loaded in any environment without needing access to the original file paths.