rohanpsingh / mujoco-python-viewer

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

Can I use mujoco-python-viewer using `dm_control` API? #28

Closed omershalev closed 1 year ago

omershalev commented 1 year ago

I'm new to mujoco and I'm trying to play with interactive visualization. mujoco-python-viewer seems really useful!

I noticed though that I cannot use it with the dm_control.mujoco.Physics API (which is more convenient for named indexing, etc.).

To clarify my intention, below is an example of the way I would like to use it:

from dm_control import mujoco
import mujoco_viewer

physics = mujoco.Physics.from_xml_path('my_model.xml')
model = physics.model
data = physics.data

viewer = mujoco_viewer.MujocoViewer(model, data)

for _ in range(10000):
    if viewer.is_alive:
        physics.step()
        viewer.render()
    else:
        break

viewer.close()

Is there a way to do that?

rohanpsingh commented 1 year ago

Hi @omershalev I haven't really tried that, could you share what exactly is the error? Also why don't you directly use dm_control.viewer ?

omershalev commented 1 year ago

Here is the relevant part of the stack trace:

self.scn = mujoco.MjvScene(self.model, maxgeom=10000) TypeError: init(): incompatible constructor arguments. The following argument types are supported:

  1. mujoco._structs.MjvScene()
  2. mujoco._structs.MjvScene(model: mujoco._structs.MjModel, maxgeom: int) Invoked with: <dm_control.mujoco.wrapper.core.MjModel object at 0x7fa3dcc9c5d0>; kwargs: maxgeom=10000

I did consider using dm_control.viewer, but constructing the Environment object seems like a big hurdle.

rohanpsingh commented 1 year ago

I think we can use model.ptr and data.ptr:

physics = mujoco.Physics.from_xml_path('my_model.xml')
model = physics.model.ptr
data = physics.data.ptr

viewer = mujoco_viewer.MujocoViewer(model, data)

For me this seems to work fine. Could you please try?

rohanpsingh commented 1 year ago

I'll close this issue, but please feel free to reopen if the above solution does not work!

omershalev commented 1 year ago

Thank you @rohanpsingh! It worked like a charm.