in mujoco_py/mjsim.pyx [0:0]
def save(self, file, format='xml', keep_inertials=False):
"""
Saves the simulator model and state to a file as either
a MuJoCo XML or MJB file. The current state is saved as
a keyframe in the model file. This is useful for debugging
using MuJoCo's `simulate` utility.
Note that this doesn't save the UDD-state which is
part of MjSimState, since that's not supported natively
by MuJoCo. If you want to save the model together with
the UDD-state, you should use the `get_xml` or `get_mjb`
methods on `MjModel` together with `MjSim.get_state` and
save them with e.g. pickle.
Args:
- file (IO stream): stream to write model to.
- format: format to use (either 'xml' or 'mjb')
- keep_inertials (bool): if False, removes all <inertial>
properties derived automatically for geoms by MuJoco. Note
that this removes ones that were provided by the user
as well.
"""
xml_str = self.model.get_xml()
dom = minidom.parseString(xml_str)
mujoco_node = dom.childNodes[0]
assert mujoco_node.tagName == 'mujoco'
keyframe_el = dom.createElement('keyframe')
key_el = dom.createElement('key')
keyframe_el.appendChild(key_el)
mujoco_node.appendChild(keyframe_el)
def str_array(arr):
return " ".join(map(str, arr))
key_el.setAttribute('time', str(self.data.time))
key_el.setAttribute('qpos', str_array(self.data.qpos))
key_el.setAttribute('qvel', str_array(self.data.qvel))
if self.data.act is not None:
key_el.setAttribute('act', str_array(self.data.act))
if not keep_inertials:
for element in dom.getElementsByTagName('inertial'):
element.parentNode.removeChild(element)
result_xml = remove_empty_lines(dom.toprettyxml(indent=" " * 4))
if format == 'xml':
file.write(result_xml)
elif format == 'mjb':
new_model = load_model_from_xml(result_xml)
file.write(new_model.get_mjb())
else:
raise ValueError("Unsupported format. Valid ones are 'xml' and 'mjb'")