mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-02 05:48:57 +08:00
Viewer refactor with state sync (#212)
* initial commit for viewer refactor * updating name change to nerfactory * adding headers * making the rendering fix which broke from a merge * things are starting to work. removing old commands now * removed prints * cleanup * updating state works now even for nested dictionaries * updating state in redux. listening for state changes. trying to draw three.js now * added drawing functionality. need to fix translation of frustums and image planes * images and frustums drawn correctly * fixing lint * github actions * back to npm * fixing eslint * remove package.json from root * new favicon * some viewer cleanup * notebook * fixed camera drawings speed to avoid redraw * trying to get Leva to update * quick fix * synced config panel * adding notebook * fixing output print error
This commit is contained in:
1 parent
966374474d
commit
73fedc7593
67 files changed
+2510
-14057
No files matched your search
@@ -16,7 +16,7 @@
|
||||
|
||||
|
||||
import sys
|
||||
from typing import Callable, List, Optional
|
||||
from typing import Callable, List, Optional, Tuple
|
||||
|
||||
import msgpack
|
||||
import msgpack_numpy
|
||||
@@ -31,24 +31,14 @@ from aiortc import RTCPeerConnection, RTCSessionDescription
|
||||
from aiortc.rtcrtpsender import RTCRtpSender
|
||||
from zmq.eventloop.zmqstream import ZMQStream
|
||||
|
||||
from nerfactory.viewer.server.tree import SceneTree, find_node, walk
|
||||
from nerfactory.viewer.server.state.node import find_node, get_tree, walk
|
||||
from nerfactory.viewer.server.state.state_node import StateNode
|
||||
from nerfactory.viewer.server.video_stream import SingleFrameStreamTrack
|
||||
|
||||
MAX_ATTEMPTS = 1000
|
||||
DEFAULT_ZMQ_METHOD = "tcp"
|
||||
DEFAULT_ZMQ_PORT = 6000
|
||||
DEFAULT_WEBSOCKET_PORT = 8051
|
||||
WEBSOCKET_COMMANDS = [
|
||||
"set_transform",
|
||||
"set_object",
|
||||
"set_output_options",
|
||||
"set_output_type",
|
||||
"set_training_state",
|
||||
"get_object",
|
||||
"set_property",
|
||||
"delete",
|
||||
]
|
||||
WEBRTC_COMMANDS = ["set_image"]
|
||||
|
||||
|
||||
def find_available_port(func: Callable, default_port: int, max_attempts: int = MAX_ATTEMPTS, **kwargs) -> None:
|
||||
@@ -105,7 +95,8 @@ class WebSocketHandler(tornado.websocket.WebSocketHandler): # pylint: disable=a
|
||||
self.bridge.send_scene(self)
|
||||
|
||||
async def on_message(self, message: bytearray): # pylint: disable=invalid-overridden-method
|
||||
"""On reception of message, parses the message and calls the appropriate function based on the type of command
|
||||
"""On reception of message from the websocket,
|
||||
parses the message and calls the appropriate function based on the type of command
|
||||
|
||||
Args:
|
||||
message: byte message to parse
|
||||
@@ -115,22 +106,23 @@ class WebSocketHandler(tornado.websocket.WebSocketHandler): # pylint: disable=a
|
||||
type_ = m["type"]
|
||||
path = list(filter(lambda x: len(x) > 0, m["path"].split("/")))
|
||||
|
||||
if type_ == "set_transform":
|
||||
find_node(self.bridge.tree, path).transform = data
|
||||
elif type_ == "set_object":
|
||||
find_node(self.bridge.tree, path).object = data
|
||||
find_node(self.bridge.tree, path).properties = []
|
||||
elif type_ == "set_output_options":
|
||||
find_node(self.bridge.tree, path).object = data
|
||||
elif type_ == "set_output_type":
|
||||
find_node(self.bridge.tree, path).object = data
|
||||
elif type_ == "set_max_resolution":
|
||||
find_node(self.bridge.tree, path).object = data
|
||||
elif type_ == "set_min_resolution":
|
||||
find_node(self.bridge.tree, path).object = data
|
||||
elif type_ == "set_training_state":
|
||||
find_node(self.bridge.tree, path).object = data
|
||||
if type_ == "write":
|
||||
# writes the data coming from the websocket
|
||||
find_node(self.bridge.state_tree, path).data = m["data"]
|
||||
if m["path"] != "renderingState/camera":
|
||||
# dispatch to the websockets if not a camera update!
|
||||
# TODO(ethan): handle the camera properly!
|
||||
# TODO: but don't update the current websocket
|
||||
command = {"type": "write", "path": m["path"], "data": m["data"]}
|
||||
packed_data = umsgpack.packb(command)
|
||||
frames = ["write".encode("utf-8"), m["path"].encode("utf-8"), packed_data]
|
||||
self.bridge.forward_to_websockets(frames, websocket_to_skip=self)
|
||||
elif type_ == "read":
|
||||
# reads and returns the data
|
||||
data = find_node(self.bridge.state_tree, path).data
|
||||
self.write_message(data, binary=True)
|
||||
elif type_ == "offer":
|
||||
# returns the description to for WebRTC to the specific websocket connection
|
||||
offer = RTCSessionDescription(m["data"]["sdp"], m["data"]["type"])
|
||||
|
||||
pc = RTCPeerConnection()
|
||||
@@ -139,7 +131,7 @@ class WebSocketHandler(tornado.websocket.WebSocketHandler): # pylint: disable=a
|
||||
video = SingleFrameStreamTrack()
|
||||
self.bridge.video_tracks.add(video)
|
||||
_ = pc.addTrack(video)
|
||||
# TODO(eventually do something with the codec)
|
||||
# TODO: do something with the codec
|
||||
# video_sender = pc.addTrack(video)
|
||||
# force_codec(pc, video_sender, video_codec)
|
||||
|
||||
@@ -154,6 +146,14 @@ class WebSocketHandler(tornado.websocket.WebSocketHandler): # pylint: disable=a
|
||||
}
|
||||
data = umsgpack.packb(cmd_data)
|
||||
self.write_message(data, binary=True)
|
||||
else:
|
||||
cmd_data = {
|
||||
"type": "error",
|
||||
"path": "",
|
||||
"data": {"error": "Unknown command type: " + type_},
|
||||
}
|
||||
data = umsgpack.packb(cmd_data)
|
||||
self.write_message(data, binary=True)
|
||||
|
||||
def on_close(self):
|
||||
self.bridge.websocket_pool.remove(self)
|
||||
@@ -196,7 +196,7 @@ class ZMQWebSocketBridge:
|
||||
self.app.listen(websocket_port, **listen_kwargs)
|
||||
self.websocket_port = websocket_port
|
||||
|
||||
self.tree = SceneTree()
|
||||
self.state_tree = get_tree(StateNode)
|
||||
|
||||
def __str__(self) -> str:
|
||||
class_name = self.__class__.__name__
|
||||
@@ -212,61 +212,37 @@ class ZMQWebSocketBridge:
|
||||
Args:
|
||||
frames: the list containing command + object to be placed in tree
|
||||
"""
|
||||
cmd = frames[0].decode("utf-8")
|
||||
print(cmd)
|
||||
if len(frames) != 3:
|
||||
self.zmq_socket.send(b"error: expected 3 frames")
|
||||
return
|
||||
type_ = frames[0].decode("utf-8")
|
||||
path = list(filter(lambda x: len(x) > 0, frames[1].decode("utf-8").split("/")))
|
||||
data = frames[2]
|
||||
if cmd in WEBSOCKET_COMMANDS:
|
||||
if cmd != "get_object":
|
||||
self.forward_to_websockets(frames)
|
||||
if cmd == "set_transform":
|
||||
find_node(self.tree, path).transform = data
|
||||
elif cmd == "set_object":
|
||||
find_node(self.tree, path).object = data
|
||||
find_node(self.tree, path).properties = []
|
||||
elif cmd == "set_output_options":
|
||||
find_node(self.tree, path).object = data
|
||||
elif cmd == "set_output_type":
|
||||
find_node(self.tree, path).object = data
|
||||
elif cmd == "set_max_resolution":
|
||||
find_node(self.tree, path).object = data
|
||||
elif cmd == "set_min_resolution":
|
||||
find_node(self.tree, path).object = data
|
||||
elif cmd == "set_training_state":
|
||||
find_node(self.tree, path).object = data
|
||||
elif cmd == "get_object":
|
||||
data = find_node(self.tree, path).object
|
||||
if isinstance(data, type(None)):
|
||||
data = umsgpack.packb("error: object not found")
|
||||
self.zmq_socket.send(data)
|
||||
return
|
||||
elif cmd == "set_property":
|
||||
find_node(self.tree, path).properties.append(data)
|
||||
elif cmd == "delete":
|
||||
if len(path) > 0:
|
||||
parent = find_node(self.tree, path[:-1])
|
||||
child = path[-1]
|
||||
if child in parent:
|
||||
del parent[child]
|
||||
else:
|
||||
self.tree = SceneTree()
|
||||
elif cmd in WEBRTC_COMMANDS:
|
||||
if cmd == "set_image":
|
||||
image = msgpack.unpackb(
|
||||
data, object_hook=msgpack_numpy.decode, use_list=False, max_bin_len=50000000, raw=False
|
||||
)
|
||||
for video_track in self.video_tracks:
|
||||
video_track.put_frame(image)
|
||||
else:
|
||||
self.zmq_socket.send(b"error: unknown command")
|
||||
return
|
||||
self.zmq_socket.send(b"ok")
|
||||
return
|
||||
|
||||
def forward_to_websockets(self, frames: List[bytes]):
|
||||
if type_ == "write":
|
||||
# TODO: use state_tree name
|
||||
unpacked_data = umsgpack.unpackb(data)
|
||||
find_node(self.state_tree, path).data = unpacked_data["data"]
|
||||
self.forward_to_websockets(frames)
|
||||
self.zmq_socket.send(umsgpack.packb(b"ok"))
|
||||
elif type_ == "read":
|
||||
# walk the node from the specified to get the full state dictionary
|
||||
# TODO(ethan): handle the "data" key...
|
||||
read_data = find_node(self.state_tree, path).data
|
||||
self.zmq_socket.send(umsgpack.packb(read_data))
|
||||
elif type_ == "set_image":
|
||||
image = msgpack.unpackb(
|
||||
data, object_hook=msgpack_numpy.decode, use_list=False, max_bin_len=50000000, raw=False
|
||||
)
|
||||
for video_track in self.video_tracks:
|
||||
video_track.put_frame(image)
|
||||
self.zmq_socket.send(umsgpack.packb(b"ok"))
|
||||
else:
|
||||
self.zmq_socket.send(umsgpack.packb(b"error: unknown command"))
|
||||
|
||||
def forward_to_websockets(
|
||||
self, frames: Tuple[str, str, bytes], websocket_to_skip: Optional[WebSocketHandler] = None
|
||||
):
|
||||
"""Forward a zmq message to all websockets.
|
||||
|
||||
Args:
|
||||
@@ -274,7 +250,10 @@ class ZMQWebSocketBridge:
|
||||
"""
|
||||
_, _, data = frames # cmd, path, data
|
||||
for websocket in self.websocket_pool:
|
||||
websocket.write_message(data, binary=True)
|
||||
if websocket_to_skip and websocket == websocket_to_skip:
|
||||
pass
|
||||
else:
|
||||
websocket.write_message(data, binary=True)
|
||||
|
||||
def setup_zmq(self, url: str):
|
||||
"""Setup a zmq socket and connect it to the given url.
|
||||
@@ -294,13 +273,11 @@ class ZMQWebSocketBridge:
|
||||
Args:
|
||||
websocket: websocket to send information over
|
||||
"""
|
||||
for node in walk(self.tree):
|
||||
if node.object is not None:
|
||||
websocket.write_message(node.object, binary=True)
|
||||
for p in node.properties:
|
||||
websocket.write_message(p, binary=True)
|
||||
if node.transform is not None:
|
||||
websocket.write_message(node.transform, binary=True)
|
||||
print("Sending entire scene state on websocket connect (i.e,. a page refresh).")
|
||||
for path, node in walk("", self.state_tree):
|
||||
if node.data is not None:
|
||||
command = {"type": "write", "path": path, "data": node.data}
|
||||
websocket.write_message(umsgpack.packb(command), binary=True)
|
||||
|
||||
def run(self):
|
||||
"""starts and runs the websocket bridge"""
|
||||
|
||||
Reference in new issue
Block a user