Raise ValueError if adding a spec results in a duplicate name.

PiperOrigin-RevId: 871397807
Change-Id: I50742efe7ba194ed3b1fe916f05e35b7fb2d7ec0
This commit is contained in:
Sam Haves
2026-02-17 10:53:35 -08:00
committed by Copybara-Service
parent af802a5386
commit 448e221690
2 changed files with 23 additions and 1 deletions
@@ -841,7 +841,9 @@ def generate_add() -> None:
code += """\n
auto set_name = [](raw::MjsElement* el, const std::optional<std::string>& name) {
if (name.has_value()) {
mjs_setName(el, name->c_str());
if (mjs_setName(el, name->c_str())) {
throw pybind11::value_error(mjs_getError(mjs_getSpec(el)));
}
}
};
"""
+20
View File
@@ -970,6 +970,26 @@ class SpecsTest(absltest.TestCase):
):
spec.add_material().name = 'yellow'
def test_duplicate_name_error_when_adding_specs_with_kwargs(self):
spec = mujoco.MjSpec()
body = spec.worldbody.add_body(name='body')
body.add_geom(
type=mujoco.mjtGeom.mjGEOM_BOX, size=[0.1, 1, 1], name='dup'
)
with self.assertRaisesRegex(
ValueError, "Error: repeated name 'dup' in geom"
):
body.add_geom(
type=mujoco.mjtGeom.mjGEOM_BOX, size=[1, 0.1, 1], name='dup'
)
spec2 = mujoco.MjSpec()
spec2.add_material(name='yellow')
with self.assertRaisesRegex(
ValueError, "Error: repeated name 'yellow' in material"
):
spec2.add_material(name='yellow')
def test_delete_unused_plugin(self):
spec = mujoco.MjSpec.from_string("""
<mujoco model="MuJoCo Model">