-
Notifications
You must be signed in to change notification settings - Fork 46
/
bakelab_post.py
388 lines (345 loc) · 15.4 KB
/
bakelab_post.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import bpy
from bpy.types import (
Operator
)
from bpy.props import (
IntProperty,
EnumProperty,
BoolProperty,
FloatProperty,
StringProperty,
PointerProperty,
CollectionProperty
)
from .bakelab_tools import (
SelectObject,
SelectObjects
)
class BakeLab_GenerateMaterials(Operator):
"""Generate materials based on baked datas"""
bl_idname = "bakelab.generate_mats"
bl_label = "Generate Materials"
bl_options = {'REGISTER','UNDO'}
def generate_mat(self, bakeMapData, name):
new_mat = bpy.data.materials.new(name+'_BAKED')
new_mat.use_nodes = True
if self.add_nodes(bakeMapData, new_mat):
return new_mat
else:
bpy.data.materials.remove(new_mat)
return None
def add_nodes(self, bakeMapData, mat):
nodes = mat.node_tree.nodes
links = mat.node_tree.links
for node in nodes:
nodes.remove(node)
out = nodes.new(type = 'ShaderNodeOutputMaterial')
out.location = 0, 0
pbr = nodes.new(type = 'ShaderNodeBsdfPrincipled')
pbr.location = -400, 0
links.new(pbr.outputs[0],out.inputs[0])
uvm = nodes.new(type = 'ShaderNodeTexCoord')
uvm.location = -1800, 0
pass_available = False
node_y_shift = 0
for data in bakeMapData:
bake_map = data.bake_map
bake_image = data.image
if bake_map.type != 'CustomPass' and bake_map.type in self.baked_types:
continue
self.baked_types.append(bake_map.type)
if bake_map.type == 'Albedo':
imgNode = nodes.new(type = 'ShaderNodeTexImage')
imgNode.hide = True
imgNode.location = -1000,-100
imgNode.image = bake_image
links.new(imgNode.outputs[0], pbr.inputs[0])
links.new(uvm.outputs[2], imgNode.inputs[0])
pass_available = True
if bake_map.type == 'Combined':
imgNode = nodes.new(type = 'ShaderNodeTexImage')
imgNode.hide = True
imgNode.location = -1000, 300
imgNode.image = bake_image
EmitNode = nodes.new(type = 'ShaderNodeEmission')
EmitNode.location = -400, 300
EmitNode.width = pbr.width
EmitNode.hide = True
links.new(imgNode.outputs[0], EmitNode.inputs[0])
links.new(EmitNode.outputs[0], out.inputs[0])
links.new(uvm.outputs[2], imgNode.inputs[0])
pass_available = True
if bake_map.type == 'Normal':
imgNode = nodes.new(type = 'ShaderNodeTexImage')
imgNode.hide = True
imgNode.location = -1000, -500
imgNode.image = bake_image
nmNode = nodes.new(type = 'ShaderNodeNormalMap')
nmNode.hide = True
nmNode.location = -700, -500
nmNode.space = bake_map.normal_space
links.new(imgNode.outputs[0], nmNode.inputs[1])
links.new(nmNode.outputs[0], pbr.inputs[20])
links.new(uvm.outputs[2], imgNode.inputs[0])
pass_available = True
if bake_map.type == 'AO':
out.location[0] += 250
imgNode = nodes.new(type = 'ShaderNodeTexImage')
imgNode.hide = True
imgNode.location = -1000, 150
imgNode.image = bake_image
reroute = nodes.new(type = 'NodeReroute')
reroute.location = -150, 150
ao_mix = nodes.new(type = 'ShaderNodeMixShader')
ao_mix.location = 0, 0
ao_dark = nodes.new(type = 'ShaderNodeEmission')
ao_dark.label = 'Dark'
ao_dark.location = -400, 100
ao_dark.width = pbr.width
ao_dark.hide = True
ao_dark.inputs[0].default_value = 0,0,0,0
ao_dark.inputs[1].default_value = 0
links.new(uvm.outputs[2],imgNode.inputs[0])
links.new(imgNode.outputs[0], reroute.inputs[0])
links.new(reroute.outputs[0], ao_mix.inputs[0])
links.new(ao_dark.outputs[0], ao_mix.inputs[1])
links.new(pbr.outputs[0], ao_mix.inputs[2])
links.new(ao_mix.outputs[0], out.inputs[0])
pass_available = True
if bake_map.type == 'Glossy':
imgNode = nodes.new(type = 'ShaderNodeTexImage')
imgNode.hide = True
imgNode.location = -1000, -200
imgNode.image = bake_image
links.new(imgNode.outputs[0],pbr.inputs[5])
links.new(uvm.outputs[2],imgNode.inputs[0])
pass_available = True
if bake_map.type == 'Roughness':
imgNode = nodes.new(type = 'ShaderNodeTexImage')
imgNode.hide = True
imgNode.location = -1000, -250
imgNode.image = bake_image
links.new(imgNode.outputs[0],pbr.inputs[7])
links.new(uvm.outputs[2],imgNode.inputs[0])
pass_available = True
if bake_map.type == 'Transmission':
imgNode = nodes.new(type = 'ShaderNodeTexImage')
imgNode.hide = True
imgNode.location = -1000, -900
imgNode.image = bake_image
links.new(imgNode.outputs[0],pbr.inputs[15])
links.new(uvm.outputs[2],imgNode.inputs[0])
pass_available = True
###### Custom Passes{
if bake_map.type == 'CustomPass':
####### Find Pass Input Socket{
split_passes = bake_map.pass_name.split(',')
for i in range(len(split_passes)):
split_passes[i] = split_passes[i].strip().casefold()
pass_input = None
for Pass in split_passes:
for tmp_input in pbr.inputs:
if tmp_input.name.casefold() == Pass:
pass_input = tmp_input
break
# }
if pass_input:
if len(pass_input.links) == 0:
imgNode = nodes.new(type = 'ShaderNodeTexImage')
imgNode.hide = True
imgNode.location = -1400,node_y_shift
imgNode.image = bake_image
links.new(imgNode.outputs[0], pass_input)
links.new(uvm.outputs[2],imgNode.inputs[0])
node_y_shift -= 100
pass_available = True
####### }
###### }
return pass_available
def execute(self, context):
props = context.scene.BakeLabProps
active_obj = context.active_object
selected_objects = context.selected_objects
baked_data = context.scene.BakeLab_Data
materials_created = False
for data in baked_data:
if data == None:
continue
self.baked_types = []
name = context.scene.BakeLabProps.global_image_name
if len(data.obj_list) == 1:
if data.obj_list[0].obj != None:
name = data.obj_list[0].obj.name
if len(data.obj_list) == 0: # Just in case
continue
mat = self.generate_mat(data.map_list, name)
if mat is None:
continue
materials_created = True
for objData in data.obj_list:
obj = objData.obj
if obj == None:
continue
if 'Normal' in self.baked_types:
obj.data.use_auto_smooth = False
if props.apply_only_selected:
if obj not in selected_objects:
continue
SelectObject(obj)
if props.make_single_user:
bpy.ops.object.make_single_user(object=True, obdata=True)
if obj.data.uv_layers.active is not None:
obj.data.uv_layers.active.active_render = True
for slot in obj.material_slots:
slot.material = mat
SelectObjects(active_obj, selected_objects)
if materials_created:
return {'FINISHED'}
else:
self.report(type = {'ERROR'}, message = 'No valid baked images to create materials')
return {'CANCELLED'}
class BakeLab_ApplyAO(Operator):
"""Add ambient occlusion materials"""
bl_idname = "bakelab.apply_ao"
bl_label = "Apply AO"
bl_options = {'REGISTER','UNDO'}
def add_ao(self, bake_image, mat):
nodes = mat.node_tree.nodes
links = mat.node_tree.links
out = None
for node in nodes:
if node.type == 'OUTPUT_MATERIAL':
out = node
break
if out == None:
return
if len(out.inputs) == 0:
return
if len(out.inputs[0].links) == 0:
return
bsdf = out.inputs[0].links[0].from_node
uvm = nodes.new(type = 'ShaderNodeTexCoord')
uvm.hide = True
uvm.width = bsdf.width
uvm.location = bsdf.location[0], bsdf.location[1]+160
imgNode = nodes.new(type = 'ShaderNodeTexImage')
imgNode.hide = True
imgNode.width = bsdf.width
imgNode.location = bsdf.location[0], bsdf.location[1]+100
imgNode.image = bake_image
ao_mix = nodes.new(type = 'ShaderNodeMixShader')
ao_mix.location = out.location[:]
out.location[0] += 200
ao_dark = nodes.new(type = 'ShaderNodeEmission')
ao_dark.label = 'Dark'
ao_dark.location = bsdf.location[0], bsdf.location[1]+50
ao_dark.width = bsdf.width
ao_dark.hide = True
ao_dark.inputs[0].default_value = 0,0,0,0
ao_dark.inputs[1].default_value = 0
links.new(uvm.outputs[2],imgNode.inputs[0])
links.new(imgNode.outputs[0], ao_mix.inputs[0])
links.new(ao_dark.outputs[0], ao_mix.inputs[1])
links.new(bsdf.outputs[0], ao_mix.inputs[2])
links.new(ao_mix.outputs[0], out.inputs[0])
def execute(self, context):
props = context.scene.BakeLabProps
active_obj = context.active_object
selected_objects = context.selected_objects
baked_data = context.scene.BakeLab_Data
materials_modified = False
for data in baked_data:
if data == None:
continue
for mapData in data.map_list:
if mapData.bake_map == None:
continue
if mapData.bake_map.type != 'AO':
continue
for objData in data.obj_list:
obj = objData.obj
if obj == None:
continue
if props.apply_only_selected:
if obj not in selected_objects:
continue
SelectObject(obj)
if props.make_single_user:
bpy.ops.object.make_single_user(object=True, obdata=True)
if obj.data.uv_layers.active is not None:
obj.data.uv_layers.active.active_render = True
if len(obj.material_slots) == 0:
bpy.ops.object.material_slot_add()
for slot in obj.material_slots:
if slot.material == None:
slot.material = bpy.data.materials.new(obj.name+'_AO')
slot.material.use_nodes = True
if slot.material.users > 1:
mat_name = slot.material.name
slot.material = slot.material.copy()
slot.material.name = mat_name + '_' + obj.name + '_AO'
self.add_ao(mapData.image, slot.material)
materials_modified = True
break
SelectObjects(active_obj, selected_objects)
if materials_modified:
return {'FINISHED'}
else:
self.report(type = {'ERROR'}, message = 'No valid baked images or objects to add AO')
return {'CANCELLED'}
class BakeLab_ApplyDisplace(Operator):
"""Apply material displacement as real geometry"""
bl_idname = "bakelab.apply_displace"
bl_label = "Apply Displacement"
bl_options = {'REGISTER','UNDO'}
def add_displacement(self, texture, obj):
mod = obj.modifiers.new(name = 'Displacement', type = 'DISPLACE')
mod.direction = 'RGB_TO_XYZ'
mod.texture_coords = 'UV'
mod.texture = texture
mod.show_in_editmode = True
mod.show_on_cage = True
def execute(self, context):
props = context.scene.BakeLabProps
baked_data = context.scene.BakeLab_Data
objects_modified = False
for data in baked_data:
if data == None:
continue
for mapData in data.map_list:
if mapData.bake_map == None:
continue
if mapData.bake_map.type != 'Displacement':
continue
name = context.scene.BakeLabProps.global_image_name
if len(data.obj_list) == 1:
if data.obj_list[0].obj != None:
name = data.obj_list[0].obj.name
tex = bpy.data.textures.new(name = name, type = 'IMAGE')
tex.intensity = 1.5
tex.image = mapData.image
for objData in data.obj_list:
obj = objData.obj
if obj == None:
continue
if props.apply_only_selected:
if not obj.select_get():
continue
self.add_displacement(tex, obj)
objects_modified = True
break
if objects_modified:
return {'FINISHED'}
else:
self.report(type = {'ERROR'}, message = 'No valid baked images or objects to add Displacement')
return {'CANCELLED'}
class BakeLab_Finish(Operator):
"""Finish"""
bl_label = "Finish"
bl_idname = "bakelab.finish"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
props = context.scene.BakeLabProps
context.scene.BakeLab_Data.clear()
props.bake_state = 'NONE'
return {'FINISHED'}