playful / chess-sim /code /sim /view_scene.py
Ali-Uraish's picture
Add files using upload-large-folder tool
33c14d6 verified
Raw History Blame Contribute Delete
1.23 kB
"""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()