This repository has been archived by the owner on Aug 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathblender_app.py
172 lines (142 loc) · 4.97 KB
/
blender_app.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import logging
import time
from typing import Iterable, List, Optional, Mapping
import sys
import tests.blender_lib as bl
from tests.process import BlenderServer
logging.basicConfig(stream=sys.stderr, level=logging.WARNING)
logger = logging.getLogger(__name__)
_set_log_level = """
from mixer.bl_preferences import set_log_level
set_log_level(None, {log_level})
"""
# wait a bit for the server to get started
_connect = """
import bpy
import time
timeout = 10
end_time = time.monotonic() + timeout
while True:
try:
bpy.ops.mixer.connect()
except RuntimeError as e:
print ("Connect failed, retry", repr(e))
if time.monotonic() > end_time:
raise
time.sleep(0.1)
else:
break
"""
_disconnect = """
import bpy
bpy.ops.mixer.disconnect()
"""
_create_room = """
from mixer.connection import join_room
join_room("{room_name}", {vrtist_protocol}, {shared_folders}, True)
"""
_keep_room_open = """
from mixer.share_data import share_data
share_data.client.set_room_keep_open("{room_name}", {keep_room_open})
"""
_join_room = """
import mixer.blender_data
from mixer.connection import join_room
from mixer.broadcaster.common import RoomAttributes
from mixer.share_data import share_data
from mixer.blender_client.client import clear_scene_content
import sys
import time
mixer.blender_data.register()
# prevent sending our contents in case of cross join. Easier to diagnose the problem
clear_scene_content()
start = time.monotonic()
max_wait = 30
def wait_joinable():
share_data.client.send_list_rooms()
joinable = False
while not joinable and time.monotonic() - start < max_wait:
time.sleep(0.1)
share_data.client.fetch_incoming_commands()
room_attributes = share_data.client.rooms_attributes.get("{room_name}")
if room_attributes is not None:
joinable = room_attributes.get(RoomAttributes.JOINABLE, False)
return room_attributes is not None and room_attributes.get(RoomAttributes.JOINABLE, False)
if wait_joinable():
join_room("{room_name}", {vrtist_protocol}, {shared_folders}, True)
else:
print(f"ERROR: Cannot join room after {max_wait} seconds. Abort")
time.sleep(5)
sys.exit(1)
"""
class BlenderApp:
def __init__(self, port: int, ptvsd_port: int = None, wait_for_debugger=False):
self._port = port
self._ptvsd_port = ptvsd_port
self._wait_for_debugger = wait_for_debugger
self._blender: BlenderServer = BlenderServer(self._port, self._ptvsd_port, self._wait_for_debugger)
self._log_level = logging.WARNING
def set_log_level(self, log_level: int):
self._log_level = log_level
def setup(self, blender_args: List = None, env: Optional[Mapping[str, str]] = None):
self._blender.start(blender_args, env)
self._blender.connect()
def connect_mixer(self):
"""Emit a mixer connect command"""
if self._log_level is not None:
set_log_level = _set_log_level.format(log_level=self._log_level)
self._blender.send_string(set_log_level)
self._blender.send_string(_connect)
def create_room(
self,
room_name="mixer_unittest",
keep_room_open=False,
vrtist_protocol: bool = False,
ignore_version_check=True,
shared_folders: List[str] = (),
):
"""Emit a mixer create room command"""
create_room = _create_room.format(
room_name=room_name,
vrtist_protocol=vrtist_protocol,
shared_folders=list(shared_folders),
ignore_version_check=ignore_version_check,
)
self._blender.send_string(create_room)
keep_room_open = _keep_room_open.format(room_name=room_name, keep_room_open=keep_room_open)
self._blender.send_string(keep_room_open)
def join_room(
self,
room_name="mixer_unittest",
vrtist_protocol: bool = False,
shared_folders: Iterable[str] = (),
ignore_version_check=True,
):
"""Emit a mixer join room command"""
join_room = _join_room.format(
room_name=room_name,
vrtist_protocol=vrtist_protocol,
shared_folders=list(shared_folders),
ignore_version_check=ignore_version_check,
max_wait="{max_wait}",
)
self._blender.send_string(join_room)
def disconnect_mixer(self):
"""Emit a mixer disconnect room command"""
self._blender.send_string(_disconnect)
def wait(self, timeout: float = None):
return self._blender.wait(timeout)
# time.sleep(60)
# self._blender.send_function(bl.quit)
def kill(self):
self._blender.kill()
def send_function(self, f, *args, **kwargs):
self._blender.send_function(f, *args, **kwargs)
time.sleep(1)
def send_string(self, s, sleep: float):
self._blender.send_string(s)
time.sleep(sleep)
def quit(self):
self._blender.send_function(bl.quit)
def close(self):
self._blender.close()