From 1c240d8badba4d0888a5f0fd63985f8e1b7a8686 Mon Sep 17 00:00:00 2001 From: Alessio Quaglino Date: Wed, 26 Feb 2025 10:06:41 -0800 Subject: [PATCH] Raise error if trying to bind with a spec with an empty name. PiperOrigin-RevId: 731360983 Change-Id: I00365d158c1cae1cd912962ef453f43e93701080 --- mjx/mujoco/mjx/_src/support.py | 8 ++++++-- mjx/mujoco/mjx/_src/support_test.py | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/mjx/mujoco/mjx/_src/support.py b/mjx/mujoco/mjx/_src/support.py index e1ab1c3d..aa1b0768 100644 --- a/mjx/mujoco/mjx/_src/support.py +++ b/mjx/mujoco/mjx/_src/support.py @@ -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_' diff --git a/mjx/mujoco/mjx/_src/support_test.py b/mjx/mujoco/mjx/_src/support_test.py index 5ae5af9f..572e7679 100644 --- a/mjx/mujoco/mjx/_src/support_test.py +++ b/mjx/mujoco/mjx/_src/support_test.py @@ -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 = """