Skip to content

rust-control/rusty_mujoco

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rusty MuJoCo Binding

Rust bindings for the MuJoCo physics simulator

MuJoCo Version: 3.3.2


  • 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, mjModel automatically calls mj_deleteModel in its Drop implementation, 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_name2id lookups.

Requirements

  • MuJoCo 3.3.2 downloaded and installed
  • Additionally, if you place mujoco library in a non-standard directory of the platform, you need MUJOCO_LIB environment variable set to the path of the directory containing libmujoco.so or mujoco.lib (e.g. $HOME/.mujoco/mujoco-3.3.2/lib when you placed the official release above in ~/.mujoco)

Note / Tips

  • 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 to mujoco-3.3.2/lib/libmujoco.so there, 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 uv or pip.

  • Depending on your setting, be sure to specify $MUJOCO_LIB as shared library path when executing your app (for example LD_LIBRARY_PATH=$MUJOCO_LIB cargo run on Linux)

Example

[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.

Previous Works

License

rusty_mujoco is licensed under Apache Lisense, Version 2.0).

About

A safe, low-level Rust binding for MuJoCo physics simulator

Topics

Resources

License

Stars

Watchers

Forks

Languages