Skip to content

[Won't Merge] Refactoring #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions .github/workflows/autopep8.yml

This file was deleted.

37 changes: 0 additions & 37 deletions .github/workflows/python-publish.yml

This file was deleted.

5 changes: 4 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
BSD 2-Clause License

Copyright (c) 2022, Rohan P. Singh
mujoco_py
├── Copyright (c) 2022, Rohan P. Singh
│   └── Copyright (c) 2022, Jack Xu

All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
90 changes: 21 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,27 @@
# Viewer for MuJoCo in Python

Interactive renderer to use with the official Python bindings for MuJoCo.

Starting with version 2.1.2, MuJoCo comes with native Python bindings officially supported by the MuJoCo devs.

If you have been a user of `mujoco-py`, you might be looking to migrate.
Some pointers on migration are available [here](https://mujoco.readthedocs.io/en/latest/python.html#migration-notes-for-mujoco-py).

# Install
Interactive renderer to use with the official Python bindings for MuJoCo 2.2.x

+ Develop along with [jx-mujoco-python-engine](https://github.com/jaku-jaku/jx-mujoco-python-engine)
+ A more efficient, robust and thread safe viewer with native MuJoCo 2.2.x with glfw
+ Works with Mac M1 with MuJoCo 2.2.x
## Points to ponder:
> This repo is/will-be a re-implementation of https://github.com/rohanpsingh/mujoco-python-viewer with custom features
> Additional credits: I do see a lot of similar structure as used in `mjviewer` from mujoco-py, so I will also give credits to https://github.com/openai/mujoco-py/blob/master/mujoco_py/mjviewer.py
> Starting with version 2.1.2, MuJoCo comes with native Python bindings officially supported by the MuJoCo devs.
> If you have been a user of `mujoco-py`, you might be looking to migrate.
> Some pointers on migration are available [here](https://mujoco.readthedocs.io/en/latest/python.html#migration-notes-for-mujoco-py).

## Tested OS Platform:
- Mac M1
- Ubuntu 18.04

## Install
```sh
$ git clone https://github.com/rohanpsingh/mujoco-python-viewer
$ cd mujoco-python-viewer
$ git clone https://github.com/jaku-jaku/jx-mujoco-python-viewer
$ cd jx-mujoco-python-viewer
$ pip install -e .
```
Or, install via Pip.
```sh
$ pip install mujoco-python-viewer
```

# Usage
#### How to render in a window?
```py
import mujoco
import mujoco_viewer

model = mujoco.MjModel.from_xml_path('humanoid.xml')
data = mujoco.MjData(model)

# create the viewer object
viewer = mujoco_viewer.MujocoViewer(model, data)

# simulate and render
for _ in range(10000):
if viewer.is_alive:
mujoco.mj_step(model, data)
viewer.render()
else:
break

# close
viewer.close()
```

The render should pop up and the simulation should be running.
Double-click on a geom and hold `Ctrl` to apply forces (right) and torques (left).

![ezgif-2-6758c40cdf](https://user-images.githubusercontent.com/16384313/161459985-a47e74dc-92c9-4a0b-99fc-92d1b5b04163.gif)


Press `ESC` to quit.
Other key bindings are shown in the overlay menu (Press `H` or hold `Alt`).



#### How to render offscreen?
```py
import mujoco
import mujoco_viewer

model = mujoco.MjModel.from_xml_path('humanoid.xml')
data = mujoco.MjData(model)

viewer = mujoco_viewer.MujocoViewer(model, data, 'offscreen')
mujoco.mj_forward(model, data)
img = viewer.read_pixels(camid=2)
## do something cool with img
```

# Optional Parameters

- `title`: set the title of the window, for example: `viewer = mujoco_viewer.MujocoViewer(model, data, title='My Demo')` (defaults to `mujoco-python-viewer`).
- `width`: set the window width, for example: `viewer = mujoco_viewer.MujocoViewer(model, data, width=300)` (defaults to full screen's width).
- `height`: set the window height, for example: `viewer = mujoco_viewer.MujocoViewer(model, data, height=300)` (defaults to full screen's height).
- `hide_menus`: set whether the overlay menus should be hidden or not (defaults to `True`).
## Demo (WIP):
- https://github.com/UW-Advanced-Robotics-Lab/simulation-mujoco-summit-wam
18 changes: 10 additions & 8 deletions examples/markers_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,39 @@
# create the viewer object
viewer = mujoco_viewer.MujocoViewer(model, data)

while viewer.is_alive:
while True:
# sim step
mujoco.mj_step(model, data)

# draw origin
x_dir = [[0, 0, 1], [0, 1, 0], [-1, 0, 0]]
y_dir = [[1, 0, 0], [0, 0, 1], [0, -1, 0]]
z_dir = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
viewer.add_marker(
viewer.add_marker_safe(
pos=[
0, 0, 0], size=[
0.05, 0.05, 0.05], rgba=[
1, 1, 1, 1], type=mujoco.mjtGeom.mjGEOM_SPHERE, label="origin")
viewer.add_marker(
viewer.add_marker_safe(
pos=[
0, 0, 0], mat=x_dir, size=[
0.01, 0.01, 2], rgba=[
1, 0, 0, 0.2], type=mujoco.mjtGeom.mjGEOM_ARROW, label="")
viewer.add_marker(
viewer.add_marker_safe(
pos=[
0, 0, 0], mat=y_dir, size=[
0.01, 0.01, 2], rgba=[
0, 1, 0, 0.2], type=mujoco.mjtGeom.mjGEOM_ARROW, label="")
viewer.add_marker(
viewer.add_marker_safe(
pos=[
0, 0, 0], mat=z_dir, size=[
0.01, 0.01, 2], rgba=[
0, 0, 1, 0.2], type=mujoco.mjtGeom.mjGEOM_ARROW, label="")

# render
viewer.render()

viewer.process_safe()
viewer.update_safe()
viewer.render_safe()

# close
viewer.close()
viewer.signal_termination_safe(if_immediate=True)
12 changes: 8 additions & 4 deletions examples/offscreen_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
model = mujoco.MjModel.from_xml_path('humanoid.xml')
data = mujoco.MjData(model)

viewer = mujoco_viewer.MujocoViewer(model, data, 'offscreen')
viewer = mujoco_viewer.MujocoViewer(model, data, if_on_scrn=False)
mujoco.mj_step(model, data)
img = viewer.read_pixels(camid=2)
img = PIL.Image.fromarray(img)
img.show()
viewer.process_safe()
viewer.update_safe()
viewer.render_safe()

# img = viewer.read_pixels(camid=2)
# img = PIL.Image.fromarray(img)
# img.show()
10 changes: 5 additions & 5 deletions examples/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
# simulate and render
for _ in range(100000):
mujoco.mj_step(model, data)
viewer.render()
if not viewer.is_alive:
break

viewer.process_safe()
viewer.update_safe()
viewer.render_safe()
# close
viewer.close()
viewer.signal_termination_safe(if_immediate=True)
8 changes: 5 additions & 3 deletions examples/simple_linkage.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@
# simulate and render
for _ in range(100000):
mujoco.mj_step(model, data)
viewer.render()

viewer.process_safe()
viewer.update_safe()
viewer.render_safe()

# close
viewer.close()
viewer.signal_termination_safe(if_immediate=True)
Loading