-
Notifications
You must be signed in to change notification settings - Fork 12
/
operators.py
363 lines (269 loc) · 11.9 KB
/
operators.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
from typing import Any
import bpy
import gpu
import time
import functools
from gpu_extras.presets import draw_texture_2d
import json
from dataclasses import dataclass
try:
from .fbs.FrameBufferSharingServer import FrameBufferSharingServer
from .fbs.FrameBufferSharingClient import FrameBufferSharingClient
from .fbs.FrameBufferDirectory import FrameBufferDirectory
except ModuleNotFoundError as ex:
print(f"Could not load FrameSharingModule: {ex}")
import uuid
fb_directories = dict()
needsRedraw = False
def add_streaming_type_ndi(dirs):
dir = FrameBufferDirectory.create("NDI_FrameBufferDirectory", "NDI")
dir.setup()
dirs["NDI"] = dir
def add_streaming_type_spout(dirs):
dir = FrameBufferDirectory.create("Spout_FrameBufferDirectory", "SPOUT")
dir.setup()
dirs["SPOUT"] = dir
#dictionary to store the references to
db_frameHandle = {} # the draw handler
db_drawHandle = {} # the draw handler
db_cameraInstances = {} # the server instance
db_writeTimerHandle = {} # the write handler
db_clientInstances = {} # the client instance
@dataclass
class FrameMetDataBuffer:
content: str = ""
def frame_metadata(name, frame_metadata_buffer):
def handler(scene, depsgraph):
camera = scene.objects.get(name)
if not camera:
return
container = {}
container['version'] = 1
container['frame'] = scene.frame_current
container['ts'] = 0
extrinsic = {}
extrinsic['pos'] = [camera.location[0], camera.location[1], camera.location[2]]
extrinsic['quat'] = [camera.rotation_quaternion[0], camera.rotation_quaternion[1], camera.rotation_quaternion[2], camera.rotation_quaternion[3]]
extrinsic['matrix'] = [camera.matrix_world[0][0], camera.matrix_world[0][1], camera.matrix_world[0][2], camera.matrix_world[0][3], camera.matrix_world[1][0], camera.matrix_world[1][1], camera.matrix_world[1][2], camera.matrix_world[1][3], camera.matrix_world[2][0], camera.matrix_world[2][1], camera.matrix_world[2][2], camera.matrix_world[2][3], camera.matrix_world[3][0], camera.matrix_world[3][1], camera.matrix_world[3][2], camera.matrix_world[3][3]]
container['extrinsic'] = extrinsic
fraMeDaPro = {}
fraMeDaPro['freMeDaPro'] = container
frame_metadata_buffer.content = json.dumps(fraMeDaPro)
return handler
"""
Obsolete function. can be removed if not needed
def write_frame(fb_client, guivars):
global needsRedraw
def write_frame_handler(scene, depsgraph):
if fb_client.has_new_frame() == True:
fb_client.apply_frame_to_image(guivars.texs_image)
needsRedraw = True
return write_frame_handler
"""
# function for the method the timer thread is calling when it is appropriate
def image_dirty_timer_call(fb_client, guivars):
if fb_client.has_new_frame() == True:
fb_client.apply_frame_to_image(guivars.texs_image)
guivars.texs_image.update_tag()
return guivars.refresh_rate / 1000 if guivars.enable == 1 else None
# function for the draw handler to capture the texture from the perspective of the camera
def texshare_capture(self, context, camera, object, space, region, scene, layer, offscreen, spyphonSender, showPreview, isFlipped, frame_metadata_buffer):
guivars = camera.TEXS_share
dWIDTH = guivars.capture_width
dHEIGHT = guivars.capture_height
applyCM = guivars.applyColorManagmentSettings
view_matrix = object.matrix_world.inverted()
projection_matrix = object.calc_matrix_camera(
context.evaluated_depsgraph_get(), x=dWIDTH, y=dHEIGHT)
#bpy.data.screens['3DView'].areas[0].regions[0]
offscreen.draw_view3d(
scene,
layer,
space,
context.region,
view_matrix,
projection_matrix,
do_color_management=applyCM,
draw_background=not guivars.backgroundTransparent)
if showPreview:
gpu.state.depth_test_set("NONE")
spyphonSender.draw_texture(offscreen, (10, 10), dWIDTH / 4, dHEIGHT / 4)
#if spyphonSender.can_memory_buffer() == True:
# spyphonSender.write_memory_buffer(camera.name, buffer, len(buffer))
spyphonSender.send_texture(offscreen, dWIDTH, dHEIGHT, isFlipped)
# main function called when the settings 'enable' property is changed
def texshare_send(self, context):
global db_drawHandle
global db_cameraInstances
guivars = context.camera.TEXS_share
# my database ID
dbID = guivars.dbID
# if streaming has been enabled and no id has yet been stored in the db
if guivars.enable == 1 and dbID not in db_drawHandle:
# first we create a unique identifier for the reference db dicts
dbID = str(uuid.uuid1())
updateDraw = False
dWIDTH = guivars.capture_width
dHEIGHT = guivars.capture_height
# create a new spout sender instance
fbSender = FrameBufferSharingServer.create(context.camera.name, guivars.streaming_type)
fbSender.setup()
#if spyphonSender.can_memory_buffer() == True:
## spyphonSender.create_memory_buffer(context.camera.name, 1024)
# create a off screen renderer
offscreen = gpu.types.GPUOffScreen(dWIDTH, dHEIGHT)
mySpace = context.space_data
myRegion = context.region
for area in bpy.data.workspaces[guivars.workspace].screens[0].areas:
if area.type == 'VIEW_3D':
myRegion = area.regions[0]
for spaces in area.spaces:
if spaces.type == 'VIEW_3D':
mySpace = spaces
myScene = bpy.data.scenes[guivars.scene]
myLayer = myScene.view_layers[guivars.layer]
myCurrentScene = bpy.context.window.scene
myCurrentLayer = bpy.context.window.view_layer
# quickly open the to be rendered scene and layer to avoid a crash of blender
bpy.context.window.scene = myScene
bpy.context.window.view_layer = myLayer
bpy.context.window.scene = myCurrentScene
bpy.context.window.view_layer = myCurrentLayer
frame_metadata_buffer = FrameMetDataBuffer("test")
# collect all the arguments to pass to the draw handler
args = (self, context, context.camera, context.object, mySpace, myRegion, myScene, myLayer, offscreen, fbSender, guivars.preview, guivars.isflipped, frame_metadata_buffer)
# frameHandler = frame_metadata(context.camera.name, frame_metadata_buffer)
# bpy.app.handlers.depsgraph_update_post.append(frameHandler)
# instantiate the draw handler,
# using the texshare_capture function defined above
drawhandle = bpy.types.SpaceView3D.draw_handler_add(texshare_capture, args, 'WINDOW', 'POST_PIXEL')
# store the references inside the db-dicts
#db_frameHandle[dbID] = frameHandler
db_drawHandle[dbID] = drawhandle
db_cameraInstances[dbID] = fbSender
# if streaming has been disabled and my ID is still stored in the db
if guivars.enable == 0 and dbID in db_drawHandle:
#bpy.app.handlers.depsgraph_update_post.remove(db_frameHandle[dbID])
bpy.types.SpaceView3D.draw_handler_remove(db_drawHandle[dbID], 'WINDOW')
db_cameraInstances[dbID].release()
#removing my ID
db_drawHandle.pop(dbID, None)
dbID == "off"
# store the database ID again inside the settings
guivars.dbID = dbID
# main function called when the share receive 'enable' property is changed
def texshare_receive(self, context):
global db_writeTimerHandle
global db_clientInstances
guivars = self
# my database ID
dbID = guivars.dbID
# if streaming has been enabled and no id has yet been stored in the db
if guivars.enable == 1 and dbID not in db_writeTimerHandle:
# first we create a unique identifier for the reference db dicts
dbID = str(uuid.uuid1())
server = guivars.texs_server
stream_type = guivars.streaming_type
# create a new spout sender instance
fb_client = FrameBufferSharingClient.create(server, stream_type)
fb_client.setup(fb_directories.get(stream_type).get_servers())
"""
Obsolete lines. can be removed in future
create a off screen renderer
offscreen = gpu.types.GPUOffScreen(dWIDTH, dHEIGHT)
collect all the arguments to pass to the write handler
write_handler = None
following code is not needed anymore and replace by a simple timer down below
write_handler = write_frame(fb_client, guivars)
bpy.app.handlers.depsgraph_update_pre.append(write_handler)
"""
# register the method to dirty the image
timer_call = functools.partial(fb_client.timer_call, guivars)
bpy.app.timers.register(timer_call)
# store the references inside the db-dicts
#db_frameHandle[dbID] = frameHandler
db_writeTimerHandle[dbID] = timer_call
db_clientInstances[dbID] = fb_client
# if streaming has been disabled and my ID is still stored in the db
if guivars.enable == 0 and dbID in db_writeTimerHandle:
bpy.app.timers.unregister(db_writeTimerHandle[dbID])
db_clientInstances[dbID].release()
#removing my ID
db_writeTimerHandle.pop(dbID, None)
db_clientInstances.pop(dbID, None)
dbID == "off"
# store the database ID again inside the settings
guivars.dbID = dbID
class TEXS_OT_ItemCreate(bpy.types.Operator):
"""Create new texture receiver"""
bl_idname = "textureshare.createitem"
bl_label = "Create"
type : bpy.props.StringProperty()
server: bpy.props.StringProperty()
@classmethod
def poll(cls, context):
return context.object is not None
def execute(self, context):
return {'FINISHED'}
def invoke(self, context, event):
keys = bpy.context.scene.TEXS_imgs
new_item = keys.add()
# we assume the new key is added at the end of the collection, so we get the index by:
index = len(bpy.context.scene.TEXS_imgs.keys()) -1
new_item.streaming_type = self.type
if self.type == 'SPOUT':
new_item.name = "SpoutReceiver"
if self.type == 'NDI':
new_item.name = "NDIReceiver"
new_item.texs_server = self.server
return {'RUNNING_MODAL'}
#######################################
# Delete TEXS Settings #
#######################################
class TEXS_OT_ItemDelete(bpy.types.Operator):
"""Delete this texture receiver"""
bl_idname = "textureshare.deleteitem"
bl_label = "Delete"
index : bpy.props.IntProperty(default=0)
@classmethod
def poll(cls, context):
return context.object is not None
def execute(self, context):
return {'FINISHED'}
def invoke(self, context, event):
bpy.context.scene.TEXS_imgs.remove(self.index)
return {'RUNNING_MODAL'}
#######################################
# UPDATE TEXS DIRECTORY #
#######################################
class TEXS_OT_DirUpdate(bpy.types.Operator):
"""update frame share directory"""
bl_idname = "textureshare.directoryupdate"
bl_label = "Update"
global fb_directories
type : bpy.props.StringProperty()
@classmethod
def poll(cls, context):
return context.object is not None
def execute(self, context):
return {'FINISHED'}
def invoke(self, context, event):
for key, value in fb_directories.items():
if self.type == key:
value.update()
return {'RUNNING_MODAL'}
op_classes = (
TEXS_OT_ItemCreate,
TEXS_OT_ItemDelete,
TEXS_OT_DirUpdate,
)
def register():
for cls in op_classes:
bpy.utils.register_class(cls)
for key, value in fb_directories.items():
value.register()
def unregister():
for cls in reversed(op_classes):
bpy.utils.unregister_class(cls)
for key, value in fb_directories.items():
value.unregister()