Prevent deletion from an attached mjSpec.

This change disallows calling `mjs_delete` on an mjSpec that has been attached to another mjSpec via `mjs_attach`. Attempting to delete an element from an attached spec will now result in an error. The Python bindings for `mjSpec.delete` have been updated to raise a ValueError when this occurs.

PiperOrigin-RevId: 897266695
Change-Id: Ic0670125a3028191ec50eca890f02b5910ec8b03
This commit is contained in:
Alessio Quaglino
2026-04-09 13:12:40 -07:00
committed by Copybara-Service
parent a744b366fb
commit 6b724616c0
5 changed files with 107 additions and 20 deletions
+15 -1
View File
@@ -979,7 +979,6 @@ class SpecsTest(absltest.TestCase):
self.assertEqual(mesh.plugin.name, 'inst')
self.assertEqual(mesh.plugin.plugin_name, 'mujoco.sdf.torus')
def test_duplicate_name_error(self):
main_xml = """
<mujoco>
@@ -1391,6 +1390,21 @@ class SpecsTest(absltest.TestCase):
with self.assertRaisesRegex(ValueError, 'Frame not found.'):
parent.attach(child4, frame='invalid_frame', prefix='child3-')
def test_delete_from_attached_spec_error(self):
parent = mujoco.MjSpec()
child = mujoco.MjSpec()
body = child.worldbody.add_body(name='child_body')
geom = body.add_geom(name='child_geom')
frame = parent.worldbody.add_frame()
parent.attach(child, frame=frame, prefix='child_')
# Now child spec is attached. Deleting from it should raise ValueError.
with self.assertRaisesRegex(
ValueError, 'Cannot delete element from an attached mjSpec.'
):
child.delete(geom)
def test_attach_valid_child_lists(self):
xml1 = """
<mujoco>