forked from eth-ait/aitviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_two_way_communication.py
91 lines (71 loc) · 3.53 KB
/
remote_two_way_communication.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright (C) 2023 ETH Zurich, Manuel Kaufmann, Velko Vechev, Dario Mylonopoulos
import argparse
# Parse command line arguments.
parser = argparse.ArgumentParser()
parser.add_argument(
"--server", help="run the viewer part of the script, if not given run the client instead", action="store_true"
)
args = parser.parse_args()
# In this example we show how a Viewer can also send data to the clients other than just receiving data to visualize.
# This can be useful to implement custom behavior that is triggered interactively by the Viewer.
#
# Before reading this example you should be familiar with the 'remote.py' and 'remote_custom_viewer.py' examples which
# go over how to connect to a remote viewer and how to subclass the Viewer class to add custom functionality.
# Run the server or the viewer depending on the command line argument.
if not args.server:
#
# Client
#
import trimesh
from aitviewer.remote.renderables.meshes import RemoteMeshes
from aitviewer.remote.viewer import RemoteViewer
cube = trimesh.load("resources/cube.obj")
def message_handler(v: RemoteViewer, msg: object):
# Print the message that we received.
print(f"{msg}")
# Send a cube with name and position as specified in the message.
RemoteMeshes(
v, cube.vertices, cube.faces, name=msg["name"], position=msg["position"], flat_shading=True, scale=0.5
)
# Create a Viewer in a separate process running this script with the --server flag on.
with RemoteViewer.create_new_process([__file__, "--server"]) as v:
# Process messages until the connection is closed on the other side.
# This helper function calls the 'message_handler' function passing in the
# RemoteViewer object and the message to process.
#
# Alternatively, passing block=False only processes the messages received so far.
v.process_messages(message_handler, block=True)
else:
#
# Server
#
import imgui
from aitviewer.configuration import CONFIG as C
from aitviewer.viewer import Viewer
position = (0, 0, 0)
name = "Test"
class CustomViewer(Viewer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Add a new gui function for drawing some custom widgets.
self.gui_controls["custom"] = self.gui_custom
# Define a new gui function for drawing some custom widgets.
def gui_custom(self):
global position, name
# Crate a new window with a default position and size.
imgui.set_next_window_position(self.window_size[0] * 0.8, 50, imgui.FIRST_USE_EVER)
imgui.set_next_window_size(275, 110, imgui.FIRST_USE_EVER)
expanded, _ = imgui.begin("Custom window")
if expanded:
_, position = imgui.drag_float3("Position", *position, 1e-2, format="%.2f")
_, name = imgui.input_text("Name", name, 64)
if imgui.button("Make cube"):
# When the button is pressed send a message to all clients.
# The first argument to this function can be any python object that can be serialized with pickle.
# The 'get_message()' method on the client returns the object obtained by deserializing the pickle data.
self.send_message({"name": name, "position": position})
imgui.end()
# Create and run the viewer with the server enabled.
C.update_conf({"server_enabled": True})
v = CustomViewer()
v.run()