File size: 1,231 Bytes
33c14d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Open the chess scene in the interactive MuJoCo viewer, arm at home, window brought to front.

Run with plain python (mjpython crashes the blocking viewer on macOS):
    .venv/bin/python sim/view_scene.py
"""
from __future__ import annotations

import subprocess
import sys
import threading
import time
from pathlib import Path

import mujoco
import mujoco.viewer

HERE = Path(__file__).resolve().parent


def bring_to_front():
    time.sleep(2.5)
    if sys.platform == "darwin":
        subprocess.run(["osascript", "-e", 'tell application "System Events" to set frontmost of first process whose name is "Python" to true'],
                       capture_output=True)


def main():
    m = mujoco.MjModel.from_xml_path(str(HERE / "chess_scene.xml"))
    d = mujoco.MjData(m)
    mujoco.mj_resetDataKeyframe(m, d, m.key("home").id)
    print(f"scene loaded: {m.nbody} bodies, {m.ncam} cameras. Opening viewer window titled 'MuJoCo'...", flush=True)
    print("If you don't see it, check Mission Control (F3) or the Dock for a Python icon.", flush=True)
    threading.Thread(target=bring_to_front, daemon=True).start()
    mujoco.viewer.launch(m, d)
    print("viewer closed", flush=True)


if __name__ == "__main__":
    main()