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:
Ethan Weber authored and GitHub committed 2022-08-02 16:02:07 -05:00
1 parent 966374474d
commit 73fedc7593
67 files changed
+2510 -14057

No files matched your search

+32 -59
View File
@@ -12,28 +12,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Code to connect and send commands to the viewer.
"""
import logging
import signal
import sys
from typing import Dict, Optional
import msgpack
import msgpack_numpy
import numpy as np
import umsgpack
import zmq
from nerfactory.viewer.server.commands import (
Delete,
GetObject,
SetObject,
SetOutputOptions,
SetProperty,
SetTransform,
)
from nerfactory.viewer.server.path import Path
class ViewerWindow:
"""The viewer window has the ZMQ connection to the viewer bridge server."""
context = zmq.Context()
def __init__(self, zmq_url):
@@ -43,15 +40,15 @@ class ViewerWindow:
self.assert_connected()
def send(self, command):
cmd_data = command.lower()
"""Sends a command to the viewer bridge server."""
self.client.send_multipart(
[
cmd_data["type"].encode("utf-8"),
cmd_data["path"].encode("utf-8"),
umsgpack.packb(cmd_data),
command["type"].encode("utf-8"),
command["path"].encode("utf-8"),
umsgpack.packb(command),
]
)
return self.client.recv()
return umsgpack.unpackb(self.client.recv())
def send_ping(self):
"""Tries to contact the viewer bridge server."""
@@ -65,7 +62,7 @@ class ViewerWindow:
data,
]
)
return self.client.recv()
return umsgpack.unpackb(self.client.recv())
def assert_connected(self, timeout_in_sec: int = 5):
"""Check if the connection was established properly within some time.
@@ -84,7 +81,7 @@ class ViewerWindow:
_ = self.send_ping()
logging.info("Successfully connected.")
signal.alarm(0) # cancel the alarm
except Exception as e:
except Exception as e: # pylint: disable=broad-except
logging.info(e)
sys.exit()
@@ -93,19 +90,18 @@ class Viewer:
"""Visualizer class for connecting to the bridge server.
Args:
zmq_url (str, optional): _description_. Defaults to None.
window (ViewerWindow, optional): _description_. Defaults to None.
timeout (str, optional): _description_. Defaults to None.
zmq_url: Where to connect with ZMQ. Defaults to None.
window: An already existing ViewerWindow. Defaults to None.
"""
def __init__(self, zmq_url: str = None, window: ViewerWindow = None):
def __init__(self, zmq_url: Optional[str] = None, window: Optional[ViewerWindow] = None):
if zmq_url is None and window is None:
raise ValueError("Must specify either zmq_url or window.")
if window is None:
self.window = ViewerWindow(zmq_url=zmq_url)
else:
self.window = window
self.path = Path(("nerfactory",))
self.path = Path(())
@staticmethod
def view_into(window: ViewerWindow, path: Path):
@@ -117,28 +113,25 @@ class Viewer:
def __getitem__(self, path):
return Viewer.view_into(self.window, self.path.append(path))
def set_object(self, geometry, material=None):
"""Set the object at the current path
Args:
geometry (_type_): _description_
material (_type_, optional): _description_. Defaults to None.
def __repr__(self):
return f"<Viewer using: {self.window} at path: {self.path}>"
Returns:
_type_: _description_
"""
return self.window.send(SetObject(geometry, material, self.path))
def write(self, data: Optional[Dict] = None):
"""Write data."""
path = self.path.lower()
return self.window.send({"type": "write", "path": path, "data": data})
def get_object(self):
"""Get the object at the current path."""
data = self.window.send(GetObject(self.path))
data = umsgpack.unpackb(data)
if isinstance(data, str) and data.find("error") == 0:
# some error meaning that the object does not exist
return None
return data
def read(self):
"""Read data."""
path = self.path.lower()
return self.window.send({"type": "read", "path": path})
def delete(self):
"""Delete data."""
return self.write(data=None)
def set_image(self, image):
"""Set the image"""
"""Sends an image to the viewer with WebRTC."""
type_ = "set_image"
path = self.path.lower()
data = msgpack.packb(image, default=msgpack_numpy.encode, use_bin_type=True)
@@ -150,23 +143,3 @@ class Viewer:
]
)
return self.window.client.recv()
def set_transform(self, matrix=np.eye(4)):
"""Set the transform"""
assert matrix.shape == (4, 4)
return self.window.send(SetTransform(matrix, self.path))
def set_property(self, key, value):
"""Set the property"""
return self.window.send(SetProperty(key, value, self.path))
def set_output_options(self, options):
"""Set the output options"""
return self.window.send(SetOutputOptions(options, self.path))
def delete(self):
"""Delete the contents of the window"""
return self.window.send(Delete(self.path))
def __repr__(self):
return f"<Viewer using: {self.window} at path: {self.path}>"