From 5802a20ef4bb1e021daf110d84ab658581d5f81c Mon Sep 17 00:00:00 2001 From: brentyi Date: Fri, 3 Jan 2025 20:11:37 -0800 Subject: [PATCH] API renaming --- src/viser/_viser.py | 20 ++++++++++++-------- src/viser/infra/_infra.py | 29 +++++++++++++---------------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/viser/_viser.py b/src/viser/_viser.py index 67cbc4d4..2d34abe6 100644 --- a/src/viser/_viser.py +++ b/src/viser/_viser.py @@ -27,7 +27,7 @@ from ._notification_handle import NotificationHandle, _NotificationHandleState from ._scene_api import SceneApi, cast_vector from ._tunnel import ViserTunnel -from .infra._infra import RecordHandle +from .infra._infra import StateSerializer class _BackwardsCompatibilityShim: @@ -992,17 +992,21 @@ def sleep_forever(self) -> None: while True: time.sleep(3600) - def _start_scene_recording(self) -> RecordHandle: - """Start recording outgoing messages for playback or embedding. - Includes only the scene. + def _start_scene_recording(self) -> None: + """**Old API.**""" + assert False, "_start_scene_recording() has been removed. See notes in https://github.com/nerfstudio-project/viser/pull/357 for the new API." - **Work-in-progress.** This API may be changed or removed. + def get_scene_serializer(self) -> StateSerializer: + """Get handle for serializing the scene state. + + This can be used for saving .viser files, which are used for offline + visualization. """ - recorder = self._websock_server.start_recording( + serializer = self._websock_server.get_message_serializer( # Don't record GUI messages. This feels brittle. filter=lambda message: "Gui" not in type(message).__name__ ) # Insert current scene state. for message in self._websock_server._broadcast_buffer.message_from_id.values(): - recorder._insert_message(message) - return recorder + serializer._insert_message(message) + return serializer diff --git a/src/viser/infra/_infra.py b/src/viser/infra/_infra.py index 10c7735b..f86539da 100644 --- a/src/viser/infra/_infra.py +++ b/src/viser/infra/_infra.py @@ -41,10 +41,9 @@ class _ClientHandleState: TMessage = TypeVar("TMessage", bound=Message) -class RecordHandle: - """**Experimental.** - - Handle for recording outgoing messages. Useful for logging + debugging.""" +class StateSerializer: + """Handle for serializing messages. In Viser, this is used to save the + scene state.""" def __init__( self, handler: WebsockMessageHandler, filter: Callable[[Message], bool] @@ -57,7 +56,8 @@ def __init__( def _insert_message(self, message: Message) -> None: """Insert a message into the recorded file.""" - # Exclude GUI messages. This is hacky. + # Exclude messages that are filtered out. In Viser, this is typically + # GUI messages. if not self._filter(message): return self._messages.append((self._time, message.as_serializable_dict())) @@ -66,14 +66,9 @@ def insert_sleep(self, duration: float) -> None: """Insert a sleep into the recorded file.""" self._time += duration - def set_loop_start(self) -> None: - """Mark the start of the loop. Messages sent after this point will be - looped. Should only be called once.""" - pass - - def end_and_serialize(self) -> bytes: - """End the recording and serialize contents. Returns the recording as - bytes, which should generally be written to a file.""" + def serialize(self) -> bytes: + """Serialize saved messages. Returns the recording as bytes, which + should be written to a file.""" packed_bytes = msgspec.msgpack.encode( { "durationSeconds": self._time, @@ -96,13 +91,15 @@ def __init__(self) -> None: self._locked_thread_id = -1 # Set to None if not recording. - self._record_handle: RecordHandle | None = None + self._record_handle: StateSerializer | None = None - def start_recording(self, filter: Callable[[Message], bool]) -> RecordHandle: + def get_message_serializer( + self, filter: Callable[[Message], bool] + ) -> StateSerializer: """Start recording messages that are sent. Sent messages will be serialized and can be used for playback.""" assert self._record_handle is None, "Already recording." - self._record_handle = RecordHandle(self, filter) + self._record_handle = StateSerializer(self, filter) return self._record_handle def register_handler(