Skip to content

Commit 1c240d8

Browse files
quaglacopybara-github
authored andcommitted
Raise error if trying to bind with a spec with an empty name.
PiperOrigin-RevId: 731360983 Change-Id: I00365d158c1cae1cd912962ef453f43e93701080
1 parent ed16f2d commit 1c240d8

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

mjx/mujoco/mjx/_src/support.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@ def __init__(self, model: Model, specs: Sequence[mujoco.MjStruct]):
295295
self.prefix = ''
296296
ids = []
297297
for spec in specs:
298-
if isinstance(spec, mujoco.MjsBody):
298+
if not spec.name:
299+
raise KeyError(f'cannot bind spec with empty name')
300+
elif isinstance(spec, mujoco.MjsBody):
299301
self.prefix = 'body_'
300302
idx = name2id(model, mujoco.mjtObj.mjOBJ_BODY, spec.name)
301303
elif isinstance(spec, mujoco.MjsJoint):
@@ -400,7 +402,9 @@ def __init__(
400402
self.prefix = ''
401403
ids = []
402404
for spec in specs:
403-
if isinstance(spec, mujoco.MjsBody):
405+
if not spec.name:
406+
raise KeyError(f'cannot bind spec with empty name')
407+
elif isinstance(spec, mujoco.MjsBody):
404408
idx = name2id(model, mujoco.mjtObj.mjOBJ_BODY, spec.name)
405409
elif isinstance(spec, mujoco.MjsJoint):
406410
self.prefix = 'jnt_'

mjx/mujoco/mjx/_src/support_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ def test_bind(self):
333333
vdx.bind(mx, s.bodies[i]).xpos, [d.xpos[i, :]] * batch_size
334334
)
335335

336+
# test emtpy name
337+
s.worldbody.add_body()
338+
m = s.compile()
339+
mx = mjx.put_model(m)
340+
with self.assertRaises(KeyError, msg='cannot bind spec with empty name'):
341+
mx.bind(s.bodies)
342+
336343
_CONTACTS = """
337344
<mujoco>
338345
<worldbody>

0 commit comments

Comments
 (0)