Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array mutation bugs #319

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/viser/_scene_handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,17 @@ def __setattr__(self, name: str, value: Any) -> None:
elif current_value == value:
return

setattr(handle._impl.props, name, value)
# Update the value.
if isinstance(value, np.ndarray):
assert value.dtype == current_value.dtype
if value.shape == current_value.shape:
current_value[:] = value
else:
setattr(handle._impl.props, name, value.copy())
else:
# Non-array properties should be immutable, so no need to copy.
setattr(handle._impl.props, name, value)

handle._impl.api._websock_interface.queue_message(
_messages.SceneNodeUpdateMessage(handle.name, {name: value})
)
Expand Down Expand Up @@ -171,7 +181,7 @@ def _make(
assert isinstance(message, _messages.Message)
api._websock_interface.queue_message(message)

out = cls(_SceneNodeHandleState(name, copy.copy(message.props), api))
out = cls(_SceneNodeHandleState(name, copy.deepcopy(message.props), api))
api._handle_from_node_name[name] = out

out.wxyz = wxyz
Expand All @@ -198,7 +208,7 @@ def wxyz(self, wxyz: tuple[float, float, float, float] | np.ndarray) -> None:
wxyz_array = np.asarray(wxyz)
if np.allclose(wxyz_cast, self._impl.wxyz):
return
self._impl.wxyz = wxyz_array
self._impl.wxyz[:] = wxyz_array
self._impl.api._websock_interface.queue_message(
_messages.SetOrientationMessage(self._impl.name, wxyz_cast)
)
Expand All @@ -218,7 +228,7 @@ def position(self, position: tuple[float, float, float] | np.ndarray) -> None:
position_array = np.asarray(position)
if np.allclose(position_array, self._impl.position):
return
self._impl.position = position_array
self._impl.position[:] = position_array
self._impl.api._websock_interface.queue_message(
_messages.SetPositionMessage(self._impl.name, position_cast)
)
Expand Down Expand Up @@ -464,7 +474,7 @@ def wxyz(self, wxyz: tuple[float, float, float, float] | np.ndarray) -> None:
wxyz_array = np.asarray(wxyz)
if np.allclose(wxyz_cast, self._impl.wxyz):
return
self._impl.wxyz = wxyz_array
self._impl.wxyz[:] = wxyz_array
self._impl.websock_interface.queue_message(
_messages.SetBoneOrientationMessage(
self._impl.name, self._impl.bone_index, wxyz_cast
Expand All @@ -486,7 +496,7 @@ def position(self, position: tuple[float, float, float] | np.ndarray) -> None:
position_array = np.asarray(position)
if np.allclose(position_array, self._impl.position):
return
self._impl.position = position_array
self._impl.position[:] = position_array
self._impl.websock_interface.queue_message(
_messages.SetBonePositionMessage(
self._impl.name, self._impl.bone_index, position_cast
Expand Down
Loading