Change set_default and default to classname in the bindings.

Having defaults in the names of the attribute or function names is confusing for the users since the defaults are not re-applied but are only used for writing to XML. The alternative would be to change mjs_setDefault to re-apply them, but this would overwrite any other attribute set by the user so far.

PiperOrigin-RevId: 726431090
Change-Id: I41f4a23e1722e278b0ecc0a00b07db9b384fe994
This commit is contained in:
Alessio Quaglino
2025-02-13 04:51:05 -08:00
committed by Copybara-Service
parent 89253d957d
commit 1b4258d274
2 changed files with 88 additions and 109 deletions
+33 -36
View File
@@ -528,22 +528,7 @@ class SpecsTest(absltest.TestCase):
spec.to_xml()
def test_modelname_default_class(self):
spec = mujoco.MjSpec()
spec.modelname = 'test'
main = spec.default()
main.geom.size[0] = 2
def1 = spec.add_default('def1', main)
def1.geom.size[0] = 3
spec.worldbody.add_geom(def1)
spec.worldbody.add_geom(main)
spec.compile()
self.assertEqual(
spec.to_xml(),
textwrap.dedent("""\
XML = textwrap.dedent("""\
<mujoco model="test">
<compiler angle="radian"/>
@@ -559,8 +544,8 @@ class SpecsTest(absltest.TestCase):
<geom/>
</worldbody>
</mujoco>
"""),
)
""")
spec = mujoco.MjSpec()
spec.modelname = 'test'
@@ -574,26 +559,38 @@ class SpecsTest(absltest.TestCase):
spec.worldbody.add_geom(main)
spec.compile()
self.assertEqual(
spec.to_xml(),
textwrap.dedent("""\
<mujoco model="test">
<compiler angle="radian"/>
self.assertEqual(spec.to_xml(), XML)
spec = mujoco.MjSpec()
spec.modelname = 'test'
<default>
<geom size="2 0 0"/>
<default class="def1">
<geom size="3 0 0"/>
</default>
</default>
main = spec.default()
main.geom.size[0] = 2
def1 = spec.add_default('def1', main)
def1.geom.size[0] = 3
<worldbody>
<geom class="def1"/>
<geom/>
</worldbody>
</mujoco>
"""),
)
geom1 = spec.worldbody.add_geom(def1)
geom2 = spec.worldbody.add_geom()
self.assertEqual(geom1.classname.name, 'def1')
self.assertEqual(geom2.classname.name, 'main')
spec.compile()
self.assertEqual(spec.to_xml(), XML)
spec = mujoco.MjSpec()
spec.modelname = 'test'
main = spec.default()
main.geom.size[0] = 2
def1 = spec.add_default('def1', main)
def1.geom.size[0] = 3
geom1 = spec.worldbody.add_geom(size=[3, 0, 0])
geom2 = spec.worldbody.add_geom(size=[2, 0, 0])
geom1.classname = def1
geom2.classname = main # actually redundant, since main is always applied
spec.compile()
self.assertEqual(spec.to_xml(), XML)
def test_element_list(self):
spec = mujoco.MjSpec()