Skip to content
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

Added graph rendering, Actuator force visualization[no sites], Sim reset method(backspace) and window positioning #23

Open
wants to merge 21 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
161 changes: 161 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,164 @@ dist/
*.egg-info
mujoco_viewer/__pycache__
mujoco_viewer/MUJOCO_LOG.TXT

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ img = viewer.read_pixels(camid=2)
- `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 `False`).
- `hide_menus`: set whether the overlay menus and graph should be hidden or not (defaults to `False`).
76 changes: 76 additions & 0 deletions examples/actuator_force_rendering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import mujoco
import mujoco_viewer
import math

MODEL_XML = """
<?xml version="1.0" ?>
<mujoco>
<option gravity="0 0 -9.81" timestep="0.002" />
<asset>
<material name="blue" rgba="0 0 1 1" />
<material name="green" rgba="0 1 0 1" />
<material name="red" rgba="1 0 0 1" />
<material name="white" rgba="1 1 1 1" />
</asset>
<worldbody>
<geom type="plane" size="10 10 0.1" pos="0 0 -1.6" rgba=".3 .3 .3 1" />
<light diffuse=".5 .5 .5" pos="0 0 3" dir="0 0 -1" />

<body name="link_1" pos="0 0 0" euler="90 0 0">
<joint name="hinge_1" type="hinge" axis="1 0 0" />
<geom name="g_link_1" type="cylinder" size=".1 .5" pos="0 0 .5" euler="0 0 0" material="red" mass="1" />
<body name="link_2" pos=".2 0 1" euler="0 0 0">
<joint name="hinge_2" type="hinge" axis="0 1 0" />
<geom name="g_link_2" type="cylinder" size=".1 .5" pos="0 0 .5" euler="0 0 0" material="green" mass="1" />
<body name="link_3" pos=".2 0 1" euler="0 0 0">
<joint name="hinge_3" type="slide" axis="1 0 0" />
<geom name="g_link_3" type="cylinder" size=".1 .5" pos="0.3 0 0" euler="0 90 0" material="blue" mass="1" />
<geom name="end_mass" type="sphere" size=".25" pos="1 0 0" euler="0 0 0" material="blue" mass="1" />
</body>
</body>
</body>

</worldbody>
<actuator>
<position name="pos_servo_1" joint="hinge_1" kp="100" />
<position name="pos_servo_2" joint="hinge_2" kp="100" />
<position name="pos_servo_3" joint="hinge_3" kp="100" />
</actuator>
<sensor>
<jointpos name="pos_sensor_1" joint="hinge_1" />
<jointpos name="pos_sensor_2" joint="hinge_2" />
<jointpos name="pos_sensor_3" joint="hinge_3" />
</sensor>
</mujoco>
"""

model = mujoco.MjModel.from_xml_string(MODEL_XML)
data = mujoco.MjData(model)

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

f_render_list = [
["hinge_1", "pos_servo_1", "force_at_hinge_1"],
["hinge_2", "pos_servo_2", "force_at_hinge_2"],
["hinge_3", "pos_servo_3", "force_at_hinge_3"],
]

for _ in range(10000):
# Render forces
viewer.show_actuator_forces(
f_render_list=f_render_list,
rgba_list=[1, 0.5, 1, 0.5],
force_scale=0.05,
arrow_radius=0.05,
show_force_labels=True,
)

# step and render
mujoco.mj_step(model, data)
viewer.render()
if not viewer.is_alive:
break

# close
viewer.close()
90 changes: 90 additions & 0 deletions examples/graphs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import mujoco
import mujoco_viewer
import math

MODEL_XML = """
<?xml version="1.0" ?>
<mujoco>
<option gravity="0 0 -9.81" timestep="0.002" />
<default>
<site type="cylinder" size="0.01 0.03" zaxis="1 0 0" rgba="0.1 0.1 1 .1" />
</default>

<asset>
<material name="blue" rgba="0 0 1 1" />
<material name="green" rgba="0 1 0 1" />
<material name="red" rgba="1 0 0 1" />
<material name="white" rgba="1 1 1 1" />
</asset>
<worldbody>
<geom type="plane" size="10 10 0.1" pos="0 0 -1.6" rgba=".3 .3 .3 1" />
<light diffuse=".5 .5 .5" pos="0 0 3" dir="0 0 -1" />

<body name="link_1" pos="0 0 0" euler="90 0 0">
<joint name="hinge_1" type="hinge" axis="1 0 0" />
<site name="site_1" />
<geom name="g_link_1" type="cylinder" size=".1 .5" pos="0 0 .5" euler="0 0 0" material="red" mass="1" />
<body name="link_2" pos=".2 0 1" euler="0 0 0">
<joint name="hinge_2" type="hinge" axis="1 0 0" />
<site name="site_2" />
<geom name="g_link_2" type="cylinder" size=".1 .5" pos="0 0 .5" euler="0 0 0" material="green" mass="1" />
<body name="link_3" pos=".2 0 1" euler="0 0 0">
<joint name="hinge_3" type="hinge" axis="1 0 0" />
<site name="site_3" />
<geom name="g_link_3" type="cylinder" size=".1 .5" pos="0 0 .5" euler="0 0 0" material="blue" mass="1" />
<geom name="end_mass" type="sphere" size=".25" pos="0 0 1" euler="0 0 0" material="blue" mass="1" />
</body>
</body>
</body>

</worldbody>
<actuator>
<position name="pos_servo_1" joint="hinge_1" kp="100" />
<position name="pos_servo_2" joint="hinge_2" kp="100" />
<position name="pos_servo_3" joint="hinge_3" kp="100" />
</actuator>
<sensor>
<jointpos name="pos_sensor_1" joint="hinge_1" />
<jointpos name="pos_sensor_2" joint="hinge_2" />
<jointpos name="pos_sensor_3" joint="hinge_3" />
</sensor>
</mujoco>
"""

model = mujoco.MjModel.from_xml_string(MODEL_XML)
data = mujoco.MjData(model)

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


# Initialize the graph lines
viewer.add_graph_line(line_name="line_1")
viewer.add_graph_line(line_name="downscaled_force_sensor")
viewer.add_graph_line(line_name="position_sensor")

# Initialize the Legend
viewer.show_graph_legend(show_legend=True)

# For a time-based graph,
# x_axis_time is the total time that you want your viewing window to see
# It needs to be set to grater than [model.opt.timestep*50]
# If you want to over ride that, change the "override" parameter to True
viewer.set_grid_divisions(x_div=5,
y_div=4,
x_axis_time=model.opt.timestep * 1000,
override=True)

for i in range(10000):
viewer.update_graph_line(line_name="line_1", line_data=math.sin(i / 10.0))
viewer.update_graph_line(line_name="downscaled_force_sensor", line_data=data.actuator_force[0]/100.0)
viewer.update_graph_line(line_name="position_sensor", line_data=data.sensordata[0])

# step and render
mujoco.mj_step(model, data)
viewer.render()
if not viewer.is_alive:
break

# close
viewer.close()
8 changes: 8 additions & 0 deletions mujoco_viewer/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self, hide_menus):
self._last_mouse_x = 0
self._last_mouse_y = 0
self._paused = False
self._hide_graph = False
self._transparent = False
self._contacts = False
self._joints = False
Expand Down Expand Up @@ -97,6 +98,9 @@ def _key_callback(self, window, key, scancode, action, mods):
self.model.geom_rgba[:, 3] /= 5.0
else:
self.model.geom_rgba[:, 3] *= 5.0
# Toggle Graph overlay
elif key == glfw.KEY_G:
self._hide_graph = not self._hide_graph
# Display inertia
elif key == glfw.KEY_I:
self._inertias = not self._inertias
Expand All @@ -115,6 +119,10 @@ def _key_callback(self, window, key, scancode, action, mods):
self.vopt.flags[
mujoco.mjtVisFlag.mjVIS_CONVEXHULL
] = self._convex_hull_rendering
# Reload Simulation
elif key == glfw.KEY_BACKSPACE:
mujoco.mj_resetData(self.model, self.data)
mujoco.mj_forward(self.model, self.data)
# Wireframe Rendering
elif key == glfw.KEY_W:
self._wire_frame = not self._wire_frame
Expand Down
Loading