This repository has been archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BpyZOCP.py
371 lines (328 loc) · 14.9 KB
/
BpyZOCP.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import bpy
import sys
import time
import json
import socket
import zmq
from zocp import ZOCP
from mathutils import Vector
from bpy.app.handlers import persistent
alreadyDeletedObjects = set()
camSettings = {}
mistSettings = ()
def toggleDebug(s, ctx):
pass
# PROPERTIES
bpy.types.Scene.zdebug_prop = bpy.props.BoolProperty( name="Toggle Debug", description = "This is a boolean", default=False, update=toggleDebug )
bpy.types.Scene.zmute_prop = bpy.props.BoolProperty( name="Mute", description = "This is a boolean", default=False )
bpy.types.Scene.zname_prop = bpy.props.StringProperty(name="Node Name", default=socket.gethostname(),description = "This can be used to identify your node")
# Menu in UI region
#
class UIPanel(bpy.types.Panel):
bl_label = "ZOCP Control"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
def draw(self, context):
scn = bpy.context.scene
self.layout.operator("send.all", text='Send all object data')
self.layout.prop( scn, "zdebug_prop" )
self.layout.prop( scn, "zmute_prop" )
self.layout.prop( scn, "zname_prop" )
class OBJECT_OT_SendMesh(bpy.types.Operator):
bl_idname = "send.all"
bl_label = "Send Object Data"
def execute(self, context):
z.refresh_objects();
return{'FINISHED'}
class OBJECT_OT_HelloButton(bpy.types.Operator):
bl_idname = "send.zocpdebug"
bl_label = "Send Debug"
def execute(self, context):
print("Sending debug!")
z.shout("ZOCP", {"MOD": {"debug": True}})
return{'FINISHED'}
def sendCameraSettings(camera):
"""
send camera fov and lensshhift
"""
global camSettings
angle = camera.angle
lx = camera.shift_x
ly = camera.shift_y
print("sending new camera settings for %s" %camera.name)
z.shout("ZOCP", json.dumps({
"MOD":
{
camera.name+".angle": {"value": camera.angle},
camera.name+".shift_x": {"value": camera.shift_x},
camera.name+".shift_y": {"value": camera.shift_y},
}
}).encode('utf-8'))
z.capability[camera.name+".angle"]['value'] = angle
z.capability[camera.name+".shift_x"]['value'] = lx
z.capability[camera.name+".shift_y"]['value'] = ly
camSettings[camera.name] = (angle, lx, ly)
# @persistent
# def update_data(scene):
# for obj in scene.objects:
# if obj.type == 'MESH': # only send the object if it is a mesh or a Camera
# z.capability[obj.name+".x"]['value'] = obj.location.x
# z.capability[obj.name+".y"]['value'] = obj.location.y
# z.capability[obj.name+".z"]['value'] = obj.location.z
# sendObjectData(obj)
# elif obj.type == 'CAMERA':
# angle = obj.data.angle
# lx = obj.data.shift_x
# ly = obj.data.shift_y
# if not ( [angle, lx, ly] == camSettings.get(obj.data.name)):
# print("camera settings changed for %s", obj.name)
# sendCameraSettings(obj.data)
# def register():
# for obj in bpy.context.scene.objects:
# if obj.type == 'MESH': # only send the object if it is a mesh or a Camera
# z.register_float(obj.name+".x", obj.location.x, 'r')
# z.register_float(obj.name+".y", obj.location.y, 'r')
# z.register_float(obj.name+".z", obj.location.z, 'r')
# elif obj.type == 'CAMERA':
# z.register_float(obj.name+".angle", obj.data.angle, 'r')
# z.register_float(obj.name+".shift_x", obj.data.shift_x, 'r')
# z.register_float(obj.name+".shift_y", obj.data.shift_y, 'r')
class BpyZOCP(ZOCP):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.start()
@persistent
def clear_objects(self):
print("CLEAR OBJECTS")
if not self.capability.get('objects'):
return
self.set_object()
self.capability['objects'].clear()
self._on_modified(self.capability)
def register_objects(self):
print("REGISTER OBJECTS")
self._running = False
for obj in bpy.context.scene.objects:
print(obj.name)
if obj.type in ['MESH', 'CAMERA', 'LAMP']:
if obj.type == "LAMP":
self._register_lamp(obj)
elif obj.type == "CAMERA":
self._register_camera(obj)
else:
self._register_mesh(obj)
self._running = True
@persistent
def refresh_objects(self):
print("REFRESHING OBJECTS")
for obj in bpy.context.scene.objects:
print(obj.name)
if obj.type in ['MESH', 'CAMERA', 'LAMP']:
if obj.type == "LAMP":
self._register_lamp(obj)
elif obj.type == "CAMERA":
self._register_camera(obj)
else:
self._register_mesh(obj)
def _register_lamp(self, obj):
self.set_object(obj.name, "BPY_Lamp")
self.register_vec3f("location", obj.location[:], 're')
#self.register_mat3f("worldOrientation", obj.worldOrientation[:], 're')
self.register_vec3f("orientation", obj.rotation_euler[:], 're')
self.register_vec3f("scale", obj.scale[:], 're')
self.register_vec3f("color", obj.data.color[:], 're')
self.register_float("energy", obj.data.energy, 're')
self.register_float("distance", obj.data.distance, 're')
#self.register_int ("state", obj.state)
#self.register_float("mass", obj.mass)
def _register_camera(self, obj):
self.set_object(obj.name, "BPY_Camera")
self.register_vec3f("location", obj.location[:], 're')
#self.register_mat3f("worldOrientation", obj.worldOrientation[:], 're')
self.register_vec3f("orientation", obj.rotation_euler[:], 're')
self.register_float("angle", obj.data.angle, 're')
self.register_float("shift_x", obj.data.shift_x, 're')
self.register_float("shift_y", obj.data.shift_y, 're')
def _register_mesh(self, obj):
self.set_object(obj.name, "BPY_Mesh")
self.register_vec3f("location", obj.location[:], 're')
#self.register_mat3f("worldOrientation", obj.worldOrientation[:], 're')
self.register_vec3f("orientation", obj.rotation_euler[:], 're')
self.register_vec3f("scale", obj.scale[:], 're')
self.register_vec4f("color", obj.color[:], 're')
#self.register_int ("state", obj.state, 're')
#self.register_float("mass", obj.mass, 're')
def send_object_changes(self, obj):
self.set_object(obj.name, "BPY_Mesh")
if self._cur_obj.get("location", {}).get("value") != obj.location[:]:
#self.register_vec3f("location", obj.location[:])
self.emit_signal("location", obj.location[:])
if self._cur_obj.get("orientation", {}).get("value") != obj.rotation_euler[:]:
#self.register_vec3f("orientation", obj.rotation_euler[:])
self.emit_signal("orientation", obj.rotation_euler[:])
if self._cur_obj.get("scale", {}).get("value") != obj.scale[:]:
#self.register_vec3f("scale", obj.scale[:])
self.emit_signal("scale", obj.scale[:])
if obj.type == "LAMP":
if self._cur_obj.get("color", {}).get("value") != obj.data.color[:]:
#self.register_vec3f("color", obj.data.color[:])
self.emit_signal("color", obj.data.color[:])
if self._cur_obj.get("energy", {}).get("value") != obj.data.energy[:]:
self.register_float("energy", obj.data.energy[:])
if self._cur_obj.get("distance", {}).get("value") != obj.data.distance[:]:
self.register_float("color", obj.data.distance[:])
elif obj.type == "MESH":
if self._cur_obj.get("color", {}).get("value") != obj.color[:]:
self.register_vec4f("color", obj.color[:])
elif obj.type == "CAMERA":
self._register_camera(obj)
def emit_signal(self, name, data):
super().emit_signal(".".join(self._cur_obj_keys + (name, )), data)
#########################################
# Event methods. These can be overwritten
#########################################
def on_peer_enter(self, peer, name, *args, **kwargs):
print("ZOCP ENTER : %s" %(name))
# create an empty for peer
# name = self.peers[peer].get("_name", peer.hex)
# bpy.ops.object.empty_add(type="PLAIN_AXES")
# bpy.context.object.name = name
# # get projectors on peer
# objects = self.peers[peer].get("objects", {})
# for obj, data in objects.items():
# if data.get("type", "") == "projector":
# loc = data.get("location", (0,0,0))
# ori = data.get("orientation", (0,0,0))
# bpy.ops.object.camera_add(view_align=True,
# enter_editmode=False,
# location=loc,
# rotation=ori,
# layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False)
# )
# bpy.context.object.name = obj
def on_peer_exit(self, peer, name, *args, **kwargs):
print("ZOCP EXIT : %s" %(name))
# bpy.ops.object.select_all(action='DESELECT')
# objects = self.peers[peer].get("objects", {})
# for obj, data in objects.items():
# if data.get("type", "") == "projector":
# bpy.ops.object.select_pattern(pattern=obj)
# bpy.ops.object.delete()
# # delete empty
# name = self.peers[peer].get("_name", peer.hex)
# bpy.ops.object.select_pattern(pattern=name)
# bpy.ops.object.delete()
# bpy.ops.object.select_pattern(pattern=peer.hex)
# bpy.ops.object.delete()
# bpy.ops.object.select_all(action='DESELECT')
def on_peer_modified(self, peer, name, data, *args, **kwargs):
#print("ZOCP PEER MODIFIED: %s modified %s" %(name, data))
name = data.get("_name", "")
if name.startswith("BGE"):
# If we have camera for it send it the settings
# otherwise create new camera
try:
c=bpy.data.objects[name]
except KeyError as e:
print("WE FOUND AN UNKNOWN BGE NODE, creating a camera for it")
# create a new camera
loc = data.get("location", (0,0,0))
ori = data.get("orientation", (0,0,0))
bpy.ops.object.camera_add(view_align=True,
enter_editmode=False,
location=loc,
rotation=ori,
layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False)
)
bpy.context.object.name = name
else:
print("WE FOUND A BGE NODE, sending camera data")
self._register_camera(c)
def on_modified(self, peer, name, data, *args, **kwargs):
pass
# if data.get('objects'):
# for obj,val in data['objects'].items():
# if val.get("Type")
# bpy.ops.object.select_all(action='DESELECT')
# bpy.ops.object.select_pattern(pattern=obj)
# try:
# blenderobj = bpy.context.scene.objects[obj]
# except KeyError as e:
# print(e)
# else:
# for key,val2 in val.items():
# try:
# setattr(blenderobj, key, val)
# except AttributeError as e:
# print(e)
# except TypeError as e:
# print(e)
# except ValueError as e:
# print(e)
#except (KeyboardInterrupt, SystemExit):
# self.stop()
z = BpyZOCP("Blender@" + socket.gethostname() + ":" + bpy.app.version_string)
compobj = {}
for ob in bpy.data.objects:
print(ob.name)
compobj[ob.name] = ob.matrix_world.copy()
# Needed for delaying
toffset = 1/30.0
tstamp = time.time()
@persistent
def clear_objects(context):
global compobj
compobj.clear()
z.clear_objects()
@persistent
def register_objects(context):
global compobj
z.register_objects()
for ob in bpy.data.objects:
compobj[ob.name] = ob.matrix_world.copy()
@persistent
def scene_update(context):
global toffset
global tstamp
# only once per 'toffset' seconds to lessen the burden
if time.time() > tstamp + toffset:
tstamp = time.time()
z.run_once(timeout=0)
update_objects()
#else:
# print("delayed", tstamp, time.time())
@persistent
def frame_update(context):
global compobj
for ob in bpy.data.objects:
if not compobj.get(ob.name):
compobj[ob.name] = ob.matrix_world.copy()
z.send_object_changes(ob)
elif ob.is_updated and ob.matrix_world != compobj[ob.name]:
z.send_object_changes(ob)
compobj[ob.name] = ob.matrix_world.copy()
def update_objects():
global compobj
if bpy.data.objects.is_updated:
for ob in bpy.data.objects:
###### TODO: BETTER CODE FOR MANAGING CHANGED DATA
if not compobj.get(ob.name):
compobj[ob.name] = ob.matrix_world.copy()
z.send_object_changes(ob)
elif ob.is_updated and ob.matrix_world != compobj[ob.name]:
z.send_object_changes(ob)
compobj[ob.name] = ob.matrix_world.copy()
#register cameras
#register()
bpy.utils.register_module(__name__)
#bpy.app.handlers.scene_update_post.append(update_data)
#bpy.app.handlers.frame_change_pre.clear()
#bpy.app.handlers.frame_change_pre.append(update_data)
bpy.app.handlers.load_pre.clear()
bpy.app.handlers.load_post.clear()
bpy.app.handlers.load_pre.append(clear_objects)
bpy.app.handlers.load_post.append(register_objects)
bpy.app.handlers.scene_update_post.clear()
bpy.app.handlers.scene_update_post.append(scene_update)
bpy.app.handlers.frame_change_post.clear()
bpy.app.handlers.frame_change_post.append(frame_update)