Skip to content

Commit bee80fe

Browse files
yuvaltassacopybara-github
authored andcommitted
Add insert method to mjcf.element.
PiperOrigin-RevId: 469135739 Change-Id: I60d239051e8bbded0f90add424418f7bd5c63c2f
1 parent 5db4e15 commit bee80fe

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

dm_control/mjcf/element.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,23 @@ def add(self, element_name, **kwargs):
592592
ValueError: If the 'element_name' is not a valid child, or if an invalid
593593
attribute is specified in `kwargs`.
594594
595+
Returns:
596+
An `mjcf.Element` corresponding to the newly created child element.
597+
"""
598+
return self.insert(element_name, position=None, **kwargs)
599+
600+
def insert(self, element_name, position, **kwargs):
601+
"""Add a new child element to this element.
602+
603+
Args:
604+
element_name: The tag of the element to add.
605+
position: Where to insert the new element.
606+
**kwargs: Attributes of the new element being created.
607+
608+
Raises:
609+
ValueError: If the 'element_name' is not a valid child, or if an invalid
610+
attribute is specified in `kwargs`.
611+
595612
Returns:
596613
An `mjcf.Element` corresponding to the newly created child element.
597614
"""
@@ -604,7 +621,10 @@ def add(self, element_name, **kwargs):
604621
raise ValueError('A <{}> child already exists, please access it directly.'
605622
.format(element_name))
606623
new_element = _make_element(child_spec, self, attributes=kwargs)
607-
self._children.append(new_element)
624+
if position is not None:
625+
self._children.insert(position, new_element)
626+
else:
627+
self._children.append(new_element)
608628
self.namescope.increment_revision()
609629
return new_element
610630

dm_control/mjcf/element_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,26 @@ def testAdd(self):
198198
mujoco.add('default')
199199
self.assertIsNotNone(mujoco.default)
200200

201+
def testInsert(self):
202+
mujoco = element.RootElement(model='test')
203+
204+
# add in order
205+
mujoco.worldbody.add('body', name='0')
206+
mujoco.worldbody.add('body', name='1')
207+
mujoco.worldbody.add('body', name='2')
208+
209+
# insert into position 0, check order
210+
mujoco.worldbody.insert('body', name='foo', position=0)
211+
expected_order = ['foo', '0', '1', '2']
212+
for i, child in enumerate(mujoco.worldbody._children):
213+
self.assertEqual(child.name, expected_order[i])
214+
215+
# insert into position -1, check order
216+
mujoco.worldbody.insert('body', name='bar', position=-1)
217+
expected_order = ['foo', '0', '1', 'bar', '2']
218+
for i, child in enumerate(mujoco.worldbody._children):
219+
self.assertEqual(child.name, expected_order[i])
220+
201221
def testAddWithInvalidAttribute(self):
202222
mujoco = element.RootElement(model='test')
203223
with self.assertRaisesRegex(AttributeError, 'not a valid attribute'):

0 commit comments

Comments
 (0)