#!/usr/bin/env python3
"""
view_scene.py — open any MuJoCo scene.xml in the passive viewer and simulate it.

Used by `zelpi visualise view` to render a generated world (scene.xml produced by
world_gen.py) so the operator can watch the world settle under physics.

Usage:
  python scripts/view_scene.py <path-to-scene.xml>
"""
from __future__ import annotations

import sys
import time
import pathlib

import mujoco
import mujoco.viewer


def main():
    if len(sys.argv) < 2:
        print("usage: python scripts/view_scene.py <scene.xml>")
        sys.exit(1)
    path = pathlib.Path(sys.argv[1])
    if not path.exists():
        print(f"scene not found: {path}")
        sys.exit(1)

    model = mujoco.MjModel.from_xml_path(str(path))
    data = mujoco.MjData(model)
    print(f"[view_scene] {path.name}: nbody={model.nbody} ngeom={model.ngeom} — close window to exit")

    with mujoco.viewer.launch_passive(model, data) as v:
        v.cam.distance = 2.0
        v.cam.elevation = -20
        v.cam.azimuth = 150
        v.cam.lookat[:] = [0.36, 0.0, 0.45]
        while v.is_running():
            mujoco.mj_step(model, data)
            v.sync()
            time.sleep(model.opt.timestep)


if __name__ == "__main__":
    main()
