Skip to content

Transform your 3D texturing workflow with the power of generative AI, directly within Blender!

repair existing baked normal maps when a file loads

sakalondda378a926fee
diff --git a/stablegen/core/load_handlers.py b/stablegen/core/load_handlers.py
index 7e4c7b7..0e5ec62 100644
--- a/stablegen/core/load_handlers.py
+++ b/stablegen/core/load_handlers.py
@@ -40,6 +40,11 @@ def load_handler(dummy, _second=None, file_loaded=True):
if '_sg_mask_run_timestamp' in each:
each.output_timestamp = each['_sg_mask_run_timestamp']
del each['_sg_mask_run_timestamp']
+ # Bakes made before their normal maps named a UV map showed a triangle mosaic.
+ from ..texturing.rendering import repair_baked_normal_maps
+ repaired = repair_baked_normal_maps(bpy.data.materials)
+ if repaired:
+ print(f"[StableGen] Baked normal maps now decode with their bake UV map: {repaired}")
# Builds saved while Qwen-Image 2.1 shared the Qwen-Image-Edit enum need a
# one-time architecture migration. The filename is used only here for
diff --git a/stablegen/texturing/rendering.py b/stablegen/texturing/rendering.py
index d9e918b..a9235eb 100644
--- a/stablegen/texturing/rendering.py
+++ b/stablegen/texturing/rendering.py
@@ -2842,6 +2842,28 @@ _PBR_CHANNEL_SUFFIXES = {
}
+def repair_baked_normal_maps(materials):
+ """Give the tangent Normal Map nodes of baked materials made before they named a UV map the UV map their
+ normal texture is sampled with. They decoded in the render-active UV map's tangents, a triangle mosaic on
+ meshes whose own UVs differ from the bake's. Returns the number of nodes repaired."""
+ repaired = 0
+ for mat in materials:
+ if not mat or not mat.use_nodes or not mat.get('sg_baked'):
+ continue
+ for nmap in mat.node_tree.nodes:
+ if nmap.type != 'NORMAL_MAP' or nmap.space != 'TANGENT' or nmap.uv_map:
+ continue
+ color = nmap.inputs['Color']
+ texture = color.links[0].from_node if color.links else None
+ if texture is None or texture.type != 'TEX_IMAGE' or not texture.inputs['Vector'].links:
+ continue
+ uv = texture.inputs['Vector'].links[0].from_node
+ if uv.type == 'UVMAP' and uv.uv_map:
+ nmap.uv_map = uv.uv_map
+ repaired += 1
+ return repaired
+
+
def _keep_earlier_bake(path, keep=None):
"""Give each image a material still reads from *path* its own copy before a new bake replaces that file.