Enable to attach a spec to a frame.

PiperOrigin-RevId: 699703988
Change-Id: Idbddcbce1e68286248458eb0bd24d5080451a58a
This commit is contained in:
Alessio Quaglino
2024-11-24 07:34:16 -08:00
committed by Copybara-Service
parent 3a12db9ad2
commit f909d63cdf
4 changed files with 68 additions and 1 deletions
+18
View File
@@ -665,6 +665,24 @@ PYBIND11_MODULE(_specs, m) {
return new_body;
},
py::return_value_policy::reference_internal);
mjsFrame.def(
"attach",
[](raw::MjsFrame& self, MjSpec& spec, std::string& prefix,
std::string& suffix) -> raw::MjsFrame* {
auto world = mjs_findBody(spec.ptr, "world");
if (!world) {
throw pybind11::value_error(
mjs_getError(mjs_getSpec(self.element)));
}
auto attached_world =
mjs_attachBody(&self, world, prefix.c_str(), suffix.c_str());
if (!attached_world) {
throw pybind11::value_error(
mjs_getError(mjs_getSpec(self.element)));
}
return mjs_bodyToFrame(&attached_world);
},
py::return_value_policy::reference_internal);
// ============================= MJSGEOM =====================================
mjsGeom.def("delete", [](raw::MjsGeom& self) { mjs_delete(self.element); });
+13
View File
@@ -884,6 +884,19 @@ class SpecsTest(absltest.TestCase):
frame = body.to_frame()
np.testing.assert_array_equal(frame.pos, [1, 2, 3])
def test_attach_spec_to_frame(self):
child = mujoco.MjSpec()
child.worldbody.add_camera(name='camera')
parent = mujoco.MjSpec()
frame = parent.worldbody.add_frame(name='frame')
frame.attach(child, 'child-', '')
self.assertLen(child.cameras, 1)
self.assertLen(parent.bodies, 1)
self.assertLen(parent.frames, 2)
self.assertEqual(parent.cameras[0].name, 'child-camera')
self.assertEqual(parent.frames[0].name, 'frame')
self.assertEqual(parent.frames[1].name, '')
if __name__ == '__main__':
absltest.main()