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

MjSpec.compile() should populate resulting MjModel's XML to enable mujoco.mj_saveLastXML #2075

Open
hartikainen opened this issue Sep 22, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@hartikainen
Copy link
Contributor

The feature, motivation and pitch

Currently, the mj_model resulting from mj_model = mj_spec.compile() cannot be saved with mujoco.mj_saveLastXML(mj_model) because the XML information is not stored in the model. It would be useful to be able to save the XML of an MjModel without carrying the MjSpec around.

It feels like this would be as simple as storing the MjSpec.to_xml() into the MjModel, but perhaps I'm overlooking something and this is actually more complicated than I think.

Alternatives

I can save the compiled model with mujoco.mj_saveModel. For debugging purposes, it would be nice to be able to look into the XML. This I can also do by keeping the spec around but it would be more convienient to not have to worry about the spec.

Additional context

Here's a simple test case that fails:

  def test_spec_model_saveLastXML(self):
    spec_1 = mujoco.MjSpec()
    spec_2 = mujoco.MjSpec()
    spec_1.from_string("<mujoco></mujoco>")
    model = spec_1.compile()
    with tempfile.NamedTemporaryFile(suffix=".xml", mode="w+t") as fp:
      mujoco.mj_saveLastXML(fp.name, model)
      spec_2.from_file(fp.name)

    self.assertEqual(spec_1.to_xml(), spec_2.to_xml())

Which gives:

======================================================================
ERROR: test_spec_model_saveLastXML (specs_test.SpecsTest.test_spec_model_saveLastXML)
specs_test.SpecsTest.test_spec_model_saveLastXML
----------------------------------------------------------------------
Traceback (most recent call last):
  File "mujoco/python/mujoco/specs_test.py", line 802, in test_spec_model_saveLastXML
    mujoco.mj_saveLastXML(fp.name, model)
mujoco.FatalError: No XML model loaded

----------------------------------------------------------------------
@hartikainen hartikainen added the enhancement New feature or request label Sep 22, 2024
@yuvaltassa
Copy link
Collaborator

yuvaltassa commented Sep 27, 2024

In what way is mj_saveXML not what you want?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants