Yield ownership of char[], vector<string>, and vector<byte> to mjSpec in Python bindings.

PiperOrigin-RevId: 681522091
Change-Id: Icb3788d9ef6e914a307531e39ad8171c24f590af
This commit is contained in:
Alessio Quaglino
2024-10-02 11:13:45 -07:00
committed by Copybara-Service
parent 36ee89d6da
commit e973dc74dd
3 changed files with 103 additions and 24 deletions
+54 -5
View File
@@ -34,9 +34,28 @@ class SpecsTest(absltest.TestCase):
spec = mujoco.MjSpec()
# Check that euler sequence order is set correctly.
self.assertEqual(spec.eulerseq[0], ord('x'))
self.assertEqual(spec.eulerseq[0], 'x')
spec.eulerseq = ['z', 'y', 'x']
self.assertEqual(spec.eulerseq[0], ord('z'))
self.assertEqual(spec.eulerseq[0], 'z')
# Change single elements of euler sequence.
spec.eulerseq[0] = 'y'
spec.eulerseq[1] = 'z'
self.assertEqual(spec.eulerseq[0], 'y')
self.assertEqual(spec.eulerseq[1], 'z')
# eulerseq is iterable
self.assertEqual('yzx', ''.join(spec.eulerseq))
# supports `len`
self.assertLen(spec.eulerseq, 3)
# field checks for out-of-bound access on read and on write
with self.assertRaises(IndexError):
spec.eulerseq[3] = 'x'
with self.assertRaises(IndexError):
spec.eulerseq[-1] = 'x'
# Add a body, check that it has default orientation.
body = spec.worldbody.add_body()
@@ -50,10 +69,14 @@ class SpecsTest(absltest.TestCase):
self.assertEqual(body.name, 'baz')
# Change the position of the body and read it back.
body.pos = [1, 2, 3]
np.testing.assert_array_equal(body.pos, [1, 2, 3])
body.pos = [4, 2, 3]
np.testing.assert_array_equal(body.pos, [4, 2, 3])
self.assertEqual(body.pos.shape, (3,))
# Change single element of position.
body.pos[0] = 1
np.testing.assert_array_equal(body.pos, [1, 2, 3])
# Change the orientation of the body and read it back.
body.quat = [0, 1, 0, 0]
np.testing.assert_array_equal(body.quat, [0, 1, 0, 0])
@@ -147,7 +170,7 @@ class SpecsTest(absltest.TestCase):
# Add tuple.
tuple_ = spec.add_tuple(objprm=[2.0, 3.0, 5.0], objname=['obj'])
np.testing.assert_array_equal(tuple_.objprm, [2.0, 3.0, 5.0])
self.assertEqual(tuple_.objname, ['obj'])
self.assertEqual(tuple_.objname[0], 'obj')
# Add flex.
flex = spec.add_flex(friction=[1, 2, 3], texcoord=[1.0, 2.0, 3.0])
@@ -797,5 +820,31 @@ class SpecsTest(absltest.TestCase):
self.assertEqual(model.stat.meansize, 0.06)
self.assertEqual(model.vis.quality.shadowsize, 8192)
def test_assign_list_element(self):
spec = mujoco.MjSpec()
material = spec.add_material()
texture_index = mujoco.mjtTextureRole.mjTEXROLE_RGB
# Assign a string to a list element.
material.textures[texture_index] = 'texture_name'
self.assertEqual(material.textures[texture_index], 'texture_name')
# Assign a complete list
material.textures = ['', 'new_name', '', '', '']
self.assertEqual(material.textures[texture_index], 'new_name')
# textures is iterable
self.assertEqual('new_name', ''.join(material.textures))
# supports `len`
self.assertLen(material.textures, 5)
# field checks for out-of-bound access on read and on write
with self.assertRaises(IndexError):
material.textures[5] = 'x'
with self.assertRaises(IndexError):
material.textures[-1] = 'x'
if __name__ == '__main__':
absltest.main()