mjc_viewer icon indicating copy to clipboard operation
mjc_viewer copied to clipboard

A browser-based 3D viewer for MuJoCo

mjc_viewer

PyPI Python Version PyPI version Open In Colab

mjc_viewer is a browser-based 3D viewer for MuJoCo that can render static trajectories from JSON.

Installation

The recommended way to install this package is via PyPI:

pip install mjc_viewer

Usage

import numpy as np
import mujoco
from mjc_viewer import Serializer, Trajectory

# Load your MuJoCo model.
model = mujoco.MjModel.from_xml_path("humanoid.xml")
data = mujoco.MjData(model)

# Create a Serializer and Trajectory instance.
serializer = Serializer(model)
trajectory = Trajectory(data)

# Simulate for 3 seconds.
trajectory.reset()
while data.time < 3.0:
    data.ctrl = np.random.uniform(*model.actuator_ctrlrange.T)
    mujoco.mj_step(model, data)
    trajectory.step()

html = serializer.render(trajectory)
with open("traj.html", "w") as f:
    f.write(html)
# You can now open traj.html in a browser or render in a notebook with
# `IPython.display.HTML`.

Todos

  • [ ] Robustify XML parser
  • [ ] Parse lights
  • [ ] Add support for meshes and height fields
  • [ ] Better default camera / light settings
  • [ ] Figure out plane reflection
  • [ ] Someday, make it interactive

Acknowledgements

mjc_viewer is heavily adapted from Brax's javascript viewer, full credit goes to its developers.