google-deepmind / dm_control

Google DeepMind's software stack for physics-based simulation and Reinforcement Learning environments, using MuJoCo.
Apache License 2.0
3.83k stars 671 forks source link

PyMJCF Keyframe Help #475

Closed jonzamora closed 5 months ago

jonzamora commented 5 months ago

Hi,

I am working with PyMJCF, and provide a minimal example below which I need help with:

I define the function

def set_keyframe(self):
    keyframe = self.arena.keyframe
    keyframe.add(
        "key",
        name="home",
        qpos="0 -0.96 1.16 0 -0.3 0 0.024 -0.024 0.4 0 0.025 1 0 0 0",
        ctrl="0 -0.96 1.16 0 -0.3 0 0.024"
    )

and call self.set_keyframe() in the __init__() of my Arena() class.

However, the keyframe is not being loaded automatically. I need to press the Tab key once the MuJoCo viewer spawns, then manually press "Load Key", which is not ideal.

Any idea how to get the keyframe to load automatically after i add it to the arena like above?

jonzamora commented 5 months ago

Found my fix rather quickly, it was a silly thing!

def create_model(arena: Arena):
    model = mjcf.Physics.from_mjcf_model(arena.get_arena())
    model = model.model.ptr
    data = mujoco.MjData(model)

    # Set the simulation to the "home" keyframe
    key_id = model.key("home").id
    mujoco.mj_resetDataKeyframe(model, data, key_id)
    return model, data