Implementation:Google deepmind Mujoco mj loadXML
| Knowledge Sources | |
|---|---|
| Domains | Physics_Simulation, Model_IO |
| Last Updated | 2026-02-15 06:00 GMT |
Overview
Concrete tool for loading and compiling MuJoCo models from MJCF XML or URDF files provided by the MuJoCo C API.
Description
The mj_loadXML function is the primary entry point for loading MuJoCo models. It parses an XML model file (MJCF or URDF), compiles it into an mjModel structure, and returns a pointer to the compiled model. Internally, it calls ParseXML to build a specification tree, then mj_compile to produce the runtime model. An optional Virtual File System (VFS) allows loading resources from memory rather than disk.
Usage
Use this function at the start of any MuJoCo simulation to load a model from an XML file. This is the standard entry point for both MJCF and URDF formats.
Code Reference
Source Location
- Repository: mujoco
- File: src/xml/xml_api.cc
- Lines: 40-70
Signature
mjModel* mj_loadXML(const char* filename, const mjVFS* vfs,
char* error, int error_sz);
Import
#include <mujoco/mujoco.h>
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| filename | const char* | Yes | Path to MJCF XML or URDF model file |
| vfs | const mjVFS* | No | Virtual file system for in-memory resources (NULL for disk) |
| error | char* | No | Buffer for error/warning messages (NULL to skip) |
| error_sz | int | No | Size of error buffer in bytes |
Outputs
| Name | Type | Description |
|---|---|---|
| return | mjModel* | Compiled physics model (NULL on failure) |
| error | char* | Populated with error or warning message if applicable |
Usage Examples
Basic Model Loading
#include <mujoco/mujoco.h>
// Load model from XML file
char error[1000] = "";
mjModel* m = mj_loadXML("humanoid.xml", NULL, error, 1000);
if (!m) {
printf("Load error: %s\n", error);
return 1;
}
// Use the model...
// Cleanup
mj_deleteModel(m);
Loading with Virtual File System
#include <mujoco/mujoco.h>
// Create and populate VFS
mjVFS vfs;
mj_defaultVFS(&vfs);
mj_addFileVFS(&vfs, ".", "model.xml");
// Load from VFS
char error[1000] = "";
mjModel* m = mj_loadXML("model.xml", &vfs, error, 1000);
// Cleanup
mj_deleteVFS(&vfs);
if (m) mj_deleteModel(m);