Skip to content

Commit

Permalink
Add to_file function to save the Spec to a file.
Browse files Browse the repository at this point in the history
Fixes #2415.

PiperOrigin-RevId: 725161617
Change-Id: I6c374cc7f7cb3f1da1cc0da531dd7e5be5d62b6a
  • Loading branch information
quagla authored and copybara-github committed Feb 10, 2025
1 parent 0e01b51 commit 4156350
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
6 changes: 6 additions & 0 deletions python/mujoco/specs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@ PYBIND11_MODULE(_specs, m) {
}
return std::string(buf.get());
});
mjSpec.def("to_file", [](MjSpec& self, std::string& file) {
std::array<char, 1024> err;
if (mj_saveXML(self.ptr, file.c_str(), err.data(), err.size()) < 0) {
throw FatalError(std::string(err.data()));
}
});
mjSpec.def(
"add_default",
[](MjSpec* spec, std::string& classname,
Expand Down
8 changes: 3 additions & 5 deletions python/mujoco/specs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ def test_basic(self):
self.assertEqual(model.nuser_site, 6)
np.testing.assert_array_equal(model.site_user[0], [1, 2, 3, 4, 5, 6])

self.assertEqual(
spec.to_xml(),
textwrap.dedent("""\
xml = textwrap.dedent("""\
<mujoco model="MuJoCo Model">
<compiler angle="radian"/>
Expand All @@ -127,8 +125,8 @@ def test_basic(self):
</body>
</worldbody>
</mujoco>
"""),
)
""")
self.assertEqual(spec.to_xml(), xml)

def test_kwarg(self):
# Create a spec.
Expand Down

0 comments on commit 4156350

Please sign in to comment.