-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLinking.py
More file actions
749 lines (632 loc) · 30.8 KB
/
Copy pathLinking.py
File metadata and controls
749 lines (632 loc) · 30.8 KB
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
import bpy
from bpy.app.handlers import persistent
# -------------------------------------------------------------------
# Helper: Get Selected Collections from the Outliner
# -------------------------------------------------------------------
def get_selected_collections(context):
"""
Uses a temporary override to obtain selected collections from the Outliner.
Returns a list of bpy.types.Collection.
"""
selected = []
outliner_area = next((area for area in context.window.screen.areas if area.type == 'OUTLINER'), None)
if outliner_area:
region = next((r for r in outliner_area.regions if r.type == 'WINDOW'), None)
with context.temp_override(window=context.window, screen=context.screen, area=outliner_area, region=region):
if hasattr(bpy.context, "selected_ids"):
for id_item in bpy.context.selected_ids:
if isinstance(id_item, bpy.types.Collection):
selected.append(id_item)
return selected
# -------------------------------------------------------------------
# Helper Functions: Ensure BB_ Collections
# -------------------------------------------------------------------
def ensure_bb_collection(light):
"""
Ensures that the BB_Light Linking collection exists for the given light.
The collection will not be linked to the scene hierarchy and will remain hidden in the Outliner.
"""
prop_name = "light_linking_receiver_collection"
expected_name = f"BB_Light Linking for {light.name}"
bb_collection = bpy.data.collections.get(expected_name)
if bb_collection:
light[prop_name] = expected_name
return bb_collection
# Use the operator to create the light linking collection
try:
bpy.ops.object.select_all(action='DESELECT')
light.select_set(True)
bpy.context.view_layer.objects.active = light
bpy.ops.object.light_linking_receiver_collection_new()
if hasattr(light, "light_linking") and hasattr(light.light_linking, "receiver_collection"):
new_collection = light.light_linking.receiver_collection
new_collection.name = expected_name
light[prop_name] = expected_name
return new_collection
except Exception as e:
print(f"Operator failed: {e}. Falling back to manual collection creation.")
new_collection = bpy.data.collections.new(expected_name)
light[prop_name] = expected_name
return new_collection
def ensure_shadow_collection(light):
"""
For shadow linking, ensures that the BB_Shadow Linking collection exists.
Expected name: "BB_Shadow Linking for <light_name>".
The collection will not be linked to the scene hierarchy and will remain hidden in the Outliner.
"""
prop_name = "shadow_linking_blocker_collection"
expected_name = f"BB_Shadow Linking for {light.name}"
shadow_collection = bpy.data.collections.get(expected_name)
if shadow_collection:
if hasattr(light, "light_linking") and hasattr(light.light_linking, "blocker_collection"):
light.light_linking.blocker_collection = shadow_collection
light[prop_name] = expected_name
return shadow_collection
new_collection = bpy.data.collections.new(expected_name)
if hasattr(light, "light_linking") and hasattr(light.light_linking, "blocker_collection"):
light.light_linking.blocker_collection = new_collection
light[prop_name] = expected_name
return new_collection
# -------------------------------------------------------------------
# Property Groups for List Items
# -------------------------------------------------------------------
class LL_LightItem(bpy.types.PropertyGroup):
name: bpy.props.StringProperty()
obj: bpy.props.PointerProperty(type=bpy.types.Object)
selected: bpy.props.BoolProperty(default=False)
class LL_MeshItem(bpy.types.PropertyGroup):
name: bpy.props.StringProperty()
obj: bpy.props.PointerProperty(type=bpy.types.Object)
selected: bpy.props.BoolProperty(default=False)
class LL_CollectionItem(bpy.types.PropertyGroup):
name: bpy.props.StringProperty()
coll: bpy.props.PointerProperty(type=bpy.types.Collection)
selected: bpy.props.BoolProperty(default=False)
# -------------------------------------------------------------------
# Update Functions for Full List Population
# -------------------------------------------------------------------
def update_light_items(scene, context):
prev_sel = {item.name: item.selected for item in scene.ll_light_items}
scene.ll_light_items.clear()
for obj in scene.objects:
if obj.type == 'LIGHT':
item = scene.ll_light_items.add()
item.name = obj.name
item.obj = obj
item.selected = prev_sel.get(obj.name, False)
scene.ll_light_index = 0 if scene.ll_light_items else -1
def update_mesh_items(scene, context):
prev_sel = {item.name: item.selected for item in scene.ll_mesh_items}
scene.ll_mesh_items.clear()
for obj in scene.objects:
if obj.type == 'MESH':
item = scene.ll_mesh_items.add()
item.name = obj.name
item.obj = obj
item.selected = prev_sel.get(obj.name, False)
scene.ll_mesh_index = 0 if scene.ll_mesh_items else -1
def update_collection_items(scene, context):
prev_sel = {item.name: item.selected for item in scene.ll_collection_items}
scene.ll_collection_items.clear()
for coll in bpy.data.collections:
if "Light Linking for" in coll.name:
continue
item = scene.ll_collection_items.add()
item.name = coll.name
item.coll = coll
item.selected = prev_sel.get(coll.name, False)
scene.ll_collection_index = 0 if scene.ll_collection_items else -1
def force_redraw(context):
for area in context.screen.areas:
if area.type == 'VIEW_3D':
area.tag_redraw()
# -------------------------------------------------------------------
# Operator to Toggle an Item’s Selection
# -------------------------------------------------------------------
class LL_OT_ToggleSelection(bpy.types.Operator):
bl_idname = "ll_editor.toggle_selection"
bl_label = "Toggle Selection"
bl_description = "Toggle the selection state for this item"
item_name: bpy.props.StringProperty()
item_type: bpy.props.EnumProperty(
items=[
('LIGHT', "Light", ""),
('MESH', "Mesh", ""),
('COLLECTION', "Collection", ""),
]
)
def execute(self, context):
scene = context.scene
if self.item_type == 'LIGHT':
for item in scene.ll_light_items:
if item.name == self.item_name:
item.selected = not item.selected
break
elif self.item_type == 'MESH':
for item in scene.ll_mesh_items:
if item.name == self.item_name:
item.selected = not item.selected
break
elif self.item_type == 'COLLECTION':
for item in scene.ll_collection_items:
if item.name == self.item_name:
item.selected = not item.selected
break
else:
self.report({'WARNING'}, "Unknown item type")
return {'CANCELLED'}
return {'FINISHED'}
# -------------------------------------------------------------------
# Operators for Refreshing/Resetting Lists
# -------------------------------------------------------------------
class LL_OT_RefreshSelectedLights(bpy.types.Operator):
bl_idname = "ll_editor.refresh_selected_lights"
bl_label = "Refresh Selected Lights"
bl_description = "Filter the lights list to show only lights selected in the viewport. If none are selected, use the active light."
def execute(self, context):
scene = context.scene
selected_lights = [obj for obj in context.selected_objects if obj.type == 'LIGHT']
if not selected_lights:
active_obj = context.view_layer.objects.active
if active_obj and active_obj.type == 'LIGHT':
selected_lights.append(active_obj)
if not selected_lights:
self.report({'WARNING'}, "No lights selected in the viewport")
return {'CANCELLED'}
scene.ll_light_items.clear()
for obj in selected_lights:
item = scene.ll_light_items.add()
item.name = obj.name
item.obj = obj
item.selected = True
scene.ll_light_index = 0 if scene.ll_light_items else -1
force_redraw(context)
self.report({'INFO'}, f"Filtered lights to {len(selected_lights)} item(s)")
return {'FINISHED'}
class LL_OT_RefreshSelectedMeshes(bpy.types.Operator):
bl_idname = "ll_editor.refresh_selected_meshes"
bl_label = "Refresh Selected Meshes"
bl_description = "Filter the mesh list to show only meshes selected in the viewport. If none are selected, use the active mesh."
def execute(self, context):
scene = context.scene
selected_meshes = [obj for obj in context.selected_objects if obj.type == 'MESH']
if not selected_meshes:
active_obj = context.view_layer.objects.active
if active_obj and active_obj.type == 'MESH':
selected_meshes.append(active_obj)
if not selected_meshes:
self.report({'WARNING'}, "No meshes selected in the viewport")
return {'CANCELLED'}
scene.ll_mesh_items.clear()
for obj in selected_meshes:
item = scene.ll_mesh_items.add()
item.name = obj.name
item.obj = obj
item.selected = True
scene.ll_mesh_index = 0 if scene.ll_mesh_items else -1
force_redraw(context)
self.report({'INFO'}, f"Filtered meshes to {len(selected_meshes)} item(s)")
return {'FINISHED'}
class LL_OT_RefreshSelectedCollections(bpy.types.Operator):
bl_idname = "ll_editor.refresh_selected_collections"
bl_label = "Refresh Selected Collections"
bl_description = (
"Filter the collection list to show only collections selected in the Outliner. "
"If none are selected, fall back to the UI list selection."
)
def execute(self, context):
scene = context.scene
selected_collections = get_selected_collections(context)
if not selected_collections:
selected_collections = [item.coll for item in scene.ll_collection_items if item.selected and item.coll]
if not selected_collections and scene.ll_collection_index >= 0:
active_item = scene.ll_collection_items[scene.ll_collection_index]
if active_item.coll:
selected_collections.append(active_item.coll)
if not selected_collections:
self.report({'WARNING'}, "No collections selected")
return {'CANCELLED'}
scene.ll_collection_items.clear()
for coll in selected_collections:
item = scene.ll_collection_items.add()
item.name = coll.name
item.coll = coll
item.selected = True
scene.ll_collection_index = 0 if scene.ll_collection_items else -1
for area in context.screen.areas:
if area.type == 'VIEW_3D':
area.tag_redraw()
self.report({'INFO'}, f"Filtered collections to {len(selected_collections)} item(s)")
return {'FINISHED'}
class LL_OT_RefreshAllLights(bpy.types.Operator):
bl_idname = "ll_editor.refresh_all_lights"
bl_label = "Refresh All Lights"
bl_description = "Display all lights in the scene that are turned on and renderable"
def execute(self, context):
scene = context.scene
prev_sel = {item.name: item.selected for item in scene.ll_light_items}
scene.ll_light_items.clear()
for obj in scene.objects:
if obj.type == 'LIGHT':
if not obj.hide_render and not obj.hide_viewport:
item = scene.ll_light_items.add()
item.name = obj.name
item.obj = obj
item.selected = prev_sel.get(obj.name, False)
scene.ll_light_index = 0 if scene.ll_light_items else -1
force_redraw(context)
self.report({'INFO'}, f"Listed {len(scene.ll_light_items)} visible and renderable lights")
return {'FINISHED'}
class LL_OT_ResetLights(bpy.types.Operator):
bl_idname = "ll_editor.reset_lights"
bl_label = "Reset Lights"
bl_description = "Deselect all lights in the list"
def execute(self, context):
for item in context.scene.ll_light_items:
item.selected = False
force_redraw(context)
self.report({'INFO'}, "Light selections reset")
return {'FINISHED'}
class LL_OT_RefreshAllMeshes(bpy.types.Operator):
bl_idname = "ll_editor.refresh_all_meshes"
bl_label = "Refresh All Meshes"
bl_description = "Display all meshes in the scene"
def execute(self, context):
update_mesh_items(context.scene, context)
force_redraw(context)
self.report({'INFO'}, f"Listed all {len(context.scene.ll_mesh_items)} meshes")
return {'FINISHED'}
class LL_OT_ResetMeshes(bpy.types.Operator):
bl_idname = "ll_editor.reset_meshes"
bl_label = "Reset Meshes"
bl_description = "Deselect all meshes in the list"
def execute(self, context):
for item in context.scene.ll_mesh_items:
item.selected = False
force_redraw(context)
self.report({'INFO'}, "Mesh selections reset")
return {'FINISHED'}
class LL_OT_RefreshAllCollections(bpy.types.Operator):
bl_idname = "ll_editor.refresh_all_collections"
bl_label = "Refresh All Collections"
bl_description = "Display all collections in the scene"
def execute(self, context):
update_collection_items(context.scene, context)
force_redraw(context)
self.report({'INFO'}, f"Listed all {len(context.scene.ll_collection_items)} collections")
return {'FINISHED'}
class LL_OT_ResetCollections(bpy.types.Operator):
bl_idname = "ll_editor.reset_collections"
bl_label = "Reset Collections"
bl_description = "Deselect all collections in the list"
def execute(self, context):
for item in context.scene.ll_collection_items:
item.selected = False
force_redraw(context)
self.report({'INFO'}, "Collection selections reset")
return {'FINISHED'}
# -------------------------------------------------------------------
# Operators for Linking/Unlinking (Here's the "Assign" operator)
# -------------------------------------------------------------------
class LL_OT_Link(bpy.types.Operator):
bl_idname = "ll_editor.link"
bl_label = "Link Lights to Objects"
bl_description = (
"For each selected light, use the UI-selected BB_ light linking collection (or create it) and add "
"the selected meshes (from the Mesh and Collection lists) to it."
)
def execute(self, context):
scene = context.scene
selected_lights = [item.obj for item in scene.ll_light_items if item.selected and item.obj]
selected_meshes = [item.obj for item in scene.ll_mesh_items if item.selected and item.obj]
collection_meshes = []
for item in scene.ll_collection_items:
if item.selected and item.coll:
for obj in item.coll.all_objects:
if obj.type == 'MESH':
collection_meshes.append(obj)
# If no groups exist at all (i.e. "lightgroups" in the view layer is empty),
# you could check that here if needed, e.g.:
# view_layer = context.view_layer
# if not hasattr(view_layer, "lightgroups") or not view_layer.lightgroups:
# self.report({'WARNING'}, "Please create a light group first.")
# return {'CANCELLED'}
if not selected_lights:
self.report({'WARNING'}, "No lights selected")
return {'CANCELLED'}
all_meshes = {obj.name: obj for obj in (selected_meshes + collection_meshes)}.values()
if not list(all_meshes):
self.report({'WARNING'}, "No mesh objects selected")
return {'CANCELLED'}
total_linked_meshes = 0
for light in selected_lights:
if not light.visible_get():
self.report({'ERROR'}, f"Light must be visible for linking: {light.name}")
continue
bpy.ops.object.select_all(action='DESELECT')
light.select_set(True)
context.view_layer.objects.active = light
new_group = ensure_bb_collection(light)
# Here is where we can show "Please create a light group first" if we fail:
if not new_group:
# Instead of the old message:
# self.report({'WARNING'}, f"Failed to create or retrieve linking group for {light.name}")
self.report({'WARNING'}, "Please create a light group first.")
continue
linked_meshes = 0
for obj in all_meshes:
if not new_group.objects.get(obj.name):
new_group.objects.link(obj)
linked_meshes += 1
total_linked_meshes += linked_meshes
self.report({'INFO'}, f"Linked {len(selected_lights)} light(s) to {total_linked_meshes} mesh(es)")
return {'FINISHED'}
class LL_OT_Unlink(bpy.types.Operator):
bl_idname = "ll_editor.unlink"
bl_label = "Unlink Lights from Objects"
bl_description = (
"For each selected light, remove objects (from the Mesh and Collection lists) that are linked "
"in the BB_ light linking collection."
)
def execute(self, context):
scene = context.scene
selected_lights = [item.obj for item in scene.ll_light_items if item.selected and item.obj]
if not selected_lights:
self.report({'WARNING'}, "No lights selected")
return {'CANCELLED'}
selected_meshes = [item.obj for item in scene.ll_mesh_items if item.selected and item.obj]
collection_meshes = []
for item in scene.ll_collection_items:
if item.selected and item.coll:
for obj in item.coll.all_objects:
if obj.type == 'MESH':
collection_meshes.append(obj)
all_meshes = {obj.name: obj for obj in (selected_meshes + collection_meshes)}.values()
total_removed = 0
for light in selected_lights:
group_name = f"BB_Light Linking for {light.name}"
linking_group = bpy.data.collections.get(group_name)
if not linking_group:
self.report({'WARNING'}, f"No BB_ linking group found for {light.name}")
continue
removed = 0
for obj in list(linking_group.objects):
if obj.name in [m_obj.name for m_obj in all_meshes]:
linking_group.objects.unlink(obj)
removed += 1
total_removed += removed
if "light_linking_receiver_collection" in light:
del light["light_linking_receiver_collection"]
self.report({'INFO'}, f"Unlinked objects from {len(selected_lights)} light(s); removed {total_removed} object(s)")
return {'FINISHED'}
class LL_OT_ShadowLink(bpy.types.Operator):
bl_idname = "ll_editor.shadow_link"
bl_label = "Shadow Link Lights to Objects"
bl_description = (
"For each selected light, use the UI-selected BB_ shadow linking collection (or create it) and add "
"the selected meshes (from the Mesh and Collection lists) to it."
)
def execute(self, context):
scene = context.scene
selected_lights = [item.obj for item in scene.ll_light_items if item.selected and item.obj]
selected_meshes = [item.obj for item in scene.ll_mesh_items if item.selected and item.obj]
collection_meshes = []
for item in scene.ll_collection_items:
if item.selected and item.coll:
for obj in item.coll.all_objects:
if obj.type == 'MESH':
collection_meshes.append(obj)
all_meshes = {obj.name: obj for obj in (selected_meshes + collection_meshes)}.values()
if not selected_lights:
self.report({'WARNING'}, "No lights selected for shadow linking.")
return {'CANCELLED'}
if not list(all_meshes):
self.report({'WARNING'}, "No mesh objects selected for shadow linking.")
return {'CANCELLED'}
total_linked_meshes = 0
for light in selected_lights:
if not light.visible_get():
self.report({'ERROR'}, f"Light must be visible for linking: {light.name}")
continue
bpy.ops.object.select_all(action='DESELECT')
light.select_set(True)
context.view_layer.objects.active = light
new_group = ensure_shadow_collection(light)
if not new_group:
self.report({'WARNING'}, "Failed to create or retrieve shadow linking group for {light.name}")
continue
linked_meshes = 0
for obj in all_meshes:
if not new_group.objects.get(obj.name):
new_group.objects.link(obj)
linked_meshes += 1
light["shadow_linking_blocker_collection"] = new_group.name
total_linked_meshes += linked_meshes
self.report({'INFO'}, f"Shadow Linked {len(selected_lights)} light(s) to {total_linked_meshes} mesh(es)")
return {'FINISHED'}
class LL_OT_ShadowUnlink(bpy.types.Operator):
bl_idname = "ll_editor.shadow_unlink"
bl_label = "Shadow Unlink Lights from Objects"
bl_description = (
"For each selected light, remove objects (from the Mesh and Collection lists) that are linked "
"in the BB_ shadow linking collection."
)
def execute(self, context):
scene = context.scene
selected_lights = [item.obj for item in scene.ll_light_items if item.selected and item.obj]
if not selected_lights:
self.report({'WARNING'}, "No lights selected")
return {'CANCELLED'}
selected_meshes = [item.obj for item in scene.ll_mesh_items if item.selected and item.obj]
collection_meshes = []
for item in scene.ll_collection_items:
if item.selected and item.coll:
for obj in item.coll.all_objects:
if obj.type == 'MESH':
collection_meshes.append(obj)
all_meshes = {obj.name: obj for obj in (selected_meshes + collection_meshes)}.values()
total_removed = 0
for light in selected_lights:
expected_name = f"BB_Shadow Linking for {light.name}"
linking_group = bpy.data.collections.get(expected_name)
if not linking_group:
self.report({'INFO'}, f"No shadow linking group '{expected_name}' found for light '{light.name}'")
continue
removed = 0
for obj in list(linking_group.objects):
if obj.name in [m_obj.name for m_obj in all_meshes]:
try:
linking_group.objects.unlink(obj)
removed += 1
except Exception as e:
print(f"DEBUG: Error unlinking {obj.name}: {e}")
total_removed += removed
if "shadow_linking_blocker_collection" in light:
del light["shadow_linking_blocker_collection"]
self.report({'INFO'}, f"Shadow Unlinked objects from {len(selected_lights)} light(s); removed {total_removed} object(s)")
return {'FINISHED'}
# -------------------------------------------------------------------
# UIList Classes for Scrollable Lists
# -------------------------------------------------------------------
class LL_UL_LightList_UI(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
self.use_filter_show = True
row = layout.row(align=True)
row.prop(item, "selected", text="")
row.label(text=item.name)
class LL_UL_MeshList_UI(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
self.use_filter_show = True
row = layout.row(align=True)
row.prop(item, "selected", text="")
row.label(text=item.name)
class LL_UL_CollectionList_UI(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
self.use_filter_show = True
row = layout.row(align=True)
row.prop(item, "selected", text="")
row.label(text=item.name)
# -------------------------------------------------------------------
# Panel – UI Layout
# -------------------------------------------------------------------
class LL_PT_Panel(bpy.types.Panel):
bl_label = "Light Link"
bl_idname = "LL_PT_panel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Light Editor"
bl_options = {'DEFAULT_CLOSED'}
@classmethod
def poll(cls, context):
return context.scene.render.engine == 'CYCLES'
def draw(self, context):
layout = self.layout
scene = context.scene
scene = context.scene
main_row = layout.row(align=True)
col_lights = main_row.column(align=True)
col_lights.label(text="Lights")
col_lights.template_list("LL_UL_LightList_UI", "", scene, "ll_light_items", scene, "ll_light_index", rows=scene.ll_list_rows)
col_meshes = main_row.column(align=True)
col_meshes.label(text="Meshes")
col_meshes.template_list("LL_UL_MeshList_UI", "", scene, "ll_mesh_items", scene, "ll_mesh_index", rows=scene.ll_list_rows)
col_colls = main_row.column(align=True)
col_colls.label(text="Collections")
col_colls.template_list("LL_UL_CollectionList_UI", "", scene, "ll_collection_items", scene, "ll_collection_index", rows=scene.ll_list_rows)
layout.separator()
layout.prop(scene, "ll_list_rows", text="List Height")
layout.separator()
op_row = layout.row(align=True)
col_light_ops = op_row.column(align=True)
col_light_ops.operator("ll_editor.refresh_selected_lights", text="Selected Lights")
col_light_ops.operator("ll_editor.refresh_all_lights", text="All Visible Lights")
col_light_ops.operator("ll_editor.reset_lights", text="Deselect All")
col_mesh_ops = op_row.column(align=True)
col_mesh_ops.operator("ll_editor.refresh_selected_meshes", text="Selected Meshes")
col_mesh_ops.operator("ll_editor.refresh_all_meshes", text="All Meshes")
col_mesh_ops.operator("ll_editor.reset_meshes", text="Deselect All")
col_coll_ops = op_row.column(align=True)
col_coll_ops.operator("ll_editor.refresh_selected_collections", text="Selected Collections")
col_coll_ops.operator("ll_editor.refresh_all_collections", text="All Collections")
col_coll_ops.operator("ll_editor.reset_collections", text="Deselect All")
layout.separator()
link_row = layout.row(align=True)
link_row.operator("ll_editor.link", text="Light Link")
link_row.operator("ll_editor.unlink", text="Light Unlink")
shadow_link_row = layout.row(align=True)
shadow_link_row.operator("ll_editor.shadow_link", text="Shadow Link")
shadow_link_row.operator("ll_editor.shadow_unlink", text="Shadow Unlink")
@persistent
def LL_clear_handler(dummy):
update_light_items(bpy.context.scene, bpy.context)
update_mesh_items(bpy.context.scene, bpy.context)
update_collection_items(bpy.context.scene, bpy.context)
# -------------------------------------------------------------------
# Registration
# -------------------------------------------------------------------
classes = [
LL_LightItem,
LL_MeshItem,
LL_CollectionItem,
LL_OT_ToggleSelection,
LL_OT_RefreshSelectedLights,
LL_OT_RefreshSelectedMeshes,
LL_OT_RefreshSelectedCollections,
LL_OT_RefreshAllLights,
LL_OT_ResetLights,
LL_OT_RefreshAllMeshes,
LL_OT_ResetMeshes,
LL_OT_RefreshAllCollections,
LL_OT_ResetCollections,
LL_OT_Link,
LL_OT_Unlink,
LL_OT_ShadowLink,
LL_OT_ShadowUnlink,
LL_UL_LightList_UI,
LL_UL_MeshList_UI,
LL_UL_CollectionList_UI,
]
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.utils.register_class(LL_PT_Panel)
bpy.types.Scene.ll_light_items = bpy.props.CollectionProperty(type=LL_LightItem)
bpy.types.Scene.ll_mesh_items = bpy.props.CollectionProperty(type=LL_MeshItem)
bpy.types.Scene.ll_collection_items = bpy.props.CollectionProperty(type=LL_CollectionItem)
bpy.types.Scene.ll_light_index = bpy.props.IntProperty(default=-1)
bpy.types.Scene.ll_mesh_index = bpy.props.IntProperty(default=-1)
bpy.types.Scene.ll_collection_index = bpy.props.IntProperty(default=-1)
bpy.types.Scene.ll_list_rows = bpy.props.IntProperty(
name="List Height",
description="Number of rows to display in each list",
default=10,
min=1,
max=50
)
bpy.app.handlers.load_post.append(LL_clear_handler)
def unregister():
# Each step is guarded so one failure can't abort the rest of unregister()
# and leave classes registered (which breaks the next enable).
for prop in (
"ll_light_items", "ll_mesh_items", "ll_collection_items",
"ll_light_index", "ll_mesh_index", "ll_collection_index",
"ll_list_rows",
):
if hasattr(bpy.types.Scene, prop):
try:
delattr(bpy.types.Scene, prop)
except (AttributeError, TypeError):
pass
try:
bpy.utils.unregister_class(LL_PT_Panel)
except (RuntimeError, ValueError):
pass
for cls in reversed(classes):
try:
bpy.utils.unregister_class(cls)
except (RuntimeError, ValueError):
pass
if LL_clear_handler in bpy.app.handlers.load_post:
bpy.app.handlers.load_post.remove(LL_clear_handler)
if __name__ == "__main__":
register()