Skip to content

Commit

Permalink
API renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Jan 4, 2025
1 parent 6429191 commit 5802a20
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
20 changes: 12 additions & 8 deletions src/viser/_viser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
29 changes: 13 additions & 16 deletions src/viser/infra/_infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()))
Expand All @@ -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,
Expand All @@ -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(
Expand Down

0 comments on commit 5802a20

Please sign in to comment.