Skip to content

Commit

Permalink
Refactor for get_render()
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Oct 12, 2023
1 parent c9e9e5e commit b349467
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 45 deletions.
2 changes: 1 addition & 1 deletion examples/19_get_renders.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _(event: viser.GuiEvent) -> None:
line_width=3.0,
color=onp.random.uniform(size=3),
)
images.append(client.get_render(height=720, width=1280))
images.append(client.camera.get_render(height=720, width=1280))

print("Writing GIF...")
iio.imwrite("saved.gif", images)
Expand Down
72 changes: 36 additions & 36 deletions src/viser/_viser.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,37 +169,6 @@ def on_update(
self._state.camera_cb.append(callback)
return callback


@dataclasses.dataclass
class _ClientHandleState:
server: infra.Server
connection: infra.ClientConnection


@dataclasses.dataclass
class ClientHandle(MessageApi, GuiApi):
"""Handle for interacting with a specific client. Can be used to send messages to
individual clients and read/write camera information."""

client_id: int
"""Unique ID for this client."""
camera: CameraHandle
"""Handle for reading from and manipulating the client's viewport camera."""
_state: _ClientHandleState

def __post_init__(self):
super().__init__(self._state.connection)

@override
def _get_api(self) -> MessageApi:
"""Message API to use."""
return self

@override
def _queue_unsafe(self, message: _messages.Message) -> None:
"""Define how the message API should send messages."""
self._state.connection.send(message)

def get_render(
self, height: int, width: int, transport_format: Literal["png", "jpeg"] = "jpeg"
) -> onp.ndarray:
Expand All @@ -219,11 +188,13 @@ def get_render(
render_ready_event = threading.Event()
out: Optional[onp.ndarray] = None

connection = self.client._state.connection

def got_render_cb(
client_id: int, message: _messages.GetRenderResponseMessage
) -> None:
del client_id
self._state.connection.unregister_handler(
connection.unregister_handler(
_messages.GetRenderResponseMessage, got_render_cb
)
nonlocal out
Expand All @@ -233,10 +204,8 @@ def got_render_cb(
)
render_ready_event.set()

self._state.connection.register_handler(
_messages.GetRenderResponseMessage, got_render_cb
)
self._queue(
connection.register_handler(_messages.GetRenderResponseMessage, got_render_cb)
self.client._queue(
_messages.GetRenderRequestMessage(
"image/jpeg" if transport_format == "jpeg" else "image/png",
height=height,
Expand All @@ -251,6 +220,37 @@ def got_render_cb(
assert out is not None
return out


@dataclasses.dataclass
class _ClientHandleState:
server: infra.Server
connection: infra.ClientConnection


@dataclasses.dataclass
class ClientHandle(MessageApi, GuiApi):
"""Handle for interacting with a specific client. Can be used to send messages to
individual clients and read/write camera information."""

client_id: int
"""Unique ID for this client."""
camera: CameraHandle
"""Handle for reading from and manipulating the client's viewport camera."""
_state: _ClientHandleState

def __post_init__(self):
super().__init__(self._state.connection)

@override
def _get_api(self) -> MessageApi:
"""Message API to use."""
return self

@override
def _queue_unsafe(self, message: _messages.Message) -> None:
"""Define how the message API should send messages."""
self._state.connection.send(message)

@contextlib.contextmanager
def atomic(self) -> Generator[None, None, None]:
"""Returns a context where:
Expand Down
9 changes: 1 addition & 8 deletions src/viser/client/src/WebsocketInterface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,6 @@ export function FrameSynchronizedMessageHandler() {
const viewer = useContext(ViewerContext)!;
const messageQueueRef = viewer.messageQueueRef;

// We'll reuse the same canvas.
const renderBufferCanvas = React.useMemo(() => new OffscreenCanvas(1, 1), []);

useFrame(() => {
// Send a render along if it was requested!
if (viewer.getRenderRequestState.current === "triggered") {
Expand All @@ -709,11 +706,7 @@ export function FrameSynchronizedMessageHandler() {
const targetHeight = viewer.getRenderRequest.current!.height;

// We'll save a render to an intermediate canvas with the requested dimensions.
if (renderBufferCanvas.width !== targetWidth)
renderBufferCanvas.width = targetWidth;
if (renderBufferCanvas.height !== targetHeight)
renderBufferCanvas.height = targetHeight;

const renderBufferCanvas = new OffscreenCanvas(targetWidth, targetHeight);
const ctx = renderBufferCanvas.getContext("2d")!;
ctx.reset();
// Use a white background for JPEGs, which don't have an alpha channel.
Expand Down

0 comments on commit b349467

Please sign in to comment.