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

Dynamically Update Assets (height field) #958

Closed
vikashplus opened this issue Jun 28, 2023 · 5 comments
Closed

Dynamically Update Assets (height field) #958

vikashplus opened this issue Jun 28, 2023 · 5 comments
Labels
question Request for help or information

Comments

@vikashplus
Copy link

vikashplus commented Jun 28, 2023

I'm trying to dynamically update assets. While the model is getting updated and the simulation reflects the update, the viewer is not appropriately reflecting the update despite these updates being pushed to the mjr_contexts. Likely I'm missing something. Im attaching a simple script to reproduce.

from mujoco import viewer
from dm_control.mujoco import Physics
from dm_control.mujoco.wrapper.mjbindings import mjlib
import numpy as np

# Prepare physics
sim = Physics.from_xml_path('height_field.xml')

# Prepare viewer
window = viewer.launch_passive(sim.model.ptr, sim.data.ptr)

# New height field
for _ in range(5):

    # Prep new hfield
    sim.model.hfield_data[:] = np.random.uniform(high=.2, low=.1, size=np.shape(sim.model.hfield_data))

    # Re-Upload hfield data to gpu
    if sim.contexts:
        print("Uploading new asset")
        with sim.contexts.gl.make_current() as ctx:
            ctx.call(mjlib.mjr_uploadHField, sim.model.ptr, sim.contexts.mujoco.ptr, 0)

    # Sync viewer
    window.sync()

    # wait for user
    input("Press enter to generate new height field")

mujoco==2.3.5
dm-control==1.0.11
os = Ubuntu 18.04.6 LTS
Python 3.8.16

asset_update.zip

cc: @cameronberg

@vikashplus vikashplus added the question Request for help or information label Jun 28, 2023
@saran-t
Copy link
Member

saran-t commented Jun 28, 2023

This cannot work since the viewer is completely separate from the Python process.

@vikashplus
Copy link
Author

I see. An ugly workaround for now is to close the existing viewer and re-open a new viewer (which picks up the new updaes)

from mujoco import viewer
from dm_control.mujoco import Physics
from dm_control.mujoco.wrapper.mjbindings import mjlib
import numpy as np

# Prepare physics
sim = Physics.from_xml_path('height_field.xml')

# Prepare viewer
window = viewer.launch_passive(sim.model.ptr, sim.data.ptr)

# New height field
for _ in range(5):

    # wait for user request =
    input("Press enter to request new height field")
    window.close()

    # Prep new hfield
    sim.model.hfield_data[:] = np.random.uniform(high=.2, low=.1, size=np.shape(sim.model.hfield_data))
    window = viewer.launch_passive(sim.model.ptr, sim.data.ptr)

    # Sync viewer
    window.sync()
    ```

@saran-t
Copy link
Member

saran-t commented Jun 28, 2023

I'll keep thinking about alternative solutions.

@vikashplus
Copy link
Author

This is amazing! Thanks @saran-t
I'll give it a try soon.

@saran-t
Copy link
Member

saran-t commented Jul 20, 2023

This is now available in version 2.3.7 which was just released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Request for help or information
Projects
None yet
Development

No branches or pull requests

2 participants