Skip to content

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

complete confidence-owned texturing pipeline rework

sakalondf984c773cb1d
diff --git a/stablegen/core/properties.py b/stablegen/core/properties.py
index 623e470..9a66e56 100644
--- a/stablegen/core/properties.py
+++ b/stablegen/core/properties.py
@@ -789,8 +789,8 @@ def register_properties(update_model_list, ControlNetUnit, LoRAUnit,
)
bpy.types.Scene.sg_silhouette_depth = bpy.props.FloatProperty(
name="Silhouette Depth",
- description="Minimum depth difference (Blender units) between the vertex and an offset ray hit to consider it a silhouette edge. Raise if too much is eroded, lower if outlines remain",
- default=0.05, min=0.001, max=1.0, step=1, precision=3, update=update_parameters,
+ description="Minimum depth discontinuity as a fraction of model scale; pixel footprint is also considered so protection behaves consistently on tiny and huge characters",
+ default=0.01, min=0.0001, max=0.25, step=1, precision=4, update=update_parameters,
)
bpy.types.Scene.sg_silhouette_rays = bpy.props.EnumProperty(
name="Silhouette Rays",
@@ -1135,6 +1135,36 @@ def register_properties(update_model_list, ControlNetUnit, LoRAUnit,
description="Dilate fallback color regions in the context render before sending to Qwen.",
default=1, min=0, max=64, update=update_parameters
)
+ bpy.types.Scene.qwen_trust_threshold = bpy.props.FloatProperty(
+ name="Trusted Source Threshold",
+ description="Minimum original source-camera acquisition quality required before RGB is protected in later Qwen context views",
+ default=0.70, min=0.0, max=1.0, update=update_parameters,
+ )
+ bpy.types.Scene.qwen_mask_min_island_area = bpy.props.IntProperty(
+ name="Minimum Trusted Island",
+ description="Trusted islands smaller than this pixel area become repair; 0 disables",
+ default=16, min=0, max=65536, update=update_parameters,
+ )
+ bpy.types.Scene.qwen_mask_min_hole_area = bpy.props.IntProperty(
+ name="Minimum Repair Hole",
+ description="Repair holes smaller than this pixel area are filled back to trusted; 0 disables",
+ default=8, min=0, max=65536, update=update_parameters,
+ )
+ bpy.types.Scene.qwen_mask_min_strip_width = bpy.props.IntProperty(
+ name="Minimum Context Strip",
+ description="Remove thin stretched trusted strips narrower than this many pixels",
+ default=2, min=0, max=64, update=update_parameters,
+ )
+ bpy.types.Scene.qwen_mask_uncertainty_dilation = bpy.props.IntProperty(
+ name="Repair Uncertainty Dilation",
+ description="Expand repair into uncertain boundary pixels while respecting object and material labels",
+ default=1, min=0, max=64, update=update_parameters,
+ )
+ bpy.types.Scene.qwen_mask_keep_largest_components = bpy.props.IntProperty(
+ name="Keep Largest Repair Components",
+ description="Keep only this many largest repair components for extremely fragmented masks; 0 keeps all",
+ default=0, min=0, max=64, update=update_parameters,
+ )
bpy.types.Scene.qwen_use_projection_write_masks = bpy.props.BoolProperty(
name="Protect Trusted Texture",
description="Use the binary sequential visibility mask both to build exact magenta context and to restrict which pixels a later Qwen view may project",
@@ -1228,12 +1258,12 @@ def register_properties(update_model_list, ControlNetUnit, LoRAUnit,
)
bpy.types.Scene.early_priority_strength = bpy.props.FloatProperty(
name="Priority Strength",
- description="Strength of the bounded global camera-rank prior. At maximum strength the final view receives one quarter of the first view's prior before confidence normalization",
+ description="Extra replacement resistance applied only to reliable early-view owners; it never promotes grazing coverage to trusted",
default=0.5, min=0.0, max=1.0, update=update_parameters
)
bpy.types.Scene.early_priority = bpy.props.BoolProperty(
name="Prioritize Initial Views",
- description="Apply a predictable global rank prior so earlier cameras win close confidence decisions. Put detail-critical cameras first",
+ description="Anchor already-reliable early-view detail by raising its replacement margin. Poorly observed side texels remain untrusted and replaceable",
default=False, update=update_parameters
)
bpy.types.Scene.sg_texel_ownership = bpy.props.BoolProperty(
@@ -1246,6 +1276,57 @@ def register_properties(update_model_list, ControlNetUnit, LoRAUnit,
description="Extra confidence a later camera needs before replacing the current per-texel owner",
default=0.05, min=0.0, max=1.0, update=update_parameters
)
+ bpy.types.Scene.sg_allow_trusted_replacement = bpy.props.BoolProperty(
+ name="Allow Trusted Quality Upgrades",
+ description="Permit a later camera to replace trusted detail only when its source quality exceeds the owner by the replacement margin",
+ default=False, update=update_parameters,
+ )
+ bpy.types.Scene.sg_ownership_blend_mode = bpy.props.EnumProperty(
+ name="Ownership Detail Fusion",
+ description="Choose how camera detail is fused after confidence resolution",
+ items=[
+ ('OWNER', "Single Detail Owner", "Keep high-frequency detail from one owner; blend only low-frequency seam colour"),
+ ('HARD', "Hard Owner", "Keep one owner with no seam colour matching"),
+ ('LEGACY', "Legacy Weighted Blend", "Use the previous weighted camera averaging behavior"),
+ ],
+ default='OWNER', update=update_parameters,
+ )
+ bpy.types.Scene.sg_ownership_seam_width = bpy.props.FloatProperty(
+ name="Ownership Seam Width",
+ description="Quality-distance band where only low-frequency colour is blended between detail owners",
+ default=0.08, min=0.0, max=0.5, update=update_parameters,
+ )
+ bpy.types.Scene.sg_ownership_lowpass_sigma = bpy.props.FloatProperty(
+ name="Ownership Low-Pass Radius",
+ description="Image-space Gaussian radius used for seam colour matching; high-frequency detail remains owned by one camera",
+ default=8.0, min=0.1, max=64.0, update=update_parameters,
+ )
+ bpy.types.Scene.sg_auto_view_sequence = bpy.props.BoolProperty(
+ name="Overlap-Aware View Sequence",
+ description="Order non-custom camera plans through nearby bridge views to reduce angular jumps and context discontinuities",
+ default=False, update=update_parameters,
+ )
+ bpy.types.Scene.sg_anchor_camera = bpy.props.StringProperty(
+ name="Anchor Camera",
+ description="Camera name used as the first view by overlap-aware sequencing; empty uses the active scene camera",
+ default="", update=update_parameters,
+ )
+ bpy.types.Scene.sg_texturing_frame = bpy.props.IntProperty(
+ name="Texturing Frame",
+ description="Explicit frame used consistently for evaluated character geometry, visibility, projection, and baking",
+ default=1, update=update_parameters,
+ )
+ bpy.types.Scene.sg_freeze_ownership_after_generation = bpy.props.BoolProperty(
+ name="Freeze Ownership to UV Texture",
+ description="Bake resolved camera ownership to UV textures after generation so pose, camera, and scene visibility changes cannot alter the final texture",
+ default=False, update=update_parameters,
+ )
+ bpy.types.Scene.sg_material_discard_factor = bpy.props.FloatProperty(
+ name="Material Discard Angle", default=90.0, options={'HIDDEN'}
+ )
+ bpy.types.Scene.sg_material_weight_exponent = bpy.props.FloatProperty(
+ name="Material Weight Exponent", default=1.0, options={'HIDDEN'}
+ )
bpy.types.Scene.texture_objects = bpy.props.EnumProperty(
name="Objects to Texture", description="Select the objects to texture",
items=[
@@ -1855,6 +1936,11 @@ def unregister_properties(load_handler, _sg_queue_load_handler):
'sg_silhouette_margin', 'sg_silhouette_depth', 'sg_silhouette_rays',
'sg_allow_backface_projection',
'sg_texel_ownership', 'sg_texel_replacement_margin',
+ 'sg_allow_trusted_replacement', 'sg_ownership_blend_mode',
+ 'sg_ownership_seam_width', 'sg_ownership_lowpass_sigma',
+ 'sg_auto_view_sequence', 'sg_anchor_camera', 'sg_texturing_frame',
+ 'sg_freeze_ownership_after_generation', 'sg_material_discard_factor',
+ 'sg_material_weight_exponent',
'refine_angle_ramp_active', 'refine_angle_ramp_pos_0', 'refine_angle_ramp_pos_1',
'refine_feather_ramp_pos_0', 'refine_feather_ramp_pos_1',
'refine_edge_feather_projection', 'refine_edge_feather_width', 'refine_edge_feather_softness',
@@ -1887,6 +1973,10 @@ def unregister_properties(load_handler, _sg_queue_load_handler):
'qwen_context_cleanup', 'qwen_context_cleanup_hue_tolerance',
'qwen_context_cleanup_value_adjust', 'qwen_context_fallback_dilation',
'qwen_use_projection_write_masks', 'qwen_residual_fallback_tolerance',
+ 'qwen_trust_threshold', 'qwen_mask_min_island_area',
+ 'qwen_mask_min_hole_area', 'qwen_mask_min_strip_width',
+ 'qwen_mask_uncertainty_dilation',
+ 'qwen_mask_keep_largest_components',
'qwen_prompt_gray_background',
]
for prop in _simple_scene_props:
diff --git a/stablegen/debug_tools.py b/stablegen/debug_tools.py
index ccbab41..0e2c089 100644
--- a/stablegen/debug_tools.py
+++ b/stablegen/debug_tools.py
@@ -829,7 +829,159 @@ class SG_OT_DebugRestoreMaterial(bpy.types.Operator):
# ═════════════════════════════════════════════════════════════════════════
-# 9. Debug Make Solid
+# 9. Live Ownership / Confidence Diagnostics
+# ═════════════════════════════════════════════════════════════════════════
+
+class SG_OT_DebugOwnershipDiagnostic(bpy.types.Operator):
+ """Visualize values from the actual projection ownership graph.
+
+ The material is copied before its output is replaced, so the diagnostic
+ cannot alter the production graph. Undo restores the original slots.
+ """
+ bl_idname = "stablegen.debug_ownership_diagnostic"
+ bl_label = "Ownership Diagnostic"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ mode: bpy.props.EnumProperty(
+ name="Diagnostic",
+ items=[
+ ('OWNER', "Owner Camera", "Hue identifies the selected source camera"),
+ ('QUALITY', "Owner Quality", "Resolved acquisition quality of the owner"),
+ ('ACQUISITION', "Camera Quality", "Raw acquisition quality for the active camera"),
+ ('TRUSTED', "Trusted Context", "White where owner quality reaches the trust threshold"),
+ ('REPAIR', "Repair Mask", "White where the current view must regenerate context"),
+ ('WRITE', "Write Mask", "Authoritative generated-pixel write mask for the active camera"),
+ ('GUARD', "Depth/Part Guard", "Rear-surface rejection mask for the active camera"),
+ ('FALLBACK', "Fallback Only", "White where no trusted owner exists"),
+ ],
+ default='QUALITY',
+ ) # type: ignore
+
+ @classmethod
+ def poll(cls, context):
+ return _common_poll(cls, context)
+
+ def invoke(self, context, event):
+ return context.window_manager.invoke_props_dialog(self)
+
+ @staticmethod
+ def _find_node(nodes, exact=None, prefix=None):
+ for node in nodes:
+ label = getattr(node, "label", "")
+ if exact is not None and label == exact:
+ return node
+ if prefix is not None and label.startswith(prefix):
+ return node
+ return None
+
+ def execute(self, context):
+ meshes = _get_target_meshes(context)
+ cameras = _get_cameras(context)
+ _cam, camera_index = _get_active_camera(context)
+ material_copies = {}
+ visualized = 0
+
+ for obj in meshes:
+ for slot in obj.material_slots:
+ source_mat = slot.material
+ if not source_mat or not source_mat.use_nodes:
+ continue
+ key = source_mat.as_pointer()
+ mat = material_copies.get(key)
+ if mat is None:
+ mat = source_mat.copy()
+ mat.name = f"SG_Debug_Diagnostic_{source_mat.name}"
+ material_copies[key] = mat
+ nodes = mat.node_tree.nodes
+ links = mat.node_tree.links
+
+ owner_quality = self._find_node(nodes, exact="SGOwnerQuality")
+ if self.mode == 'OWNER':
+ source = self._find_node(nodes, exact="SGOwnerCamera")
+ elif self.mode == 'QUALITY':
+ source = owner_quality
+ elif self.mode == 'ACQUISITION':
+ source = self._find_node(
+ nodes, prefix=f"AcquisitionQuality-{camera_index}-"
+ )
+ elif self.mode in {'TRUSTED', 'REPAIR'}:
+ source = owner_quality
+ elif self.mode == 'WRITE':
+ source = self._find_node(
+ nodes, prefix=f"SequentialWriteWeight-{camera_index}-"
+ )
+ elif self.mode == 'GUARD':
+ source = self._find_node(
+ nodes, prefix=f"ProjectionGuardWeight-{camera_index}-"
+ )
+ else:
+ source = self._find_node(nodes, exact="OwnerUncovered")
+
+ if source is None or not source.outputs:
+ bpy.data.materials.remove(mat)
+ material_copies[key] = None
+ continue
+
+ value_socket = source.outputs[0]
+ if self.mode in {'TRUSTED', 'REPAIR'}:
+ compare = nodes.new("ShaderNodeMath")
+ compare.operation = 'GREATER_THAN'
+ compare.label = "SGDebugTrusted"
+ compare.inputs[1].default_value = getattr(
+ context.scene, 'qwen_trust_threshold', 0.55
+ )
+ links.new(value_socket, compare.inputs[0])
+ value_socket = compare.outputs[0]
+ if self.mode == 'REPAIR':
+ invert = nodes.new("ShaderNodeMath")
+ invert.operation = 'SUBTRACT'
+ invert.inputs[0].default_value = 1.0
+ links.new(value_socket, invert.inputs[1])
+ value_socket = invert.outputs[0]
+
+ ramp = nodes.new("ShaderNodeValToRGB")
+ ramp.label = f"SGDebug-{self.mode}"
+ if self.mode == 'OWNER':
+ normalize = nodes.new("ShaderNodeMath")
+ normalize.operation = 'DIVIDE'
+ normalize.inputs[1].default_value = max(len(cameras) - 1, 1)
+ links.new(value_socket, normalize.inputs[0])
+ value_socket = normalize.outputs[0]
+ ramp.color_ramp.elements[0].color = (1.0, 0.0, 0.0, 1.0)
+ ramp.color_ramp.elements[1].color = (0.0, 0.2, 1.0, 1.0)
+ middle = ramp.color_ramp.elements.new(0.5)
+ middle.color = (0.0, 1.0, 0.1, 1.0)
+ else:
+ ramp.color_ramp.elements[0].color = (0.0, 0.0, 0.0, 1.0)
+ ramp.color_ramp.elements[1].color = (1.0, 1.0, 1.0, 1.0)
+ links.new(value_socket, ramp.inputs['Fac'])
+
+ emission = nodes.new("ShaderNodeEmission")
+ emission.label = "SGDebugEmission"
+ links.new(ramp.outputs['Color'], emission.inputs['Color'])
+ output = next(
+ (n for n in nodes if n.type == 'OUTPUT_MATERIAL' and n.is_active_output),
+ None,
+ )
+ if output is None:
+ output = nodes.new("ShaderNodeOutputMaterial")
+ for link in list(output.inputs['Surface'].links):
+ links.remove(link)
+ links.new(emission.outputs[0], output.inputs['Surface'])
+
+ if mat is not None:
+ slot.material = mat
+ visualized += 1
+
+ if not visualized:
+ self.report({'WARNING'}, "The selected diagnostic is not present in these projection materials")
+ return {'CANCELLED'}
+ self.report({'INFO'}, f"Applied {self.mode.lower()} diagnostic to {visualized} material slot(s)")
+ return {'FINISHED'}
+
+
+# ═════════════════════════════════════════════════════════════════════════
+# 10. Debug Make Solid
# ═════════════════════════════════════════════════════════════════════════
class SG_OT_DebugMakeSolid(bpy.types.Operator):
@@ -1087,5 +1239,6 @@ debug_classes = [
SG_OT_DebugFeatherPreview,
SG_OT_DebugUVSeamViz,
SG_OT_DebugRestoreMaterial,
+ SG_OT_DebugOwnershipDiagnostic,
SG_OT_DebugMakeSolid,
]
diff --git a/stablegen/feather.osl b/stablegen/feather.osl
index 2f74227..bf12b36 100644
--- a/stablegen/feather.osl
+++ b/stablegen/feather.osl
@@ -2,6 +2,8 @@ shader raycast_feather(
vector Direction = N, // Ray direction in world space
float CameraAspect = 1.77778, // width/height ratio (e.g., 16:9)
float CameraFOV = 1.0, // horizontal FOV in radians
+ float CameraShiftX = 0.0,
+ float CameraShiftY = 0.0,
vector CameraDir = vector(0,0,-1), // Camera look direction in world space
vector CameraUp = vector(0,1,0), // Camera up vector in world space
float EdgeFeather = 0.0, // Fraction of radius used for feather (0 = off)
@@ -35,6 +37,8 @@ shader raycast_feather(
// Project the ray direction onto the film plane.
float x_proj = cam_x / -cam_z;
float y_proj = cam_y / -cam_z;
+ x_proj -= 2.0 * CameraShiftX * filmw;
+ y_proj -= 2.0 * CameraShiftY * filmh;
// If the projected point falls outside the film bounds, exit.
if (abs(x_proj) > filmw || abs(y_proj) > filmh) {
@@ -59,4 +63,4 @@ shader raycast_feather(
// No raycast needed here (handled by angle shader).
Result = edge_factor;
-}
\ No newline at end of file
+}
diff --git a/stablegen/raycast.osl b/stablegen/raycast.osl
index 0dcf86a..9b7585f 100644
--- a/stablegen/raycast.osl
+++ b/stablegen/raycast.osl
@@ -5,6 +5,8 @@ shader raycast(
float threshold = 1,
float CameraAspect = 1.77778, // width/height ratio (e.g., 16:9)
float CameraFOV = 1.0, // horizontal FOV in radians
+ float CameraShiftX = 0.0,
+ float CameraShiftY = 0.0,
vector CameraDir = vector(0,0,-1), // Camera look direction in world space
vector CameraUp = vector(0,1,0), // Camera up vector in world space
float AngleThreshold = 90.0, // Maximum angle in degrees between ray and normal
@@ -40,6 +42,8 @@ shader raycast(
// Project the ray direction onto the film plane.
float x_proj = cam_x / -cam_z;
float y_proj = cam_y / -cam_z;
+ x_proj -= 2.0 * CameraShiftX * filmw;
+ y_proj -= 2.0 * CameraShiftY * filmh;
// If the projected point falls outside the film bounds, exit.
if (abs(x_proj) > filmw || abs(y_proj) > filmh) {
diff --git a/stablegen/texturing/confidence.py b/stablegen/texturing/confidence.py
new file mode 100644
index 0000000..7274923
--- /dev/null
+++ b/stablegen/texturing/confidence.py
@@ -0,0 +1,252 @@
+"""Confidence, ownership, and sequencing primitives for sequential texturing.
+
+The functions in this module intentionally avoid Blender imports. The live
+shader graph and the baked pipeline use the same rules, while unit tests can
+exercise the policy without opening Blender.
+"""
+
+from __future__ import annotations
+
+import math
+from typing import Iterable, Sequence
+
+import numpy as np
+
+
+def _gaussian_blur(image, sigma):
+ """Use OpenCV when available, with a deterministic NumPy test fallback."""
+ sigma = max(0.1, float(sigma))
+ try:
+ import cv2
+ return cv2.GaussianBlur(image, (0, 0), sigma)
+ except ImportError:
+ radius = max(1, int(math.ceil(3.0 * sigma)))
+ coordinates = np.arange(-radius, radius + 1, dtype=np.float32)
+ kernel = np.exp(-(coordinates * coordinates) / (2.0 * sigma * sigma))
+ kernel /= kernel.sum()
+
+ def convolve_axis(values, axis):
+ pad = [(0, 0)] * values.ndim
+ pad[axis] = (radius, radius)
+ padded = np.pad(values, pad, mode="reflect")
+ return np.apply_along_axis(
+ lambda row: np.convolve(row, kernel, mode="valid"),
+ axis,
+ padded,
+ )
+
+ return convolve_axis(convolve_axis(image, 0), 1)
+
+
+def acquisition_quality(
+ visibility,
+ signed_frontality,
+ projected_density=1.0,
+ depth_edge_confidence=1.0,
+ frame_edge_confidence=1.0,
+):
+ """Return source-camera quality in ``[0, 1]``.
+
+ Each factor describes the camera that originally produced the colour.
+ Calling this function on a re-rendered/copying camera is therefore a policy
+ error; copied pixels retain their prior owner quality instead.
+ """
+ factors = [
+ visibility,
+ signed_frontality,
+ projected_density,
+ depth_edge_confidence,
+ frame_edge_confidence,
+ ]
+ result = np.asarray(factors[0], dtype=np.float32)
+ result = np.clip(result, 0.0, 1.0)
+ for factor in factors[1:]:
+ result = result * np.clip(np.asarray(factor, dtype=np.float32), 0.0, 1.0)
+ return np.clip(result, 0.0, 1.0)
+
+
+def resolve_ownership(
+ qualities,
+ write_masks=None,
+ *,
+ trust_threshold=0.7,
+ replacement_margin=0.05,
+ allow_trusted_replacement=False,
+ initial_priority_strength=0.0,
+):
+ """Resolve a single high-frequency source owner for every sample.
+
+ Uncovered and untrusted samples remain replaceable. Trusted samples lock
+ unless quality upgrades are explicitly enabled. Initial-view priority is
+ applied only as extra replacement margin for an already-trusted early
+ owner; it never changes acquisition quality or promotes grazing coverage.
+ """
+ q = np.asarray(qualities, dtype=np.float32)
+ if q.ndim < 1:
+ raise ValueError("qualities must contain a camera axis")
+ if write_masks is None:
+ writes = np.ones_like(q, dtype=bool)
+ else:
+ writes = np.asarray(write_masks) > 0
+ if writes.shape != q.shape:
+ raise ValueError("write_masks must match qualities")
+
+ q = np.where(writes, np.clip(q, 0.0, 1.0), 0.0)
+ sample_shape = q.shape[1:]
+ owner = np.full(sample_shape, -1, dtype=np.int32)
+ owner_quality = np.zeros(sample_shape, dtype=np.float32)
+ threshold = float(np.clip(trust_threshold, 0.0, 1.0))
+ base_margin = max(0.0, float(replacement_margin))
+ priority = float(np.clip(initial_priority_strength, 0.0, 1.0))
+ count = q.shape[0]
+
+ for camera_index in range(count):
+ new_q = q[camera_index]
+ has_write = writes[camera_index] & (new_q > 0.0)
+ uncovered = owner < 0
+ owner_trusted = owner_quality >= threshold
+
+ # Earlier reliable owners receive an additional margin. This is
+ # deliberately indexed by the current owner, not by the candidate.
+ if count > 1:
+ owner_rank = np.clip(owner, 0, count - 1) / float(count - 1)
+ else:
+ owner_rank = np.zeros(sample_shape, dtype=np.float32)
+ priority_margin = priority * (1.0 - owner_rank) * 0.25
+ quality_upgrade = new_q > owner_quality + base_margin + priority_margin
+
+ replace = has_write & (
+ uncovered
+ | (~owner_trusted)
+ | (bool(allow_trusted_replacement) & quality_upgrade)
+ )
+ owner[replace] = camera_index
+ owner_quality[replace] = new_q[replace]
+
+ coverage = owner >= 0
+ trusted = coverage & (owner_quality >= threshold)
+ return {
+ "owner": owner,
+ "owner_quality": owner_quality,
+ "trusted": trusted,
+ "coverage": coverage,
+ }
+
+
+def trusted_repair_masks(owner_quality, visible_target, trust_threshold=0.7):
+ """Return explicit trusted and repair masks without inspecting RGB."""
+ quality = np.asarray(owner_quality, dtype=np.float32)
+ visible = np.asarray(visible_target) > 0
+ trusted = visible & (quality >= float(trust_threshold))
+ repair = visible & ~trusted
+ return trusted, repair
+
+
+def low_frequency_seam_blend(images, owner, transition_mask, sigma=8.0):
+ """Blend only low-frequency colour inside a narrow ownership transition.
+
+ High-frequency detail always comes from the discrete owner image. The
+ returned image changes only the low-pass component in ``transition_mask``.
+ """
+ stack = np.asarray(images)
+ owner = np.asarray(owner, dtype=np.int32)
+ transition = np.asarray(transition_mask) > 0
+ if stack.ndim != 4 or stack.shape[-1] < 3:
+ raise ValueError("images must be camera x height x width x channels")
+ if owner.shape != stack.shape[1:3]:
+ raise ValueError("owner dimensions must match images")
+
+ yy, xx = np.indices(owner.shape)
+ safe_owner = np.clip(owner, 0, stack.shape[0] - 1)
+ hard = stack[safe_owner, yy, xx].astype(np.float32)
+ low_stack = np.stack([
+ _gaussian_blur(image.astype(np.float32), sigma)
+ for image in stack
+ ])
+ owner_low = low_stack[safe_owner, yy, xx]
+ average_low = np.mean(low_stack, axis=0)
+ result = hard.copy()
+ result[transition] = np.clip(
+ hard[transition] - owner_low[transition] + average_low[transition],
+ 0,
+ 255 if np.issubdtype(stack.dtype, np.integer) else 1.0,
+ )
+ return result.astype(stack.dtype)
+
+
+def camera_direction_from_matrix(matrix_rows) -> np.ndarray:
+ """Extract normalized world-space camera forward direction (-local Z)."""
+ matrix = np.asarray(matrix_rows, dtype=np.float64)
+ direction = -matrix[:3, 2]
+ length = np.linalg.norm(direction)
+ if length <= 1e-12:
+ return np.array((0.0, 0.0, -1.0), dtype=np.float64)
+ return direction / length
+
+
+def overlap_aware_order(
+ directions: Sequence[Iterable[float]],
+ *,
+ anchor_index=0,
+ reliable_new_coverage=None,
+ trusted_overlap=None,
+ occlusion_risk=None,
+ reliable_coverage=None,
+):
+ """Return a bridge-view order using the approved overlap-aware score.
+
+ Optional coverage/overlap/risk matrices allow a renderer to supply exact
+ measurements. Without them, angular proximity is used as a conservative
+ overlap proxy, which avoids front-to-back jumps for character turntables.
+ """
+ dirs = np.asarray(directions, dtype=np.float64)
+ if dirs.ndim != 2 or dirs.shape[1] != 3:
+ raise ValueError("directions must be an N x 3 sequence")
+ lengths = np.linalg.norm(dirs, axis=1, keepdims=True)
+ dirs = dirs / np.maximum(lengths, 1e-12)
+ count = len(dirs)
+ if not count:
+ return []
+ anchor = min(max(0, int(anchor_index)), count - 1)
+ remaining = set(range(count))
+ remaining.remove(anchor)
+ order = [anchor]
+
+ new_cov = np.ones(count) if reliable_new_coverage is None else np.asarray(reliable_new_coverage)
+ overlap = None if trusted_overlap is None else np.asarray(trusted_overlap)
+ risk = np.zeros(count) if occlusion_risk is None else np.asarray(occlusion_risk)
+ coverage = None if reliable_coverage is None else np.asarray(reliable_coverage, dtype=bool)
+ if coverage is not None and (coverage.ndim != 2 or coverage.shape[0] != count):
+ raise ValueError("reliable_coverage must be camera x sample")
+ covered = coverage[anchor].copy() if coverage is not None else None
+
+ while remaining:
+ previous = order[-1]
+ best = None
+ best_score = -float("inf")
+ for candidate in sorted(remaining):
+ cosine = float(np.clip(np.dot(dirs[previous], dirs[candidate]), -1.0, 1.0))
+ angular_jump = math.acos(cosine) / math.pi
+ if coverage is not None:
+ candidate_coverage = coverage[candidate]
+ new_score = float(np.mean(candidate_coverage & ~covered))
+ denominator = max(int(np.count_nonzero(candidate_coverage)), 1)
+ overlap_score = float(
+ np.count_nonzero(candidate_coverage & covered) / denominator
+ )
+ else:
+ new_score = float(new_cov[candidate])
+ overlap_score = (
+ float(overlap[previous, candidate])
+ if overlap is not None
+ else max(0.0, cosine)
+ )
+ score = new_score + overlap_score - angular_jump - float(risk[candidate])
+ if score > best_score:
+ best_score = score
+ best = candidate
+ order.append(best)
+ remaining.remove(best)
+ if coverage is not None:
+ covered |= coverage[best]
+ return order
diff --git a/stablegen/texturing/generator.py b/stablegen/texturing/generator.py
index 55e0336..8fdb5f8 100644
--- a/stablegen/texturing/generator.py
+++ b/stablegen/texturing/generator.py
@@ -13,10 +13,8 @@ import urllib.parse
import socket
import threading
import traceback
-import io
from datetime import datetime
import math
-import colorsys
from PIL import Image, ImageEnhance
import gpu # pylint: disable=import-error
@@ -24,22 +22,30 @@ import blf # pylint: disable=import-error
from gpu_extras.batch import batch_for_shader # pylint: disable=import-error
from ..util.workflow_templates import prompt_text, prompt_text_img2img, prompt_text_qwen_image_edit
-from .rendering import export_emit_image, export_visibility, export_canny, bake_texture, prepare_baking, unwrap, export_render, export_viewport, render_edge_feather_mask, apply_uv_inpaint_texture
+from .rendering import export_emit_image, export_visibility, export_canny, bake_texture, prepare_baking, unwrap, export_render, export_viewport, render_edge_feather_mask, apply_uv_inpaint_texture, flatten_projection_material_for_refine, _RenderStateTransaction
from ..cameras.geometry import _SGCameraResolution, _get_camera_resolution
from ..cameras.overlays import _sg_restore_square_display, _sg_remove_crop_overlay, _sg_ensure_crop_overlay, _sg_hide_label_overlay, _sg_restore_label_overlay
-from .projection import project_image, reinstate_compare_nodes, _bake_visibility_weights
+from .projection import project_image, reinstate_compare_nodes, _bake_visibility_weights, reset_projection_material_nodes, _is_projection_mesh
from ..utils import get_last_material_index, get_generation_dirs, get_file_path, get_dir_path, remove_empty_dirs, get_compositor_node_tree, configure_output_node_paths, get_eevee_engine_id, sg_modal_active
from ..util.mirror_color import MirrorReproject, _get_viewport_ref_np, _apply_color_match_to_file
from ..timeout_config import get_timeout
from .._generator_utils import redraw_ui, setup_studio_lighting, _pbr_setup_studio_lights, upload_image_to_comfyui
from .pbr import _PBRMixin
from .gallery import _PreviewGalleryOverlay
-from .view_plan import ViewRecord, choose_aligned_resolution, order_cameras
+from .view_plan import (
+ ViewRecord,
+ choose_aligned_resolution,
+ order_cameras,
+ order_cameras_from_manifest,
+)
from .quality_masks import (
apply_binary_context_mask,
regularize_repair_mask,
reject_fallback_from_write_mask,
+ residual_fallback_percentage,
+ build_depth_part_rejection_mask,
)
+from .confidence import camera_direction_from_matrix, overlap_aware_order
import websocket
@@ -446,16 +452,82 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
print(f"[StableGen] Binary Qwen context skipped; missing context or mask for camera {camera_id}")
return
+ # Constrain morphology to rendered object/material parts. This avoids
+ # letting a hand/accessory repair seed expand onto a dress/body part.
+ depth_dir = get_dir_path(context, "controlnet")["depth"]
+ frame_suffix = "0001" if bpy.app.version < (5, 0, 0) else ""
+ object_ids = cv2.imread(
+ os.path.join(depth_dir, f"object_id{camera_id}{frame_suffix}.png"),
+ cv2.IMREAD_GRAYSCALE,
+ )
+ material_ids = cv2.imread(
+ os.path.join(depth_dir, f"material_id{camera_id}{frame_suffix}.png"),
+ cv2.IMREAD_GRAYSCALE,
+ )
+ boundary_labels = None
+ if object_ids is not None or material_ids is not None:
+ shape = repair_mask.shape[:2]
+ if object_ids is None:
+ object_ids = np.ones(shape, dtype=np.uint8)
+ elif object_ids.shape != shape:
+ object_ids = cv2.resize(
+ object_ids, (shape[1], shape[0]), interpolation=cv2.INTER_NEAREST
+ )
+ if material_ids is None:
+ material_ids = np.zeros(shape, dtype=np.uint8)
+ elif material_ids.shape != shape:
+ material_ids = cv2.resize(
+ material_ids, (shape[1], shape[0]), interpolation=cv2.INTER_NEAREST
+ )
+ boundary_labels = (
+ object_ids.astype(np.int32) * 256 + material_ids.astype(np.int32)
+ )
+
repair_mask = regularize_repair_mask(
repair_mask,
threshold=127,
- dilation=context.scene.qwen_context_fallback_dilation,
+ dilation=getattr(context.scene, 'qwen_mask_uncertainty_dilation', 1),
+ min_island_area=getattr(context.scene, 'qwen_mask_min_island_area', 16),
+ min_hole_area=getattr(context.scene, 'qwen_mask_min_hole_area', 8),
+ min_strip_width=getattr(context.scene, 'qwen_mask_min_strip_width', 2),
+ boundary_labels=boundary_labels,
+ keep_largest_components=getattr(
+ context.scene, 'qwen_mask_keep_largest_components', 0
+ ),
+ )
+ # Depth/ID discontinuities are never trusted context. Forcing these
+ # pixels to repair prevents front objects from imprinting silhouettes
+ # onto the surface behind them.
+ guard_path = os.path.join(
+ get_dir_path(context, "inpaint")["visibility"],
+ f"projection_guard{camera_id}.png",
)
+ guard = cv2.imread(guard_path, cv2.IMREAD_GRAYSCALE)
+ if guard is not None:
+ if guard.shape != repair_mask.shape:
+ guard = cv2.resize(
+ guard,
+ (repair_mask.shape[1], repair_mask.shape[0]),
+ interpolation=cv2.INTER_NEAREST,
+ )
+ repair_mask[guard < 128] = 255
context_image = apply_binary_context_mask(
context_image, repair_mask, fallback_color
)
cv2.imwrite(context_path, context_image)
cv2.imwrite(mask_path, repair_mask)
+ stem, extension = os.path.splitext(mask_path)
+ repair_path = f"{stem}_repair{extension}"
+ trusted_path = f"{stem}_trusted{extension}"
+ cv2.imwrite(repair_path, repair_mask)
+ cv2.imwrite(trusted_path, 255 - repair_mask)
+ self._record_confidence_artifacts(
+ context,
+ camera_id,
+ repair_mask=repair_path,
+ trusted_mask=trusted_path,
+ trust_threshold=getattr(context.scene, 'qwen_trust_threshold', 0.7),
+ )
# These files are generated immediately before their first upload, but
# invalidate defensively for regeneration/debug workflows.
@@ -483,6 +555,42 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
tolerance=getattr(context.scene, "qwen_residual_fallback_tolerance", 8),
)
cv2.imwrite(mask_path, write_mask)
+ stem, extension = os.path.splitext(mask_path)
+ write_path = f"{stem}_write{extension}"
+ cv2.imwrite(write_path, write_mask)
+ residual_percent = residual_fallback_percentage(
+ cv2.imread(f"{stem}_repair{extension}", cv2.IMREAD_GRAYSCALE)
+ if os.path.exists(f"{stem}_repair{extension}") else write_mask,
+ generated,
+ fallback_color,
+ tolerance=getattr(context.scene, "qwen_residual_fallback_tolerance", 8),
+ )
+ print(
+ f"[StableGen] Qwen camera {camera_id}: residual sentinel "
+ f"{residual_percent:.3f}% of requested repair pixels"
+ )
+ self._record_confidence_artifacts(
+ context,
+ camera_id,
+ write_mask=write_path,
+ residual_sentinel_percent=residual_percent,
+ )
+
+ def _record_confidence_artifacts(self, context, camera_id, **values):
+ """Persist provenance/debug paths incrementally for regeneration."""
+ revision_dir = get_generation_dirs(context)["revision"]
+ manifest_path = os.path.join(revision_dir, "confidence_manifest.json")
+ try:
+ with open(manifest_path, "r", encoding="utf-8") as stream:
+ manifest = json.load(stream)
+ except (FileNotFoundError, OSError, ValueError, TypeError):
+ manifest = {"schema": 1, "views": {}}
+ view = manifest.setdefault("views", {}).setdefault(str(camera_id), {})
+ view.update(values)
+ temp_path = manifest_path + ".tmp"
+ with open(temp_path, "w", encoding="utf-8") as stream:
+ json.dump(manifest, stream, indent=2)
+ os.replace(temp_path, manifest_path)
@classmethod
def poll(cls, context):
@@ -603,6 +711,56 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
order_names = [item.name for item in context.scene.sg_camera_order]
self._cameras = order_cameras(self._cameras, order_names)
+ self._original_texturing_frame = context.scene.frame_current
+ context.scene.frame_set(int(getattr(context.scene, 'sg_texturing_frame', 1)))
+
+ if (context.scene.generation_mode == 'standard'
+ and not order_names
+ and getattr(context.scene, 'sg_auto_view_sequence', False)):
+ anchor_name = getattr(context.scene, 'sg_anchor_camera', '')
+ if not anchor_name and context.scene.camera:
+ anchor_name = context.scene.camera.name
+ anchor_index = next(
+ (index for index, camera in enumerate(self._cameras)
+ if camera.name == anchor_name),
+ 0,
+ )
+ directions = [
+ camera_direction_from_matrix(camera.matrix_world)
+ for camera in self._cameras
+ ]
+ try:
+ coverage, occlusion_risk = self._estimate_view_coverage(
+ context, self._cameras
+ )
+ except Exception as coverage_error:
+ print(
+ f"[StableGen] Overlap coverage sampling unavailable; "
+ f"using angular bridge fallback: {coverage_error}"
+ )
+ coverage, occlusion_risk = None, None
+ sequence = overlap_aware_order(
+ directions,
+ anchor_index=anchor_index,
+ reliable_coverage=coverage,
+ occlusion_risk=occlusion_risk,
+ )
+ self._cameras = [self._cameras[index] for index in sequence]
+
+ revision_dir = get_generation_dirs(context)["revision"]
+ manifest_path = os.path.join(revision_dir, "view_plan.json")
+ if context.scene.generation_mode in {'regenerate_selected', 'project_only'}:
+ try:
+ with open(manifest_path, "r", encoding="utf-8") as manifest_file:
+ persisted_manifest = json.load(manifest_file)
+ self._cameras = order_cameras_from_manifest(
+ self._cameras, persisted_manifest
+ )
+ except FileNotFoundError:
+ pass
+ except (OSError, ValueError, TypeError) as error:
+ print(f"[StableGen] Warning: could not restore view plan: {error}")
+
# Hide crop and label overlays during generation
_sg_remove_crop_overlay()
_sg_hide_label_overlay()
@@ -620,24 +778,54 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
cam["sg_res_x"] = crx
cam["sg_res_y"] = cry
- self._view_plan = []
+ view_records = []
for index, cam in enumerate(self._cameras):
if "sg_view_uuid" not in cam:
cam["sg_view_uuid"] = uuid.uuid4().hex
cam_res_x = int(cam.get("sg_res_x", render.resolution_x))
cam_res_y = int(cam.get("sg_res_y", render.resolution_y))
- self._view_plan.append(ViewRecord(
- view_id=str(cam["sg_view_uuid"]),
+ prompt_item = next(
+ (item for item in context.scene.camera_prompts if item.name == cam.name),
+ None,
+ )
+ view_prompt = prompt_item.prompt if prompt_item and prompt_item.prompt else ""
+ view_id = str(cam["sg_view_uuid"])
+ matrix_world = tuple(
+ tuple(float(value) for value in row) for row in cam.matrix_world
+ )
+ view_records.append(ViewRecord(
+ view_id=view_id,
index=index,
name=cam.name,
camera=cam,
resolution_x=cam_res_x,
resolution_y=cam_res_y,
+ prompt=view_prompt,
+ matrix_world=matrix_world,
+ intrinsics={
+ "type": cam.data.type,
+ "lens": cam.data.lens,
+ "sensor_width": cam.data.sensor_width,
+ "sensor_height": cam.data.sensor_height,
+ "shift_x": cam.data.shift_x,
+ "shift_y": cam.data.shift_y,
+ "ortho_scale": cam.data.ortho_scale,
+ },
+ file_identifiers={
+ "generated": f"generated:{view_id}",
+ "guide_depth": f"guide-depth:{view_id}",
+ "guide_normal": f"guide-normal:{view_id}",
+ "guide_canny": f"guide-canny:{view_id}",
+ "context": f"context:{view_id}",
+ "repair_mask": f"repair-mask:{view_id}",
+ "write_mask": f"write-mask:{view_id}",
+ },
))
+ self._view_plan = tuple(view_records)
+ self._cameras = tuple(record.camera for record in self._view_plan)
- revision_dir = get_generation_dirs(context)["revision"]
os.makedirs(revision_dir, exist_ok=True)
- with open(os.path.join(revision_dir, "view_plan.json"), "w", encoding="utf-8") as manifest_file:
+ with open(manifest_path, "w", encoding="utf-8") as manifest_file:
json.dump(
{"schema": 1, "views": [record.to_manifest() for record in self._view_plan]},
manifest_file,
@@ -657,6 +845,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
render.resolution_percentage = self._original_resolution_percentage
render.resolution_x = self._original_resolution_x
render.resolution_y = self._original_resolution_y
+ context.scene.frame_set(self._original_texturing_frame)
return {'CANCELLED'}
# If there are curves within the scene, warn the user
@@ -681,6 +870,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
render.resolution_percentage = self._original_resolution_percentage
render.resolution_x = self._original_resolution_x
render.resolution_y = self._original_resolution_y
+ context.scene.frame_set(self._original_texturing_frame)
return {'CANCELLED'}
else: # all
self._to_texture = [obj for obj in bpy.context.view_layer.objects if obj.type == 'MESH' and not obj.hide_get()]
@@ -703,6 +893,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
render.resolution_percentage = self._original_resolution_percentage
render.resolution_x = self._original_resolution_x
render.resolution_y = self._original_resolution_y
+ context.scene.frame_set(self._original_texturing_frame)
return {'CANCELLED'}
if not context.scene.overwrite_material or self._material_id == -1 or (context.scene.generation_method == 'local_edit' or (context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method == 'local_edit')):
@@ -876,6 +1067,18 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
for obj in bpy.context.view_layer.objects:
if obj.type == 'MESH' and obj.name in self._original_visibility:
obj.hide_render = self._original_visibility[obj.name]
+ # This is the modal equivalent of ``finally``: it runs once
+ # after the worker exits, before either success or error can
+ # return from the operator.
+ try:
+ self._reset_projection_state(context)
+ except Exception as reset_error:
+ traceback.print_exc()
+ if not self._error:
+ self._error = f"Failed to restore projection state: {reset_error}"
+ finally:
+ if hasattr(self, '_original_texturing_frame'):
+ context.scene.frame_set(self._original_texturing_frame)
if self._error:
if self._error == "'25'" or self._error == "'111'" or self._error == "'5'":
# Probably canceled by user, quietly return
@@ -898,101 +1101,6 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
if not context.scene.generation_mode == 'project_only':
self.report({'INFO'}, "Generation complete.")
- # Reset discard factor if enabled
- if (context.scene.discard_factor_generation_only and
- (self._generation_method_on_start == 'sequential' or context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein'))):
-
- new_discard_angle = context.scene.discard_factor_after_generation
- print(f"[StableGen] Resetting discard angle in material nodes to {new_discard_angle}...")
-
- for obj in self._to_texture:
- if not obj.active_material or not obj.active_material.use_nodes:
- continue
-
- nodes = obj.active_material.node_tree.nodes
- for node in nodes:
- # OSL script nodes (internal or external)
- if node.type == 'SCRIPT':
- if 'AngleThreshold' in node.inputs:
- node.inputs['AngleThreshold'].default_value = new_discard_angle
- # Native MATH LESS_THAN nodes (Blender 5.1+ native raycast path)
- elif node.type == 'MATH' and node.operation == 'LESS_THAN' and node.label.startswith('AngleThreshold-'):
- node.inputs[1].default_value = new_discard_angle
-
- print("[StableGen] Discard angle reset complete.")
-
- # Reset weight exponent if enabled
- if (context.scene.weight_exponent_generation_only and
- (self._generation_method_on_start == 'sequential' or context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein'))):
-
- new_exponent = context.scene.weight_exponent_after_generation
- print(f"[StableGen] Resetting weight exponent in material nodes to {new_exponent}...")
-
- for obj in self._to_texture:
- if not obj.active_material or not obj.active_material.use_nodes:
- continue
-
- nodes = obj.active_material.node_tree.nodes
- has_normalized_sharpness = any(
- node.type == 'MATH'
- and node.operation == 'POWER'
- and node.label.startswith('SharpW-')
- for node in nodes
- )
- for node in nodes:
- if (node.type == 'MATH'
- and node.operation == 'POWER'
- and node.label.startswith('SharpW-')):
- node.inputs[1].default_value = new_exponent
- elif node.type == 'SCRIPT' and 'Power' in node.inputs:
- # Normalized materials sharpen at SharpW; their
- # per-camera source must remain the base weight.
- node.inputs['Power'].default_value = (
- 1.0 if has_normalized_sharpness else new_exponent
- )
- elif (node.type == 'MATH'
- and node.operation == 'POWER'
- and node.label == 'power_weight'):
- node.inputs[1].default_value = (
- 1.0 if has_normalized_sharpness else new_exponent
- )
- elif (node.type == 'MATH'
- and node.operation == 'MULTIPLY'
- and node.label.startswith('InitialViewPrior-')
- and "sg_desired_prior" in node):
- node.inputs[1].default_value = float(
- node["sg_desired_prior"]
- ) ** (1.0 / max(float(new_exponent), 1e-6))
-
- print("[StableGen] Weight exponent reset complete.")
-
- # Baked visibility stores the angle/exponent numerically in
- # mesh attributes, so changing shader nodes alone cannot reset
- # it. Recompute once using the final display settings.
- reset_angle = (
- context.scene.discard_factor_after_generation
- if context.scene.discard_factor_generation_only
- else context.scene.discard_factor
- )
- reset_exponent = (
- context.scene.weight_exponent_after_generation
- if context.scene.weight_exponent_generation_only
- else context.scene.weight_exponent
- )
- if (context.scene.bake_visibility_weights
- and (context.scene.discard_factor_generation_only
- or context.scene.weight_exponent_generation_only)
- and self._cameras):
- print("[StableGen] Rebaking visibility weights for final display settings...")
- _bake_visibility_weights(
- context,
- self._to_texture,
- self._cameras,
- self._material_id,
- angle_threshold=reset_angle,
- power_exponent=reset_exponent,
- )
-
# If viewport rendering mode is 'Rendered' and mode is 'regenerate_selected', switch to 'Solid' and then back to 'Rendered' to refresh the viewport
if context.scene.generation_mode == 'regenerate_selected' and context.area.spaces.active.shading.type == 'RENDERED':
context.area.spaces.active.shading.type = 'SOLID'
@@ -1040,6 +1148,45 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
return self.execute(context)
return {'PASS_THROUGH'}
+
+ def _reset_projection_state(self, context):
+ """Restore final display controls after every worker termination."""
+ applicable = (
+ getattr(self, '_generation_method_on_start', None) == 'sequential'
+ or context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein')
+ )
+ reset_angle = (
+ context.scene.discard_factor_after_generation
+ if context.scene.discard_factor_generation_only and applicable
+ else context.scene.discard_factor
+ )
+ reset_exponent = (
+ context.scene.weight_exponent_after_generation
+ if context.scene.weight_exponent_generation_only and applicable
+ else context.scene.weight_exponent
+ )
+
+ for obj in getattr(self, '_to_texture', ()):
+ material = getattr(obj, 'active_material', None)
+ reset_projection_material_nodes(material, reset_angle, reset_exponent)
+
+ # Material state is diagnostic state, not generation configuration.
+ context.scene.sg_material_discard_factor = reset_angle
+ context.scene.sg_material_weight_exponent = reset_exponent
+
+ if (context.scene.bake_visibility_weights
+ and applicable
+ and (context.scene.discard_factor_generation_only
+ or context.scene.weight_exponent_generation_only)
+ and getattr(self, '_cameras', None)):
+ _bake_visibility_weights(
+ context,
+ self._to_texture,
+ self._cameras,
+ self._material_id,
+ angle_threshold=reset_angle,
+ power_exponent=reset_exponent,
+ )
def cancel_generate(self, context):
"""
@@ -1068,6 +1215,124 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
# Map preparation — runs from the async thread via timer callbacks
# so that _stage updates are visible in real time through the modal.
# ------------------------------------------------------------------
+ def _estimate_view_coverage(self, context, cameras, max_samples=8192):
+ """Sample evaluated character geometry for overlap-aware scheduling."""
+ targets = [
+ obj for obj in context.view_layer.objects
+ if _is_projection_mesh(obj)
+ and (context.scene.texture_objects != 'selected' or obj.select_get())
+ ]
+ if not targets and context.scene.texture_objects == 'selected':
+ targets = [obj for obj in context.view_layer.objects if _is_projection_mesh(obj)]
+ if not targets:
+ return None, None
+
+ depsgraph = context.evaluated_depsgraph_get()
+ positions = []
+ normals = []
+ for obj in targets:
+ evaluated = obj.evaluated_get(depsgraph)
+ mesh = evaluated.to_mesh()
+ try:
+ if not mesh.vertices:
+ continue
+ world = np.asarray(evaluated.matrix_world, dtype=np.float64)
+ normal_matrix = np.asarray(
+ evaluated.matrix_world.to_3x3().inverted().transposed(),
+ dtype=np.float64,
+ )
+ local_positions = np.asarray(
+ [vertex.co[:] for vertex in mesh.vertices], dtype=np.float64
+ )
+ local_normals = np.asarray(
+ [vertex.normal[:] for vertex in mesh.vertices], dtype=np.float64
+ )
+ homogeneous = np.concatenate(
+ [local_positions, np.ones((len(local_positions), 1))], axis=1
+ )
+ positions.append((homogeneous @ world.T)[:, :3])
+ world_normals = local_normals @ normal_matrix.T
+ world_normals /= np.maximum(
+ np.linalg.norm(world_normals, axis=1, keepdims=True), 1e-12
+ )
+ normals.append(world_normals)
+ finally:
+ evaluated.to_mesh_clear()
+ if not positions:
+ return None, None
+ positions = np.concatenate(positions)
+ normals = np.concatenate(normals)
+ if len(positions) > max_samples:
+ sample_indices = np.linspace(
+ 0, len(positions) - 1, max_samples, dtype=np.int64
+ )
+ positions = positions[sample_indices]
+ normals = normals[sample_indices]
+
+ reliable = []
+ risks = []
+ trust = float(getattr(context.scene, 'qwen_trust_threshold', 0.7))
+ discard_cosine = math.cos(math.radians(float(context.scene.discard_factor)))
+ front_threshold = max(0.0, min(1.0, max(trust, discard_cosine)))
+ homogeneous_positions = np.concatenate(
+ [positions, np.ones((len(positions), 1))], axis=1
+ )
+
+ for camera in cameras:
+ camera_location = np.asarray(camera.matrix_world.translation, dtype=np.float64)
+ surface_to_camera = camera_location[None, :] - positions
+ distances = np.linalg.norm(surface_to_camera, axis=1)
+ view_direction = surface_to_camera / np.maximum(distances[:, None], 1e-12)
+ frontality = np.einsum('ij,ij->i', normals, view_direction)
+ camera_space = homogeneous_positions @ np.asarray(
+ camera.matrix_world.inverted(), dtype=np.float64
+ ).T
+ depth = -camera_space[:, 2]
+ res_x, res_y = _get_camera_resolution(camera, context.scene)
+ aspect = res_x / max(res_y, 1)
+ if camera.data.type == 'ORTHO':
+ half_h = max(float(camera.data.ortho_scale) * 0.5, 1e-8)
+ half_w = half_h * aspect
+ norm_x = camera_space[:, 0] / half_w - 2.0 * camera.data.shift_x
+ norm_y = camera_space[:, 1] / half_h - 2.0 * camera.data.shift_y
+ else:
+ half_w = max(math.tan(camera.data.angle_x * 0.5), 1e-8)
+ half_h = half_w / aspect
+ norm_x = camera_space[:, 0] / np.maximum(depth * half_w, 1e-8) - 2.0 * camera.data.shift_x
+ norm_y = camera_space[:, 1] / np.maximum(depth * half_h, 1e-8) - 2.0 * camera.data.shift_y
+ in_frame = (depth > 0) & (np.abs(norm_x) < 1.0) & (np.abs(norm_y) < 1.0)
+ camera_reliable = in_frame & (frontality >= front_threshold)
+ reliable.append(camera_reliable)
+ frame_edge = np.maximum(np.abs(norm_x), np.abs(norm_y))
+ grazing = in_frame & (frontality > 0.0) & (frontality < front_threshold + 0.15)
+ boundary = in_frame & (frame_edge > 0.85)
+ risks.append(float(np.mean(grazing | boundary)))
+ return np.asarray(reliable, dtype=bool), np.asarray(risks, dtype=np.float64)
+
+ def _activate_view(self, context, index):
+ """Reapply the immutable view snapshot before every camera stage."""
+ if not self._view_plan or not (0 <= index < len(self._view_plan)):
+ return None
+ texturing_frame = int(getattr(context.scene, 'sg_texturing_frame', 1))
+ if context.scene.frame_current != texturing_frame:
+ context.scene.frame_set(texturing_frame)
+ record = self._view_plan[index]
+ camera = record.camera
+ if record.matrix_world:
+ camera.matrix_world = mathutils.Matrix(record.matrix_world)
+ intrinsics = dict(record.intrinsics or {})
+ for name in (
+ 'type', 'lens', 'sensor_width', 'sensor_height',
+ 'shift_x', 'shift_y', 'ortho_scale',
+ ):
+ value = intrinsics.get(name)
+ if value is not None and hasattr(camera.data, name):
+ setattr(camera.data, name, value)
+ camera['sg_res_x'] = int(record.resolution_x)
+ camera['sg_res_y'] = int(record.resolution_y)
+ context.scene.camera = camera
+ return camera
+
def _prepare_maps(self, context):
"""Render ControlNet / refine maps. Called at the start of the async
thread; every Blender render is dispatched to the main thread via
@@ -1084,13 +1349,15 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
or (context.scene.model_architecture.startswith('qwen')
and context.scene.qwen_generation_method in ('refine', 'local_edit')
and context.scene.qwen_refine_use_depth)
+ or (context.scene.generation_method == 'sequential'
+ and getattr(context.scene, 'sg_silhouette_margin', 0) > 0)
)
if need_depth and context.scene.generation_method != 'uv_inpaint':
for i, camera in enumerate(cameras):
self._stage = f"Rendering Depth Maps ({i+1}/{len(cameras)})"
_i, _cam = i, camera
def _render_depth(_i=_i, _cam=_cam):
- bpy.context.scene.camera = _cam
+ self._activate_view(context, _i)
with _SGCameraResolution(context, _cam):
self.export_depthmap(context, camera_id=_i)
self._run_on_main_thread(_render_depth)
@@ -1108,7 +1375,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
self._stage = f"Rendering Canny Maps ({i+1}/{len(cameras)})"
_i, _cam = i, camera
def _render_canny(_i=_i, _cam=_cam):
- bpy.context.scene.camera = _cam
+ self._activate_view(context, _i)
with _SGCameraResolution(context, _cam):
export_canny(context, camera_id=_i,
low_threshold=context.scene.canny_threshold_low,
@@ -1132,7 +1399,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
self._stage = f"Rendering Normal Maps ({i+1}/{len(cameras)})"
_i, _cam = i, camera
def _render_normal(_i=_i, _cam=_cam):
- bpy.context.scene.camera = _cam
+ self._activate_view(context, _i)
with _SGCameraResolution(context, _cam):
self.export_normal(context, camera_id=_i)
self._run_on_main_thread(_render_normal)
@@ -1153,7 +1420,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
self._stage = f"Rendering Workbench ({i+1}/{len(cameras)})"
_i, _cam = i, camera
def _render_wb(_i=_i, _cam=_cam):
- bpy.context.scene.camera = _cam
+ self._activate_view(context, _i)
with _SGCameraResolution(context, _cam):
export_render(context, camera_id=_i,
output_dir=workbench_dir, filename=f"render{_i}")
@@ -1175,7 +1442,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
self._stage = f"Rendering Viewport ({i+1}/{len(cameras)})"
_i, _cam = i, camera
def _render_vp(_i=_i, _cam=_cam):
- bpy.context.scene.camera = _cam
+ self._activate_view(context, _i)
with _SGCameraResolution(context, _cam):
export_viewport(context, camera_id=_i,
output_dir=viewport_dir, filename=f"viewport{_i}")
@@ -1205,7 +1472,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
self._stage = f"Preparing Refinement Maps ({i+1}/{len(cameras)})"
_i, _cam = i, camera
def _render_refine(_i=_i, _cam=_cam):
- bpy.context.scene.camera = _cam
+ self._activate_view(context, _i)
with _SGCameraResolution(context, _cam):
export_emit_image(context, self._to_texture, camera_id=_i)
if need_feather:
@@ -1273,7 +1540,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
redraw_ui(context)
project_image(
context, self._to_texture, self._material_id,
- stop_index=self._current_image, cameras=self._cameras,
+ stop_index=self._current_image, view_plan=self._view_plan,
)
except Exception as e:
self._error = str(e)
@@ -1376,7 +1643,8 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
# (export_visibility internally picks the next camera after _vis_cam),
# so use the current camera's resolution for the render.
_vis_cam = self._cameras[self._current_image - 1]
- _cur_cam = self._cameras[self._current_image] if self._current_image < len(self._cameras) else _vis_cam
+ _cur_index = self._current_image if self._current_image < len(self._cameras) else self._current_image - 1
+ _cur_cam = self._activate_view(context, _cur_index)
with _SGCameraResolution(context, _cur_cam):
export_visibility(
context,
@@ -1386,7 +1654,6 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
camera_id=self._current_image,
) # Export mask for current view
_emit_cam = _cur_cam
- context.scene.camera = _emit_cam
with _SGCameraResolution(context, _emit_cam):
if context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein'): # export custom bg and fallback for Qwen/Klein image edit
fallback_color, background_color = self._get_qwen_context_colors(context)
@@ -1437,13 +1704,6 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
self._error = "Connection to ComfyUI server failed."
return # Error message set
- if (context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein') and
- context.scene.generation_method == 'sequential' and
- self._current_image > 0 and
- context.scene.qwen_context_cleanup and
- context.scene.qwen_context_render_mode in {'REPLACE_STYLE', 'ADDITIONAL'}):
- image = self._apply_qwen_context_cleanup(context, image)
-
# Save the generated image using new path structure
if context.scene.generation_method == 'uv_inpaint':
image_path = get_file_path(context, "generated_baked", object_name=self._to_texture[self._current_image].name, material_id=self._material_id)
@@ -1504,7 +1764,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
redraw_ui(context)
project_image(
context, self._to_texture, self._material_id,
- stop_index=self._current_image, cameras=self._cameras,
+ stop_index=self._current_image, view_plan=self._view_plan,
)
except Exception as e:
self._error = str(e)
@@ -1643,13 +1903,14 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
)
if getattr(context.scene, 'pbr_auto_lighting', False):
_pbr_setup_studio_lights(context, self._to_texture)
+ self._freeze_projection_ownership(context)
return None
self._stage = "Projecting Image"
redraw_ui(context)
if context.scene.generation_method != 'uv_inpaint':
project_image(
context, self._to_texture, self._material_id,
- cameras=self._cameras,
+ view_plan=self._view_plan,
)
else:
# Apply the UV inpainted textures to each mesh
@@ -1670,6 +1931,8 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
if getattr(context.scene, 'pbr_auto_lighting', False):
_pbr_setup_studio_lights(context, self._to_texture)
+ self._freeze_projection_ownership(context)
+
return None
if context.scene.view_blend_use_color_match and self._to_texture:
@@ -1701,6 +1964,33 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
elif context.scene.control_after_generate == 'randomize':
context.scene.seed = np.random.randint(0, 1000000)
+ def _freeze_projection_ownership(self, context):
+ """Bake live camera ownership into production UV textures when enabled."""
+ ownership_active = (
+ getattr(context.scene, 'sg_texel_ownership', False)
+ or (context.scene.model_architecture == 'qwen_image_edit'
+ and context.scene.generation_method == 'sequential'
+ and getattr(context.scene, 'qwen_use_projection_write_masks', True))
+ )
+ if not (ownership_active
+ and getattr(context.scene, 'sg_freeze_ownership_after_generation', False)):
+ return
+ self._stage = "Freezing Texture Ownership"
+ output_dir = get_dir_path(context, "baked")
+ prepare_baking(context)
+ for obj in self._to_texture:
+ if bake_texture(
+ context,
+ obj,
+ texture_resolution=2048,
+ view_transform='Standard',
+ output_dir=output_dir,
+ ):
+ baked_path = os.path.join(output_dir, f"{obj.name}.png")
+ flatten_projection_material_for_refine(context, obj, baked_path)
+ obj["sg_ownership_frozen"] = True
+ obj["sg_ownership_texture"] = baked_path
+
def draw(self, context):
layout = self.layout
if context.scene.generation_method == 'uv_inpaint' and self.show_prompt_dialog:
@@ -1724,120 +2014,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
return context.window_manager.invoke_props_dialog(self, width=400)
return self.execute(context)
- def _dilate_qwen_context_fallback(self, context, camera_id, fallback_color):
- dilation = int(max(0, context.scene.qwen_context_fallback_dilation))
- if dilation <= 0:
- return
-
- image_path = get_file_path(context, "inpaint", subtype="render", camera_id=camera_id)
- if not image_path or not os.path.exists(image_path):
- return
-
- try:
- with Image.open(image_path) as img:
- pixel_data = np.array(img.convert("RGBA"))
- except Exception as err:
- print(f"[StableGen] Failed to load context render for dilation at {image_path}: {err}")
- return
-
- fallback_rgb = np.array([int(round(component * 255.0)) for component in fallback_color], dtype=np.uint8)
- rgb = pixel_data[:, :, :3].astype(np.int16)
- diff = np.abs(rgb - fallback_rgb[np.newaxis, np.newaxis, :])
- mask = np.all(diff <= 3, axis=2)
- if not np.any(mask):
- return
-
- mask_uint8 = (mask.astype(np.uint8) * 255)
- kernel_size = max(1, dilation * 2 + 1)
- kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size))
- dilated = cv2.dilate(mask_uint8, kernel, iterations=1)
- dilated_mask = dilated > 0
-
- pixel_data[dilated_mask, :3] = fallback_rgb
- if pixel_data.shape[2] == 4:
- pixel_data[dilated_mask, 3] = 255
-
- try:
- Image.fromarray(pixel_data).save(image_path)
- except Exception as err:
- print(f"[StableGen] Failed to save dilated context render at {image_path}: {err}")
- return
-
- if hasattr(self, '_uploaded_images_cache') and self._uploaded_images_cache is not None:
- self._uploaded_images_cache.pop(os.path.abspath(image_path), None)
-
- # ── Qwen context cleanup ──────────────────────────────────────────
-
- def _apply_qwen_context_cleanup(self, context, image_bytes):
- hue_tolerance = max(context.scene.qwen_context_cleanup_hue_tolerance, 0.0)
- value_adjust = context.scene.qwen_context_cleanup_value_adjust
- fallback_color = tuple(context.scene.qwen_guidance_fallback_color)
- try:
- with Image.open(io.BytesIO(image_bytes)) as pil_image:
- rgba_image = pil_image.convert("RGBA")
- pixel_data = np.array(rgba_image)
- except Exception as err:
- print(f"[StableGen] Warning: Failed to read Qwen context render for cleanup: {err}")
- traceback.print_exc()
- return image_bytes
-
- rgb = pixel_data[:, :, :3].astype(np.float32) / 255.0
- alpha = pixel_data[:, :, 3]
-
- maxc = rgb.max(axis=2)
- minc = rgb.min(axis=2)
- delta = maxc - minc
-
- hue = np.zeros_like(maxc, dtype=np.float32)
- non_gray = delta > 1e-6
- safe_delta = np.where(non_gray, delta, 1.0) # avoid divide-by-zero
-
- r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
-
- idx = non_gray & (r == maxc)
- hue[idx] = ((g[idx] - b[idx]) / safe_delta[idx]) % 6.0
- idx = non_gray & (g == maxc)
- hue[idx] = ((b[idx] - r[idx]) / safe_delta[idx]) + 2.0
- idx = non_gray & (b == maxc)
- hue[idx] = ((r[idx] - g[idx]) / safe_delta[idx]) + 4.0
- hue = (hue / 6.0) % 1.0
-
- try:
- fallback_hue = colorsys.rgb_to_hsv(*fallback_color)[0]
- except Exception:
- fallback_hue = 0.0
- hue_tol_normalized = hue_tolerance / 360.0
- if hue_tol_normalized <= 0.0:
- hue_tol_normalized = 0.0
-
- diff = np.abs(hue - fallback_hue)
- diff = np.minimum(diff, 1.0 - diff)
- target_mask = non_gray & (diff <= hue_tol_normalized)
-
- if not np.any(target_mask):
- return image_bytes
-
- value = maxc
- adjusted_value = np.clip(value[target_mask] + value_adjust, 0.0, 1.0)
-
- updated_rgb = np.array(rgb)
- grayscale_values = np.repeat(adjusted_value[:, None], 3, axis=1)
- updated_rgb[target_mask] = grayscale_values
-
- updated_pixels = np.empty_like(pixel_data)
- updated_pixels[:, :, :3] = np.clip(np.round(updated_rgb * 255.0), 0, 255).astype(np.uint8)
- updated_pixels[:, :, 3] = alpha
-
- try:
- buffer = io.BytesIO()
- Image.fromarray(updated_pixels, mode="RGBA").save(buffer, format="PNG")
- return buffer.getvalue()
- except Exception as err:
- print(f"[StableGen] Warning: Failed to write cleaned Qwen context render: {err}")
- traceback.print_exc()
- return image_bytes
-
- def export_depthmap(self, context, camera_id=None):
+ def _export_depthmap_impl(self, context, camera_id=None):
"""
Exports the depth map of the scene.
:param context: Blender context.
@@ -1852,9 +2029,6 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
original_use_compositing = bpy.context.scene.render.use_compositing
original_filepath = bpy.context.scene.render.filepath
- # Set animation frame to 1
- bpy.context.scene.frame_set(1)
-
output_dir = get_dir_path(context, "controlnet")["depth"]
output_file = f"depth_map{camera_id}" if camera_id is not None else "depth_map"
@@ -1865,8 +2039,11 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
# Get the active view layer
view_layer = bpy.context.view_layer
- # Switch to WORKBENCH render engine
- bpy.context.scene.render.engine = 'BLENDER_WORKBENCH'
+ # Cycles exposes depth plus object/material index passes. Workbench and
+ # Eevee in Blender 5 expose only Image/Alpha/Depth on Render Layers,
+ # silently disabling the hand/dress part guard.
+ bpy.context.scene.render.engine = 'CYCLES'
+ bpy.context.scene.cycles.samples = 1
bpy.context.scene.display_settings.display_device = 'sRGB'
bpy.context.scene.view_settings.view_transform = 'Raw'
@@ -1875,6 +2052,23 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
# Enable depth pass in the render settings
view_layer.use_pass_z = True
+ if hasattr(view_layer, 'use_pass_object_index'):
+ view_layer.use_pass_object_index = True
+ if hasattr(view_layer, 'use_pass_material_index'):
+ view_layer.use_pass_material_index = True
+
+ material_indices = {}
+ next_material_index = 1
+ for object_index, render_object in enumerate(
+ (obj for obj in context.view_layer.objects
+ if obj.type == 'MESH' and not obj.hide_render), start=1):
+ render_object.pass_index = min(object_index, 255)
+ for slot in render_object.material_slots:
+ material = slot.material
+ if material and material not in material_indices:
+ material_indices[material] = min(next_material_index, 255)
+ material.pass_index = material_indices[material]
+ next_material_index += 1
# Enable compositor pipeline (may have been disabled by prior GIF export)
bpy.context.scene.render.use_compositing = True
@@ -1886,10 +2080,6 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
# Ensure animation format is not selected
bpy.context.scene.render.image_settings.file_format = 'PNG'
- # Clear default nodes
- for node in nodes:
- nodes.remove(node)
-
# Add render layers node
render_layers_node = nodes.new(type="CompositorNodeRLayers")
render_layers_node.location = (0, 0)
@@ -1912,9 +2102,78 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
configure_output_node_paths(output_node, output_dir, output_file)
links.new(invert_node.outputs[0], output_node.inputs[0])
+ def _add_index_output(socket_name, filename, y):
+ aliases = {
+ "IndexOB": ("IndexOB", "Object Index"),
+ "IndexMA": ("IndexMA", "Material Index"),
+ }
+ actual_socket = next(
+ (name for name in aliases.get(socket_name, (socket_name,))
+ if name in render_layers_node.outputs),
+ None,
+ )
+ if actual_socket is None:
+ return False
+ divide = nodes.new(type="CompositorNodeMath")
+ divide.operation = 'DIVIDE'
+ divide.inputs[1].default_value = 255.0
+ divide.location = (200, y)
+ links.new(render_layers_node.outputs[actual_socket], divide.inputs[0])
+ index_output = nodes.new(type="CompositorNodeOutputFile")
+ index_output.location = (600, y)
+ configure_output_node_paths(index_output, output_dir, filename)
+ links.new(divide.outputs[0], index_output.inputs[0])
+ return True
+
+ has_object_ids = _add_index_output(
+ "IndexOB", f"object_id{camera_id}", -200
+ )
+ has_material_ids = _add_index_output(
+ "IndexMA", f"material_id{camera_id}", -400
+ )
+
# Render the scene
bpy.ops.render.render(write_still=True)
+ frame_suffix = "0001" if bpy.app.version < (5, 0, 0) else ""
+ depth_path = os.path.join(output_dir, f"{output_file}{frame_suffix}.png")
+ object_path = os.path.join(output_dir, f"object_id{camera_id}{frame_suffix}.png")
+ material_path = os.path.join(output_dir, f"material_id{camera_id}{frame_suffix}.png")
+ depth_image = cv2.imread(depth_path, cv2.IMREAD_GRAYSCALE)
+ if depth_image is not None:
+ object_image = (
+ cv2.imread(object_path, cv2.IMREAD_GRAYSCALE)
+ if has_object_ids else None
+ )
+ material_image = (
+ cv2.imread(material_path, cv2.IMREAD_GRAYSCALE)
+ if has_material_ids else None
+ )
+ guard = build_depth_part_rejection_mask(
+ depth_image,
+ object_image,
+ material_image,
+ margin=getattr(context.scene, 'sg_silhouette_margin', 3),
+ depth_threshold=max(
+ 1,
+ round(float(getattr(context.scene, 'sg_silhouette_depth', 0.01)) * 255),
+ ),
+ )
+ guard_dir = get_dir_path(context, "inpaint")["visibility"]
+ os.makedirs(guard_dir, exist_ok=True)
+ guard_path = os.path.join(
+ guard_dir, f"projection_guard{camera_id}.png"
+ )
+ cv2.imwrite(guard_path, guard)
+ self._record_confidence_artifacts(
+ context,
+ camera_id,
+ depth_map=depth_path,
+ object_id_map=object_path if has_object_ids else None,
+ material_id_map=material_path if has_material_ids else None,
+ projection_guard=guard_path,
+ )
+
bpy.context.scene.view_settings.view_transform = 'Standard'
print(f"[StableGen] Depth map saved to: {os.path.join(output_dir, output_file)}.png")
@@ -1927,6 +2186,11 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
bpy.context.scene.render.filepath = original_filepath
view_layer.use_pass_z = original_pass_z
+ def export_depthmap(self, context, camera_id=None):
+ meshes = [obj for obj in context.view_layer.objects if obj.type == 'MESH']
+ with _RenderStateTransaction(context, meshes):
+ return self._export_depthmap_impl(context, camera_id=camera_id)
+
def export_normal(self, context, camera_id=None):
"""
Exports the normal map of the scene.
@@ -1935,8 +2199,13 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
:param camera_id: ID of the camera.
:return: None
"""
+ meshes = [obj for obj in context.view_layer.objects if obj.type == 'MESH']
+ with _RenderStateTransaction(context, meshes):
+ return self._export_normal_impl(context, camera_id=camera_id)
+
+ def _export_normal_impl(self, context, camera_id=None):
+ """Render the evaluated normal pass without mutating production state."""
print("[StableGen] Exporting normal map")
- bpy.context.scene.frame_set(1)
output_dir = get_dir_path(context, "controlnet")["normal"]
output_file = f"normal_map{camera_id}" if camera_id is not None else "normal_map"
@@ -1960,12 +2229,11 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
bpy.context.scene.render.use_compositing = True
bpy.context.scene.use_nodes = True
- # Clear existing nodes.
+ # Add temporary compositor nodes. _RenderStateTransaction removes only
+ # nodes created during this export and leaves the user's graph intact.
node_tree = get_compositor_node_tree(bpy.context.scene)
nodes = node_tree.nodes
links = node_tree.links
- for node in nodes:
- nodes.remove(node)
# Create the Render Layers node (provides the baked normal pass).
render_layers_node = nodes.new(type="CompositorNodeRLayers")
diff --git a/stablegen/texturing/graph_validation.py b/stablegen/texturing/graph_validation.py
index bbc83ae..754fc86 100644
--- a/stablegen/texturing/graph_validation.py
+++ b/stablegen/texturing/graph_validation.py
@@ -59,3 +59,16 @@ def validate_workflow_graph(prompt: Mapping, semantic_node_ids=()) -> list[str]:
f"semantic image slots differ between text encoders and node {node_id}"
)
return issues
+
+
+def qwen_style_semantic_slot(context_mode: str, is_initial_image: bool) -> str:
+ """Return the Qwen text-encoder slot occupied by the style reference."""
+ if context_mode == "ADDITIONAL" and not is_initial_image:
+ return "image3"
+ return "image2"
+
+
+def sdxl_final_mask_reference(blur_mask: bool, nodes: Mapping) -> list:
+ """Return the one processed mask consumed by all SDXL inpaint nodes."""
+ node_id = nodes['image_to_mask'] if blur_mask else nodes['grow_mask']
+ return [node_id, 0]
diff --git a/stablegen/texturing/projection.py b/stablegen/texturing/projection.py
index b01e9fc..a72dc76 100644
--- a/stablegen/texturing/projection.py
+++ b/stablegen/texturing/projection.py
@@ -5,12 +5,77 @@ from math import atan, tan, pi
from ..utils import get_last_material_index, get_file_path, get_dir_path
from .rendering import prepare_baking, bake_texture, unwrap
from ..cameras.geometry import _get_camera_resolution
-from mathutils import Vector
+from mathutils import Matrix, Vector
_SG_BUFFER_UV_NAME = "_SG_ProjectionBuffer"
+def _is_projection_mesh(obj):
+ """Consistent render/visibility filter for production geometry."""
+ if obj.type != 'MESH' or obj.hide_get() or obj.hide_render:
+ return False
+ if obj.get("sg_rig_helper", False) or obj.get("sg_exclude_texturing", False):
+ return False
+ upper_name = obj.name.upper()
+ if upper_name.startswith(("WGT-", "WGT_", "RIG_WIDGET", "CTRL_SHAPE")):
+ return False
+ return True
+
+
+def _scene_projection_scale(context):
+ corners = []
+ for obj in context.view_layer.objects:
+ if _is_projection_mesh(obj):
+ corners.extend(obj.matrix_world @ Vector(corner) for corner in obj.bound_box)
+ if not corners:
+ return 1.0
+ return max(
+ (Vector((
+ max(point.x for point in corners),
+ max(point.y for point in corners),
+ max(point.z for point in corners),
+ )) - Vector((
+ min(point.x for point in corners),
+ min(point.y for point in corners),
+ min(point.z for point in corners),
+ ))).length,
+ 1e-6,
+ )
+
+
+def _ensure_lowpass_texture(image_path, sigma=8.0):
+ """Create/update a low-pass companion used only for ownership seams."""
+ if not image_path or not os.path.exists(image_path):
+ return None
+ root, extension = os.path.splitext(image_path)
+ sigma_value = max(0.1, float(sigma))
+ sigma_key = f"{sigma_value:.2f}".replace('.', '_')
+ output_path = f"{root}_sg_lowpass_s{sigma_key}{extension or '.png'}"
+ try:
+ if (os.path.exists(output_path)
+ and os.path.getmtime(output_path) >= os.path.getmtime(image_path)):
+ return output_path
+ import cv2
+ source = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
+ if source is None:
+ return None
+ result = source.copy()
+ channels = result[:, :, :3] if result.ndim == 3 else result
+ blurred = cv2.GaussianBlur(
+ channels, (0, 0), sigmaX=sigma_value
+ )
+ if result.ndim == 3:
+ result[:, :, :3] = blurred
+ else:
+ result = blurred
+ cv2.imwrite(output_path, result)
+ return output_path
+ except Exception as error:
+ print(f"[StableGen] Low-frequency seam texture failed: {error}")
+ return None
+
+
def _camera_world_location(camera):
"""Return camera position in world space (including parent transforms)."""
return camera.matrix_world.translation.copy()
@@ -20,7 +85,7 @@ def _scene_ray_epsilon(context):
"""Scale ray tolerances to the scene instead of assuming metre-sized assets."""
corners = []
for obj in context.view_layer.objects:
- if obj.type != 'MESH' or obj.hide_get():
+ if not _is_projection_mesh(obj):
continue
corners.extend(obj.matrix_world @ Vector(corner) for corner in obj.bound_box)
if not corners:
@@ -120,14 +185,18 @@ def _bake_visibility_weights(
filmw = tan(fov / 2)
aspect = cam_res_x / cam_res_y
filmh = filmw / aspect
+ camera_type = camera.data.type
+ ortho_half_h = max(float(camera.data.ortho_scale) * 0.5, 1e-8)
+ ortho_half_w = ortho_half_h * aspect
cam_data.append((
np.array(cam_loc, dtype=np.float64),
np.array(cam_dir, dtype=np.float64),
np.array(cam_right, dtype=np.float64),
np.array(cam_up_vec, dtype=np.float64),
- filmw, filmh,
+ filmw, filmh, float(camera.data.shift_x), float(camera.data.shift_y),
cam_loc, # keep mathutils Vector for BVH calls
+ camera_type, ortho_half_w, ortho_half_h,
))
# Build scene-wide BVH for occlusion (matches ShaderNodeRaycast behaviour,
@@ -136,7 +205,7 @@ def _bake_visibility_weights(
scene_bm = bmesh.new()
vert_offset = 0
for scene_obj in context.view_layer.objects:
- if scene_obj.type != 'MESH' or scene_obj.hide_get():
+ if not _is_projection_mesh(scene_obj):
continue
tmp = bmesh.new()
evaluated_obj = scene_obj.evaluated_get(depsgraph)
@@ -205,26 +274,42 @@ def _bake_visibility_weights(
attr.data.foreach_set("value", weights.tolist())
continue
- cam_loc_np, cam_dir_np, cam_right_np, cam_up_np, filmw, filmh, cam_loc_mu = cam_data[cam_idx]
+ (cam_loc_np, cam_dir_np, cam_right_np, cam_up_np, filmw, filmh,
+ shift_x, shift_y, cam_loc_mu, camera_type,
+ ortho_half_w, ortho_half_h) = cam_data[cam_idx]
# ── Vectorised frustum + angle pre-filter ──────────────────
to_vert = positions_np - cam_loc_np # (N, 3)
- dists = np.linalg.norm(to_vert, axis=1) # (N,)
- valid = dists > 1e-8
- direction = np.zeros_like(to_vert)
- direction[valid] = to_vert[valid] / dists[valid, None]
-
- # Frustum: d_neg_fwd < 0 (vertex is in front of camera)
- d_neg_fwd = np.einsum('ij,j->i', direction, -cam_dir_np)
- valid &= d_neg_fwd < 0
-
- neg_cam_z = np.where(valid, -d_neg_fwd, 1.0) # avoid /0
- d_right = np.einsum('ij,j->i', direction, cam_right_np)
- d_up = np.einsum('ij,j->i', direction, cam_up_np)
- x_proj = d_right / neg_cam_z
- y_proj = d_up / neg_cam_z
-
- valid &= (np.abs(x_proj) < filmw) & (np.abs(y_proj) < filmh)
+ if camera_type == 'ORTHO':
+ dists = np.einsum('ij,j->i', to_vert, cam_dir_np)
+ valid = dists > 1e-8
+ direction = np.repeat(cam_dir_np[None, :], n_verts, axis=0)
+ x_proj = np.einsum('ij,j->i', to_vert, cam_right_np)
+ y_proj = np.einsum('ij,j->i', to_vert, cam_up_np)
+ x_centered = x_proj / ortho_half_w - 2.0 * shift_x
+ y_centered = y_proj / ortho_half_h - 2.0 * shift_y
+ valid &= (np.abs(x_centered) < 1.0) & (np.abs(y_centered) < 1.0)
+ elif camera_type == 'PERSP':
+ dists = np.linalg.norm(to_vert, axis=1)
+ valid = dists > 1e-8
+ direction = np.zeros_like(to_vert)
+ direction[valid] = to_vert[valid] / dists[valid, None]
+ d_neg_fwd = np.einsum('ij,j->i', direction, -cam_dir_np)
+ valid &= d_neg_fwd < 0
+ neg_cam_z = np.where(valid, -d_neg_fwd, 1.0)
+ d_right = np.einsum('ij,j->i', direction, cam_right_np)
+ d_up = np.einsum('ij,j->i', direction, cam_up_np)
+ x_proj = d_right / neg_cam_z
+ y_proj = d_up / neg_cam_z
+ x_centered = x_proj / max(filmw, 1e-12) - 2.0 * shift_x
+ y_centered = y_proj / max(filmh, 1e-12) - 2.0 * shift_y
+ valid &= (np.abs(x_centered) < 1.0) & (np.abs(y_centered) < 1.0)
+ else:
+ valid = np.zeros(n_verts, dtype=bool)
+ dists = np.ones(n_verts, dtype=np.float64)
+ direction = np.repeat(cam_dir_np[None, :], n_verts, axis=0)
+ x_centered = np.zeros(n_verts)
+ y_centered = np.zeros(n_verts)
# Angle threshold
raw_dot = np.einsum('ij,ij->i', direction, normals_np)
@@ -244,8 +329,13 @@ def _bake_visibility_weights(
ray_epsilon = _scene_ray_epsilon(context)
for vi in candidate_indices:
d_mu = Vector(direction[vi])
+ ray_origin = cam_loc_mu
+ if camera_type == 'ORTHO':
+ ray_origin = Vector(
+ positions_np[vi] - cam_dir_np * (dists[vi] + ray_epsilon * 10.0)
+ )
hit_loc, _, _, hit_dist = scene_tree.ray_cast(
- cam_loc_mu, d_mu, float(dists[vi]) + ray_epsilon * 10.0)
+ ray_origin, d_mu, float(dists[vi]) + ray_epsilon * 20.0)
if hit_loc is not None and dists[vi] > hit_dist + ray_epsilon:
occluded[vi] = True
@@ -288,15 +378,39 @@ def _bake_visibility_weights(
dist_v = float(dists[vi])
is_edge = False
for off in all_offsets:
- d_off = d_base + off
+ d_off = d_base + off if camera_type == 'PERSP' else d_base
# Normalise (offset is small, so direction barely changes)
norm = float(np.linalg.norm(d_off))
if norm < 1e-8:
continue
d_off_mu = Vector(d_off / norm)
+ offset_origin = cam_loc_mu
+ if camera_type == 'ORTHO':
+ world_per_pixel = (2.0 * ortho_half_w) / max(cam_res_x_n, 1)
+ offset_world = off / max(offset_angle, 1e-12) * (
+ world_per_pixel * silhouette_margin_px
+ )
+ offset_origin = Vector(
+ positions_np[vi] - cam_dir_np * (dist_v + ray_epsilon * 10.0)
+ + offset_world
+ )
h_loc, _, _, h_dist = scene_tree.ray_cast(
- cam_loc_mu, d_off_mu, dist_v + ray_epsilon * 10.0)
- depth_thr = getattr(scene, 'sg_silhouette_depth', 0.05)
+ offset_origin, d_off_mu, dist_v + ray_epsilon * 20.0)
+ scene_scale = _scene_projection_scale(context)
+ relative_depth = getattr(scene, 'sg_silhouette_depth', 0.01)
+ if camera_type == 'ORTHO':
+ pixel_footprint = (
+ (2.0 * ortho_half_w) / max(cam_res_x_n, 1)
+ * max(silhouette_margin_px, 1)
+ )
+ else:
+ pixel_footprint = (
+ dist_v * rad_per_px * max(silhouette_margin_px, 1)
+ )
+ depth_thr = max(
+ scene_scale * relative_depth,
+ pixel_footprint * 0.5,
+ )
if h_loc is not None and dist_v > h_dist + depth_thr:
is_edge = True
break
@@ -306,11 +420,34 @@ def _bake_visibility_weights(
valid &= ~near_silhouette
# ── Vectorised weight + feather ────────────────────────────
- w = np.power(np.maximum(dot_dn, 1e-12), power_exponent)
+ # Projected density estimates how many camera pixels describe a
+ # model-scale span at this depth. It penalizes far/stretched
+ # coverage while remaining dimensionless across scene scales.
+ cam_res_x_n, cam_res_y_n = _get_camera_resolution(camera, scene)
+ if camera_type == 'ORTHO':
+ projected_span = np.full(
+ n_verts,
+ _scene_projection_scale(context) * cam_res_x_n
+ / max(2.0 * ortho_half_w, 1e-8),
+ )
+ else:
+ focal_pixels = cam_res_x_n / max(2.0 * filmw, 1e-12)
+ projected_span = (
+ _scene_projection_scale(context) * focal_pixels
+ / np.maximum(dists, 1e-8)
+ )
+ density_confidence = np.clip(
+ projected_span / max(0.5 * min(cam_res_x_n, cam_res_y_n), 1.0),
+ 0.0,
+ 1.0,
+ )
+ # Store unsharpened acquisition quality. The material owns the
+ # single authoritative SharpW exponent stage.
+ w = np.maximum(dot_dn, 0.0) * density_confidence
if feather_width > 0:
- nx = np.abs(x_proj) / filmw
- ny = np.abs(y_proj) / filmh
+ nx = np.abs(x_centered)
+ ny = np.abs(y_centered)
edge = np.maximum(nx, ny)
low = 1.0 - feather_width
need_feather = edge > low
@@ -507,10 +644,28 @@ def create_native_raycast_visibility(nodes, links, camera, geometry, context, i,
links.new(dot_d_up.outputs["Value"], y_proj.inputs[0])
links.new(neg_cam_z.outputs[0], y_proj.inputs[1])
+ shift_x_amount = nodes.new("ShaderNodeMath")
+ shift_x_amount.operation = 'MULTIPLY'
+ shift_x_amount.inputs[1].default_value = 2.0 * float(camera.data.shift_x)
+ links.new(filmw.outputs[0], shift_x_amount.inputs[0])
+ centered_x = nodes.new("ShaderNodeMath")
+ centered_x.operation = 'SUBTRACT'
+ links.new(x_proj.outputs[0], centered_x.inputs[0])
+ links.new(shift_x_amount.outputs[0], centered_x.inputs[1])
+
+ shift_y_amount = nodes.new("ShaderNodeMath")
+ shift_y_amount.operation = 'MULTIPLY'
+ shift_y_amount.inputs[1].default_value = 2.0 * float(camera.data.shift_y)
+ links.new(filmh.outputs[0], shift_y_amount.inputs[0])
+ centered_y = nodes.new("ShaderNodeMath")
+ centered_y.operation = 'SUBTRACT'
+ links.new(y_proj.outputs[0], centered_y.inputs[0])
+ links.new(shift_y_amount.outputs[0], centered_y.inputs[1])
+
abs_x = nodes.new("ShaderNodeMath")
abs_x.operation = 'ABSOLUTE'
abs_x.location = (0, y_base - 200)
- links.new(x_proj.outputs[0], abs_x.inputs[0])
+ links.new(centered_x.outputs[0], abs_x.inputs[0])
x_in_bounds = nodes.new("ShaderNodeMath")
x_in_bounds.operation = 'LESS_THAN'
@@ -521,7 +676,7 @@ def create_native_raycast_visibility(nodes, links, camera, geometry, context, i,
abs_y = nodes.new("ShaderNodeMath")
abs_y.operation = 'ABSOLUTE'
abs_y.location = (0, y_base - 300)
- links.new(y_proj.outputs[0], abs_y.inputs[0])
+ links.new(centered_y.outputs[0], abs_y.inputs[0])
y_in_bounds = nodes.new("ShaderNodeMath")
y_in_bounds.operation = 'LESS_THAN'
@@ -666,7 +821,7 @@ def create_native_raycast_visibility(nodes, links, camera, geometry, context, i,
def create_native_feather(nodes, links, normalize_node, camera_fov_node, camera_aspect_node,
- camera_dir_node, camera_up_node, context, i, mat_id):
+ camera_dir_node, camera_up_node, camera, context, i, mat_id):
"""
Creates a native node equivalent of feather.osl for Blender 5.1+.
Computes edge feathering based on distance to camera frustum border.
@@ -778,11 +933,29 @@ def create_native_feather(nodes, links, normalize_node, camera_fov_node, camera_
links.new(dot_d_up.outputs["Value"], y_proj.inputs[0])
links.new(neg_cam_z.outputs[0], y_proj.inputs[1])
+ shift_x_amount = nodes.new("ShaderNodeMath")
+ shift_x_amount.operation = 'MULTIPLY'
+ shift_x_amount.inputs[1].default_value = 2.0 * float(camera.data.shift_x)
+ links.new(filmw.outputs[0], shift_x_amount.inputs[0])
+ centered_x = nodes.new("ShaderNodeMath")
+ centered_x.operation = 'SUBTRACT'
+ links.new(x_proj.outputs[0], centered_x.inputs[0])
+ links.new(shift_x_amount.outputs[0], centered_x.inputs[1])
+
+ shift_y_amount = nodes.new("ShaderNodeMath")
+ shift_y_amount.operation = 'MULTIPLY'
+ shift_y_amount.inputs[1].default_value = 2.0 * float(camera.data.shift_y)
+ links.new(filmh.outputs[0], shift_y_amount.inputs[0])
+ centered_y = nodes.new("ShaderNodeMath")
+ centered_y.operation = 'SUBTRACT'
+ links.new(y_proj.outputs[0], centered_y.inputs[0])
+ links.new(shift_y_amount.outputs[0], centered_y.inputs[1])
+
# nx = abs(x_proj) / filmw
abs_x = nodes.new("ShaderNodeMath")
abs_x.operation = 'ABSOLUTE'
abs_x.location = (x_base + 300, y_base - 100)
- links.new(x_proj.outputs[0], abs_x.inputs[0])
+ links.new(centered_x.outputs[0], abs_x.inputs[0])
nx = nodes.new("ShaderNodeMath")
nx.operation = 'DIVIDE'
@@ -794,7 +967,7 @@ def create_native_feather(nodes, links, normalize_node, camera_fov_node, camera_
abs_y = nodes.new("ShaderNodeMath")
abs_y.operation = 'ABSOLUTE'
abs_y.location = (x_base + 300, y_base - 200)
- links.new(y_proj.outputs[0], abs_y.inputs[0])
+ links.new(centered_y.outputs[0], abs_y.inputs[0])
ny = nodes.new("ShaderNodeMath")
ny.operation = 'DIVIDE'
@@ -868,7 +1041,14 @@ def _get_voronoi_placeholder(color=(1.0, 0.0, 1.0)):
return img
-def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None):
+def project_image(
+ context,
+ to_project,
+ mat_id,
+ stop_index=1000000,
+ cameras=None,
+ view_plan=None,
+):
"""
Projects an image onto all mesh objects using UV Project Modifier.
"""
@@ -1098,12 +1278,66 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
user_exponent = pn.inputs[1].default_value
pn.inputs[1].default_value = 1.0
+ # Runtime acquisition wrapper: angle output × projected
+ # density. Traverse to its angle source so normalization can
+ # still neutralize the base exponent.
+ elif (node.type == 'MATH' and node.operation == 'MULTIPLY'
+ and node.label.startswith('AcquisitionQuality-')):
+ pending = [node]
+ visited = set()
+ while pending:
+ upstream = pending.pop()
+ pointer = upstream.as_pointer()
+ if pointer in visited:
+ continue
+ visited.add(pointer)
+ if (upstream.type == 'MATH'
+ and upstream.operation == 'POWER'
+ and upstream.label == 'power_weight'):
+ if user_exponent is None:
+ user_exponent = upstream.inputs[1].default_value
+ upstream.inputs[1].default_value = 1.0
+ break
+ if upstream.type == 'SCRIPT' and "Power" in upstream.inputs:
+ if user_exponent is None:
+ user_exponent = upstream.inputs["Power"].default_value
+ upstream.inputs["Power"].default_value = 1.0
+ break
+ for socket in upstream.inputs:
+ pending.extend(link.from_node for link in socket.links)
+
# OSL path: ShaderNodeScript with "Power" input
elif node.type == 'SCRIPT' and "Power" in node.inputs:
if user_exponent is None:
user_exponent = node.inputs["Power"].default_value
node.inputs["Power"].default_value = 1.0
+ # Masks/guards may wrap the acquisition node. Always scan
+ # upstream so every projection path reaches the same single
+ # SharpW exponent stage.
+ pending = [node]
+ visited = set()
+ while pending:
+ upstream = pending.pop()
+ pointer = upstream.as_pointer()
+ if pointer in visited:
+ continue
+ visited.add(pointer)
+ if (upstream.type == 'MATH'
+ and upstream.operation == 'POWER'
+ and upstream.label == 'power_weight'):
+ if user_exponent is None:
+ user_exponent = upstream.inputs[1].default_value
+ upstream.inputs[1].default_value = 1.0
+ break
+ if upstream.type == 'SCRIPT' and "Power" in upstream.inputs:
+ if user_exponent is None:
+ user_exponent = upstream.inputs["Power"].default_value
+ upstream.inputs["Power"].default_value = 1.0
+ break
+ for socket in upstream.inputs:
+ pending.extend(link.from_node for link in socket.links)
+
if user_exponent is None:
user_exponent = 1.0
@@ -1113,31 +1347,11 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
v_off = -200 * idx
all_w_outputs.append(_get_weight_output(weight_nodes[idx], v_off))
- # Apply initial-view priority once, globally, before max
- # normalization. The old recursive Map Range changed meaning as
- # cameras were paired, so results depended on camera count/tree
- # shape rather than only on view rank.
- if context.scene.early_priority:
- from .view_plan import initial_view_prior
- ranked_outputs = []
- for idx, weight_output in enumerate(all_w_outputs):
- prior = nodes_collection.new("ShaderNodeMath")
- prior.operation = 'MULTIPLY'
- prior.location = (x_offset - 1700, y_offset - 120 * idx)
- prior.label = f"InitialViewPrior-{idx}"
- desired_prior = initial_view_prior(
- idx, len(all_w_outputs), context.scene.early_priority_strength
- )
- prior["sg_desired_prior"] = desired_prior
- # This node sits before SharpW. Apply the inverse exponent
- # so the requested rank prior remains bounded after
- # sharpening (especially for Voronoi exponents near 1000).
- prior.inputs[1].default_value = desired_prior ** (
- 1.0 / max(float(user_exponent), 1e-6)
- )
- links.new(weight_output, prior.inputs[0])
- ranked_outputs.append(prior.outputs[0])
- all_w_outputs = ranked_outputs
+ # Keep these unmodified source-camera acquisition qualities for
+ # ownership. Initial-view priority is applied later only as
+ # replacement resistance for a trusted owner; it must never turn
+ # poor grazing coverage into trusted context.
+ acquisition_outputs = list(all_w_outputs)
# ── Step 3: Find per-pixel maximum ────────────────────────
max_node = nodes_collection.new("ShaderNodeMath")
@@ -1197,46 +1411,102 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
# Replace weight_nodes with sharpened-normalised nodes
weight_nodes = powered_nodes
- if getattr(context.scene, 'sg_texel_ownership', False):
- # Sequential hard ownership with hysteresis:
- # replace only when new_confidence > owner + margin. An
- # uncovered texel has owner confidence 0 and is accepted by
- # any positive new confidence.
+ use_confidence_ownership = (
+ getattr(context.scene, 'sg_texel_ownership', False)
+ or (
+ context.scene.model_architecture == 'qwen_image_edit'
+ and context.scene.generation_method == 'sequential'
+ and getattr(context.scene, 'qwen_use_projection_write_masks', True)
+ )
+ )
+ legacy_blend = getattr(
+ context.scene, 'sg_ownership_blend_mode', 'OWNER'
+ ) == 'LEGACY'
+ if use_confidence_ownership and not legacy_blend:
+ # Sequential single-owner detail. Untrusted owners remain
+ # replaceable; trusted owners lock unless explicit quality
+ # upgrades are enabled.
owner_color = shaders[0]
- owner_weight = weight_nodes[0]
+ owner_low = lowpass_tex_image_nodes[0]
+ seam_low = owner_low
+ owner_quality = acquisition_outputs[0]
margin = max(
0.0,
float(getattr(context.scene, 'sg_texel_replacement_margin', 0.05)),
)
+ trust_threshold = float(getattr(
+ context.scene, 'qwen_trust_threshold', 0.7
+ ))
+ allow_upgrade = bool(getattr(
+ context.scene, 'sg_allow_trusted_replacement', False
+ ))
+ priority_strength = (
+ float(getattr(context.scene, 'early_priority_strength', 0.5))
+ if getattr(context.scene, 'early_priority', False) else 0.0
+ )
+
+ owner_camera = nodes_collection.new("ShaderNodeValue")
+ owner_camera.outputs[0].default_value = 0.0
+ owner_camera.label = "SGOwnerCamera-0"
+ owner_priority = nodes_collection.new("ShaderNodeValue")
+ owner_priority.outputs[0].default_value = priority_strength * 0.25
+ owner_priority.label = "SGOwnerPriority-0"
+
for idx in range(1, len(shaders)):
+ new_quality = acquisition_outputs[idx]
+ old_owner_low = owner_low
+
+ owner_trusted = nodes_collection.new("ShaderNodeMath")
+ owner_trusted.operation = 'GREATER_THAN'
+ owner_trusted.inputs[1].default_value = trust_threshold - 1e-7
+ owner_trusted.label = f"OwnerTrusted-{idx}"
+ links.new(owner_quality, owner_trusted.inputs[0])
+
+ owner_untrusted = nodes_collection.new("ShaderNodeMath")
+ owner_untrusted.operation = 'SUBTRACT'
+ owner_untrusted.inputs[0].default_value = 1.0
+ links.new(owner_trusted.outputs[0], owner_untrusted.inputs[1])
+
+ candidate_valid = nodes_collection.new("ShaderNodeMath")
+ candidate_valid.operation = 'GREATER_THAN'
+ candidate_valid.inputs[1].default_value = 0.0
+ links.new(new_quality, candidate_valid.inputs[0])
+
+ replace_untrusted = nodes_collection.new("ShaderNodeMath")
+ replace_untrusted.operation = 'MULTIPLY'
+ links.new(owner_untrusted.outputs[0], replace_untrusted.inputs[0])
+ links.new(candidate_valid.outputs[0], replace_untrusted.inputs[1])
+
margin_node = nodes_collection.new("ShaderNodeMath")
margin_node.operation = 'ADD'
margin_node.location = (x_offset - 200, y_offset - 180 * idx)
margin_node.inputs[1].default_value = margin
margin_node.label = f"OwnerMargin-{idx}"
- links.new(owner_weight.outputs[0], margin_node.inputs[0])
+ links.new(owner_quality, margin_node.inputs[0])
+
+ priority_margin = nodes_collection.new("ShaderNodeMath")
+ priority_margin.operation = 'ADD'
+ links.new(margin_node.outputs[0], priority_margin.inputs[0])
+ links.new(owner_priority.outputs[0], priority_margin.inputs[1])
replace = nodes_collection.new("ShaderNodeMath")
replace.operation = 'GREATER_THAN'
replace.location = (x_offset, y_offset - 180 * idx)
replace.label = f"OwnerReplace-{idx}"
- links.new(weight_nodes[idx].outputs[0], replace.inputs[0])
- links.new(margin_node.outputs[0], replace.inputs[1])
+ links.new(new_quality, replace.inputs[0])
+ links.new(priority_margin.outputs[0], replace.inputs[1])
- owner_empty = nodes_collection.new("ShaderNodeMath")
- owner_empty.operation = 'COMPARE'
- owner_empty.location = (x_offset, y_offset - 180 * idx - 70)
- owner_empty.inputs[1].default_value = 0.0
- owner_empty.inputs[2].default_value = 0.0
- owner_empty.label = f"OwnerEmpty-{idx}"
- links.new(owner_weight.outputs[0], owner_empty.inputs[0])
+ replace_trusted = nodes_collection.new("ShaderNodeMath")
+ replace_trusted.operation = 'MULTIPLY'
+ replace_trusted.inputs[1].default_value = 1.0 if allow_upgrade else 0.0
+ links.new(replace.outputs[0], replace_trusted.inputs[0])
replace_or_empty = nodes_collection.new("ShaderNodeMath")
replace_or_empty.operation = 'MAXIMUM'
replace_or_empty.location = (x_offset + 100, y_offset - 180 * idx)
replace_or_empty.label = f"OwnerAccept-{idx}"
- links.new(replace.outputs[0], replace_or_empty.inputs[0])
- links.new(owner_empty.outputs[0], replace_or_empty.inputs[1])
+ links.new(replace_untrusted.outputs[0], replace_or_empty.inputs[0])
+ links.new(replace_trusted.outputs[0], replace_or_empty.inputs[1])
select_color = nodes_collection.new("ShaderNodeMixRGB")
select_color.location = (x_offset + 200, y_offset - 180 * idx)
@@ -1245,11 +1515,56 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
links.new(owner_color.outputs[0], select_color.inputs["Color1"])
links.new(shaders[idx].outputs[0], select_color.inputs["Color2"])
+ select_low = nodes_collection.new("ShaderNodeMixRGB")
+ select_low.label = f"LowFrequencyOwner-{idx}"
+ links.new(replace_or_empty.outputs[0], select_low.inputs["Fac"])
+ links.new(seam_low.outputs[0], select_low.inputs["Color1"])
+ links.new(lowpass_tex_image_nodes[idx].outputs[0], select_low.inputs["Color2"])
+
+ if getattr(context.scene, 'sg_ownership_blend_mode', 'OWNER') == 'OWNER':
+ difference = nodes_collection.new("ShaderNodeMath")
+ difference.operation = 'SUBTRACT'
+ links.new(new_quality, difference.inputs[0])
+ links.new(owner_quality, difference.inputs[1])
+ absolute_difference = nodes_collection.new("ShaderNodeMath")
+ absolute_difference.operation = 'ABSOLUTE'
+ links.new(difference.outputs[0], absolute_difference.inputs[0])
+ near_boundary = nodes_collection.new("ShaderNodeMath")
+ near_boundary.operation = 'LESS_THAN'
+ near_boundary.inputs[1].default_value = float(getattr(
+ context.scene, 'sg_ownership_seam_width', 0.08
+ ))
+ links.new(absolute_difference.outputs[0], near_boundary.inputs[0])
+ candidate_trusted = nodes_collection.new("ShaderNodeMath")
+ candidate_trusted.operation = 'GREATER_THAN'
+ candidate_trusted.inputs[1].default_value = trust_threshold - 1e-7
+ links.new(new_quality, candidate_trusted.inputs[0])
+ both_trusted = nodes_collection.new("ShaderNodeMath")
+ both_trusted.operation = 'MULTIPLY'
+ links.new(owner_trusted.outputs[0], both_trusted.inputs[0])
+ links.new(candidate_trusted.outputs[0], both_trusted.inputs[1])
+ seam_factor = nodes_collection.new("ShaderNodeMath")
+ seam_factor.operation = 'MULTIPLY'
+ links.new(near_boundary.outputs[0], seam_factor.inputs[0])
+ links.new(both_trusted.outputs[0], seam_factor.inputs[1])
+ low_average = nodes_collection.new("ShaderNodeMixRGB")
+ low_average.inputs["Fac"].default_value = 0.5
+ links.new(owner_low.outputs[0], low_average.inputs["Color1"])
+ links.new(lowpass_tex_image_nodes[idx].outputs[0], low_average.inputs["Color2"])
+ blended_seam_low = nodes_collection.new("ShaderNodeMixRGB")
+ blended_seam_low.label = f"LowFrequencySeam-{idx}"
+ links.new(seam_factor.outputs[0], blended_seam_low.inputs["Fac"])
+ links.new(select_low.outputs[0], blended_seam_low.inputs["Color1"])
+ links.new(low_average.outputs[0], blended_seam_low.inputs["Color2"])
+ next_seam_low = blended_seam_low
+ else:
+ next_seam_low = select_low
+
confidence_delta = nodes_collection.new("ShaderNodeMath")
confidence_delta.operation = 'SUBTRACT'
confidence_delta.location = (x_offset + 200, y_offset - 180 * idx - 90)
- links.new(weight_nodes[idx].outputs[0], confidence_delta.inputs[0])
- links.new(owner_weight.outputs[0], confidence_delta.inputs[1])
+ links.new(new_quality, confidence_delta.inputs[0])
+ links.new(owner_quality, confidence_delta.inputs[1])
selected_delta = nodes_collection.new("ShaderNodeMath")
selected_delta.operation = 'MULTIPLY'
selected_delta.location = (x_offset + 350, y_offset - 180 * idx - 90)
@@ -1259,10 +1574,69 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
selected_confidence.operation = 'ADD'
selected_confidence.location = (x_offset + 500, y_offset - 180 * idx - 90)
selected_confidence.label = f"OwnerConfidence-{idx}"
- links.new(owner_weight.outputs[0], selected_confidence.inputs[0])
+ links.new(owner_quality, selected_confidence.inputs[0])
links.new(selected_delta.outputs[0], selected_confidence.inputs[1])
+
+ camera_delta = nodes_collection.new("ShaderNodeMath")
+ camera_delta.operation = 'SUBTRACT'
+ camera_delta.inputs[0].default_value = float(idx)
+ links.new(owner_camera.outputs[0], camera_delta.inputs[1])
+ selected_camera_delta = nodes_collection.new("ShaderNodeMath")
+ selected_camera_delta.operation = 'MULTIPLY'
+ links.new(camera_delta.outputs[0], selected_camera_delta.inputs[0])
+ links.new(replace_or_empty.outputs[0], selected_camera_delta.inputs[1])
+ selected_camera = nodes_collection.new("ShaderNodeMath")
+ selected_camera.operation = 'ADD'
+ selected_camera.label = f"SGOwnerCamera-{idx}"
+ links.new(owner_camera.outputs[0], selected_camera.inputs[0])
+ links.new(selected_camera_delta.outputs[0], selected_camera.inputs[1])
+
+ candidate_priority = priority_strength * 0.25 * (
+ 1.0 - idx / float(max(len(shaders) - 1, 1))
+ )
+ priority_delta = nodes_collection.new("ShaderNodeMath")
+ priority_delta.operation = 'SUBTRACT'
+ priority_delta.inputs[0].default_value = candidate_priority
+ links.new(owner_priority.outputs[0], priority_delta.inputs[1])
+ selected_priority_delta = nodes_collection.new("ShaderNodeMath")
+ selected_priority_delta.operation = 'MULTIPLY'
+ links.new(priority_delta.outputs[0], selected_priority_delta.inputs[0])
+ links.new(replace_or_empty.outputs[0], selected_priority_delta.inputs[1])
+ selected_priority = nodes_collection.new("ShaderNodeMath")
+ selected_priority.operation = 'ADD'
+ selected_priority.label = f"SGOwnerPriority-{idx}"
+ links.new(owner_priority.outputs[0], selected_priority.inputs[0])
+ links.new(selected_priority_delta.outputs[0], selected_priority.inputs[1])
+
owner_color = select_color
- owner_weight = selected_confidence
+ owner_low = nodes_collection.new("ShaderNodeMixRGB")
+ owner_low.label = f"SelectedOwnerLow-{idx}"
+ links.new(replace_or_empty.outputs[0], owner_low.inputs["Fac"])
+ links.new(old_owner_low.outputs[0], owner_low.inputs["Color1"])
+ links.new(lowpass_tex_image_nodes[idx].outputs[0], owner_low.inputs["Color2"])
+ seam_low = next_seam_low
+ owner_quality = selected_confidence.outputs[0]
+ owner_camera = selected_camera
+ owner_priority = selected_priority
+
+ selected_confidence.label = "SGOwnerQuality"
+ selected_camera.label = "SGOwnerCamera"
+
+ if getattr(context.scene, 'sg_ownership_blend_mode', 'OWNER') == 'OWNER':
+ detail = nodes_collection.new("ShaderNodeMixRGB")
+ detail.blend_type = 'SUBTRACT'
+ detail.inputs["Fac"].default_value = 1.0
+ detail.label = "OwnedHighFrequencyDetail"
+ links.new(owner_color.outputs[0], detail.inputs["Color1"])
+ links.new(owner_low.outputs[0], detail.inputs["Color2"])
+ recompose = nodes_collection.new("ShaderNodeMixRGB")
+ recompose.blend_type = 'ADD'
+ recompose.inputs["Fac"].default_value = 1.0
+ recompose.use_clamp = True
+ recompose.label = "LowFrequencySeamComposite"
+ links.new(detail.outputs[0], recompose.inputs["Color1"])
+ links.new(seam_low.outputs[0], recompose.inputs["Color2"])
+ owner_color = recompose
final_mix = nodes_collection.new("ShaderNodeMixRGB")
final_mix.location = (x_offset + 450, y_offset)
@@ -1277,9 +1651,9 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
uncovered.inputs[1].default_value = 0.0
uncovered.inputs[2].default_value = 0.0
uncovered.label = "OwnerUncovered"
- links.new(owner_weight.outputs[0], uncovered.inputs[0])
+ links.new(owner_quality, uncovered.inputs[0])
links.new(uncovered.outputs[0], final_mix.inputs["Fac"])
- return final_mix, owner_weight
+ return final_mix, selected_confidence
new_shaders = []
new_weight_nodes = []
@@ -1373,7 +1747,17 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
i += 1
return build_mix_tree(new_shaders, new_weight_nodes, nodes_collection, links, last_node, level+1)
- if cameras is None:
+ if view_plan is not None:
+ cameras = []
+ for record in view_plan:
+ camera = record.camera
+ if record.matrix_world:
+ camera.matrix_world = Matrix(record.matrix_world)
+ for name, value in dict(record.intrinsics or {}).items():
+ if value is not None and hasattr(camera.data, name):
+ setattr(camera.data, name, value)
+ cameras.append(camera)
+ elif cameras is None:
from .view_plan import order_cameras
cameras = order_cameras(
obj for obj in context.scene.objects if obj.type == 'CAMERA'
@@ -1458,16 +1842,24 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
obj.data.uv_layers.remove(buffer_uv)
# ── Optional: bake per-vertex visibility weights ──────────────────
- use_baked_weights = getattr(context.scene, 'bake_visibility_weights', False)
+ use_baked_weights = (
+ getattr(context.scene, 'bake_visibility_weights', False)
+ or any(camera.data.type == 'ORTHO' for camera in cameras)
+ )
if use_baked_weights:
# Always compute weights for ALL cameras (ignore stop_index).
#
# Needs further testing, maybe will need to revert this.
- # In sequential mode, only bake on the first call (stop_index=0)
- # to avoid recomputing the same weights N times.
+ # In sequential mode reuse the first bake, but never assume it exists:
+ # regeneration, project-only runs, and orthographic imports can enter
+ # at an arbitrary stop index.
is_sequential = context.scene.generation_method == 'sequential'
- if not is_sequential or stop_index == 0:
+ missing_bake = any(
+ obj.data.attributes.get(f"_SG_VisWeight_{cam_idx}_{mat_id}") is None
+ for obj in to_project for cam_idx in range(len(cameras))
+ )
+ if not is_sequential or stop_index == 0 or missing_bake:
_bake_visibility_weights(context, to_project, cameras, mat_id)
# Switch to Cycles (needed for Raycast shader node on all versions)
@@ -1538,45 +1930,11 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
original_uv_map = obj.data.uv_layers[1]
- if (context.scene.generation_method == 'sequential' and stop_index > 0) or context.scene.generation_mode == 'regenerate_selected':
- # We just need to remove the compare nodes which are connected to script node at stop_index
- script_node = None
- # First find all script nodes with label Angle-{stop_index}-{mat_id} or {stop_index}-{mat_id} (legacy)
- # Also match native MATH MULTIPLY nodes (Blender 5.1+ native raycast path)
- for node in nodes:
- if (node.type == 'SCRIPT' or (node.type == 'MATH' and node.operation == 'MULTIPLY')) and (node.label == f"Angle-{stop_index}-{mat_id}" or node.label == f"{stop_index}-{mat_id}"):
- script_node = node
- break
- compare_output_sockets = set()
- if script_node:
- # Find all compare nodes connected to the script node
- for link in script_node.outputs[0].links:
- if link.to_node.type == 'MATH' and link.to_node.operation == 'LESS_THAN':
- # Save all outputs (nodes connected to) of compare nodes
- for link2 in link.to_node.outputs[0].links:
- compare_output_sockets.add(link2.to_socket)
- # Remove the compare node
- nodes.remove(link.to_node)
- # Connect the script node to all outputs
- if script_node:
- for output in compare_output_sockets:
- links.new(script_node.outputs[0], output)
- # We also need to set the generated image to the texture node with label {stop_index}-{mat_id}
- for node in nodes:
- if node.type == 'TEX_IMAGE' and node.label == f"{stop_index}-{mat_id}":
- image_path = get_file_path(context, "generated", camera_id=stop_index, material_id=mat_id)
- if (context.scene.generation_method == 'sequential' or context.scene.generation_method == 'separate') and context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_regenerate \
- and not context.scene.use_ipadapter and stop_index == 0 and context.scene.sequential_ipadapter_mode == 'first':
- image_path = get_file_path(context, "generated", camera_id=stop_index, material_id=mat_id).replace(".png", "_ipadapter.png")
-
- image = get_or_load_image(image_path, force_reload=context.scene.overwrite_material)
- if image:
- node.image = image
- break
- # Now we can continue to the next object
- continue
-
- elif not is_local_edit:
+ if not is_local_edit:
+ # Rebuild deterministically at every sequential step. Later views
+ # acquire write masks, projection guards, low-pass companions, and
+ # ownership confidence only after generation; patching the old
+ # graph left those inputs stale and could launder poor context.
# Clear existing nodes
for node in nodes:
nodes.remove(node)
@@ -1603,6 +1961,7 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
use_native_raycast = bpy.app.version >= (5, 1, 0)
tex_image_nodes = []
+ lowpass_tex_image_nodes = []
uv_map_nodes = []
subtract_nodes = []
normalize_nodes = []
@@ -1653,8 +2012,8 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
links.new(mask_tex.outputs[0], to_float.inputs[0])
return to_float
- def _create_qwen_write_mask_weight(cam_index, mat_id_inner):
- """Sample the binary repair mask saved for a sequential Qwen view."""
+ def _create_write_mask_weight(cam_index, mat_id_inner, binary=False):
+ """Sample the authoritative per-view sequential write mask."""
mask_path = get_file_path(
context,
"inpaint",
@@ -1674,8 +2033,8 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
mask_tex = nodes.new("ShaderNodeTexImage")
mask_tex.image = mask_image
mask_tex.extension = 'CLIP'
- mask_tex.interpolation = 'Closest'
- mask_tex.label = f"QwenWriteMask-{cam_index}-{mat_id_inner}"
+ mask_tex.interpolation = 'Closest' if binary else 'Linear'
+ mask_tex.label = f"SequentialWriteMask-{cam_index}-{mat_id_inner}"
mask_tex.location = (-500, -200 * cam_index - 300)
uv_node = uv_map_nodes[cam_index]
@@ -1686,10 +2045,39 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
to_float.operation = 'MULTIPLY'
to_float.inputs[1].default_value = 1.0
to_float.location = (-300, -200 * cam_index - 300)
- to_float.label = f"QwenWriteWeight-{cam_index}-{mat_id_inner}"
+ to_float.label = f"SequentialWriteWeight-{cam_index}-{mat_id_inner}"
links.new(mask_tex.outputs[0], to_float.inputs[0])
return to_float
+ def _create_projection_guard_weight(cam_index, mat_id_inner):
+ """Sample per-pixel depth/object/material silhouette rejection."""
+ guard_path = os.path.join(
+ get_dir_path(context, "inpaint")["visibility"],
+ f"projection_guard{cam_index}.png",
+ )
+ if not os.path.exists(guard_path):
+ return None
+ guard_image = get_or_load_image(
+ guard_path, force_reload=context.scene.overwrite_material
+ )
+ if guard_image is None:
+ return None
+ guard_image.colorspace_settings.name = 'Non-Color'
+ guard_tex = nodes.new("ShaderNodeTexImage")
+ guard_tex.image = guard_image
+ guard_tex.extension = 'CLIP'
+ guard_tex.interpolation = 'Linear'
+ guard_tex.label = f"ProjectionGuard-{cam_index}-{mat_id_inner}"
+ uv_node = uv_map_nodes[cam_index]
+ uv_out = "Vector" if uv_node.type == 'ATTRIBUTE' else "UV"
+ links.new(uv_node.outputs[uv_out], guard_tex.inputs["Vector"])
+ guard_weight = nodes.new("ShaderNodeMath")
+ guard_weight.operation = 'MULTIPLY'
+ guard_weight.inputs[1].default_value = 1.0
+ guard_weight.label = f"ProjectionGuardWeight-{cam_index}-{mat_id_inner}"
+ links.new(guard_tex.outputs[0], guard_weight.inputs[0])
+ return guard_weight
+
# Voronoi mode flag: keep natural weights for non-generated cameras
voronoi_active = (context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein')
and getattr(context.scene, 'qwen_voronoi_mode', False)
@@ -1719,6 +2107,27 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
tex_image.extension = 'CLIP'
tex_image.label = f"{i}-{mat_id}"
tex_image_nodes.append(tex_image)
+
+ lowpass_node = tex_image
+ if (getattr(context.scene, 'sg_ownership_blend_mode', 'OWNER') == 'OWNER'
+ and i <= stop_index and tex_image.image is not None):
+ source_path = bpy.path.abspath(tex_image.image.filepath)
+ lowpass_path = _ensure_lowpass_texture(
+ source_path,
+ getattr(context.scene, 'sg_ownership_lowpass_sigma', 8.0),
+ )
+ if lowpass_path:
+ lowpass_image = get_or_load_image(
+ lowpass_path,
+ force_reload=context.scene.overwrite_material,
+ )
+ if lowpass_image:
+ lowpass_node = nodes.new("ShaderNodeTexImage")
+ lowpass_node.image = lowpass_image
+ lowpass_node.extension = 'CLIP'
+ lowpass_node.label = f"LowPass-{i}-{mat_id}"
+ lowpass_node.location = (200, -200 * i)
+ lowpass_tex_image_nodes.append(lowpass_node)
# Add UV map / attribute node
# Use ShaderNodeAttribute to read from the corner-domain attribute
@@ -1741,12 +2150,19 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
vis_attr.location = (-400, (-800) * i)
vis_attr.label = f"BakedVis-{i}-{mat_id}"
+ power_weight = nodes.new("ShaderNodeMath")
+ power_weight.operation = 'POWER'
+ power_weight.inputs[1].default_value = context.scene.weight_exponent
+ power_weight.location = (-300, (-800) * i)
+ power_weight.label = 'power_weight'
+ links.new(vis_attr.outputs["Fac"], power_weight.inputs[0])
+
passthrough = nodes.new("ShaderNodeMath")
passthrough.operation = 'MULTIPLY'
- passthrough.inputs[1].default_value = 1.0
+ passthrough.inputs[0].default_value = 1.0
passthrough.location = (-200, (-800) * i)
passthrough.label = f"Angle-{i}-{mat_id}"
- links.new(vis_attr.outputs["Fac"], passthrough.inputs[0])
+ links.new(power_weight.outputs[0], passthrough.inputs[1])
scripts_to_connect = []
final_weight_node = passthrough
@@ -1788,7 +2204,7 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
if is_local_edit_mode:
feather_weight = create_native_feather(
nodes, links, normalize_node, camera_fov_node, camera_aspect_node,
- camera_dir_node, camera_up_node, context, i, mat_id
+ camera_dir_node, camera_up_node, camera, context, i, mat_id
)
# Optionally add edge-feather mask as third multiplier
if context.scene.refine_edge_feather_projection and i <= stop_index:
@@ -1801,6 +2217,19 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
final_weight_node = (angle_weight, feather_weight)
else:
final_weight_node = angle_weight
+ if (context.scene.generation_method == 'sequential'
+ and context.scene.visibility_vignette):
+ frame_weight = create_native_feather(
+ nodes, links, normalize_node, camera_fov_node,
+ camera_aspect_node, camera_dir_node, camera_up_node,
+ camera, context, i, mat_id,
+ )
+ frame_quality = nodes.new("ShaderNodeMath")
+ frame_quality.operation = 'MULTIPLY'
+ frame_quality.label = f"FrameEdgeQuality-{i}-{mat_id}"
+ links.new(angle_weight.outputs[0], frame_quality.inputs[0])
+ links.new(frame_weight.outputs[0], frame_quality.inputs[1])
+ final_weight_node = frame_quality
script_nodes.append(scripts_to_connect)
@@ -1819,11 +2248,11 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
subtract_nodes.append(None)
normalize_nodes.append(None)
add_camera_loc_nodes.append(None)
- length_nodes.append(None)
- camera_fov_nodes.append(None)
- camera_aspect_ratio_nodes.append(None)
- camera_direction_nodes.append(None)
- camera_up_nodes.append(None)
+ length_nodes.append(length_node)
+ camera_fov_nodes.append(camera_fov_node)
+ camera_aspect_ratio_nodes.append(camera_aspect_node)
+ camera_direction_nodes.append(camera_dir_node)
+ camera_up_nodes.append(camera_up_node)
else:
# ── OSL path (Blender < 5.1) ──
subtract = nodes.new("ShaderNodeVectorMath")
@@ -1860,12 +2289,25 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
)
if "RayEpsilon" in script_angle.inputs:
script_angle.inputs["RayEpsilon"].default_value = _scene_ray_epsilon(context)
+ if "CameraShiftX" in script_angle.inputs:
+ script_angle.inputs["CameraShiftX"].default_value = camera.data.shift_x
+ if "CameraShiftY" in script_angle.inputs:
+ script_angle.inputs["CameraShiftY"].default_value = camera.data.shift_y
script_angle.label = f"Angle-{i}-{mat_id}"
scripts_to_connect = [script_angle]
final_weight_node = None
- if (context.scene.generation_method == 'local_edit' or (context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method == 'local_edit')):
+ is_local_edit_mode = (
+ context.scene.generation_method == 'local_edit'
+ or (context.scene.model_architecture.startswith('qwen')
+ and context.scene.qwen_generation_method == 'local_edit')
+ )
+ needs_frame_quality = (
+ context.scene.generation_method == 'sequential'
+ and context.scene.visibility_vignette
+ )
+ if is_local_edit_mode or needs_frame_quality:
# Add Feather script
script_feather = nodes.new("ShaderNodeScript")
script_feather.location = (-400, (-800) * i - 200) # Offset slightly
@@ -1888,17 +2330,28 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
script_feather.inputs["EdgeFeather"].default_value = 0.0
script_feather.inputs["EdgeGamma"].default_value = 1.0
script_feather.label = f"Feather-{i}-{mat_id}"
+ if "CameraShiftX" in script_feather.inputs:
+ script_feather.inputs["CameraShiftX"].default_value = camera.data.shift_x
+ if "CameraShiftY" in script_feather.inputs:
+ script_feather.inputs["CameraShiftY"].default_value = camera.data.shift_y
scripts_to_connect.append(script_feather)
# Optionally add edge-feather mask as third multiplier
- if context.scene.refine_edge_feather_projection and i <= stop_index:
+ if is_local_edit_mode and context.scene.refine_edge_feather_projection and i <= stop_index:
ef_node = _create_edge_feather_weight(i, mat_id)
if ef_node is not None:
final_weight_node = (script_angle, script_feather, ef_node)
else:
final_weight_node = (script_angle, script_feather)
- else:
+ elif is_local_edit_mode:
final_weight_node = (script_angle, script_feather)
+ else:
+ frame_quality = nodes.new("ShaderNodeMath")
+ frame_quality.operation = 'MULTIPLY'
+ frame_quality.label = f"FrameEdgeQuality-{i}-{mat_id}"
+ links.new(script_angle.outputs[0], frame_quality.inputs[0])
+ links.new(script_feather.outputs[0], frame_quality.inputs[1])
+ final_weight_node = frame_quality
else:
# Just use Angle script output directly
final_weight_node = script_angle
@@ -1968,6 +2421,38 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
length.location = (-400, 200 * (i+1))
length_nodes.append(length)
+ # Add projected pixel density to live native/OSL paths. Baked
+ # acquisition attributes already contain this term.
+ if (not use_baked_weights and script_nodes_outputs
+ and not isinstance(script_nodes_outputs[-1], tuple)
+ and length_nodes[-1] is not None):
+ cam_res_x, cam_res_y = _get_camera_resolution(camera, context.scene)
+ fov_for_density = camera.data.angle_x
+ if cam_res_y > cam_res_x:
+ fov_for_density = 2 * atan(
+ tan(fov_for_density / 2) * cam_res_x / cam_res_y
+ )
+ focal_pixels = cam_res_x / max(2.0 * tan(fov_for_density / 2), 1e-7)
+ density_numerator = (
+ _scene_projection_scale(context) * focal_pixels
+ / max(0.5 * min(cam_res_x, cam_res_y), 1.0)
+ )
+ density = nodes.new("ShaderNodeMath")
+ density.operation = 'DIVIDE'
+ density.inputs[0].default_value = density_numerator
+ density.label = f"ProjectedDensity-{i}-{mat_id}"
+ links.new(length_nodes[-1].outputs[0], density.inputs[1])
+ clamp_density = nodes.new("ShaderNodeMath")
+ clamp_density.operation = 'MINIMUM'
+ clamp_density.inputs[1].default_value = 1.0
+ links.new(density.outputs[0], clamp_density.inputs[0])
+ acquisition = nodes.new("ShaderNodeMath")
+ acquisition.operation = 'MULTIPLY'
+ acquisition.label = f"AcquisitionQuality-{i}-{mat_id}"
+ links.new(script_nodes_outputs[-1].outputs[0], acquisition.inputs[0])
+ links.new(clamp_density.outputs[0], acquisition.inputs[1])
+ script_nodes_outputs[-1] = acquisition
+
use_qwen_write_mask = (
i > 0
and i <= stop_index
@@ -1977,10 +2462,18 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
and context.scene.qwen_context_render_mode in {'REPLACE_STYLE', 'ADDITIONAL'}
and getattr(context.scene, 'qwen_use_projection_write_masks', True)
)
- if use_qwen_write_mask and script_nodes_outputs:
+ use_sdxl_write_mask = (
+ i > 0
+ and i <= stop_index
+ and context.scene.model_architecture == 'sdxl'
+ and context.scene.generation_method == 'sequential'
+ )
+ if (use_qwen_write_mask or use_sdxl_write_mask) and script_nodes_outputs:
current_weight = script_nodes_outputs[-1]
if not isinstance(current_weight, tuple):
- write_weight = _create_qwen_write_mask_weight(i, mat_id)
+ write_weight = _create_write_mask_weight(
+ i, mat_id, binary=use_qwen_write_mask
+ )
if write_weight is not None:
masked_weight = nodes.new("ShaderNodeMath")
masked_weight.operation = 'MULTIPLY'
@@ -1990,6 +2483,25 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
links.new(write_weight.outputs[0], masked_weight.inputs[1])
script_nodes_outputs[-1] = masked_weight
+ if (use_sdxl_write_mask and script_nodes_outputs
+ and not isinstance(script_nodes_outputs[-1], tuple)):
+ alpha_weighted = nodes.new("ShaderNodeMath")
+ alpha_weighted.operation = 'MULTIPLY'
+ alpha_weighted.label = f"SDXLFeatherAlpha-{i}-{mat_id}"
+ links.new(script_nodes_outputs[-1].outputs[0], alpha_weighted.inputs[0])
+ links.new(tex_image_nodes[i].outputs["Alpha"], alpha_weighted.inputs[1])
+ script_nodes_outputs[-1] = alpha_weighted
+
+ if script_nodes_outputs and not isinstance(script_nodes_outputs[-1], tuple):
+ guard_weight = _create_projection_guard_weight(i, mat_id)
+ if guard_weight is not None:
+ guarded = nodes.new("ShaderNodeMath")
+ guarded.operation = 'MULTIPLY'
+ guarded.label = f"DepthPartGuarded-{i}-{mat_id}"
+ links.new(script_nodes_outputs[-1].outputs[0], guarded.inputs[0])
+ links.new(guard_weight.outputs[0], guarded.inputs[1])
+ script_nodes_outputs[-1] = guarded
+
# Build mix shader tree
if mat.name in processed_materials and is_local_edit:
# If we already processed this material, we don't need to rebuild the mix tree
@@ -2012,6 +2524,9 @@ def project_image(context, to_project, mat_id, stop_index=1000000, cameras=None)
# Connect UV → Texture (needed for both paths)
uv_output = "Vector" if uv_map_node.type == 'ATTRIBUTE' else "UV"
links.new(uv_map_node.outputs[uv_output], tex_image.inputs["Vector"])
+ lowpass_tex = lowpass_tex_image_nodes[i]
+ if lowpass_tex is not tex_image:
+ links.new(uv_map_node.outputs[uv_output], lowpass_tex.inputs["Vector"])
if use_baked_weights or use_native_raycast:
# Baked-weights path: all links already made during creation.
@@ -2161,6 +2676,32 @@ def get_or_load_image(filepath, force_reload=False):
return image
+def reset_projection_material_nodes(material, angle_threshold, exponent):
+ """Reset display controls without changing stored ownership confidence."""
+ if not material or not material.use_nodes:
+ return False
+ nodes = material.node_tree.nodes
+ has_sharp = any(
+ node.type == 'MATH' and node.operation == 'POWER'
+ and node.label.startswith('SharpW-') for node in nodes
+ )
+ for node in nodes:
+ if node.type == 'SCRIPT' and 'AngleThreshold' in node.inputs:
+ node.inputs['AngleThreshold'].default_value = angle_threshold
+ elif (node.type == 'MATH' and node.operation == 'LESS_THAN'
+ and node.label.startswith('AngleThreshold-')):
+ node.inputs[1].default_value = angle_threshold
+ elif (node.type == 'MATH' and node.operation == 'POWER'
+ and node.label.startswith('SharpW-')):
+ node.inputs[1].default_value = exponent
+ elif node.type == 'SCRIPT' and 'Power' in node.inputs:
+ node.inputs['Power'].default_value = 1.0 if has_sharp else exponent
+ elif (node.type == 'MATH' and node.operation == 'POWER'
+ and node.label == 'power_weight'):
+ node.inputs[1].default_value = 1.0 if has_sharp else exponent
+ return has_sharp
+
+
def reinstate_compare_nodes(context, to_project, stop_id_mat_id_pairs):
"""
Reinstates the 'LESS_THAN' compare nodes that were removed for sequential generation.
diff --git a/stablegen/texturing/quality_masks.py b/stablegen/texturing/quality_masks.py
index 24dd1b9..f6a9cd8 100644
--- a/stablegen/texturing/quality_masks.py
+++ b/stablegen/texturing/quality_masks.py
@@ -2,22 +2,191 @@
from __future__ import annotations
-import cv2
import numpy as np
+try:
+ import cv2
+except ImportError: # Blender's clean Python omits OpenCV; addon builds include it.
+ class _NumpyCV2Fallback:
+ """Small deterministic fallback for binary-mask operations and tests."""
+ COLOR_BGR2GRAY = 0
+ INTER_NEAREST = 0
+ INTER_LINEAR = 1
+ MORPH_ELLIPSE = 0
+ MORPH_RECT = 1
+ MORPH_CLOSE = 2
+ CC_STAT_AREA = 4
-def regularize_repair_mask(mask, threshold=127, dilation=0):
- """Return an exact uint8 binary repair mask (255 means editable)."""
+ @staticmethod
+ def cvtColor(image, _code):
+ return np.rint(np.mean(image[:, :, :3], axis=2)).astype(image.dtype)
+
+ @staticmethod
+ def resize(image, size, interpolation=0):
+ width, height = size
+ y = np.linspace(0, image.shape[0] - 1, height)
+ x = np.linspace(0, image.shape[1] - 1, width)
+ if interpolation == _NumpyCV2Fallback.INTER_NEAREST:
+ return image[np.rint(y).astype(int)[:, None], np.rint(x).astype(int)]
+ # Bilinear accuracy is unnecessary for binary policy fallback;
+ # nearest keeps masks and IDs categorical and deterministic.
+ return image[np.rint(y).astype(int)[:, None], np.rint(x).astype(int)]
+
+ @staticmethod
+ def getStructuringElement(shape, size):
+ width, height = size
+ if shape == _NumpyCV2Fallback.MORPH_RECT:
+ return np.ones((height, width), dtype=np.uint8)
+ yy, xx = np.ogrid[:height, :width]
+ cy, cx = (height - 1) / 2.0, (width - 1) / 2.0
+ ry, rx = max(height / 2.0, 0.5), max(width / 2.0, 0.5)
+ return (((yy - cy) / ry) ** 2 + ((xx - cx) / rx) ** 2 <= 1.0).astype(np.uint8)
+
+ @staticmethod
+ def _morph(image, kernel, maximum):
+ kh, kw = kernel.shape
+ py, px = kh // 2, kw // 2
+ pad_value = 0 if maximum else 255
+ padded = np.pad(image, ((py, py), (px, px)), constant_values=pad_value)
+ samples = []
+ for y, x in np.argwhere(kernel > 0):
+ samples.append(padded[y:y + image.shape[0], x:x + image.shape[1]])
+ stack = np.stack(samples)
+ return (np.max(stack, axis=0) if maximum else np.min(stack, axis=0)).astype(image.dtype)
+
+ @classmethod
+ def dilate(cls, image, kernel, iterations=1):
+ result = image
+ for _ in range(max(1, int(iterations))):
+ result = cls._morph(result, kernel, True)
+ return result
+
+ @classmethod
+ def erode(cls, image, kernel, iterations=1):
+ result = image
+ for _ in range(max(1, int(iterations))):
+ result = cls._morph(result, kernel, False)
+ return result
+
+ @classmethod
+ def morphologyEx(cls, image, operation, kernel):
+ if operation != cls.MORPH_CLOSE:
+ raise ValueError("Only binary close is supported without OpenCV")
+ return cls.erode(cls.dilate(image, kernel), kernel)
+
+ @classmethod
+ def connectedComponentsWithStats(cls, image, connectivity=8):
+ foreground = image > 0
+ labels = np.zeros(foreground.shape, dtype=np.int32)
+ component = 0
+ stats = [[0, 0, image.shape[1], image.shape[0], int(np.count_nonzero(~foreground))]]
+ offsets = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
+ if connectivity == 4:
+ offsets = [(-1, 0), (0, -1), (0, 1), (1, 0)]
+ for start_y, start_x in np.argwhere(foreground):
+ if labels[start_y, start_x]:
+ continue
+ component += 1
+ queue = [(int(start_y), int(start_x))]
+ labels[start_y, start_x] = component
+ points = []
+ while queue:
+ y, x = queue.pop()
+ points.append((y, x))
+ for dy, dx in offsets:
+ ny, nx = y + dy, x + dx
+ if (0 <= ny < foreground.shape[0] and 0 <= nx < foreground.shape[1]
+ and foreground[ny, nx] and labels[ny, nx] == 0):
+ labels[ny, nx] = component
+ queue.append((ny, nx))
+ ys, xs = zip(*points)
+ stats.append([min(xs), min(ys), max(xs) - min(xs) + 1,
+ max(ys) - min(ys) + 1, len(points)])
+ centroids = np.zeros((component + 1, 2), dtype=np.float64)
+ return component + 1, labels, np.asarray(stats, dtype=np.int32), centroids
+
+ cv2 = _NumpyCV2Fallback()
+
+
+def regularize_repair_mask(
+ mask,
+ threshold=127,
+ dilation=0,
+ min_island_area=0,
+ min_hole_area=0,
+ min_strip_width=0,
+ boundary_labels=None,
+ keep_largest_components=0,
+):
+ """Return a regularized exact binary repair mask (255 means editable).
+
+ Morphology can be constrained independently inside object/material labels,
+ preventing repair regions from bleeding across part boundaries. Thin
+ trusted strips are removed with a close operation on the repair side.
+ """
if mask is None:
raise ValueError("mask is required")
if mask.ndim == 3:
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
binary = np.where(mask > int(threshold), 255, 0).astype(np.uint8)
- dilation = max(0, int(dilation))
- if dilation:
- size = dilation * 2 + 1
- kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (size, size))
- binary = cv2.dilate(binary, kernel, iterations=1)
+
+ def _regularize_region(region_mask, region):
+ # Zero other parts before morphology. Masking only after dilation lets
+ # a hand/accessory region seed repair pixels across a dress/body part.
+ result = np.where(region, region_mask, 0).astype(np.uint8)
+ if min_island_area > 0:
+ # Small trusted islands are holes in the repair mask.
+ trusted = np.where(region & (result == 0), 255, 0).astype(np.uint8)
+ count, labels, stats, _ = cv2.connectedComponentsWithStats(trusted, 8)
+ for component in range(1, count):
+ if stats[component, cv2.CC_STAT_AREA] < int(min_island_area):
+ result[labels == component] = 255
+ if min_hole_area > 0:
+ count, labels, stats, _ = cv2.connectedComponentsWithStats(
+ np.where(region & (result > 0), 255, 0).astype(np.uint8), 8
+ )
+ # Fill small repair components back to trusted.
+ for component in range(1, count):
+ if stats[component, cv2.CC_STAT_AREA] < int(min_hole_area):
+ result[labels == component] = 0
+ if min_strip_width > 0:
+ size = max(1, int(min_strip_width)) * 2 + 1
+ kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (size, size))
+ closed = cv2.morphologyEx(result, cv2.MORPH_CLOSE, kernel)
+ result[region] = closed[region]
+ if dilation > 0:
+ size = int(dilation) * 2 + 1
+ kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (size, size))
+ expanded = cv2.dilate(result, kernel, iterations=1)
+ result[region] = expanded[region]
+ result[~region] = 0
+ return result
+
+ if boundary_labels is None:
+ binary = _regularize_region(binary, np.ones(binary.shape, dtype=bool))
+ else:
+ labels = np.asarray(boundary_labels)
+ if labels.shape != binary.shape:
+ labels = cv2.resize(
+ labels.astype(np.float32),
+ (binary.shape[1], binary.shape[0]),
+ interpolation=cv2.INTER_NEAREST,
+ )
+ combined = np.zeros_like(binary)
+ for label in np.unique(labels):
+ if label == 0:
+ continue
+ region = labels == label
+ combined = np.maximum(combined, _regularize_region(binary, region))
+ binary = combined
+
+ keep = max(0, int(keep_largest_components))
+ if keep:
+ count, labels, stats, _ = cv2.connectedComponentsWithStats(binary, 8)
+ ranked = sorted(
+ range(1, count), key=lambda item: stats[item, cv2.CC_STAT_AREA], reverse=True
+ )[:keep]
+ binary = np.where(np.isin(labels, ranked), 255, 0).astype(np.uint8)
return binary
@@ -60,5 +229,71 @@ def reject_fallback_from_write_mask(
delta = generated_bgr[:, :, :3].astype(np.int32) - fallback_bgr.astype(np.int32)[None, None, :]
sentinel = np.sqrt(np.sum(delta * delta, axis=2)) <= max(0, int(tolerance))
result = write_mask.copy()
- result[(write_mask > 0) & sentinel] = 0
+ rejected = (write_mask > 0) & sentinel
+ result[rejected] = 0
return result
+
+
+def residual_fallback_percentage(write_mask, generated_bgr, fallback_rgb, tolerance=8):
+ """Measure sentinel leakage only inside the explicit editable region."""
+ editable = np.asarray(write_mask) > 0
+ if not np.any(editable):
+ return 0.0
+ if generated_bgr.shape[:2] != editable.shape:
+ generated_bgr = cv2.resize(
+ generated_bgr,
+ (editable.shape[1], editable.shape[0]),
+ interpolation=cv2.INTER_LINEAR,
+ )
+ fallback_bgr = np.array(
+ [round(fallback_rgb[2] * 255), round(fallback_rgb[1] * 255), round(fallback_rgb[0] * 255)],
+ dtype=np.int32,
+ )
+ delta = generated_bgr[:, :, :3].astype(np.int32) - fallback_bgr[None, None, :]
+ sentinel = np.sqrt(np.sum(delta * delta, axis=2)) <= max(0, int(tolerance))
+ return float(np.count_nonzero(editable & sentinel) * 100.0 / np.count_nonzero(editable))
+
+
+def build_depth_part_rejection_mask(
+ depth,
+ object_ids=None,
+ material_ids=None,
+ *,
+ margin=3,
+ depth_threshold=3,
+):
+ """Build a per-pixel projection guard around depth/part silhouettes.
+
+ White pixels may project; black pixels are rejected. ID boundaries catch
+ hand/dress and accessory/body transitions even when normalized depth is
+ locally similar.
+ """
+ if depth is None:
+ raise ValueError("depth is required")
+ if depth.ndim == 3:
+ depth = cv2.cvtColor(depth, cv2.COLOR_BGR2GRAY)
+ depth = depth.astype(np.uint8)
+ kernel3 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
+ depth_range = cv2.dilate(depth, kernel3) - cv2.erode(depth, kernel3)
+ edge = depth_range > max(0, int(depth_threshold))
+
+ for ids in (object_ids, material_ids):
+ if ids is None:
+ continue
+ if ids.ndim == 3:
+ ids = cv2.cvtColor(ids, cv2.COLOR_BGR2GRAY)
+ if ids.shape != depth.shape:
+ ids = cv2.resize(
+ ids, (depth.shape[1], depth.shape[0]), interpolation=cv2.INTER_NEAREST
+ )
+ edge |= cv2.dilate(ids, kernel3) != cv2.erode(ids, kernel3)
+
+ edge_u8 = np.where(edge, 255, 0).astype(np.uint8)
+ margin = max(0, int(margin))
+ if margin:
+ size = margin * 2 + 1
+ edge_u8 = cv2.dilate(
+ edge_u8,
+ cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (size, size)),
+ )
+ return np.where(edge_u8 > 0, 0, 255).astype(np.uint8)
diff --git a/stablegen/texturing/rendering.py b/stablegen/texturing/rendering.py
index c86ade1..eb5ea59 100644
--- a/stablegen/texturing/rendering.py
+++ b/stablegen/texturing/rendering.py
@@ -2,6 +2,7 @@
import os
import math
+from contextlib import contextmanager
import bpy, bmesh # pylint: disable=import-error
import numpy as np
import mathutils
@@ -42,6 +43,219 @@ def _restore_material_state(obj, state):
)
+class _RenderStateTransaction:
+ """Restore temporary render mutations even when an export raises."""
+
+ _PASS_NAMES = (
+ "use_pass_combined",
+ "use_pass_diffuse_color",
+ "use_pass_diffuse_direct",
+ "use_pass_diffuse_indirect",
+ "use_pass_ambient_occlusion",
+ "use_pass_shadow",
+ "use_pass_emit",
+ "use_pass_environment",
+ "use_pass_z",
+ "use_pass_normal",
+ "use_pass_object_index",
+ "use_pass_material_index",
+ )
+
+ def __init__(self, context, objects=()):
+ self.context = context
+ self.scene = context.scene
+ self.objects = [obj for obj in objects if obj and obj.type == 'MESH']
+
+ def __enter__(self):
+ scene = self.scene
+ render = scene.render
+ self.material_states = {
+ obj: _snapshot_material_state(obj) for obj in self.objects
+ }
+ self.object_pass_indices = {
+ obj: int(obj.pass_index) for obj in scene.objects
+ if hasattr(obj, 'pass_index')
+ }
+ self.material_pass_indices = {
+ material: int(material.pass_index) for material in bpy.data.materials
+ if hasattr(material, 'pass_index')
+ }
+ self.material_ptrs = {material.as_pointer() for material in bpy.data.materials}
+ self.world_ptrs = {world.as_pointer() for world in bpy.data.worlds}
+ self.hide_render_state = {
+ obj: bool(obj.hide_render) for obj in scene.objects
+ }
+ self.world = scene.world
+ self.world_state = None
+ if self.world:
+ backgrounds = []
+ if self.world.node_tree:
+ for node in self.world.node_tree.nodes:
+ if node.type == 'BACKGROUND':
+ backgrounds.append((
+ node,
+ tuple(node.inputs["Color"].default_value),
+ node.inputs["Strength"].default_value,
+ ))
+ self.world_state = (
+ self.world.use_nodes,
+ tuple(self.world.color),
+ backgrounds,
+ )
+ self.scene_state = {
+ "frame": scene.frame_current,
+ "camera": scene.camera,
+ "engine": render.engine,
+ "film_transparent": render.film_transparent,
+ "use_compositing": render.use_compositing,
+ "filepath": render.filepath,
+ "resolution_x": render.resolution_x,
+ "resolution_y": render.resolution_y,
+ "resolution_percentage": render.resolution_percentage,
+ "image_file_format": render.image_settings.file_format,
+ "scene_use_nodes": scene.use_nodes,
+ "display_device": scene.display_settings.display_device,
+ "view_transform": scene.view_settings.view_transform,
+ "look": scene.view_settings.look,
+ "exposure": scene.view_settings.exposure,
+ "gamma": scene.view_settings.gamma,
+ "cycles_samples": getattr(scene.cycles, "samples", None),
+ "cycles_device": getattr(scene.cycles, "device", None),
+ "cycles_shading_system": getattr(scene.cycles, "shading_system", None),
+ "cycles_use_osl": getattr(scene.cycles, "use_osl", None),
+ "material_override": getattr(self.context.view_layer, "material_override", None),
+ "shading_light": scene.display.shading.light,
+ "shading_color_type": scene.display.shading.color_type,
+ }
+ self.pass_state = {
+ name: getattr(self.context.view_layer, name)
+ for name in self._PASS_NAMES
+ if hasattr(self.context.view_layer, name)
+ }
+ node_tree = (
+ getattr(scene, "compositing_node_group", None)
+ if hasattr(scene, "compositing_node_group")
+ else getattr(scene, "node_tree", None)
+ )
+ self.compositor_tree = node_tree
+ self.compositor_ptrs = (
+ {node.as_pointer() for node in node_tree.nodes} if node_tree else set()
+ )
+ return self
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ scene = self.scene
+ state = self.scene_state
+ restored_meshes = set()
+ for obj, material_state in self.material_states.items():
+ if obj.name in bpy.data.objects:
+ mesh_pointer = obj.data.as_pointer()
+ if mesh_pointer not in restored_meshes:
+ _restore_material_state(obj, material_state)
+ restored_meshes.add(mesh_pointer)
+ # Clearing slots on shared mesh data clamps every user's active index;
+ # restore per-object active slots only after all shared data is final.
+ for obj, material_state in self.material_states.items():
+ if obj.name in bpy.data.objects and obj.data.materials:
+ obj.active_material_index = min(
+ material_state["active_index"], len(obj.data.materials) - 1
+ )
+
+ for obj, hidden in self.hide_render_state.items():
+ if obj.name in bpy.data.objects:
+ obj.hide_render = hidden
+ for obj, pass_index in self.object_pass_indices.items():
+ if obj.name in bpy.data.objects:
+ obj.pass_index = pass_index
+ for material, pass_index in self.material_pass_indices.items():
+ if material.name in bpy.data.materials:
+ material.pass_index = pass_index
+
+ compositor_tree = (
+ getattr(scene, "compositing_node_group", None)
+ if hasattr(scene, "compositing_node_group")
+ else getattr(scene, "node_tree", None)
+ )
+ if compositor_tree:
+ for node in list(compositor_tree.nodes):
+ if node.as_pointer() not in self.compositor_ptrs:
+ compositor_tree.nodes.remove(node)
+
+ scene.world = self.world
+ if self.world and self.world_state:
+ use_nodes, color, backgrounds = self.world_state
+ self.world.use_nodes = use_nodes
+ self.world.color = color
+ for node, bg_color, strength in backgrounds:
+ if node.id_data:
+ node.inputs["Color"].default_value = bg_color
+ node.inputs["Strength"].default_value = strength
+
+ scene.camera = state["camera"]
+ scene.render.engine = state["engine"]
+ scene.render.film_transparent = state["film_transparent"]
+ scene.render.use_compositing = state["use_compositing"]
+ scene.render.filepath = state["filepath"]
+ scene.render.resolution_x = state["resolution_x"]
+ scene.render.resolution_y = state["resolution_y"]
+ scene.render.resolution_percentage = state["resolution_percentage"]
+ scene.render.image_settings.file_format = state["image_file_format"]
+ scene.use_nodes = state["scene_use_nodes"]
+ scene.display_settings.display_device = state["display_device"]
+ scene.view_settings.view_transform = state["view_transform"]
+ scene.view_settings.look = state["look"]
+ scene.view_settings.exposure = state["exposure"]
+ scene.view_settings.gamma = state["gamma"]
+ if state["cycles_samples"] is not None:
+ scene.cycles.samples = state["cycles_samples"]
+ if state["cycles_device"] is not None:
+ scene.cycles.device = state["cycles_device"]
+ if state["cycles_shading_system"] is not None:
+ scene.cycles.shading_system = state["cycles_shading_system"]
+ if state["cycles_use_osl"] is not None:
+ scene.cycles.use_osl = state["cycles_use_osl"]
+ if hasattr(self.context.view_layer, "material_override"):
+ self.context.view_layer.material_override = state["material_override"]
+ scene.display.shading.light = state["shading_light"]
+ scene.display.shading.color_type = state["shading_color_type"]
+ for name, value in self.pass_state.items():
+ setattr(self.context.view_layer, name, value)
+ scene.frame_set(state["frame"])
+
+ for material in list(bpy.data.materials):
+ if material.as_pointer() not in self.material_ptrs and material.users == 0:
+ bpy.data.materials.remove(material)
+ for world in list(bpy.data.worlds):
+ if world.as_pointer() not in self.world_ptrs and world.users == 0:
+ bpy.data.worlds.remove(world)
+ return False
+
+
+@contextmanager
+def _temporary_render_copies(context, objects):
+ """Yield isolated mesh copies without touching production material slots."""
+ originals = [obj for obj in objects if obj and obj.type == 'MESH']
+ copies = []
+ mapping = {}
+ try:
+ for original in originals:
+ duplicate = original.copy()
+ duplicate.data = original.data.copy()
+ duplicate.name = f"_SG_RenderCopy_{original.name}"
+ context.scene.collection.objects.link(duplicate)
+ original.hide_render = True
+ duplicate.hide_render = False
+ copies.append(duplicate)
+ mapping[original] = duplicate
+ yield copies, mapping
+ finally:
+ for duplicate in copies:
+ mesh = duplicate.data
+ bpy.data.objects.remove(duplicate, do_unlink=True)
+ if mesh and mesh.users == 0:
+ bpy.data.meshes.remove(mesh)
+
+
def purge_orphans():
"""
Purge unused datablocks (images, materials, node groups, etc).
@@ -160,7 +374,7 @@ def create_edge_feathered_mask(mask_path, feather_width=30):
return output_path
-def render_edge_feather_mask(context, to_export, camera, camera_index, feather_width=30, softness=1.0):
+def _render_edge_feather_mask_impl(context, to_export, camera, camera_index, feather_width=30, softness=1.0):
"""Render a geometry silhouette from *camera* and apply distance-transform
edge feathering.
@@ -204,25 +418,15 @@ def render_edge_feather_mask(context, to_export, camera, camera_index, feather_w
context.scene.camera = camera
# ── Replace target-object materials with white Emission ─────────────────
- saved_materials = {}
- saved_active_materials = {}
- temp_materials = []
- for obj in to_export:
- saved_materials[obj] = list(obj.data.materials)
- saved_active_materials[obj] = obj.active_material
-
- mat = bpy.data.materials.new(name="_SG_EdgeFeather_Temp")
- mat.use_nodes = True
- mat.node_tree.nodes.clear()
- emission = mat.node_tree.nodes.new("ShaderNodeEmission")
- emission.inputs["Color"].default_value = (1.0, 1.0, 1.0, 1.0)
- emission.inputs["Strength"].default_value = 1.0
- out_node = mat.node_tree.nodes.new("ShaderNodeOutputMaterial")
- mat.node_tree.links.new(emission.outputs[0], out_node.inputs["Surface"])
-
- obj.data.materials.clear()
- obj.data.materials.append(mat)
- temp_materials.append(mat)
+ mat = bpy.data.materials.new(name="_SG_EdgeFeather_Temp")
+ mat.use_nodes = True
+ mat.node_tree.nodes.clear()
+ emission = mat.node_tree.nodes.new("ShaderNodeEmission")
+ emission.inputs["Color"].default_value = (1.0, 1.0, 1.0, 1.0)
+ emission.inputs["Strength"].default_value = 1.0
+ out_node = mat.node_tree.nodes.new("ShaderNodeOutputMaterial")
+ mat.node_tree.links.new(emission.outputs[0], out_node.inputs["Surface"])
+ context.view_layer.material_override = mat
# ── Hide non-target mesh objects ────────────────────────────────────────
hidden_restore = {}
@@ -262,8 +466,6 @@ def render_edge_feather_mask(context, to_export, camera, camera_index, feather_w
node_tree = get_compositor_node_tree(context.scene)
comp_nodes = node_tree.nodes
comp_links = node_tree.links
- comp_nodes.clear()
-
render_layers = comp_nodes.new('CompositorNodeRLayers')
try:
mix_node = comp_nodes.new('CompositorNodeMixRGB')
@@ -289,18 +491,6 @@ def render_edge_feather_mask(context, to_export, camera, camera_index, feather_w
frame_suffix = "0001" if bpy.app.version < (5, 0, 0) else ""
raw_path = os.path.join(output_dir, f"{raw_file}{frame_suffix}.png")
- # ── Restore materials ───────────────────────────────────────────────────
- for obj, mats in saved_materials.items():
- obj.data.materials.clear()
- if saved_active_materials[obj]:
- obj.data.materials.append(saved_active_materials[obj])
- for m in mats:
- if m != saved_active_materials[obj]:
- obj.data.materials.append(m)
- for mat in temp_materials:
- if mat and mat.name in bpy.data.materials:
- bpy.data.materials.remove(mat)
-
# ── Restore non-target visibility ───────────────────────────────────────
for obj, was_hidden in hidden_restore.items():
obj.hide_render = was_hidden
@@ -367,6 +557,19 @@ def render_edge_feather_mask(context, to_export, camera, camera_index, feather_w
return ef_path
+def render_edge_feather_mask(context, to_export, camera, camera_index, feather_width=30, softness=1.0):
+ all_meshes = [obj for obj in context.scene.objects if obj.type == 'MESH']
+ with _RenderStateTransaction(context, all_meshes):
+ return _render_edge_feather_mask_impl(
+ context,
+ to_export,
+ camera,
+ camera_index,
+ feather_width=feather_width,
+ softness=softness,
+ )
+
+
def apply_uv_inpaint_texture(context, obj, baked_image_path):
"""
Apply a UV inpainted/baked texture to the active material.
@@ -630,7 +833,7 @@ def flatten_projection_material_for_refine(context, obj, baked_image_path):
print(f"[StableGen] Flattened ProjectionMaterial for '{obj.name}' to baked texture '{os.path.basename(baked_image_path)}'")
-def export_emit_image(context, to_export, camera_id=None, bg_color=(0.5, 0.5, 0.5), view_transform='Standard', fallback_color=(0,0,0)):
+def _export_emit_image_impl(context, to_export, camera_id=None, bg_color=(0.5, 0.5, 0.5), view_transform='Standard', fallback_color=(0,0,0)):
"""
Exports a emit-only render of the scene from a camera's perspective.
:param context: Blender context.
@@ -638,9 +841,6 @@ def export_emit_image(context, to_export, camera_id=None, bg_color=(0.5, 0.5, 0.
:return: None
"""
print("[StableGen] Exporting emit render")
- # Set animation frame to 1
- bpy.context.scene.frame_set(1)
-
# Store original materials and create temporary ones
original_materials = {}
temporary_materials = {}
@@ -790,9 +990,6 @@ def export_emit_image(context, to_export, camera_id=None, bg_color=(0.5, 0.5, 0.
nodes = node_tree.nodes
links = node_tree.links
- # Clear existing nodes
- nodes.clear()
-
# Create nodes
render_layers = nodes.new('CompositorNodeRLayers')
try:
@@ -868,7 +1065,20 @@ def export_emit_image(context, to_export, camera_id=None, bg_color=(0.5, 0.5, 0.
print(f"[StableGen] Emmision render saved to: {os.path.join(output_dir, output_file)}.png")
-def export_render(context, camera_id=None, output_dir=None, filename=None):
+def export_emit_image(context, to_export, camera_id=None, bg_color=(0.5, 0.5, 0.5), view_transform='Standard', fallback_color=(0,0,0)):
+ with _RenderStateTransaction(context, to_export):
+ with _temporary_render_copies(context, to_export) as (copies, _mapping):
+ return _export_emit_image_impl(
+ context,
+ copies,
+ camera_id=camera_id,
+ bg_color=bg_color,
+ view_transform=view_transform,
+ fallback_color=fallback_color,
+ )
+
+
+def _export_render_impl(context, camera_id=None, output_dir=None, filename=None):
"""
Renders the scene from a camera's perspective using Workbench.
Creates temporary materials for consistent rendering.
@@ -880,39 +1090,6 @@ def export_render(context, camera_id=None, output_dir=None, filename=None):
"""
print("[StableGen] Exporting render using Workbench")
- # Store original materials and create temporary ones
- original_materials = {}
- temporary_materials = []
- for obj in context.view_layer.objects:
- if obj.type == 'MESH':
- # Store original materials
- original_materials[obj] = _snapshot_material_state(obj)
-
- # Create temporary material
- temp_mat = bpy.data.materials.new(name="TempRenderMaterial")
- temporary_materials.append(temp_mat)
- temp_mat.use_nodes = True # Even Workbench uses nodes for basic color
- nodes = temp_mat.node_tree.nodes
- links = temp_mat.node_tree.links
-
- # Clear default nodes
- for node in nodes:
- nodes.remove(node)
-
- # Create basic material output and diffuse BSDF (Workbench respects Base Color)
- mat_output = nodes.new('ShaderNodeOutputMaterial')
- diffuse = nodes.new('ShaderNodeBsdfDiffuse') # Simple diffuse color
- diffuse.inputs['Color'].default_value = (0.8, 0.8, 0.8, 1.0) # Default grey
- diffuse.inputs['Roughness'].default_value = 0.5
- links.new(diffuse.outputs['BSDF'], mat_output.inputs['Surface'])
-
- # Clear materials and assign temp material
- obj.data.materials.clear()
- obj.data.materials.append(temp_mat)
-
- # Set animation frame to 1
- context.scene.frame_set(1)
-
# Setup output path
if output_dir is None:
output_dir = get_dir_path(context, "misc")
@@ -951,8 +1128,6 @@ def export_render(context, camera_id=None, output_dir=None, filename=None):
node_tree = get_compositor_node_tree(context.scene)
nodes = node_tree.nodes
links = node_tree.links
- nodes.clear()
-
render_layers = nodes.new('CompositorNodeRLayers')
output_node = nodes.new('CompositorNodeOutputFile')
configure_output_node_paths(output_node, output_dir, output_file)
@@ -961,19 +1136,9 @@ def export_render(context, camera_id=None, output_dir=None, filename=None):
# Render
bpy.ops.render.render(write_still=True)
- # Restore original materials
- for obj, material_state in original_materials.items():
- _restore_material_state(obj, material_state)
-
# Restore original render settings
render_layer.use_pass_combined = original_combined
- # Clean up temporary materials
- for mat in temporary_materials:
- if mat.name in bpy.data.materials:
- bpy.data.materials.remove(mat)
-
-
# Restore original render settings
if context.scene.render.engine != original_engine:
context.scene.render.engine = original_engine
@@ -984,10 +1149,15 @@ def export_render(context, camera_id=None, output_dir=None, filename=None):
context.scene.render.use_compositing = original_use_compositing
- print(f"[StableGen] Render saved to: {os.path.join(output_dir, output_file)}0001.png") # Blender adds frame number
+def export_render(context, camera_id=None, output_dir=None, filename=None):
+ objects = [obj for obj in context.view_layer.objects if obj.type == 'MESH']
+ with _RenderStateTransaction(context, objects):
+ return _export_render_impl(
+ context, camera_id=camera_id, output_dir=output_dir, filename=filename
+ )
-def export_viewport(context, camera_id=None, output_dir=None, filename=None):
+def _export_viewport_impl(context, camera_id=None, output_dir=None, filename=None):
"""
Renders the scene using viewport OpenGL render to include overlays.
:param context: Blender context.
@@ -1083,6 +1253,15 @@ def export_viewport(context, camera_id=None, output_dir=None, filename=None):
print(f"[StableGen] Viewport render saved to: {os.path.join(output_dir, output_file)}.png")
+
+def export_viewport(context, camera_id=None, output_dir=None, filename=None):
+ """Viewport export with full scene-state restoration on every exit path."""
+ objects = [obj for obj in context.view_layer.objects if obj.type == 'MESH']
+ with _RenderStateTransaction(context, objects):
+ return _export_viewport_impl(
+ context, camera_id=camera_id, output_dir=output_dir, filename=filename
+ )
+
def export_canny(context, camera_id=None, low_threshold=0, high_threshold=80):
"""
Uses export_render and openCV to generate a Canny edge detection image.
@@ -1194,8 +1373,8 @@ def expand_mask_to_blocks(mask_file_path, block_size=8):
return None
-def export_visibility(context, to_export, obj=None, camera_visibility=None,
- prepare_only=False, camera_render=None, camera_id=None):
+def _export_visibility_impl(context, to_export, obj=None, camera_visibility=None,
+ prepare_only=False, camera_render=None, camera_id=None):
"""
Exports the visibility of the mesh by temporarily altering the shading nodes.
:param context: Blender context.
@@ -1253,6 +1432,40 @@ def export_visibility(context, to_export, obj=None, camera_visibility=None,
if not output.inputs[0].links:
bpy.data.materials.remove(mat_copy)
return False
+
+ # Confidence-owned sequential materials expose the original source
+ # quality explicitly. Build repair = visible surface & ~trusted from
+ # that socket instead of reverse-engineering RGB or summed blend
+ # weights. The copied material is used only by this render.
+ owner_quality = next(
+ (node for node in nodes if node.label == 'SGOwnerQuality'), None
+ )
+ if owner_quality is not None:
+ trusted = nodes.new("ShaderNodeMath")
+ trusted.operation = 'GREATER_THAN'
+ trusted.inputs[1].default_value = float(getattr(
+ context.scene, 'qwen_trust_threshold', 0.7
+ )) - 1e-7
+ trusted.label = "SGTrustedContext"
+ links.new(owner_quality.outputs[0], trusted.inputs[0])
+
+ repair = nodes.new("ShaderNodeMath")
+ repair.operation = 'SUBTRACT'
+ repair.inputs[0].default_value = 1.0
+ repair.label = "SGRepairMask"
+ links.new(trusted.outputs[0], repair.inputs[1])
+
+ repair_color = nodes.new("ShaderNodeMixRGB")
+ repair_color.inputs["Color1"].default_value = (0, 0, 0, 1)
+ repair_color.inputs["Color2"].default_value = (1, 1, 1, 1)
+ repair_color.label = "SGRepairMaskColor"
+ links.new(repair.outputs[0], repair_color.inputs["Fac"])
+ links.new(repair_color.outputs[0], output.inputs[0])
+
+ obj.data.materials.clear()
+ obj.data.materials.append(mat_copy)
+ temporary_materials[obj] = mat_copy
+ return True
# Determine which input to used based on existence of BSDF node before the output
if output.inputs[0].links and output.inputs[0].links[0].from_node.type == 'BSDF_PRINCIPLED':
@@ -1476,7 +1689,7 @@ def export_visibility(context, to_export, obj=None, camera_visibility=None,
else:
camera_render_index = camera_id
context.scene.camera = camera_render
- export_emit_image(context, to_export, camera_id=f"{camera_render_index}_visibility", bg_color=(1, 1, 1), view_transform='Raw', fallback_color=(1,1,1))
+ _export_emit_image_impl(context, to_export, camera_id=f"{camera_render_index}_visibility", bg_color=(1, 1, 1), view_transform='Raw', fallback_color=(1,1,1))
# Restore original materials
restore_prepared_materials()
@@ -1484,6 +1697,25 @@ def export_visibility(context, to_export, obj=None, camera_visibility=None,
return True
+def export_visibility(context, to_export, obj=None, camera_visibility=None,
+ prepare_only=False, camera_render=None, camera_id=None):
+ objects = [obj] if obj is not None else list(to_export or ())
+ if prepare_only:
+ return _export_visibility_impl(
+ context, to_export, obj=obj, camera_visibility=camera_visibility,
+ prepare_only=True, camera_render=camera_render, camera_id=camera_id,
+ )
+ with _RenderStateTransaction(context, objects):
+ with _temporary_render_copies(context, objects) as (copies, mapping):
+ mapped_obj = mapping.get(obj) if obj is not None else None
+ return _export_visibility_impl(
+ context, copies, obj=mapped_obj,
+ camera_visibility=camera_visibility,
+ prepare_only=False, camera_render=camera_render,
+ camera_id=camera_id,
+ )
+
+
# =========================================================
# Camera Placement Helper Functions
# =========================================================
diff --git a/stablegen/texturing/view_plan.py b/stablegen/texturing/view_plan.py
index 5cc613f..85f492a 100644
--- a/stablegen/texturing/view_plan.py
+++ b/stablegen/texturing/view_plan.py
@@ -9,7 +9,8 @@ from __future__ import annotations
from dataclasses import dataclass
import math
import re
-from typing import Any, Iterable, Sequence
+from types import MappingProxyType
+from typing import Any, Iterable, Mapping, Sequence
_NATURAL_PARTS = re.compile(r"(\d+)")
@@ -37,6 +38,29 @@ def order_cameras(cameras: Iterable[Any], custom_names: Sequence[str] = ()) -> l
return ordered
+def order_cameras_from_manifest(cameras: Iterable[Any], manifest: Mapping) -> list[Any]:
+ """Restore persisted UUID ordering for regeneration/project-only runs."""
+ cameras = list(cameras)
+ by_id = {
+ str(camera.get("sg_view_uuid")): camera
+ for camera in cameras
+ if camera.get("sg_view_uuid")
+ }
+ ordered = []
+ seen = set()
+ for record in manifest.get("views", []):
+ camera = by_id.get(str(record.get("view_id", "")))
+ if camera is not None and camera not in seen:
+ ordered.append(camera)
+ seen.add(camera)
+ ordered.extend(
+ camera
+ for camera in sorted(cameras, key=lambda item: natural_name_key(item.name))
+ if camera not in seen
+ )
+ return ordered
+
+
def choose_aligned_resolution(
width: int,
height: int,
@@ -113,29 +137,51 @@ class ViewRecord:
camera: Any
resolution_x: int
resolution_y: int
+ prompt: str = ""
+ file_identifiers: Mapping[str, str] | None = None
+ matrix_world: tuple[tuple[float, ...], ...] | None = None
+ intrinsics: Mapping[str, Any] | None = None
+
+ def __post_init__(self):
+ object.__setattr__(
+ self,
+ "file_identifiers",
+ MappingProxyType(dict(self.file_identifiers or {})),
+ )
+ object.__setattr__(
+ self,
+ "intrinsics",
+ MappingProxyType(dict(self.intrinsics or {})),
+ )
def to_manifest(self) -> dict[str, Any]:
camera_data = getattr(self.camera, "data", None)
- matrix = getattr(self.camera, "matrix_world", None)
- matrix_rows = None
- if matrix is not None:
+ matrix_rows = self.matrix_world
+ if matrix_rows is None:
+ matrix = getattr(self.camera, "matrix_world", None)
try:
- matrix_rows = [[float(value) for value in row] for row in matrix]
+ matrix_rows = tuple(
+ tuple(float(value) for value in row) for row in matrix
+ ) if matrix is not None else None
except (TypeError, ValueError):
matrix_rows = None
+ intrinsics = dict(self.intrinsics or {})
return {
"schema": 1,
"view_id": self.view_id,
"index": self.index,
"name": self.name,
"resolution": [self.resolution_x, self.resolution_y],
- "matrix_world": matrix_rows,
+ "prompt": self.prompt,
+ "files": dict(self.file_identifiers or {}),
+ "matrix_world": [list(row) for row in matrix_rows] if matrix_rows else None,
"camera": {
- "type": getattr(camera_data, "type", None),
- "lens": getattr(camera_data, "lens", None),
- "sensor_width": getattr(camera_data, "sensor_width", None),
- "sensor_height": getattr(camera_data, "sensor_height", None),
- "shift_x": getattr(camera_data, "shift_x", None),
- "shift_y": getattr(camera_data, "shift_y", None),
+ "type": intrinsics.get("type", getattr(camera_data, "type", None)),
+ "lens": intrinsics.get("lens", getattr(camera_data, "lens", None)),
+ "sensor_width": intrinsics.get("sensor_width", getattr(camera_data, "sensor_width", None)),
+ "sensor_height": intrinsics.get("sensor_height", getattr(camera_data, "sensor_height", None)),
+ "shift_x": intrinsics.get("shift_x", getattr(camera_data, "shift_x", None)),
+ "shift_y": intrinsics.get("shift_y", getattr(camera_data, "shift_y", None)),
+ "ortho_scale": intrinsics.get("ortho_scale", getattr(camera_data, "ortho_scale", None)),
},
}
diff --git a/stablegen/texturing/workflows.py b/stablegen/texturing/workflows.py
index 0c70433..944ecbe 100644
--- a/stablegen/texturing/workflows.py
+++ b/stablegen/texturing/workflows.py
@@ -10,7 +10,7 @@ from datetime import datetime
from ..util.workflow_templates import prompt_text_qwen_image_edit, prompt_text_flux2_klein
from ..utils import get_generation_dirs
from ..timeout_config import get_timeout
-from .graph_validation import validate_workflow_graph
+from .graph_validation import validate_workflow_graph, qwen_style_semantic_slot, sdxl_final_mask_reference
from io import BytesIO
import numpy as np
@@ -19,6 +19,15 @@ from PIL import Image
_ADDON_PKG = __package__.rsplit('.', 1)[0]
+def _immutable_view_prompt(operator):
+ """Return the prompt frozen into the canonical generation view plan."""
+ index = getattr(operator, '_current_image', -1)
+ plan = getattr(operator, '_view_plan', ()) or ()
+ if 0 <= index < len(plan):
+ return plan[index].prompt or ""
+ return ""
+
+
def _texturing_prompt(scene):
"""Return the texturing portion of the prompt.
@@ -54,25 +63,33 @@ def _generation_prompt(scene):
class _TexturingWorkflowMixin:
+ def _queue_prompt(self, prompt, client_id, server_address):
+ """Validate every dynamically edited texturing graph at submission."""
+ issues = validate_workflow_graph(prompt)
+ if issues:
+ message = "Invalid texturing workflow: " + "; ".join(issues)
+ self.operator._error = message
+ raise ValueError(message)
+ return super()._queue_prompt(prompt, client_id, server_address)
+
"""Texturing workflow methods for WorkflowManager."""
def _crop_and_vignette(
self,
img_bytes,
- border_px: int = 8,
+ border_px: int = 0,
feather: float = 0.15,
gamma: float = 0.7,
):
"""
- Crop a constant border from the image and apply an *alpha* vignette
- that fades the image out toward the edges.
+ Apply an alpha vignette without changing image registration.
IMPORTANT:
- RGB is NOT darkened here, only alpha is shaped.
- This assumes the shader uses alpha to blend with the underlying
surface (e.g. Mix using alpha as Fac).
- border_px : how many pixels to remove on each side.
+ border_px : deprecated and ignored; sequential outputs stay full-size.
feather : fraction of min(width, height) that is used as the feather band.
gamma : exponent applied to the feather mask (1.0 = linear).
"""
@@ -89,19 +106,8 @@ class _TexturingWorkflowMixin:
f"({len(img_bytes)} bytes): {e} — returning raw bytes.")
return img_bytes
- w, h = img.size
- if w <= 2 * border_px or h <= 2 * border_px:
- # Too small to crop; just return as-is
- out_buf = BytesIO()
- img.save(out_buf, format="PNG")
- return out_buf.getvalue()
-
- # --- Crop hard Comfy border first ---
- left = border_px
- top = border_px
- right = w - border_px
- bottom = h - border_px
- img = img.crop((left, top, right, bottom))
+ # Never crop here. The old fixed eight-pixel crop changed camera/image
+ # registration and the projection transform was not updated.
w, h = img.size
# Convert to numpy for mask math
@@ -548,6 +554,27 @@ class _TexturingWorkflowMixin:
# --- Reference Latent chain (Qwen unzoom fix) ---
self._build_qwen_reference_latent_chain(prompt, NODES)
+ prompt[NODES['sampler']]['inputs']['seed'] = context.scene.seed
+ prompt[NODES['sampler']]['inputs']['steps'] = context.scene.steps
+ prompt[NODES['sampler']]['inputs']['cfg'] = context.scene.cfg
+ prompt[NODES['sampler']]['inputs']['sampler_name'] = context.scene.sampler
+ prompt[NODES['sampler']]['inputs']['scheduler'] = context.scene.scheduler
+ prompt[NODES['sampler']]['inputs']['denoise'] = 1.0
+
+ if is_nunchaku:
+ prompt[NODES['unet_loader']] = {
+ "inputs": {
+ "model_name": context.scene.model_name,
+ "cpu_offload": "auto",
+ "num_blocks_on_gpu": 1,
+ "use_pin_memory": "enable",
+ },
+ "class_type": "NunchakuQwenImageDiTLoader",
+ "_meta": {"title": "Nunchaku Qwen-Image DiT Loader"},
+ }
+ else:
+ prompt[NODES['unet_loader']]['inputs']['unet_name'] = context.scene.model_name
+
graph_issues = validate_workflow_graph(
prompt, (NODES['pos_prompt'], NODES['neg_prompt'])
)
@@ -632,12 +659,9 @@ class _TexturingWorkflowMixin:
remove_context = False
# --- Camera Prompt Injection ---
- if context.scene.use_camera_prompts and self.operator._cameras and self.operator._current_image < len(self.operator._cameras):
- current_camera_name = self.operator._cameras[self.operator._current_image].name
- # Find the prompt in the collection
- prompt_item = next((item for item in context.scene.camera_prompts if item.name == current_camera_name), None)
- if prompt_item and prompt_item.prompt:
- view_desc = prompt_item.prompt
+ if context.scene.use_camera_prompts:
+ view_desc = _immutable_view_prompt(self.operator)
+ if view_desc:
# Prepend the view description
user_prompt = f"{view_desc}, {user_prompt}"
@@ -718,7 +742,7 @@ class _TexturingWorkflowMixin:
prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
# Case 2: Sequential generation (after first image)
- elif not is_initial_image:
+ if style_image_info is None and not is_initial_image:
if context_mode == 'REPLACE_STYLE':
# The context render becomes the style image
style_image_info = context_render_info
@@ -792,8 +816,9 @@ class _TexturingWorkflowMixin:
}
# Point text-encode image2 at the scaled version so both
# VL processing and the reference latent chain see ~1 MP.
- prompt[NODES['pos_prompt']]['inputs']['image2'] = [scale_node_key, 0]
- prompt[NODES['neg_prompt']]['inputs']['image2'] = [scale_node_key, 0]
+ style_slot = qwen_style_semantic_slot(context_mode, is_initial_image)
+ prompt[NODES['pos_prompt']]['inputs'][style_slot] = [scale_node_key, 0]
+ prompt[NODES['neg_prompt']]['inputs'][style_slot] = [scale_node_key, 0]
# --- Reference Latent chain (Qwen unzoom fix) ---
self._build_qwen_reference_latent_chain(prompt, NODES)
@@ -926,14 +951,10 @@ class _TexturingWorkflowMixin:
# --- Base user prompt + camera injection ---
user_prompt = _texturing_prompt(context.scene)
camera_suffix = ""
- if (context.scene.use_camera_prompts
- and self.operator._cameras
- and self.operator._current_image < len(self.operator._cameras)):
- cam_name = self.operator._cameras[self.operator._current_image].name
- prompt_item = next(
- (p for p in context.scene.camera_prompts if p.name == cam_name), None)
- if prompt_item and prompt_item.prompt:
- camera_suffix = f", {prompt_item.prompt}"
+ if context.scene.use_camera_prompts:
+ view_desc = _immutable_view_prompt(self.operator)
+ if view_desc:
+ camera_suffix = f", {view_desc}"
# ─── Collect reference images ────────────────────────────────
ref_images = []
@@ -1929,12 +1950,9 @@ class _TexturingWorkflowMixin:
base_prompt_text = _texturing_prompt(context.scene)
# Camera Prompt Injection
- if context.scene.use_camera_prompts and context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit'] and self.operator._cameras and self.operator._current_image < len(self.operator._cameras):
- current_camera_name = self.operator._cameras[self.operator._current_image].name
- # Find the prompt in the collection
- prompt_item = next((item for item in context.scene.camera_prompts if item.name == current_camera_name), None)
- if prompt_item and prompt_item.prompt:
- view_desc = prompt_item.prompt
+ if context.scene.use_camera_prompts and context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit']:
+ view_desc = _immutable_view_prompt(self.operator)
+ if view_desc:
# Prepend the view description
base_prompt_text = f"{view_desc}, {base_prompt_text}"
@@ -2328,12 +2346,9 @@ class _TexturingWorkflowMixin:
base_prompt_text = _texturing_prompt(context.scene)
# Camera Prompt Injection
- if context.scene.use_camera_prompts and context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit'] and self.operator._cameras and self.operator._current_image < len(self.operator._cameras):
- current_camera_name = self.operator._cameras[self.operator._current_image].name
- # Find the prompt in the collection
- prompt_item = next((item for item in context.scene.camera_prompts if item.name == current_camera_name), None)
- if prompt_item and prompt_item.prompt:
- view_desc = prompt_item.prompt
+ if context.scene.use_camera_prompts and context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit']:
+ view_desc = _immutable_view_prompt(self.operator)
+ if view_desc:
# Prepend the view description
base_prompt_text = f"{view_desc}, {base_prompt_text}"
@@ -2429,10 +2444,10 @@ class _TexturingWorkflowMixin:
prompt[NODES['mask_image']]["inputs"]["image"] = mask_info['name']
prompt[NODES['input_image']]["inputs"]["image"] = render_info['name']
- # Configure mask blur settings
- if not context.scene.blur_mask:
- prompt[NODES['inpaint_conditioning']]["inputs"]["mask"] = [NODES['grow_mask'], 0] # Direct connection
- prompt[NODES['vae_encode_inpaint']]["inputs"]["mask"] = [NODES['grow_mask'], 0] # Direct connection
+ # One authoritative processed mask drives every inpaint consumer.
+ final_mask = sdxl_final_mask_reference(context.scene.blur_mask, NODES)
+ prompt[NODES['inpaint_conditioning']]["inputs"]["mask"] = final_mask
+ prompt[NODES['vae_encode_inpaint']]["inputs"]["mask"] = final_mask
# Set blur parameters
prompt[NODES['blur']]["inputs"]["sigma"] = context.scene.blur_mask_sigma
@@ -2565,12 +2580,9 @@ class _TexturingWorkflowMixin:
base_prompt_text = _texturing_prompt(context.scene)
# Camera Prompt Injection
- if context.scene.use_camera_prompts and context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit'] and self.operator._cameras and self.operator._current_image < len(self.operator._cameras):
- current_camera_name = self.operator._cameras[self.operator._current_image].name
- # Find the prompt in the collection
- prompt_item = next((item for item in context.scene.camera_prompts if item.name == current_camera_name), None)
- if prompt_item and prompt_item.prompt:
- view_desc = prompt_item.prompt
+ if context.scene.use_camera_prompts and context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit']:
+ view_desc = _immutable_view_prompt(self.operator)
+ if view_desc:
# Prepend the view description
base_prompt_text = f"{view_desc}, {base_prompt_text}"
@@ -2771,12 +2783,9 @@ class _TexturingWorkflowMixin:
base_prompt_text = _texturing_prompt(context.scene)
# Camera Prompt Injection
- if context.scene.use_camera_prompts and context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit', 'grid'] and self.operator._cameras and self.operator._current_image < len(self.operator._cameras):
- current_camera_name = self.operator._cameras[self.operator._current_image].name
- # Find the prompt in the collection
- prompt_item = next((item for item in context.scene.camera_prompts if item.name == current_camera_name), None)
- if prompt_item and prompt_item.prompt:
- view_desc = prompt_item.prompt
+ if context.scene.use_camera_prompts and context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit', 'grid']:
+ view_desc = _immutable_view_prompt(self.operator)
+ if view_desc:
# Prepend the view description
base_prompt_text = f"{view_desc}, {base_prompt_text}"
diff --git a/stablegen/ui/panel.py b/stablegen/ui/panel.py
index b31ceac..8c24695 100644
--- a/stablegen/ui/panel.py
+++ b/stablegen/ui/panel.py
@@ -1309,9 +1309,38 @@ class StableGenPanel(bpy.types.Panel):
row = content_box.row()
row.prop(scene, "sg_texel_ownership", text="Per-Texel View Ownership", toggle=True, icon="UV")
- if scene.sg_texel_ownership:
+ ownership_active = (
+ scene.sg_texel_ownership
+ or (scene.model_architecture == 'qwen_image_edit'
+ and scene.generation_method == 'sequential'
+ and scene.qwen_use_projection_write_masks)
+ )
+ if ownership_active:
+ if not scene.sg_texel_ownership:
+ row = content_box.row()
+ row.label(text="Enabled automatically for protected Qwen sequential projection", icon="INFO")
row = content_box.row()
row.prop(scene, "sg_texel_replacement_margin", text="Replacement Margin")
+ row = content_box.row()
+ row.prop(scene, "sg_allow_trusted_replacement", text="Allow Trusted Quality Upgrades", toggle=True)
+ row = content_box.row()
+ row.prop(scene, "sg_ownership_blend_mode", text="Detail Fusion")
+ if scene.sg_ownership_blend_mode == 'OWNER':
+ row = content_box.row()
+ row.prop(scene, "sg_ownership_seam_width", text="Seam Width")
+ row = content_box.row()
+ row.prop(scene, "sg_ownership_lowpass_sigma", text="Low-Pass Radius")
+ row = content_box.row()
+ row.prop(scene, "sg_freeze_ownership_after_generation", text="Freeze Ownership to UV", toggle=True)
+
+ row = content_box.row()
+ row.prop(scene, "sg_auto_view_sequence", text="Overlap-Aware View Sequence", toggle=True)
+ if scene.sg_auto_view_sequence:
+ row = content_box.row()
+ row.prop(scene, "sg_anchor_camera", text="Anchor Camera")
+
+ row = content_box.row()
+ row.prop(scene, "sg_texturing_frame", text="Texturing Frame")
row = content_box.row()
row.prop(scene, "bake_visibility_weights", text="Bake Visibility (Transform-Stable)", toggle=True, icon="MESH_DATA")
@@ -1595,9 +1624,6 @@ class StableGenPanel(bpy.types.Panel):
row = colors_box.row()
row.prop(scene, "qwen_guidance_fallback_color", text="Fallback")
- dilation_row = context_box.row()
- dilation_row.prop(scene, "qwen_context_fallback_dilation", text="Fallback Dilate (px)")
-
protection_row = context_box.row()
protection_row.prop(
scene,
@@ -1607,20 +1633,23 @@ class StableGenPanel(bpy.types.Panel):
icon="LOCKED",
)
if scene.qwen_use_projection_write_masks:
+ threshold_row = context_box.row()
+ threshold_row.prop(
+ scene, "qwen_trust_threshold", text="Trusted Quality"
+ )
tolerance_row = context_box.row()
tolerance_row.prop(
scene,
"qwen_residual_fallback_tolerance",
text="Residual Magenta Tol.",
)
-
- cleanup_row = context_box.row()
- cleanup_row.prop(scene, "qwen_context_cleanup", text="Apply Cleanup", toggle=True, icon="BRUSH_DATA")
- if scene.qwen_context_cleanup:
- row = context_box.row()
- row.prop(scene, "qwen_context_cleanup_hue_tolerance", text="Hue Tol (°)")
- row = context_box.row()
- row.prop(scene, "qwen_context_cleanup_value_adjust", text="Value Adjust")
+ regularize_box = context_box.box()
+ regularize_box.label(text="Binary Boundary Regularization")
+ regularize_box.prop(scene, "qwen_mask_min_island_area", text="Min Trusted Island")
+ regularize_box.prop(scene, "qwen_mask_min_hole_area", text="Min Repair Hole")
+ regularize_box.prop(scene, "qwen_mask_min_strip_width", text="Min Context Strip")
+ regularize_box.prop(scene, "qwen_mask_uncertainty_dilation", text="Uncertainty Dilate")
+ regularize_box.prop(scene, "qwen_mask_keep_largest_components", text="Largest Components")
elif scene.model_architecture == 'sdxl' or scene.model_architecture == 'flux1':
# IPAdapter Parameters
@@ -2174,6 +2203,9 @@ class StableGenPanel(bpy.types.Panel):
row = debug_box.row()
op2 = row.operator("stablegen.debug_feather_preview", text="Feather Preview", icon="MOD_SMOOTH")
+ row = debug_box.row()
+ row.operator("stablegen.debug_ownership_diagnostic", text="Ownership Diagnostics", icon="SHADING_RENDERED")
+
row = debug_box.row()
row.operator("stablegen.debug_make_solid", text="Debug Make Solid", icon="MOD_REMESH")
diff --git a/stablegen/ui/presets.py b/stablegen/ui/presets.py
index add065d..a403079 100644
--- a/stablegen/ui/presets.py
+++ b/stablegen/ui/presets.py
@@ -87,6 +87,14 @@ GEN_PARAMETERS = [
"early_priority_strength",
"sg_texel_ownership",
"sg_texel_replacement_margin",
+ "sg_allow_trusted_replacement",
+ "sg_ownership_blend_mode",
+ "sg_ownership_seam_width",
+ "sg_ownership_lowpass_sigma",
+ "sg_auto_view_sequence",
+ "sg_anchor_camera",
+ "sg_texturing_frame",
+ "sg_freeze_ownership_after_generation",
"differential_diffusion",
"differential_noise",
"blur_mask",
@@ -113,6 +121,12 @@ GEN_PARAMETERS = [
"qwen_context_cleanup_value_adjust",
"qwen_context_fallback_dilation",
"qwen_use_projection_write_masks",
+ "qwen_trust_threshold",
+ "qwen_mask_min_island_area",
+ "qwen_mask_min_hole_area",
+ "qwen_mask_min_strip_width",
+ "qwen_mask_uncertainty_dilation",
+ "qwen_mask_keep_largest_components",
"qwen_residual_fallback_tolerance",
"qwen_prompt_gray_background",
"qwen_rescale_alignment",