-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathListItem.py
364 lines (286 loc) · 13.2 KB
/
ListItem.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
import bpy, re
from bpy.props import *
from .common import *
def get_layer_item_index(layer):
yp = layer.id_data.yp
layer_index = get_layer_index(layer)
for i, item in enumerate(yp.list_items):
if item.index == layer_index and item.type == 'LAYER':
return i
return -1
def get_collapsed_parent_item_index(layer):
yp = layer.id_data.yp
collapsed_parent = None
for idx in reversed(get_list_of_parent_ids(layer)):
parent = yp.layers[idx]
if not parent.expand_subitems:
collapsed_parent = parent
break
if collapsed_parent:
return get_layer_item_index(collapsed_parent)
return -1
def refresh_list_items(yp, repoint_active=False):
# Get current active item and its parent
active_item_name = ''
active_item_type = ''
active_item_is_second_member = False
active_collapsed_parent_item_index = -1
if repoint_active:
# If layer index is out of bound, point to last layer
if yp.active_layer_index >= len(yp.layers) and len(yp.layers) != 0:
layer = yp.layers[len(yp.layers)-1]
if not layer.expand_subitems or not yp.enable_expandable_subitems:
active_item_name = layer.name
active_item_type = 'LAYER'
active_collapsed_parent_item_index = get_collapsed_parent_item_index(layer)
elif layer.expand_subitems and yp.enable_expandable_subitems:
for mask in layer.masks:
if mask.active_edit:
active_item_name = mask.name
active_item_type = 'MASK'
for i, ch in enumerate(layer.channels):
if not ch.enable: continue
root_ch = yp.channels[i]
if ch.override and ch.override_type in {'IMAGE', 'VCOL'} and ch.active_edit:
active_item_name = layer.name + ' ' + root_ch.name
active_item_type = 'CHANNEL_OVERRIDE'
if root_ch.type == 'NORMAL' and ch.override_1 and ch.override_1_type == 'IMAGE' and ch.active_edit_1:
active_item_name = layer.name + ' ' + root_ch.name + ' 1'
active_item_type = 'CHANNEL_OVERRIDE'
active_item_is_second_member = True
# Get current item
elif yp.active_item_index < len(yp.list_items):
item = yp.list_items[yp.active_item_index]
# Get corresponding layer
layer_index = item.index if item.type == 'LAYER' else item.parent_index
if layer_index < len(yp.layers):
layer = yp.layers[layer_index]
# Get collapsed parent item index
active_collapsed_parent_item_index = get_collapsed_parent_item_index(layer)
# Get current active item
active_item_name = item.name
active_item_type = item.type
active_item_is_second_member = item.is_second_member
# Reset list
yp.list_items.clear()
new_active_index = -1
for i, layer in enumerate(yp.layers):
# Check if layer is collapsed
collapsed_parent_item_index = get_collapsed_parent_item_index(layer)
if collapsed_parent_item_index != -1:
if collapsed_parent_item_index == active_collapsed_parent_item_index:
new_active_index = collapsed_parent_item_index
else:
item = yp.list_items.add()
item.type = 'LAYER'
item.index = i
item.name = layer.name
item.parent_index = layer.parent_idx
layer_item_index = len(yp.list_items)-1
if active_item_name == layer.name and active_item_type == 'LAYER':
new_active_index = layer_item_index
for j, ch in enumerate(layer.channels):
root_ch = yp.channels[j]
# Channel Override
if (layer.expand_subitems and
(root_ch.type != 'NORMAL' or ch.normal_map_type in {'BUMP_MAP', 'BUMP_NORMAL_MAP'}) and
(ch.override and ch.override_type in {'IMAGE', 'VCOL'}) and
ch.enable and
yp.enable_expandable_subitems
):
item = yp.list_items.add()
item.type = 'CHANNEL_OVERRIDE'
item.index = j
item.parent_index = i
item.parent_name = layer.name
item.name = layer.name + ' ' + root_ch.name
if (
# Select channel with active edit after expand subitems
(repoint_active and yp.active_layer_index == i and ch.active_edit) or
# Select correct mask after other layer uncollapsing
(active_item_name == item.name and active_item_type == 'CHANNEL_OVERRIDE' and not active_item_is_second_member)
):
new_active_index = len(yp.list_items)-1
elif active_item_name == layer.name + ' ' + root_ch.name and active_item_type == 'CHANNEL_OVERRIDE':
new_active_index = layer_item_index
# Channel Override 1 / Normal
if (layer.expand_subitems and
(root_ch.type == 'NORMAL' and ch.normal_map_type in {'NORMAL_MAP', 'BUMP_NORMAL_MAP'}) and
(ch.override_1 and ch.override_1_type in {'IMAGE', 'VCOL'}) and
ch.enable and
yp.enable_expandable_subitems
):
item = yp.list_items.add()
item.type = 'CHANNEL_OVERRIDE'
item.index = j
item.parent_index = i
item.parent_name = layer.name
item.name = layer.name + ' ' + root_ch.name + ' 1'
item.is_second_member = True
if (
# Select channel with active edit after expand subitems
(repoint_active and yp.active_layer_index == i and ch.active_edit_1) or
# Select correct mask after other layer uncollapsing
(active_item_name == item.name and active_item_type == 'CHANNEL_OVERRIDE' and active_item_is_second_member)
):
new_active_index = len(yp.list_items)-1
elif active_item_name == layer.name + ' ' + root_ch.name + ' 1' and active_item_type == 'CHANNEL_OVERRIDE':
new_active_index = layer_item_index
# Masks
for j, mask in enumerate(layer.masks):
if layer.expand_subitems and mask.enable and yp.enable_expandable_subitems:
item = yp.list_items.add()
item.type = 'MASK'
item.index = j
item.parent_index = i
item.parent_name = layer.name
item.name = mask.name
if (
# Select mask with active edit after expand subitems
(repoint_active and yp.active_layer_index == i and mask.active_edit) or
# Select correct mask after other layer uncollapsing
(active_item_name == mask.name and active_item_type == 'MASK')
):
new_active_index = len(yp.list_items)-1
elif active_item_name == mask.name and active_item_type == 'MASK':
new_active_index = layer_item_index
# If there's no new active index, set it active layer
if new_active_index == -1 and yp.active_layer_index < len(yp.layers):
new_active_index = get_layer_item_index(yp.layers[yp.active_layer_index])
# Set new active index
if new_active_index != -1:
yp.active_item_index = new_active_index
class YListItem(bpy.types.PropertyGroup):
name : StringProperty(default='')
index : IntProperty(default=0)
parent_name : StringProperty(default='')
parent_index : IntProperty(default=-1)
# To mark normal override
is_second_member : BoolProperty(default=False)
type : EnumProperty(
name = 'Item Type',
items = (
('LAYER', 'Layer', ''),
('CHANNEL_OVERRIDE', 'Channel Override', ''),
('MASK', 'Mask', '')
),
default = 'LAYER'
)
class YRefreshListItems(bpy.types.Operator):
bl_idname = "wm.y_refresh_list_items"
bl_label = "Refresh List Items"
bl_description = "Refresh List Items"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return get_active_ypaint_node()
def execute(self, context):
node = get_active_ypaint_node()
yp = node.node_tree.yp
refresh_list_items(yp)
return {'FINISHED'}
def update_expand_subitems(self, context):
yp = self.id_data.yp
refresh_list_items(yp, repoint_active=True)
def update_list_item_index(self, context):
yp = self.id_data.yp
if yp.halt_update: return
if yp.active_item_index >= len(yp.list_items): return
item = yp.list_items[yp.active_item_index]
layer_index = -1
if item.type == 'LAYER':
layer_index = item.index
# Disable active edit on masks and overrides if layer is expanded
if layer_index < len(yp.layers):
layer = yp.layers[layer_index]
if layer.expand_subitems and layer.type in {'IMAGE', 'VCOL'}:
for mask in layer.masks:
if mask.active_edit: mask.active_edit = False
for ch in layer.channels:
if ch.active_edit: ch.active_edit = False
if ch.active_edit_1: ch.active_edit_1 = False
elif item.type == 'MASK':
layer_index = item.parent_index
if layer_index < len(yp.layers):
layer = yp.layers[layer_index]
if item.index < len(layer.masks):
mask = layer.masks[item.index]
mask.active_edit = True
elif item.type == 'CHANNEL_OVERRIDE':
layer_index = item.parent_index
if layer_index < len(yp.layers):
layer = yp.layers[layer_index]
if item.index < len(layer.channels):
ch = layer.channels[item.index]
if not item.is_second_member:
ch.active_edit = True
else: ch.active_edit_1 = True
if layer_index != -1 and layer_index < len(yp.layers): # and yp.active_layer_index != layer_index:
yp.active_layer_index = layer_index
def get_active_item_entity(yp):
if yp.active_item_index >= len(yp.list_items) or len(yp.list_items) == 0:
return None
item = yp.list_items[yp.active_item_index]
if item.type == 'LAYER':
layer_index = item.index
if layer_index < len(yp.layers):
return yp.layers[layer_index]
elif item.type == 'MASK':
layer_index = item.parent_index
if layer_index < len(yp.layers):
layer = yp.layers[layer_index]
if item.index < len(layer.masks):
return layer.masks[item.index]
elif item.type == 'CHANNEL_OVERRIDE':
layer_index = item.parent_index
if layer_index < len(yp.layers):
layer = yp.layers[layer_index]
if item.index < len(layer.channels):
return layer.channels[item.index]
return None
def set_active_entity_item(entity):
yp = entity.id_data.yp
m1 = re.match(r'^yp\.layers\[(\d+)\]$', entity.path_from_id())
m2 = re.match(r'^yp\.layers\[(\d+)\]\.channels\[(\d+)\]$', entity.path_from_id())
m3 = re.match(r'^yp\.layers\[(\d+)\]\.masks\[(\d+)\]$', entity.path_from_id())
ch = None
mask = None
root_ch = None
appendix = ''
repoint_to_layer = False
if m1:
layer = yp.layers[int(m1.group(1))]
elif m2:
layer = yp.layers[int(m2.group(1))]
ch = layer.channels[int(m2.group(2))]
root_ch = yp.channels[int(m2.group(2))]
if not yp.enable_expandable_subitems or not layer.expand_subitems or (not ch.active_edit_1 and not ch.active_edit):
repoint_to_layer = True
if ch.active_edit_1:
appendix = ' 1'
elif m3:
layer = yp.layers[int(m3.group(1))]
mask = layer.masks[int(m3.group(2))]
if not yp.enable_expandable_subitems or not layer.expand_subitems or not mask.active_edit:
repoint_to_layer = True
else: return
ori_halt_update = yp.halt_update
if not yp.halt_update:
yp.halt_update = True
for i, item in enumerate(yp.list_items):
if (
((m1 or repoint_to_layer) and item.type == 'LAYER' and item.name == layer.name) or
(m2 and item.type == 'CHANNEL_OVERRIDE' and item.name == layer.name + ' ' + root_ch.name + appendix) or
(m3 and item.type == 'MASK' and item.name == mask.name)
):
if yp.active_item_index != i:
yp.active_item_index = i
break
if not ori_halt_update:
yp.halt_update = False
def register():
bpy.utils.register_class(YListItem)
bpy.utils.register_class(YRefreshListItems)
def unregister():
bpy.utils.unregister_class(YListItem)
bpy.utils.unregister_class(YRefreshListItems)