rust_viewer/
rust_viewer.rs

1//! Example of the native Rust viewer implementation.
2//! This can only be run in passive mode, which means the user program is the one
3//! controlling everything.
4use std::time::Duration;
5
6use mujoco_rs::viewer::MjViewer;
7use mujoco_rs::prelude::*;
8
9
10const EXAMPLE_MODEL: &str = "
11<mujoco>
12  <worldbody>
13    <light ambient=\"0.2 0.2 0.2\"/>
14    <body name=\"ball\">
15        <geom name=\"green_sphere\" pos=\".2 .2 .2\" size=\".1\" rgba=\"0 1 0 1\"/>
16        <joint type=\"free\"/>
17    </body>
18
19    <geom name=\"floor\" type=\"plane\" size=\"10 10 1\" euler=\"5 0 0\"/>
20
21  </worldbody>
22</mujoco>
23";
24
25fn main() {
26    let model = MjModel::from_xml_string(EXAMPLE_MODEL).expect("could not load the model");
27    let mut data = model.make_data();  // or MjData::new(&model);
28    let mut viewer = MjViewer::launch_passive(&model, 100)
29        .expect("could not launch the viewer");
30    while viewer.running() {
31        viewer.sync(&mut data);
32        data.step();
33        std::thread::sleep(Duration::from_millis(2));
34    }
35}