-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
BakeTarget.py
266 lines (215 loc) · 7.59 KB
/
BakeTarget.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
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', 'Vertex Color', '', '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 = "node.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 = "node.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'}
def register():
bpy.utils.register_class(YNewBakeTarget)
bpy.utils.register_class(YRemoveBakeTarget)
bpy.utils.register_class(YBakeTargetChannel)
bpy.utils.register_class(YBakeTarget)
def unregister():
bpy.utils.unregister_class(YNewBakeTarget)
bpy.utils.unregister_class(YRemoveBakeTarget)
bpy.utils.unregister_class(YBakeTargetChannel)
bpy.utils.unregister_class(YBakeTarget)