-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasybake.py
More file actions
318 lines (242 loc) · 8.41 KB
/
Copy patheasybake.py
File metadata and controls
318 lines (242 loc) · 8.41 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
bl_info = {
"name": "Procedural Texture Baker",
"author": "Friedrich Eibl",
"version": (0, 1, 0),
"blender": (4, 0, 0),
"location": "View3D > Sidebar > Bake",
"description": "Bake procedural materials to diffuse, roughness, and normal textures",
"category": "Material",
}
import bpy
import os
def ensure_dir(path):
path = bpy.path.abspath(path)
os.makedirs(path, exist_ok=True)
return path
def create_image(name, width, height, alpha=True):
img = bpy.data.images.new(name, width=width, height=height, alpha=alpha)
img.generated_color = (0, 0, 0, 1)
return img
def get_or_create_image_node(material, image):
material.use_nodes = True
nodes = material.node_tree.nodes
node = nodes.new("ShaderNodeTexImage")
node.name = "__BAKE_TARGET__"
node.label = "Bake Target"
node.image = image
nodes.active = node
node.select = True
return node
def remove_node(material, node):
if material and material.use_nodes and node:
material.node_tree.nodes.remove(node)
class PTB_Settings(bpy.types.PropertyGroup):
target_object: bpy.props.PointerProperty(
name="Object",
type=bpy.types.Object,
description="Mesh object to bake"
)
resolution: bpy.props.EnumProperty(
name="Resolution",
items=[
("512", "512", ""),
("1024", "1024", ""),
("2048", "2048", ""),
("4096", "4096", ""),
("8192", "8192", ""),
],
default="2048"
)
samples: bpy.props.IntProperty(
name="Samples",
default=64,
min=1,
max=4096
)
margin: bpy.props.IntProperty(
name="Margin",
default=16,
min=0,
max=128
)
output_dir: bpy.props.StringProperty(
name="Output Folder",
subtype="DIR_PATH",
default="//baked_textures/"
)
file_format: bpy.props.EnumProperty(
name="Format",
items=[
("PNG", "PNG", ""),
("JPEG", "JPEG", ""),
("TIFF", "TIFF", ""),
("OPEN_EXR", "OpenEXR", ""),
],
default="PNG"
)
bake_diffuse: bpy.props.BoolProperty(
name="Diffuse / Base Color",
default=True
)
bake_roughness: bpy.props.BoolProperty(
name="Roughness",
default=True
)
bake_normal: bpy.props.BoolProperty(
name="Normal",
default=True
)
create_baked_material: bpy.props.BoolProperty(
name="Create Baked Material",
default=True
)
class PTB_OT_bake(bpy.types.Operator):
bl_idname = "ptb.bake"
bl_label = "Bake Textures"
bl_options = {"REGISTER", "UNDO"}
def bake_pass(self, obj, image_name, bake_type, settings):
output_dir = ensure_dir(settings.output_dir)
resolution = int(settings.resolution)
image = create_image(image_name, resolution, resolution)
image.file_format = settings.file_format
ext = {
"PNG": "png",
"JPEG": "jpg",
"TIFF": "tif",
"OPEN_EXR": "exr",
}[settings.file_format]
image.filepath_raw = os.path.join(output_dir, f"{image_name}.{ext}")
bake_nodes = []
for slot in obj.material_slots:
mat = slot.material
if not mat:
continue
node = get_or_create_image_node(mat, image)
bake_nodes.append((mat, node))
bpy.ops.object.select_all(action="DESELECT")
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
bpy.context.scene.render.engine = "CYCLES"
bpy.context.scene.cycles.samples = settings.samples
bpy.context.scene.render.bake.margin = settings.margin
bpy.context.scene.render.bake.target = "IMAGE_TEXTURES"
if bake_type == "DIFFUSE":
bpy.context.scene.render.bake.use_pass_direct = False
bpy.context.scene.render.bake.use_pass_indirect = False
bpy.context.scene.render.bake.use_pass_color = True
bpy.ops.object.bake(type=bake_type)
image.save()
for mat, node in bake_nodes:
remove_node(mat, node)
return image
def create_baked_material(self, obj, images):
mat = bpy.data.materials.new(f"{obj.name}_Baked_Material")
mat.use_nodes = True
nodes = mat.node_tree.nodes
links = mat.node_tree.links
bsdf = nodes.get("Principled BSDF")
if "diffuse" in images:
tex = nodes.new("ShaderNodeTexImage")
tex.image = images["diffuse"]
tex.label = "Baked Diffuse"
links.new(tex.outputs["Color"], bsdf.inputs["Base Color"])
if "roughness" in images:
tex = nodes.new("ShaderNodeTexImage")
tex.image = images["roughness"]
tex.label = "Baked Roughness"
tex.image.colorspace_settings.name = "Non-Color"
links.new(tex.outputs["Color"], bsdf.inputs["Roughness"])
if "normal" in images:
tex = nodes.new("ShaderNodeTexImage")
tex.image = images["normal"]
tex.label = "Baked Normal"
tex.image.colorspace_settings.name = "Non-Color"
normal_map = nodes.new("ShaderNodeNormalMap")
links.new(tex.outputs["Color"], normal_map.inputs["Color"])
links.new(normal_map.outputs["Normal"], bsdf.inputs["Normal"])
obj.data.materials.clear()
obj.data.materials.append(mat)
def execute(self, context):
settings = context.scene.ptb_settings
obj = settings.target_object or context.object
if not obj:
self.report({"ERROR"}, "No object selected")
return {"CANCELLED"}
if obj.type != "MESH":
self.report({"ERROR"}, "Object must be a mesh")
return {"CANCELLED"}
if not obj.data.uv_layers:
self.report({"ERROR"}, "Object has no UV map")
return {"CANCELLED"}
if not obj.material_slots:
self.report({"ERROR"}, "Object has no materials")
return {"CANCELLED"}
baked_images = {}
try:
if settings.bake_diffuse:
baked_images["diffuse"] = self.bake_pass(
obj,
f"{obj.name}_Diffuse",
"DIFFUSE",
settings
)
if settings.bake_roughness:
baked_images["roughness"] = self.bake_pass(
obj,
f"{obj.name}_Roughness",
"ROUGHNESS",
settings
)
if settings.bake_normal:
baked_images["normal"] = self.bake_pass(
obj,
f"{obj.name}_Normal",
"NORMAL",
settings
)
if settings.create_baked_material:
self.create_baked_material(obj, baked_images)
except Exception as e:
self.report({"ERROR"}, str(e))
return {"CANCELLED"}
self.report({"INFO"}, "Bake complete")
return {"FINISHED"}
class PTB_PT_panel(bpy.types.Panel):
bl_label = "Procedural Texture Baker"
bl_idname = "PTB_PT_panel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Bake"
def draw(self, context):
layout = self.layout
settings = context.scene.ptb_settings
layout.prop(settings, "target_object")
layout.prop(settings, "resolution")
layout.prop(settings, "samples")
layout.prop(settings, "margin")
layout.prop(settings, "output_dir")
layout.prop(settings, "file_format")
layout.separator()
layout.label(text="Maps")
layout.prop(settings, "bake_diffuse")
layout.prop(settings, "bake_roughness")
layout.prop(settings, "bake_normal")
layout.separator()
layout.prop(settings, "create_baked_material")
layout.separator()
layout.operator("ptb.bake", icon="RENDER_STILL")
classes = (
PTB_Settings,
PTB_OT_bake,
PTB_PT_panel,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.ptb_settings = bpy.props.PointerProperty(type=PTB_Settings)
def unregister():
del bpy.types.Scene.ptb_settings
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()