- Offers safe, low-level Rust wrappers for the entire MuJoCo API.
- Provides getters for all struct fields and setters for user-modifiable fields, instead of allowing direct field access.
- Implements automatic resource management via Rust's RAII pattern.
For example,
mjModelautomatically callsmj_deleteModelin itsDropimplementation, preventing memory leaks. - Emphasizes type-safety and efficiency within a lean binding interface.
For instance, the
ObjectId<T>type system replaces raw integer IDs to:- Ensure compile-time safety for all object-related operations.
- Improve performance by eliminating the need for repeated
mj_name2idlookups.
- MuJoCo 3.3.2 downloaded and installed
- Additionally, if you place mujoco library in a non-standard directory of the platform,
you need
MUJOCO_LIBenvironment variable set to the path of the directory containinglibmujoco.soormujoco.lib(e.g.$HOME/.mujoco/mujoco-3.3.2/libwhen you placed the official release above in~/.mujoco)
-
For example on x86_64 Linux, run:
wget https://github.com/google-deepmind/mujoco/releases/download/3.3.2/mujoco-3.3.2-linux-x86_64.tar.gz tar -xzf mujoco-3.3.2-linux-x86_64.tar.gz
to download & expand MuJoCo 3.3.2.
On other platforms, do the same with the appropriate archive file for your system. -
One way to setup is to install MuJoCo to a default standard path like
/usr/local/lib/(or a folder in PATH on Windows), then if needed create symlink tomujoco-3.3.2/lib/libmujoco.sothere, and insert to your shell config file:# example on Linux with /usr/local/lib/ export MUJOCO_LIB="/usr/local/lib/mujoco-3.3.2/lib"
Or if you'd like to avoid to install MuJoCo to such a system directory:
# example on Linux with $HOME/.mujoco/ export MUJOCO_LIB="$HOME/.mujoco/mujoco-3.3.2/lib" export LD_LIBRARY_PATH="$MUJOCO_LIB:$LD_LIBRARY_PATH"
-
Or, you can get MuJoCo library through Python toolchain like
uvorpip. -
Depending on your setting, be sure to specify
$MUJOCO_LIBas shared library path when executing your app (for exampleLD_LIBRARY_PATH=$MUJOCO_LIB cargo runon Linux)
[dependencies]
rusty_mujoco = "0.2"
glfw = "0.60"use rusty_mujoco::{mj_loadXML, mj_makeData, mj_name2id, mj_step, mjr_render, mjv_updateScene};
use rusty_mujoco::{mjrContext, mjrRect, mjvScene, mjvCamera, mjvOption, mjtCatBit, mjtFontScale};
let xml_path: String = todo!();
let camera_name: Option<String> = todo!();
let model = mj_loadXML(xml_path).expect("Failed to load XML file");
let mut data = mj_makeData(&model);
let mut glfw = glfw::init(glfw::fail_on_errors).expect("Failed to initialize GLFW");
let (mut window, _) = glfw
.create_window(1200, 900, "Acrobot Simulation", glfw::WindowMode::Windowed)
.expect("Failed to create GLFW window");
window.set_size_polling(true);
window.set_close_polling(true);
glfw::Context::make_current(&mut *window);
let con = mjrContext::new(&model, mjtFontScale::X150);
let opt = mjvOption::default();
let mut scn = mjvScene::new(&model, 2000);
let mut cam = mjvCamera::default();
camera_name.map(|name| cam.set_fixedcamid(mj_name2id(&model, &name).expect("No camera of such name in the model")));
while !window.should_close() {
while data.time() < glfw.get_time() {
mj_step(&model, &mut data);
}
let viewport = {
let (width, height) = window.get_framebuffer_size();
mjrRect::new(0, 0, width as u32, height as u32)
};
mjv_updateScene(
&model,
&mut data,
&opt,
None, /* No perturbation */
&mut cam,
mjtCatBit::ALL,
&mut scn,
);
mjr_render(viewport, &mut scn, &con);
glfw::Context::swap_buffers(&mut *window);
glfw.poll_events();
}See examples/visualize_left_object.rs for full example and examples/README.md for the description.
- TheButlah/mujoco-rs : archived by the owner on Sep 19, 2021.
- MuJoCo-Rust/MuJoCo-Rust : seems not maintained anymore (last commit on 2 years ago) and not compatible with MuJoCo 3+.
rusty_mujoco is licensed under Apache Lisense, Version 2.0).