1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! Example of the native Rust viewer implementation.
//! This can only be run in passive mode, which means the user program is the one
//! controlling everything.
//!
//! This example uses the viewer in single-threaded fashion.
use std::time::Duration;
use mujoco_rs::viewer::MjViewer;
use mujoco_rs::prelude::*;
const EXAMPLE_MODEL: &str = stringify! {
<mujoco>
<worldbody>
<light ambient="0.2 0.2 0.2" pos=".2 .2 .2"/>
<body name="ball">
<geom name="green_sphere" size=".1" rgba="0 1 0 1"/>
<joint name="sphere_joint" type="free"/>
</body>
<geom name="floor" type="plane" size="10 10 1" euler="5 0 0"/>
</worldbody>
</mujoco>
};
fn main() {
let model = MjModel::from_xml_string(EXAMPLE_MODEL).expect("could not load the model");
let mut data = MjData::new(&model);
let mut viewer = MjViewer::builder()
.max_user_geoms(0)
.build_passive(&model).unwrap();
/* Add a custom UI window */
#[cfg(feature = "viewer-ui")]
let mut opened = true; // gets moved into the callback
#[cfg(feature = "viewer-ui")]
viewer.add_ui_callback_detached(move |ctx| {
use mujoco_rs::viewer::egui;
egui::Window::new("Custom controls")
.scroll(true)
.open(&mut opened)
.show(ctx, |ui| {
ui.heading("My Custom Widget");
ui.label("This is a custom UI element!");
if ui.button("Click me").clicked() {
println!("Button clicked!");
}
});
});
// OR, to gain access to the internal passive data at the same time:
// viewer.add_ui_callback(move |ctx, _data| {
// use mujoco_rs::viewer::egui;
// egui::Window::new("Custom controls")
// .scroll(true)
// .open(&mut opened)
// .show(ctx, |ui| {
// ui.heading("My Custom Widget");
// ui.label("This is a custom UI element!");
// if ui.button("Click me").clicked() {
// println!("Button clicked!");
// }
// });
// });
let timestep = model.opt().timestep;
while viewer.running() {
data.step();
viewer.sync_data(&mut data);
viewer.render().unwrap();
// Sleep for approximately timestep of seconds.
// Use Instant::now() and Instant::elapsed() in a while loop for a more accurate (but less efficient) wait.
std::thread::sleep(Duration::from_secs_f64(timestep));
}
}