Raise error if trying to bind with a spec with an empty name.

PiperOrigin-RevId: 731360983
Change-Id: I00365d158c1cae1cd912962ef453f43e93701080
This commit is contained in:
Alessio Quaglino
2025-02-26 10:06:41 -08:00
committed by Copybara-Service
parent ed16f2daf2
commit 1c240d8bad
2 changed files with 13 additions and 2 deletions
+6 -2
View File
@@ -295,7 +295,9 @@ class BindModel(object):
self.prefix = ''
ids = []
for spec in specs:
if isinstance(spec, mujoco.MjsBody):
if not spec.name:
raise KeyError(f'cannot bind spec with empty name')
elif isinstance(spec, mujoco.MjsBody):
self.prefix = 'body_'
idx = name2id(model, mujoco.mjtObj.mjOBJ_BODY, spec.name)
elif isinstance(spec, mujoco.MjsJoint):
@@ -400,7 +402,9 @@ class BindData(object):
self.prefix = ''
ids = []
for spec in specs:
if isinstance(spec, mujoco.MjsBody):
if not spec.name:
raise KeyError(f'cannot bind spec with empty name')
elif isinstance(spec, mujoco.MjsBody):
idx = name2id(model, mujoco.mjtObj.mjOBJ_BODY, spec.name)
elif isinstance(spec, mujoco.MjsJoint):
self.prefix = 'jnt_'
+7
View File
@@ -333,6 +333,13 @@ class SupportTest(parameterized.TestCase):
vdx.bind(mx, s.bodies[i]).xpos, [d.xpos[i, :]] * batch_size
)
# test emtpy name
s.worldbody.add_body()
m = s.compile()
mx = mjx.put_model(m)
with self.assertRaises(KeyError, msg='cannot bind spec with empty name'):
mx.bind(s.bodies)
_CONTACTS = """
<mujoco>
<worldbody>