|
29 | 29 | from .infra._infra import StateSerializer |
30 | 30 |
|
31 | 31 |
|
| 32 | +@dataclasses.dataclass |
| 33 | +class InitialCameraConfig: |
| 34 | + """Configuration for the initial camera pose. |
| 35 | +
|
| 36 | + Accessed via :attr:`ViserServer.initial_camera`. Values set here are |
| 37 | + applied to new client connections and serialized scenes. |
| 38 | +
|
| 39 | + The API is designed to match :class:`CameraHandle`, which is used for |
| 40 | + per-client camera control. |
| 41 | + """ |
| 42 | + |
| 43 | + position: tuple[float, float, float] | npt.NDArray[np.floating] | None = None |
| 44 | + """Camera position in world coordinates.""" |
| 45 | + |
| 46 | + look_at: tuple[float, float, float] | npt.NDArray[np.floating] | None = None |
| 47 | + """Point the camera looks at in world coordinates.""" |
| 48 | + |
| 49 | + up: tuple[float, float, float] | npt.NDArray[np.floating] | None = None |
| 50 | + """Camera up direction.""" |
| 51 | + |
| 52 | + fov: float | None = None |
| 53 | + """Vertical field of view in radians.""" |
| 54 | + |
| 55 | + near: float | None = None |
| 56 | + """Near clipping plane distance.""" |
| 57 | + |
| 58 | + far: float | None = None |
| 59 | + """Far clipping plane distance.""" |
| 60 | + |
| 61 | + def _get_messages(self) -> list[_messages.Message]: |
| 62 | + """Get camera messages for all non-None fields. |
| 63 | +
|
| 64 | + Messages are marked with initial=True so the client can skip them |
| 65 | + if URL parameters were provided. |
| 66 | + """ |
| 67 | + messages: list[_messages.Message] = [] |
| 68 | + if self.position is not None: |
| 69 | + messages.append( |
| 70 | + _messages.SetCameraPositionMessage( |
| 71 | + cast_vector(self.position, 3), |
| 72 | + initial=True, |
| 73 | + ) |
| 74 | + ) |
| 75 | + if self.look_at is not None: |
| 76 | + messages.append( |
| 77 | + _messages.SetCameraLookAtMessage( |
| 78 | + cast_vector(self.look_at, 3), |
| 79 | + initial=True, |
| 80 | + ) |
| 81 | + ) |
| 82 | + if self.up is not None: |
| 83 | + messages.append( |
| 84 | + _messages.SetCameraUpDirectionMessage( |
| 85 | + cast_vector(self.up, 3), |
| 86 | + initial=True, |
| 87 | + ) |
| 88 | + ) |
| 89 | + if self.fov is not None: |
| 90 | + messages.append( |
| 91 | + _messages.SetCameraFovMessage(float(self.fov), initial=True) |
| 92 | + ) |
| 93 | + if self.near is not None: |
| 94 | + messages.append( |
| 95 | + _messages.SetCameraNearMessage(float(self.near), initial=True) |
| 96 | + ) |
| 97 | + if self.far is not None: |
| 98 | + messages.append( |
| 99 | + _messages.SetCameraFarMessage(float(self.far), initial=True) |
| 100 | + ) |
| 101 | + return messages |
| 102 | + |
| 103 | + |
32 | 104 | @dataclasses.dataclass |
33 | 105 | class _CameraHandleState: |
34 | 106 | """Information about a client's camera state.""" |
@@ -639,6 +711,7 @@ def __init__( |
639 | 711 |
|
640 | 712 | _client_autobuild.ensure_client_is_built() |
641 | 713 |
|
| 714 | + self._initial_camera = InitialCameraConfig() |
642 | 715 | self._connection = server |
643 | 716 | self._connected_clients: dict[int, ClientHandle] = {} |
644 | 717 | self._client_lock = threading.Lock() |
@@ -707,6 +780,10 @@ async def handle_camera_message( |
707 | 780 |
|
708 | 781 | conn.register_handler(_messages.ViewerCameraMessage, handle_camera_message) |
709 | 782 |
|
| 783 | + # Send initial camera messages. |
| 784 | + for msg in self._initial_camera._get_messages(): |
| 785 | + conn.queue_message(msg) |
| 786 | + |
710 | 787 | # Remove clients when they disconnect. |
711 | 788 | @server.on_client_disconnect |
712 | 789 | async def _(conn: infra.WebsockClientConnection) -> None: |
@@ -797,6 +874,21 @@ def request_share_url_no_return() -> None: # To suppress type error. |
797 | 874 | self.gui.reset() |
798 | 875 | self.gui.set_panel_label(label) |
799 | 876 |
|
| 877 | + @property |
| 878 | + def initial_camera(self) -> InitialCameraConfig: |
| 879 | + """Configuration for initial camera pose. |
| 880 | +
|
| 881 | + Set these values to control the initial camera position for new |
| 882 | + clients and serialized/embedded scenes. The API is designed to match |
| 883 | + :class:`viser.CameraHandle`, which is used for per-client camera control. |
| 884 | +
|
| 885 | + Example usage:: |
| 886 | +
|
| 887 | + server.initial_camera.position = (5.0, 5.0, 3.0) |
| 888 | + server.initial_camera.look_at = (0.0, 0.0, 0.0) |
| 889 | + """ |
| 890 | + return self._initial_camera |
| 891 | + |
800 | 892 | def _run_garbage_collector(self, force: bool = False) -> None: |
801 | 893 | """Clean up old messages. This is not elegant; a refactor of our |
802 | 894 | message persistence logic will significantly reduce complexity.""" |
@@ -1105,4 +1197,12 @@ def get_scene_serializer(self) -> StateSerializer: |
1105 | 1197 | # Insert current scene state. |
1106 | 1198 | for message in self._websock_server._broadcast_buffer.message_from_id.values(): |
1107 | 1199 | serializer._insert_message(message) |
| 1200 | + |
| 1201 | + # Prepend initial camera messages. |
| 1202 | + camera_messages = [ |
| 1203 | + (0.0, msg.as_serializable_dict()) |
| 1204 | + for msg in self._initial_camera._get_messages() |
| 1205 | + ] |
| 1206 | + serializer._messages = camera_messages + serializer._messages |
| 1207 | + |
1108 | 1208 | return serializer |
0 commit comments