Yield ownership of vector<double>, vector<float>, and vector<int> to mjSpec in Python bindings.

Fixes #2756

PiperOrigin-RevId: 785417251
Change-Id: Ib399c46ee59585258ace7d9583c68b7d06d0a9e4
This commit is contained in:
Alessio Quaglino
2025-07-21 06:43:28 -07:00
committed by Copybara-Service
parent 60f9b34a77
commit fc13995dd4
3 changed files with 13 additions and 6 deletions
@@ -193,7 +193,7 @@ def _ptr_binding_code(
[]({rawclassname}& self, std::string_view {varname}) {{
*(self.{fullvarname}) = {varname};
}});"""
elif ( # C++ vectors of values -> Python array
elif ( # C++ vectors of values -> custom array
vartype == 'mjDoubleVec'
or vartype == 'mjFloatVec'
or vartype == 'mjIntVec'
@@ -202,9 +202,9 @@ def _ptr_binding_code(
return f"""\
{classname}.def_property(
"{varname}",
[]({rawclassname}& self) -> py::array_t<{vartype}> {{
return py::array_t<{vartype}>(self.{fullvarname}->size(),
self.{fullvarname}->data());
[]({rawclassname}& self) -> MjTypeVec<{vartype}> {{
return MjTypeVec<{vartype}>(self.{fullvarname}->data(),
self.{fullvarname}->size());
}},
[]({rawclassname}& self, py::object rhs) {{
self.{fullvarname}->clear();
@@ -212,7 +212,7 @@ def _ptr_binding_code(
for (auto val : rhs) {{
self.{fullvarname}->push_back(py::cast<{vartype}>(val));
}}
}}, py::return_value_policy::reference_internal);"""
}}, py::return_value_policy::move);"""
elif vartype == 'mjByteVec':
return f"""\
{classname}.def_property(