Skip to content

Python mjSpec recompilation #1822

@jdao913

Description

@jdao913

Hello Mujoco team,

Loading and compiling a model and then adding a lone geom directly to the world body and then recompiling does not work with the new mjSpec in Python.

Following some examples from the spec_tests.py file the following works:

spec = mj.MjSpec()
spec.compile()
geom = spec.worldbody.add_geom()
geom.name = 'my_geom'
geom.size[0] = 1
geom.pos = [1, 1, 0]
spec.compile()

but when first loading a spec from a xml or a string things do not work

model_string = """
    <mujoco>
        <worldbody>
            <body>
                <freejoint/>
                <geom size=".15" mass="1" type="sphere"/>
            </body>
        </worldbody>
    </mujoco>
"""

spec = mj.MjSpec()
spec.from_string(model_string)
spec.compile()
geom = spec.worldbody.add_geom()
geom.name = 'my_geom'
geom.size[0] = 1
geom.pos = [1, 1, 0]
spec.compile()

Gives me the following error:

ValueError: Error: incompatible id in geom array, position 1
Element name '', id 0, line 6

Note that spec.recompile does not work either, in this case I get a mj_setPtrData: mjData buffer size mismatch error.
Is this intended? Looking the mjSpec Python tests it seems like none of the test cases preloads a xml and then adds a geom.

Adding a body first and then a geom to that body does work, for example the following as no issues:

model_string = """
    <mujoco>
        <worldbody>
            <body>
                <freejoint/>
                <geom size=".15" mass="1" type="sphere"/>
            </body>
        </worldbody>
    </mujoco>
"""

spec = mj.MjSpec()
spec.from_string(model_string)
spec.compile()
body = spec.worldbody.add_body()
body.pos = [1, 2, 3]
body.quat = [0, 1, 0, 0]
geom = body.add_geom()
geom.name = 'my_geom'
geom.size[0] = 1
geom.pos = [1, 1, 0]
spec.compile()

On a separate note it seems like mjSpec recompilation in Python does not happen in place? As in the test_recompile test in the spec_tests.py it seems like recompile has to be used like

model_new, data_new = spec.recompile(model, data)

as opposed to just

spec.recompile(model, data)

and the model and data being updated accordingly. This is what I thought would happen based off of the mj_recompile documentation, but it seems like this is not that case.

Is this intended and just a product of having to deal with Python jank or can the recompilation be made in place in the future?

Thank you,
Jeremy

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