forked from leomoon-studios/leomoon-lightstudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
light_list.py
338 lines (266 loc) · 13.2 KB
/
light_list.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
import bpy
from bpy.props import BoolProperty, StringProperty, PointerProperty, FloatProperty, EnumProperty, IntProperty
import os, sys, subprocess
from . common import *
from itertools import chain
from . operators import modal
from . operators.modal import close_control_panel, update_light_sets
from . operators.modal_utils import send_light_to_top, LightImage
from . light_data import *
_ = os.sep
class LightListItem(bpy.types.PropertyGroup):
""" Group of properties representing an item in the list """
def update_name(self, context):
name = self.name
if self.name == '':
name = self.handle_name
self.name = name
bpy.data.objects[self.handle_name].LLStudio.light_name = name
name: StringProperty(
name="Profile Name",
default="Untitled",
update=update_name)
handle_name: StringProperty(
description="",
default="")
class LLS_UL_LightList(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
custom_icon = 'OUTLINER_OB_LIGHT' if index == context.scene.LLStudio.list_index else 'LIGHT'
if item.handle_name in context.scene.objects:
# Make sure your code supports all 3 layout types
if self.layout_type in {'DEFAULT', 'COMPACT'}:
layout.prop(item, 'name', text='', emboss=False, translate=False)
mesh_object = context.scene.objects[item.handle_name]
mesh_collection = get_collection(mesh_object)
view_layer = find_view_layer(mesh_collection, context.view_layer.layer_collection)
icon = 'LIGHT' if view_layer.exclude else 'OUTLINER_OB_LIGHT'
layout.operator('light_studio.mute_toggle', emboss=False, icon=icon, text="").index = index
props = context.scene.LLStudio
excluded=0
for li in props.light_list:
if not li.handle_name in context.scene.objects:
continue
mesh_object = context.scene.objects[li.handle_name]
mesh_collection = get_collection(mesh_object)
vl = find_view_layer(mesh_collection, context.view_layer.layer_collection)
excluded += vl.exclude
icon = 'SOLO_ON' if excluded == len(props.light_list)-1 and not view_layer.exclude else 'SOLO_OFF'
layout.operator('light_studio.isolate', emboss=False, icon=icon, text="").index = index
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label("", icon = custom_icon)
def get_list_index(self):
ob = bpy.context.view_layer.objects.active
if isFamily(ob):
for i, li in enumerate(self.light_list):
if li.handle_name == ob.parent.name:
return i
return -1
def set_list_index(self, index):
selected_light = self.light_list[index]
light_handle = bpy.context.scene.objects[selected_light.handle_name] # Get the object
light_collection = light_handle.users_collection[0]
light_layer = find_view_layer(light_collection, bpy.context.view_layer.layer_collection)
if light_layer.exclude:
return
bpy.ops.object.select_all(action='DESELECT') # Deselect all objects
try:
basic_light_collection = [c for c in light_collection.children if c.name.startswith('LLS_Basic')][0]
basic_light_layer = find_view_layer(basic_light_collection, bpy.context.view_layer.layer_collection)
advanced_light_collection = [c for c in light_collection.children if c.name.startswith('LLS_Advanced')][0]
advanced_light_layer = find_view_layer(advanced_light_collection, bpy.context.view_layer.layer_collection)
if basic_light_layer.exclude + advanced_light_layer.exclude != 1:
advanced_light_layer.exclude = False
basic_light_layer.exclude = True
if not basic_light_layer.exclude:
light_object = basic_light_collection.objects[0]
elif not advanced_light_layer.exclude:
light_object = advanced_light_collection.objects[0]
if light_object.name in bpy.context.view_layer.objects:
bpy.context.view_layer.objects.active = light_object
light_object.select_set(True)
if modal.running_modals:
light_icon = [l for l in LightImage.lights if l._lls_handle == light_object.parent][0]
send_light_to_top(light_icon)
except IndexError:
print("Malformed light. Trying to fix.")
light = salvage_data(light_collection)
light_root = light_handle.parent.parent
profile_collection = light_root.parent.users_collection[0]
family_obs = family(light_root)
bpy.ops.object.delete({"selected_objects": list(family_obs)}, use_global=True)
bpy.data.collections.remove(light_collection)
light_from_dict(light, profile_collection)
def update_light_list_set(context):
'''Update light list set. Use when the light list needs to be synced with real object hierarchy. '''
props = context.scene.LLStudio
lls_collection, profile_collection = llscol_profilecol(context)
if profile_collection is not None:
props.light_list.clear()
lls_lights = set(profile_collection.children)
lights = [m for col in lls_lights for m in col.objects if m.name.startswith("LLS_LIGHT_HANDLE")]
lights.sort(key= lambda m: m.LLStudio.order_index)
for i, lls_handle in enumerate(lights):
lls_handle.LLStudio.order_index = i
ll = props.light_list.add()
ll.handle_name = lls_handle.name
ll.name = lls_handle.LLStudio.light_name if lls_handle.LLStudio.light_name else f"Light {lls_handle.LLStudio.order_index}"
view_layer = find_view_layer(lls_handle.users_collection[0], context.view_layer.layer_collection)
visible_lights = [c for c in lls_handle.children if c.visible_get()]
if len(visible_lights) == 1 and not view_layer.exclude:
light_object = visible_lights[0]
real_light_type = 'ADVANCED' if light_object.type == 'MESH' else 'BASIC'
if real_light_type != lls_handle.LLStudio.type:
lls_handle.LLStudio.type = lls_handle.LLStudio.type
else:
if not view_layer.exclude:
lls_handle.LLStudio.type = lls_handle.LLStudio.type
else:
for vl in view_layer.children:
vl.exclude = True
class LLS_OT_MuteToggle(bpy.types.Operator):
bl_idname = "light_studio.mute_toggle"
bl_label = "Mute Light"
bl_options = {"REGISTER", "UNDO", "INTERNAL"}
index: IntProperty(default=-1)
@classmethod
def poll(cls, context):
return context.area.type == 'VIEW_3D' and context.mode == 'OBJECT' and context.scene.LLStudio.initialized
def execute(self, context):
props = context.scene.LLStudio
handle_name = props.light_list[self.index].handle_name
light_handle = context.scene.objects[handle_name]
light_collection = get_collection(light_handle)
view_layer = find_view_layer(light_collection, context.view_layer.layer_collection)
view_layer.exclude = not view_layer.exclude
if not view_layer.exclude:
light_handle.LLStudio.type = light_handle.LLStudio.type
return {"FINISHED"}
class LLS_OT_Isolate(bpy.types.Operator):
bl_idname = "light_studio.isolate"
bl_label = "Isolate Light"
bl_options = {"REGISTER", "UNDO", "INTERNAL"}
index: IntProperty(default=-1)
@classmethod
def poll(cls, context):
return context.area.type == 'VIEW_3D' and context.mode == 'OBJECT' and context.scene.LLStudio.initialized
def execute(self, context):
props = context.scene.LLStudio
handle_name = props.light_list[self.index].handle_name
light_handle = context.scene.objects[handle_name]
light_collection = get_collection(light_handle)
view_layer = find_view_layer(light_collection, context.view_layer.layer_collection)
view_layers=[]
excluded=0
for li in props.light_list:
lls_handle = context.scene.objects[li.handle_name]
light_collection = get_collection(lls_handle)
vl = find_view_layer(light_collection, context.view_layer.layer_collection)
view_layers.append(vl)
excluded += vl.exclude
print([v.name for v in view_layers])
if not view_layer.exclude and excluded == len(view_layers)-1:
for v in view_layers:
v.exclude = False
lls_handle = v.children[0].collection.objects[0].parent
lls_handle.LLStudio.type = lls_handle.LLStudio.type
else:
for v in view_layers:
if not v.exclude:
# Do not set exclude=True twice because it propagates to children.
v.exclude = True
view_layer.exclude = False
light_handle.LLStudio.type = light_handle.LLStudio.type
return {"FINISHED"}
class LLS_OT_LightListMoveItem(bpy.types.Operator):
bl_idname = "lls_list.move_light"
bl_label = "Move Light"
bl_options = {"INTERNAL"}
direction: bpy.props.EnumProperty(
items=(
('UP', 'Up', ""),
('DOWN', 'Down', ""),))
@classmethod
def poll(self, context):
""" Enable if there's something in the list. """
return len(context.scene.LLStudio.light_list)
def execute(self, context):
props = context.scene.LLStudio
list = props.light_list
index = props.light_list_index
if self.direction == 'DOWN':
neighbor = index + 1
list.move(index,neighbor)
elif self.direction == 'UP':
neighbor = index - 1
list.move(neighbor, index)
else:
return{'CANCELLED'}
for i, e in enumerate(list):
if e.handle_name in bpy.data.objects:
bpy.data.objects[e.handle_name].LLStudio.order_index = i
return{'FINISHED'}
class LIST_OT_LightListCopyItem(bpy.types.Operator):
bl_idname = "lls_list.copy_light"
bl_label = "Copy Light"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
light = context.active_object
return context.area.type == 'VIEW_3D' and \
context.mode == 'OBJECT' and \
context.scene.LLStudio.initialized and \
light and \
light.name.startswith('LLS_LIGHT_')
def execute(self, context):
props = context.scene.LLStudio
list = props.profile_list
light_handle = context.object.parent
visible_lights = [c for c in light_handle.children if c.visible_get()]
if len(visible_lights) != 1:
visible_light_object = light_handle.children[0]
else:
visible_light_object = visible_lights[0]
lls_collection, profile_collection = llscol_profilecol(context)
lls_handle = context.object.parent
lcol = [c for c in lls_handle.users_collection if c.name.startswith('LLS_Light')]
if not lcol:
return{'CANCELLED'}
lcol = lcol[0]
light_copy = duplicate_collection(lcol, profile_collection)
lls_handle_copy = [lm for lm in light_copy.objects if lm.name.startswith('LLS_LIGHT_HANDLE')][0] # original light mesh exists so no checks necessary
lls_handle_copy.LLStudio.light_name = lls_handle.LLStudio.light_name if lls_handle.LLStudio.light_name else f"Light {lls_handle.LLStudio.order_index}"
lls_handle_copy.LLStudio.light_name += " Copy"
lls_handle_copy.LLStudio.order_index += 1
# place copied profile next to source profile
for e in props.light_list[lls_handle.LLStudio.order_index + 1 : ]:
bpy.data.objects[e.handle_name].LLStudio.order_index += 1
update_light_list_set(context)
light_object = [obj for obj in lls_handle_copy.children if obj.visible_get()][0]
bpy.context.view_layer.objects.active = light_object
light_object.select_set(True)
if modal.panel_global:
update_light_sets(modal.panel_global, context, always=True)
light_icon = [l for l in LightImage.lights if l._lls_handle == lls_handle][0]
send_light_to_top(light_icon)
return{'FINISHED'}
from bpy.app.handlers import persistent
@persistent
def load_post(scene):
context = bpy.context
props = bpy.context.scene.LLStudio
if not props.initialized:
return
lls_collection, profile_collection = llscol_profilecol(context)
if profile_collection is None:
return
# props.light_list.clear()
lls_lights = set(profile_collection.children)
lights = [m for col in lls_lights for m in col.objects if m.name.startswith("LLS_LIGHT_MESH")]
for i, lls_mesh in enumerate(lights):
convert_old_light(lls_mesh, profile_collection)
update_light_list_set(bpy.context)
def register():
bpy.app.handlers.load_post.append(load_post)
def unregister():
bpy.app.handlers.load_post.remove(load_post)