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

Cant use mjr_uploadHField when using passive viewer because of missing context #965

Closed
ripkea opened this issue Jul 4, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@ripkea
Copy link

ripkea commented Jul 4, 2023

Hi,

I'm currently trying to run a simulation of a dynamic heightfield using the mujoco python bindings. I would like to use the passive viewer (launch_passive) rendering, since it comes with a lot of neat capabilities, but there is no way for me to execute mjr_uploadHField, which is needed to reflect changes in the heightfield, since it requires an mjrContext.

For the heightfield to chance dynamically, I need to use mujoco.mjr_uploadHField which requires a context, that the viewer does not expose. I have tried using context = mujoco.MjrContext(model, mujoco.mjtFontScale.mjFONTSCALE_150.value) , however this will crash the application with exit code -1073741819. I've had no luck accesing the context any other way.

Is this intended? If yes, how else should I access the context to execute uploadHField or uploadMesh etc.? Please change the label accordingly, if this is not a bug.

Thanks !

This is a small example of how I tried to run my code (I for the life of me cannot get it nicely formatted with actual code markdown):

import mujoco
import mujoco.viewer as v
import time
import numpy as np
import random

field_size = 32
base_height = 30
height_perturbation_range = 5
heightfield = np.array([0] * field_size * field_size)


def change_heightfield(heightfield):
    for x in range(int(field_size / 2)):
        for y in range(int(field_size / 2)):
            height = random.uniform(base_height, height_perturbation_range + base_height)
            heightfield[2 * y + 2 * x * field_size] = height
            heightfield[2 * y + 1 + 2 * x * field_size] = height
            heightfield[2 * y + (2 * x + 1) * field_size] = height
            heightfield[2 * y + 1 + (2 * x + 1) * field_size] = height
    return heightfield
xml ="""
<mujoco>
    <asset>
        <hfield name="terrain" nrow="{field_size}" ncol="{field_size}" size="{world_field_size} {world_field_size} 1 1"/>
    </asset>
  <worldbody>
    <light name="top" pos="0 0 1"/>
      <geom name="red_sphere" type="box" size=".1 .5 .5" rgba="1 0 0 1" pos="0 0 4"/>
        <geom name="heightfield" type="hfield" hfield="terrain" 
     />
  </worldbody>
</mujoco>
""".format(field_size=field_size, world_field_size=field_size*0.1)
model = mujoco.MjModel.from_xml_string(xml)
data = mujoco.MjData(model)
mujoco.mj_step(model, data)

with v.launch_passive(model, data) as v:

  start = time.time()
  context = mujoco.MjrContext(model, mujoco.mjtFontScale.mjFONTSCALE_150.value)

  while v.is_running() and time.time() - start < 60

    model.hfield_data = heightfield.astype(float) * 0.1
    renderer = mujoco.Renderer(model)
    step_start = time.time()

    heightfield = change_heightfield(heightfield)
    model.hfield_data = heightfield.astype(float) * 0.1
    mujoco.mjr_uploadHField(model, context, 0)
    mujoco.mj_step(model, data)
    v.sync()
    time_until_next_step = model.opt.timestep - (time.time() - step_start)
    if time_until_next_step > 0:
      time.sleep(time_until_next_step)
@ripkea ripkea added the bug Something isn't working label Jul 4, 2023
@gaiyi7788
Copy link

I meet same problem. Have you finish this problem yet?

@saran-t
Copy link
Member

saran-t commented Jul 20, 2023

This feature is now available in version 2.3.7 which was just released. Please consult our documentation for the new viewer API for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants