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

SceneNodeHandle cleanup, bump version #113

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "viser"
version = "0.1.5"
version = "0.1.6"
description = "3D visualization + Python"
readme = "README.md"
license = { text="MIT" }
Expand Down
9 changes: 5 additions & 4 deletions src/viser/_message_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
PointCloudHandle,
SceneNodeHandle,
TransformControlsHandle,
_SupportsVisibility,
_TransformControlsState,
)

Expand Down Expand Up @@ -353,10 +352,11 @@ def add_label(
text: str,
wxyz: Tuple[float, float, float, float] | onp.ndarray = (1.0, 0.0, 0.0, 0.0),
position: Tuple[float, float, float] | onp.ndarray = (0.0, 0.0, 0.0),
visible: bool = True,
) -> LabelHandle:
"""Add a 2D label to the scene."""
self._queue(_messages.LabelMessage(name, text))
return LabelHandle._make(self, name, wxyz, position)
return LabelHandle._make(self, name, wxyz, position, visible=visible)

def add_point_cloud(
self,
Expand Down Expand Up @@ -562,7 +562,7 @@ def sync_cb(client_id: ClientId, state: TransformControlsHandle) -> None:
message_position.excluded_self_client = client_id
self._queue(message_position)

node_handle = _SupportsVisibility._make(self, name, wxyz, position, visible)
node_handle = SceneNodeHandle._make(self, name, wxyz, position, visible)
state_aux = _TransformControlsState(
last_updated=time.time(),
update_cb=[],
Expand Down Expand Up @@ -633,6 +633,7 @@ def add_3d_gui_container(
name: str,
wxyz: Tuple[float, float, float, float] | onp.ndarray = (1.0, 0.0, 0.0, 0.0),
position: Tuple[float, float, float] | onp.ndarray = (0.0, 0.0, 0.0),
visible: bool = True,
) -> Gui3dContainerHandle:
"""Add a 3D gui container to the scene. The returned container handle can be
used as a context to place GUI elements into the 3D scene."""
Expand All @@ -658,5 +659,5 @@ def add_3d_gui_container(
container_id=container_id,
)
)
node_handle = SceneNodeHandle._make(self, name, wxyz, position)
node_handle = SceneNodeHandle._make(self, name, wxyz, position, visible=visible)
return Gui3dContainerHandle(node_handle._impl, gui_api, container_id)
66 changes: 23 additions & 43 deletions src/viser/_scene_handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@


TSceneNodeHandle = TypeVar("TSceneNodeHandle", bound="SceneNodeHandle")
TSupportsVisibility = TypeVar("TSupportsVisibility", bound="_SupportsVisibility")


@dataclasses.dataclass
Expand Down Expand Up @@ -60,12 +59,14 @@ def _make(
name: str,
wxyz: Tuple[float, float, float, float] | onp.ndarray,
position: Tuple[float, float, float] | onp.ndarray,
visible: bool,
) -> TSceneNodeHandle:
out = cls(_SceneNodeHandleState(name, api))
api._handle_from_node_name[name] = out

out.wxyz = wxyz
out.position = position
out.visible = visible
return out

@property
Expand Down Expand Up @@ -102,6 +103,18 @@ def position(self, position: Tuple[float, float, float] | onp.ndarray) -> None:
_messages.SetPositionMessage(self._impl.name, position_cast)
)

@property
def visible(self) -> bool:
"""Whether the scene node is visible or not. Synchronized to clients automatically when assigned."""
return self._impl.visible

@visible.setter
def visible(self, visible: bool) -> None:
self._impl.api._queue(
_messages.SetSceneNodeVisibilityMessage(self._impl.name, visible)
)
self._impl.visible = visible

def remove(self) -> None:
"""Remove the node from the scene."""
self._impl.api._queue(_messages.RemoveSceneNodeMessage(self._impl.name))
Expand All @@ -114,7 +127,7 @@ class ClickEvent(Generic[TSceneNodeHandle]):


@dataclasses.dataclass
class _SupportsClick(SceneNodeHandle):
class _ClickableSceneNodeHandle(SceneNodeHandle):
def on_click(
self: TSceneNodeHandle, func: Callable[[ClickEvent[TSceneNodeHandle]], None]
) -> Callable[[ClickEvent[TSceneNodeHandle]], None]:
Expand All @@ -134,71 +147,38 @@ def on_click(


@dataclasses.dataclass
class _SupportsVisibility(SceneNodeHandle):
@classmethod
def _make(
cls: Type[TSupportsVisibility],
api: MessageApi,
name: str,
wxyz: Tuple[float, float, float, float] | onp.ndarray,
position: Tuple[float, float, float] | onp.ndarray,
visible: bool = True,
) -> TSupportsVisibility:
out = cls(_SceneNodeHandleState(name, api))
api._handle_from_node_name[name] = out

out.wxyz = wxyz
out.position = position
out.visible = visible

return out

@property
def visible(self) -> bool:
"""Whether the scene node is visible or not. Synchronized to clients automatically when assigned."""
return self._impl.visible

@visible.setter
def visible(self, visible: bool) -> None:
self._impl.api._queue(
_messages.SetSceneNodeVisibilityMessage(self._impl.name, visible)
)
self._impl.visible = visible


@dataclasses.dataclass
class CameraFrustumHandle(_SupportsClick, _SupportsVisibility):
class CameraFrustumHandle(_ClickableSceneNodeHandle):
"""Handle for camera frustums."""


@dataclasses.dataclass
class PointCloudHandle(_SupportsVisibility):
class PointCloudHandle(SceneNodeHandle):
"""Handle for point clouds. Does not support click events."""


@dataclasses.dataclass
class FrameHandle(_SupportsClick, _SupportsVisibility):
class FrameHandle(_ClickableSceneNodeHandle):
"""Handle for coordinate frames."""


@dataclasses.dataclass
class MeshHandle(_SupportsClick, _SupportsVisibility):
class MeshHandle(_ClickableSceneNodeHandle):
"""Handle for mesh objects."""


@dataclasses.dataclass
class GlbHandle(_SupportsClick, _SupportsVisibility):
class GlbHandle(_ClickableSceneNodeHandle):
"""Handle for GLB objects."""


@dataclasses.dataclass
class ImageHandle(_SupportsClick, _SupportsVisibility):
class ImageHandle(_ClickableSceneNodeHandle):
"""Handle for 2D images, rendered in 3D."""


@dataclasses.dataclass
class LabelHandle(SceneNodeHandle):
"""Handle for 2D label objects. Does not support click events or visibility toggling."""
"""Handle for 2D label objects. Does not support click events."""


@dataclasses.dataclass
Expand All @@ -209,7 +189,7 @@ class _TransformControlsState:


@dataclasses.dataclass
class TransformControlsHandle(_SupportsClick, _SupportsVisibility):
class TransformControlsHandle(_ClickableSceneNodeHandle):
"""Handle for interacting with transform control gizmos."""

_impl_aux: _TransformControlsState
Expand Down
Loading