forked from ucupumar/ucupaint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaskModifier.py
272 lines (202 loc) · 7.7 KB
/
MaskModifier.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
import bpy, re
from bpy.props import *
from .common import *
from .node_connections import *
from .node_arrangements import *
mask_modifier_type_items = (
('INVERT', 'Invert', 'Invert', 'MODIFIER', 0),
('RAMP', 'Ramp', '', 'MODIFIER', 1),
('CURVE', 'Curve', '', 'MODIFIER', 2),
)
can_be_expanded = {
'RAMP',
'CURVE',
}
def update_mask_modifier_enable(self, context):
yp = self.id_data.yp
match = re.match(r'yp\.layers\[(\d+)\]\.masks\[(\d+)\]\.modifiers\[(\d+)\]', self.path_from_id())
layer = yp.layers[int(match.group(1))]
mask = layer.masks[int(match.group(2))]
mod = self
tree = get_mask_tree(mask)
if mod.type == 'INVERT':
invert = tree.nodes.get(mod.invert)
if invert:
invert.mute = not mod.enable
invert.inputs[0].default_value = 1.0 if mod.enable else 0.0
elif mod.type == 'RAMP':
ramp_mix = tree.nodes.get(mod.ramp_mix)
if ramp_mix:
ramp_mix.mute = not mod.enable
#ramp_mix.inputs[0].default_value = 1.0 if mod.enable else 0.0
elif mod.type == 'CURVE':
curve = tree.nodes.get(mod.curve)
if curve:
curve.mute = not mod.enable
#curve.inputs[0].default_value = 1.0 if mod.enable else 0.0
def add_modifier_nodes(mod, tree, ref_tree=None):
# Create the nodes
if mod.type == 'INVERT':
if ref_tree:
invert_ref = ref_tree.nodes.get(mod.invert)
invert = new_node(tree, mod, 'invert', 'ShaderNodeInvert', 'Invert')
if ref_tree:
copy_node_props(invert_ref, invert)
ref_tree.nodes.remove(invert_ref)
elif mod.type == 'RAMP':
if ref_tree:
ramp_ref = ref_tree.nodes.get(mod.ramp)
ramp_mix_ref = ref_tree.nodes.get(mod.ramp_mix)
ramp = new_node(tree, mod, 'ramp', 'ShaderNodeValToRGB', 'Ramp')
ramp_mix = new_mix_node(tree, mod, 'ramp_mix', 'Ramp Mix', 'FLOAT')
if ref_tree:
copy_node_props(ramp_ref, ramp)
copy_node_props(ramp_mix_ref, ramp_mix)
ref_tree.nodes.remove(ramp_ref)
ref_tree.nodes.remove(ramp_mix_ref)
else:
ramp_mix.inputs[0].default_value = 1.0
elif mod.type == 'CURVE':
if ref_tree:
curve_ref = ref_tree.nodes.get(mod.curve)
curve = new_node(tree, mod, 'curve', 'ShaderNodeRGBCurve', 'Curve')
if ref_tree:
copy_node_props(curve_ref, curve)
ref_tree.nodes.remove(curve_ref)
def delete_modifier_nodes(tree, mod):
if mod.type == 'INVERT':
remove_node(tree, mod, 'invert')
elif mod.type == 'RAMP':
remove_node(tree, mod, 'ramp')
remove_node(tree, mod, 'ramp_mix')
elif mod.type == 'CURVE':
remove_node(tree, mod, 'curve')
def add_new_mask_modifier(mask, modifier_type):
tree = get_mask_tree(mask)
name = [mt[1] for mt in mask_modifier_type_items if mt[0] == modifier_type][0]
m = mask.modifiers.add()
m.name = get_unique_name(name, mask.modifiers)
m.type = modifier_type
add_modifier_nodes(m, tree)
def draw_modifier_properties(tree, m, layout):
if m.type == 'RAMP':
ramp = tree.nodes.get(m.ramp)
layout.template_color_ramp(ramp, "color_ramp", expand=True)
elif m.type == 'CURVE':
curve = tree.nodes.get(m.curve)
curve.draw_buttons_ext(bpy.context, layout)
class YNewMaskModifier(bpy.types.Operator):
bl_idname = "node.y_new_mask_modifier"
bl_label = "New Mask Modifier"
bl_description = "New Mask Modifier"
bl_options = {'REGISTER', 'UNDO'}
type : EnumProperty(
name = 'Modifier Type',
items = mask_modifier_type_items,
default = 'INVERT')
@classmethod
def poll(cls, context):
return hasattr(context, 'mask') and hasattr(context, 'layer')
def execute(self, context):
match = re.match(r'yp\.layers\[(\d+)\]\.masks\[(\d+)\]', context.mask.path_from_id())
mask_idx = int(match.group(2))
add_new_mask_modifier(context.mask, self.type)
rearrange_layer_nodes(context.layer)
reconnect_layer_nodes(context.layer)
# Update UI
ypui = context.window_manager.ypui
ypui.layer_ui.masks[mask_idx].expand_content = True
ypui.need_update = True
return {'FINISHED'}
class YMoveMaskModifier(bpy.types.Operator):
bl_idname = "node.y_move_mask_modifier"
bl_label = "Move Mask Modifier"
bl_description = "Move Mask Modifier"
bl_options = {'REGISTER', 'UNDO'}
direction : EnumProperty(
name = 'Direction',
items = (('UP', 'Up', ''),
('DOWN', 'Down', '')),
default = 'UP')
@classmethod
def poll(cls, context):
return hasattr(context, 'mask') and hasattr(context, 'modifier') and hasattr(context, 'layer')
def execute(self, context):
layer = context.layer
mask = context.mask
mod = context.modifier
num_mods = len(mask.modifiers)
if num_mods < 2: return {'CANCELLED'}
index = -1
for i, m in enumerate(mask.modifiers):
if m == mod:
index = i
break
if index == -1: return {'CANCELLED'}
# Get new index
if self.direction == 'UP' and index > 0:
new_index = index-1
elif self.direction == 'DOWN' and index < num_mods-1:
new_index = index+1
else:
return {'CANCELLED'}
# Swap modifier
mask.modifiers.move(index, new_index)
# Reconnect modifier nodes
rearrange_layer_nodes(layer)
reconnect_layer_nodes(layer)
# Update UI
context.window_manager.ypui.need_update = True
return {'FINISHED'}
class YRemoveMaskModifier(bpy.types.Operator):
bl_idname = "node.y_remove_mask_modifier"
bl_label = "Remove Mask Modifier"
bl_description = "Remove Mask Modifier"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return hasattr(context, 'layer') and hasattr(context, 'mask') and hasattr(context, 'modifier')
def execute(self, context):
layer = context.layer
mask = context.mask
mod = context.modifier
tree = get_mask_tree(mask)
index = -1
for i, m in enumerate(mask.modifiers):
if m == mod:
index = i
break
if index == -1: return {'CANCELLED'}
delete_modifier_nodes(tree, context.modifier)
mask.modifiers.remove(index)
rearrange_layer_nodes(layer)
reconnect_layer_nodes(layer)
# Update UI
context.window_manager.ypui.need_update = True
return {'FINISHED'}
class YMaskModifier(bpy.types.PropertyGroup):
enable : BoolProperty(
name = 'Enable Modifier',
description = 'Enable modifier',
default=True, update=update_mask_modifier_enable)
name : StringProperty(default='')
type : EnumProperty(
name = 'Modifier Type',
items = mask_modifier_type_items,
default = 'INVERT')
ramp : StringProperty(default='')
ramp_mix : StringProperty(default='')
invert : StringProperty(default='')
curve : StringProperty(default='')
# UI
expand_content : BoolProperty(default=True)
def register():
bpy.utils.register_class(YNewMaskModifier)
bpy.utils.register_class(YMoveMaskModifier)
bpy.utils.register_class(YRemoveMaskModifier)
bpy.utils.register_class(YMaskModifier)
def unregister():
bpy.utils.unregister_class(YNewMaskModifier)
bpy.utils.unregister_class(YMoveMaskModifier)
bpy.utils.unregister_class(YRemoveMaskModifier)
bpy.utils.unregister_class(YMaskModifier)