-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathui.py
2022 lines (1523 loc) · 75.1 KB
/
ui.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ##### BEGIN GPL LICENSE BLOCK #####
#
# Copyright © 2021 Christian Stolze
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ##### END GPL LICENSE BLOCK #####
# MODULE DESCRIPTION:
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# This includes everything that is related the user interface
# ------------------ INTERNAL MODULES --------------------
from .globals import *
# ------------------- EXTERNAL MODULES -------------------
import sys, os, json
import time
from math import *
import bpy
from bpy.props import FloatProperty, PointerProperty
from bpy.types import PropertyGroup
import numpy as np
# append the add-on's path to Blender's python PATH
sys.path.insert(0, LookingGlassAddon.path)
sys.path.insert(0, LookingGlassAddon.libpath)
# TODO: Would be better, if from .lib import pylightio could be called,
# but for some reason that does not import all modules and throws
# "AliceLG.lib.pylio has no attribute 'lookingglass"
import pylightio as pylio
# ---------------- GLOBAL ADDON LOGGER -------------------
import logging
LookingGlassAddonLogger = logging.getLogger('Alice/LG')
# ------------- Add-on UI -------------
# Class that contains all functions relevant for the UI
class LookingGlassAddonUI:
# This callback is required to be able to update the list of connected Looking Glass devices
def connected_device_list_callback(self, context):
# prepare a item list with entries of the form "identifier, name, description"
items = []
# if at least one Looking Glass is connected OR debug mode is activated
if pylio.DeviceManager.count() > 0 or LookingGlassAddon.debugging_use_dummy_device:
# then for each display in the device list
for idx, device in enumerate(sorted(pylio.DeviceManager.to_list(), key=lambda device_type: device_type.index)):
# if this device is a real one OR the debug mode is active
if (not device.emulated or LookingGlassAddon.debugging_use_dummy_device):
# add an entry in the item list
items.append((str(device.id), 'Display ' + str(device.index) + ': ' + device.name, 'Use this Looking Glass for light field preview rendering.'))
# FOR DEBUGGING ONLY
if LookingGlassAddon.debugging_use_dummy_device:
# then for each display in the device list
for idx, device in enumerate(pylio.DeviceManager.to_list(None, True)):
# if this device is a real one OR the debug mode is active
if (not device.emulated or LookingGlassAddon.debugging_use_dummy_device):
# add an entry in the item list
items.append((str(device.id), 'Display ' + str(device.index) + ': ' + device.name, 'Use this Looking Glass for light field preview rendering.'))
else:
# add an entry to notify the user about the missing Looking Glass
items.append(('-1', 'No Looking Glass Found', 'Please connect a Looking Glass.'))
# return the item list
return LookingGlassAddon.enum_string_cache_items(items)
# This callback is required to be able to update the list of emulated Looking Glass devices
def emulated_device_list_callback(self, context):
# prepare a item list with entries of the form "identifier, name, description"
items = []
# if at least one emulated Looking Glass exists
if pylio.DeviceManager.count(False, True) > 0:
# then for each display in the device list
for idx, device in enumerate(sorted(pylio.DeviceManager.to_list(False, True), key=lambda device_type: device_type.index, reverse=True)):
# if this device is a real one OR the debug mode is active
if device.emulated:
# add an entry in the item list
items.append((str(device.index), device.name, 'Use this Looking Glass type for light field rendering.'))
else:
# add an entry to notify the user about the missing Looking Glass
items.append(('-1', 'No Emulated Devices Found', 'Please connect a Looking Glass.'))
# return the item list
return LookingGlassAddon.enum_string_cache_items(items)
# This callback is required to be able to update the list of presets
def quilt_preset_list_callback(self, context):
# prepare a item list with entries of the form "identifier, name, description"
items = []
# then for each display in the device list
for idx, preset in pylio.LookingGlassQuilt.formats.get().items():
# if this preset is not marked as hidden
if not pylio.LookingGlassQuilt.formats.is_hidden(id=idx):
# add an entry in the item list
items.append((str(idx), preset['description'], 'Sets the resolution for light field preview.'))
# return the item list
return LookingGlassAddon.enum_string_cache_items(items)
# poll function for the Looking Glass camera selection
# this prevents that an object is picked or listed, which is no camera
def camera_selection_poll(self, object):
# the object has to be a Camera of the current view layer
return (object.type == 'CAMERA' and (object in [o for o in bpy.context.view_layer.objects]))
# update function for the workspace selection
def update_workspace_selection(self, context):
if context != None:
# if the settings shall be taken from the current viewport
if context.scene.addon_settings.viewportMode == 'BLENDER':
# status variable
success = False
# find the correct SpaceView3D object
for screen in bpy.data.workspaces[context.scene.addon_settings.blender_workspace].screens:
for area in screen.areas:
for space in area.spaces:
# if this is the correct space
if str(space) == str(context.scene.addon_settings.blender_view3d):
# save the space object in the global variable
LookingGlassAddon.BlenderViewport = space
success = True
break
# if the current space was not found in the chosen workspace
if success == False:
# find and use the first SpaceView3D object of the workspace
for screen in bpy.data.workspaces[context.scene.addon_settings.blender_workspace].screens:
for area in screen.areas:
for space in area.spaces:
if space.type == 'VIEW_3D':
# save the space object in the global variable
LookingGlassAddon.BlenderViewport = space
success = True
break
# if there is no 3D View in this workspace, use the active 3D View instead
if success == False:
# update the viewport selection
context.scene.addon_settings.blender_view3d = "None"
# fall back to the use of the custom settings
LookingGlassAddon.BlenderViewport = None
return None
# update function for the viewport selection
def update_viewport_selection(self, context):
if context != None:
# if the settings shall be taken from the current viewport
if context.scene.addon_settings.viewportMode == 'BLENDER':
# if a viewport is chosen
if str(context.scene.addon_settings.blender_view3d) != "None":
# find the correct SpaceView3D object
for screen in bpy.data.workspaces[context.scene.addon_settings.blender_workspace].screens:
for area in screen.areas:
for space in area.spaces:
# if this is the correct space
if str(space) == str(context.scene.addon_settings.blender_view3d):
# save the space object in the global variable
LookingGlassAddon.BlenderViewport = space
break
else:
# fall back to the use of the custom settings
LookingGlassAddon.BlenderViewport = None
return None
# update function for property updates concerning render settings
def update_render_setting_with_preset(self, context):
# perform all updates
LookingGlassAddonUI.update_render_setting_without_preset(self, context)
# if a device is selected by the user
if int(context.window_manager.addon_settings.activeDisplay) != -1: pylio.DeviceManager.set_active(int(context.window_manager.addon_settings.activeDisplay))
else: pylio.DeviceManager.reset_active()
# set device variable
device = None
# if a camera is selected
if context.scene.addon_settings.lookingglassCamera != None:
# GET DEVICE INFORMATION
# +++++++++++++++++++++++++++++++++++++++++++++++++++++
# if the settings are to be taken from device selection AND a device is active
if context.scene.addon_settings.render_use_device == True and pylio.DeviceManager.get_active() is not None:
# currently selected device
device = pylio.DeviceManager.get_active()
else:
# make the emulated device the active device, if one was found
device = pylio.DeviceManager.get_device(key='index', value=int(context.scene.addon_settings.render_device_type))
# UPDATE DROPDOWNS
# if the loaded file does not have a lockfile
if not LookingGlassAddon.has_lockfile:
# try to find the suitable default quilt preset
if device:
preset = pylio.LookingGlassQuilt.formats.find(device.default_quilt_width, device.default_quilt_height, device.default_quilt_rows, device.default_quilt_columns)
# then update the selected quilt preset from the device's default quilt
if device and preset:
if bpy.context.scene.addon_settings.render_quilt_preset != str(preset):
bpy.context.scene.addon_settings.render_quilt_preset = str(preset)
elif not device or not preset:
# fallback solution, if the default quilt is not found:
# We use the Looking Glass Go standard quilt (48 views)
if bpy.context.scene.addon_settings.render_quilt_preset != "5":
bpy.context.scene.addon_settings.render_quilt_preset = "5"
return None
# update function for property updates concerning render settings (WITHOUT quilt preset)
def update_render_setting_without_preset(self, context):
# if a device is selected by the user
if int(context.window_manager.addon_settings.activeDisplay) != -1: pylio.DeviceManager.set_active(int(context.window_manager.addon_settings.activeDisplay))
else: pylio.DeviceManager.reset_active()
# set device variable
device = None
# if a camera is selected
if context.scene.addon_settings.lookingglassCamera != None:
# GET DEVICE INFORMATION
# +++++++++++++++++++++++++++++++++++++++++++++++++++++
# if the settings are to be taken from device selection AND a device is active
if context.scene.addon_settings.render_use_device == True and pylio.DeviceManager.get_active() is not None:
# currently selected device
device = pylio.DeviceManager.get_active()
else:
# make the emulated device the active device, if one was found
device = pylio.DeviceManager.get_device(key='index', value=int(context.scene.addon_settings.render_device_type))
# APPLY RENDER SETTINGS
# +++++++++++++++++++++++++++++++++++++++++++++++++++++
# apply render settings for the scene to get the correct rendering frustum
context.scene.render.resolution_x = pylio.LookingGlassQuilt.formats.get()[int(context.scene.addon_settings.render_quilt_preset)]["view_width"]
context.scene.render.resolution_y = pylio.LookingGlassQuilt.formats.get()[int(context.scene.addon_settings.render_quilt_preset)]["view_height"]
# for landscape formatted devices
if (context.scene.render.resolution_x / context.scene.render.resolution_y) / device.aspect > 1:
# apply the correct aspect ratio
context.scene.render.pixel_aspect_x = 1.0
context.scene.render.pixel_aspect_y = context.scene.render.resolution_x / (context.scene.render.resolution_y * device.aspect)
# for portrait formatted devices
else:
# apply the correct aspect ratio
context.scene.render.pixel_aspect_x = (context.scene.render.resolution_y * device.aspect) / context.scene.render.resolution_x
context.scene.render.pixel_aspect_y = 1.0
# BLOCK RENDERER
if LookingGlassAddon.ViewportBlockRenderer:
block = LookingGlassAddon.ViewportBlockRenderer.get_viewport_block()
else:
block = None
if block and device and context.region:
# update state variables
block.set_active(False)
if context.scene.addon_settings.render_use_device == True: block.set_preset(int(context.scene.addon_settings.quiltPreset))
if context.scene.addon_settings.render_use_device == False: block.set_preset(int(context.scene.addon_settings.render_quilt_preset))
block.set_aspect(device.aspect)
block.set_view_cone(device.viewCone)
# redraw region
bpy.ops.wm.update_block_renderer('INVOKE_DEFAULT')
return None
# update settings for the viewport editor blocks
def update_viewport_block_settings(self, context):
# set device variable
device = None
# if a camera is selected
if context.scene.addon_settings.lookingglassCamera != None:
# if the settings are to be taken from device selection AND a device is active
if context.scene.addon_settings.render_use_device == True and pylio.DeviceManager.get_active() is not None:
# currently selected device
device = pylio.DeviceManager.get_active()
else:
# make the emulated device the active device, if one was found
device = pylio.DeviceManager.get_device(key='index', value=int(context.scene.addon_settings.render_device_type))
# BLOCK RENDERER
if device and LookingGlassAddon.ViewportBlockRenderer:
# if the block renderer should be started
if context.scene.addon_settings.viewport_block_show:
LookingGlassAddon.ViewportBlockRenderer.start(bpy.context)
# if the block renderer should be stopped
if not context.scene.addon_settings.viewport_block_show:
LookingGlassAddon.ViewportBlockRenderer.stop()
# if the block renderer is running
if LookingGlassAddon.ViewportBlockRenderer.is_running():
# get the viewport block
block = LookingGlassAddon.ViewportBlockRenderer.get_viewport_block()
if block:
# update state variables
block.set_active(False)
if context.scene.addon_settings.render_use_device == True: block.set_preset(int(context.scene.addon_settings.quiltPreset))
if context.scene.addon_settings.render_use_device == False: block.set_preset(int(context.scene.addon_settings.render_quilt_preset))
block.set_aspect(device.aspect)
block.set_view_cone(device.viewCone)
# redraw region
bpy.ops.wm.update_block_renderer('INVOKE_DEFAULT')
return None
# update settings for the image editor blocks
def update_imageeditor_block_settings(self, context):
# reset variables
device = None
block = None
# make the emulated device the active device, if one was found
device = pylio.DeviceManager.get_device(key='index', value=int(context.scene.addon_settings.imageeditor_block_device_type))
# BLOCK RENDERER
if device and LookingGlassAddon.ImageBlockRenderer:
# if the block renderer should be started
if context.scene.addon_settings.imageeditor_block_show:
LookingGlassAddon.ImageBlockRenderer.start(bpy.context)
# if the block renderer should be stopped
if not context.scene.addon_settings.imageeditor_block_show:
LookingGlassAddon.ImageBlockRenderer.stop()
# if the block renderer is running
if LookingGlassAddon.ImageBlockRenderer.is_running():
# get the viewport block
block = LookingGlassAddon.ImageBlockRenderer.get_imageeditor_block()
if block:
# update state variables
block.set_active(False)
block.set_preset(int(context.scene.addon_settings.imageeditor_block_quilt_preset))
block.set_aspect(device.aspect)
block.set_view_cone(device.viewCone)
# redraw region
bpy.ops.wm.update_block_renderer('INVOKE_DEFAULT')
return None
# Application handler that continously checks for changes of the depsgraph
def synchronize_active_camera(scene, depsgraph):
# if the active camera changed AND the active camera is not the "_quilt_render_cam"
if scene.addon_settings.lookingglassCamera != scene.camera and scene.camera.name != "_quilt_render_cam":
scene.addon_settings.lookingglassCamera = scene.camera
# update function for property updates concerning camera selection
def update_camera_synchronization(self, context):
# if the synchronization is active, set the lookingglass camera to the active camera
if context.scene.addon_settings.toggleCameraSync:
if not LookingGlassAddonUI.synchronize_active_camera in bpy.app.handlers.depsgraph_update_post: bpy.app.handlers.depsgraph_update_post.append(LookingGlassAddonUI.synchronize_active_camera)
if not LookingGlassAddonUI.synchronize_active_camera in bpy.app.handlers.frame_change_post: bpy.app.handlers.frame_change_post.append(LookingGlassAddonUI.synchronize_active_camera)
elif not context.scene.addon_settings.toggleCameraSync:
if LookingGlassAddonUI.synchronize_active_camera in bpy.app.handlers.depsgraph_update_post: bpy.app.handlers.depsgraph_update_post.remove(LookingGlassAddonUI.synchronize_active_camera)
if LookingGlassAddonUI.synchronize_active_camera in bpy.app.handlers.frame_change_post: bpy.app.handlers.frame_change_post.remove(LookingGlassAddonUI.synchronize_active_camera)
# update function for property updates concerning camera selection
def update_camera_selection(self, context):
# if no Looking Glass was detected AND debug mode is not activated
if not pylio.DeviceManager.count() and not LookingGlassAddon.debugging_use_dummy_device:
# set the checkbox to False (because there is no device we
# could take the settings from)
context.scene.addon_settings.render_use_device = False
# if a camera was selected
if context.scene.addon_settings.lookingglassCamera != None:
# if this camera becomes a Looking Glass camera for the first time
if not context.scene.addon_settings.lookingglassCamera.data.is_lightfield:
# adjust the clipping values to default values if necessary
if bpy.context.scene.addon_settings.clip_start <= 0.11:
bpy.context.scene.addon_settings.clip_start = 4.2
if bpy.context.scene.addon_settings.clip_end >= 99:
bpy.context.scene.addon_settings.clip_end = 6.5
if not (bpy.context.scene.addon_settings.clip_start < bpy.context.scene.addon_settings.focalPlane < bpy.context.scene.addon_settings.clip_end):
bpy.context.scene.addon_settings.focalPlane = 5
# set flag
context.scene.addon_settings.lookingglassCamera.data.is_lightfield = True
# update render settings
LookingGlassAddonUI.update_render_setting_without_preset(self, context)
return None
# TODO: This function was previously used to keep the focal plane in the clipping range.
# It's left here in case it may be of further use later.
# update function for property updates concerning camera clipping in the livew view
def update_camera_setting(self, context):
# if a camera was selected
if context.scene.addon_settings.lookingglassCamera != None:
# apply the settings to the selected camera object
camera = context.scene.addon_settings.lookingglassCamera
return None
# getter for the (camera) clipping start
def clip_start_getter(self):
# display the clipping settings
camera = bpy.context.scene.addon_settings.lookingglassCamera
if camera:
return camera.data.clip_start
# setter for the (camera) clipping start
def clip_start_setter(self, value):
# display the clipping settings
camera = bpy.context.scene.addon_settings.lookingglassCamera
if camera:
# update the clipping value
camera.data.clip_start = value
# make sure the focal plane stays within the clipping range
if bpy.context.scene.addon_settings.focalPlane < camera.data.clip_start:
bpy.context.scene.addon_settings.focalPlane = value
# getter for the (camera) clipping end
def clip_end_getter(self):
# display the clipping settings
camera = bpy.context.scene.addon_settings.lookingglassCamera
if camera:
return camera.data.clip_end
# setter for the (camera) clipping end
def clip_end_setter(self, value):
# display the clipping settings
camera = bpy.context.scene.addon_settings.lookingglassCamera
if camera:
# update the clipping value
camera.data.clip_end = value
# make sure the focal plane stays within the clipping range
if bpy.context.scene.addon_settings.focalPlane > camera.data.clip_end:
bpy.context.scene.addon_settings.focalPlane = value
# TODO: This function was previously used to keep the focal plane in the clipping range.
# It's left here in case it may be of further use later.
# update function for property updates concerning camera clipping in the livew view
def update_focal_sync(self, context):
# if a camera was selected
if context.scene.addon_settings.lookingglassCamera != None:
# apply the settings to the selected camera object
camera = context.scene.addon_settings.lookingglassCamera
if context.scene.addon_settings.toggleFocalSync:
camera.data.dof.focus_distance = self['focalPlane']
return None
# getter for the focalPlane
def focal_plane_getter(self):
# if the focal plane is sync'ed with the camera focus distance
if bpy.context.scene.addon_settings.toggleFocalSync:
# use the focus distance of the camera
camera = bpy.context.scene.addon_settings.lookingglassCamera
if camera:
if self.get('focalPlane', 5) != camera.data.dof.focus_distance:
bpy.context.scene.addon_settings.focalPlane = camera.data.dof.focus_distance
return self.get('focalPlane', 5)
# setter for the focalPlane
def focal_plane_setter(self, value):
# get the current camera
camera = bpy.context.scene.addon_settings.lookingglassCamera
if camera:
# if the clipping planes are pinned to the focal plane
if bpy.context.scene.addon_settings.lockClippingPlanes:
# make sure the new value is within the clipping range
camera.data.clip_start += (value - self['focalPlane'])
camera.data.clip_end += (value - self['focalPlane'])
else:
# make sure the new value is within the clipping range
if value <= camera.data.clip_start:
value = camera.data.clip_start
elif value >= camera.data.clip_end:
value = camera.data.clip_end
# if the focal plane is sync'ed with the camera focus distance
if bpy.context.scene.addon_settings.toggleFocalSync:
camera.data.dof.focus_distance = value
# set the focal plane value
self['focalPlane'] = value
# This callback populates the viewport selection with the WORKSPACES
def workspaces_list_callback(self, context):
# prepare a item list with entries of the form "identifier, name, description"
items = []
# check if the space still exists
for workspace in bpy.data.workspaces.keys():
# add an entry to notify the user about the missing Looking Glass
items.append((workspace, workspace, 'The workspace the desired viewport is found.'))
# return the item list
return LookingGlassAddon.enum_string_cache_items(items)
# This callback populates the viewport selection with the 3DVIEWs
def view3D_list_callback(self, context):
# prepare a item list with entries of the form "identifier, name, description" for the EnumProperty
items = []
# check if the space still exists
if context.scene.addon_settings.blender_workspace in bpy.data.workspaces:
# find all 3D Views in the selected Workspace
for screen in bpy.data.workspaces[context.scene.addon_settings.blender_workspace].screens:
for area in screen.areas:
for space in area.spaces:
if space.type == 'VIEW_3D':
# add an item to the item list
items.append((str(space), 'Viewport ' + str(len(items) + 1), 'The Blender viewport to which the Looking Glass adjusts.'))
# if no spaces were found
if len(items) == 0:
# add a dummy entry to the item list
items.append(('None', 'None', 'The Blender viewport to which the Looking Glass adjusts.'))
# return the item list
return LookingGlassAddon.enum_string_cache_items(items)
# update function for property updates concerning lightfield window settings
def update_lightfield_window_settings(self, context):
# if the lightfield viewport is in quilt viewer mode
if context.window_manager.addon_settings.renderMode == '1':
# update the lightfield displayed on the device
LookingGlassAddon.update_lightfield_window(int(context.window_manager.addon_settings.renderMode), LookingGlassAddon.quiltViewerLightfieldImage)
# update function for property updates concerning quilt image selection
def update_quilt_selection(self, context):
# if a quilt was selected
if context.window_manager.addon_settings.quiltImage != None:
# update the setting observers
LookingGlassAddon.quiltViewAsRender = context.window_manager.addon_settings.quiltImage.use_view_as_render
LookingGlassAddon.quiltImageColorSpaceSetting = context.window_manager.addon_settings.quiltImage.colorspace_settings
# if no pixel array exists
if LookingGlassAddon.quiltPixels is None:
# create a numpy array for the pixel data
LookingGlassAddon.quiltPixels = np.empty(len(context.window_manager.addon_settings.quiltImage.pixels), dtype=np.float32)
else:
# resize the numpy array
LookingGlassAddon.quiltPixels.resize(len(context.window_manager.addon_settings.quiltImage.pixels), refcheck=False)
# GET PIXEL DATA
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# TODO: Change. The current approach is hacky and slow, but I don't
# know of any other way to access the pixel data WITH applied
# color management directly in memory. Seems like Blender
# does not expose this pixel data to the Python API
#
# I asked this also on stackexchange but got no better way yet:
# https://blender.stackexchange.com/questions/206910/access-image-pixel-data-with-color-management-settings
#
# if the image has the "view as render" option inactive
if context.window_manager.addon_settings.quiltImage.use_view_as_render == False:
# save the original settings
tempViewTransform = context.scene.view_settings.view_transform
tempLook = context.scene.view_settings.look
tempExposure = context.scene.view_settings.exposure
tempGamma = context.scene.view_settings.gamma
tempUseCurveMapping = context.scene.view_settings.use_curve_mapping
# apply standard settings
context.scene.view_settings.view_transform = "Standard"
context.scene.view_settings.look = "None"
context.scene.view_settings.exposure = 0
context.scene.view_settings.gamma = 1
context.scene.view_settings.use_curve_mapping = False
# set the temporary file path
tempFilepath = bpy.app.tempdir + 'temp' + str(int(time.time())) + '.png'
# set the output settings
tempUseRenderCache = context.scene.render.use_render_cache
tempFileFormat = context.scene.render.image_settings.file_format
tempColorDepth = context.scene.render.image_settings.color_depth
tempColorMode = context.scene.render.image_settings.color_mode
context.scene.render.use_render_cache = False
context.scene.render.image_settings.file_format = 'PNG'
context.scene.render.image_settings.color_depth = '8'
context.scene.render.image_settings.color_mode = 'RGBA'
# save the image to the temporary directory
context.window_manager.addon_settings.quiltImage.save_render(filepath=tempFilepath, scene=context.scene)
# restore output render settings
context.scene.render.use_render_cache = tempUseRenderCache
context.scene.render.image_settings.file_format = tempFileFormat
context.scene.render.image_settings.color_depth = tempColorDepth
context.scene.render.image_settings.color_mode = tempColorMode
# if the image has the "view as render" option inactive
if context.window_manager.addon_settings.quiltImage.use_view_as_render == False:
# restore the original settings
context.scene.view_settings.view_transform = tempViewTransform
context.scene.view_settings.look = tempLook
context.scene.view_settings.exposure = tempExposure
context.scene.view_settings.gamma = tempGamma
context.scene.view_settings.use_curve_mapping = tempUseCurveMapping
# if the file was created
if os.path.isfile(tempFilepath) == True:
# append the loaded image to the list
tempImage = bpy.data.images.load(filepath=tempFilepath)
# copy pixel data to the array and a BGL Buffer
tempImage.pixels.foreach_get(LookingGlassAddon.quiltPixels)
# TODO: The following lines would be enough, if the color
# management settings would be applied in memory. Not deleted
# for later
#
# # copy pixel data to the array and a BGL Buffer
# context.window_manager.addon_settings.quiltImage.pixels.foreach_get(LookingGlassAddon.quiltPixels)
# LookingGlassAddon.quiltTextureBuffer = bgl.Buffer(bgl.GL_FLOAT, len(context.window_manager.addon_settings.quiltImage.pixels), LookingGlassAddon.quiltPixels)
# delete the temporary Blender image
bpy.data.images.remove(tempImage)
# delete the temporary file
os.remove(tempFilepath)
# CREATE A NEW PYLIGHTIO LIGHTFIELD IMAGE FROM THE BLENDER QUILT
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# convert to uint8
quiltPixels = 255 * LookingGlassAddon.quiltPixels
quiltPixels = quiltPixels.astype(dtype=np.uint8)
# if no quilt viewer LightfieldImage is selected
if context.window_manager.addon_settings.quiltImage is None:
# free the view data of the lightfield image, if one was loaded
if LookingGlassAddon.quiltViewerLightfieldImage:
LookingGlassAddon.quiltViewerLightfieldImage.clear_views()
LookingGlassAddon.quiltViewerLightfieldImage = None
# create a LightfieldImage from the selected quilt
LookingGlassAddon.quiltViewerLightfieldImage = pylio.LightfieldImage.from_buffer(pylio.LookingGlassQuilt, quiltPixels, context.window_manager.addon_settings.quiltImage.size[0], context.window_manager.addon_settings.quiltImage.size[1], context.window_manager.addon_settings.quiltImage.channels, quilt_name = context.window_manager.addon_settings.quiltImage.name)
# update the lightfield displayed on the device
LookingGlassAddon.update_lightfield_window(int(context.window_manager.addon_settings.renderMode), LookingGlassAddon.quiltViewerLightfieldImage)
# if the quilt selection was deleted
else:
# free the view data of the lightfield image, if one was loaded
if LookingGlassAddon.quiltViewerLightfieldImage:
LookingGlassAddon.quiltViewerLightfieldImage.clear_views()
LookingGlassAddon.quiltViewerLightfieldImage = None
# update the lightfield displayed on the device
LookingGlassAddon.update_lightfield_window(int(context.window_manager.addon_settings.renderMode), LookingGlassAddon.quiltViewerLightfieldImage)
# reset the variables
LookingGlassAddon.quiltPixels = None
# Properties in the preferences pane for this addon - Global values
class LookingGlassAddonSettingsWM(bpy.types.PropertyGroup):
# PANEL: GENERAL
# a list of connected Looking Glass displays
activeDisplay: bpy.props.EnumProperty(
items = LookingGlassAddonUI.connected_device_list_callback,
name="Please select a Looking Glass",
update=LookingGlassAddonUI.update_render_setting_without_preset,
)
# a boolean to toogle the render window on or off
ShowLightfieldWindow: bpy.props.BoolProperty(
name="Lightfield Window",
description = "Creates a window for the light field rendering on the current Looking Glass device",
default = False,
)
# PANEL: LIGHTFIELD WINDOW SETTINGS
# UI elements for user control
renderMode: bpy.props.EnumProperty(
items = [('0', 'Viewport', 'Viewport rendering of the current scene within the Looking Glass', 'VIEW3D', 0),
('1', 'Quilt Viewer', 'Display a pre-rendered quilt image in the Looking Glass', 'RENDER_RESULT', 1)],
default='0',
name="Render Mode",
update=LookingGlassAddonUI.update_lightfield_window_settings,
)
# Lightfield Viewport Modes
lightfieldMode: bpy.props.EnumProperty(
items = [('0', 'Refresh Mode: Automatic', 'Automatically refresh the light field viewport'),
('1', 'Refresh Mode: Manual', 'Manually refresh the light field viewport')],
default='1',
name="Light Field Viewport Modes",
update=LookingGlassAddonUI.update_lightfield_window_settings,
)
# Lightfield Preview Resolution in Auto lightfield mode
lightfield_preview_mode: bpy.props.EnumProperty(
items = [('0', 'No Preview', 'Light field window updates are performed after (not during) user interactions.'),
('1', 'Low-resolution Preview', '1024x1024 quilt, 32 views'),
('2', 'Skipped-views Preview I', 'Skip every second view'),
('3', 'Skipped-views Preview II', 'Skip every third view'),
('4', 'Restricted Viewcone Preview', 'Render only a restricted view cone'),
],
default='0',
name="Light Field Preview Mode",
update=LookingGlassAddonUI.update_lightfield_window_settings,
)
# pointer property that can be used to load a pre-rendered quilt image
quiltImage: bpy.props.PointerProperty(
name="Quilt",
type=bpy.types.Image,
description = "Quilt image for display in the Looking Glass",
update = LookingGlassAddonUI.update_quilt_selection,
)
viewport_use_preview_mode: bpy.props.BoolProperty(
name="Use Preview Mode",
description="If enabled, a simplified light field is rendered during scene changes (for higher render speed)",
default = True,
)
viewport_manual_refresh: bpy.props.BoolProperty(
name="Refresh Looking Glass",
description="Redraw the light field in the Looking Glass",
default = False,
)
# Properties in the preferences pane for this addon - Scene specific values
class LookingGlassAddonSettingsScene(bpy.types.PropertyGroup):
# PANEL: GENERAL
# a list of quilt presets
quiltPreset: bpy.props.EnumProperty(
items = LookingGlassAddonUI.quilt_preset_list_callback,
name="View Resolution",
update=LookingGlassAddonUI.update_render_setting_without_preset,
)
# PANEL: CAMERA SETTINGS
# pointer property that can be used to load a pre-rendered quilt image
lookingglassCamera: bpy.props.PointerProperty(
name="Looking Glass Camera",
type=bpy.types.Object,
description = "Select a camera, which defines the view for your Looking Glass or quilt image",
poll = LookingGlassAddonUI.camera_selection_poll,
update = LookingGlassAddonUI.update_camera_selection,
# options = {'ANIMATABLE'}
)
showFocalPlane: bpy.props.BoolProperty(
name="Show Focal Plane",
description="If enabled, the focal plane of the Looking Glass is shown in the viewport",
default = True,
)
showFrustum: bpy.props.BoolProperty(
name="Show Camera Frustum",
description="If enabled, the render volume of the Looking Glass is shown in the viewport",
default = True,
)
toggleCameraSync: bpy.props.BoolProperty(
name="Synchronize with Active Camera",
description="If enabled, the active camera will always be used as the Looking Glass camera (e.g., useful with camera markers)",
default = False,
update = LookingGlassAddonUI.update_camera_synchronization,
)
clip_start: bpy.props.FloatProperty(
name = "Clip Start",
default = 4.2,
min = 0.000001,
soft_min = 0.1,
precision = 1,
step = 5,
unit = "LENGTH",
description = "Far clipping plane of the Looking Glass frustum",
update = LookingGlassAddonUI.update_camera_setting,
get = LookingGlassAddonUI.clip_start_getter,
set = LookingGlassAddonUI.clip_start_setter,
)
clip_end: bpy.props.FloatProperty(
name = "Clip End",
default = 6.5,
min = 0,
precision = 1,
step = 5,
unit = "LENGTH",
description = "Far clipping plane of the Looking Glass frustum",
update = LookingGlassAddonUI.update_camera_setting,
get = LookingGlassAddonUI.clip_end_getter,
set = LookingGlassAddonUI.clip_end_setter,
)
# the virtual distance of the plane, which represents the focal plane of the Looking Glass
focalPlane: bpy.props.FloatProperty(
name = "Focal Plane",
default = 5,
min = 0,
precision = 2,
step = 5,
unit = "LENGTH",
description = "Virtual distance to the focal plane. (This plane is directly mapped to the LCD display of the Looking Glass)",
update = LookingGlassAddonUI.update_camera_setting,
get = LookingGlassAddonUI.focal_plane_getter,
set = LookingGlassAddonUI.focal_plane_setter,
)
toggleFocalSync: bpy.props.BoolProperty(
name="Synchronize with Camera Focus Distance",
description="If enabled, the focus distance (depth of field) and focal plane values of the Looking Glass Camera will be kept in synchronization",
default = True,
update = LookingGlassAddonUI.update_focal_sync,
)
lockClippingPlanes: bpy.props.BoolProperty(
name="Lock Clipping Planes to Focal Plane",
description="If enabled, the clipping planes will move with the focal plane",
default = False,
)
# PANEL: RENDER SETTINGS
# Use the device to set device settings
render_use_device: bpy.props.BoolProperty(
name="Use Device Settings",
description="If enabled, the render settings are taken from the selected device",
default = True,
update=LookingGlassAddonUI.update_render_setting_without_preset,
)
# Add a suffix with metadata to the file name