From a2acd2d4ed9f0e5512b2a3e7bff463d8d8c96bce Mon Sep 17 00:00:00 2001 From: Alessio Quaglino Date: Fri, 20 Jun 2025 06:22:15 -0700 Subject: [PATCH] Rename `detach_default` and `detach_body` to `delete` in the mjSpec bindings. PiperOrigin-RevId: 773674361 Change-Id: I3977060e19e9ff0afc65ad91693c017958576b78 --- doc/python.rst | 9 ++++----- python/mjspec.ipynb | 8 ++++---- python/mujoco/specs.cc | 4 ++-- python/mujoco/specs_test.py | 2 +- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/doc/python.rst b/doc/python.rst index 251cce46..7e669959 100644 --- a/doc/python.rst +++ b/doc/python.rst @@ -603,11 +603,10 @@ Lists of all elements in a spec can be accessed using named properties, using th Element removal ^^^^^^^^^^^^^^^ -For elements that can have children (bodies and defaults), the methods ``spec.detach_body(body)`` and -``spec.detach_default(def)`` remove, respectively, ``body`` and ``def`` from the spec, together with all of their -children. When detaching body subtrees, all elements which reference elements in the subtree, will also be removed. For -all other elements, the method ``delete()`` removes the corresponding element from the spec, e.g. -``spec.delete(spec.geom('my_geom'))`` will remove the geom named "my_geom" and all of the elements that reference it. +The method ``delete()`` removes the corresponding element from the spec, e.g. ``spec.delete(spec.geom('my_geom'))`` will +remove the geom named "my_geom" and all of the elements that reference it. For elements that can have children (bodies +and defaults), ``delete`` removes also all of their children. When detaching body subtrees, all elements which reference +elements in the subtree, will also be removed. Tree traversal ^^^^^^^^^^^^^^ diff --git a/python/mjspec.ipynb b/python/mjspec.ipynb index 49b1f54f..2b98508c 100644 --- a/python/mjspec.ipynb +++ b/python/mjspec.ipynb @@ -1827,7 +1827,7 @@ "\n", "# Remove all bodies in the list from the spec\n", "for body in delete_list:\n", - " spec.detach_body(body)\n", + " spec.delete(body)\n", "\n", "# # Add another humanoid\n", "spec_humanoid = mj.MjSpec.from_file(humanoid_file)\n", @@ -1900,8 +1900,8 @@ "shoulder_right = torso.add_frame(pos=arm_right.pos)\n", "\n", "# Remove the arms\n", - "spec.detach_body(arm_left)\n", - "spec.detach_body(arm_right)\n", + "spec.delete(arm_left)\n", + "spec.delete(arm_right)\n", "\n", "# Add new legs\n", "shoulder_left.attach_body(leg_left, 'shoulder', 'left')\n", @@ -1942,7 +1942,7 @@ "arm_right = spec.body('upper_arm_right')\n", "torso = spec.body('torso')\n", "shoulder_right = torso.add_frame(pos=arm_right.pos, quat=[0, 0.8509035, 0, 0.525322])\n", - "spec.detach_body(arm_right)\n", + "spec.delete(arm_right)\n", "\n", "# Attach Franka arm to humanoid\n", "franka_arm = franka.body('fr3_link2')\n", diff --git a/python/mujoco/specs.cc b/python/mujoco/specs.cc index a6a9139e..2b477c04 100644 --- a/python/mujoco/specs.cc +++ b/python/mujoco/specs.cc @@ -396,7 +396,7 @@ PYBIND11_MODULE(_specs, m) { return mjs_addDefault(spec->ptr, classname.c_str(), parent); }, py::return_value_policy::reference_internal); - mjSpec.def("detach_default", [](MjSpec& self, raw::MjsDefault& def) { + mjSpec.def("delete", [](MjSpec& self, raw::MjsDefault& def) { if (mjs_delete(self.ptr, def.element) != 0) { throw pybind11::value_error(mjs_getError(self.ptr)); } @@ -407,7 +407,7 @@ PYBIND11_MODULE(_specs, m) { return mjs_getSpecDefault(self.ptr); }, py::return_value_policy::reference_internal); - mjSpec.def("detach_body", [](MjSpec& self, raw::MjsBody& body) { + mjSpec.def("delete", [](MjSpec& self, raw::MjsBody& body) { mjs_delete(self.ptr, body.element); }); mjSpec.def( diff --git a/python/mujoco/specs_test.py b/python/mujoco/specs_test.py index 4020d4f2..7f444c73 100644 --- a/python/mujoco/specs_test.py +++ b/python/mujoco/specs_test.py @@ -604,7 +604,7 @@ class SpecsTest(absltest.TestCase): # test delete default def1 = spec.find_default('def1') - spec.detach_default(def1) + spec.delete(def1) def1 = spec.find_default('def1') self.assertIsNone(def1)