Skip to content

Recompile does not apply in-place updates to model and data  #1870

Closed

Description

@bikcrum

Branch: main (9febd76)

I am adding a geom to the existing model and runningrecompile(model, data) to obtain the updated model. However, the updates are not being applied in-place contrary to the documentation https://mujoco.readthedocs.io/en/latest/APIreference/APIfunctions.html#mj-recompile. This issue is also mentioned in #1822, which was closed without any comment.

Code to reproduce:

import mujoco as mj

# Define a simple model
model_string = """
    <mujoco>
        <worldbody>
            <body>
                <freejoint/>
                <geom size=".15" mass="1" type="sphere"/>
            </body>
        </worldbody>
    </mujoco>
"""

# Compile the model
spec = mj.MjSpec()
spec.from_string(model_string)
model = spec.compile()
data = mj.MjData(model)

# Add a geom to the world body
body = spec.worldbody
geom = body.add_geom()
geom.name = 'my_geom'
geom.size[0] = 1
geom.pos = [1, 1, 0]

# Recompile the model
new_model, new_data = spec.recompile(model, data)

new_model.geom('my_geom')  # No error

model.geom('my_geom')

Error:

# KeyError: "Invalid name 'my_geom'. Valid names: []"

Activity

  1. changed the title [-]Recompile is not doing in-place update of model and data [/-] [+]Recompile does not apply in-place updates to model and data [/+] on Aug 1, 2024
  2. quagla commented on Aug 1, 2024

    @quagla
    Collaborator

    It happens in place only in C (which is the function described in the docs), not in Python.

  3. quagla commented on Aug 1, 2024

    @quagla
    Collaborator

    Here is how the C function is wrapped in the bindings if you wish to have a look at what happens under the hood

    https://github.com/google-deepmind/mujoco/blob/main/python/mujoco/structs.cc#L454

  4. bikcrum commented on Aug 1, 2024

    @bikcrum
    Author

    @quagla Is it possible to edit this function to perform in-place operations for Python as well? Is there something preventing this?

  5. quagla commented on Aug 7, 2024

    @quagla
    Collaborator

    They will not be true in-place operations anyway because sizes change, causing possible issues with variables referencing the old mjData which would become invalid without the user explicitly being aware of it.

  6. yuvaltassa commented on Aug 14, 2024

    @yuvaltassa
    Collaborator

    As @quagla explained, there are technical issues preventing in-place updates in Python. I suspect the dangling reference issue could easily cause segfault. Perhaps @saran-t can expand more on the (im?)possibility of doing this...

    Closing for now.

    Regarding

    This issue is also mentioned in #1822, which was closed without any comment.

    Not true, it was closed with a comment saying it was completed and referencing the fix:

    image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions