rohanpsingh / mujoco-python-viewer

Simple renderer for use with MuJoCo (>=2.1.2) Python Bindings.
BSD 2-Clause "Simplified" License
176 stars 27 forks source link

Creating custom Overlays while using MuJoCo Viewer #44

Open karthyyy opened 7 months ago

karthyyy commented 7 months ago

Hi,

I am using Mujoco_viewer for my onscreen and offline rendering. I need to add custom overlays to the rendering. So I disabled default overlays by setting "hide_menus='True'" as

viewer = mujoco_viewer.MujocoViewer(model, data, hide_menus='True')

Then custom overlays were created in my main file as given below

# Overlay Functions
_overlay = {}
def add_overlay(gridpos, text1, text2):

    if gridpos not in _overlay:
        _overlay[gridpos] = ["", ""]
    _overlay[gridpos][0] += text1 + "\n"
    _overlay[gridpos][1] += text2 + "\n"

# Add overlay here
def create_overlay(model ,data):
    topleft = mj.mjtGridPos.mjGRID_TOPLEFT
    topright = mj.mjtGridPos.mjGRID_TOPRIGHT
    bottomleft = mj.mjtGridPos.mjGRID_BOTTOMLEFT
    bottomright = mj.mjtGridPos.mjGRID_BOTTOMRIGHT   

    add_overlay(
        bottomleft,
        "Time", '%.2f' %data.time,
        )

From the MuJoCo literature, the steps are mentioned as

1) Calling the function "create_overlay(model, data)" 2) Add overlay items as follows

for gridpos, [t1, t2] in _overlay.items():

mj.mjr_overlay(
mj.mjtFontScale.mjFONTSCALE_150,
gridpos,
viewport,
t1,
t2,
context)

3) Clear the overlay "_overlay.clear()"

The simulation code is given as

# Simulation
mj.mj_step(model, data)        
create_overlay(model, data) #create overlay
glfw.make_context_current(viewer.window)
viewer.render()  
_overlay.clear()  # clear overlay
glfw.make_context_current(None)

Doubt-1) Whether the locations of the functions "create_overlay(model, data)" and "_overlay.clear()" in the above simulation and rendering code are correct ? Doubt-2) In add overlay items as given in step-2, what is 'viewport' and 'context' ? Doubt-3) Is there any better method than the one mentioned here ?

Can you please help me with the same ?

Thanks Karthik R