Skip to content

Instantly share code, notes, and snippets.

@ugo-nama-kun
Last active December 6, 2021 05:55
Show Gist options
  • Save ugo-nama-kun/a13ec948ad494aa443f3458bda909a1c to your computer and use it in GitHub Desktop.
Save ugo-nama-kun/a13ec948ad494aa443f3458bda909a1c to your computer and use it in GitHub Desktop.
How to use the marker util of mujoco-py
import numpy as np
import gym
from gym.envs.mujoco import mujoco_env
from mujoco_py.generated import const
from scipy.spatial.transform import Rotation
""" Marker types in const
GEOM_PLANE = 0
GEOM_HFIELD = 1
GEOM_SPHERE = 2
GEOM_CAPSULE = 3
GEOM_ELLIPSOID = 4
GEOM_CYLINDER = 5
GEOM_BOX = 6
GEOM_MESH = 7
NGEOMTYPES = 8
GEOM_ARROW = 100
GEOM_ARROW1 = 101
GEOM_ARROW2 = 102
GEOM_LINE = 103
GEOM_SKIN = 104
GEOM_LABEL = 105
GEOM_NONE = 1001
"""
def euler2mat(euler, degrees=True):
r = Rotation.from_euler('xyz', euler, degrees=degrees)
return r.as_matrix()
class SwimmerEnvRV(gym.envs.mujoco.SwimmerEnv):
def render(self, **kwargs):
if self.viewer:
x, y = self.sim.data.qpos[:2]
# Draw a sphere marker
self.viewer.add_marker(pos=np.array([x, y, 0]), # Position
label=" ", # Text beside the marker
type=const.GEOM_SPHERE, # Geomety type
size=(0.1, 0.1, 0.1), # Size of the marker
rgba=(1, 0, 0, 1)) # RGBA of the marker
# Draw an arrow
# from: https://github.com/openai/mujoco-py/issues/31
self.viewer.add_marker(pos=np.array([0, 0, 1]), # Root Position
label="Arrow", # Text beside the marker
mat=euler2mat([90, 0, 90]), # Direction
type=const.GEOM_ARROW, # Geomety type
size=(0.1, 0.1, 2), # Size of the marker
rgba=(1, 0, 1, 0.3)) # RGBA of the marker
# Default swimmer renderer method
super(SwimmerEnvRV, self).render()
# Run
env = SwimmerEnvRV()
while True:
env.step(env.action_space.sample())
env.render()
@ugo-nama-kun
Copy link
Author

ugo-nama-kun commented Aug 15, 2021

According to MuJoCo forum, update_scene resets all virtual geoms, i.e. all markers. Unfortunately, mujoco-py does not clear the _markers list in the rgb_array (offscreen=True) mode.

Calling del viewer._markers[:] after render() fixes the issue.

Copied from:
openai/mujoco-py#423

@ugo-nama-kun
Copy link
Author

All marker configuration is done by using attributes of mjvGeom.
https://mujoco.readthedocs.io/en/latest/APIreference.html#mjvGeom

@ugo-nama-kun
Copy link
Author

How to add a mesh

(Assuming you have .stl file, Banana.stl by converting from .fbx using Meshlab)
(to do exactly same thinng below, you will need additional rescaling the banana by x0.001)

  1. Load the mesh file (.stl) in the xml (banana.xml) in the asset region.
  2. In python flile, set type option of add_maker to "GEOM_MESH". Set dataid to the right value (dataid=0 in this case).
  3. And run!

Example banana.xml

<?xml version="1.0" ?>
<mujoco>
    <asset>
        <mesh file="Banana.stl" name="banana"></mesh>
    </asset>
    <worldbody>
        <body name="floor" pos="0 0 0.025">
            <geom size="1.0 1.0 0.02" rgba="0 1 0 1" type="box"/>
        </body>
    </worldbody>
</mujoco>

Example banana_mujoco.py

import mujoco_py
from mujoco_py.generated import const
import os
import numpy as np
import time

xml_path = 'banana.xml'
model = mujoco_py.load_model_from_path(xml_path)
sim = mujoco_py.MjSim(model)

viewer = mujoco_py.MjViewer(sim)
while True:
    sim.step()
    t = time.time()

    for i in range(5):
        for j in range(5):
            viewer.add_marker(pos=np.array([0.5 * np.sin(t) + 0.2 * i, 0.5 * np.cos(t) + 0.2 * j, 0.5]),
                      type=const.GEOM_MESH,
					 # size=(0.1, 0.1, 0.1),  <-- Caution. this doesn't work...
                      rgba=(1, 1, 0, 1),
                      dataid=0)

    viewer.render()

You will see this.

banana_mesh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment