Skip to content

Commit

Permalink
Fix assigning bytes to texture data.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 706747323
Change-Id: I6b0ee67d4f35db3a149ba8d9979546d3c712bb8b
  • Loading branch information
quagla authored and copybara-github committed Dec 16, 2024
1 parent a7eb6ef commit 644dfc7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions python/mujoco/codegen/generate_spec_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,12 @@ def _ptr_binding_code(
return MjTypeVec<std::byte>(self.{fullvarname}->data(),
self.{fullvarname}->size());
}},
[]({rawclassname}& self, py::object rhs) {{
[]({rawclassname}& self, py::bytes& rhs) {{
self.{fullvarname}->clear();
self.{fullvarname}->reserve(py::len(rhs));
for (auto val : rhs) {{
self.{fullvarname}->push_back(py::cast<const std::byte>(val));
std::string_view rhs_view = py::cast<std::string_view>(rhs);
for (auto val : rhs_view) {{
self.{fullvarname}->push_back(static_cast<std::byte>(val));
}}
}}, py::return_value_policy::move);"""
elif vartype == 'mjStringVec':
Expand Down
6 changes: 6 additions & 0 deletions python/mujoco/specs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,12 @@ def test_assign_list_element(self):
with self.assertRaises(IndexError):
material.textures[-1] = 'x'

def test_assign_texture(self):
spec = mujoco.MjSpec()
texture = spec.add_texture(name='texture', height=2, width=2)
texture.data = np.zeros((2, 2, 3), dtype=np.uint8).tobytes()
spec.compile()

def test_attach_units(self):
child = mujoco.MjSpec()
parent = mujoco.MjSpec()
Expand Down

0 comments on commit 644dfc7

Please sign in to comment.