@babylonjs-toolkit/agent 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -21
- package/lib/claude-settings.js +146 -1
- package/lib/doctor.js +21 -4
- package/lib/install.js +67 -4
- package/package.json +1 -1
- package/scripts/hooks/block-sleep.js +138 -0
- package/skills/bt-atlas/SKILL.md +22 -5
- package/skills/bt-design/references/3d-hero-docs.md +10 -3
- package/skills/bt-design/references/3d-hero-scroll.md +30 -5
- package/skills/bt-execute/SKILL.md +89 -120
- package/skills/bt-execute/scripts/dashboard.mjs +279 -19
- package/skills/bt-execute/templates/dashboard/data-schema.md +36 -15
- package/skills/bt-gauntlet/SKILL.md +4 -4
- package/skills/bt-gauntlet/references/spec-composition.md +4 -2
- package/skills/bt-gauntlet/references/target-images.md +28 -4
- package/skills/bt-gauntlet/references/unity-pipeline.md +30 -8
- package/skills/bt-hero/SKILL.md +3 -3
- package/skills/bt-landing/SKILL.md +1 -1
- package/skills/bt-plan/SKILL.md +52 -62
- package/skills/bt-prototype/SKILL.md +3 -3
- package/skills/bt-unwrap/SKILL.md +396 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/bt_unwrap.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/image_utils.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/mesh_tools.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/model_io.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/previews.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/rasterizer.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/reference_views.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/reference_warp.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/texture_bake.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/texture_compose.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/uv_layout.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/bt_unwrap.py +678 -0
- package/skills/bt-unwrap/scripts/blender/image_utils.py +264 -0
- package/skills/bt-unwrap/scripts/blender/mesh_tools.py +508 -0
- package/skills/bt-unwrap/scripts/blender/model_io.py +135 -0
- package/skills/bt-unwrap/scripts/blender/previews.py +205 -0
- package/skills/bt-unwrap/scripts/blender/rasterizer.py +217 -0
- package/skills/bt-unwrap/scripts/blender/reference_views.py +945 -0
- package/skills/bt-unwrap/scripts/blender/reference_warp.py +571 -0
- package/skills/bt-unwrap/scripts/blender/texture_bake.py +390 -0
- package/skills/bt-unwrap/scripts/blender/texture_compose.py +596 -0
- package/skills/bt-unwrap/scripts/blender/uv_layout.py +605 -0
- package/skills/bt-unwrap/scripts/bt-unwrap.mjs +165 -0
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Cycles baking for bt-unwrap: transfer the source model's base color, metallic-roughness and normal detail
|
|
3
|
+
onto the new UV layout, and build the final single-material PBR setup the exporters understand.
|
|
4
|
+
|
|
5
|
+
The source is a copy of the model taken before decimation and before the new layout, so the bake reads the
|
|
6
|
+
full-detail surface with its original textures and writes into the new, clean layout. Normal maps are baked
|
|
7
|
+
in the new layout's own tangent space, which is why they stay correct after re-unwrapping.
|
|
8
|
+
"""
|
|
9
|
+
from contextlib import contextmanager
|
|
10
|
+
|
|
11
|
+
import bpy
|
|
12
|
+
|
|
13
|
+
import image_utils
|
|
14
|
+
import mesh_tools
|
|
15
|
+
import uv_layout
|
|
16
|
+
|
|
17
|
+
BAKE_MATERIAL_NAME = "BtUnwrapBakeTarget"
|
|
18
|
+
GPU_DEVICE_TYPES = ("OPTIX", "CUDA", "HIP", "METAL", "ONEAPI")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def find_principled(material: bpy.types.Material):
|
|
22
|
+
"""
|
|
23
|
+
Finds the Principled BSDF that feeds a material's output.
|
|
24
|
+
|
|
25
|
+
:param material: Any material.
|
|
26
|
+
:returns: The Principled BSDF node, or None.
|
|
27
|
+
"""
|
|
28
|
+
if material is None or not material.use_nodes:
|
|
29
|
+
return None
|
|
30
|
+
return next((node for node in material.node_tree.nodes if node.type == "BSDF_PRINCIPLED"), None)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _linked_source(socket):
|
|
34
|
+
"""The output socket feeding an input socket, or None when it is unlinked."""
|
|
35
|
+
return socket.links[0].from_socket if socket.is_linked else None
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _image_feeds(socket, depth: int = 0) -> bool:
|
|
39
|
+
"""True when an image texture (or vertex colors) feeds this input somewhere upstream — importers often
|
|
40
|
+
wire empty normal-map or bump nodes that carry no data."""
|
|
41
|
+
if depth > 12 or not socket.is_linked:
|
|
42
|
+
return False
|
|
43
|
+
node = socket.links[0].from_node
|
|
44
|
+
if node.type in ("TEX_IMAGE", "VERTEX_COLOR", "ATTRIBUTE"):
|
|
45
|
+
return True
|
|
46
|
+
return any(_image_feeds(upstream, depth + 1) for upstream in node.inputs)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def describe_source_materials(objects: list) -> dict:
|
|
50
|
+
"""
|
|
51
|
+
Reports what the source materials carry, to decide which maps are worth baking.
|
|
52
|
+
|
|
53
|
+
:param objects: Source mesh objects.
|
|
54
|
+
:returns: {has_color_texture, has_vertex_colors, has_metallic_roughness_texture, has_normal_map,
|
|
55
|
+
material_count}.
|
|
56
|
+
"""
|
|
57
|
+
summary = {"has_color_texture": False, "has_vertex_colors": False, "has_metallic_roughness_texture": False,
|
|
58
|
+
"has_normal_map": False, "material_count": 0}
|
|
59
|
+
seen = set()
|
|
60
|
+
for obj in mesh_tools.mesh_objects(objects):
|
|
61
|
+
if obj.data.color_attributes:
|
|
62
|
+
summary["has_vertex_colors"] = True
|
|
63
|
+
for slot in obj.material_slots:
|
|
64
|
+
material = slot.material
|
|
65
|
+
if material is None or material.name in seen:
|
|
66
|
+
continue
|
|
67
|
+
seen.add(material.name)
|
|
68
|
+
principled = find_principled(material)
|
|
69
|
+
if principled is None:
|
|
70
|
+
continue
|
|
71
|
+
summary["has_color_texture"] |= _image_feeds(principled.inputs["Base Color"])
|
|
72
|
+
summary["has_metallic_roughness_texture"] |= (_image_feeds(principled.inputs["Roughness"]) or
|
|
73
|
+
_image_feeds(principled.inputs["Metallic"]))
|
|
74
|
+
summary["has_normal_map"] |= _image_feeds(principled.inputs["Normal"])
|
|
75
|
+
summary["material_count"] = len(seen)
|
|
76
|
+
return summary
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def source_base_color(objects: list) -> tuple:
|
|
80
|
+
"""
|
|
81
|
+
The constant base color of the first source material, used when there is no color data to bake.
|
|
82
|
+
|
|
83
|
+
:param objects: Source mesh objects.
|
|
84
|
+
:returns: RGBA tuple (linear, as Blender stores node colors).
|
|
85
|
+
"""
|
|
86
|
+
for obj in mesh_tools.mesh_objects(objects):
|
|
87
|
+
for slot in obj.material_slots:
|
|
88
|
+
principled = find_principled(slot.material)
|
|
89
|
+
if principled is not None:
|
|
90
|
+
return tuple(principled.inputs["Base Color"].default_value)
|
|
91
|
+
return (0.8, 0.8, 0.8, 1.0)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def ensure_source_materials(objects: list) -> None:
|
|
95
|
+
"""
|
|
96
|
+
Gives material-less source meshes a material, wiring their vertex colors in when they have them
|
|
97
|
+
(PLY / OBJ scans and some generators ship vertex colors instead of textures).
|
|
98
|
+
|
|
99
|
+
:param objects: Source mesh objects.
|
|
100
|
+
"""
|
|
101
|
+
for obj in mesh_tools.mesh_objects(objects):
|
|
102
|
+
if any(slot.material for slot in obj.material_slots):
|
|
103
|
+
continue
|
|
104
|
+
material = bpy.data.materials.new(f"{obj.name}_Source")
|
|
105
|
+
material.use_nodes = True
|
|
106
|
+
principled = find_principled(material)
|
|
107
|
+
color_attribute = obj.data.color_attributes.active_color
|
|
108
|
+
if principled is not None and color_attribute is not None:
|
|
109
|
+
attribute_node = material.node_tree.nodes.new("ShaderNodeVertexColor")
|
|
110
|
+
attribute_node.layer_name = color_attribute.name
|
|
111
|
+
material.node_tree.links.new(attribute_node.outputs["Color"], principled.inputs["Base Color"])
|
|
112
|
+
obj.data.materials.clear()
|
|
113
|
+
obj.data.materials.append(material)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def configure_cycles(samples: int, use_gpu: bool) -> str:
|
|
117
|
+
"""
|
|
118
|
+
Switches the scene to Cycles for baking and picks a device.
|
|
119
|
+
|
|
120
|
+
:param samples: Anti-aliasing samples per texel.
|
|
121
|
+
:param use_gpu: Try a GPU first and fall back to the CPU when none is usable.
|
|
122
|
+
:returns: The device actually used: 'GPU (<type>)' or 'CPU'.
|
|
123
|
+
"""
|
|
124
|
+
scene = bpy.context.scene
|
|
125
|
+
scene.render.engine = "CYCLES"
|
|
126
|
+
scene.cycles.samples = max(1, samples)
|
|
127
|
+
scene.cycles.use_denoising = False
|
|
128
|
+
scene.cycles.device = "CPU"
|
|
129
|
+
if not use_gpu:
|
|
130
|
+
return "CPU"
|
|
131
|
+
preferences = bpy.context.preferences.addons["cycles"].preferences
|
|
132
|
+
for device_type in GPU_DEVICE_TYPES:
|
|
133
|
+
try:
|
|
134
|
+
preferences.compute_device_type = device_type
|
|
135
|
+
preferences.get_devices()
|
|
136
|
+
except TypeError:
|
|
137
|
+
continue
|
|
138
|
+
gpu_devices = [device for device in preferences.devices if device.type == device_type]
|
|
139
|
+
if gpu_devices:
|
|
140
|
+
for device in preferences.devices:
|
|
141
|
+
device.use = device.type == device_type
|
|
142
|
+
scene.cycles.device = "GPU"
|
|
143
|
+
return f"GPU ({device_type})"
|
|
144
|
+
return "CPU"
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _new_bake_image(name: str, size: int, is_color: bool) -> bpy.types.Image:
|
|
148
|
+
"""A blank 8-bit bake target; color maps are sRGB, data maps (normals, roughness) are Non-Color."""
|
|
149
|
+
image = bpy.data.images.new(name, size, size, alpha=False, float_buffer=False)
|
|
150
|
+
image.colorspace_settings.name = "sRGB" if is_color else "Non-Color"
|
|
151
|
+
return image
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _assign_bake_target(target_objects: list, image: bpy.types.Image) -> None:
|
|
155
|
+
"""Gives every target mesh a single material whose active image node receives the bake."""
|
|
156
|
+
material = bpy.data.materials.get(BAKE_MATERIAL_NAME) or bpy.data.materials.new(BAKE_MATERIAL_NAME)
|
|
157
|
+
material.use_nodes = True
|
|
158
|
+
nodes = material.node_tree.nodes
|
|
159
|
+
image_node = next((node for node in nodes if node.type == "TEX_IMAGE"), None) or nodes.new("ShaderNodeTexImage")
|
|
160
|
+
image_node.image = image
|
|
161
|
+
nodes.active = image_node
|
|
162
|
+
for obj in target_objects:
|
|
163
|
+
obj.data.materials.clear()
|
|
164
|
+
obj.data.materials.append(material)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@contextmanager
|
|
168
|
+
def _emission_override(source_objects: list, channel: str):
|
|
169
|
+
"""
|
|
170
|
+
Temporarily routes one material property straight into an emission shader on every source material,
|
|
171
|
+
so an EMIT bake records exactly that property. Restores the original wiring afterwards.
|
|
172
|
+
|
|
173
|
+
:param source_objects: Source meshes.
|
|
174
|
+
:param channel: 'color' (base color) or 'metallic_roughness' (R=1, G=roughness, B=metallic as glTF packs it).
|
|
175
|
+
"""
|
|
176
|
+
restorations = []
|
|
177
|
+
seen = set()
|
|
178
|
+
for obj in mesh_tools.mesh_objects(source_objects):
|
|
179
|
+
for slot in obj.material_slots:
|
|
180
|
+
material = slot.material
|
|
181
|
+
if material is None or material.name in seen or not material.use_nodes:
|
|
182
|
+
continue
|
|
183
|
+
seen.add(material.name)
|
|
184
|
+
tree = material.node_tree
|
|
185
|
+
output = next((node for node in tree.nodes if node.type == "OUTPUT_MATERIAL" and node.is_active_output),
|
|
186
|
+
None) or next((node for node in tree.nodes if node.type == "OUTPUT_MATERIAL"), None)
|
|
187
|
+
if output is None:
|
|
188
|
+
continue
|
|
189
|
+
principled = find_principled(material)
|
|
190
|
+
previous_surface = _linked_source(output.inputs["Surface"])
|
|
191
|
+
emission = tree.nodes.new("ShaderNodeEmission")
|
|
192
|
+
emission.inputs["Strength"].default_value = 1.0
|
|
193
|
+
added_nodes = [emission]
|
|
194
|
+
if channel == "color":
|
|
195
|
+
_wire_color(tree, principled, emission)
|
|
196
|
+
else:
|
|
197
|
+
added_nodes.append(_wire_metallic_roughness(tree, principled, emission))
|
|
198
|
+
tree.links.new(emission.outputs["Emission"], output.inputs["Surface"])
|
|
199
|
+
restorations.append((tree, output, previous_surface, added_nodes))
|
|
200
|
+
try:
|
|
201
|
+
yield
|
|
202
|
+
finally:
|
|
203
|
+
for tree, output, previous_surface, added_nodes in restorations:
|
|
204
|
+
for node in added_nodes:
|
|
205
|
+
tree.nodes.remove(node)
|
|
206
|
+
if previous_surface is not None:
|
|
207
|
+
tree.links.new(previous_surface, output.inputs["Surface"])
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
@contextmanager
|
|
211
|
+
def _without_normal_maps(source_objects: list):
|
|
212
|
+
"""
|
|
213
|
+
Temporarily unplugs the normal maps of every source material, so a normal bake records the surface's
|
|
214
|
+
real shape only. Generated normal maps are mostly relief guessed from their own texture (embossed logos
|
|
215
|
+
where the generator painted them, noise on smooth panels) and would fight the repainted texture.
|
|
216
|
+
"""
|
|
217
|
+
unplugged = []
|
|
218
|
+
seen = set()
|
|
219
|
+
for obj in mesh_tools.mesh_objects(source_objects):
|
|
220
|
+
for slot in obj.material_slots:
|
|
221
|
+
material = slot.material
|
|
222
|
+
if material is None or material.name in seen or not material.use_nodes:
|
|
223
|
+
continue
|
|
224
|
+
seen.add(material.name)
|
|
225
|
+
principled = find_principled(material)
|
|
226
|
+
if principled is None or not principled.inputs["Normal"].is_linked:
|
|
227
|
+
continue
|
|
228
|
+
link = principled.inputs["Normal"].links[0]
|
|
229
|
+
unplugged.append((material.node_tree, link.from_socket, principled.inputs["Normal"]))
|
|
230
|
+
material.node_tree.links.remove(link)
|
|
231
|
+
try:
|
|
232
|
+
yield
|
|
233
|
+
finally:
|
|
234
|
+
for tree, from_socket, to_socket in unplugged:
|
|
235
|
+
tree.links.new(from_socket, to_socket)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _wire_color(tree, principled, emission) -> None:
|
|
239
|
+
"""Feeds the base color (texture or constant) into the emission shader."""
|
|
240
|
+
if principled is None:
|
|
241
|
+
emission.inputs["Color"].default_value = (0.8, 0.8, 0.8, 1.0)
|
|
242
|
+
return
|
|
243
|
+
source = _linked_source(principled.inputs["Base Color"])
|
|
244
|
+
if source is not None:
|
|
245
|
+
tree.links.new(source, emission.inputs["Color"])
|
|
246
|
+
else:
|
|
247
|
+
emission.inputs["Color"].default_value = principled.inputs["Base Color"].default_value
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _wire_metallic_roughness(tree, principled, emission):
|
|
251
|
+
"""Packs roughness into green and metallic into blue (glTF layout) and feeds it to the emission shader."""
|
|
252
|
+
combine = tree.nodes.new("ShaderNodeCombineColor")
|
|
253
|
+
combine.inputs["Red"].default_value = 1.0
|
|
254
|
+
for input_name, channel_name in (("Roughness", "Green"), ("Metallic", "Blue")):
|
|
255
|
+
if principled is None:
|
|
256
|
+
combine.inputs[channel_name].default_value = 0.5 if input_name == "Roughness" else 0.0
|
|
257
|
+
continue
|
|
258
|
+
source = _linked_source(principled.inputs[input_name])
|
|
259
|
+
if source is not None:
|
|
260
|
+
tree.links.new(source, combine.inputs[channel_name])
|
|
261
|
+
else:
|
|
262
|
+
combine.inputs[channel_name].default_value = principled.inputs[input_name].default_value
|
|
263
|
+
tree.links.new(combine.outputs["Color"], emission.inputs["Color"])
|
|
264
|
+
return combine
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def _run_bake(target_objects: list, source_objects: list, bake_type: str, distance: float, padding: int) -> None:
|
|
268
|
+
"""Runs one selected-to-active Cycles bake from the sources onto the targets' new UV layout."""
|
|
269
|
+
bpy.ops.object.select_all(action="DESELECT")
|
|
270
|
+
for obj in source_objects + target_objects:
|
|
271
|
+
obj.select_set(True)
|
|
272
|
+
bpy.context.view_layer.objects.active = target_objects[0]
|
|
273
|
+
keywords = {
|
|
274
|
+
"type": bake_type,
|
|
275
|
+
"use_selected_to_active": True,
|
|
276
|
+
"cage_extrusion": distance,
|
|
277
|
+
"max_ray_distance": distance * 2.0,
|
|
278
|
+
"margin": padding,
|
|
279
|
+
"margin_type": "EXTEND",
|
|
280
|
+
"use_clear": True,
|
|
281
|
+
"target": "IMAGE_TEXTURES",
|
|
282
|
+
"uv_layer": uv_layout.NEW_UV_LAYER,
|
|
283
|
+
}
|
|
284
|
+
if bake_type == "NORMAL":
|
|
285
|
+
keywords["normal_space"] = "TANGENT"
|
|
286
|
+
result = bpy.ops.object.bake(**keywords)
|
|
287
|
+
if "FINISHED" not in result:
|
|
288
|
+
raise RuntimeError(f"Cycles {bake_type} bake failed: {result}")
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def bake_source_maps(target_objects: list, source_objects: list, size: int, distance: float, padding: int,
|
|
292
|
+
maps: tuple, keep_source_normal_map: bool = False) -> dict:
|
|
293
|
+
"""
|
|
294
|
+
Bakes the requested maps from the source copy onto the targets' new layout.
|
|
295
|
+
|
|
296
|
+
:param target_objects: Meshes with the new UV layer (their materials are replaced).
|
|
297
|
+
:param source_objects: Full-detail copies with the original UVs and materials.
|
|
298
|
+
:param size: Texture size in pixels (square).
|
|
299
|
+
:param distance: Cage extrusion in meters; rays search twice this far for the source surface.
|
|
300
|
+
:param padding: Pixels of edge padding around islands.
|
|
301
|
+
:param maps: Any of 'color', 'metallic_roughness', 'normal'.
|
|
302
|
+
:param keep_source_normal_map: Bake the source's own normal map into the result too. Off by default: the
|
|
303
|
+
normal bake then records only the source's geometric detail.
|
|
304
|
+
:returns: map name -> float32 array (size, size, 4), row 0 at the bottom.
|
|
305
|
+
"""
|
|
306
|
+
results = {}
|
|
307
|
+
for map_name in maps:
|
|
308
|
+
image = _new_bake_image(f"bake_{map_name}", size, is_color=map_name == "color")
|
|
309
|
+
_assign_bake_target(target_objects, image)
|
|
310
|
+
if map_name == "normal" and keep_source_normal_map:
|
|
311
|
+
_run_bake(target_objects, source_objects, "NORMAL", distance, padding)
|
|
312
|
+
elif map_name == "normal":
|
|
313
|
+
with _without_normal_maps(source_objects):
|
|
314
|
+
_run_bake(target_objects, source_objects, "NORMAL", distance, padding)
|
|
315
|
+
else:
|
|
316
|
+
with _emission_override(source_objects, map_name):
|
|
317
|
+
_run_bake(target_objects, source_objects, "EMIT", distance, padding)
|
|
318
|
+
results[map_name] = image_utils.read_blender_image(image)
|
|
319
|
+
bpy.data.images.remove(image)
|
|
320
|
+
return results
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def build_final_material(target_objects: list, name: str, texture_paths: dict, roughness, metallic,
|
|
324
|
+
constant_color: tuple = (0.8, 0.8, 0.8, 1.0), double_sided: bool = False) -> bpy.types.Material:
|
|
325
|
+
"""
|
|
326
|
+
Creates the single PBR material the model ships with, arranged the way the glTF exporter recognises:
|
|
327
|
+
base color image -> Base Color; metallic-roughness image -> Separate Color (G roughness, B metallic);
|
|
328
|
+
normal image -> Normal Map node. Replaces every material on the targets.
|
|
329
|
+
|
|
330
|
+
:param target_objects: Final meshes.
|
|
331
|
+
:param name: Material name.
|
|
332
|
+
:param texture_paths: Saved texture files: any of 'color', 'metallic_roughness', 'normal'.
|
|
333
|
+
:param roughness: Constant roughness to use instead of a map, or None.
|
|
334
|
+
:param metallic: Constant metallic to use instead of a map, or None.
|
|
335
|
+
:param constant_color: Base color (linear RGBA) used when there is no color texture.
|
|
336
|
+
:param double_sided: Render back faces too. Off by default: a closed model never shows them, and culling
|
|
337
|
+
them halves the shading work (glTF doubleSided follows this).
|
|
338
|
+
:returns: The material.
|
|
339
|
+
"""
|
|
340
|
+
material = bpy.data.materials.new(name)
|
|
341
|
+
material.use_nodes = True
|
|
342
|
+
material.use_backface_culling = not double_sided
|
|
343
|
+
tree = material.node_tree
|
|
344
|
+
principled = find_principled(material)
|
|
345
|
+
principled.inputs["Base Color"].default_value = constant_color
|
|
346
|
+
|
|
347
|
+
color_node = None
|
|
348
|
+
if "color" in texture_paths:
|
|
349
|
+
color_node = _image_node(tree, texture_paths["color"], is_color=True, location=(-600, 300))
|
|
350
|
+
tree.links.new(color_node.outputs["Color"], principled.inputs["Base Color"])
|
|
351
|
+
if "metallic_roughness" in texture_paths and (roughness is None or metallic is None):
|
|
352
|
+
packed_node = _image_node(tree, texture_paths["metallic_roughness"], is_color=False, location=(-600, 0))
|
|
353
|
+
separate = tree.nodes.new("ShaderNodeSeparateColor")
|
|
354
|
+
separate.location = (-300, 0)
|
|
355
|
+
tree.links.new(packed_node.outputs["Color"], separate.inputs["Color"])
|
|
356
|
+
if roughness is None:
|
|
357
|
+
tree.links.new(separate.outputs["Green"], principled.inputs["Roughness"])
|
|
358
|
+
if metallic is None:
|
|
359
|
+
tree.links.new(separate.outputs["Blue"], principled.inputs["Metallic"])
|
|
360
|
+
if roughness is not None:
|
|
361
|
+
principled.inputs["Roughness"].default_value = roughness
|
|
362
|
+
if metallic is not None:
|
|
363
|
+
principled.inputs["Metallic"].default_value = metallic
|
|
364
|
+
if "normal" in texture_paths:
|
|
365
|
+
normal_image_node = _image_node(tree, texture_paths["normal"], is_color=False, location=(-600, -300))
|
|
366
|
+
normal_map = tree.nodes.new("ShaderNodeNormalMap")
|
|
367
|
+
normal_map.location = (-300, -300)
|
|
368
|
+
normal_map.uv_map = uv_layout.FINAL_UV_LAYER
|
|
369
|
+
tree.links.new(normal_image_node.outputs["Color"], normal_map.inputs["Color"])
|
|
370
|
+
tree.links.new(normal_map.outputs["Normal"], principled.inputs["Normal"])
|
|
371
|
+
if color_node is not None:
|
|
372
|
+
# Workbench previews show the active image node.
|
|
373
|
+
tree.nodes.active = color_node
|
|
374
|
+
|
|
375
|
+
for obj in mesh_tools.mesh_objects(target_objects):
|
|
376
|
+
obj.data.materials.clear()
|
|
377
|
+
obj.data.materials.append(material)
|
|
378
|
+
leftover = bpy.data.materials.get(BAKE_MATERIAL_NAME)
|
|
379
|
+
if leftover is not None and leftover.users == 0:
|
|
380
|
+
bpy.data.materials.remove(leftover)
|
|
381
|
+
return material
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def _image_node(tree, path: str, is_color: bool, location: tuple):
|
|
385
|
+
"""Adds an image texture node for a saved texture file."""
|
|
386
|
+
node = tree.nodes.new("ShaderNodeTexImage")
|
|
387
|
+
node.image = bpy.data.images.load(path, check_existing=False)
|
|
388
|
+
node.image.colorspace_settings.name = "sRGB" if is_color else "Non-Color"
|
|
389
|
+
node.location = location
|
|
390
|
+
return node
|