Skip to content

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

clean up and expand context renders with or without confidence gating

sakalondcc2d5963bb84
diff --git a/stablegen/core/feature_flags.py b/stablegen/core/feature_flags.py
index 677d9bf..658a7f6 100644
--- a/stablegen/core/feature_flags.py
+++ b/stablegen/core/feature_flags.py
@@ -2,6 +2,7 @@
GRADUATED_FEATURE_DEFAULTS = {
"qwen_use_confidence_context": False,
+ "qwen_regularize_context_mask": False,
"sg_rework_acquisition_quality": False,
"sg_rework_projection_guard": False,
"sg_allow_backface_projection": True,
@@ -10,7 +11,7 @@ EXPERIMENTAL_FEATURE_DEFAULTS = dict.fromkeys((
"sg_rework_aligned_resolution", "sg_rework_full_resolution_scale",
"sg_rework_final_display", "qwen_global_style_vlm_only",
"qwen_context_vlm_only", "qwen_latent_locked_context",
- "qwen_latent_lock_neutral_fill", "qwen_regularize_context_mask",
+ "qwen_latent_lock_neutral_fill",
"qwen_use_projection_write_masks", "qwen_reject_residual_fallback",
"sg_auto_view_sequence",
), False)
diff --git a/stablegen/core/properties.py b/stablegen/core/properties.py
index 5a2f7d0..1460752 100644
--- a/stablegen/core/properties.py
+++ b/stablegen/core/properties.py
@@ -1254,8 +1254,8 @@ def register_properties(update_model_list, ControlNetUnit, LoRAUnit,
subtype='COLOR', default=(1.0, 0.0, 1.0), min=0.0, max=1.0, update=update_parameters
)
bpy.types.Scene.qwen_context_cleanup = bpy.props.BoolProperty(
- name="Legacy fallback cleanup",
- description="Clean fallback-color residue before projecting subsequent Qwen outputs in the legacy context path. Used when confidence-gated context is off.",
+ name="Fallback cleanup",
+ description="Recolor missing-context-color residue in each later Qwen output before it is projected.",
default=False, update=update_parameters
)
bpy.types.Scene.qwen_context_cleanup_hue_tolerance = bpy.props.FloatProperty(
@@ -1264,12 +1264,12 @@ def register_properties(update_model_list, ControlNetUnit, LoRAUnit,
)
bpy.types.Scene.qwen_context_cleanup_value_adjust = bpy.props.FloatProperty(
name="Brightness adjustment",
- description="Brightness adjustment applied during legacy fallback cleanup. -1 darkens toward black; 0 preserves brightness; 1 brightens toward white.",
+ description="Brightness adjustment applied during fallback cleanup. -1 darkens toward black; 0 preserves brightness; 1 brightens toward white.",
default=0.0, min=-1.0, max=1.0, update=update_parameters
)
bpy.types.Scene.qwen_context_fallback_dilation = bpy.props.IntProperty(
- name="Legacy context expansion (px)",
- description="In the legacy context path, expand missing-context regions by this many image pixels before sending the render to the model. Used when confidence-gated context is off.",
+ name="Context expansion (px)",
+ description="Expand the missing-context regions of each context render by this many image pixels before sending it to the model.",
default=1, min=0, max=64, update=update_parameters
)
bpy.types.Scene.qwen_use_confidence_context = bpy.props.BoolProperty(
@@ -1278,8 +1278,8 @@ def register_properties(update_model_list, ControlNetUnit, LoRAUnit,
default=TEXTURING_DEFAULTS["qwen_use_confidence_context"], update=update_parameters,
)
bpy.types.Scene.qwen_regularize_context_mask = bpy.props.BoolProperty(
- name="Clean repair-mask boundaries",
- description="Clean fragmented repair-mask boundaries using the island, hole, strip and expansion controls below. Experimental.",
+ name="Regularize Context Boundary",
+ description="Clean up the missing-context mask of each context render before it is blanked: drop small kept islands, fill small missing holes, close thin kept strips and expand missing regions by the settings below, separately within each object and material.",
default=TEXTURING_DEFAULTS["qwen_regularize_context_mask"], update=update_parameters,
)
bpy.types.Scene.qwen_trust_threshold = bpy.props.FloatProperty(
diff --git a/stablegen/texturing/generator.py b/stablegen/texturing/generator.py
index 1a7772e..4c65c3f 100644
--- a/stablegen/texturing/generator.py
+++ b/stablegen/texturing/generator.py
@@ -67,6 +67,74 @@ import websocket
_ADDON_PKG = __package__.rsplit('.', 1)[0]
+
+def _regularized_context(scene):
+ """Regularize Context Boundary cleans each context render's missing-context
+ mask inside object/material parts, so it needs their ID passes."""
+ return (getattr(scene, 'qwen_regularize_context_mask', False)
+ and scene.model_architecture in EDIT_ARCHITECTURES)
+
+
+def _context_boundary_labels(context, camera_id, shape):
+ """Object/material part labels of a view at *shape*, or None without ID maps."""
+ 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,
+ )
+ if object_ids is None and material_ids is None:
+ return None
+ 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
+ )
+ return object_ids.astype(np.int32) * 256 + material_ids.astype(np.int32)
+
+
+def _regularize_context_mask(context, camera_id, repair_mask):
+ """Binarize a missing-context mask (255 = generate) and, with Regularize
+ Context Boundary, clean it up."""
+ if not _regularized_context(context.scene):
+ return np.where(repair_mask > 127, 255, 0).astype(np.uint8)
+ # Constrain morphology to rendered object/material parts. This avoids
+ # letting a hand/accessory repair seed expand onto a dress/body part.
+ return regularize_repair_mask(
+ repair_mask,
+ threshold=127,
+ 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=_context_boundary_labels(context, camera_id, repair_mask.shape[:2]),
+ keep_largest_components=getattr(
+ context.scene, 'qwen_mask_keep_largest_components', 0
+ ),
+ )
+
+
+def _expand_context_mask(context, repair_mask):
+ """Context expansion: grow a binary missing-context mask by the set pixels."""
+ dilation = int(max(0, getattr(context.scene, 'qwen_context_fallback_dilation', 0)))
+ if dilation <= 0:
+ return repair_mask
+ kernel = cv2.getStructuringElement(
+ cv2.MORPH_ELLIPSE, (dilation * 2 + 1, dilation * 2 + 1)
+ )
+ return cv2.dilate(repair_mask, kernel, iterations=1)
+
class Regenerate(bpy.types.Operator):
"""Regenerate textures for selected cameras / viewpoints
- Works for sequential and separate generation modes
@@ -536,52 +604,9 @@ 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,
+ repair_mask = _expand_context_mask(
+ context, _regularize_context_mask(context, camera_id, repair_mask)
)
- 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)
- )
-
- if experimental_enabled(context.scene, 'qwen_regularize_context_mask'):
- repair_mask = regularize_repair_mask(
- repair_mask,
- threshold=127,
- 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
- ),
- )
- else:
- repair_mask = np.where(repair_mask > 127, 255, 0).astype(np.uint8)
# Depth/ID discontinuities are never trusted context. Forcing these
# pixels to repair prevents front objects from imprinting silhouettes
# onto the surface behind them.
@@ -665,9 +690,10 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
)
def _dilate_qwen_context_fallback(self, context, camera_id, fallback_color):
- """Legacy rigging-branch fallback-color dilation."""
+ """Clean up (Regularize Context Boundary) and expand (context expansion)
+ the fallback-color regions of a context render without confidence gating."""
dilation = int(max(0, context.scene.qwen_context_fallback_dilation))
- if dilation <= 0:
+ if dilation <= 0 and not _regularized_context(context.scene):
return
image_path = get_file_path(
context, "inpaint", subtype="render", camera_id=camera_id
@@ -691,12 +717,11 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
)
if not np.any(mask):
return
- kernel_size = max(1, dilation * 2 + 1)
- kernel = cv2.getStructuringElement(
- cv2.MORPH_ELLIPSE, (kernel_size, kernel_size)
- )
- dilated_mask = cv2.dilate(
- mask.astype(np.uint8) * 255, kernel, iterations=1
+ # Holes the cleanup marks as kept have no projected colour to restore;
+ # only the regions it adds are painted.
+ dilated_mask = _expand_context_mask(
+ context,
+ _regularize_context_mask(context, camera_id, mask.astype(np.uint8) * 255),
) > 0
pixel_data[dilated_mask, :3] = fallback_rgb
pixel_data[dilated_mask, 3] = 255
@@ -709,7 +734,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
self._uploaded_images_cache.pop(os.path.abspath(image_path), None)
def _apply_qwen_context_cleanup(self, context, image_bytes):
- """Legacy rigging-branch global fallback-hue cleanup."""
+ """Fallback cleanup: recolor fallback-hue residue in a later Qwen output."""
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)
@@ -1649,7 +1674,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
and context.scene.qwen_refine_use_depth)
or (getattr(context.scene, 'sg_rework_projection_guard', False)
and context.scene.generation_method == 'sequential')
- or (experimental_enabled(context.scene, 'qwen_regularize_context_mask')
+ or (_regularized_context(context.scene)
and context.scene.generation_method == 'sequential')
)
if need_depth and context.scene.generation_method != 'uv_inpaint':
@@ -2022,7 +2047,6 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
if (context.scene.model_architecture in EDIT_ARCHITECTURES
and context.scene.generation_method == 'sequential'
and self._current_image > 0
- and not getattr(context.scene, 'qwen_use_confidence_context', False)
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)
@@ -2378,7 +2402,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
)
use_part_ids = (
use_projection_guard
- or experimental_enabled(context.scene, 'qwen_regularize_context_mask')
+ or _regularized_context(context.scene)
)
# Workbench retains the fast ordinary depth-guidance path. Cycles is
# required whenever a consumer needs categorical object/material IDs;
@@ -2661,7 +2685,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
"""
with _RenderStateTransaction(context):
use_part_ids = (getattr(context.scene, 'sg_rework_projection_guard', False)
- or experimental_enabled(context.scene, 'qwen_regularize_context_mask'))
+ or _regularized_context(context.scene))
engine = 'CYCLES' if use_part_ids else 'BLENDER_WORKBENCH'
if context.scene.render.engine != engine:
context.scene.render.engine = engine
diff --git a/stablegen/ui/panel.py b/stablegen/ui/panel.py
index 6bf0dd1..6b914b4 100644
--- a/stablegen/ui/panel.py
+++ b/stablegen/ui/panel.py
@@ -1529,12 +1529,6 @@ class StableGenPanel(bpy.types.Panel):
neutral = context_exp.row()
neutral.enabled = scene.qwen_latent_locked_context
neutral.prop(scene, "qwen_latent_lock_neutral_fill", toggle=True)
- regularize = context_exp.column()
- regularize.enabled = scene.qwen_use_confidence_context
- regularize.prop(scene, "qwen_regularize_context_mask", toggle=True)
- if scene.qwen_regularize_context_mask:
- for key in ("qwen_mask_min_island_area", "qwen_mask_min_hole_area", "qwen_mask_min_strip_width", "qwen_mask_uncertainty_dilation", "qwen_mask_keep_largest_components"):
- regularize.prop(scene, key)
context_exp.prop(scene, "qwen_use_projection_write_masks", text="Restrict Projection to Write Mask", toggle=True)
context_exp.prop(scene, "qwen_reject_residual_fallback", toggle=True)
if scene.qwen_reject_residual_fallback:
@@ -1849,16 +1843,19 @@ class StableGenPanel(bpy.types.Panel):
threshold_row.prop(
scene, "qwen_trust_threshold", text="Minimum trusted view quality"
)
- else:
- dilation_row = context_box.row()
- dilation_row.prop(scene, "qwen_context_fallback_dilation", text="Legacy context expansion (px)")
- cleanup_row = context_box.row()
- cleanup_row.prop(scene, "qwen_context_cleanup", text="Legacy fallback 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 tolerance (°)")
- row = context_box.row()
- row.prop(scene, "qwen_context_cleanup_value_adjust", text="Brightness adjustment")
+ context_box.prop(scene, "qwen_regularize_context_mask", text="Regularize Context Boundary", toggle=True)
+ if scene.qwen_regularize_context_mask:
+ for key in ("qwen_mask_min_island_area", "qwen_mask_min_hole_area", "qwen_mask_min_strip_width", "qwen_mask_uncertainty_dilation", "qwen_mask_keep_largest_components"):
+ context_box.prop(scene, key)
+ dilation_row = context_box.row()
+ dilation_row.prop(scene, "qwen_context_fallback_dilation", text="Context expansion (px)")
+ cleanup_row = context_box.row()
+ cleanup_row.prop(scene, "qwen_context_cleanup", text="Fallback 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 tolerance (°)")
+ row = context_box.row()
+ row.prop(scene, "qwen_context_cleanup_value_adjust", text="Brightness adjustment")
elif scene.model_architecture == 'sdxl' or scene.model_architecture == 'flux1':
# IPAdapter Parameters
@@ -2430,33 +2427,43 @@ class StableGenPanel(bpy.types.Panel):
context_box.prop(
scene, "qwen_trust_threshold", text="Minimum trusted view quality", slider=True
)
- else:
+ context_box.prop(
+ scene,
+ "qwen_regularize_context_mask",
+ text="Regularize Context Boundary",
+ toggle=True,
+ )
+ if scene.qwen_regularize_context_mask:
+ for key in ("qwen_mask_min_island_area", "qwen_mask_min_hole_area",
+ "qwen_mask_min_strip_width", "qwen_mask_uncertainty_dilation",
+ "qwen_mask_keep_largest_components"):
+ context_box.prop(scene, key)
+ context_box.prop(
+ scene,
+ "qwen_context_fallback_dilation",
+ text="Context expansion (px)",
+ slider=True,
+ )
+ context_box.prop(
+ scene,
+ "qwen_context_cleanup",
+ text="Fallback cleanup",
+ toggle=True,
+ icon="BRUSH_DATA",
+ )
+ if scene.qwen_context_cleanup:
context_box.prop(
scene,
- "qwen_context_fallback_dilation",
- text="Legacy context expansion (px)",
+ "qwen_context_cleanup_hue_tolerance",
+ text="Hue tolerance (°)",
slider=True,
)
context_box.prop(
scene,
- "qwen_context_cleanup",
- text="Legacy fallback cleanup",
- toggle=True,
- icon="BRUSH_DATA",
+ "qwen_context_cleanup_value_adjust",
+ text="Brightness adjustment",
+ slider=True,
)
- if scene.qwen_context_cleanup:
- context_box.prop(
- scene,
- "qwen_context_cleanup_hue_tolerance",
- text="Hue tolerance (°)",
- slider=True,
- )
- context_box.prop(
- scene,
- "qwen_context_cleanup_value_adjust",
- text="Brightness adjustment",
- slider=True,
- )
actions = preview_box.row(align=True)
actions.operator(
diff --git a/stablegen/ui/presets.py b/stablegen/ui/presets.py
index 54da9a6..8df1ec1 100644
--- a/stablegen/ui/presets.py
+++ b/stablegen/ui/presets.py
@@ -165,7 +165,7 @@ PRESETS["QWEN 2.1 CONTEXT + REFERENCE"] = {
"then resets to 90 degrees for the final material; blending exponent is 7. "
"Uses 20 steps, CFG 1.0, DPM++ SDE, SGM Uniform and no LoRA. "
"Confidence-Gated Context, Acquisition Quality Weighting and Depth/Part "
- "Projection Guard are off; legacy fallback dilation is 1 px, cleanup is "
+ "Projection Guard are off; context expansion is 1 px, fallback cleanup is "
"off and backface projection is allowed. Captures the current manually "
"tuned setup; intended for comparing context coverage and appearance "
"consistency across overlapping views."
diff --git a/stablegen/ui/studio_cards.py b/stablegen/ui/studio_cards.py
index 9c129c4..c0c5ddb 100644
--- a/stablegen/ui/studio_cards.py
+++ b/stablegen/ui/studio_cards.py
@@ -169,7 +169,7 @@ SCHEMAS = {
'sg_rework_full_resolution_scale', 'sg_rework_final_display')),
feature('sg_auto_view_sequence', 'Automatic view sequence', 'camera'),
Spec('context', 'Experimental Qwen context', 'shield', ('qwen_global_style_vlm_only',
- 'qwen_context_vlm_only', 'qwen_latent_locked_context', 'qwen_regularize_context_mask',
+ 'qwen_context_vlm_only', 'qwen_latent_locked_context',
'qwen_use_projection_write_masks', 'qwen_reject_residual_fallback')),
),
'queue': (
@@ -274,8 +274,8 @@ LABELS = {
'qwen_guidance_fallback_color': 'Missing-context color',
'qwen_guidance_background_color': 'Image background',
'qwen_voronoi_mode': 'Hard view segmentation',
- 'qwen_context_fallback_dilation': 'Legacy context expansion (px)',
- 'qwen_context_cleanup': 'Legacy fallback cleanup',
+ 'qwen_context_fallback_dilation': 'Context expansion (px)',
+ 'qwen_context_cleanup': 'Fallback cleanup',
'qwen_context_cleanup_hue_tolerance': 'Hue tolerance (°)',
'qwen_context_cleanup_value_adjust': 'Brightness adjustment',
'blur_mask_radius': 'Mask blur radius (px)',
@@ -327,7 +327,7 @@ LABELS = {
'sg_rework_final_display': 'Rebuild final projection',
'sg_auto_view_sequence': 'Automatic view sequence',
'qwen_latent_locked_context': 'Lock reliable context during sampling',
- 'qwen_regularize_context_mask': 'Clean repair-mask boundaries',
+ 'qwen_regularize_context_mask': 'Regularize context boundary',
'qwen_mask_min_island_area': 'Minimum trusted island (px²)',
'qwen_mask_min_hole_area': 'Minimum repair hole (px²)',
'qwen_mask_min_strip_width': 'Minimum trusted strip (px)',
diff --git a/stablegen/ui/studio_hierarchy.py b/stablegen/ui/studio_hierarchy.py
index 3426140..4f8f54b 100644
--- a/stablegen/ui/studio_hierarchy.py
+++ b/stablegen/ui/studio_hierarchy.py
@@ -35,9 +35,9 @@ FAMILIES = {
'qwen_prompt_template': ('qwen_custom_prompt_initial', 'qwen_custom_prompt_seq_none',
'qwen_custom_prompt_seq_replace', 'qwen_custom_prompt_seq_additional'),
'qwen_context_render_mode': ('qwen_prompt_gray_background', 'qwen_guidance_background_color',
- 'qwen_guidance_fallback_color', 'qwen_use_confidence_context'),
- 'qwen_use_confidence_context': ('qwen_trust_threshold', 'qwen_context_fallback_dilation',
- 'qwen_context_cleanup'),
+ 'qwen_guidance_fallback_color', 'qwen_use_confidence_context', 'qwen_regularize_context_mask',
+ 'qwen_context_fallback_dilation', 'qwen_context_cleanup'),
+ 'qwen_use_confidence_context': ('qwen_trust_threshold',),
'qwen_context_cleanup': ('qwen_context_cleanup_hue_tolerance', 'qwen_context_cleanup_value_adjust'),
'qwen_regularize_context_mask': ('qwen_mask_min_island_area', 'qwen_mask_min_hole_area',
'qwen_mask_min_strip_width', 'qwen_mask_uncertainty_dilation', 'qwen_mask_keep_largest_components'),