Allow keyword arguments in attach.
PiperOrigin-RevId: 702718597 Change-Id: I1f1a5e9c88c8e102d5cbe5984511e7923ec7be3e
This commit is contained in:
committed by
Copybara-Service
parent
afc86ac1b3
commit
d1b02556ef
+31
-16
@@ -625,15 +625,19 @@ PYBIND11_MODULE(_specs, m) {
|
|||||||
py::return_value_policy::reference_internal);
|
py::return_value_policy::reference_internal);
|
||||||
mjsBody.def(
|
mjsBody.def(
|
||||||
"attach_frame",
|
"attach_frame",
|
||||||
[](raw::MjsBody& self, raw::MjsFrame& frame, std::string& prefix,
|
[](raw::MjsBody& self, raw::MjsFrame& frame,
|
||||||
std::string& suffix) -> raw::MjsFrame* {
|
std::optional<std::string>& prefix,
|
||||||
auto new_frame =
|
std::optional<std::string>& suffix) -> raw::MjsFrame* {
|
||||||
mjs_attachFrame(&self, &frame, prefix.c_str(), suffix.c_str());
|
const char* p = prefix.has_value() ? prefix.value().c_str() : "";
|
||||||
|
const char* s = suffix.has_value() ? suffix.value().c_str() : "";
|
||||||
|
auto new_frame = mjs_attachFrame(&self, &frame, p, s);
|
||||||
if (!new_frame) {
|
if (!new_frame) {
|
||||||
throw pybind11::value_error(mjs_getError(mjs_getSpec(self.element)));
|
throw pybind11::value_error(mjs_getError(mjs_getSpec(self.element)));
|
||||||
}
|
}
|
||||||
return new_frame;
|
return new_frame;
|
||||||
},
|
},
|
||||||
|
py::arg("frame"), py::arg("prefix") = py::none(),
|
||||||
|
py::arg("suffix") = py::none(),
|
||||||
py::return_value_policy::reference_internal);
|
py::return_value_policy::reference_internal);
|
||||||
mjsBody.def(
|
mjsBody.def(
|
||||||
"to_frame",
|
"to_frame",
|
||||||
@@ -654,34 +658,41 @@ PYBIND11_MODULE(_specs, m) {
|
|||||||
});
|
});
|
||||||
mjsFrame.def(
|
mjsFrame.def(
|
||||||
"attach_body",
|
"attach_body",
|
||||||
[](raw::MjsFrame& self, raw::MjsBody& body, std::string& prefix,
|
[](raw::MjsFrame& self, raw::MjsBody& body,
|
||||||
std::string& suffix) -> raw::MjsBody* {
|
std::optional<std::string>& prefix,
|
||||||
auto new_body =
|
std::optional<std::string>& suffix) -> raw::MjsBody* {
|
||||||
mjs_attachBody(&self, &body, prefix.c_str(), suffix.c_str());
|
const char* p = prefix.has_value() ? prefix.value().c_str() : "";
|
||||||
|
const char* s = suffix.has_value() ? suffix.value().c_str() : "";
|
||||||
|
auto new_body = mjs_attachBody(&self, &body, p, s);
|
||||||
if (!new_body) {
|
if (!new_body) {
|
||||||
throw pybind11::value_error(
|
throw pybind11::value_error(
|
||||||
mjs_getError(mjs_getSpec(self.element)));
|
mjs_getError(mjs_getSpec(self.element)));
|
||||||
}
|
}
|
||||||
return new_body;
|
return new_body;
|
||||||
},
|
},
|
||||||
|
py::arg("body"), py::arg("prefix") = py::none(),
|
||||||
|
py::arg("suffix") = py::none(),
|
||||||
py::return_value_policy::reference_internal);
|
py::return_value_policy::reference_internal);
|
||||||
mjsFrame.def(
|
mjsFrame.def(
|
||||||
"attach",
|
"attach",
|
||||||
[](raw::MjsFrame& self, MjSpec& spec, std::string& prefix,
|
[](raw::MjsFrame& self, MjSpec& spec, std::optional<std::string>& prefix,
|
||||||
std::string& suffix) -> raw::MjsFrame* {
|
std::optional<std::string>& suffix) -> raw::MjsFrame* {
|
||||||
auto world = mjs_findBody(spec.ptr, "world");
|
auto world = mjs_findBody(spec.ptr, "world");
|
||||||
if (!world) {
|
if (!world) {
|
||||||
throw pybind11::value_error(
|
throw pybind11::value_error(
|
||||||
mjs_getError(mjs_getSpec(self.element)));
|
mjs_getError(mjs_getSpec(self.element)));
|
||||||
}
|
}
|
||||||
auto attached_world =
|
const char* p = prefix.has_value() ? prefix.value().c_str() : "";
|
||||||
mjs_attachBody(&self, world, prefix.c_str(), suffix.c_str());
|
const char* s = suffix.has_value() ? suffix.value().c_str() : "";
|
||||||
|
auto attached_world = mjs_attachBody(&self, world, p, s);
|
||||||
if (!attached_world) {
|
if (!attached_world) {
|
||||||
throw pybind11::value_error(
|
throw pybind11::value_error(
|
||||||
mjs_getError(mjs_getSpec(self.element)));
|
mjs_getError(mjs_getSpec(self.element)));
|
||||||
}
|
}
|
||||||
return mjs_bodyToFrame(&attached_world);
|
return mjs_bodyToFrame(&attached_world);
|
||||||
},
|
},
|
||||||
|
py::arg("spec"), py::arg("prefix") = py::none(),
|
||||||
|
py::arg("suffix") = py::none(),
|
||||||
py::return_value_policy::reference_internal);
|
py::return_value_policy::reference_internal);
|
||||||
|
|
||||||
// ============================= MJSGEOM =====================================
|
// ============================= MJSGEOM =====================================
|
||||||
@@ -730,16 +741,20 @@ PYBIND11_MODULE(_specs, m) {
|
|||||||
py::return_value_policy::reference_internal);
|
py::return_value_policy::reference_internal);
|
||||||
mjsSite.def(
|
mjsSite.def(
|
||||||
"attach",
|
"attach",
|
||||||
[](raw::MjsSite& self, raw::MjsBody& body, std::string& prefix,
|
[](raw::MjsSite& self, raw::MjsBody& body,
|
||||||
std::string& suffix) -> raw::MjsBody* {
|
std::optional<std::string>& prefix,
|
||||||
auto new_body =
|
std::optional<std::string>& suffix) -> raw::MjsBody* {
|
||||||
mjs_attachToSite(&self, &body, prefix.c_str(), suffix.c_str());
|
const char* p = prefix.has_value() ? prefix.value().c_str() : "";
|
||||||
|
const char* s = suffix.has_value() ? suffix.value().c_str() : "";
|
||||||
|
auto new_body = mjs_attachToSite(&self, &body, p, s);
|
||||||
if (!new_body) {
|
if (!new_body) {
|
||||||
throw pybind11::value_error(
|
throw pybind11::value_error(
|
||||||
mjs_getError(mjs_getSpec(self.element)));
|
mjs_getError(mjs_getSpec(self.element)));
|
||||||
}
|
}
|
||||||
return new_body;
|
return new_body;
|
||||||
},
|
},
|
||||||
|
py::arg("body"), py::arg("prefix") = py::none(),
|
||||||
|
py::arg("suffix") = py::none(),
|
||||||
py::return_value_policy::reference_internal);
|
py::return_value_policy::reference_internal);
|
||||||
|
|
||||||
// ============================= MJSCAMERA ===================================
|
// ============================= MJSCAMERA ===================================
|
||||||
|
|||||||
@@ -867,7 +867,7 @@ class SpecsTest(absltest.TestCase):
|
|||||||
parent.compiler.degree = not child.compiler.degree
|
parent.compiler.degree = not child.compiler.degree
|
||||||
body = child.worldbody.add_body(euler=[90, 0, 0])
|
body = child.worldbody.add_body(euler=[90, 0, 0])
|
||||||
frame = parent.worldbody.add_frame(euler=[-mujoco.mjPI / 2, 0, 0])
|
frame = parent.worldbody.add_frame(euler=[-mujoco.mjPI / 2, 0, 0])
|
||||||
frame.attach_body(body, 'child-', '')
|
frame.attach_body(body, prefix='child-')
|
||||||
model = parent.compile()
|
model = parent.compile()
|
||||||
np.testing.assert_almost_equal(model.body_quat[1], [1, 0, 0, 0])
|
np.testing.assert_almost_equal(model.body_quat[1], [1, 0, 0, 0])
|
||||||
|
|
||||||
@@ -876,7 +876,7 @@ class SpecsTest(absltest.TestCase):
|
|||||||
parent = mujoco.MjSpec()
|
parent = mujoco.MjSpec()
|
||||||
site = parent.worldbody.add_site(pos=[1, 2, 3])
|
site = parent.worldbody.add_site(pos=[1, 2, 3])
|
||||||
body = child.worldbody.add_body()
|
body = child.worldbody.add_body()
|
||||||
self.assertIsNotNone(site.attach(body, '_', ''))
|
self.assertIsNotNone(site.attach(body, prefix='_'))
|
||||||
model = parent.compile()
|
model = parent.compile()
|
||||||
np.testing.assert_array_equal(model.body_pos[1], [1, 2, 3])
|
np.testing.assert_array_equal(model.body_pos[1], [1, 2, 3])
|
||||||
|
|
||||||
@@ -892,7 +892,7 @@ class SpecsTest(absltest.TestCase):
|
|||||||
child.worldbody.add_camera(name='camera')
|
child.worldbody.add_camera(name='camera')
|
||||||
parent = mujoco.MjSpec()
|
parent = mujoco.MjSpec()
|
||||||
frame = parent.worldbody.add_frame(name='frame')
|
frame = parent.worldbody.add_frame(name='frame')
|
||||||
frame.attach(child, 'child-', '')
|
frame.attach(child, prefix='child-')
|
||||||
self.assertLen(child.cameras, 1)
|
self.assertLen(child.cameras, 1)
|
||||||
self.assertLen(parent.bodies, 1)
|
self.assertLen(parent.bodies, 1)
|
||||||
self.assertLen(parent.frames, 2)
|
self.assertLen(parent.frames, 2)
|
||||||
|
|||||||
Reference in New Issue
Block a user