mjSpec python bindings for add functions: accept size with 1, 2, or 3 parameters

PiperOrigin-RevId: 858494291
Change-Id: I8a02dcac1d2419fbc728607907425b401cf66da4
This commit is contained in:
Taylor Howell
2026-01-20 02:38:11 -08:00
committed by Copybara-Service
parent de47cf0e6f
commit 23c56ec4bb
2 changed files with 59 additions and 0 deletions
+36
View File
@@ -452,6 +452,42 @@ class SpecsTest(absltest.TestCase):
'Only one of: iaxisangle, ixyaxes, izaxis, or ieuler can be set.',
)
def test_size_kwarg_variable_length(self):
spec = mujoco.MjSpec()
body = spec.worldbody.add_body()
geom_size1 = body.add_geom(size=[0.5])
np.testing.assert_array_equal(geom_size1.size, [0.5, 0, 0])
geom_size2 = body.add_geom(size=[0.5, 0.3])
np.testing.assert_array_equal(geom_size2.size, [0.5, 0.3, 0])
geom_size3 = body.add_geom(size=[0.5, 0.3, 0.1])
np.testing.assert_array_equal(geom_size3.size, [0.5, 0.3, 0.1])
site_size1 = body.add_site(size=[0.2])
np.testing.assert_array_equal(site_size1.size, [0.2, 0, 0])
site_size2 = body.add_site(size=[0.2, 0.1])
np.testing.assert_array_equal(site_size2.size, [0.2, 0.1, 0])
site_size3 = body.add_site(size=[0.2, 0.1, 0.05])
np.testing.assert_array_equal(site_size3.size, [0.2, 0.1, 0.05])
with self.assertRaises(ValueError) as cm:
body.add_geom(size=[])
self.assertEqual(
str(cm.exception),
'size should be a list/array of size 1, 2, or 3.',
)
with self.assertRaises(ValueError) as cm:
body.add_geom(size=[1, 2, 3, 4])
self.assertEqual(
str(cm.exception),
'size should be a list/array of size 1, 2, or 3.',
)
def test_load_xml(self):
file_path = epath.resource_path("mujoco") / "testdata" / "model.xml"
filename = file_path.as_posix()