-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathBakeTarget.py
More file actions
393 lines (314 loc) · 11.9 KB
/
BakeTarget.py
File metadata and controls
393 lines (314 loc) · 11.9 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
import bpy
from .common import *
from bpy.props import *
rgba_items = (
('0', 'R', ''),
('1', 'G', ''),
('2', 'B', ''),
('3', 'A', ''),
)
normal_type_items = (
('COMBINED', 'Combined Normal', ''),
('DISPLACEMENT', 'Displacement', ''),
('OVERLAY_ONLY', 'Normal Without Bump', ''),
('VECTOR_DISPLACEMENT', 'Vector Displacement', ''),
)
def update_active_bake_target_index(self, context):
yp = self
tree = self.id_data
try: bt = yp.bake_targets[yp.active_bake_target_index]
except: return
bt_node = tree.nodes.get(bt.image_node)
if bt_node and bt_node.image:
update_image_editor_image(context, bt_node.image)
else:
update_image_editor_image(context, None)
class YBakeTargetChannel(bpy.types.PropertyGroup):
channel_name : StringProperty(
name = 'Channel Source Name',
description = 'Channel source name for bake target',
default = ''
)
subchannel_index : EnumProperty(
name = 'Subchannel',
description = 'Channel source RGBA index',
items = rgba_items,
default = '0'
)
default_value : FloatProperty(
name = 'Default Value',
description = 'Channel default value',
subtype = 'FACTOR',
default = 0.0, min=0.0, max=1.0
)
normal_type : EnumProperty(
name = 'Normal Channel Type',
description = 'Normal channel source type',
items = normal_type_items,
default = 'COMBINED'
)
invert_value : BoolProperty(
name = 'Invert Value',
description = 'Invert value',
default = False
)
class YBakeTarget(bpy.types.PropertyGroup):
name : StringProperty(
name = 'Bake Target Name',
description = 'Name of bake target name',
default = ''
)
data_type : EnumProperty(
name = 'Bake Target Data Type',
description = 'Bake target data type',
items = (
('IMAGE', 'Image', '', 'IMAGE_DATA', 0),
('VCOL', get_vertex_color_label(), '', 'GROUP_VCOL', 1),
),
default = 'IMAGE'
)
use_float : BoolProperty(
name = '32-bit Image',
description = 'Use 32-bit float image',
default = False
)
r : PointerProperty(type=YBakeTargetChannel)
g : PointerProperty(type=YBakeTargetChannel)
b : PointerProperty(type=YBakeTargetChannel)
a : PointerProperty(type=YBakeTargetChannel)
# Nodes
image_node : StringProperty(default='')
image_node_outside : StringProperty(default='')
# UI
expand_content : BoolProperty(default=True)
expand_r : BoolProperty(default=False)
expand_g : BoolProperty(default=False)
expand_b : BoolProperty(default=False)
expand_a : BoolProperty(default=False)
def update_new_bake_target_preset(self, context):
node = get_active_ypaint_node()
tree = node.node_tree
yp = tree.yp
tree_name = tree.name.replace(get_addon_title() + ' ', '')
if self.preset == 'BLANK':
suffix = ' Bake Target'
elif self.preset == 'ORM':
suffix = ' ORM'
elif self.preset == 'DX_NORMAL':
suffix = ' Normal DirectX'
#self.name = get_unique_name(tree_name + suffix, yp.bake_targets)
self.name = get_unique_name(tree_name + suffix, bpy.data.images)
class YNewBakeTarget(bpy.types.Operator):
bl_idname = "wm.y_new_bake_target"
bl_label = "New Bake Target"
bl_description = "New bake target"
bl_options = {'REGISTER', 'UNDO'}
name : StringProperty(
name = 'New Bake Target Name',
description = 'New bake target name',
default = ''
)
preset : EnumProperty(
name = 'Bake Target Preset',
description = 'Customm bake target preset',
items = (
('BLANK', 'Blank', ''),
('ORM', 'GLTF ORM', ''),
('DX_NORMAL', 'DirectX Normal', ''),
),
default = 'BLANK',
update = update_new_bake_target_preset
)
use_float : BoolProperty(
name = '32-bit Float',
description = 'Use 32-bit float image',
default = False
)
@classmethod
def poll(cls, context):
return get_active_ypaint_node()
def invoke(self, context, event):
node = get_active_ypaint_node()
tree = node.node_tree
yp = tree.yp
tree_name = tree.name.replace(get_addon_title() + ' ', '')
#self.name = get_unique_name(tree_name + ' Bake Target', yp.bake_targets)
self.name = get_unique_name(tree_name + ' Bake Target', bpy.data.images)
return context.window_manager.invoke_props_dialog(self, width=300)
def draw(self, context):
row = split_layout(self.layout, 0.3)
col = row.column(align=False)
col.label(text='Name:')
col.label(text='Preset:')
col = row.column(align=False)
col.prop(self, 'name', text='')
col.prop(self, 'preset', text='')
col.prop(self, 'use_float')
def execute(self, context):
wm = context.window_manager
node = get_active_ypaint_node()
yp = node.node_tree.yp
ypui = wm.ypui
bt = yp.bake_targets.add()
bt.name = self.name
bt.use_float = self.use_float
bt.a.default_value = 1.0
if self.preset == 'ORM':
for ch in yp.channels:
if ch.name in {'Ambient Occlusion', 'AO'}:
bt.r.channel_name = ch.name
elif ch.name in {'Roughness', 'R'}:
bt.g.channel_name = ch.name
elif ch.name in {'Metallic', 'Metalness', 'M'}:
bt.b.channel_name = ch.name
bt.r.default_value = 1.0
elif self.preset == 'DX_NORMAL':
for ch in yp.channels:
if ch.type == 'NORMAL':
bt.r.channel_name = ch.name
bt.g.channel_name = ch.name
bt.b.channel_name = ch.name
bt.r.subchannel_index = '0'
bt.g.subchannel_index = '1'
bt.b.subchannel_index = '2'
bt.g.invert_value = True
yp.active_bake_target_index = len(yp.bake_targets)-1
ypui.bake_target_ui.expand_content = True
ypui.need_update = True
#wm.yptimer.time = str(time.time())
# Update panel
context.area.tag_redraw()
return {'FINISHED'}
class YRemoveBakeTarget(bpy.types.Operator):
bl_idname = "wm.y_remove_bake_target"
bl_label = "Remove Bake Target"
bl_description = "Remove bake target"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return get_active_ypaint_node()
def execute(self, context):
wm = context.window_manager
node = get_active_ypaint_node()
tree = node.node_tree
yp = tree.yp
try: bt = yp.bake_targets[yp.active_bake_target_index]
except: return {'CANCELLED'}
# Remove related nodes
remove_node(tree, bt, 'image_node')
# Remove bake target
yp.bake_targets.remove(yp.active_bake_target_index)
if len(yp.bake_targets) > 0:
yp.active_bake_target_index = len(yp.bake_targets)-1
# Update panel
context.area.tag_redraw()
return {'FINISHED'}
class YCopyBakeTarget(bpy.types.Operator):
bl_idname = "wm.y_copy_bake_target"
bl_label = "Copy Bake Target"
bl_description = "Copy Bake Target"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
node = get_active_ypaint_node()
if not node: return False
group_tree = node.node_tree
yp = group_tree.yp
return context.object and len(yp.bake_targets) > 0 and yp.active_bake_target_index >= 0
def execute(self, context):
node = get_active_ypaint_node()
yp = node.node_tree.yp
wmp = context.window_manager.ypprops
bt = yp.bake_targets[yp.active_bake_target_index]
wmp.clipboard_bake_target.clear()
cbt = wmp.clipboard_bake_target.add()
cbt.name = bt.name
cbt.use_float = bt.use_float
cbt.data_type = bt.data_type
cbt.r.channel_name = bt.r.channel_name
cbt.r.subchannel_index = bt.r.subchannel_index
cbt.r.default_value = bt.r.default_value
cbt.r.normal_type = bt.r.normal_type
cbt.r.invert_value = bt.r.invert_value
cbt.g.channel_name = bt.g.channel_name
cbt.g.subchannel_index = bt.g.subchannel_index
cbt.g.default_value = bt.g.default_value
cbt.g.normal_type = bt.g.normal_type
cbt.g.invert_value = bt.g.invert_value
cbt.b.channel_name = bt.b.channel_name
cbt.b.subchannel_index = bt.b.subchannel_index
cbt.b.default_value = bt.b.default_value
cbt.b.normal_type = bt.b.normal_type
cbt.b.invert_value = bt.b.invert_value
cbt.a.channel_name = bt.a.channel_name
cbt.a.subchannel_index = bt.a.subchannel_index
cbt.a.default_value = bt.a.default_value
cbt.a.normal_type = bt.a.normal_type
cbt.a.invert_value = bt.a.invert_value
return {'FINISHED'}
class YPasteBakeTarget(bpy.types.Operator):
bl_idname = "wm.y_paste_bake_target"
bl_label = "Paste Bake Target As New"
bl_description = "Paste Bake Target"
bl_options = {'UNDO'}
paste_as_new : BoolProperty(
name = 'Paste As New Bake Target',
default = True
)
@classmethod
def poll(cls, context):
node = get_active_ypaint_node()
wmp = context.window_manager.ypprops
has_clipboard = len(wmp.clipboard_bake_target) > 0
return context.object and node and has_clipboard
def execute(self, context):
node = get_active_ypaint_node()
yp = node.node_tree.yp
wmp = context.window_manager.ypprops
if not self.paste_as_new and (yp.active_bake_target_index < 0 or yp.active_bake_target_index >= len(yp.bake_targets) or len(yp.bake_targets) == 0):
self.report({'ERROR'}, "Cannot paste values, no bake target selected")
return {'CANCELLED'}
cbt = wmp.clipboard_bake_target[0]
if self.paste_as_new:
name = get_unique_name(cbt.name, yp.bake_targets)
bt = yp.bake_targets.add()
bt.name = name
else:
bt = yp.bake_targets[yp.active_bake_target_index]
bt.use_float = cbt.use_float
bt.data_type = cbt.data_type
bt.r.channel_name = cbt.r.channel_name
bt.r.subchannel_index = cbt.r.subchannel_index
bt.r.default_value = cbt.r.default_value
bt.r.normal_type = cbt.r.normal_type
bt.r.invert_value = cbt.r.invert_value
bt.g.channel_name = cbt.g.channel_name
bt.g.subchannel_index = cbt.g.subchannel_index
bt.g.default_value = cbt.g.default_value
bt.g.normal_type = cbt.g.normal_type
bt.g.invert_value = cbt.g.invert_value
bt.b.channel_name = cbt.b.channel_name
bt.b.subchannel_index = cbt.b.subchannel_index
bt.b.default_value = cbt.b.default_value
bt.b.normal_type = cbt.b.normal_type
bt.b.invert_value = cbt.b.invert_value
bt.a.channel_name = cbt.a.channel_name
bt.a.subchannel_index = cbt.a.subchannel_index
bt.a.default_value = cbt.a.default_value
bt.a.normal_type = cbt.a.normal_type
bt.a.invert_value = cbt.a.invert_value
return {'FINISHED'}
def register():
bpy.utils.register_class(YNewBakeTarget)
bpy.utils.register_class(YRemoveBakeTarget)
bpy.utils.register_class(YBakeTargetChannel)
bpy.utils.register_class(YBakeTarget)
bpy.utils.register_class(YCopyBakeTarget)
bpy.utils.register_class(YPasteBakeTarget)
def unregister():
bpy.utils.unregister_class(YNewBakeTarget)
bpy.utils.unregister_class(YRemoveBakeTarget)
bpy.utils.unregister_class(YBakeTargetChannel)
bpy.utils.unregister_class(YBakeTarget)
bpy.utils.unregister_class(YCopyBakeTarget)
bpy.utils.unregister_class(YPasteBakeTarget)