This is a rust interface to MJCF files. MJCF is the modeling language used by
MuJoCo.
This uses the serde library to convert between the
XML-based MJCF files and native rust data-structures.
For more information about the MJCF file format, see the official documentation:
https://mujoco.readthedocs.io/en/stable/XMLreference.html
-
Install from crates.io:
cargo add mjcf
-
Read the documentation:
cargo doc --package mjcf --open
And the corresponding MJCF XML Reference. -
Use the function
mjcf::from_str
to parse an MJCF XML string into the native rust data structures.
Use the functionmjcf::to_string
to serialize the rust data structures into an MJCF XML string.
let mjcf_string = std::fs::read_to_string("my_mujoco_model.xml").unwrap();
// Deserialize the string into rust struct's.
let mut mujoco = mjcf::from_str(&mjcf_string).unwrap();
// Let's add another body to this model.
let worldbody = mujoco.worldbody.as_mut().unwrap();
let mut new_body = mjcf::Body::default();
new_body.name = "my_body".to_string();
worldbody.body.push(new_body);
// Serialize the modified data into a string.
let mjcf_string = mjcf::to_string(&mujoco).unwrap();
std::fs::write("modified_mujoco_model.xml", mjcf_string).unwrap();
This library is tested using MJCF files which were scraped from the internet. In
order to run the tests you need to clone this GIT repository and all of its
submodules using the following command:
git clone --recurse-submodules