Skip to content

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

revamp qwen edit workflow with context cleanup, add new qwen presets, add qwen fallback and background color parameters to panel

sakalond2a49191e124d
diff --git a/stablegen/__init__.py b/stablegen/__init__.py
index 4b9fe02..40d4dbc 100755
--- a/stablegen/__init__.py
+++ b/stablegen/__init__.py
@@ -1737,6 +1737,60 @@ def register():
update=update_parameters
)
+ bpy.types.Scene.qwen_guidance_fallback_color = bpy.props.FloatVectorProperty(
+ name="Guidance Fallback Color",
+ description="Color used for fallback regions in the Qwen context render",
+ subtype='COLOR',
+ default=(1.0, 0.0, 1.0),
+ min=0.0,
+ max=1.0,
+ update=update_parameters
+ )
+
+ bpy.types.Scene.qwen_guidance_background_color = bpy.props.FloatVectorProperty(
+ name="Guidance Background Color",
+ description="Background color for the Qwen context render",
+ 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="Use Context Cleanup",
+ description="Replace fallback color in subsequent Qwen renders before projection",
+ default=False,
+ update=update_parameters
+ )
+
+ bpy.types.Scene.qwen_context_cleanup_hue_tolerance = bpy.props.FloatProperty(
+ name="Cleanup Hue Tolerance",
+ description="Hue tolerance in degrees for identifying fallback regions during cleanup",
+ default=5.0,
+ min=0.0,
+ max=180.0,
+ update=update_parameters
+ )
+
+ bpy.types.Scene.qwen_context_cleanup_value_adjust = bpy.props.FloatProperty(
+ name="Cleanup Value Adjustment",
+ description="Adjust value (brightness) for cleaned pixels. -1 darkens to black, 1 brightens to white.",
+ default=0.0,
+ min=-1.0,
+ max=1.0,
+ update=update_parameters
+ )
+
+ bpy.types.Scene.qwen_context_fallback_dilation = bpy.props.IntProperty(
+ name="Fallback Dilation",
+ 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.output_timestamp = bpy.props.StringProperty(
name="Output Timestamp",
description="Timestamp for generation output directory",
@@ -1972,6 +2026,12 @@ def unregister():
del bpy.types.Scene.qwen_custom_prompt_seq_none
del bpy.types.Scene.qwen_custom_prompt_seq_replace
del bpy.types.Scene.qwen_custom_prompt_seq_additional
+ del bpy.types.Scene.qwen_guidance_fallback_color
+ del bpy.types.Scene.qwen_guidance_background_color
+ del bpy.types.Scene.qwen_context_cleanup
+ del bpy.types.Scene.qwen_context_cleanup_hue_tolerance
+ del bpy.types.Scene.qwen_context_cleanup_value_adjust
+ del bpy.types.Scene.qwen_context_fallback_dilation
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
diff --git a/stablegen/generator.py b/stablegen/generator.py
index 4399f6b..b0ba63d 100644
--- a/stablegen/generator.py
+++ b/stablegen/generator.py
@@ -1,6 +1,7 @@
import os
import bpy # pylint: disable=import-error
import numpy as np
+import cv2
import uuid
import json
@@ -10,9 +11,11 @@ import threading
import requests
import traceback
import json
+import io
from datetime import datetime
import math
+import colorsys
from PIL import Image, ImageEnhance
from .util.helpers import prompt_text, prompt_text_img2img, prompt_text_qwen_image_edit # pylint: disable=relative-beyond-top-level
@@ -349,6 +352,14 @@ class ComfyUIGenerate(bpy.types.Operator):
self._wait_event = threading.Event()
self.workflow_manager = WorkflowManager(self)
+ def _get_qwen_context_colors(self, context):
+ fallback = (1.0, 0.0, 1.0)
+ background = (1.0, 0.0, 1.0)
+ if context.scene.qwen_context_render_mode in {'REPLACE_STYLE', 'ADDITIONAL'}:
+ fallback = tuple(context.scene.qwen_guidance_fallback_color)
+ background = tuple(context.scene.qwen_guidance_background_color)
+ return fallback, background
+
@classmethod
def poll(cls, context):
"""
@@ -858,7 +869,9 @@ class ComfyUIGenerate(bpy.types.Operator):
# Export visibility mask and render for the current camera, we need to use a callback to be in the main thread
export_visibility(context, self._to_texture, camera_visibility=self._cameras[self._current_image - 1]) # Export mask for current view
if context.scene.model_architecture == 'qwen_image_edit': # export custom bg and fallback for Qwen image edit
- export_emit_image(context, self._to_texture, camera_id=self._current_image, bg_color=(1.0, 0.0, 1.0), fallback_color=(1.0, 0.0, 1.0)) # Export render for next view
+ fallback_color, background_color = self._get_qwen_context_colors(context)
+ export_emit_image(context, self._to_texture, camera_id=self._current_image, bg_color=background_color, fallback_color=fallback_color) # Export render for next view
+ self._dilate_qwen_context_fallback(context, self._current_image, fallback_color)
else:
export_emit_image(context, self._to_texture, camera_id=self._current_image, bg_color=context.scene.fallback_color, fallback_color=context.scene.fallback_color) # Export render for next view
self._wait_event.set()
@@ -885,6 +898,13 @@ class ComfyUIGenerate(bpy.types.Operator):
if image == {"error": "conn_failed"}:
return # Error message already set
+
+ if (context.scene.model_architecture == 'qwen_image_edit' 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':
@@ -1044,6 +1064,117 @@ class ComfyUIGenerate(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"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"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)
+
+ 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" 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" Warning: Failed to write cleaned Qwen context render: {err}")
+ traceback.print_exc()
+ return image_bytes
+
def export_depthmap(self, context, camera_id=None):
"""
Exports the depth map of the scene.
diff --git a/stablegen/stablegen.py b/stablegen/stablegen.py
index 231b480..193d109 100755
--- a/stablegen/stablegen.py
+++ b/stablegen/stablegen.py
@@ -14,7 +14,9 @@ PRESETS = {
"QUICK DRAFT": {"description": "Optimized for speed", "control_after_generate": "fixed", "model_architecture": "sdxl", "steps": 4, "cfg": 1.0, "sampler": "dpmpp_2s_ancestral", "scheduler": "sgm_uniform", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "grid", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.7, "sequential_factor_smooth": 0.1, "sequential_ipadapter": True, "sequential_ipadapter_mode": "first", "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 1.0, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "differential_diffusion": True, "differential_noise": True, "blur_mask": True, "blur_mask_radius": 2, "blur_mask_sigma": 1.0, "grow_mask_by": 2, "canny_threshold_low": 0, "canny_threshold_high": 80, "controlnet_units": [{'unit_type': 'depth', 'model_name': 'controlnet_depth_sdxl.safetensors', 'strength': 0.5, 'start_percent': 0.0, 'end_percent': 1.0, 'is_union': False, 'use_union_type': True}], "lora_units": [{'model_name': 'Hyper-SDXL-4steps-lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]},
"UV INPAINTING": {"description": "Recommended UV Inpainting setup. It is recommended to bake texutures manually before running the generation to fine-tune unwrapping and avoid lag when generating.", "control_after_generate": "fixed", "model_architecture": "sdxl", "steps": 10, "cfg": 1.5, "sampler": "dpmpp_2s_ancestral", "scheduler": "sgm_uniform", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 80.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "uv_inpaint", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "3,0,1,2", "sequential_factor": 0.6000000238418579, "sequential_factor_smooth": 0.11000001430511475, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "first", "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 1.0, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "differential_diffusion": True, "differential_noise": True, "blur_mask": True, "blur_mask_radius": 3, "blur_mask_sigma": 1.0, "grow_mask_by": 3, "canny_threshold_low": 0, "canny_threshold_high": 80, "lora_units": [{'model_name': 'sdxl_lightning_8step_lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]}, # No ControlNet for UV Inpainting by default
"ARCHITECTURE": {"description": "Prioritizes only the most straight-on camera for each point. This means details generated on flat surfaces will not get blurred by getting generated differently from two or more viewpoints. Does not use visibility masking. Each picture will get generated as new, consistency depends on IPAdapter + geometry.", "control_after_generate": "fixed", "model_architecture": "sdxl", "steps": 8, "cfg": 1.5, "sampler": "dpmpp_2s_ancestral", "scheduler": "sgm_uniform", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 80.0, "weight_exponent": 10.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "separate", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": False, "sequential_custom_camera_order": "", "sequential_factor": 0.75, "sequential_factor_smooth": 0.15000000596046448, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "first", "ipadapter_weight_type": "style", "ipadapter_strength": 0.800000011920929, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "differential_diffusion": True, "differential_noise": False, "blur_mask": True, "blur_mask_radius": 3, "blur_mask_sigma": 1.0, "grow_mask_by": 3, "canny_threshold_low": 0, "canny_threshold_high": 80, "controlnet_units": [{'unit_type': 'depth', 'model_name': 'controlnet_depth_sdxl.safetensors', 'strength': 0.6000000238418579, 'start_percent': 0.0, 'end_percent': 1.0, 'is_union': False, 'use_union_type': True}], "lora_units": [{'model_name': 'sdxl_lightning_8step_lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]},
- "QWEN IMAGE EDIT": {"description": "Recommended settings for Qwen Image Edit. Uses depth map for guidance and sequential mode for consistency.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 90.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.7, "sequential_factor_smooth": 0.15, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "first", "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 1.0, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "differential_diffusion": True, "differential_noise": True, "blur_mask": True, "blur_mask_radius": 1, "blur_mask_sigma": 1.0, "grow_mask_by": 3, "canny_threshold_low": 0, "canny_threshold_high": 80, "qwen_guidance_map_type": "depth", "qwen_use_external_style_image": False, "qwen_external_style_image": "", "qwen_context_render_mode": "NONE"},
+ "QWEN EDIT PRECISE": {"description": "Precise detail when camera overlap is good. Relies on context renders plus the prompt, so sparse coverage can still introduce artifacts.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.699999988079071, "sequential_factor_smooth": 0.15000000596046448, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": False, "sequential_ipadapter_mode": "first", "sequential_desaturate_factor": 0.0, "sequential_contrast_factor": 0.0, "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 1.0, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "differential_diffusion": True, "differential_noise": True, "blur_mask": True, "blur_mask_radius": 1, "blur_mask_sigma": 1.0, "grow_mask_by": 3, "canny_threshold_low": 0, "canny_threshold_high": 80, "qwen_guidance_map_type": "depth", "qwen_use_external_style_image": False, "qwen_external_style_image": "", "qwen_context_render_mode": "REPLACE_STYLE", "qwen_external_style_initial_only": False, "qwen_use_custom_prompts": False, "qwen_custom_prompt_initial": "Change the format of image 1 to '{main_prompt}'", "qwen_custom_prompt_seq_none": "Change and transfer the format of '{main_prompt}' in image 1 to the style from image 2", "qwen_custom_prompt_seq_replace": "Change and transfer the format of image 1 to '{main_prompt}'. Replace all solid magenta areas in image 2. Replace the background with solid gray. The style from image 2 should smoothly continue into the previously magenta areas.", "qwen_custom_prompt_seq_additional": "Change and transfer the format of image 1 to '{main_prompt}'. Replace all solid magenta areas in image 2. Replace the background with solid gray. The style from image 2 should smoothly continue into the previously magenta areas. Image 3 represents the overall style of the object.", "qwen_guidance_fallback_color": mathutils.Color((1.0, 0.0, 1.0)), "qwen_guidance_background_color": mathutils.Color((1.0, 0.0, 1.0)), "qwen_context_cleanup": False, "qwen_context_cleanup_hue_tolerance": 0.0, "qwen_context_cleanup_value_adjust": 0.0, "qwen_context_fallback_dilation": 1, "lora_units": [{'model_name': 'Qwen-Image-Lightning-4steps-V1.0.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
+ "QWEN EDIT SAFE": {"description": "Safer fallback when coverage is limited. Uses the previous view (recent mode) instead of context renders to keep global look coherent, at the cost of some fine-detail persistence.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "weight_exponent": 6.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.699999988079071, "sequential_factor_smooth": 0.15000000596046448, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "first", "sequential_desaturate_factor": 0.0, "sequential_contrast_factor": 0.10000000149011612, "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 1.0, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "early_priority": False, "early_priority_strength": 0.5, "differential_diffusion": True, "differential_noise": True, "blur_mask": True, "blur_mask_radius": 1, "blur_mask_sigma": 1.0, "grow_mask_by": 3, "canny_threshold_low": 0, "canny_threshold_high": 80, "qwen_guidance_map_type": "depth", "qwen_use_external_style_image": False, "qwen_external_style_image": "", "qwen_context_render_mode": "NONE", "qwen_external_style_initial_only": False, "qwen_use_custom_prompts": False, "qwen_custom_prompt_initial": "Change the format of image 1 to '{main_prompt}'", "qwen_custom_prompt_seq_none": "Change and transfer the format of '{main_prompt}' in image 1 to the style from image 2", "qwen_custom_prompt_seq_replace": "Change and transfer the format of image 1 to '{main_prompt}'. Replace all solid magenta areas in image 2. Replace the background with solid gray. The style from image 2 should smoothly continue into the previously magenta areas.", "qwen_custom_prompt_seq_additional": "Change and transfer the format of image 1 to '{main_prompt}'. Replace all solid magenta areas in image 2. Replace the background with solid gray. The style from image 2 should smoothly continue into the previously magenta areas. Image 3 represents the overall style of the object.", "qwen_guidance_fallback_color": mathutils.Color((1.0, 0.0, 1.0)), "qwen_guidance_background_color": mathutils.Color((1.0, 0.0, 1.0)), "qwen_context_cleanup": False, "qwen_context_cleanup_hue_tolerance": 0.0, "qwen_context_cleanup_value_adjust": 0.0, "qwen_context_fallback_dilation": 1, "lora_units": [{'model_name': 'Qwen-Image-Lightning-4steps-V1.0.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
+ "QWEN EDIT ALT": {"description": "Balanced option that mixes additional context renders with sequential references to smooth out coverage while keeping detail reasonable.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.699999988079071, "sequential_factor_smooth": 0.15000000596046448, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "recent", "sequential_desaturate_factor": 0.0, "sequential_contrast_factor": 0.10000000149011612, "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 1.0, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "differential_diffusion": True, "differential_noise": True, "blur_mask": True, "blur_mask_radius": 1, "blur_mask_sigma": 1.0, "grow_mask_by": 3, "canny_threshold_low": 0, "canny_threshold_high": 80, "qwen_guidance_map_type": "depth", "qwen_use_external_style_image": False, "qwen_external_style_image": "", "qwen_context_render_mode": "ADDITIONAL", "qwen_external_style_initial_only": False, "qwen_use_custom_prompts": False, "qwen_custom_prompt_initial": "Change the format of image 1 to '{main_prompt}'", "qwen_custom_prompt_seq_none": "Change and transfer the format of '{main_prompt}' in image 1 to the style from image 2", "qwen_custom_prompt_seq_replace": "Change and transfer the format of image 1 to '{main_prompt}'. Replace all solid magenta areas in image 2. Replace the background with solid gray. The style from image 2 should smoothly continue into the previously magenta areas.", "qwen_custom_prompt_seq_additional": "Change and transfer the format of image 1 to '{main_prompt}'. Replace all solid magenta areas in image 2. Replace the background with solid gray. The style from image 2 should smoothly continue into the previously magenta areas. Image 3 represents the overall style of the object.", "qwen_guidance_fallback_color": mathutils.Color((1.0, 0.0, 1.0)), "qwen_guidance_background_color": mathutils.Color((1.0, 0.0, 1.0)), "qwen_context_cleanup": False, "qwen_context_cleanup_hue_tolerance": 0.0, "qwen_context_cleanup_value_adjust": 0.0, "qwen_context_fallback_dilation": 1, "early_priority": True, "early_priority_strength": 0.5, "lora_units": [{'model_name': 'Qwen-Image-Lightning-4steps-V1.0.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
}
# Global list of all generation parameter names to check for a preset.
@@ -58,6 +60,8 @@ GEN_PARAMETERS = [
"ipadapter_strength",
"ipadapter_start",
"ipadapter_end",
+ "early_priority",
+ "early_priority_strength",
"differential_diffusion",
"differential_noise",
"blur_mask",
@@ -76,6 +80,12 @@ GEN_PARAMETERS = [
"qwen_custom_prompt_seq_none",
"qwen_custom_prompt_seq_replace",
"qwen_custom_prompt_seq_additional",
+ "qwen_guidance_fallback_color",
+ "qwen_guidance_background_color",
+ "qwen_context_cleanup",
+ "qwen_context_cleanup_hue_tolerance",
+ "qwen_context_cleanup_value_adjust",
+ "qwen_context_fallback_dilation",
]
def get_preset_items(self, context):
@@ -540,17 +550,25 @@ class StableGenPanel(bpy.types.Panel):
row.prop(scene, "qwen_external_style_initial_only", text="External for Initial Only", toggle=True)
if scene.qwen_use_external_style_image and scene.qwen_external_style_initial_only:
- split = style_box.split(factor=0.5)
+ subsequent_box = style_box.box()
+ split = subsequent_box.split(factor=0.5)
split.label(text="Subsequent mode:")
split.prop(scene, "sequential_ipadapter_mode", text="")
+ if scene.sequential_ipadapter_mode == 'recent':
+ subsequent_box.prop(scene, "sequential_desaturate_factor", text="Desaturate")
+ subsequent_box.prop(scene, "sequential_contrast_factor", text="Reduce Contrast")
if not scene.qwen_use_external_style_image and scene.generation_method in ['sequential', 'separate']:
row = content_box.row()
row.prop(scene, "sequential_ipadapter", text="Use Previous Image as Style", toggle=True, icon="MODIFIER")
if scene.sequential_ipadapter:
- split = content_box.split(factor=0.5)
+ sub_ip_box = content_box.box()
+ split = sub_ip_box.split(factor=0.5)
split.label(text="Mode:")
split.prop(scene, "sequential_ipadapter_mode", text="")
+ if scene.sequential_ipadapter_mode == 'recent':
+ sub_ip_box.prop(scene, "sequential_desaturate_factor", text="Desaturate")
+ sub_ip_box.prop(scene, "sequential_contrast_factor", text="Reduce Contrast")
if scene.generation_method == 'sequential':
split = content_box.split(factor=0.5)
@@ -589,6 +607,30 @@ class StableGenPanel(bpy.types.Panel):
op = row.operator("stablegen.reset_qwen_prompt", text="", icon='FILE_REFRESH')
op.prompt_type = op_prop
+ if (scene.generation_method == 'sequential' and
+ scene.qwen_context_render_mode in {'REPLACE_STYLE', 'ADDITIONAL'}):
+ context_box = content_box.box()
+ context_box.label(text="Context Render Options")
+
+ if scene.qwen_use_custom_prompts:
+ colors_box = context_box.box()
+ colors_box.label(text="Context Render Colors")
+ row = colors_box.row()
+ row.prop(scene, "qwen_guidance_background_color", text="Background")
+ 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)")
+
+ 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")
+
elif scene.model_architecture == 'sdxl' or scene.model_architecture == 'flux1':
# ControlNet settings
cn_box = content_box.box()
@@ -1066,6 +1108,11 @@ class SavePreset(bpy.types.Operator):
if self.include_controlnet:
print(f'"controlnet_units": {controlnet_units},', end="")
print("},")
+
+ # Print LoRA units in a compact format if included
+ if self.include_loras:
+ print(f'"lora_units": {lora_units_data},', end="")
+ print("},")
return {'FINISHED'}
def invoke(self, context, event):
diff --git a/stablegen/workflows.py b/stablegen/workflows.py
index 5970e42..1228b61 100644
--- a/stablegen/workflows.py
+++ b/stablegen/workflows.py
@@ -25,11 +25,16 @@ class WorkflowManager:
is_generating = hasattr(self.operator, '_current_image')
is_subsequent_in_sequence = is_generating and self.operator._current_image > 0
- style_image_provided = (
- context.scene.qwen_use_external_style_image or
- ((not is_generating or is_subsequent_in_sequence) and
- (context.scene.sequential_ipadapter or context.scene.qwen_context_render_mode == 'REPLACE_STYLE'))
- )
+ if is_initial_image:
+ # First frame only has a style reference when an explicit external image is supplied.
+ style_image_provided = context.scene.qwen_use_external_style_image
+ else:
+ # Later frames can draw style from previous renders, external sources, or context renders.
+ style_image_provided = (
+ context.scene.qwen_use_external_style_image or
+ context.scene.sequential_ipadapter or
+ context.scene.qwen_context_render_mode in {'REPLACE_STYLE', 'ADDITIONAL'}
+ )
context_mode = context.scene.qwen_context_render_mode
if is_initial_image:
@@ -102,6 +107,7 @@ class WorkflowManager:
context_render_info = None
context_mode = context.scene.qwen_context_render_mode
remove_context = False
+ style_requires_scaling = False
# --- Camera Prompt Injection ---
if context.scene.use_camera_prompts and self.operator._cameras and self.operator._current_image < len(self.operator._cameras):
@@ -158,6 +164,7 @@ class WorkflowManager:
if not style_image_info:
self.operator._error = "External style image enabled, but file not found or could not be uploaded."
return {"error": "conn_failed"}
+ style_requires_scaling = True
if context_mode != 'ADDITIONAL':
if context.scene.qwen_use_custom_prompts:
pos_prompt_text = (context.scene.qwen_custom_prompt_initial if is_initial_image else context.scene.qwen_custom_prompt_seq_none).format(main_prompt=user_prompt)
@@ -203,6 +210,24 @@ class WorkflowManager:
else:
# A style image is provided, so set the loader input.
prompt[NODES['style_map_loader']]['inputs']['image'] = style_image_info['name']
+ if style_requires_scaling:
+ scale_node_int = 600
+ while str(scale_node_int) in prompt:
+ scale_node_int += 1
+ scale_node_key = str(scale_node_int)
+ prompt[scale_node_key] = {
+ "inputs": {
+ "upscale_method": "lanczos",
+ "megapixels": 1,
+ "image": [NODES['style_map_loader'], 0]
+ },
+ "class_type": "ImageScaleToTotalPixels",
+ "_meta": {
+ "title": "Scale Image to Total Pixels"
+ }
+ }
+ prompt[NODES['pos_prompt']]['inputs']['image2'] = [scale_node_key, 0]
+ prompt[NODES['neg_prompt']]['inputs']['image2'] = [scale_node_key, 0]
# --- Configure Sampler ---
prompt[NODES['sampler']]['inputs']['seed'] = context.scene.seed
@@ -383,8 +408,8 @@ class WorkflowManager:
"""Sets the generation resolution based on mode."""
if context.scene.generation_method == 'grid':
# Use the resolution of the grid image
- prompt[NODES['latent']]["inputs"]["width"] = self._grid_width
- prompt[NODES['latent']]["inputs"]["height"] = self._grid_height
+ prompt[NODES['latent']]["inputs"]["width"] = self.operator._grid_width
+ prompt[NODES['latent']]["inputs"]["height"] = self.operator._grid_height
else:
# Use current render resolution
prompt[NODES['latent']]["inputs"]["width"] = context.scene.render.resolution_x
@@ -644,7 +669,7 @@ class WorkflowManager:
elif message['type'] == 'progress':
progress = (message['data']['value'] / message['data']['max']) * 100
if progress != 0:
- self._progress = progress # Update progress for UI
+ self.operator._progress = progress # Update progress for UI
print(f"Progress: {progress:.1f}%")
else:
# Binary data (image)