Skip to content

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

refactor: decompose monolithic addon into component packages

sakalond33e66e1a1c4e
diff --git a/stablegen/__init__.py b/stablegen/__init__.py
index f132985..b64ee3f 100644
--- a/stablegen/__init__.py
+++ b/stablegen/__init__.py
@@ -1,4062 +1,161 @@
-""" This script registers the addon. """
-import bpy # pylint: disable=import-error
-from .stablegen import StableGenPanel, ApplyPreset, SavePreset, DeletePreset, get_preset_items, update_parameters, ResetQwenPrompt, SwitchToMeshGeneration
-from .stablegen import SG_UL_SceneQueueList, SceneQueueAdd, SceneQueueRemove, SceneQueueClear, SceneQueueMoveUp, SceneQueueMoveDown, SceneQueueOpenResult, SceneQueueInvalidate, SceneQueueProcess
-from .render_tools import BakeTextures, AddCameras, CloneCamera, MirrorCamera, ToggleCameraLabels, SwitchMaterial, ExportOrbitGIF, ExportForGameEngine, CollectCameraPrompts, CameraPromptItem, CameraOrderItem, SG_UL_CameraOrderList, SyncCameraOrder, MoveCameraOrder, ApplyCameraOrderPreset
-from .debug_tools import debug_classes as _debug_classes
-from .utils import AddHDRI, ApplyModifiers, CurvesToMesh, sg_modal_active
-from .generator import ComfyUIGenerate, Reproject, Regenerate, MirrorReproject, Trellis2Generate
-import os
-import requests
-import json
-from bpy.app.handlers import persistent
-from urllib.parse import urlparse
-
-bl_info = {
- "name": "StableGen",
- "category": "Object",
- "author": "Ondrej Sakala",
- "version": (0, 3, 0),
- 'blender': (4, 2, 0)
-}
-
-classes = [
- StableGenPanel,
- ApplyPreset,
- SwitchToMeshGeneration,
- SavePreset,
- DeletePreset,
- ResetQwenPrompt,
- BakeTextures,
- AddCameras,
- CloneCamera,
- MirrorCamera,
- ToggleCameraLabels,
- SwitchMaterial,
- ExportOrbitGIF,
- ExportForGameEngine,
- CollectCameraPrompts,
- CameraPromptItem,
- CameraOrderItem,
- SG_UL_CameraOrderList,
- SyncCameraOrder,
- MoveCameraOrder,
- ApplyCameraOrderPreset,
- AddHDRI,
- ApplyModifiers,
- CurvesToMesh,
- ComfyUIGenerate,
- Reproject,
- Regenerate,
- MirrorReproject,
- Trellis2Generate,
- SG_UL_SceneQueueList,
- SceneQueueAdd,
- SceneQueueRemove,
- SceneQueueClear,
- SceneQueueMoveUp,
- SceneQueueMoveDown,
- SceneQueueOpenResult,
- SceneQueueInvalidate,
- SceneQueueProcess,
-]
-
-# Global caches for model lists fetched via API
-_cached_checkpoint_list = [("NONE_AVAILABLE", "None available", "Fetch models from server")]
-_cached_lora_list = [("NONE_AVAILABLE", "None available", "Fetch models from server")]
-_cached_checkpoint_architecture = None
-_pending_checkpoint_refresh_architecture = None
-
-# Counter for in-flight async refresh operations (checkpoint, LoRA, controlnet).
-# The UI checks this to display a "Refreshing…" indicator.
-_pending_refreshes = 0
-_refresh_started_at = 0.0 # monotonic timestamp of first in-flight refresh
-_REFRESH_TIMEOUT = 30.0 # seconds before we force-clear a stuck indicator
-
-import time as _time_mod
-
-def _inc_pending_refreshes():
- """Increment the in-flight refresh counter (call from main thread)."""
- global _pending_refreshes, _refresh_started_at
- if _pending_refreshes <= 0:
- _refresh_started_at = _time_mod.monotonic()
- _pending_refreshes += 1
-
-def _dec_pending_refreshes():
- """Decrement the in-flight refresh counter (call from main thread)."""
- global _pending_refreshes, _refresh_started_at
- _pending_refreshes = max(0, _pending_refreshes - 1)
- if _pending_refreshes <= 0:
- _refresh_started_at = 0.0
-
-# ---------------------------------------------------------------------------
-# Async network helper — run blocking I/O off the main thread
-# ---------------------------------------------------------------------------
-import threading as _threading
-import traceback as _traceback
-
-# Monotonically incrementing token so we can discard stale results when the
-# server address changes while a request is still in-flight.
-_async_generation = 0
-
-def _run_async(work_fn, done_fn, poll_interval=0.25, track_generation=False):
- """Run *work_fn* in a background thread; call *done_fn(result)* on the
- main thread via ``bpy.app.timers`` when finished.
-
- *work_fn* receives no arguments and should return a result dict/object.
- *done_fn* receives that result. Both run without any lock — *done_fn*
- is guaranteed to execute on the main Blender thread.
-
- If *track_generation* is True the call increments the global
- ``_async_generation`` counter and the result is silently discarded if
- a newer tracked call was started before this one finishes. Use this
- only for server-address-change callbacks where stale results must be
- dropped; refresh operators should leave it False so their results are
- never accidentally discarded.
- """
- global _async_generation
- if track_generation:
- _async_generation += 1
- gen = _async_generation
-
- container = {} # mutable box for the thread to deposit its result
-
- def _worker():
- try:
- container['result'] = work_fn()
- except Exception:
- _traceback.print_exc()
- container['result'] = None
-
- t = _threading.Thread(target=_worker, daemon=True)
- t.start()
-
- def _poll():
- # Discard result if the server address changed after we started.
- if gen != _async_generation:
- # Still call done_fn so it can clean up (e.g. decrement
- # _pending_refreshes). Pass None to signal stale/no result.
- try:
- done_fn(None)
- except Exception:
- _traceback.print_exc()
- return None # stop polling — stale
- if t.is_alive():
- return poll_interval # keep polling
- try:
- done_fn(container.get('result'))
- except Exception:
- _traceback.print_exc()
- return None # done
- bpy.app.timers.register(_poll, first_interval=poll_interval)
-
-
-def update_architecture_mode(self, context):
- """Called when the user changes the architecture_mode dropdown.
-
- For standard diffusion modes (sdxl / flux1 / qwen_image_edit) the hidden
- ``model_architecture`` property is synced which in turn triggers the
- existing ``update_combined`` callback (server check, checkpoint refresh,
- etc.).
-
- For trellis2 mode the diffusion backbone is only synced when the
- active ``trellis2_texture_mode`` is itself a diffusion architecture.
- """
- scene = context.scene
- mode = scene.architecture_mode
-
- if mode != 'trellis2':
- # Standard diffusion — sync backbone and let its callback refresh
- if scene.model_architecture != mode:
- scene.model_architecture = mode # triggers update_combined
- else:
- # Entering TRELLIS.2 mode — sync backbone for any diffusion need
- _sync_trellis2_backbone(scene)
-
- # Trigger preset detection
- update_parameters(self, context)
-
-
-def _sync_trellis2_backbone(scene):
- """Pick the right diffusion backbone for the current TRELLIS.2 state.
-
- If the texture-mode is a diffusion arch, use that. Otherwise fall
- back to the initial-image arch (relevant when generate_from == prompt
- and texture_mode is native/none).
- """
- tex_mode = getattr(scene, 'trellis2_texture_mode', 'native')
- if tex_mode in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein'):
- target = tex_mode
- elif getattr(scene, 'trellis2_generate_from', 'image') == 'prompt':
- target = getattr(scene, 'trellis2_initial_image_arch', 'sdxl')
- else:
- return # no diffusion backbone needed
- if scene.model_architecture != target:
- scene.model_architecture = target # triggers update_combined
-
-
-def update_trellis2_texture_mode(self, context):
- """Called when the user changes the texture generation mode inside TRELLIS.2.
-
- * Syncs ``trellis2_skip_texture`` (True when *not* native).
- * If the chosen mode is a diffusion architecture and differs from the
- current backbone, syncs ``model_architecture`` (which triggers a
- checkpoint refresh).
- * When ``generate_from`` is *prompt* and texture_mode is non-diffusion,
- syncs the backbone from ``trellis2_initial_image_arch`` instead.
- """
- scene = context.scene
- if getattr(scene, 'architecture_mode', '') != 'trellis2':
- return
-
- tex_mode = scene.trellis2_texture_mode
-
- # Sync skip_texture flag used by the workflow code
- scene.trellis2_skip_texture = (tex_mode != 'native')
-
- _sync_trellis2_backbone(scene)
-
- # Trigger preset detection
- update_parameters(self, context)
-
-
-def update_trellis2_initial_image_arch(self, context):
- """Called when the user changes the initial-image architecture for TRELLIS.2.
-
- Syncs the diffusion backbone when texture_mode is native/none so the
- checkpoint list refreshes to match the chosen architecture.
- """
- scene = context.scene
- if getattr(scene, 'architecture_mode', '') != 'trellis2':
- return
- _sync_trellis2_backbone(scene)
-
- # Trigger preset detection
- update_parameters(self, context)
-
-
-def update_trellis2_generate_from(self, context):
- """Called when the user switches between Image and Prompt input mode.
-
- Syncs the diffusion backbone via ``_sync_trellis2_backbone`` so a
- checkpoint is available for the initial-image generation step.
- """
- scene = context.scene
- if getattr(scene, 'architecture_mode', '') != 'trellis2':
- return
-
- if scene.trellis2_generate_from == 'prompt':
- _sync_trellis2_backbone(scene)
-
- # Trigger preset detection
- update_parameters(self, context)
-
-
-def update_combined(self, context):
- # This now primarily updates the preset status and might trigger Enum updates implicitly
- prefs = context.preferences.addons[__package__].preferences
- raw_address = prefs.server_address
-
- if raw_address:
- # Ensure we have a scheme for correct parsing
- if not raw_address.startswith(('http://', 'https://')):
- # Prepend http scheme if it's missing
- parsed_url = urlparse(f"http://{raw_address}")
- else:
- parsed_url = urlparse(raw_address)
-
- clean_address = parsed_url.netloc
-
- # If parsing resulted in a change, update the property.
- # This will re-trigger the update function, so we return early.
- if clean_address and raw_address != clean_address:
- prefs.server_address = clean_address
- return None
-
- server_address = prefs.server_address
-
- if not server_address:
- prefs.server_online = False
- global _cached_checkpoint_list, _cached_lora_list
- _cached_checkpoint_list = [("NO_SERVER", "Set Server Address", "...")]
- _cached_lora_list = [("NO_SERVER", "Set Server Address", "...")]
- return None
-
- # ── Async: run ping + TRELLIS check + model list refresh in background ──
- # Results are applied on the main thread via _run_async's done callback.
- print("Server address changed, checking asynchronously...")
-
- def _bg_work():
- """Background thread – only network I/O, no bpy access."""
- result = {}
- result['online'] = check_server_availability(server_address, timeout=get_timeout('ping'))
- if result['online']:
- result['trellis2'] = check_trellis2_available(server_address, timeout=get_timeout('api'))
- result['pbr'] = check_pbr_available(server_address, timeout=get_timeout('api'))
- else:
- result['trellis2'] = False
- result['pbr'] = False
- return result
-
- def _on_done(result):
- """Main-thread callback – apply results to bpy properties."""
- if result is None:
- return
- prefs = bpy.context.preferences.addons[__package__].preferences
- prefs.server_online = result.get('online', False)
-
- if hasattr(bpy.context, 'scene') and bpy.context.scene:
- bpy.context.scene.trellis2_available = result.get('trellis2', False)
- bpy.context.scene.pbr_nodes_available = result.get('pbr', False)
-
- if not result.get('online', False):
- print("ComfyUI server is not reachable.")
- return
-
- # Server is online — trigger model list refresh + load_handler
- # on the main thread. The refresh operators themselves will
- # be made async individually.
- update_parameters(None, bpy.context)
- load_handler(None)
-
- def _deferred_refresh():
- try:
- bpy.ops.stablegen.refresh_checkpoint_list('INVOKE_DEFAULT')
- bpy.ops.stablegen.refresh_lora_list('INVOKE_DEFAULT')
- bpy.ops.stablegen.refresh_controlnet_mappings('INVOKE_DEFAULT')
- except Exception as e:
- print(f"Error during deferred refresh: {e}")
- return None
- bpy.app.timers.register(_deferred_refresh, first_interval=0.1)
-
- _run_async(_bg_work, _on_done, track_generation=True)
-
- # Synchronous parts that don't block (no network) can stay here:
- update_parameters(self, context)
- load_handler(None)
-
- return None
-
-
-class ControlNetModelMappingItem(bpy.types.PropertyGroup):
- """Stores info about a detected ControlNet model and its supported types."""
- name: bpy.props.StringProperty(name="Model Filename") # Read-only, set by refresh op
-
- # Use Booleans for each supported type
- supports_depth: bpy.props.BoolProperty(
- name="Depth",
- description="Check if this model supports Depth guidance",
- default=False
- ) # type: ignore
- supports_canny: bpy.props.BoolProperty(
- name="Canny",
- description="Check if this model supports Canny/Edge guidance",
- default=False
- ) # type: ignore
- supports_normal: bpy.props.BoolProperty(
- name="Normal",
- description="Check if this model supports Normal map guidance",
- default=False
- ) # type: ignore
-
-class StableGenAddonPreferences(bpy.types.AddonPreferences):
- """
- Preferences for the StableGen addon.
- """
- bl_idname = __package__
-
- server_address: bpy.props.StringProperty(
- name="Server Address",
- description="Address of the ComfyUI server",
- default="127.0.0.1:8188",
- update=update_combined
- ) # type: ignore
-
- output_dir: bpy.props.StringProperty(
- name="Output Directory",
- description="Directory to save generated outputs",
- default="",
- subtype='DIR_PATH',
- update=update_parameters
- ) # type: ignore
-
- controlnet_model_mappings: bpy.props.CollectionProperty(
- type=ControlNetModelMappingItem,
- name="ControlNet Model Mappings"
- ) # type: ignore
-
- save_blend_file: bpy.props.BoolProperty(
- name="Save Blend File",
- description="Save the current Blender file with packed textures",
- default=False,
- update=update_parameters
- ) # type: ignore
-
- controlnet_mapping_index: bpy.props.IntProperty(default=0, name="Active ControlNet Mapping Index") # type: ignore
-
- server_online: bpy.props.BoolProperty(
- name="Server Online",
- description="Indicates if the ComfyUI server is reachable",
- default=False
- ) # type: ignore
-
- overlay_color: bpy.props.FloatVectorProperty(
- name="Overlay Color",
- description="Color used for the camera aspect-ratio crop rectangle and floating view labels in the viewport",
- subtype='COLOR',
- size=3,
- min=0.0, max=1.0,
- default=(0.3, 0.5, 1.0),
- update=lambda self, ctx: [a.tag_redraw() for a in ctx.screen.areas if a.type == 'VIEW_3D'] if ctx.screen else None
- ) # type: ignore
-
- enable_debug: bpy.props.BoolProperty(
- name="Enable Debug Settings",
- description="Show diagnostic tools in the main panel for visualising projection weights, blending and coverage",
- default=False
- ) # type: ignore
-
- show_advanced: bpy.props.BoolProperty(
- name="Advanced Preferences",
- description="Show advanced preferences",
- default=False
- ) # type: ignore
-
- timeout_ping: bpy.props.FloatProperty(
- name="Ping Timeout (s)",
- description="Timeout for quick server availability checks (connectivity pings)",
- default=1.0,
- min=0.1,
- max=30.0,
- step=10,
- precision=1,
- ) # type: ignore
-
- timeout_api: bpy.props.FloatProperty(
- name="API Timeout (s)",
- description="Timeout for standard API requests (VRAM flush, queue prompt, server info)",
- default=10.0,
- min=1.0,
- max=120.0,
- step=100,
- precision=0,
- ) # type: ignore
-
- timeout_transfer: bpy.props.FloatProperty(
- name="Transfer Timeout (s)",
- description="Timeout for large data transfers (image upload, file download)",
- default=120.0,
- min=10.0,
- max=600.0,
- step=100,
- precision=0,
- ) # type: ignore
-
- timeout_reboot: bpy.props.FloatProperty(
- name="Reboot Timeout (s)",
- description="How long to wait for ComfyUI server to come back after a reboot",
- default=120.0,
- min=10.0,
- max=600.0,
- step=100,
- precision=0,
- ) # type: ignore
-
- timeout_mesh_gen: bpy.props.FloatProperty(
- name="Mesh Generation Timeout (s)",
- description="Timeout for TRELLIS.2 mesh generation WebSocket. "
- "Mesh simplification / post-processing can take several "
- "minutes without sending progress messages",
- default=600.0,
- min=60.0,
- max=3600.0,
- step=100,
- precision=0,
- ) # type: ignore
-
- def draw(self, context):
- """
- Draws the preferences panel.
- :param context: Blender context.
- :return: None
- """
- layout = self.layout
- layout.prop(self, "output_dir")
- row = layout.row(align=True)
- row.prop(self, "server_address")
-
- # Add the check button
- row.operator("stablegen.check_server_status", text="", icon='FILE_REFRESH')
-
- layout.prop(self, "save_blend_file")
-
- layout.separator()
-
- box = layout.box()
- row = box.row()
- row.label(text="ControlNet Model Assignments:")
- row.operator("stablegen.refresh_controlnet_mappings", text="", icon='FILE_REFRESH')
-
- # Use a template_list for a cleaner UI if many models
- # Requires creating a UIList class, more complex.
- # Simple loop for now:
- if not self.controlnet_model_mappings:
- box.label(text="No models found or list not refreshed.", icon='INFO')
- else:
- rows = max(1, min(len(self.controlnet_model_mappings), 5)) # Show up to 5 rows
- box.template_list(
- "STABLEGEN_UL_ControlNetMappingList", # Custom UIList identifier
- "", # Data List ID (unused here)
- self, # Data source (the preferences instance)
- "controlnet_model_mappings", # Property name of the collection
- self, # Active item index source
- "controlnet_mapping_index", # Property name for active index
- rows=rows
- )
-
- # --- Advanced Preferences ---
- layout.separator()
- adv_box = layout.box()
- row = adv_box.row()
- row.prop(self, "show_advanced",
- icon='TRIA_DOWN' if self.show_advanced else 'TRIA_RIGHT',
- emboss=False, text="Advanced Preferences")
- if self.show_advanced:
- adv_box.prop(self, "overlay_color")
- adv_box.prop(self, "enable_debug")
-
- adv_box.separator()
- adv_box.label(text="Timeouts:", icon="TIME")
- col = adv_box.column(align=True)
- col.prop(self, "timeout_ping")
- col.prop(self, "timeout_api")
- col.prop(self, "timeout_transfer")
- col.prop(self, "timeout_reboot")
- col.prop(self, "timeout_mesh_gen")
-
-class CheckServerStatus(bpy.types.Operator):
- """Checks if the ComfyUI server is reachable."""
- bl_idname = "stablegen.check_server_status"
- bl_label = "Check Server Status"
- bl_description = "Ping the ComfyUI server to check connectivity"
-
- @classmethod
- def poll(cls, context):
- prefs = context.preferences.addons.get(__package__)
- if not prefs or not prefs.preferences.server_address:
- cls.poll_message_set("Server address not configured (check addon preferences)")
- return False
- if sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- return True
-
- def execute(self, context):
- prefs = context.preferences.addons[__package__].preferences
- server_addr = prefs.server_address
-
- print(f"Checking server status at {server_addr}...")
-
- def _bg_work():
- result = {}
- result['online'] = check_server_availability(server_addr, timeout=get_timeout('ping'))
- if result['online']:
- result['trellis2'] = check_trellis2_available(server_addr, timeout=get_timeout('api'))
- result['pbr'] = check_pbr_available(server_addr, timeout=get_timeout('api'))
- else:
- result['trellis2'] = False
- result['pbr'] = False
- return result
-
- def _on_done(result):
- if result is None:
- return
- _prefs = bpy.context.preferences.addons[__package__].preferences
- _prefs.server_online = result.get('online', False)
-
- if hasattr(bpy.context, 'scene') and bpy.context.scene:
- bpy.context.scene.trellis2_available = result.get('trellis2', False)
- bpy.context.scene.pbr_nodes_available = result.get('pbr', False)
-
- if result.get('online', False):
- print(f"ComfyUI server is online at {server_addr}.")
- load_handler(None)
- def _deferred_refresh():
- try:
- bpy.ops.stablegen.refresh_checkpoint_list('INVOKE_DEFAULT')
- bpy.ops.stablegen.refresh_lora_list('INVOKE_DEFAULT')
- bpy.ops.stablegen.refresh_controlnet_mappings('INVOKE_DEFAULT')
- except Exception as e:
- print(f"Error during deferred refresh: {e}")
- return None
- bpy.app.timers.register(_deferred_refresh, first_interval=0.1)
- else:
- print(f"ComfyUI server unreachable or timed out at {server_addr}.")
-
- _run_async(_bg_work, _on_done, track_generation=True)
- self.report({'INFO'}, f"Checking server at {server_addr}...")
-
- return {'FINISHED'}
-
-class STABLEGEN_UL_ControlNetMappingList(bpy.types.UIList):
- """UIList for displaying ControlNet model mappings."""
- def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
- prefs = data # 'data' is the AddonPreferences instance
- # 'item' is the ControlNetModelMappingItem instance
-
- if self.layout_type in {'DEFAULT', 'COMPACT'}:
- # Use a split so the filename gets more space than the checkboxes
- split = layout.split(factor=0.65) # adjust factor to give filename more room
- col_name = split.column(align=True)
- col_checks = split.column(align=True)
-
- # Layout: Filename | [x] Depth | [x] Canny | [x] Normal
- col_name.prop(item, "name", text="", emboss=False) # Show filename read-only
-
- # Add checkboxes for each type using icons
- row = col_checks.row(align=True)
- row.prop(item, "supports_depth", text="Depth", toggle=True)
- row.prop(item, "supports_canny", text="Canny", toggle=True)
- row.prop(item, "supports_normal", text="Normal", toggle=True)
- # Add more props here if you add types
-
- elif self.layout_type == 'GRID':
- layout.alignment = 'CENTER'
- layout.label(text="", icon_value=icon)
-
-class RefreshControlNetMappings(bpy.types.Operator):
- """Fetches ControlNet models from ComfyUI API and updates the mapping list."""
- bl_idname = "stablegen.refresh_controlnet_mappings"
- bl_label = "Refresh ControlNet Model List"
- bl_description = "Connect to ComfyUI server to get ControlNet models and update assignments"
-
- @classmethod
- def poll(cls, context):
- prefs = context.preferences.addons.get(__package__)
- if not prefs or not prefs.preferences.server_address:
- cls.poll_message_set("Server address not configured (check addon preferences)")
- return False
- if sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- return True
-
- def execute(self, context):
- prefs = context.preferences.addons.get(__package__)
- if not prefs:
- self.report({'ERROR'}, "Cannot access addon preferences.")
- return {'CANCELLED'}
-
- server_address = prefs.preferences.server_address
-
- # Snapshot existing mapping names so the bg thread can compute
- # which models to add/remove without touching bpy.
- existing_model_names = set()
- for item in prefs.preferences.controlnet_model_mappings:
- existing_model_names.add(item.name)
-
- def _bg_work():
- server_models = _fetch_api_list(server_address, "/models/controlnet")
- return {'server_models': server_models,
- 'existing_names': existing_model_names}
-
- def _on_done(result):
- _dec_pending_refreshes()
- if result is None:
- return
- server_models = result.get('server_models')
- existing_names = result.get('existing_names', set())
-
- _prefs = bpy.context.preferences.addons.get(__package__)
- if not _prefs:
- return
- mappings = _prefs.preferences.controlnet_model_mappings
-
- if server_models is None:
- print("ControlNet refresh: cannot reach server.")
- return
-
- if not server_models:
- print("ControlNet refresh: no models found, clearing list.")
- mappings.clear()
- else:
- server_set = set(server_models)
- current_set = set(item.name for item in mappings)
-
- # Remove stale entries
- models_to_remove = current_set - server_set
- indices_to_remove = []
- for i, item in enumerate(mappings):
- if item.name in models_to_remove:
- indices_to_remove.append(i)
- for i in sorted(indices_to_remove, reverse=True):
- mappings.remove(i)
- if _prefs.preferences.controlnet_mapping_index >= len(mappings):
- _prefs.preferences.controlnet_mapping_index = max(0, len(mappings) - 1)
-
- # Add new entries with type guessing
- models_to_add = server_set - current_set
- for model_name in sorted(models_to_add):
- new_item = mappings.add()
- new_item.name = model_name
- name_lower = model_name.lower()
- is_union = 'union' in name_lower or 'promax' in name_lower
- if is_union:
- new_item.supports_depth = True
- new_item.supports_canny = True
- new_item.supports_normal = True
- print(f" Guessed '{model_name}' as Union (Depth, Canny, Normal).")
- else:
- if 'depth' in name_lower:
- new_item.supports_depth = True
- print(f" Guessed '{model_name}' as Depth.")
- if 'canny' in name_lower or 'lineart' in name_lower or 'scribble' in name_lower:
- new_item.supports_canny = True
- print(f" Guessed '{model_name}' as Canny.")
- if 'normal' in name_lower:
- new_item.supports_normal = True
- print(f" Guessed '{model_name}' as Normal.")
- if not (new_item.supports_depth or new_item.supports_canny or new_item.supports_normal):
- print(f" Could not guess type for '{model_name}'. Please assign manually.")
-
- print(f"ControlNet refresh: {len(models_to_add)} added, {len(models_to_remove)} removed.")
-
- # Redraw UI
- for window in bpy.context.window_manager.windows:
- for area in window.screen.areas:
- area.tag_redraw()
-
- _run_async(_bg_work, _on_done)
- _inc_pending_refreshes()
- self.report({'INFO'}, "Fetching ControlNet models...")
- return {'FINISHED'}
-
-from .timeout_config import get_timeout # noqa: E402 – placed here to avoid circular imports
-
-
-def check_server_availability(server_address, timeout=0.5):
- """
- Quickly checks if the ComfyUI server is responding.
-
- Args:
- server_address (str): The address:port of the ComfyUI server.
- timeout (float): Strict timeout in seconds for this check.
-
- Returns:
- bool: True if the server responds quickly, False otherwise.
- """
- if not server_address:
- return False
-
- # Use a lightweight endpoint like /queue or root '/'
- # /system_stats might be slightly heavier
- url = f"http://{server_address}/queue" # Or just f"http://{server_address}/"
- # print(f"Pinging server at: {url} (Timeout: {timeout}s)") # Debug
- try:
- # HEAD request is faster as it doesn't download the body
- response = requests.head(url, timeout=timeout)
- # Check for successful status codes (2xx, maybe 404 if hitting root)
- # /queue typically gives 200 OK even on GET/HEAD
- response.raise_for_status()
- # print(" Server responded.") # Debug
- return True
- except requests.exceptions.Timeout:
- print(f" Initial server check failed: Timeout ({timeout}s).")
- return False
- except requests.exceptions.ConnectionError:
- print(" Initial server check failed: Connection Error.")
- return False
-
-
-def check_trellis2_available(server_address, timeout=1.0):
- """
- Checks if TRELLIS.2 custom nodes are installed in ComfyUI by querying /object_info.
-
- Args:
- server_address (str): The address:port of the ComfyUI server.
- timeout (float): Timeout in seconds.
-
- Returns:
- bool: True if TRELLIS.2 nodes are detected, False otherwise.
- """
- if not server_address:
- return False
- try:
- url = f"http://{server_address}/object_info/Trellis2ImageToShape"
- response = requests.get(url, timeout=timeout)
- if response.status_code == 200:
- data = response.json()
- if 'Trellis2ImageToShape' in data:
- print("[TRELLIS2] Auto-detect: TRELLIS.2 nodes found in ComfyUI.")
- return True
- print("[TRELLIS2] Auto-detect: TRELLIS.2 nodes NOT found in ComfyUI.")
- return False
- except Exception as e:
- print(f"[TRELLIS2] Auto-detect failed: {e}")
- return False
- except requests.exceptions.RequestException as e:
- # Other errors (like 404 on root) might still mean the server is *running*
- # but depends on the chosen endpoint. /queue should be reliable.
- # Let's consider most request exceptions here as a failure to connect quickly.
- print(f" Initial server check failed: Request Error ({e}).")
- return False
- except Exception as e:
- print(f" Initial server check failed: Unexpected Error ({e}).")
- return False
-
-def check_pbr_available(server_address, timeout=1.0):
- """
- Checks if PBR decomposition custom nodes are installed in ComfyUI.
-
- Looks for MarigoldModelLoader (ComfyUI-Marigold) and
- LoadStableDelightModel (ComfyUI_StableDelight_ll).
-
- Args:
- server_address (str): The address:port of the ComfyUI server.
- timeout (float): Timeout in seconds.
-
- Returns:
- bool: True if both node packages are detected, False otherwise.
- """
- if not server_address:
- return False
- required = ['MarigoldModelLoader', 'LoadStableDelightModel']
- try:
- for node_class in required:
- url = f"http://{server_address}/object_info/{node_class}"
- response = requests.get(url, timeout=timeout)
- if response.status_code != 200 or node_class not in response.json():
- print(f"[PBR] Auto-detect: node '{node_class}' NOT found in ComfyUI.")
- return False
- print("[PBR] Auto-detect: PBR decomposition nodes found in ComfyUI.")
- return True
- except Exception as e:
- print(f"[PBR] Auto-detect failed: {e}")
- return False
-
-def fetch_from_comfyui_api(context, endpoint):
- """
- Fetches data from a specified ComfyUI API endpoint.
-
- Args:
- context: Blender context to access addon preferences.
- endpoint (str): The API endpoint path (e.g., "/models/checkpoints").
-
- Returns:
- list: A list of items returned by the API (usually filenames),
- or an empty list if the request fails or returns invalid data.
- Returns None if the server address is not set.
- """
- addon_prefs = context.preferences.addons.get(__package__)
- if not addon_prefs:
- print("Error: Could not access StableGen addon preferences.")
- return None # Indicate config error
-
- server_address = addon_prefs.preferences.server_address
- if not server_address:
- print("Error: ComfyUI Server Address is not set in preferences.")
- # Return None to signify a configuration issue preventing the API call
- return None
-
- if not check_server_availability(server_address, timeout=get_timeout('ping')): # Use a strict timeout here
- # Error message printed by check_server_availability
- return None # Server unreachable or timed out on initial check
-
- # Ensure endpoint starts with a slash
- if not endpoint.startswith('/'):
- endpoint = '/' + endpoint
-
- url = f"http://{server_address}{endpoint}"
-
- try:
- response = requests.get(url, timeout=get_timeout('api')) # Add a timeout
- response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
- data = response.json()
-
- # Basic validation: Check if the response is a list (expected for model lists)
- if isinstance(data, list):
- # Further check if list items look like filenames (simple check)
- if all(isinstance(item, str) for item in data):
- return data # Return the list of filenames
- elif data: # List exists but contains non-strings
- print(f" Warning: API endpoint {endpoint} returned a list, but it contains non-string items: {data[:5]}...") # Show first few
- # Decide how to handle: return empty, or try to filter strings?
- # For now, let's filter assuming filenames are strings:
- string_items = [item for item in data if isinstance(item, str)]
- if string_items:
- return string_items
- else:
- print(f" Error: No valid string filenames found in list from {endpoint}.")
- return [] # Return empty list if no strings found
- else:
- # API returned an empty list, which is valid
- return []
- else:
- print(f" Error: API endpoint {endpoint} did not return a JSON list. Received: {type(data)}")
- return [] # Return empty list on unexpected type
-
- except requests.exceptions.Timeout:
- print(f" Error: Timeout connecting to {url}.")
- except requests.exceptions.ConnectionError:
- print(f" Error: Connection failed to {url}. Is ComfyUI running and accessible?")
- except requests.exceptions.RequestException as e:
- print(f" Error fetching from {url}: {e}")
- except json.JSONDecodeError:
- print(f" Error: Could not decode JSON response from {url}. Response text: {response.text}")
- except Exception as e:
- print(f" An unexpected error occurred fetching from {url}: {e}")
-
- return [] # Return empty list on any failure
-
-
-def _fetch_api_list(server_address, endpoint):
- """Thread-safe variant of *fetch_from_comfyui_api* — takes an explicit
- *server_address* instead of reading from bpy context, so it can run in a
- background thread.
-
- Returns a list of strings on success, ``None`` on connection/config error,
- or ``[]`` when the server returns an empty or invalid list.
- """
- if not server_address:
- return None
-
- if not check_server_availability(server_address, timeout=get_timeout('ping')):
- return None
-
- if not endpoint.startswith('/'):
- endpoint = '/' + endpoint
-
- url = f"http://{server_address}{endpoint}"
- try:
- response = requests.get(url, timeout=get_timeout('api'))
- response.raise_for_status()
- data = response.json()
-
- if isinstance(data, list):
- string_items = [item for item in data if isinstance(item, str)]
- return string_items
- else:
- print(f" Error: API endpoint {endpoint} did not return a JSON list.")
- return []
- except requests.exceptions.Timeout:
- print(f" Error: Timeout connecting to {url}.")
- except requests.exceptions.ConnectionError:
- print(f" Error: Connection failed to {url}.")
- except requests.exceptions.RequestException as e:
- print(f" Error fetching from {url}: {e}")
- except json.JSONDecodeError:
- print(f" Error: Could not decode JSON from {url}.")
- except Exception as e:
- print(f" Unexpected error fetching from {url}: {e}")
-
- return []
-
-
-def get_models_from_directory(scan_root_path: str, valid_extensions: tuple, type_for_description: str, path_prefix_for_id: str = ""):
- """
- Scans a given root directory (and its subdirectories) for model files.
- Returns paths relative to scan_root_path, optionally prefixed.
-
- Args:
- scan_root_path (str): The absolute root path to start scanning from.
- valid_extensions (tuple): Tuple of valid lowercase file extensions.
- type_for_description (str): String like "Checkpoint" or "LoRA" for UI descriptions.
- path_prefix_for_id (str): A prefix to add to the identifier if needed to distinguish sources
- """
- items = []
- if not (scan_root_path and os.path.isdir(scan_root_path)):
- # Don't add error items here, let the caller handle empty results
- return items
-
- try:
- for root, _, files in os.walk(scan_root_path):
- for f_name in files:
- if f_name.lower().endswith(valid_extensions):
- full_path = os.path.join(root, f_name)
- # Path relative to the specific scan_root_path (ComfyUI or external)
- relative_path = os.path.relpath(full_path, scan_root_path)
-
- # The identifier sent to ComfyUI should be this relative_path
- # if scan_root_path is a path ComfyUI recognizes.
- identifier = path_prefix_for_id + relative_path
- display_name = identifier # Show the full "prefixed" path if prefix is used
-
- items.append((identifier, display_name, f"{type_for_description}: {display_name}"))
- except PermissionError:
- print(f"Permission Denied for {scan_root_path}") # Log it
- except Exception as e:
- print(f"Error Scanning {scan_root_path}: {e}") # Log it
-
- return items
-
-def merge_and_deduplicate_models(model_lists: list):
- """
- Merges multiple lists of model items and de-duplicates based on the identifier.
- Keeps the first encountered entry in case of duplicate identifiers.
- """
- merged_items = []
- seen_identifiers = set()
- for model_list in model_lists:
- for identifier, name, description in model_list:
- # Filter out placeholder/error items from get_models_from_directory if they existed
- if identifier.startswith("NO_") or identifier.startswith("PERM_") or identifier.startswith("SCAN_") or identifier == "NONE_FOUND":
- continue
- if identifier not in seen_identifiers:
- merged_items.append((identifier, name, description))
- seen_identifiers.add(identifier)
-
- if not merged_items: # If after all scans and merges, still nothing
- merged_items.append(("NONE_AVAILABLE", "No Models Found", "Check ComfyUI and External Directories in Preferences"))
-
- merged_items.sort(key=lambda x: x[1]) # Sort by display name
- return merged_items
-
-def update_model_list(self, context):
- """Returns the cached list of checkpoint/unet models."""
- global _cached_checkpoint_list
- # Basic check in case cache hasn't been populated correctly
- if not _cached_checkpoint_list:
- return [("NONE_AVAILABLE", "None available", "Fetch models from server")]
- return _cached_checkpoint_list
-
-def update_union(self, context):
- if "union" in self.model_name.lower() or "promax" in self.model_name.lower():
- self.is_union = True
- else:
- self.is_union = False
-
-def update_controlnet(self, context):
- update_parameters(self, context)
- update_union(self, context)
- return None
-
-class ControlNetUnit(bpy.types.PropertyGroup):
- unit_type: bpy.props.StringProperty(
- name="Type",
- description="ControlNet type (e.g. 'depth', 'canny')",
- default="",
- update=update_parameters
- ) # type: ignore
- model_name: bpy.props.EnumProperty(
- name="Model",
- description="Select the ControlNet model",
- items=lambda self, context: get_controlnet_models(context, self.unit_type),
- update=update_controlnet
- ) # type: ignore
- strength: bpy.props.FloatProperty(
- name="Strength",
- description="Strength of the ControlNet effect",
- default=0.5,
- min=0.0,
- max=3.0,
- update=update_parameters
- ) # type: ignore
- start_percent: bpy.props.FloatProperty(
- name="Start",
- description="Start percentage (/100)",
- default=0.0,
- min=0.0,
- max=1.0,
- update=update_parameters
- ) # type: ignore
- end_percent: bpy.props.FloatProperty(
- name="End",
- description="End percentage (/100)",
- default=1.0,
- min=0.0,
- max=1.0,
- update=update_parameters
- ) # type: ignore
- is_union: bpy.props.BoolProperty(
- name="Is Union Type",
- description="Is this a union ControlNet?",
- default=False,
- update=update_parameters
- ) # type: ignore
- use_union_type: bpy.props.BoolProperty(
- name="Use Union Type",
- description="Use union type for ControlNet",
- default=True,
- update=update_parameters
- ) # type: ignore
-
-class LoRAUnit(bpy.types.PropertyGroup):
- model_name: bpy.props.EnumProperty(
- name="LoRA Model",
- description="Select the LoRA model file",
- items=lambda self, context: get_lora_models(self, context),
- update=update_parameters
- ) # type: ignore
- model_strength: bpy.props.FloatProperty(
- name="Model Strength",
- description="Strength of the LoRA's effect on the model's weights",
- default=1.0,
- min=0.0,
- max=100.0, # Adjusted max based on typical LoRA usage
- update=update_parameters
- ) # type: ignore
- clip_strength: bpy.props.FloatProperty(
- name="CLIP Strength",
- description="Strength of the LoRA's effect on the CLIP/text conditioning",
- default=1.0,
- min=0.0,
- max=100.0, # Adjusted max
- update=update_parameters
- ) # type: ignore
-
-def get_controlnet_models(context, unit_type):
- """
- Get available ControlNet models suitable for a specific 'unit_type'
- based on user assignments in addon preferences.
-
- Args:
- context: Blender context.
- unit_type (str): The type required (e.g., 'depth', 'canny').
-
- Returns:
- list: A list of (identifier, name, description) tuples for EnumProperty.
- """
- items = []
- prefs = context.preferences.addons.get(__package__)
- if not prefs:
- return [("NO_PREFS", "Addon Error", "Could not access preferences")]
-
- mappings = prefs.preferences.controlnet_model_mappings
-
- if not mappings:
- return [("REFRESH", "Refresh List in Prefs", "Fetch models via Preferences")]
-
- # Determine which boolean property corresponds to the requested unit_type
- prop_name = f"supports_{unit_type}"
-
- found_count = 0
- for item in mappings:
- # Check if the item object actually has the property (safety check)
- if hasattr(item, prop_name):
- # Check if the boolean flag for the required type is True
- if getattr(item, prop_name):
- # Identifier and Name are the filename
- items.append((item.name, item.name, f"ControlNet: {item.name}"))
- found_count += 1
-
- if found_count == 0:
- return [("NO_ASSIGNED", f"No models assigned to '{unit_type}'", f"Assign types in Addon Preferences or Refresh")]
-
- # Sort alphabetically
- items.sort(key=lambda x: x[1])
-
- return items
-
-def get_lora_models(self, context):
- """Returns the cached list of LoRA models."""
- global _cached_lora_list
- # Basic check
- if not _cached_lora_list:
- return [("NONE_AVAILABLE", "None available", "Fetch models from server")]
- return _cached_lora_list
-
-class RefreshCheckpointList(bpy.types.Operator):
- """Fetches Checkpoint/UNET models from ComfyUI API and updates the cache."""
- bl_idname = "stablegen.refresh_checkpoint_list"
- bl_label = "Refresh Checkpoint/UNET List"
- bl_description = "Connect to ComfyUI server to get available Checkpoint/UNET models"
-
- @classmethod
- def poll(cls, context):
- prefs = context.preferences.addons.get(__package__)
- if not prefs or not prefs.preferences.server_address:
- cls.poll_message_set("Server address not configured (check addon preferences)")
- return False
- if sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- return True
-
- def execute(self, context):
- prefs = context.preferences.addons.get(__package__)
- if not prefs:
- self.report({'ERROR'}, "Cannot access addon preferences.")
- return {'CANCELLED'}
-
- server_address = prefs.preferences.server_address
- architecture = getattr(context.scene, "model_architecture", "sdxl")
-
- def _bg_work():
- """Background thread — network I/O only, no bpy access."""
- model_list = None
- if architecture == 'sdxl':
- model_list = _fetch_api_list(server_address, "/models/checkpoints")
- model_type_desc = "Checkpoint"
- elif architecture in ('flux1', 'qwen_image_edit'):
- model_list = _fetch_api_list(server_address, "/models/unet_gguf")
- if model_list is not None:
- extra = _fetch_api_list(server_address, "/models/diffusion_models")
- if extra:
- model_list.extend(extra)
- model_type_desc = "UNET" if architecture == 'flux1' else "UNET (GGUF/Safetensors)"
- elif architecture == 'flux2_klein':
- model_list = _fetch_api_list(server_address, "/models/diffusion_models")
- model_type_desc = "Diffusion Model"
- else:
- model_type_desc = "Model"
- return {'model_list': model_list, 'architecture': architecture,
- 'model_type_desc': model_type_desc}
-
- def _on_done(result):
- """Main-thread callback — update caches and UI."""
- global _cached_checkpoint_list, _cached_checkpoint_architecture
- _dec_pending_refreshes()
- if result is None:
- return
-
- model_list = result.get('model_list')
- arch = result.get('architecture')
- desc = result.get('model_type_desc', 'Model')
-
- if model_list is None:
- _cached_checkpoint_list = [("NO_SERVER", "Set Server Address", "Cannot fetch")]
- _cached_checkpoint_architecture = None
- print(f"Checkpoint refresh: cannot reach server.")
- elif not model_list:
- _cached_checkpoint_list = [("NONE_FOUND", f"No {desc}s Found", "Server list is empty")]
- _cached_checkpoint_architecture = arch
- print(f"Checkpoint refresh: no {desc} models found.")
- else:
- items = []
- for name in sorted(model_list):
- items.append((name, name, f"{desc}: {name}"))
- _cached_checkpoint_list = items
- _cached_checkpoint_architecture = arch
- print(f"Checkpoint refresh: {len(items)} {desc}(s) found.")
-
- # Reset model_name if current selection is no longer valid.
- # Prefer the sg_model_name_backup (plain StringProperty) which
- # survives file loads even when the dynamic Enum items were stale.
- scene = bpy.context.scene if hasattr(bpy.context, 'scene') else None
- if scene:
- valid_ids = {it[0] for it in _cached_checkpoint_list}
- backup = getattr(scene, 'sg_model_name_backup', '')
- if backup and backup in valid_ids:
- # Restore from backup — this is the value that was
- # selected before the file was saved / queue snapshot.
- if scene.model_name != backup:
- scene.model_name = backup
- elif scene.model_name not in valid_ids:
- placeholder = next((it[0] for it in _cached_checkpoint_list
- if it[0].startswith("NO_") or it[0] == "NONE_FOUND"), None)
- if placeholder:
- scene.model_name = placeholder
- elif _cached_checkpoint_list:
- scene.model_name = _cached_checkpoint_list[0][0]
-
- # Redraw UI
- for window in bpy.context.window_manager.windows:
- for area in window.screen.areas:
- area.tag_redraw()
-
- _run_async(_bg_work, _on_done)
- _inc_pending_refreshes()
- self.report({'INFO'}, "Fetching checkpoint list...")
- return {'FINISHED'}
-
-class RefreshLoRAList(bpy.types.Operator):
- """Fetches LoRA models from ComfyUI API and updates the cache."""
- bl_idname = "stablegen.refresh_lora_list"
- bl_label = "Refresh LoRA List"
- bl_description = "Connect to ComfyUI server to get available LoRA models"
-
- @classmethod
- def poll(cls, context):
- prefs = context.preferences.addons.get(__package__)
- if not prefs or not prefs.preferences.server_address:
- cls.poll_message_set("Server address not configured (check addon preferences)")
- return False
- if sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- return True
-
- def execute(self, context):
- prefs = context.preferences.addons.get(__package__)
- if not prefs:
- self.report({'ERROR'}, "Cannot access addon preferences.")
- return {'CANCELLED'}
-
- server_address = prefs.preferences.server_address
-
- def _bg_work():
- return {'lora_list': _fetch_api_list(server_address, "/models/loras")}
-
- def _on_done(result):
- global _cached_lora_list
- _dec_pending_refreshes()
- if result is None:
- return
-
- lora_list = result.get('lora_list')
-
- if lora_list is None:
- _cached_lora_list = [("NO_SERVER", "Set Server Address", "Cannot fetch")]
- print("LoRA refresh: cannot reach server.")
- elif not lora_list:
- _cached_lora_list = [("NONE_FOUND", "No LoRAs Found", "Server list is empty")]
- print("LoRA refresh: no models found.")
- else:
- items = []
- for name in sorted(lora_list):
- items.append((name, name, f"LoRA: {name}"))
- _cached_lora_list = items
- print(f"LoRA refresh: {len(items)} model(s) found.")
-
- # Clean up invalid lora_units selections
- scene = bpy.context.scene if hasattr(bpy.context, 'scene') else None
- if scene and hasattr(scene, 'lora_units'):
- valid_ids = {it[0] for it in _cached_lora_list}
- placeholder = next((it[0] for it in _cached_lora_list
- if it[0].startswith("NO_") or it[0] == "NONE_FOUND"), None)
- indices_to_remove = []
- for i, unit in enumerate(scene.lora_units):
- if unit.model_name not in valid_ids or unit.model_name == placeholder:
- indices_to_remove.append(i)
- for i in sorted(indices_to_remove, reverse=True):
- scene.lora_units.remove(i)
-
- num_loras = len(scene.lora_units)
- if scene.lora_units_index >= num_loras:
- scene.lora_units_index = max(0, num_loras - 1)
- elif num_loras == 0:
- scene.lora_units_index = 0
-
- # Redraw UI
- for window in bpy.context.window_manager.windows:
- for area in window.screen.areas:
- area.tag_redraw()
-
- _run_async(_bg_work, _on_done)
- _inc_pending_refreshes()
- self.report({'INFO'}, "Fetching LoRA list...")
- return {'FINISHED'}
-
-class AddControlNetUnit(bpy.types.Operator):
- bl_idname = "stablegen.add_controlnet_unit"
- bl_label = "Add ControlNet Unit"
- bl_description = "Add a ControlNet Unit. Only one unit per type is allowed."
-
- unit_type: bpy.props.EnumProperty(
- name="Type",
- items=[('depth', 'Depth', ''), ('canny', 'Canny', ''), ('normal', 'Normal', '')],
- default='depth',
- update=update_parameters
- ) # type: ignore
-
- model_name: bpy.props.EnumProperty(
- name="Model",
- description="Select the ControlNet model",
- items=lambda self, context: get_controlnet_models(context, self.unit_type),
- update=update_parameters
- ) # type: ignore
-
- @classmethod
- def poll(cls, context):
- return not sg_modal_active(context)
-
- def invoke(self, context, event):
- # Always prompt for unit type and model selection
- return context.window_manager.invoke_props_dialog(self)
-
- def draw(self, context):
- layout = self.layout
- layout.prop(self, "unit_type")
- models = get_controlnet_models(context, self.unit_type)
- if len(models) > 1:
- layout.prop(self, "model_name")
-
- def execute(self, context):
- units = context.scene.controlnet_units
- # Only add if not already present
- for unit in units:
- if unit.unit_type == self.unit_type:
- self.report({'WARNING'}, f"Unit '{self.unit_type}' already exists.")
- return {'CANCELLED'}
- new_unit = units.add()
- new_unit.unit_type = self.unit_type
- new_unit.model_name = self.model_name
- new_unit.strength = 0.5
- new_unit.start_percent = 0.0
- new_unit.end_percent = 1.0
- if "union" in new_unit.model_name.lower() or "promax" in new_unit.model_name.lower():
- new_unit.is_union = True
- context.scene.controlnet_units_index = len(units) - 1
- # Force redraw of the UI
- for area in context.screen.areas:
- area.tag_redraw()
- return {'FINISHED'}
-
-class RemoveControlNetUnit(bpy.types.Operator):
- bl_idname = "stablegen.remove_controlnet_unit"
- bl_label = "Remove ControlNet Unit"
- bl_description = "Remove the selected ControlNet Unit"
-
- unit_type: bpy.props.EnumProperty(
- name="Type",
- items=[('depth', 'Depth', ''), ('canny', 'Canny', ''), ('normal', 'Normal', '')],
- default='depth',
- update=update_parameters
- ) # type: ignore
-
- @classmethod
- def poll(cls, context):
- return not sg_modal_active(context)
-
- def invoke(self, context, event):
- units = context.scene.controlnet_units
- if len(units) == 1:
- self.unit_type = units[0].unit_type
- return self.execute(context)
- return context.window_manager.invoke_props_dialog(self)
-
- def draw(self, context):
- layout = self.layout
- layout.prop(self, "unit_type")
-
- def execute(self, context):
- units = context.scene.controlnet_units
- for index, unit in enumerate(units):
- if unit.unit_type == self.unit_type:
- units.remove(index)
- context.scene.controlnet_units_index = min(max(0, index - 1), len(units) - 1)
- # Force redraw of the UI
- update_parameters(self, context)
- for area in context.screen.areas:
- area.tag_redraw()
- return {'FINISHED'}
- self.report({'WARNING'}, f"No unit of type '{self.unit_type}' found.")
- return {'CANCELLED'}
-
-class AddLoRAUnit(bpy.types.Operator):
- bl_idname = "stablegen.add_lora_unit"
- bl_label = "Add LoRA Unit"
- bl_description = "Add a LoRA to the chain. Disabled if no LoRAs are available or all available LoRAs have been added."
-
- @classmethod
- def poll(cls, context):
- scene = context.scene
- addon_prefs = context.preferences.addons.get(__package__)
-
- if not addon_prefs: # Should not happen if addon is enabled
- return False
- addon_prefs = addon_prefs.preferences
-
- # Get the merged list of LoRAs.
- # Assuming get_lora_models is robust and returns placeholders if dirs are bad.
- lora_enum_items = get_lora_models(scene, context)
-
- # Count actual available LoRAs, excluding placeholders/errors
- # Placeholders used in get_models_from_directory and merge_and_deduplicate_models
- placeholder_ids = {"NONE_AVAILABLE", "NO_COMFYUI_DIR_LORA", "NO_LORAS_SUBDIR", "PERM_ERROR", "SCAN_ERROR", "NONE_FOUND"} # Add any others used by your helpers
-
- available_lora_files_count = sum(1 for item in lora_enum_items if item[0] not in placeholder_ids)
-
- if available_lora_files_count == 0:
- cls.poll_message_set("No LoRA model files found in any specified directory (including subdirectories).")
- return False
-
- num_current_lora_units = len(scene.lora_units)
- # Prevent adding more units than distinct available LoRA files
- if num_current_lora_units >= available_lora_files_count:
- cls.poll_message_set("All available distinct LoRA models appear to have corresponding units.")
- return False
-
- return not sg_modal_active(context)
-
- def execute(self, context):
- loras = context.scene.lora_units
- new_lora = loras.add()
-
- # Get available LoRAs (these are (identifier, name, description) tuples)
- all_lora_enum_items = get_lora_models(context.scene, context)
-
- placeholder_ids = {"NONE_AVAILABLE", "NO_COMFYUI_DIR_LORA", "NO_LORAS_SUBDIR", "PERM_ERROR", "SCAN_ERROR", "NONE_FOUND"}
- available_lora_identifiers = [item[0] for item in all_lora_enum_items if item[0] not in placeholder_ids]
-
- if available_lora_identifiers:
- current_lora_model_identifiers_in_use = {unit.model_name for unit in loras if unit.model_name and unit.model_name not in placeholder_ids}
-
- assigned_model = None
- # Try to assign a LoRA that isn't already in use by another unit
- for lora_id in available_lora_identifiers:
- if lora_id not in current_lora_model_identifiers_in_use:
- assigned_model = lora_id
- break
-
- # If all available LoRAs are "in use" or no unused one was found, assign the first available one
- if not assigned_model:
- assigned_model = available_lora_identifiers[0]
-
- if assigned_model:
- try:
- new_lora.model_name = assigned_model
- except TypeError:
- # This might happen if the enum items list isn't perfectly in sync
- print(f"AddLoRAUnit Execute: TypeError assigning model '{assigned_model}'. Enum might not be ready.")
- pass
-
- new_lora.model_strength = 1.0
- new_lora.clip_strength = 1.0
- context.scene.lora_units_index = len(loras) - 1 # Select the newly added unit
-
- # Ensure parameters are updated which might affect preset status
- update_parameters(self, context)
-
- # Force UI redraw
- for area in context.screen.areas:
- if area.type == 'VIEW_3D': # Redraw 3D views, common place for the panel
- area.tag_redraw()
- elif area.type == 'PROPERTIES': # Redraw properties editor if panel is there
- area.tag_redraw()
-
- return {'FINISHED'}
-
-class RemoveLoRAUnit(bpy.types.Operator):
- bl_idname = "stablegen.remove_lora_unit"
- bl_label = "Remove Selected LoRA Unit"
- bl_description = "Remove the selected LoRA from the chain"
-
- @classmethod
- def poll(cls, context):
- scene = context.scene
- # Operator can run if there are LoRA units AND the current index is valid
- return (len(scene.lora_units) > 0 and
- 0 <= scene.lora_units_index < len(scene.lora_units)
- and not sg_modal_active(context))
-
- def execute(self, context):
- loras = context.scene.lora_units
- index = context.scene.lora_units_index
- if 0 <= index < len(loras):
- loras.remove(index)
- context.scene.lora_units_index = min(max(0, index - 1), len(loras) - 1)
- update_parameters(self, context)
- for area in context.screen.areas:
- area.tag_redraw()
- return {'FINISHED'}
- self.report({'WARNING'}, "No LoRA unit selected or list is empty.")
- return {'CANCELLED'}
-
-# ── Scene Queue persistence (JSON file in output dir) ─────────────────
-_SG_QUEUE_FILENAME = "sg_scene_queue.json"
-
-def _sg_queue_filepath():
- """Return the path to the queue JSON file.
-
- Uses the addon output_dir if set; otherwise falls back to a
- `.stablegen` folder next to the addon package.
- """
- try:
- prefs = bpy.context.preferences.addons.get(__package__)
- if prefs and prefs.preferences.output_dir:
- d = bpy.path.abspath(prefs.preferences.output_dir)
- if os.path.isdir(d):
- return os.path.join(d, _SG_QUEUE_FILENAME)
- except Exception:
- pass
- # Fallback: <addon_dir>/.stablegen/
- fallback = os.path.join(os.path.dirname(__file__), ".stablegen")
- os.makedirs(fallback, exist_ok=True)
- return os.path.join(fallback, _SG_QUEUE_FILENAME)
-
-
-def _sg_queue_save(processing=False, current_idx=0, phase='idle'):
- """Serialize the scene queue + processing state to a JSON file on disk."""
- wm = bpy.context.window_manager
- if not hasattr(wm, 'sg_scene_queue'):
- return
- items = [{"label": it.label,
- "scene_name": it.scene_name,
- "blend_file": it.blend_file,
- "prompt": it.prompt,
- "negative_prompt": it.negative_prompt,
- "status": it.status,
- "retries": it.retries,
- "error_reason": it.error_reason}
- for it in wm.sg_scene_queue]
- # GIF export settings (persist across .blend switches)
- gif_settings = {}
- _GIF_KEYS = (
- 'sg_queue_gif_export', 'sg_queue_gif_duration', 'sg_queue_gif_fps',
- 'sg_queue_gif_resolution', 'sg_queue_gif_samples', 'sg_queue_gif_engine',
- 'sg_queue_gif_interpolation', 'sg_queue_gif_use_hdri',
- 'sg_queue_gif_hdri_path', 'sg_queue_gif_hdri_strength',
- 'sg_queue_gif_hdri_rotation', 'sg_queue_gif_env_mode',
- 'sg_queue_gif_denoiser', 'sg_queue_gif_use_gpu',
- 'sg_queue_gif_also_no_pbr',
- )
- for key in _GIF_KEYS:
- if hasattr(wm, key):
- gif_settings[key] = getattr(wm, key)
-
- data = {
- "items": items,
- "processing": processing,
- "current_idx": current_idx,
- "phase": phase,
- "gif_settings": gif_settings,
- }
- try:
- fp = _sg_queue_filepath()
- with open(fp, 'w', encoding='utf-8') as f:
- json.dump(data, f)
- except Exception as e:
- print(f"[Queue] save error: {e}")
-
-
-def _sg_queue_load():
- """Restore the scene queue from the JSON file on disk.
-
- If the JSON indicates that queue processing was active (e.g. after a
- .blend switch), automatically resume.
- """
- wm = bpy.context.window_manager
- if not hasattr(wm, 'sg_scene_queue'):
- return
- fp = _sg_queue_filepath()
- if not os.path.isfile(fp):
- return
- try:
- with open(fp, 'r', encoding='utf-8') as f:
- raw = json.load(f)
-
- # Support both old (list) and new (dict) format
- if isinstance(raw, list):
- items = raw
- meta = {}
- else:
- items = raw.get("items", [])
- meta = raw
-
- wm.sg_scene_queue.clear()
- for entry in items:
- new_item = wm.sg_scene_queue.add()
- new_item.label = entry.get("label", "")
- new_item.scene_name = entry.get("scene_name", "")
- new_item.blend_file = entry.get("blend_file", "")
- new_item.prompt = entry.get("prompt", "")
- new_item.negative_prompt = entry.get("negative_prompt", "")
- new_item.status = entry.get("status", "pending")
- new_item.retries = entry.get("retries", 0)
- new_item.error_reason = entry.get("error_reason", "")
- wm.sg_scene_queue_index = min(0, len(wm.sg_scene_queue) - 1)
-
- # Restore GIF export settings
- gif_settings = meta.get("gif_settings", {})
- for key, value in gif_settings.items():
- if hasattr(wm, key):
- try:
- setattr(wm, key, value)
- except Exception:
- pass
-
- # Auto-resume processing if it was active (e.g. after a blend switch)
- if meta.get("processing", False):
- try:
- from .stablegen import _resume_queue
- idx = meta.get("current_idx", 0)
- _resume_queue(idx)
- print(f"[Queue] Auto-resuming from item {idx}")
- except Exception as e:
- print(f"[Queue] Failed to auto-resume: {e}")
- except Exception as e:
- print(f"[Queue] Failed to restore queue: {e}")
-
-
-@persistent
-def _sg_queue_load_handler(dummy):
- """load_post handler: restore the queue when a .blend is opened."""
- def _deferred():
- _sg_queue_load()
- return None
- bpy.app.timers.register(_deferred, first_interval=0.3)
-
-# load handler to set default ControlNet and LoRA units on first load
-@persistent
-def load_handler(dummy):
- global _cached_checkpoint_architecture, _pending_checkpoint_refresh_architecture
- if bpy.context.scene:
- scene = bpy.context.scene
- addon_prefs = bpy.context.preferences.addons[__package__].preferences
-
- # Re-register aspect-ratio crop overlays if any cameras have
- # per-camera resolutions stored. GPU draw handlers do not
- # survive file loads so they must be re-created here.
- try:
- from .render_tools import _sg_ensure_crop_overlay
- for obj in scene.objects:
- if obj.type == 'CAMERA' and 'sg_display_crop' in obj:
- _sg_ensure_crop_overlay()
- break
- except Exception:
- pass
-
- if hasattr(scene, "controlnet_units") and not scene.controlnet_units:
- default_unit = scene.controlnet_units.add()
- default_unit.unit_type = 'depth'
- # Default LoRA Unit
- if hasattr(scene, "lora_units") and not scene.lora_units:
- default_lora_filename_to_find = None
- model_strength = 1.0
- clip_strength = 1.0
-
- if scene.model_architecture == 'sdxl':
- default_lora_filename_to_find = 'sdxl_lightning_8step_lora.safetensors'
- elif scene.model_architecture == 'qwen_image_edit':
- default_lora_filename_to_find = 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors'
- clip_strength = 0.0 # Qwen uses model-only LoRA
-
- if not default_lora_filename_to_find:
- return # No default LoRA for this architecture
-
- all_available_loras_enums = get_lora_models(scene, bpy.context)
-
- found_lora_identifier_to_load = None
- for identifier, name, description in all_available_loras_enums:
- # Identifiers are relative paths like "subdir/model.safetensors" or "model.safetensors"
- # Check if the identifier (which is the relative path) ends with the desired filename
- if identifier.endswith(default_lora_filename_to_find):
- # Ensure it's not a placeholder/error identifier
- if identifier not in ["NONE_AVAILABLE", "NO_COMFYUI_DIR_LORA", "NO_LORAS_SUBDIR", "PERM_ERROR", "SCAN_ERROR", "NONE_FOUND"]:
- found_lora_identifier_to_load = identifier
- break
-
- if found_lora_identifier_to_load:
- new_lora_unit = None
- try:
- new_lora_unit = scene.lora_units.add()
- new_lora_unit.model_name = found_lora_identifier_to_load
- new_lora_unit.model_strength = model_strength
- new_lora_unit.clip_strength = clip_strength
- # print(f"StableGen Load Handler: Default LoRA '{found_lora_identifier_to_load}' added.")
- except TypeError:
- # This can happen if Enum items are not fully synchronized at this early stage of loading.
- print(f"StableGen Load Handler: TypeError setting default LoRA '{found_lora_identifier_to_load}'. Enum items might not be fully ready.")
- if new_lora_unit and scene.lora_units and new_lora_unit == scene.lora_units[-1]:
- scene.lora_units.remove(len(scene.lora_units)-1) # Attempt to remove partially added unit
- except Exception as e:
- print(f"StableGen Load Handler: Unexpected error setting default LoRA '{found_lora_identifier_to_load}': {e}")
- if new_lora_unit and scene.lora_units and new_lora_unit == scene.lora_units[-1]:
- scene.lora_units.remove(len(scene.lora_units)-1)
-
- # Ensure checkpoint cache matches the scene architecture that just loaded
- current_architecture = getattr(scene, "model_architecture", None)
- prefs_wrapper = bpy.context.preferences.addons.get(__package__)
- if current_architecture and prefs_wrapper:
- prefs = prefs_wrapper.preferences
- if prefs.server_address and current_architecture != _cached_checkpoint_architecture and _pending_checkpoint_refresh_architecture != current_architecture:
-
- def _refresh_checkpoint_for_architecture():
- global _pending_checkpoint_refresh_architecture
- try:
- bpy.ops.stablegen.refresh_checkpoint_list('INVOKE_DEFAULT')
- except Exception as timer_error:
- print(f"StableGen Load Handler: Failed to refresh checkpoints for '{current_architecture}': {timer_error}")
- finally:
- _pending_checkpoint_refresh_architecture = None
- return None
-
- _pending_checkpoint_refresh_architecture = current_architecture
- bpy.app.timers.register(_refresh_checkpoint_for_architecture, first_interval=0.2)
-
-class SceneQueueItem(bpy.types.PropertyGroup):
- """A single item in the scene generation queue."""
- label: bpy.props.StringProperty(name="Label", default="") # type: ignore # user display name
- scene_name: bpy.props.StringProperty(name="Scene", default="") # type: ignore
- blend_file: bpy.props.StringProperty(name="Blend File", default="") # type: ignore # path to .blend COPY
- prompt: bpy.props.StringProperty(name="Prompt", default="") # type: ignore # display-only
- negative_prompt: bpy.props.StringProperty(name="Negative Prompt", default="") # type: ignore
- status: bpy.props.StringProperty(name="Status", default="pending") # type: ignore
- retries: bpy.props.IntProperty(name="Retries", default=0) # type: ignore # how many times this item has been retried
- error_reason: bpy.props.StringProperty(name="Error Reason", default="") # type: ignore # last failure reason
-
-classes_to_append = [CheckServerStatus, RefreshCheckpointList, RefreshLoRAList, STABLEGEN_UL_ControlNetMappingList, ControlNetModelMappingItem, RefreshControlNetMappings, StableGenAddonPreferences, ControlNetUnit, LoRAUnit, AddControlNetUnit, RemoveControlNetUnit, AddLoRAUnit, RemoveLoRAUnit, SceneQueueItem]
-for cls in classes_to_append:
- classes.append(cls)
-for cls in _debug_classes:
- classes.append(cls)
-
-def register():
- """
- Registers the addon.
- :return: None
- """
- for cls in classes:
- bpy.utils.register_class(cls)
-
- # --- Scene Queue properties (on WindowManager so they're global) ---
- bpy.types.WindowManager.sg_scene_queue = bpy.props.CollectionProperty(
- type=SceneQueueItem,
- name="Scene Queue"
- )
- bpy.types.WindowManager.sg_scene_queue_index = bpy.props.IntProperty(
- name="Queue Index", default=0
- )
- bpy.types.WindowManager.sg_show_queue = bpy.props.BoolProperty(
- name="Show Queue", default=False
- )
-
- # --- Queue GIF Export settings (global, applied to all items) ---
- bpy.types.WindowManager.sg_queue_gif_export = bpy.props.BoolProperty(
- name="Export GIF After Each Item", default=False,
- description="Automatically export an orbit GIF/MP4 after each queue item completes"
- )
- bpy.types.WindowManager.sg_queue_gif_duration = bpy.props.FloatProperty(
- name="Duration (s)", default=5.0, min=0.1, max=60.0,
- description="Duration of the 360-degree orbit"
- )
- bpy.types.WindowManager.sg_queue_gif_fps = bpy.props.IntProperty(
- name="FPS", default=24, min=1, max=60,
- description="Frames per second"
- )
- bpy.types.WindowManager.sg_queue_gif_resolution = bpy.props.IntProperty(
- name="Resolution %", default=50, min=10, max=100, subtype='PERCENTAGE',
- description="Percentage of scene render resolution"
- )
- bpy.types.WindowManager.sg_queue_gif_samples = bpy.props.IntProperty(
- name="Samples", default=32, min=1, max=4096,
- description="Render samples per frame"
- )
- bpy.types.WindowManager.sg_queue_gif_engine = bpy.props.EnumProperty(
- name="Engine",
- items=[
- ('BLENDER_WORKBENCH', "Workbench", "Fast preview renderer — no lighting, flat shading"),
- ('EEVEE', "Eevee", "Real-time renderer — good quality with fast render times"),
- ('CYCLES', "Cycles", "Path-traced renderer — highest quality but slowest"),
- ],
- default='CYCLES'
- )
- bpy.types.WindowManager.sg_queue_gif_interpolation = bpy.props.EnumProperty(
- name="Rotation Curve",
- items=[
- ('LINEAR', "Linear", "Constant rotation speed throughout the orbit"),
- ('BEZIER', "Ease In/Out", "Smooth acceleration and deceleration for a polished look"),
- ],
- default='LINEAR'
- )
- bpy.types.WindowManager.sg_queue_gif_use_hdri = bpy.props.BoolProperty(
- name="HDRI Environment", default=False
- )
- bpy.types.WindowManager.sg_queue_gif_hdri_path = bpy.props.StringProperty(
- name="HDRI File",
- description="Path to the HDRI image file (.hdr / .exr). "
- "Applied via AddHDRI before each GIF export",
- default="",
- subtype='FILE_PATH',
- )
- bpy.types.WindowManager.sg_queue_gif_hdri_strength = bpy.props.FloatProperty(
- name="HDRI Strength",
- description="Brightness of the HDRI environment lighting",
- default=1.0, min=0.01, max=10.0,
- )
- bpy.types.WindowManager.sg_queue_gif_hdri_rotation = bpy.props.FloatProperty(
- name="HDRI Rotation", default=0.0, min=0.0, max=360.0, subtype='ANGLE'
- )
- bpy.types.WindowManager.sg_queue_gif_env_mode = bpy.props.EnumProperty(
- name="Environment Mode",
- items=[
- ('FIXED', "Fixed", "HDRI stays in place while the camera orbits"),
- ('LOCKED', "Locked", "HDRI rotates with the camera to keep lighting consistent"),
- ('COUNTER', "Counter-Rotate", "HDRI rotates opposite to the camera for dynamic lighting"),
- ('ENV_ONLY', "Environment Only", "Render only the HDRI environment without the object"),
- ],
- default='FIXED'
- )
- bpy.types.WindowManager.sg_queue_gif_denoiser = bpy.props.BoolProperty(
- name="Denoise", default=True
- )
- bpy.types.WindowManager.sg_queue_gif_use_gpu = bpy.props.BoolProperty(
- name="GPU Compute", default=True,
- description="Use GPU for Cycles rendering (Blender 5.1+ only)"
- )
- bpy.types.WindowManager.sg_queue_gif_also_no_pbr = bpy.props.BoolProperty(
- name="Also Export Without PBR", default=False,
- description="After the main GIF export, disable PBR, reproject, and export a second GIF with emission-only shading (1 sample)"
- )
-
- def initial_refresh():
- print("StableGen: Performing initial model list refresh...")
- try:
- bpy.ops.stablegen.check_server_status('INVOKE_DEFAULT')
- if not bpy.context.preferences.addons.get(__package__).preferences.server_online:
- print("StableGen: Server not reachable during initial refresh.")
- return None
- # Check if server address is set before attempting
- prefs = bpy.context.preferences.addons.get(__package__)
- if prefs and prefs.preferences.server_address:
- bpy.ops.stablegen.refresh_checkpoint_list('INVOKE_DEFAULT')
- bpy.ops.stablegen.refresh_lora_list('INVOKE_DEFAULT')
- bpy.ops.stablegen.refresh_controlnet_mappings('INVOKE_DEFAULT')
- else:
- print("StableGen: Server address not set, skipping initial refresh.")
- # Run load handler to set defaults
- load_handler(None)
- _sg_queue_load()
- except Exception as e:
- # Catch potential errors during startup refresh
- print(f"StableGen: Error during initial refresh: {e}")
-
- return None # Timer runs only once
-
- bpy.app.timers.register(initial_refresh, first_interval=1.0) # Delay slightly
-
- bpy.types.Scene.comfyui_prompt = bpy.props.StringProperty(
- name="ComfyUI Prompt",
- description="Text prompt for generation (also used for texturing unless a separate texture prompt is provided)",
- default="gold cube",
- update=update_parameters
- )
- bpy.types.Scene.use_separate_texture_prompt = bpy.props.BoolProperty(
- name="Use Separate Texture Prompt",
- description="Enable a dedicated prompt field for the texturing pass instead of reusing the main prompt",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.texture_prompt = bpy.props.StringProperty(
- name="Texture Prompt",
- description="Prompt used for the per-camera texturing step. "
- "Avoid view-specific details here (e.g. 'logo on the front') — "
- "this prompt is applied from every camera angle. "
- "Describe side-specific features in the main prompt or in per-camera prompts instead. "
- "When empty, the main prompt is used",
- default="",
- update=update_parameters
- )
- bpy.types.Scene.comfyui_negative_prompt = bpy.props.StringProperty(
- name="ComfyUI Negative Prompt",
- description="Enter the negative text prompt for ComfyUI generation",
- default="",
- update=update_parameters
- )
- bpy.types.Scene.model_name = bpy.props.EnumProperty(
- name="Model Name",
- description="Select the SDXL checkpoint",
- items=update_model_list,
- update=update_parameters
- )
- # Plain-string mirror of model_name so the value survives file loads
- # even when the dynamic EnumProperty items callback returns a stale
- # list (e.g. during queue processing across different architectures).
- bpy.types.Scene.sg_model_name_backup = bpy.props.StringProperty(
- name="Model Name Backup",
- description="Internal: stores the last known-good model_name identifier",
- default="",
- options={'HIDDEN'},
- )
- bpy.types.Scene.seed = bpy.props.IntProperty(
- name="Seed",
- description="Seed for image generation",
- default=42,
- min=0,
- max=1000000,
- update=update_parameters
- )
- bpy.types.Scene.control_after_generate = bpy.props.EnumProperty(
- name="Control After Generate",
- description="Control behavior after generation",
- items=[
- ('fixed', 'Fixed', 'Keep the same seed every generation'),
- ('increment', 'Increment', 'Add 1 to the seed after each generation'),
- ('decrement', 'Decrement', 'Subtract 1 from the seed after each generation'),
- ('randomize', 'Randomize', 'Pick a random seed for every generation')
- ],
- default='fixed',
- update=update_parameters
- )
- bpy.types.Scene.steps = bpy.props.IntProperty(
- name="Steps",
- description="Number of denoising steps. Higher values improve detail and coherence but take longer",
- default=8,
- min=0,
- max=200,
- update=update_parameters
- )
- bpy.types.Scene.cfg = bpy.props.FloatProperty(
- name="CFG",
- description="Classifier-Free Guidance scale. Higher values follow the prompt more strictly but may reduce quality; lower values give more creative freedom",
- default=1.5,
- min=0.0,
- max=100.0,
- update=update_parameters
- )
- bpy.types.Scene.sampler = bpy.props.EnumProperty(
- name="Sampler",
- description="Sampler for generation",
- items=[
- ('euler', 'Euler', ''),
- ('euler_ancestral', 'Euler A', ''),
- ('dpmpp_sde', 'DPM++ SDE', ''),
- ('dpmpp_2m', 'DPM++ 2M', ''),
- ('dpmpp_2s_ancestral', 'DPM++ 2S Ancestral', ''),
- ],
- default='dpmpp_2s_ancestral',
- update=update_parameters
- )
- bpy.types.Scene.scheduler = bpy.props.EnumProperty(
- name="Scheduler",
- description="Scheduler for generation",
- items=[
- ('sgm_uniform', 'SGM Uniform', ''),
- ('karras', 'Karras', ''),
- ('beta', 'Beta', ''),
- ('normal', 'Normal', ''),
- ('simple', 'Simple', ''),
- ],
- default='sgm_uniform',
- update=update_parameters
- )
- bpy.types.Scene.show_advanced_params = bpy.props.BoolProperty(
- name="Show Advanced Parameters",
- description="Show or hide advanced parameters",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.show_generation_params = bpy.props.BoolProperty(
- name="Show Generation Parameters",
- description="Toggle visibility of core generation settings (steps, CFG, sampler, scheduler, seed)",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.auto_rescale = bpy.props.BoolProperty(
- name="Auto Rescale Resolution",
- description="Automatically rescale resolution to appropriate size for the selected model",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.qwen_rescale_alignment = bpy.props.BoolProperty(
- name="Qwen VL-Aligned Rescale",
- description="Round resolution to multiples of 112 instead of 8 when auto-rescaling. "
- "112 is the window size used by the Qwen2.5-VL vision encoder "
- "(LCM of VAE factor 8, ViT patch 14, spatial merge 28). "
- "The official diffusers pipeline rounds to 32; using 112 is stricter "
- "and may reduce subtle zoom / pixel-shift artifacts in some cases",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.auto_rescale_target_mp = bpy.props.FloatProperty(
- name="Target Megapixels",
- description=(
- "Target total megapixels when auto-rescaling the render resolution.\n\n"
- "Default: 1.0 MP (~1024\u00d71024). This is the native resolution for SDXL "
- "and the recommended default for Qwen Image Edit.\n\n"
- "For SDXL / Flux models 1.0 MP is a safe default that matches "
- "the training resolution. Higher values are possible if your "
- "model and hardware support it, but may reduce quality or cause "
- "out-of-memory issues.\n\n"
- "For Qwen Image Edit the vision encoder becomes unreliable above "
- "~1.4 MP and the model often fails to follow the prompt above "
- "~1.2 MP. Values below ~0.3 MP also cause failures.\n\n"
- "Adjust with care \u2013 staying close to 1.0 MP works best for most setups"
- ),
- default=1.0,
- min=0.1,
- max=4.0,
- step=10,
- precision=2,
- update=update_parameters
- )
- bpy.types.Scene.use_ipadapter = bpy.props.BoolProperty(
- name="Use IPAdapter",
- description="""Use IPAdapter for image generation. Requires an external reference image. Can improve consistency, can be useful for generating images with similar styles.\n\n - Has priority over mode specific IPAdapter.""",
- default=False,
- update=update_parameters
- )
- #IPAdapter image
- bpy.types.Scene.ipadapter_image = bpy.props.StringProperty(
- name="Reference Image",
- description="Path to the reference image",
- default="",
- subtype='FILE_PATH',
- update=update_parameters
- )
- bpy.types.Scene.ipadapter_strength = bpy.props.FloatProperty(
- name="IPAdapter Strength",
- description="Strength for IPAdapter",
- default=1.0,
- min=-1.0,
- max=3.0,
- update=update_parameters
- )
- bpy.types.Scene.ipadapter_start = bpy.props.FloatProperty(
- name="IPAdapter Start",
- description="Start percentage for IPAdapter (/100)",
- default=0.0,
- min=0.0,
- max=1.0,
- update=update_parameters
- )
- bpy.types.Scene.ipadapter_end = bpy.props.FloatProperty(
- name="IPAdapter End",
- description="End percentage for IPAdapter (/100)",
- default=1.0,
- min=0.0,
- max=1.0,
- update=update_parameters
- )
- bpy.types.Scene.ipadapter_weight_type = bpy.props.EnumProperty(
- name="IPAdapter Weight Type",
- description="Weight type for IPAdapter",
- items=[
- ('standard', 'Standard', ''),
- ('prompt', 'Prompt is more important', ''),
- ('style', 'Style transfer', ''),
- ],
- default='style',
- update=update_parameters
- )
- bpy.types.Scene.sequential_ipadapter = bpy.props.BoolProperty(
- name="Use IPAdapter",
- description="""Uses IPAdapter to improve consistency between images.\n\n - Applicable for Separate, Sequential and Refine modes.\n - Uses either the first generated image or the most recent one as a reference for the rest of the images.\n - If 'Regenerate IPAdapter' is enabled, the first viewpoint will be regenerated with IPAdapter to match the rest of the images.\n - If 'Use IPAdapter (External Image)' is enabled, this setting is effectively overriden.""",
- default=False,
- update=update_parameters
- )
- def _get_ipadapter_mode_items(self, context):
- items = [
- ('first', 'Use first generated image', '', 0),
- ('recent', 'Use most recent generated image', '', 1),
- ]
- if context:
- is_local_edit = (
- context.scene.generation_method == 'local_edit'
- or (context.scene.model_architecture.startswith('qwen')
- and context.scene.qwen_generation_method == 'local_edit')
- )
- if is_local_edit:
- items.append(('original_render', 'Use original render', 'Uses the existing texture render from each camera viewpoint as IPAdapter reference', 2))
- # TRELLIS.2 input image option — only when architecture is trellis2
- if getattr(context.scene, 'architecture_mode', '') == 'trellis2':
- items.append(('trellis2_input', 'Use TRELLIS.2 input image',
- 'Uses the input image from TRELLIS.2 mesh generation as IPAdapter reference', 3))
- return items
- bpy.types.Scene.sequential_ipadapter_mode = bpy.props.EnumProperty(
- name="IPAdapter Mode",
- description="Mode for IPAdapter in sequential generation",
- items=_get_ipadapter_mode_items,
- update=update_parameters
- )
- bpy.types.Scene.sequential_desaturate_factor = bpy.props.FloatProperty(
- name="Desaturate Recent Image",
- description="Desaturation factor for the 'most recent' image to prevent color stacking. 0.0 is no change, 1.0 is fully desaturated",
- default=0.0,
- min=0.0,
- max=1.0,
- update=update_parameters
- )
- bpy.types.Scene.sequential_contrast_factor = bpy.props.FloatProperty(
- name="Reduce Contrast of Recent Image",
- description="Contrast reduction factor for the 'most recent' image to prevent contrast stacking. 0.0 is no change, 1.0 is maximum reduction (grey)",
- default=0.0,
- min=0.0,
- max=1.0,
- update=update_parameters
- )
- bpy.types.Scene.sequential_ipadapter_regenerate = bpy.props.BoolProperty(
- name="Regenerate IPAdapter",
- description="IPAdapter generations may differ from the original image. This option regenerates the first viewpoint with IPAdapter to match the rest of the images.",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.sequential_ipadapter_regenerate_wo_controlnet = bpy.props.BoolProperty(
- name="Generate IPAdapter reference without ControlNet",
- description="Generate the first viewpoint with IPAdapter without ControlNet. This is useful for generating a reference image that is not affected by ControlNet. Can possibly generate higher quality reference.",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.generation_method = bpy.props.EnumProperty(
- name="Generation Mode",
- description="Choose the mode for generating images",
- items=[
- ('separate', 'Generate Separately', 'Generates images one by one for each viewpoint. Each image is generated independently using only its own control signals (e.g., depth map) without context from other views. All images are applied at the end.'),
- ('sequential', 'Generate Sequentially', 'Generates images viewpoint by viewpoint. After the first view, each subsequent view is generated using inpainting, guided by a visibility mask and an RGB render of the texture projected from previous viewpoints to maintain consistency.'),
- ('grid', 'Generate Using Grid', 'Combines control signals from all viewpoints into a single grid, generates a single image, then splits it back into individual viewpoint textures. Faster but lower resolution per view. Includes an optional second pass to refine each split image individually at full resolution for improved quality.'),
- ('refine', 'Refine/Restyle Texture (Img2Img)', 'Uses the current rendered texture appearance as input for an img2img generation pass. Replaces the previous material with the new result. Good for restyling or globally changing the look of an existing texture. Works on any existing material setup.'),
- ('local_edit', 'Local Edit', 'Make localized changes to an existing texture. Point cameras at areas you want to modify — the new generation blends over the original using angle and vignette-based feathering, preserving untouched areas. Works only with StableGen generated textures.'),
- ('uv_inpaint', 'UV Inpaint Missing Areas', 'Identifies untextured areas on a standard UV map using a visibility calculation. Performs baking if not baked already. Performs diffusion inpainting directly on the UV texture map to fill only these missing regions, using the surrounding texture as context.'),
- ],
- default='sequential',
- update=update_parameters
- )
- bpy.types.Scene.qwen_generation_method = bpy.props.EnumProperty(
- name="Generation Mode",
- description="Choose the mode for generating images with Qwen",
- items=[
- ('generate', 'Generate', 'Standard generation mode'),
- ('refine', 'Refine', 'Refine/restyle the entire texture using Qwen Image Edit. Replaces the existing material with the new result. Describe the desired look in the prompt — you can completely change the style, color scheme, or overall appearance.'),
- ('local_edit', 'Local Edit', 'Make targeted changes to specific areas of the texture. Point cameras at what you want to change and describe the edit — you can change colors, add details, sharpen, alter text, or restyle selected parts. Untouched areas are preserved.'),
- ],
- default='generate',
- update=update_parameters
- )
-
- bpy.types.Scene.qwen_refine_use_prev_ref = bpy.props.BoolProperty(
- name="Use Previous Refined View",
- description="Use the previous modified/refined view output as a second image for reference (Image 2)",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.qwen_refine_use_depth = bpy.props.BoolProperty(
- name="Use Depth Map",
- description="Include depth map as an additional reference image (Image 3)",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.qwen_timestep_zero_ref = bpy.props.BoolProperty(
- name="Timestep-Zero References",
- description="Tell the model that reference images are fully clean (timestep 0) instead of noisy. "
- "Reduces color shift and over-saturation, especially with Qwen 2511. "
- "Requires the FluxKontextMultiReferenceLatentMethod node in ComfyUI",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.refine_images = bpy.props.BoolProperty(
- name="Refine Images",
- description="Refine images after generation",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.refine_steps = bpy.props.IntProperty(
- name="Refine Steps",
- description="Number of steps for refining",
- default=8,
- min=0,
- max=200,
- update=update_parameters
- )
- bpy.types.Scene.refine_sampler = bpy.props.EnumProperty(
- name="Refine Sampler",
- description="Sampler for refining",
- items=[
- ('euler', 'Euler', ''),
- ('euler_ancestral', 'Euler A', ''),
- ('dpmpp_sde', 'DPM++ SDE', ''),
- ('dpmpp_2m', 'DPM++ 2M', ''),
- ('dpmpp_2s_ancestral', 'DPM++ 2S Ancestral', ''),
- ],
- default='dpmpp_2s_ancestral',
- update=update_parameters
- )
- bpy.types.Scene.refine_scheduler = bpy.props.EnumProperty(
- name="Refine Scheduler",
- description="Scheduler for refining",
- items=[
- ('sgm_uniform', 'SGM Uniform', ''),
- ('karras', 'Karras', ''),
- ('beta', 'Beta', ''),
- ('normal', 'Normal', ''),
- ('simple', 'Simple', ''),
- ],
- default='sgm_uniform',
- update=update_parameters
- )
- bpy.types.Scene.denoise = bpy.props.FloatProperty(
- name="Denoise",
- description="Denoise level for refining",
- default=0.8,
- min=0.0,
- max=1.0,
- update=update_parameters
- )
- bpy.types.Scene.refine_cfg = bpy.props.FloatProperty(
- name="Refine CFG",
- description="Classifier-Free Guidance scale for refining",
- default=1.5,
- min=0.0,
- max=100.0,
- update=update_parameters
- )
- bpy.types.Scene.refine_prompt = bpy.props.StringProperty(
- name="Refine Prompt",
- description="Prompt for refining (leave empty to use same prompt as generation)",
- default="",
- update=update_parameters
- )
- bpy.types.Scene.refine_upscale_method = bpy.props.EnumProperty(
- name="Refine Upscale Method",
- description="Upscale method for refining",
- items=[
- ('nearest-exact', 'Nearest Exact', ''),
- ('bilinear', 'Bilinear', ''),
- ('bicubic', 'Bicubic', ''),
- ('lanczos', 'Lanczos', ''),
- ],
- default='lanczos',
- update=update_parameters
- )
- bpy.types.Scene.generation_status = bpy.props.EnumProperty(
- name="Generation Status",
- description="Status of the generation process",
- items=[
- ('idle', 'Idle', ''),
- ('running', 'Running', ''),
- ('waiting', 'Waiting for cancel', ''),
- ('error', 'Error', '')
- ],
- default='idle',
- update=update_parameters
- )
- bpy.types.Scene.sg_last_gen_error = bpy.props.BoolProperty(
- name="Last Generation Error",
- description="True if the most recent generation ended with an error",
- default=False,
- )
- bpy.types.Scene.generation_progress = bpy.props.FloatProperty(
- name="Generation Progress",
- description="Current progress of image generation",
- default=0.0,
- min=0.0,
- max=100.0,
- update=update_parameters
- )
- bpy.types.Scene.overwrite_material = bpy.props.BoolProperty(
- name="Overwrite Material",
- description="Overwrite existing material",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.bake_visibility_weights = bpy.props.BoolProperty(
- name="Bake Visibility Weights",
- description="Pre-compute per-vertex visibility weights in Python instead of "
- "using shader Raycast nodes. Makes projected textures survive "
- "object transforms (move/rotate/scale) at the cost of vertex-level "
- "weight resolution instead of per-pixel. Recommended for meshes "
- "with >500 faces",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.discard_factor = bpy.props.FloatProperty(
- name="Discard Factor",
- description="If the texture is facing the camera at an angle greater than this value, it will be discarded. This is useful for preventing artifacts from the very edge of the generated texture appearing when keeping high discard factor (use ~65 for best results when generating textures around an object)",
- default=90.0,
- min=0.0,
- max=180.0,
- update=update_parameters
- )
-
- bpy.types.Scene.discard_factor_generation_only = bpy.props.BoolProperty(
- name="Reset Discard Angle After Generation",
- description="If enabled, the 'Discard Factor' will be reset to a specified value after generation completes. Useful for sequential/Qwen modes where a low discard angle is needed during generation but not for final blending",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.discard_factor_after_generation = bpy.props.FloatProperty(
- name="Discard Factor After Generation",
- description="The value to set the 'Discard Factor' to after generation is complete",
- default=90.0,
- min=0.0,
- max=180.0,
- update=update_parameters
- )
- bpy.types.Scene.weight_exponent_generation_only = bpy.props.BoolProperty(
- name="Reset Exponent After Generation",
- description="If enabled, the Weight Exponent will be reset to a specified value after generation completes. Useful for Voronoi projection mode where a high exponent produces sharp segmentation during generation but softer blending is preferred for the final result",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.weight_exponent_after_generation = bpy.props.FloatProperty(
- name="Exponent After Generation",
- description="The value to set the Weight Exponent to after generation is complete",
- default=15.0,
- min=0.01,
- max=1000.0,
- update=update_parameters
- )
- bpy.types.Scene.view_blend_use_color_match = bpy.props.BoolProperty(
- name="Match Colors to Viewport",
- description="Match each generated view’s colors to the current viewport texture before blending",
- default=False,
- update=update_parameters,
- )
- bpy.types.Scene.view_blend_color_match_method = bpy.props.EnumProperty(
- name="Color Match Method",
- description="Algorithm used when matching view colors to the viewport texture",
- items=[
- ("mkl", "MKL", ""),
- ("hm", "Histogram", ""),
- ("reinhard", "Reinhard", ""),
- ("mvgd", "MVGD", ""),
- ("hm-mvgd-hm", "HM–MVGD–HM", ""),
- ("hm-mkl-hm", "HM–MKL–HM", ""),
- ],
- default="hm-mvgd-hm",
- update=update_parameters,
- )
- bpy.types.Scene.view_blend_color_match_strength = bpy.props.FloatProperty(
- name="Match Strength",
- description="Blend between original and viewport-matched colors",
- default=1.0,
- min=0.0,
- max=2.0,
- update=update_parameters,
- )
- bpy.types.Scene.weight_exponent = bpy.props.FloatProperty(
- name="Weight Exponent",
- description="Controls the falloff curve for viewpoint weighting based on the angle to the surface normal (θ). "
- "Weight = |cos(θ)|^Exponent. Higher values prioritize straight-on views more strongly, creating sharper transitions. "
- "1.0 = standard |cos(θ)| weighting..",
- default=3.0,
- min=0.1,
- max=1000.0,
- update=update_parameters
- )
- bpy.types.Scene.allow_modify_existing_textures = bpy.props.BoolProperty(
- name="Allow modifying existing textures",
- description="Disconnect compare node in export_visibility so that smooth output is not pure 1 areas",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.ask_object_prompts = bpy.props.BoolProperty(
- name="Ask for object prompts",
- description="Use object-specific prompts; if disabled, the normal prompt is used for all objects",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.fallback_color = bpy.props.FloatVectorProperty(
- name="Fallback Color",
- description="Color to use as fallback in texture generation",
- subtype='COLOR',
- default=(0.0, 0.0, 0.0),
- min=0.0, max=1.0,
- update=update_parameters
- )
- bpy.types.Scene.sequential_smooth = bpy.props.BoolProperty(
- name="Sequential Smooth",
- description="""Use smooth visibility map for sequential generation mode. Disabling this uses a binary visibility map and may need more mask blurring to reduce artifacts.
-
- - Visibility map is a mask that indicates which pixels have textures already projected from previous viewpoints.
- - Both methods are using weights which are calculated based on the angle between the surface normal and the camera view direction.
- - 'Smooth' uses these calculated weights directly (0.0-1.0 range, giving gradual transitions). The transition point can be further tuned by the 'Smooth Factor' parameters.
- - Disabling 'Smooth' thresholds these weights to create a hard-edged binary mask (0.0 or 1.0).""",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.weight_exponent_mask = bpy.props.BoolProperty(
- name="Weight Exponent Mask",
- description="Use weight exponent for visibility map generation. Uses 1.0 if disabled.",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.canny_threshold_low = bpy.props.IntProperty(
- name="Canny Threshold Low",
- description="Low threshold for Canny edge detection",
- default=0,
- min=0,
- max=255,
- update=update_parameters
- )
- bpy.types.Scene.canny_threshold_high = bpy.props.IntProperty(
- name="Canny Threshold High",
- description="High threshold for Canny edge detection",
- default=80,
- min=0,
- max=255,
- update=update_parameters
- )
- bpy.types.Scene.sequential_factor_smooth = bpy.props.FloatProperty(
- name="Smooth Visibility Black Point",
- description="Controls the black point (start) of the Color Ramp used for the smooth visibility mask in sequential mode. Defines the weight threshold below which areas are considered fully invisible/untextured from previous views. Higher values create a sharper transition start.",
- default=0.15,
- min=0.0,
- max=1.0,
- update=update_parameters
- )
- bpy.types.Scene.sequential_factor_smooth_2 = bpy.props.FloatProperty(
- name="Smooth Visibility White Point",
- description="Controls the white point (end) of the Color Ramp used for the smooth visibility mask in sequential mode. Defines the weight threshold above which areas are considered fully visible/textured from previous views. Lower values create a sharper transition end.",
- default=1.0,
- min=0.0,
- max=1.0,
- update=update_parameters
- )
- bpy.types.Scene.sequential_factor = bpy.props.FloatProperty(
- name="Binary Visibility Threshold",
- description="Threshold value used when 'Sequential Smooth' is OFF. Calculated visibility weights below this value are treated as 0 (invisible), and those above as 1 (visible), creating a hard-edged binary mask.",
- default=0.7,
- min=0.0,
- max=1.0,
- update=update_parameters
- )
- bpy.types.Scene.differential_noise = bpy.props.BoolProperty(
- name="Differential Noise",
- description="Adds latent noise mask to the image before inpainting. This must be used with low factor smooth mask or with a high blur mask radius. Disabling this effectively discrads the mask and only uses the inapaint conditioning.",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.grow_mask_by = bpy.props.IntProperty(
- name="Grow Mask By",
- description="Grow mask by this amount (ComfyUI)",
- default=3,
- min=0,
- update=update_parameters
- )
- bpy.types.Scene.mask_blocky = bpy.props.BoolProperty(
- name="Blocky Visibility Map",
- description="Uses a blocky visibility map. This will downscale the visibility map according to the 8x8 grid which Stable Diffusion uses in latent space. Highly experimental.",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.visibility_vignette = bpy.props.BoolProperty(
- name="Feather Visibility Edges",
- description="Blend refinement edges using a vignette mask to reduce seams",
- default=True,
- update=update_parameters,
- )
-
- bpy.types.Scene.visibility_vignette_width = bpy.props.FloatProperty(
- name="Vignette Width",
- description="Fraction of the image radius used as a feather band (0 = no feather, 0.5 = very soft edges)",
- default=0.15,
- min=0.0,
- max=0.5,
- update=update_parameters,
- )
- bpy.types.Scene.visibility_vignette_softness = bpy.props.FloatProperty(
- name="Vignette Softness",
- description="Exponent shaping feather falloff (<1 = sharper transition, >1 = softer/wider transition)",
- default=1.0,
- min=0.1,
- max=5.0,
- update=update_parameters,
- )
- bpy.types.Scene.visibility_vignette_blur = bpy.props.BoolProperty(
- name="Blur Vignette Mask",
- description="Apply a Gaussian blur to the vignette mask to soften edges",
- default=False,
- update=update_parameters,
- )
-
- bpy.types.Scene.sg_silhouette_margin = bpy.props.IntProperty(
- name="Silhouette Margin",
- description="Pixel margin around occluder silhouettes where projection is suppressed. "
- "Prevents thin outlines of foreground geometry (e.g. hands) bleeding onto "
- "surfaces behind them. 0 = disabled",
- default=3,
- min=0,
- max=20,
- update=update_parameters,
- )
-
- 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,
- )
-
- bpy.types.Scene.sg_silhouette_rays = bpy.props.EnumProperty(
- name="Silhouette Rays",
- description="Number of offset directions for silhouette detection. "
- "4 = cardinal only (faster), 8 = cardinal + diagonal (better coverage)",
- items=[
- ('4', "4 (Cardinal)", "Right, Left, Up, Down"),
- ('8', "8 (Cardinal + Diagonal)", "Cardinal + 4 diagonal directions"),
- ],
- default='4',
- update=update_parameters,
- )
-
- # Refine Mode Ramp Controls
- bpy.types.Scene.refine_angle_ramp_active = bpy.props.BoolProperty(
- name="Use Angle Ramp",
- description="Blend refinement based on surface angle",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.refine_angle_ramp_pos_0 = bpy.props.FloatProperty(
- name="Angle Ramp Black Point",
- description="Position of the black point (Invisible) for the angle ramp",
- default=0.0,
- min=0.0,
- max=1.0,
- update=update_parameters
- )
- bpy.types.Scene.refine_angle_ramp_pos_1 = bpy.props.FloatProperty(
- name="Angle Ramp White Point",
- description="Position of the white point (Visible) for the angle ramp",
- default=0.05,
- min=0.0,
- max=1.0,
- update=update_parameters
- )
- bpy.types.Scene.refine_feather_ramp_pos_0 = bpy.props.FloatProperty(
- name="Feather Ramp Black Point",
- description="Position of the black point (Invisible) for the feather ramp",
- default=0.0,
- min=0.0,
- max=1.0,
- update=update_parameters
- )
- bpy.types.Scene.refine_feather_ramp_pos_1 = bpy.props.FloatProperty(
- name="Feather Ramp White Point",
- description="Position of the white point (Visible) for the feather ramp",
- default=0.6,
- min=0.0,
- max=1.0,
- update=update_parameters
- )
-
- bpy.types.Scene.refine_edge_feather_projection = bpy.props.BoolProperty(
- name="Edge Feather (Projection)",
- description="Add a screen-space distance-transform ramp at the projection "
- "silhouette boundary as an additional multiplier on top of the "
- "angle×feather weight. Interior surfaces keep full strength; "
- "only the geometric edge is softened",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.refine_edge_feather_width = bpy.props.IntProperty(
- name="Edge Feather Width",
- description="Width in pixels of the transition band at projection boundaries. "
- "Larger values produce a wider blend zone",
- default=30,
- min=1,
- max=200,
- update=update_parameters
- )
- bpy.types.Scene.refine_edge_feather_softness = bpy.props.FloatProperty(
- name="Edge Feather Softness",
- description="Rounds off the sharp corners at both ends of the linear feather ramp "
- "using a Gaussian blur. 0 = raw linear ramp (hard kinks at edge and "
- "interior boundary). 1 = moderate smoothing. Higher values give "
- "progressively gentler transitions without shifting the ramp position",
- default=1.0,
- min=0.0,
- max=5.0,
- step=10,
- precision=2,
- update=update_parameters
- )
-
- bpy.types.Scene.differential_diffusion = bpy.props.BoolProperty(
- name="Differential Diffusion",
- description="Replace standard inpainting with a differential diffusion based workflow\n\n - Generally works better and reduces artifacts.\n - Using a Smooth Visibilty Map is recommended for Sequential Mode.",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.blur_mask = bpy.props.BoolProperty(
- name="Blur Mask",
- description="Blur mask before inpainting (ComfyUI)",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.blur_mask_radius = bpy.props.IntProperty(
- name="Blur Mask Radius",
- description="Radius for mask blurring (ComfyUI)",
- default=1,
- min=1,
- max=31,
- update=update_parameters
- )
- bpy.types.Scene.blur_mask_sigma = bpy.props.FloatProperty(
- name="Blur Mask Sigma",
- description="Sigma for mask blurring (ComfyUI)",
- default=1.0,
- min=0.1,
- update=update_parameters
- )
- bpy.types.Scene.sequential_custom_camera_order = bpy.props.StringProperty(
- name="Custom Camera Order",
- description="""Custom camera order for Sequential Mode. Format: 'index1,index2,index3,...'
-
- - This will permanently change the order of the cameras in the scene.""",
- default="",
- update=update_parameters
- )
- bpy.types.Scene.clip_skip = bpy.props.IntProperty(
- name="CLIP Skip",
- description="CLIP skip value for generation",
- default=1,
- min=1,
- update=update_parameters
- )
- bpy.types.Scene.stablegen_preset = bpy.props.EnumProperty(
- name="Preset",
- description="Select a preset for easy mode",
- items=get_preset_items,
- default=0
- )
-
- bpy.types.Scene.active_preset = bpy.props.StringProperty(
- name="Active Preset",
- default="DEFAULT"
- )
-
- bpy.types.Scene.model_architecture = bpy.props.EnumProperty(
- name="Model Architecture",
- description="Select the model architecture to use for generation",
- items=[
- ('sdxl', 'SDXL', ''),
- ('flux1', 'FLUX.1', ''),
- ('qwen_image_edit', 'Qwen Image Edit', ''),
- ('flux2_klein', 'FLUX.2 Klein', '')
- ],
- default='sdxl',
- update=update_combined
- )
-
- bpy.types.Scene.architecture_mode = bpy.props.EnumProperty(
- name="Architecture",
- description="Select the overall generation architecture",
- items=[
- ('sdxl', 'SDXL', 'Stable Diffusion XL'),
- ('flux1', 'FLUX.1', 'FLUX.1 architecture'),
- ('qwen_image_edit', 'Qwen Image Edit', 'Qwen Image Edit architecture'),
- ('flux2_klein', 'FLUX.2 Klein', 'FLUX.2 Klein multi-reference edit model (Apache 2.0)'),
- ('trellis2', 'TRELLIS.2', 'Image to 3D mesh generation with TRELLIS.2'),
- ],
- default='sdxl',
- update=update_architecture_mode
- )
-
- bpy.types.Scene.trellis2_generate_from = bpy.props.EnumProperty(
- name="Generate From",
- description="Source for the TRELLIS.2 input",
- items=[
- ('image', 'Image', 'Use an existing image as input'),
- ('prompt', 'Prompt', 'Generate an image from the prompt first, then feed to TRELLIS.2'),
- ],
- default='image',
- update=update_trellis2_generate_from
- )
-
- bpy.types.Scene.trellis2_texture_mode = bpy.props.EnumProperty(
- name="Texture Mode",
- description="How to generate textures for the 3D mesh",
- items=[
- ('none', 'None', 'Shape only, no texture generation'),
- ('native', 'Native (TRELLIS.2)', 'Use TRELLIS.2 built-in texture generation'),
- ('sdxl', 'SDXL', 'Use SDXL for camera-based texture projection'),
- ('flux1', 'FLUX.1', 'Use FLUX.1 for camera-based texture projection'),
- ('qwen_image_edit', 'Qwen Image Edit', 'Use Qwen for camera-based texture projection'),
- ('flux2_klein', 'FLUX.2 Klein', 'Use FLUX.2 Klein for camera-based texture projection'),
- ],
- default='native',
- update=update_trellis2_texture_mode
- )
-
- bpy.types.Scene.trellis2_initial_image_arch = bpy.props.EnumProperty(
- name="Initial Image Architecture",
- description="Diffusion architecture used to generate the initial image when Generate From is set to Prompt and Texture Mode is Native or None",
- items=[
- ('sdxl', 'SDXL', 'Stable Diffusion XL'),
- ('flux1', 'FLUX.1', 'FLUX.1 architecture'),
- ('qwen_image_edit', 'Qwen Image Edit', 'Qwen Image Edit architecture'),
- ('flux2_klein', 'FLUX.2 Klein', 'FLUX.2 Klein multi-reference edit'),
- ],
- default='sdxl',
- update=update_trellis2_initial_image_arch
- )
-
- bpy.types.Scene.trellis2_camera_count = bpy.props.IntProperty(
- name="Camera Count",
- description="Number of cameras to place around the generated mesh for texture projection",
- default=8,
- min=2,
- max=32,
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_placement_mode = bpy.props.EnumProperty(
- name="Placement Mode",
- description="Strategy for placing cameras around the generated mesh",
- items=[
- ('orbit_ring', "Orbit Ring", "Place cameras in a circle around the center"),
- ('hemisphere', "Sphere Coverage", "Distribute cameras evenly across a sphere using a Fibonacci spiral"),
- ('normal_weighted', "Normal-Weighted", "Automatically place cameras to cover the most surface area, using K-means on face normals weighted by area"),
- ('pca_axes', "PCA Axes", "Place cameras along the mesh's principal axes of variation"),
- ('greedy_coverage', "Greedy Coverage", "Iteratively add cameras that maximise new visible surface. Automatically determines the number of cameras needed"),
- ('fan_from_camera', "Fan from Camera", "Spread cameras in an arc around the active camera's orbit position"),
- ],
- default='normal_weighted',
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_auto_prompts = bpy.props.BoolProperty(
- name="Auto View Prompts",
- description="Automatically generate view-direction prompts for each camera (e.g. 'front view', 'rear view, from above')",
- default=True,
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_exclude_bottom = bpy.props.BoolProperty(
- name="Exclude Bottom Faces",
- description="Ignore downward-facing geometry when placing cameras",
- default=True,
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_exclude_bottom_angle = bpy.props.FloatProperty(
- name="Bottom Angle Threshold",
- description="Faces whose normal points more than this many degrees below the horizon are excluded",
- default=1.5533, # 89 degrees in radians
- min=0.1745,
- max=1.5708,
- subtype='ANGLE',
- unit='ROTATION',
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_auto_aspect = bpy.props.EnumProperty(
- name="Auto Aspect Ratio",
- description="Automatically adjust render aspect ratio to match the mesh silhouette",
- items=[
- ('off', "Off", "Use current scene resolution for all cameras"),
- ('shared', "Shared", "Average silhouette aspect across all cameras and set a single scene resolution"),
- ('per_camera', "Per Camera", "Each camera gets its own optimal aspect ratio"),
- ],
- default='per_camera',
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_occlusion_mode = bpy.props.EnumProperty(
- name="Occlusion Handling",
- description="How to account for self-occlusion when choosing camera directions",
- items=[
- ('none', "None (Fast)", "Back-face culling only — ignores self-occlusion"),
- ('full_matrix', "Full Occlusion Matrix", "Build a complete BVH-validated visibility matrix. Most accurate but slower"),
- ('two_pass', "Two-Pass Refinement", "Fast back-face pass, then targeted BVH refinement"),
- ('vis_weighted', "Visibility-Weighted", "Weight faces by visibility fraction; mostly-occluded faces have reduced influence"),
- ],
- default='none',
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_consider_existing = bpy.props.BoolProperty(
- name="Consider Existing Cameras",
- description="Treat existing cameras as already-placed directions so auto modes avoid duplicating their coverage",
- default=True,
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_delete_cameras = bpy.props.BoolProperty(
- name="Delete Cameras After",
- description="Automatically delete cameras placed during the TRELLIS.2 pipeline once generation and texturing are complete",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_coverage_target = bpy.props.FloatProperty(
- name="Coverage Target",
- description="Stop adding cameras when this fraction of surface area is visible (Greedy mode)",
- default=0.95,
- min=0.5,
- max=1.0,
- subtype='FACTOR',
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_max_auto_cameras = bpy.props.IntProperty(
- name="Max Cameras (Greedy)",
- description="Upper limit on cameras for Greedy Coverage mode",
- default=12,
- min=2,
- max=50,
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_fan_angle = bpy.props.FloatProperty(
- name="Fan Angle",
- description="Total angular spread of the fan in degrees",
- default=90.0,
- min=10.0,
- max=350.0,
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_import_scale = bpy.props.FloatProperty(
- name="Import Scale (BU)",
- description="Scale imported TRELLIS.2 meshes so the longest axis equals this many Blender units. Set to 0 to keep the original size",
- default=2.0,
- min=0.0,
- max=100.0,
- soft_min=0.0,
- soft_max=10.0,
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_shade_mode = bpy.props.EnumProperty(
- name="Shading",
- description="Shading mode applied to imported TRELLIS.2 meshes",
- items=[
- ('flat', "Flat", "Keep flat shading (default)"),
- ('smooth', "Smooth", "Apply smooth shading to the entire mesh"),
- ('auto_smooth', "Auto Smooth", "Apply auto smooth shading by angle"),
- ],
- default='flat',
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_clamp_elevation = bpy.props.BoolProperty(
- name="Clamp Elevation",
- description="Restrict camera elevation to avoid extreme top-down or bottom-up views that diffusion models often struggle with",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_max_elevation = bpy.props.FloatProperty(
- name="Max Elevation",
- description="Maximum upward elevation angle. Cameras looking further up will be clamped",
- default=1.2217, # 70 degrees
- min=0.0,
- max=1.5708, # 90 degrees
- subtype='ANGLE',
- unit='ROTATION',
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_min_elevation = bpy.props.FloatProperty(
- name="Min Elevation",
- description="Minimum downward elevation angle. Cameras looking further down will be clamped",
- default=-0.1745, # -10 degrees
- min=-1.5708, # -90 degrees
- max=0.0,
- subtype='ANGLE',
- unit='ROTATION',
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_preview_gallery_enabled = bpy.props.BoolProperty(
- name="Preview Gallery",
- description="When in prompt mode, generate multiple images with different seeds and let you pick the best one before proceeding to 3D generation",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.trellis2_preview_gallery_count = bpy.props.IntProperty(
- name="Gallery Count",
- description="Number of images to generate per batch in the preview gallery",
- default=4,
- min=1,
- max=16,
- update=update_parameters
- )
-
- bpy.types.Scene.qwen_guidance_map_type = bpy.props.EnumProperty(
- name="Guidance Map",
- description="The type of guidance map to use for Qwen Image Edit",
- items=[
- ('depth', 'Depth Map', 'Use depth map for structural guidance'),
- ('normal', 'Normal Map', 'Use normal map for structural guidance'),
- ('workbench', 'Workbench Render', 'Use workbench render for structural guidance'),
- ('viewport', 'Viewport Render', 'Use viewport render (OpenGL) for structural guidance')
- ],
- default='depth',
- update=update_parameters
- )
-
- bpy.types.Scene.qwen_voronoi_mode = bpy.props.BoolProperty(
- name="Voronoi Projection",
- description=(
- "Instead of zeroing weights of non-generated cameras, keep natural "
- "angle-based weights and project magenta from cameras that have not "
- "been generated yet. Use a high Weight Exponent to achieve Voronoi-"
- "like segmentation where each surface point is dominated by its "
- "closest camera. This eliminates the need for low discard-over-angle "
- "thresholds"
- ),
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.qwen_context_render_mode = bpy.props.EnumProperty(
- name="Context Render",
- description="How to use the RGB context render in sequential mode for Qwen",
- items=[
- ('NONE', 'Disabled', 'Do not use the RGB context render'),
- ('REPLACE_STYLE', 'Replace Style Image', 'Use context render instead of the previous generated image as the style reference'),
- ('ADDITIONAL', 'Additional Context', 'Use context render as an additional image input (image 3) for context')
- ],
- default='NONE',
- update=update_parameters
- )
-
- bpy.types.Scene.qwen_use_external_style_image = bpy.props.BoolProperty(
- name="Use External Style Image",
- description="Use a separate, external image as the style reference for all viewpoints",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.qwen_external_style_image = bpy.props.StringProperty(
- name="Style Reference Image",
- description="Path to the external style reference image",
- default="",
- subtype='FILE_PATH',
- update=update_parameters
- )
-
- bpy.types.Scene.qwen_external_style_initial_only = bpy.props.BoolProperty(
- name="External for Initial Only",
- description="Use the external style image for the first image, then use the previously generated image for subsequent images",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.qwen_use_custom_prompts = bpy.props.BoolProperty(
- name="Use Custom Qwen Prompts",
- description="Enable to override the default guidance prompts for the Qwen Image Edit workflow",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.qwen_custom_prompt_initial = bpy.props.StringProperty(
- name="Initial Image Prompt",
- description="Custom prompt for the first generated image. Use {main_prompt} to insert the main prompt text.",
- default="Change and transfer the format of '{main_prompt}' in image 1 to the style from image 2",
- update=update_parameters
- )
-
- bpy.types.Scene.qwen_custom_prompt_seq_none = bpy.props.StringProperty(
- name="Sequential Prompt (No Context)",
- description="Custom prompt for subsequent images when Context Render is 'Disabled'. Use {main_prompt} to insert the main prompt text.",
-
- default="Change and transfer the format of '{main_prompt}' in image 1 to the style from image 2",
- update=update_parameters
- )
-
- bpy.types.Scene.qwen_custom_prompt_seq_replace = bpy.props.StringProperty(
- name="Sequential Prompt (Replace Style)",
- description="Custom prompt for subsequent images when Context Render is 'Replace Style'. Use {main_prompt} to insert the main prompt text.",
- default="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.",
- update=update_parameters
- )
-
- bpy.types.Scene.qwen_custom_prompt_seq_additional = bpy.props.StringProperty(
- name="Sequential Prompt (Additional Context)",
- description="Custom prompt for subsequent images when Context Render is 'Additional Context'. Use {main_prompt} to insert the main prompt text.",
- default="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.",
- 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.qwen_prompt_gray_background = bpy.props.BoolProperty(
- name="Gray Background Prompt",
- description="Include 'Replace the background with solid gray' in the default context-render prompt. Disable to let the model choose the background freely",
- default=True,
- update=update_parameters
- )
-
- bpy.types.Scene.output_timestamp = bpy.props.StringProperty(
- name="Output Timestamp",
- description="Timestamp for generation output directory",
- default=""
- )
-
- bpy.types.Scene.camera_prompts = bpy.props.CollectionProperty(
- type=CameraPromptItem,
- name="Camera Prompts",
- description="Stores viewpoint descriptions for each camera"
- ) # type: ignore
-
- bpy.types.Scene.use_camera_prompts = bpy.props.BoolProperty(
- name="Use Camera Prompts",
- description="Use camera prompts for generating images",
- default=True,
- update=update_parameters
- )
-
- bpy.types.Scene.sg_camera_order = bpy.props.CollectionProperty(
- type=CameraOrderItem,
- name="Camera Generation Order",
- description="Defines the order in which cameras are processed during generation"
- ) # type: ignore
- bpy.types.Scene.sg_camera_order_index = bpy.props.IntProperty(
- name="Active Camera Order Index",
- default=0
- )
- bpy.types.Scene.sg_use_custom_camera_order = bpy.props.BoolProperty(
- name="Use Custom Camera Order",
- description="When enabled, generation uses the custom camera order list instead of alphabetical sorting",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.show_core_settings = bpy.props.BoolProperty(
- name="Core Generation Settings",
- description="Parameters used for the image generation process. Also includes LoRAs for faster generation.",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.show_lora_settings = bpy.props.BoolProperty(
- name="LoRA Settings",
- description="Settings for custom LoRA management.",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.show_camera_options = bpy.props.BoolProperty(
- name="Camera Settings",
- description="Camera prompt and generation order settings.",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.show_scene_understanding_settings = bpy.props.BoolProperty(
- name="Viewpoint Blending Settings",
- description="Settings for how the addon blends different viewpoints together.",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.show_output_material_settings = bpy.props.BoolProperty(
- name="Output & Material Settings",
- description="Settings for output characteristics and material handling, including texture processing and final image resolution.",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.show_image_guidance_settings = bpy.props.BoolProperty(
- name="Image Guidance (IPAdapter & ControlNet)",
- description="Configuration for advanced image guidance techniques, allowing more precise control via reference images or structural inputs.",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.show_masking_inpainting_settings = bpy.props.BoolProperty(
- name="Inpainting Options",
- description="Parameters for inpainting and mask manipulation to refine specific image areas. (Visible for UV Inpaint & Sequential modes).",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.show_mode_specific_settings = bpy.props.BoolProperty(
- name="Generation Mode Specifics",
- description="Parameters exclusively available for the selected Generation Mode, allowing tailored control over mode-dependent behaviors.",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.generation_mode = bpy.props.EnumProperty(
- name="Generation Mode",
- description="Controls the generation behavior",
- items=[
- ('standard', 'Standard', 'Standard generation process'),
- ('regenerate_selected', 'Regenerate Selected', 'Regenerate only specific viewpoints, keeping the rest from the previous run'),
- ('project_only', 'Project Only', 'Only project existing textures onto the model without generating new ones')
- ],
- default='standard',
- update=update_parameters
- )
-
- bpy.types.Scene.early_priority_strength = bpy.props.FloatProperty(
- name="Prioritize Initial Views",
- description="""Strength of the priority applied to initial views. Higher values will make the earlier cameras more important than the later ones. Every view will be prioritized over the next one.
- - Very high values may cause various artifacts.""",
- default=0.5,
- min=0.0,
- max=1.0,
- update=update_parameters
- )
-
- bpy.types.Scene.early_priority = bpy.props.BoolProperty(
- name="Priority Strength",
- description="""Enable blending priority for earlier cameras.
- - This may prevent artifacts caused by later cameras overwriting earlier ones.
- - You will have to place the important cameras first.""",
- default=False,
- update=update_parameters
- )
-
- bpy.types.Scene.texture_objects = bpy.props.EnumProperty(
- name="Objects to Texture",
- description="Select the objects to texture",
- items=[
- ('all', 'All Visible', 'Texture all visible objects in the scene'),
- ('selected', 'Selected', 'Texture only selected objects'),
- ],
- default='all',
- update=update_parameters
- )
-
- bpy.types.Scene.use_flux_lora = bpy.props.BoolProperty(
- name="Use FLUX Depth LoRA",
- description="Use FLUX.1-Depth-dev LoRA for depth conditioning instead of ControlNet. This disables all ControlNet units.",
- default=True,
- update=update_parameters
- )
-
-
- # IPADAPTER parameters
-
- bpy.types.Scene.controlnet_units = bpy.props.CollectionProperty(type=ControlNetUnit)
- bpy.types.Scene.lora_units = bpy.props.CollectionProperty(type=LoRAUnit)
- bpy.types.Scene.controlnet_units_index = bpy.props.IntProperty(default=0)
- bpy.types.Scene.lora_units_index = bpy.props.IntProperty(default=0)
- bpy.app.handlers.load_post.append(load_handler)
- bpy.app.handlers.load_post.append(_sg_queue_load_handler)
-
- # --- TRELLIS.2 Properties ---
- bpy.types.Scene.trellis2_available = bpy.props.BoolProperty(
- name="TRELLIS.2 Available",
- description="Whether TRELLIS.2 nodes are available on the ComfyUI server (auto-detected)",
- default=False
- )
- bpy.types.Scene.pbr_nodes_available = bpy.props.BoolProperty(
- name="PBR Nodes Available",
- description="Whether PBR decomposition nodes (Marigold IID + StableDelight) are available on the ComfyUI server (auto-detected)",
- default=False
- )
- bpy.types.Scene.show_trellis2_params = bpy.props.BoolProperty(
- name="Show TRELLIS.2 Section",
- description="Toggle TRELLIS.2 Image-to-3D section",
- default=False
- )
- bpy.types.Scene.show_trellis2_advanced = bpy.props.BoolProperty(
- name="Show TRELLIS.2 Settings",
- description="Toggle TRELLIS.2 advanced settings",
- default=False
- )
- bpy.types.Scene.show_trellis2_mesh_settings = bpy.props.BoolProperty(
- name="Show Mesh Generation Settings",
- description="Toggle TRELLIS.2 mesh generation advanced settings",
- default=False
- )
- bpy.types.Scene.show_trellis2_texture_settings = bpy.props.BoolProperty(
- name="Show Texture Settings",
- description="Toggle TRELLIS.2 native texture settings",
- default=False
- )
- bpy.types.Scene.show_trellis2_camera_settings = bpy.props.BoolProperty(
- name="Show Camera Placement Settings",
- description="Toggle TRELLIS.2 camera placement settings",
- default=False
- )
- bpy.types.Scene.trellis2_last_input_image = bpy.props.StringProperty(
- name="TRELLIS.2 Last Input Image",
- description="Path to the image most recently used as TRELLIS.2 input (set automatically after generation)",
- subtype='FILE_PATH',
- default=""
- )
- bpy.types.Scene.qwen_use_trellis2_style = bpy.props.BoolProperty(
- name="Use TRELLIS.2 Input as Style",
- description="Use the TRELLIS.2 input image as the Qwen style reference",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.qwen_trellis2_style_initial_only = bpy.props.BoolProperty(
- name="TRELLIS.2 Style for Initial Only",
- description="Use the TRELLIS.2 input image only for the first image, then fall back to sequential style for subsequent images",
- default=False,
- update=update_parameters
- )
- # 3-tier progress pipeline flags (written by Trellis2Generate, read by UI)
- bpy.types.Scene.trellis2_pipeline_active = bpy.props.BoolProperty(
- name="TRELLIS.2 Pipeline Active",
- description="True while the TRELLIS.2 pipeline (incl. texturing) is running",
- default=False
- )
- bpy.types.Scene.trellis2_pipeline_phase_start_pct = bpy.props.FloatProperty(
- name="Phase Start %",
- description="Overall-% at which the current texturing phase begins",
- default=0.0, min=0.0, max=100.0
- )
- bpy.types.Scene.trellis2_pipeline_total_phases = bpy.props.IntProperty(
- name="Total Phases",
- description="Total number of phases in the TRELLIS.2 pipeline",
- default=3, min=1, max=3
- )
- bpy.types.Scene.trellis2_input_image = bpy.props.StringProperty(
- name="Input Image",
- description="Path to the reference image for 3D mesh generation",
- subtype='FILE_PATH',
- default=""
- )
- bpy.types.Scene.trellis2_resolution = bpy.props.EnumProperty(
- name="Resolution",
- description="Model resolution for generation. Higher values use more VRAM",
- items=[
- ('512', '512', 'Low resolution, fast, less VRAM'),
- ('1024', '1024', 'Direct 1024 generation with higher sparse structure resolution'),
- ('1024_cascade', '1024 Cascade', 'Medium resolution with cascade (recommended)'),
- ('1536_cascade', '1536 Cascade', 'High resolution with cascade, most VRAM'),
- ],
- default='1024_cascade',
- update=update_parameters
- )
- bpy.types.Scene.trellis2_vram_mode = bpy.props.EnumProperty(
- name="VRAM Mode",
- description="Controls model offloading strategy to manage VRAM usage",
- items=[
- ('keep_loaded', 'Keep Loaded', 'Keep all models in VRAM (fastest, ~12GB VRAM)'),
- ('disk_offload', 'Disk Offload', 'Load models on demand from disk (recommended for <=16GB VRAM)'),
- ],
- default='disk_offload',
- update=update_parameters
- )
- bpy.types.Scene.trellis2_attn_backend = bpy.props.EnumProperty(
- name="Attention Backend",
- description="Attention implementation to use. flash_attn is fastest but requires CUDA",
- items=[
- ('flash_attn', 'Flash Attention', 'Fastest (requires flash-attn package)'),
- ('xformers', 'xFormers', 'Fast (requires xformers package)'),
- ('sdpa', 'SDPA', 'PyTorch native, always available'),
- ('sageattn', 'SageAttention', 'SageAttention backend (requires sageattn package)'),
- ],
- default='flash_attn',
- update=update_parameters
- )
- bpy.types.Scene.trellis2_seed = bpy.props.IntProperty(
- name="Seed",
- description="Random seed for generation (0 = random)",
- default=0,
- min=0,
- max=2147483647
- )
- bpy.types.Scene.trellis2_ss_guidance = bpy.props.FloatProperty(
- name="SS Guidance",
- description="Sparse structure CFG scale. Higher = stronger adherence to input image",
- default=7.5,
- min=1.0,
- max=20.0,
- step=10,
- update=update_parameters
- )
- bpy.types.Scene.trellis2_ss_steps = bpy.props.IntProperty(
- name="SS Steps",
- description="Sparse structure sampling steps. More steps = better quality but slower",
- default=12,
- min=1,
- max=50,
- update=update_parameters
- )
- bpy.types.Scene.trellis2_shape_guidance = bpy.props.FloatProperty(
- name="Shape Guidance",
- description="Shape CFG scale. Higher = stronger adherence to input image",
- default=7.5,
- min=1.0,
- max=20.0,
- step=10,
- update=update_parameters
- )
- bpy.types.Scene.trellis2_shape_steps = bpy.props.IntProperty(
- name="Shape Steps",
- description="Shape sampling steps. More steps = better quality but slower",
- default=12,
- min=1,
- max=50,
- update=update_parameters
- )
- bpy.types.Scene.trellis2_tex_guidance = bpy.props.FloatProperty(
- name="Texture Guidance",
- description="Texture CFG scale. Higher = stronger adherence to input image",
- default=7.5,
- min=1.0,
- max=20.0,
- step=10,
- update=update_parameters
- )
- bpy.types.Scene.trellis2_tex_steps = bpy.props.IntProperty(
- name="Texture Steps",
- description="Texture sampling steps. More steps = better quality but slower",
- default=12,
- min=1,
- max=50,
- update=update_parameters
- )
- bpy.types.Scene.trellis2_max_tokens = bpy.props.IntProperty(
- name="Max Tokens",
- description="Max sparse-voxel tokens during cascade upsampling (only affects cascade modes). "
- "Higher = more detail but more VRAM. "
- "Increase to 49152 for maximum detail if VRAM allows",
- default=32768,
- min=16384,
- max=65536,
- step=4096,
- update=update_parameters
- )
- bpy.types.Scene.trellis2_texture_size = bpy.props.IntProperty(
- name="Texture Size",
- description="Resolution of the UV-baked texture in pixels. Higher values reduce aliasing at UV seams. "
- "The native voxel grid is 1024, so values above 1024 interpolate existing data",
- default=4096,
- min=512,
- max=8192,
- step=512,
- update=update_parameters
- )
- bpy.types.Scene.trellis2_decimation = bpy.props.IntProperty(
- name="Decimation Target",
- description="Target polygon count for mesh simplification. Lower = simpler mesh",
- default=100000,
- min=1000,
- max=5000000,
- step=10000,
- update=update_parameters
- )
- bpy.types.Scene.trellis2_remesh = bpy.props.BoolProperty(
- name="Remesh",
- description="Apply remeshing for cleaner topology",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.trellis2_post_processing_enabled = bpy.props.BoolProperty(
- name="Post-Processing",
- description="Run ComfyUI-side mesh post-processing (decimation + remeshing). "
- "Disable to import the raw mesh for manual retopology",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.trellis2_auto_lighting = bpy.props.BoolProperty(
- name="Auto Studio Lighting",
- description="Create a three-point studio lighting rig (key, fill, rim) after import "
- "to showcase PBR materials",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.trellis2_skip_texture = bpy.props.BoolProperty(
- name="Skip Texture",
- description="Export shape-only mesh (no PBR textures). Much faster and uses less VRAM",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.trellis2_bg_removal = bpy.props.EnumProperty(
- name="Background Removal",
- description="Method for removing the image background before TRELLIS.2 processing",
- items=[
- ('auto', 'Auto (BiRefNet)', 'Automatically remove background using BiRefNet model'),
- ('skip', 'Skip (Use Alpha)', 'Skip background removal — use the input image\'s alpha channel as mask. '
- 'Use this when your image already has a transparent background'),
- ],
- default='auto',
- update=update_parameters
- )
- bpy.types.Scene.trellis2_background_color = bpy.props.EnumProperty(
- name="Background Color",
- description="Background color for image conditioning",
- items=[
- ('black', 'Black', 'Black background (default)'),
- ('gray', 'Gray', 'Gray background'),
- ('white', 'White', 'White background'),
- ],
- default='black',
- update=update_parameters
- )
-
- # ── PBR Decomposition (Marigold IID) ──────────────────────────────
- bpy.types.Scene.pbr_decomposition = bpy.props.BoolProperty(
- name="PBR Decomposition",
- description="Run Marigold decomposition on each generated image to "
- "produce PBR material maps (albedo, roughness, metallic, normal, depth)",
- default=False,
- update=update_parameters
- )
-
- # ── Albedo source ─────────────────────────────────────────────────
- bpy.types.Scene.pbr_albedo_source = bpy.props.EnumProperty(
- name="Albedo Source",
- description="Model to use for extracting the Base Color / albedo map",
- items=[
- ('marigold', "Marigold IID (Flat Albedo)",
- "True albedo from Marigold IID-Appearance — removes all lighting "
- "but may lose texture detail"),
- ('delight', "StableDelight (Delighted)",
- "Specular-removed image via StableDelight — preserves diffuse "
- "shading and texture detail, only strips highlights"),
- ('lighting', "Marigold IID-Lighting (Vibrant)",
- "Albedo from Marigold IID-Lighting — tends to produce more "
- "vibrant, saturated colours than IID-Appearance"),
- ],
- default='marigold',
- update=update_parameters,
- )
-
- # ── Per-map enable toggles ────────────────────────────────────────
- bpy.types.Scene.pbr_map_albedo = bpy.props.BoolProperty(
- name="Albedo",
- description="Extract albedo (diffuse colour without lighting) and use it as Base Color",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.pbr_map_roughness = bpy.props.BoolProperty(
- name="Roughness",
- description="Extract roughness map (0 = mirror, 1 = rough)",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.pbr_map_metallic = bpy.props.BoolProperty(
- name="Metallic",
- description="Extract metallic map (0 = dielectric, 1 = metal)",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.pbr_map_normal = bpy.props.BoolProperty(
- name="Normal",
- description="Extract surface normal map for detail and bump. "
- "Warning: may cause triangle artifacts on voxel-remeshed geometry",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.pbr_normal_strength = bpy.props.FloatProperty(
- name="Normal Strength",
- description="How strongly the normal map perturbs the surface shading. "
- "Lower values reduce triangle artifacts on poor topology",
- default=1.0,
- min=0.0,
- max=2.0,
- step=0.05,
- update=update_parameters
- )
- bpy.types.Scene.pbr_delight_strength = bpy.props.FloatProperty(
- name="Delight Strength",
- description="How strongly StableDelight removes specular reflections. "
- "Lower values preserve more original texture detail. "
- "1.0 = full delighting, 0.5 = subtle",
- default=1.0,
- min=0.01,
- max=5.0,
- step=0.1,
- update=update_parameters
- )
- bpy.types.Scene.pbr_map_height = bpy.props.BoolProperty(
- name="Height",
- description="Extract height/displacement map via Marigold depth estimation",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.pbr_height_scale = bpy.props.FloatProperty(
- name="Height Scale",
- description="Displacement scale for the height map. "
- "Higher values produce more pronounced surface displacement",
- default=0.1,
- min=0.001,
- max=2.0,
- step=0.01,
- precision=3,
- update=update_parameters
- )
- bpy.types.Scene.pbr_map_ao = bpy.props.BoolProperty(
- name="AO",
- description="Bake an Ambient Occlusion map from mesh geometry "
- "(uses Blender's built-in bake, no ML required)",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.pbr_ao_samples = bpy.props.IntProperty(
- name="AO Samples",
- description="Number of samples for AO baking. "
- "Higher = cleaner but slower",
- default=16,
- min=1,
- max=128,
- update=update_parameters
- )
- bpy.types.Scene.pbr_ao_distance = bpy.props.FloatProperty(
- name="AO Distance",
- description="Maximum ray distance for AO baking. "
- "0 = scene size (automatic)",
- default=0.0,
- min=0.0,
- max=100.0,
- step=0.1,
- update=update_parameters
- )
- bpy.types.Scene.pbr_map_emission = bpy.props.BoolProperty(
- name="Emission",
- description="Extract an emission map (experimental). "
- "Identifies self-illuminating regions in the generated texture",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.pbr_emission_method = bpy.props.EnumProperty(
- name="Emission Method",
- description="Algorithm used to detect emissive regions",
- items=[
- ('residual', "IID-Lighting Residual",
- "Run Marigold IID-Lighting and extract the residual "
- "(image − albedo × shading). Most accurate but requires "
- "an additional model pass"),
- ('hsv', "HSV Threshold",
- "Isolate pixels with high saturation + high value in HSV "
- "space. Fast, zero model cost, best for neon/sci-fi styles"),
- ],
- default='residual',
- update=update_parameters
- )
- bpy.types.Scene.pbr_emission_threshold = bpy.props.FloatProperty(
- name="Emission Threshold",
- description="Minimum value to consider as emission. "
- "In Residual mode, fades out low-luminance residuals. "
- "In HSV mode, zeroes the blurred mask below this cutoff. "
- "Higher = fewer false positives, lower = catches subtle glow",
- default=0.2,
- min=0.0,
- max=1.0,
- step=0.05,
- precision=2,
- update=update_parameters
- )
- bpy.types.Scene.pbr_emission_saturation_min = bpy.props.FloatProperty(
- name="Saturation Min",
- description="(HSV method) Minimum saturation for a pixel to be "
- "considered emissive. Glowing objects retain colour "
- "intensity while lit surfaces wash out",
- default=0.5,
- min=0.0,
- max=1.0,
- step=0.05,
- precision=2,
- update=update_parameters
- )
- bpy.types.Scene.pbr_emission_value_min = bpy.props.FloatProperty(
- name="Value Min",
- description="(HSV method) Minimum value/brightness for a pixel "
- "to be considered emissive",
- default=0.85,
- min=0.0,
- max=1.0,
- step=0.05,
- precision=2,
- update=update_parameters
- )
- bpy.types.Scene.pbr_emission_bloom = bpy.props.FloatProperty(
- name="Bloom Radius",
- description="(HSV method) Gaussian blur radius applied to the "
- "emission mask to simulate bloom / glow bleed. "
- "0 = sharp mask, higher = more bloom",
- default=5.0,
- min=0.0,
- max=50.0,
- step=1.0,
- precision=1,
- update=update_parameters
- )
- bpy.types.Scene.pbr_emission_strength = bpy.props.FloatProperty(
- name="Emission Strength",
- description="Strength of the emission channel in the Principled BSDF",
- default=2.5,
- min=0.0,
- max=100.0,
- step=0.5,
- precision=1,
- update=update_parameters
- )
- bpy.types.Scene.pbr_use_native_resolution = bpy.props.BoolProperty(
- name="Use Native Resolution",
- description="Process PBR maps at the image's native resolution (longest "
- "edge, rounded to 64px). Produces sharper results but uses "
- "more VRAM. When disabled, the fixed Processing Resolution is used",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.pbr_tiling = bpy.props.EnumProperty(
- name="Tiling",
- description="Tile-based super-resolution for PBR maps. Each tile "
- "is upscaled to the full image resolution before "
- "processing, producing N\u00b2\u00d7 the effective detail",
- items=[
- ('off', "Off", "No tiling — process the full image in one pass"),
- ('selective', "Selective", "Tile albedo only (StableDelight and/or "
- "IID albedo). Normals, roughness, metallic "
- "and height are processed normally"),
- ('all', "All", "Tile every PBR model including normals and height"),
- ('custom', "Custom", "Choose which maps to tile individually. "
- "Models are run as efficiently as possible"),
- ],
- default='selective',
- update=update_parameters
- )
- bpy.types.Scene.pbr_tile_albedo = bpy.props.BoolProperty(
- name="Tile Albedo",
- description="Tile the albedo map for higher detail",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.pbr_tile_material = bpy.props.BoolProperty(
- name="Tile Material",
- description="Tile the roughness and metallic maps (both come from "
- "the same IID-Appearance material output)",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.pbr_tile_normal = bpy.props.BoolProperty(
- name="Tile Normal",
- description="Tile the normal map for higher detail",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.pbr_tile_height = bpy.props.BoolProperty(
- name="Tile Height",
- description="Tile the height/displacement map for higher detail",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.pbr_tile_emission = bpy.props.BoolProperty(
- name="Tile Emission",
- description="Tile the IID-Lighting residual used for emission "
- "(only applies to the Residual emission method)",
- default=False,
- update=update_parameters
- )
- bpy.types.Scene.pbr_tile_grid = bpy.props.IntProperty(
- name="Tile Grid",
- description="N\u00d7N grid size for tiling. 2 = 4 tiles (4\u00d7 detail), "
- "3 = 9 tiles (9\u00d7 detail), 4 = 16 tiles (16\u00d7 detail). "
- "Processing time scales with N\u00b2",
- default=2,
- min=2,
- max=4,
- update=update_parameters
- )
- bpy.types.Scene.pbr_tile_superres = bpy.props.BoolProperty(
- name="Super Resolution",
- description="When enabled, the stitched PBR maps are kept at the "
- "upscaled tile resolution (~N\u00d7 the original image size). "
- "When disabled (default), tiles are scaled back to the "
- "original image resolution — still higher detail from "
- "tiled processing, but matching the source texture size",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.pbr_processing_resolution = bpy.props.IntProperty(
- name="Processing Resolution",
- description="Internal processing resolution for the Marigold model. "
- "Output is upscaled back to the original resolution. "
- "768 is the default for the model's training resolution",
- default=768,
- min=256,
- max=2048,
- step=64,
- update=update_parameters
- )
- bpy.types.Scene.pbr_denoise_steps = bpy.props.IntProperty(
- name="Denoise Steps",
- description="Number of denoising steps. "
- "More steps = better quality but slower. 4 is a good balance",
- default=4,
- min=1,
- max=50,
- update=update_parameters
- )
- bpy.types.Scene.pbr_ensemble_size = bpy.props.IntProperty(
- name="Ensemble Size",
- description="Number of ensemble predictions to average. "
- "Higher = more stable results but linearly slower. "
- "1 is usually sufficient",
- default=1,
- min=1,
- max=10,
- update=update_parameters
- )
- bpy.types.Scene.pbr_albedo_auto_saturation = bpy.props.BoolProperty(
- name="Correct Albedo Saturation",
- description="Automatically correct albedo saturation by comparing the PBR albedo "
- "against the original rendered image and boosting to match. "
- "Averaged across all cameras for uniform results by default. "
- "Recommended for Marigold IID (Flat Albedo) which tends to desaturate. "
- "Usually not needed for StableDelight or IID-Lighting",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.pbr_albedo_saturation_mode = bpy.props.EnumProperty(
- name="Saturation Mode",
- description="How to compute the albedo saturation correction ratio. "
- "Mean averages all camera ratios (sensitive to outliers). "
- "Median takes the middle value (robust to outliers). "
- "Per Camera applies an individual ratio to each camera",
- items=[
- ('MEDIAN', "Median", "Use the median ratio across cameras (recommended, outlier-resistant)"),
- ('MEAN', "Mean", "Use the arithmetic mean of all camera ratios"),
- ('PER_CAMERA', "Per Camera", "Compute and apply an individual ratio for each camera"),
- ],
- default='MEDIAN',
- update=update_parameters
- )
- bpy.types.Scene.pbr_replace_color_with_albedo = bpy.props.BoolProperty(
- name="Use Albedo as Base Color",
- description="Replace the projected colour texture with the albedo map. "
- "This effectively delights the texture",
- default=True,
- update=update_parameters
- )
- bpy.types.Scene.pbr_auto_lighting = bpy.props.BoolProperty(
- name="Studio Lighting",
- description="Create a three-point studio lighting rig (key, fill, rim) "
- "after PBR projection to showcase PBR materials",
- default=False,
- update=update_parameters
- )
-
-def unregister():
- """
- Unregisters the addon.
- :return: None
- """
- # Ensure properties added to preferences are deleted
- if hasattr(bpy.types.Scene, 'controlnet_model_mappings'): # Check if added to Scene mistakenly
- del bpy.types.Scene.controlnet_model_mappings
- if hasattr(bpy.types.Scene, 'controlnet_mapping_index'):
- del bpy.types.Scene.controlnet_mapping_index
-
- del bpy.types.Scene.use_flux_lora
- del bpy.types.Scene.comfyui_prompt
- del bpy.types.Scene.use_separate_texture_prompt
- del bpy.types.Scene.texture_prompt
- del bpy.types.Scene.comfyui_negative_prompt
- del bpy.types.Scene.model_name
- del bpy.types.Scene.sg_model_name_backup
- del bpy.types.Scene.seed
- del bpy.types.Scene.control_after_generate
- del bpy.types.Scene.steps
- del bpy.types.Scene.cfg
- del bpy.types.Scene.sampler
- del bpy.types.Scene.scheduler
- del bpy.types.Scene.show_advanced_params
- del bpy.types.Scene.show_generation_params
- del bpy.types.Scene.auto_rescale
- del bpy.types.Scene.qwen_rescale_alignment
- del bpy.types.Scene.auto_rescale_target_mp
- del bpy.types.Scene.generation_method
- del bpy.types.Scene.use_ipadapter
- del bpy.types.Scene.refine_images
- del bpy.types.Scene.refine_steps
- del bpy.types.Scene.refine_sampler
- del bpy.types.Scene.refine_scheduler
- del bpy.types.Scene.denoise
- del bpy.types.Scene.refine_cfg
- del bpy.types.Scene.refine_prompt
- del bpy.types.Scene.refine_upscale_method
- del bpy.types.Scene.generation_status
- del bpy.types.Scene.sg_last_gen_error
- del bpy.types.Scene.generation_progress
- del bpy.types.Scene.overwrite_material
- del bpy.types.Scene.bake_visibility_weights
- del bpy.types.Scene.discard_factor
- del bpy.types.Scene.discard_factor_generation_only
- del bpy.types.Scene.discard_factor_after_generation
- del bpy.types.Scene.weight_exponent
- del bpy.types.Scene.weight_exponent_generation_only
- del bpy.types.Scene.weight_exponent_after_generation
- del bpy.types.Scene.allow_modify_existing_textures
- del bpy.types.Scene.ask_object_prompts
- del bpy.types.Scene.fallback_color
- del bpy.types.Scene.controlnet_units
- del bpy.types.Scene.controlnet_units_index
- del bpy.types.Scene.lora_units
- del bpy.types.Scene.lora_units_index
- del bpy.types.Scene.weight_exponent_mask
- del bpy.types.Scene.sequential_smooth
- del bpy.types.Scene.canny_threshold_low
- del bpy.types.Scene.canny_threshold_high
- del bpy.types.Scene.sequential_factor_smooth
- del bpy.types.Scene.sequential_factor_smooth_2
- del bpy.types.Scene.sequential_factor
- del bpy.types.Scene.grow_mask_by
- del bpy.types.Scene.mask_blocky
- del bpy.types.Scene.visibility_vignette
- del bpy.types.Scene.visibility_vignette_width
- del bpy.types.Scene.visibility_vignette_softness
- del bpy.types.Scene.sg_silhouette_margin
- del bpy.types.Scene.sg_silhouette_depth
- del bpy.types.Scene.sg_silhouette_rays
- del bpy.types.Scene.refine_edge_feather_projection
- del bpy.types.Scene.refine_edge_feather_width
- del bpy.types.Scene.refine_edge_feather_softness
- del bpy.types.Scene.differential_diffusion
- del bpy.types.Scene.differential_noise
- del bpy.types.Scene.blur_mask
- del bpy.types.Scene.blur_mask_radius
- del bpy.types.Scene.blur_mask_sigma
- del bpy.types.Scene.sequential_custom_camera_order
- del bpy.types.Scene.ipadapter_strength
- del bpy.types.Scene.ipadapter_start
- del bpy.types.Scene.ipadapter_end
- del bpy.types.Scene.sequential_ipadapter
- del bpy.types.Scene.sequential_ipadapter_mode
- del bpy.types.Scene.sequential_desaturate_factor
- del bpy.types.Scene.sequential_contrast_factor
- del bpy.types.Scene.sequential_ipadapter_regenerate
- del bpy.types.Scene.ipadapter_weight_type
- del bpy.types.Scene.clip_skip
- del bpy.types.Scene.stablegen_preset
- del bpy.types.Scene.model_architecture
- del bpy.types.Scene.architecture_mode
- del bpy.types.Scene.output_timestamp
- del bpy.types.Scene.camera_prompts
- del bpy.types.Scene.use_camera_prompts
- del bpy.types.Scene.sg_camera_order
- del bpy.types.Scene.sg_camera_order_index
- del bpy.types.Scene.sg_use_custom_camera_order
- del bpy.types.Scene.show_core_settings
- del bpy.types.Scene.show_lora_settings
- del bpy.types.Scene.show_camera_options
- del bpy.types.Scene.show_scene_understanding_settings
- del bpy.types.Scene.show_output_material_settings
- del bpy.types.Scene.show_image_guidance_settings
- del bpy.types.Scene.show_masking_inpainting_settings
- del bpy.types.Scene.show_mode_specific_settings
- del bpy.types.Scene.generation_mode
- del bpy.types.Scene.early_priority_strength
- del bpy.types.Scene.early_priority
- del bpy.types.Scene.texture_objects
- del bpy.types.Scene.qwen_guidance_map_type
- del bpy.types.Scene.qwen_voronoi_mode
- del bpy.types.Scene.qwen_context_render_mode
- del bpy.types.Scene.qwen_use_external_style_image
- del bpy.types.Scene.qwen_external_style_image
- del bpy.types.Scene.qwen_external_style_initial_only
- del bpy.types.Scene.qwen_use_custom_prompts
- del bpy.types.Scene.qwen_custom_prompt_initial
- 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
- del bpy.types.Scene.qwen_prompt_gray_background
- del bpy.types.Scene.qwen_timestep_zero_ref
-
- # --- PBR Decomposition Properties ---
- pbr_props = [
- 'pbr_decomposition', 'pbr_albedo_source',
- 'pbr_map_albedo', 'pbr_map_roughness', 'pbr_map_metallic',
- 'pbr_map_normal', 'pbr_map_height', 'pbr_height_scale',
- 'pbr_map_ao', 'pbr_ao_samples', 'pbr_ao_distance',
- 'pbr_map_emission', 'pbr_emission_method',
- 'pbr_emission_threshold', 'pbr_emission_saturation_min',
- 'pbr_emission_value_min', 'pbr_emission_bloom',
- 'pbr_emission_strength',
- 'pbr_normal_strength',
- 'pbr_delight_strength',
- 'pbr_use_native_resolution', 'pbr_tiling',
- 'pbr_tile_albedo', 'pbr_tile_material', 'pbr_tile_normal', 'pbr_tile_height',
- 'pbr_tile_emission', 'pbr_tile_grid', 'pbr_tile_superres',
- 'pbr_processing_resolution', 'pbr_denoise_steps', 'pbr_ensemble_size',
- 'pbr_albedo_auto_saturation', 'pbr_albedo_saturation_mode',
- 'pbr_replace_color_with_albedo', 'pbr_auto_lighting',
- 'pbr_model_variant', # legacy, kept for compat
- ]
- for prop in pbr_props:
- if hasattr(bpy.types.Scene, prop):
- delattr(bpy.types.Scene, prop)
-
- # --- TRELLIS.2 Properties ---
- trellis2_props = [
- 'trellis2_available', 'pbr_nodes_available', 'show_trellis2_params', 'show_trellis2_advanced',
- 'show_trellis2_mesh_settings', 'show_trellis2_texture_settings',
- 'show_trellis2_camera_settings',
- 'trellis2_last_input_image', 'qwen_use_trellis2_style', 'qwen_trellis2_style_initial_only',
- 'trellis2_pipeline_active', 'trellis2_pipeline_phase_start_pct',
- 'trellis2_pipeline_total_phases',
- 'trellis2_generate_from', 'trellis2_texture_mode', 'trellis2_initial_image_arch',
- 'trellis2_camera_count',
- 'trellis2_placement_mode', 'trellis2_auto_prompts',
- 'trellis2_exclude_bottom', 'trellis2_exclude_bottom_angle',
- 'trellis2_auto_aspect', 'trellis2_occlusion_mode',
- 'trellis2_consider_existing', 'trellis2_delete_cameras',
- 'trellis2_coverage_target',
- 'trellis2_max_auto_cameras', 'trellis2_fan_angle',
- 'trellis2_import_scale', 'trellis2_shade_mode',
- 'trellis2_clamp_elevation', 'trellis2_max_elevation', 'trellis2_min_elevation',
- 'trellis2_preview_gallery_enabled', 'trellis2_preview_gallery_count',
- 'trellis2_input_image', 'trellis2_resolution', 'trellis2_vram_mode',
- 'trellis2_attn_backend', 'trellis2_seed', 'trellis2_ss_guidance',
- 'trellis2_ss_steps', 'trellis2_shape_guidance', 'trellis2_shape_steps',
- 'trellis2_tex_guidance', 'trellis2_tex_steps', 'trellis2_max_tokens',
- 'trellis2_texture_size', 'trellis2_decimation', 'trellis2_remesh',
- 'trellis2_post_processing_enabled',
- 'trellis2_auto_lighting',
- 'trellis2_skip_texture', 'trellis2_bg_removal', 'trellis2_background_color',
- ]
- for prop in trellis2_props:
- if hasattr(bpy.types.Scene, prop):
- delattr(bpy.types.Scene, prop)
-
- # --- Scene Queue cleanup ---
- if hasattr(bpy.types.WindowManager, 'sg_scene_queue'):
- del bpy.types.WindowManager.sg_scene_queue
- if hasattr(bpy.types.WindowManager, 'sg_scene_queue_index'):
- del bpy.types.WindowManager.sg_scene_queue_index
- if hasattr(bpy.types.WindowManager, 'sg_show_queue'):
- del bpy.types.WindowManager.sg_show_queue
-
- for attr in (
- 'sg_queue_gif_export', 'sg_queue_gif_duration', 'sg_queue_gif_fps',
- 'sg_queue_gif_resolution', 'sg_queue_gif_samples', 'sg_queue_gif_engine',
- 'sg_queue_gif_interpolation', 'sg_queue_gif_use_hdri',
- 'sg_queue_gif_hdri_path', 'sg_queue_gif_hdri_strength',
- 'sg_queue_gif_hdri_rotation', 'sg_queue_gif_env_mode',
- 'sg_queue_gif_denoiser', 'sg_queue_gif_use_gpu',
- 'sg_queue_gif_also_no_pbr',
- ):
- if hasattr(bpy.types.WindowManager, attr):
- delattr(bpy.types.WindowManager, attr)
-
- for cls in reversed(classes):
- bpy.utils.unregister_class(cls)
-
- # Remove the load handler for default controlnet unit
- if load_handler in bpy.app.handlers.load_post:
- bpy.app.handlers.load_post.remove(load_handler)
- if _sg_queue_load_handler in bpy.app.handlers.load_post:
- bpy.app.handlers.load_post.remove(_sg_queue_load_handler)
-
-if __name__ == "__main__":
- register()
+"""StableGen addon - registration entry point.
+
+This module is intentionally thin: it assembles the *classes* list from
+submodules, registers them with Blender, and delegates property
+registration to :pymod:`core.properties`.
+"""
+
+import bpy # pylint: disable=import-error
+
+# -- UI / Operator classes ------------------------------------------------
+from .ui.presets import (
+ ApplyPreset, SavePreset, DeletePreset,
+ ResetQwenPrompt, SwitchToMeshGeneration,
+)
+from .ui.panel import StableGenPanel
+from .ui.queue import (
+ SG_UL_SceneQueueList, SceneQueueAdd, SceneQueueRemove, SceneQueueClear,
+ SceneQueueMoveUp, SceneQueueMoveDown, SceneQueueOpenResult,
+ SceneQueueInvalidate, SceneQueueProcess,
+)
+from .texturing.rendering import (
+ BakeTextures, SwitchMaterial,
+)
+from .texturing.orbit_export import ExportOrbitGIF
+from .texturing.game_export import ExportForGameEngine
+from .cameras import (
+ AddCameras, CloneCamera, MirrorCamera, ToggleCameraLabels,
+ CollectCameraPrompts,
+ CameraPromptItem, CameraOrderItem, SG_UL_CameraOrderList, SyncCameraOrder,
+ MoveCameraOrder, ApplyCameraOrderPreset,
+)
+from .debug_tools import debug_classes as _debug_classes
+from .utils import AddHDRI, ApplyModifiers, CurvesToMesh
+from .texturing.generator import (
+ ComfyUIGenerate, Reproject, Regenerate, MirrorReproject,
+)
+from .mesh_gen.trellis2 import Trellis2Generate
+
+# -- Core / UI sub-packages ----------------------------------------------
+from .core.preferences import (
+ CheckServerStatus, StableGenAddonPreferences,
+ ControlNetModelMappingItem, STABLEGEN_UL_ControlNetMappingList,
+ RefreshControlNetMappings,
+)
+from .core.load_handlers import load_handler
+from .core.properties import register_properties, unregister_properties
+from .ui.model_units import (
+ ControlNetUnit, LoRAUnit,
+ RefreshCheckpointList, RefreshLoRAList,
+ AddControlNetUnit, RemoveControlNetUnit, AddLoRAUnit, RemoveLoRAUnit,
+ update_model_list,
+)
+from .ui.queue import SceneQueueItem, _sg_queue_load_handler, _sg_queue_load
+
+bl_info = {
+ "name": "StableGen",
+ "category": "Object",
+ "author": "Ondrej Sakala",
+ "version": (0, 3, 0),
+ 'blender': (4, 2, 0)
+}
+
+# ---------------------------------------------------------------------------
+# Class registration list
+# ---------------------------------------------------------------------------
+# PropertyGroups first (they are referenced by CollectionProperty in
+# register_properties), then operators and preferences, then the panel last.
+classes = [
+ # PropertyGroups
+ CameraPromptItem,
+ CameraOrderItem,
+ ControlNetModelMappingItem,
+ ControlNetUnit,
+ LoRAUnit,
+ SceneQueueItem,
+ # Preferences
+ STABLEGEN_UL_ControlNetMappingList,
+ RefreshControlNetMappings,
+ StableGenAddonPreferences,
+ # Operators - server / model management
+ CheckServerStatus,
+ RefreshCheckpointList,
+ RefreshLoRAList,
+ AddControlNetUnit,
+ RemoveControlNetUnit,
+ AddLoRAUnit,
+ RemoveLoRAUnit,
+ # Operators - generation
+ ComfyUIGenerate,
+ Reproject,
+ Regenerate,
+ MirrorReproject,
+ Trellis2Generate,
+ # Operators - render tools
+ BakeTextures,
+ AddCameras,
+ CloneCamera,
+ MirrorCamera,
+ ToggleCameraLabels,
+ SwitchMaterial,
+ ExportOrbitGIF,
+ ExportForGameEngine,
+ CollectCameraPrompts,
+ SG_UL_CameraOrderList,
+ SyncCameraOrder,
+ MoveCameraOrder,
+ ApplyCameraOrderPreset,
+ # Operators - utilities
+ AddHDRI,
+ ApplyModifiers,
+ CurvesToMesh,
+ # Operators - presets / panel
+ ApplyPreset,
+ SavePreset,
+ DeletePreset,
+ ResetQwenPrompt,
+ SwitchToMeshGeneration,
+ # Queue operators
+ SG_UL_SceneQueueList,
+ SceneQueueAdd,
+ SceneQueueRemove,
+ SceneQueueClear,
+ SceneQueueMoveUp,
+ SceneQueueMoveDown,
+ SceneQueueOpenResult,
+ SceneQueueInvalidate,
+ SceneQueueProcess,
+ # Main panel (must be last – it uses all the above)
+ StableGenPanel,
+]
+# Append debug classes (defined in debug_tools.py)
+classes.extend(_debug_classes)
+
+
+def register():
+ for cls in classes:
+ bpy.utils.register_class(cls)
+
+ register_properties(
+ update_model_list=update_model_list,
+ ControlNetUnit=ControlNetUnit,
+ LoRAUnit=LoRAUnit,
+ SceneQueueItem=SceneQueueItem,
+ load_handler=load_handler,
+ _sg_queue_load_handler=_sg_queue_load_handler,
+ _sg_queue_load=_sg_queue_load,
+ )
+
+
+def unregister():
+ unregister_properties(
+ load_handler=load_handler,
+ _sg_queue_load_handler=_sg_queue_load_handler,
+ )
+
+ for cls in reversed(classes):
+ bpy.utils.unregister_class(cls)
+
+
+if __name__ == "__main__":
+ register()
\ No newline at end of file
diff --git a/stablegen/_generator_utils.py b/stablegen/_generator_utils.py
new file mode 100644
index 0000000..099fb9d
--- /dev/null
+++ b/stablegen/_generator_utils.py
@@ -0,0 +1,150 @@
+"""Shared utilities for generation operators (ComfyUIGenerate, Trellis2Generate)."""
+
+import os
+import bpy # pylint: disable=import-error
+import mathutils # pylint: disable=import-error
+import json
+import math
+import traceback
+import requests
+
+from .timeout_config import get_timeout
+
+def redraw_ui(context):
+ """Redraws the UI to reflect changes in the operator's progress and status."""
+ for area in context.screen.areas:
+ area.tag_redraw()
+
+
+def setup_studio_lighting(context, scale=1.0):
+ """Create a three-point studio lighting rig (key, fill, rim).
+
+ Re-usable from any generation mode (TRELLIS.2, PBR decomposition, etc.).
+
+ Args:
+ context: Blender context.
+ scale: Scene scale factor — lights are placed at ``scale * 2.5`` distance.
+ """
+ S = max(scale, 0.5)
+ dist = S * 2.5
+
+ light_defs = [
+ ("SG_Key", 200, 1.5 * S, (1.0, 0.96, 0.90), 45, 40),
+ ("SG_Fill", 80, 2.5 * S, (0.90, 0.94, 1.0), -60, 15),
+ ("SG_Rim", 120, 0.8 * S, (1.0, 1.0, 1.0), 170, 55),
+ ]
+
+ collection = context.collection
+ created = []
+
+ for name, power, size, color, az_deg, el_deg in light_defs:
+ old = bpy.data.objects.get(name)
+ if old:
+ bpy.data.objects.remove(old, do_unlink=True)
+
+ az = math.radians(az_deg)
+ el = math.radians(el_deg)
+ x = dist * math.cos(el) * math.sin(az)
+ y = -dist * math.cos(el) * math.cos(az)
+ z = dist * math.sin(el)
+
+ light_data = bpy.data.lights.new(name=name, type='AREA')
+ light_data.energy = power
+ light_data.size = size
+ light_data.color = color
+
+ light_obj = bpy.data.objects.new(name=name, object_data=light_data)
+ collection.objects.link(light_obj)
+
+ light_obj.location = (x, y, z)
+ direction = mathutils.Vector((0, 0, 0)) - mathutils.Vector((x, y, z))
+ rot = direction.to_track_quat('-Z', 'Y')
+ light_obj.rotation_euler = rot.to_euler()
+
+ created.append(light_obj)
+
+ print(f"[StableGen] Studio lighting created: {[o.name for o in created]}")
+ return created
+
+
+def _pbr_setup_studio_lights(context, to_texture):
+ """Calculate scene scale from target objects and set up studio lights."""
+ max_dim = 1.0
+ for obj in to_texture:
+ if hasattr(obj, 'dimensions'):
+ max_dim = max(max_dim, *obj.dimensions)
+ setup_studio_lighting(context, scale=max_dim)
+
+
+def upload_image_to_comfyui(server_address, image_path, image_type="input"):
+ """
+ Uploads an image file to the ComfyUI server's /upload/image endpoint.
+
+ Args:
+ server_address (str): The address:port of the ComfyUI server (e.g., "127.0.0.1:8188").
+ image_path (str): The local path to the image file to upload.
+ image_type (str): The type parameter for the upload (usually "input").
+
+ Returns:
+ dict: A dictionary containing the server's response (e.g., {'name': 'filename.png', 'subfolder': '', 'type': 'input'})
+ Returns None if the upload fails or file doesn't exist.
+ """
+ if not os.path.exists(image_path):
+ # This is expected for optional files, so don't log as an error
+ # print(f"Debug: Image file not found at {image_path}, cannot upload.")
+ return None
+ if not os.path.isfile(image_path):
+ print(f"[StableGen] Error: Path exists but is not a file: {image_path}")
+ return None
+
+ upload_url = f"http://{server_address}/upload/image"
+ print(f"[StableGen] Uploading {os.path.basename(image_path)} to {upload_url}...")
+
+ try:
+ with open(image_path, 'rb') as f:
+ # Determine mime type based on extension
+ mime_type = 'application/octet-stream' # Default fallback
+ if image_path.lower().endswith('.png'):
+ mime_type = 'image/png'
+ elif image_path.lower().endswith(('.jpg', '.jpeg')):
+ mime_type = 'image/jpeg'
+ elif image_path.lower().endswith('.webp'):
+ mime_type = 'image/webp'
+ # Add other types if needed (e.g., .bmp, .gif)
+
+ files = {'image': (os.path.basename(image_path), f, mime_type)}
+ # 'overwrite': 'true' prevents errors if the same filename is uploaded again
+ # useful for re-running generations with the same intermediate files.
+ data = {'overwrite': 'true', 'type': image_type}
+
+ # Increased timeout for potentially large images or slow networks
+ response = requests.post(upload_url, files=files, data=data, timeout=get_timeout('transfer'))
+ response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
+
+ response_data = response.json()
+ print(f"[StableGen] Upload successful for '{os.path.basename(image_path)}'. Server response: {response_data}")
+
+ # Crucial Validation
+ if 'name' not in response_data:
+ print(f"[StableGen] Error: ComfyUI upload response for {os.path.basename(image_path)} missing 'name'. Response: {response_data}")
+ return None
+ # End Validation
+
+ return response_data # Should contain 'name', often 'subfolder', 'type'
+
+ except requests.exceptions.Timeout:
+ print(f"[StableGen] Error: Timeout uploading image {os.path.basename(image_path)} to {upload_url}.")
+ except requests.exceptions.ConnectionError:
+ print(f"[StableGen] Error: Connection failed when uploading image {os.path.basename(image_path)} to {upload_url}. Is ComfyUI running and accessible?")
+ except requests.exceptions.HTTPError as e:
+ print(f"[StableGen] Error: HTTP Error {e.response.status_code} uploading image {os.path.basename(image_path)} to {upload_url}.")
+ print(f"[StableGen] Server response content: {e.response.text}") # Show response body on error
+ except requests.exceptions.RequestException as e:
+ print(f"[StableGen] Error uploading image {os.path.basename(image_path)} to {upload_url}: {e}")
+ except json.JSONDecodeError:
+ print(f"[StableGen] Error decoding ComfyUI response after uploading {os.path.basename(image_path)}. Response text: {response.text}")
+ except Exception as e:
+ print(f"[StableGen] An unexpected error occurred during image upload of {os.path.basename(image_path)}: {e}")
+ traceback.print_exc() # Print full traceback for unexpected errors
+
+ return None
\ No newline at end of file
diff --git a/stablegen/cameras/__init__.py b/stablegen/cameras/__init__.py
new file mode 100644
index 0000000..05bc781
--- /dev/null
+++ b/stablegen/cameras/__init__.py
@@ -0,0 +1,42 @@
+"""cameras – camera placement, overlays, ordering, and camera operators."""
+
+# -- geometry.py --------------------------------------------------------------
+from .geometry import ( # noqa: F401
+ _SGCameraResolution,
+ _get_camera_resolution,
+ _store_per_camera_resolution,
+)
+
+# -- overlays.py --------------------------------------------------------------
+from .overlays import ( # noqa: F401
+ _sg_ensure_crop_overlay,
+ _sg_remove_crop_overlay,
+ _sg_ensure_label_overlay,
+ _sg_remove_label_overlay,
+ _sg_hide_label_overlay,
+ _sg_restore_label_overlay,
+ _sg_restore_square_display,
+ _setup_square_camera_display,
+)
+
+# -- placement.py -------------------------------------------------------------
+from .placement import AddCameras # noqa: F401
+
+# -- operators.py -------------------------------------------------------------
+from .operators import ( # noqa: F401
+ switch_viewport_to_camera,
+ CloneCamera,
+ MirrorCamera,
+ ToggleCameraLabels,
+)
+
+# -- prompts.py ---------------------------------------------------------------
+from .prompts import ( # noqa: F401
+ CameraPromptItem,
+ CameraOrderItem,
+ SG_UL_CameraOrderList,
+ SyncCameraOrder,
+ MoveCameraOrder,
+ ApplyCameraOrderPreset,
+ CollectCameraPrompts,
+)
diff --git a/stablegen/cameras/geometry.py b/stablegen/cameras/geometry.py
new file mode 100644
index 0000000..26d6d47
--- /dev/null
+++ b/stablegen/cameras/geometry.py
@@ -0,0 +1,875 @@
+"""Camera math, BVH, coverage algorithms, and resolution helpers."""
+
+import math
+import bpy, bmesh # pylint: disable=import-error
+import numpy as np
+import mathutils
+from mathutils import Matrix
+
+def _existing_camera_directions(mesh_center):
+ """Return unit-direction vectors from *mesh_center* toward each existing
+ camera in the scene. Used by 'Consider existing cameras' to treat
+ pre-existing cameras as already-placed directions."""
+ dirs = []
+ center = np.array(mesh_center, dtype=float)
+ for obj in bpy.data.objects:
+ if obj.type == 'CAMERA':
+ pos = np.array(obj.location, dtype=float)
+ d = pos - center
+ norm = np.linalg.norm(d)
+ if norm > 1e-6:
+ dirs.append(d / norm)
+ return dirs
+
+
+def _filter_near_existing(directions, existing_dirs, min_angle_deg=30.0):
+ """Remove directions that are within *min_angle_deg* of any existing
+ camera direction. Both *directions* and *existing_dirs* should be
+ lists of unit-length numpy arrays."""
+ if not existing_dirs or not directions:
+ return directions
+ cos_thresh = math.cos(math.radians(min_angle_deg))
+ existing_np = np.array(existing_dirs) # (M, 3)
+ filtered = []
+ for d in directions:
+ d_np = np.asarray(d, dtype=float)
+ n = np.linalg.norm(d_np)
+ if n < 1e-12:
+ continue
+ d_unit = d_np / n
+ dots = existing_np @ d_unit # (M,)
+ if dots.max() < cos_thresh:
+ filtered.append(d)
+ return filtered
+
+def _fibonacci_sphere_points(n):
+ """Generate *n* approximately evenly-spaced unit vectors on a sphere
+ using a Fibonacci spiral. Returns list of (x, y, z) tuples."""
+ points = []
+ golden_ratio = (1 + math.sqrt(5)) / 2
+ for i in range(n):
+ theta = math.acos(1 - 2 * (i + 0.5) / n)
+ phi = 2 * math.pi * i / golden_ratio
+ x = math.sin(theta) * math.cos(phi)
+ y = math.sin(theta) * math.sin(phi)
+ z = math.cos(theta)
+ points.append((x, y, z))
+ return points
+
+
+def _gather_target_meshes(context):
+ """Return a list of mesh objects to cover.
+ - If any selected objects are meshes, use those.
+ - Otherwise, use ALL mesh objects in the scene.
+ """
+ selected = [o for o in context.selected_objects if o.type == 'MESH']
+ if selected:
+ return selected
+ return [o for o in context.scene.objects if o.type == 'MESH']
+
+
+def _get_mesh_face_data(objs):
+ """Return world-space face (normals, areas, centers) as numpy arrays.
+ *objs* can be a single object or a list of mesh objects."""
+ if not isinstance(objs, (list, tuple)):
+ objs = [objs]
+ all_normals, all_areas, all_centers = [], [], []
+ for obj in objs:
+ mesh = obj.data
+ mat = obj.matrix_world
+ rot = mat.to_3x3()
+ scale_det = abs(rot.determinant())
+ n = len(mesh.polygons)
+ normals = np.empty((n, 3))
+ areas = np.empty(n)
+ centers = np.empty((n, 3))
+ for idx, poly in enumerate(mesh.polygons):
+ wn = rot @ poly.normal
+ wn.normalize()
+ normals[idx] = (wn.x, wn.y, wn.z)
+ wc = mat @ poly.center
+ centers[idx] = (wc.x, wc.y, wc.z)
+ areas[idx] = poly.area * (scale_det ** 0.5)
+ all_normals.append(normals)
+ all_areas.append(areas)
+ all_centers.append(centers)
+ return np.vstack(all_normals), np.concatenate(all_areas), np.vstack(all_centers)
+
+
+def _filter_bottom_faces(normals, areas, centers, angle_rad):
+ """Remove faces whose normals point more than *angle_rad* below the
+ horizon (negative-Z). Returns filtered (normals, areas, centers).
+
+ *angle_rad* = 80° (≈1.40 rad) removes faces whose normal is > 80° below
+ horizontal, i.e. nearly straight down. The Z-component threshold is
+ ``-cos(angle_rad)``."""
+ threshold_z = -math.cos(angle_rad)
+ mask = normals[:, 2] >= threshold_z # keep faces *above* the threshold
+ return normals[mask], areas[mask], centers[mask]
+
+
+def _get_mesh_verts_world(objs):
+ """Return world-space vertex positions as (N, 3) numpy array.
+ *objs* can be a single object or a list of mesh objects."""
+ if not isinstance(objs, (list, tuple)):
+ objs = [objs]
+ parts = []
+ for obj in objs:
+ mesh = obj.data
+ mat = obj.matrix_world
+ n = len(mesh.vertices)
+ verts = np.empty((n, 3))
+ for i, v in enumerate(mesh.vertices):
+ wv = mat @ v.co
+ verts[i] = (wv.x, wv.y, wv.z)
+ parts.append(verts)
+ return np.vstack(parts)
+
+
+def _camera_basis(cam_dir_np):
+ """Build an orthonormal camera basis from a centre-to-camera direction.
+
+ Returns ``(right, up, d_unit)`` where
+ * *right* – camera local X (numpy unit vector)
+ * *up* – camera local Y (numpy unit vector)
+ * *d_unit* – normalised centre-to-camera direction (camera local Z)
+
+ When the direction is nearly vertical the world-up fallback switches
+ from Z to Y, giving a deterministic orientation that avoids
+ ``to_track_quat`` gimbal degeneracy.
+ """
+ d = cam_dir_np / np.linalg.norm(cam_dir_np)
+ forward = -d
+ world_up = np.array([0.0, 0.0, 1.0])
+ if abs(np.dot(d, world_up)) > 0.99:
+ world_up = np.array([0.0, 1.0, 0.0])
+ right = np.cross(forward, world_up)
+ right /= np.linalg.norm(right)
+ up = np.cross(right, forward)
+ up /= np.linalg.norm(up)
+ return right, up, d
+
+
+def _rotation_from_basis(right, up, d_unit):
+ """Build a Blender rotation (Euler) from camera basis vectors.
+
+ The resulting rotation aligns the camera's local axes:
+ * local X → *right*
+ * local Y → *up*
+ * local Z → *d_unit* (away from mesh; -Z is the look direction)
+ """
+ rot_mat = mathutils.Matrix((
+ (right[0], up[0], d_unit[0]),
+ (right[1], up[1], d_unit[1]),
+ (right[2], up[2], d_unit[2]),
+ ))
+ return rot_mat.to_euler()
+
+
+def _compute_silhouette_distance(verts_world, center_np, cam_dir_np, fov_x, fov_y, margin=0.10):
+ """Compute the optimal distance along *cam_dir_np* so every mesh vertex
+ fits inside the camera frame with *margin* breathing room.
+
+ Uses a perspective-correct formula so objects with depth along the
+ viewing direction are never clipped.
+
+ Returns ``(distance, aim_offset)`` where *aim_offset* is a 3-element numpy
+ vector that shifts from *center_np* to the visual center of the silhouette.
+ The caller should aim the camera at ``center_np + aim_offset`` and place
+ it at ``center_np + aim_offset + direction * distance``.
+ """
+ right, up, d = _camera_basis(cam_dir_np)
+ forward = -d
+
+ # Project every vertex onto the camera's right / up plane
+ rel = verts_world - center_np
+ proj_r = rel @ right
+ proj_u = rel @ up
+
+ r_min, r_max = float(proj_r.min()), float(proj_r.max())
+ u_min, u_max = float(proj_u.min()), float(proj_u.max())
+
+ # Visual centre of the silhouette (midpoint of extents)
+ mid_r = (r_max + r_min) / 2.0
+ mid_u = (u_max + u_min) / 2.0
+
+ # Aim offset: shifts from mesh center to silhouette visual centre
+ aim_offset = mid_r * right + mid_u * up
+
+ eff_fov_x = fov_x * (1.0 - margin)
+ eff_fov_y = fov_y * (1.0 - margin)
+
+ tan_hx = math.tan(eff_fov_x / 2) if eff_fov_x > 0.02 else 1e-6
+ tan_hy = math.tan(eff_fov_y / 2) if eff_fov_y > 0.02 else 1e-6
+
+ # Perspective-correct distance: for each vertex the minimum camera
+ # distance along the view direction is |lateral| / tan(half_fov) - depth
+ # where depth is signed distance from the aim point along the view ray.
+ aim_point = center_np + aim_offset
+ rel_aim = verts_world - aim_point
+ pr = rel_aim @ right
+ pu = rel_aim @ up
+ # Depth: positive = in front of aim point (toward camera)
+ pd = rel_aim @ d # dot with camera direction (away from mesh)
+
+ min_dist_r = np.abs(pr) / tan_hx + pd
+ min_dist_u = np.abs(pu) / tan_hy + pd
+
+ dist = max(float(min_dist_r.max()), float(min_dist_u.max()), 0.5)
+
+ # --- Refine aim_offset using perspective angular centre ---
+ # The orthographic midpoint doesn't account for perspective foreshortening.
+ # Compute where the visual centre actually is from the camera position and
+ # shift the aim to centre it, then recompute the distance.
+ cam_pos = aim_point + d * dist
+ rel_cam = verts_world - cam_pos
+ depth = -(rel_cam @ d) # positive = in front of camera
+ depth = np.maximum(depth, 0.001)
+ ang_r = np.arctan2(rel_cam @ right, depth)
+ ang_u = np.arctan2(rel_cam @ up, depth)
+ ang_mid_r = (float(ang_r.max()) + float(ang_r.min())) / 2.0
+ ang_mid_u = (float(ang_u.max()) + float(ang_u.min())) / 2.0
+ # Convert angular offset to world-space shift (at the computed distance)
+ aim_offset = aim_offset + dist * math.tan(ang_mid_r) * right + dist * math.tan(ang_mid_u) * up
+
+ # Recompute distance with refined aim
+ aim_point = center_np + aim_offset
+ rel_aim = verts_world - aim_point
+ pr = rel_aim @ right
+ pu = rel_aim @ up
+ pd = rel_aim @ d
+ min_dist_r = np.abs(pr) / tan_hx + pd
+ min_dist_u = np.abs(pu) / tan_hy + pd
+ dist = max(float(min_dist_r.max()), float(min_dist_u.max()), 0.5)
+
+ return dist, aim_offset
+
+
+def _kmeans_on_sphere(directions, weights, k, max_iter=50):
+ """Spherical K-means: cluster unit vectors weighted by area.
+ Returns (k, 3) numpy array of cluster-centre unit vectors."""
+ n_pts = len(directions)
+ if n_pts == 0 or k == 0:
+ return np.zeros((max(k, 1), 3))
+ k = min(k, n_pts)
+ rng = np.random.default_rng(42)
+ probs = weights / weights.sum()
+ indices = rng.choice(n_pts, size=k, replace=False, p=probs)
+ centers = directions[indices].copy()
+ for _ in range(max_iter):
+ dots = directions @ centers.T
+ labels = np.argmax(dots, axis=1)
+ new_centers = np.zeros_like(centers)
+ for j in range(k):
+ mask = labels == j
+ if mask.any():
+ ws = (directions[mask] * weights[mask, np.newaxis]).sum(axis=0)
+ nrm = np.linalg.norm(ws)
+ new_centers[j] = ws / nrm if nrm > 0 else centers[j]
+ else:
+ new_centers[j] = centers[j]
+ if np.allclose(centers, new_centers, atol=1e-6):
+ break
+ centers = new_centers
+ return centers
+
+
+def _compute_pca_axes(verts):
+ """Return the 3 principal axes of a (N, 3) vertex array (rows of a
+ 3x3 array, sorted by descending eigenvalue)."""
+ mean = verts.mean(axis=0)
+ centered = verts - mean
+ cov = np.cov(centered.T)
+ eigenvalues, eigenvectors = np.linalg.eigh(cov)
+ idx = np.argsort(eigenvalues)[::-1]
+ return eigenvectors[:, idx].T # rows = principal axes
+
+
+def _greedy_coverage_directions(normals, areas, max_cameras=12,
+ coverage_target=0.95, n_candidates=200,
+ existing_dirs=None):
+ """Greedy set-cover: iteratively pick the camera direction that adds the
+ most newly-visible surface area (back-face culling only, no occlusion,
+ for speed). Returns (selected_directions, final_coverage_fraction)."""
+ total_area = areas.sum()
+ if total_area <= 0:
+ return [], 0.0
+
+ candidates = np.array(_fibonacci_sphere_points(n_candidates))
+
+ # visibility[i, j] = True if face i faces toward candidate j
+ # cos(75°) ≈ 0.26 – ignore near-grazing faces that won't texture well
+ visibility = normals @ candidates.T > 0.26 # (n_faces, n_candidates)
+
+ covered = np.zeros(len(areas), dtype=bool)
+ # Pre-seed coverage from existing cameras
+ if existing_dirs:
+ for edir in existing_dirs:
+ covered |= normals @ np.asarray(edir, dtype=float) > 0.26
+ selected = []
+
+ for _ in range(max_cameras):
+ uncovered = ~covered
+ # Vectorised: for each candidate sum the area of faces that are
+ # both visible from it AND not yet covered
+ new_vis = visibility & uncovered[:, np.newaxis] # broadcast
+ new_areas = (new_vis * areas[:, np.newaxis]).sum(axis=0)
+
+ best_idx = int(np.argmax(new_areas))
+ if new_areas[best_idx] < total_area * 0.005: # < 0.5 % new coverage
+ break
+
+ selected.append(candidates[best_idx].copy())
+ covered |= visibility[:, best_idx]
+
+ coverage = float(areas[covered].sum() / total_area)
+ if coverage >= coverage_target:
+ break
+
+ final_coverage = float(areas[covered].sum() / total_area) if covered.any() else 0.0
+ return selected, final_coverage
+
+
+# ──────────────────────────────────────────────────────────────────────
+# BVH occlusion helpers
+# ──────────────────────────────────────────────────────────────────────
+
+def _build_bvh_trees(objs, depsgraph):
+ """Build a list of BVHTree objects (one per mesh) for raycasting.
+
+ Returns
+ -------
+ list[mathutils.bvhtree.BVHTree]
+ One BVH per object, in world space.
+ """
+ from mathutils.bvhtree import BVHTree
+ trees = []
+ for obj in objs:
+ bm = bmesh.new()
+ bm.from_object(obj, depsgraph)
+ bm.transform(obj.matrix_world)
+ tree = BVHTree.FromBMesh(bm)
+ bm.free()
+ trees.append(tree)
+ return trees
+
+
+def _ray_occluded(bvh_trees, origin, direction, max_dist):
+ """Return True if *any* BVH tree has a hit between *origin* and
+ *origin + direction * max_dist* (exclusive of the starting face).
+
+ *origin* should already be slightly offset along the face normal to avoid
+ self-intersection.
+ """
+ for tree in bvh_trees:
+ hit_loc, _normal, _index, _dist = tree.ray_cast(
+ mathutils.Vector(origin), mathutils.Vector(direction), max_dist)
+ if hit_loc is not None:
+ return True
+ return False
+
+
+def _greedy_select_from_visibility(vis, areas, max_cameras, coverage_target,
+ candidates, existing_dirs=None,
+ normals=None):
+ """Run greedy set-cover on a pre-computed visibility matrix.
+
+ Parameters
+ ----------
+ vis : ndarray (n_faces, n_candidates), bool
+ areas : ndarray (n_faces,)
+ max_cameras : int
+ coverage_target : float
+ candidates : ndarray (n_candidates, 3)
+ existing_dirs : list[ndarray] or None
+ Directions of existing cameras to pre-seed coverage.
+ normals : ndarray (n_faces, 3) or None
+ Face normals, required when *existing_dirs* is given.
+
+ Returns
+ -------
+ (selected_directions, final_coverage)
+ """
+ total_area = areas.sum()
+ if total_area <= 0:
+ return [], 0.0
+
+ covered = np.zeros(len(areas), dtype=bool)
+ # Pre-seed coverage from existing cameras
+ if existing_dirs and normals is not None:
+ for edir in existing_dirs:
+ covered |= normals @ np.asarray(edir, dtype=float) > 0.26
+ selected = []
+
+ for _ in range(max_cameras):
+ uncovered = ~covered
+ new_vis = vis & uncovered[:, np.newaxis]
+ new_areas = (new_vis * areas[:, np.newaxis]).sum(axis=0)
+
+ best_idx = int(np.argmax(new_areas))
+ if new_areas[best_idx] < total_area * 0.005:
+ break
+
+ selected.append(candidates[best_idx].copy())
+ covered |= vis[:, best_idx]
+
+ coverage = float(areas[covered].sum() / total_area)
+ if coverage >= coverage_target:
+ break
+
+ final_cov = float(areas[covered].sum() / total_area) if covered.any() else 0.0
+ return selected, final_cov
+
+
+# ──────────────────────────────────────────────────────────────────────
+# Occlusion generators (modal-friendly, yield progress)
+# ──────────────────────────────────────────────────────────────────────
+
+def _occ_filter_faces_generator(normals, areas, centers, bvh_trees,
+ n_candidates=200):
+ """Generator: determine which faces are visible from at least one
+ camera direction (exterior faces).
+
+ Yields progress floats in [0, 1]. Final result (via
+ ``StopIteration.value``) is a boolean mask ``(n_faces,)`` where True
+ means the face is visible from at least one direction.
+
+ Uses early-exit per face: as soon as one unoccluded direction is found,
+ the face is marked exterior and skipped for remaining candidates.
+ """
+ n_faces = len(normals)
+ candidates = np.array(_fibonacci_sphere_points(n_candidates))
+ backface_vis = normals @ candidates.T > 0.26 # (F, C)
+ exterior = np.zeros(n_faces, dtype=bool)
+ epsilon = 0.001
+ BATCH = 5
+
+ for j in range(n_candidates):
+ cam_dir = candidates[j]
+ for i in range(n_faces):
+ if exterior[i]:
+ continue # already proven exterior
+ if not backface_vis[i, j]:
+ continue
+ origin = centers[i] + normals[i] * epsilon
+ if not _ray_occluded(bvh_trees, origin, cam_dir, 1e6):
+ exterior[i] = True
+ if (j + 1) % BATCH == 0 or j == n_candidates - 1:
+ yield (j + 1) / n_candidates
+ # Early termination: all faces proven exterior
+ if exterior.all():
+ break
+
+ return exterior
+
+
+def _occ_vis_count_generator(normals, areas, centers, bvh_trees,
+ n_candidates=200):
+ """Generator: count how many candidate directions can see each face.
+
+ Like ``_occ_filter_faces_generator`` but returns per-face visibility
+ *counts* instead of a boolean mask. This enables continuous weighting
+ rather than binary keep/remove filtering.
+
+ Yields progress floats in [0, 1]. Final result (via
+ ``StopIteration.value``) is an int array ``(n_faces,)`` with the number
+ of unoccluded candidate directions per face.
+ """
+ n_faces = len(normals)
+ candidates = np.array(_fibonacci_sphere_points(n_candidates))
+ backface_vis = normals @ candidates.T > 0.26 # (F, C)
+ vis_count = np.zeros(n_faces, dtype=int)
+ epsilon = 0.001
+ BATCH = 5
+
+ for j in range(n_candidates):
+ cam_dir = candidates[j]
+ for i in range(n_faces):
+ if not backface_vis[i, j]:
+ continue
+ origin = centers[i] + normals[i] * epsilon
+ if not _ray_occluded(bvh_trees, origin, cam_dir, 1e6):
+ vis_count[i] += 1
+ if (j + 1) % BATCH == 0 or j == n_candidates - 1:
+ yield (j + 1) / n_candidates
+
+ return vis_count
+
+
+def _occ_fom_generator(normals, areas, centers, bvh_trees,
+ max_cameras, coverage_target, n_candidates=200,
+ existing_dirs=None):
+ """Generator: Full Occlusion Matrix approach.
+
+ Yields progress floats in [0, 1]. Final result is available via
+ ``StopIteration.value`` as ``(directions, coverage)``.
+ """
+ total_area = areas.sum()
+ if total_area <= 0:
+ yield 1.0
+ return [], 0.0
+
+ candidates = np.array(_fibonacci_sphere_points(n_candidates))
+ backface_vis = normals @ candidates.T > 0.26
+ n_faces = len(normals)
+ vis = np.zeros((n_faces, n_candidates), dtype=bool)
+ epsilon = 0.001
+ BATCH = 5
+
+ for j in range(n_candidates):
+ cam_dir = candidates[j]
+ for i in range(n_faces):
+ if not backface_vis[i, j]:
+ continue
+ origin = centers[i] + normals[i] * epsilon
+ if not _ray_occluded(bvh_trees, origin, cam_dir, 1e6):
+ vis[i, j] = True
+ if (j + 1) % BATCH == 0 or j == n_candidates - 1:
+ yield (j + 1) / n_candidates
+
+ return _greedy_select_from_visibility(
+ vis, areas, max_cameras, coverage_target, candidates,
+ existing_dirs=existing_dirs, normals=normals)
+
+
+def _occ_tpr_generator(normals, areas, centers, bvh_trees,
+ max_cameras, coverage_target, n_candidates=200,
+ existing_dirs=None):
+ """Generator: Two-Pass Refinement approach.
+
+ Yields progress floats in [0, 1]. Final result is available via
+ ``StopIteration.value`` as ``(directions, coverage)``.
+ """
+ total_area = areas.sum()
+ if total_area <= 0:
+ yield 1.0
+ return [], 0.0
+
+ candidates = np.array(_fibonacci_sphere_points(n_candidates))
+ backface_vis = normals @ candidates.T > 0.26
+
+ # ── Pass 1: fast greedy (back-face only, instant) ─────────────────
+ covered_bf = np.zeros(len(areas), dtype=bool)
+ # Pre-seed coverage from existing cameras
+ if existing_dirs:
+ for edir in existing_dirs:
+ covered_bf |= normals @ np.asarray(edir, dtype=float) > 0.26
+ selected_indices = []
+
+ for _ in range(max_cameras):
+ uncov = ~covered_bf
+ new_vis = backface_vis & uncov[:, np.newaxis]
+ new_areas_arr = (new_vis * areas[:, np.newaxis]).sum(axis=0)
+ best = int(np.argmax(new_areas_arr))
+ if new_areas_arr[best] < total_area * 0.005:
+ break
+ selected_indices.append(best)
+ covered_bf |= backface_vis[:, best]
+ if float(areas[covered_bf].sum() / total_area) >= coverage_target:
+ break
+
+ if not selected_indices:
+ yield 1.0
+ return [], 0.0
+
+ yield 0.0 # pass-1 done
+
+ # ── Phase 2a: BVH validate selected set (0 % → 30 %) ─────────────
+ epsilon = 0.001
+ true_covered = np.zeros(len(areas), dtype=bool)
+ n_sel = len(selected_indices)
+ for idx, ci in enumerate(selected_indices):
+ cam_dir = candidates[ci]
+ for fi in range(len(normals)):
+ if not backface_vis[fi, ci]:
+ continue
+ origin = centers[fi] + normals[fi] * epsilon
+ if not _ray_occluded(bvh_trees, origin, cam_dir, 1e6):
+ true_covered[fi] = True
+ yield 0.3 * (idx + 1) / n_sel
+
+ # ── Phase 2b: patch phantom-uncovered faces (30 % → 100 %) ───────
+ phantom_uncovered = covered_bf & ~true_covered
+
+ if phantom_uncovered.any():
+ remaining_budget = max_cameras - len(selected_indices)
+ if remaining_budget > 0:
+ used_set = set(selected_indices)
+ unused_mask = np.array([i not in used_set
+ for i in range(n_candidates)])
+ unused_indices = np.where(unused_mask)[0]
+
+ if len(unused_indices) > 0:
+ phantom_indices = np.where(phantom_uncovered)[0]
+ ph_normals = normals[phantom_indices]
+ ph_centers = centers[phantom_indices]
+ ph_areas = areas[phantom_indices]
+ ph_bf = ph_normals @ candidates[unused_indices].T > 0.26
+
+ ph_vis = np.zeros_like(ph_bf)
+ n_unused = len(unused_indices)
+ BATCH = 5
+ for j_local, j_global in enumerate(unused_indices):
+ cam_dir = candidates[j_global]
+ for i_local in range(len(phantom_indices)):
+ if not ph_bf[i_local, j_local]:
+ continue
+ origin = (ph_centers[i_local]
+ + ph_normals[i_local] * epsilon)
+ if not _ray_occluded(bvh_trees, origin,
+ cam_dir, 1e6):
+ ph_vis[i_local, j_local] = True
+ if (j_local + 1) % BATCH == 0 or j_local == n_unused - 1:
+ yield 0.3 + 0.7 * (j_local + 1) / n_unused
+
+ # Greedy on sub-matrix (instant)
+ ph_covered = np.zeros(len(ph_areas), dtype=bool)
+ for _ in range(remaining_budget):
+ uncov = ~ph_covered
+ nv = ph_vis & uncov[:, np.newaxis]
+ na = (nv * ph_areas[:, np.newaxis]).sum(axis=0)
+ best_local = int(np.argmax(na))
+ if na[best_local] < total_area * 0.005:
+ break
+ best_global = int(unused_indices[best_local])
+ selected_indices.append(best_global)
+ ph_covered |= ph_vis[:, best_local]
+ for i_local in range(len(phantom_indices)):
+ if ph_vis[i_local, best_local]:
+ true_covered[phantom_indices[i_local]] = True
+
+ yield 1.0 # ensure caller sees 100 %
+ selected = [candidates[ci].copy() for ci in selected_indices]
+ final_cov = (float(areas[true_covered].sum() / total_area)
+ if true_covered.any() else 0.0)
+ return selected, final_cov
+
+
+def _sort_directions_spatially(directions, ref_direction=None):
+ """Sort direction vectors by azimuth angle so cameras progress smoothly
+ around the subject. The camera whose azimuth is closest to
+ *ref_direction* (typically the viewport look direction) becomes the first
+ entry. Important for sequential generation mode where each camera needs
+ spatial context from the previous one."""
+ if len(directions) <= 1:
+ return directions
+ # Azimuth = atan2(y, x) gives a smooth circular ordering
+ angles = [math.atan2(float(d[1]), float(d[0])) for d in directions]
+ paired = sorted(zip(angles, directions), key=lambda p: p[0])
+
+ if ref_direction is not None:
+ # Use full 3D angular distance (dot product) so elevation is
+ # considered when choosing the start camera, not just azimuth.
+ ref_v = mathutils.Vector(ref_direction).normalized()
+ best_idx = 0
+ best_dot = -2.0
+ for idx, (_, d) in enumerate(paired):
+ dot = mathutils.Vector(d).normalized().dot(ref_v)
+ if dot > best_dot:
+ best_dot = dot
+ best_idx = idx
+ # Rotate so closest-to-ref is first
+ paired = paired[best_idx:] + paired[:best_idx]
+
+ return [d for _, d in paired]
+
+
+def _classify_camera_direction(cam_dir_np, ref_front_np):
+ """Classify a camera direction into a human-readable view label.
+
+ Uses Option-B scheme: elevation tiers first (top/bottom if >60°),
+ then 4 azimuth quadrants (front/right/left/rear, 90° each),
+ with 'from above'/'from below' modifiers for 30–60° elevation.
+
+ Parameters
+ ----------
+ cam_dir_np : array-like, shape (3,)
+ Centre-to-camera unit vector (where the camera is placed relative to
+ the mesh centre).
+ ref_front_np : array-like, shape (3,)
+ The reference "front" direction (centre-to-camera direction that
+ corresponds to the user's viewport, i.e. Camera_0's neighbourhood).
+
+ Returns
+ -------
+ str
+ A prompt-friendly label such as ``"front view"``,
+ ``"right side view, from above"``, ``"top view"``, etc.
+ """
+ d = np.array(cam_dir_np, dtype=float)
+ d /= max(np.linalg.norm(d), 1e-12)
+
+ # --- Elevation (angle above / below the horizontal XY plane) ---
+ elevation_rad = math.asin(np.clip(d[2], -1.0, 1.0))
+ elevation_deg = math.degrees(elevation_rad)
+
+ if elevation_deg > 60.0:
+ return "top view"
+ if elevation_deg < -60.0:
+ return "bottom view"
+
+ # --- Azimuth relative to ref_front (projected onto XY plane) ---
+ ref = np.array(ref_front_np, dtype=float)
+ # Project both onto XY
+ d_h = np.array([d[0], d[1]], dtype=float)
+ r_h = np.array([ref[0], ref[1]], dtype=float)
+ d_h_len = np.linalg.norm(d_h)
+ r_h_len = np.linalg.norm(r_h)
+ if d_h_len < 1e-8 or r_h_len < 1e-8:
+ # Nearly vertical – should have been caught by the elevation check,
+ # but fall back just in case.
+ return "top view" if d[2] >= 0 else "bottom view"
+ d_h /= d_h_len
+ r_h /= r_h_len
+
+ # Signed angle: positive = counterclockwise from above = to the user's right
+ sin_a = r_h[0] * d_h[1] - r_h[1] * d_h[0]
+ cos_a = r_h[0] * d_h[0] + r_h[1] * d_h[1]
+ azimuth_deg = math.degrees(math.atan2(sin_a, cos_a))
+
+ # Quadrant classification (each 90°)
+ abs_az = abs(azimuth_deg)
+ if abs_az <= 45.0:
+ base = "front view"
+ elif abs_az >= 135.0:
+ base = "rear view"
+ elif azimuth_deg > 0:
+ base = "right side view"
+ else:
+ base = "left side view"
+
+ # Elevation modifier
+ if elevation_deg > 30.0:
+ return f"{base}, from above"
+ if elevation_deg < -30.0:
+ return f"{base}, from below"
+ return base
+
+
+def _compute_per_camera_aspect(direction_np, verts_world, center):
+ """Compute the silhouette aspect ratio (width / height) from a single
+ camera direction. Returns the aspect ratio as a float."""
+ right, up, _d = _camera_basis(direction_np)
+ rel = verts_world - center
+ proj_r = rel @ right
+ proj_u = rel @ up
+ w = max(float(proj_r.max() - proj_r.min()), 0.001)
+ h = max(float(proj_u.max() - proj_u.min()), 0.001)
+ return w / h
+
+
+def _perspective_aspect(verts_world, cam_pos_np, cam_dir_np):
+ """Compute the visible angular aspect ratio (width / height) as seen
+ from a perspective camera at *cam_pos_np* looking along *cam_dir_np*."""
+ right, up, d = _camera_basis(cam_dir_np)
+ forward = -d
+
+ rel = verts_world - cam_pos_np
+ depth = rel @ forward # positive = in front of camera
+ depth = np.maximum(depth, 0.001)
+ pr = rel @ right
+ pu = rel @ up
+
+ angle_r = np.arctan2(pr, depth)
+ angle_u = np.arctan2(pu, depth)
+
+ angular_w = float(angle_r.max() - angle_r.min())
+ angular_h = float(angle_u.max() - angle_u.min())
+ if angular_h < 0.001:
+ return 1.0
+ return angular_w / angular_h
+
+
+def _resolution_from_aspect(aspect, total_px, align=8):
+ """Compute (res_x, res_y) for a given *aspect* ratio (w/h) keeping
+ approximately *total_px* pixels, snapped to *align*."""
+ new_x = math.sqrt(total_px * aspect)
+ new_y = total_px / new_x
+ new_x = max(align, int(round(new_x / align)) * align)
+ new_y = max(align, int(round(new_y / align)) * align)
+ return new_x, new_y
+
+
+def _get_resolution_align(context):
+ """Return the resolution alignment step: 112 for Qwen with alignment
+ enabled, 8 otherwise."""
+ if (context.scene.model_architecture.startswith('qwen')
+ and getattr(context.scene, 'qwen_rescale_alignment', False)):
+ return 112
+ return 8
+
+
+def _apply_auto_aspect(directions_np, context, verts_world):
+ """Adjust scene render resolution to match the mesh's average apparent
+ aspect ratio across the given camera *directions_np*. This is the
+ 'shared' mode — all cameras use the same resolution.
+ Keeps total pixel count approximately constant and snaps to alignment step.
+ Returns (new_res_x, new_res_y)."""
+ center = verts_world.mean(axis=0)
+ aspects = [_compute_per_camera_aspect(d, verts_world, center)
+ for d in directions_np]
+ avg_aspect = float(np.mean(aspects))
+
+ render = context.scene.render
+ total_px = render.resolution_x * render.resolution_y
+ align = _get_resolution_align(context)
+ new_x, new_y = _resolution_from_aspect(avg_aspect, total_px, align=align)
+ render.resolution_x = new_x
+ render.resolution_y = new_y
+ return new_x, new_y
+
+
+def _store_per_camera_resolution(cam_obj, res_x, res_y):
+ """Store per-camera resolution as custom properties on the camera object."""
+ cam_obj["sg_res_x"] = res_x
+ cam_obj["sg_res_y"] = res_y
+
+
+def _get_camera_resolution(cam_obj, scene):
+ """Return (res_x, res_y) for a camera. Falls back to scene render
+ resolution if no per-camera resolution is stored."""
+ if "sg_res_x" in cam_obj and "sg_res_y" in cam_obj:
+ return int(cam_obj["sg_res_x"]), int(cam_obj["sg_res_y"])
+ return scene.render.resolution_x, scene.render.resolution_y
+
+
+class _SGCameraResolution:
+ """Context manager: temporarily set scene render resolution to a camera's
+ per-camera values (if stored), and restore the original on exit.
+
+ Usage::
+
+ with _SGCameraResolution(context, camera_obj):
+ bpy.ops.render.render(write_still=True)
+ """
+ def __init__(self, context, cam_obj):
+ self._render = context.scene.render
+ self._cam = cam_obj
+ self._scene = context.scene
+ self._orig_x = self._render.resolution_x
+ self._orig_y = self._render.resolution_y
+
+ def __enter__(self):
+ rx, ry = _get_camera_resolution(self._cam, self._scene)
+ self._render.resolution_x = rx
+ self._render.resolution_y = ry
+ return self
+
+ def __exit__(self, *exc):
+ self._render.resolution_x = self._orig_x
+ self._render.resolution_y = self._orig_y
+ return False
+
+
+def _get_fov(cam_settings, context, res_x=None, res_y=None):
+ """Return (fov_x, fov_y) in radians for the given camera data block.
+ If *res_x* / *res_y* are not provided, reads from scene render settings."""
+ fov_x = cam_settings.angle_x
+ if res_x is None:
+ res_x = context.scene.render.resolution_x
+ if res_y is None:
+ res_y = context.scene.render.resolution_y
+ if res_y > res_x:
+ fov_x = 2 * math.atan(math.tan(fov_x / 2) * res_x / res_y)
+ fov_y = 2 * math.atan(math.tan(fov_x / 2) * res_y / res_x)
+ return fov_x, fov_y
diff --git a/stablegen/cameras/operators.py b/stablegen/cameras/operators.py
new file mode 100644
index 0000000..5d58aca
--- /dev/null
+++ b/stablegen/cameras/operators.py
@@ -0,0 +1,193 @@
+"""Camera utility operators – switch viewport, clone, mirror, toggle labels."""
+
+import bpy # pylint: disable=import-error
+import mathutils
+from ..utils import sg_modal_active
+from . import overlays as _overlays
+from .overlays import (
+ _sg_ensure_label_overlay, _sg_remove_label_overlay,
+ _sg_ensure_crop_overlay, _sg_remove_crop_overlay,
+ _setup_square_camera_display,
+)
+from .geometry import _store_per_camera_resolution
+
+def switch_viewport_to_camera(context, camera):
+ """Switches the first found 3D viewport to the specified camera's view."""
+ if not camera:
+ return
+ for area in context.screen.areas:
+ if area.type == 'VIEW_3D':
+ # Ensure we are in the right space and region
+ for space in area.spaces:
+ if space.type == 'VIEW_3D':
+ space.region_3d.view_perspective = 'CAMERA'
+ # Make sure the scene's active camera is set
+ context.scene.camera = camera
+ area.tag_redraw()
+ break
+ break
+
+class CloneCamera(bpy.types.Operator):
+ """Create a new camera at the active camera's position and enter fly mode to reposition it.
+
+ If no camera exists, one is created from the current viewport.
+ Useful for incrementally adding cameras one at a time."""
+ bl_idname = "object.clone_camera"
+ bl_label = "Clone Camera"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ _timer = None
+ _camera = None
+ _original_camera = None
+
+ @classmethod
+ def poll(cls, context):
+ if sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ return True
+
+ def execute(self, context):
+ ref_cam = context.scene.camera
+ self._original_camera = ref_cam
+
+ if not ref_cam:
+ # Create from viewport
+ rv3d = context.region_data
+ cam_data = bpy.data.cameras.new(name='Camera_clone')
+ cam_obj = bpy.data.objects.new('Camera_clone', cam_data)
+ context.collection.objects.link(cam_obj)
+ cam_obj.matrix_world = rv3d.view_matrix.inverted()
+ else:
+ # Clone from active camera
+ cam_data = bpy.data.cameras.new(name=f'{ref_cam.name}_clone')
+ cam_obj = bpy.data.objects.new(cam_data.name, cam_data)
+ context.collection.objects.link(cam_obj)
+ cam_obj.matrix_world = ref_cam.matrix_world.copy()
+ cam_obj.data.type = ref_cam.data.type
+ cam_obj.data.lens = ref_cam.data.lens
+ cam_obj.data.sensor_width = ref_cam.data.sensor_width
+ cam_obj.data.sensor_height = ref_cam.data.sensor_height
+ cam_obj.data.clip_start = ref_cam.data.clip_start
+ cam_obj.data.clip_end = ref_cam.data.clip_end
+
+ self._camera = cam_obj
+ context.scene.camera = cam_obj
+
+ rv3d = context.region_data
+ if rv3d.view_perspective != 'CAMERA':
+ bpy.ops.view3d.view_camera()
+ bpy.ops.view3d.view_center_camera()
+ try:
+ rv3d.view_camera_zoom = 1.0
+ except Exception:
+ pass
+
+ context.window_manager.modal_handler_add(self)
+ self._timer = context.window_manager.event_timer_add(0.5, window=context.window)
+ bpy.ops.view3d.fly('INVOKE_DEFAULT')
+ return {'RUNNING_MODAL'}
+
+ def modal(self, context, event):
+ if event.type == 'TIMER':
+ fly_running = any(op.bl_idname == 'VIEW3D_OT_fly'
+ for w in context.window_manager.windows
+ for op in w.modal_operators)
+ if not fly_running:
+ context.window_manager.event_timer_remove(self._timer)
+ if self._original_camera:
+ context.scene.camera = self._original_camera
+ self.report({'INFO'}, f"Camera cloned: {self._camera.name}")
+ return {'FINISHED'}
+ return {'PASS_THROUGH'}
+ return {'PASS_THROUGH'}
+
+
+class MirrorCamera(bpy.types.Operator):
+ """Create a mirror of the active camera across a chosen axis through the object/scene center.
+
+ The new camera is placed symmetrically on the opposite side and oriented to look at the center."""
+ bl_idname = "object.mirror_camera"
+ bl_label = "Mirror Camera"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ mirror_axis: bpy.props.EnumProperty(
+ name="Mirror Axis",
+ description="Axis to mirror across",
+ items=[
+ ('X', "X Axis", "Mirror left / right"),
+ ('Y', "Y Axis", "Mirror front / back"),
+ ('Z', "Z Axis", "Mirror top / bottom"),
+ ],
+ default='X'
+ ) # type: ignore
+
+ @classmethod
+ def poll(cls, context):
+ return context.scene.camera is not None and not sg_modal_active(context)
+
+ def invoke(self, context, event):
+ return context.window_manager.invoke_props_dialog(self)
+
+ def execute(self, context):
+ cam = context.scene.camera
+ obj = context.object
+
+ # Determine center point
+ if obj and obj.type == 'MESH':
+ center = obj.matrix_world.translation.copy()
+ else:
+ center = mathutils.Vector((0, 0, 0))
+
+ # Mirror position
+ pos = cam.location.copy()
+ axis_idx = 'XYZ'.index(self.mirror_axis)
+ delta = pos - center
+ delta[axis_idx] = -delta[axis_idx]
+ new_pos = center + delta
+
+ # Create new camera
+ cam_data = bpy.data.cameras.new(name=f'{cam.name}_mirror_{self.mirror_axis}')
+ cam_obj = bpy.data.objects.new(cam_data.name, cam_data)
+ context.collection.objects.link(cam_obj)
+
+ # Copy settings
+ cam_obj.data.type = cam.data.type
+ cam_obj.data.lens = cam.data.lens
+ cam_obj.data.sensor_width = cam.data.sensor_width
+ cam_obj.data.sensor_height = cam.data.sensor_height
+ cam_obj.data.clip_start = cam.data.clip_start
+ cam_obj.data.clip_end = cam.data.clip_end
+
+ cam_obj.location = new_pos
+ direction = center - new_pos
+ rot_quat = direction.to_track_quat('-Z', 'Y')
+ cam_obj.rotation_euler = rot_quat.to_euler()
+
+ self.report({'INFO'}, f"Mirrored camera created: {cam_obj.name}")
+ return {'FINISHED'}
+
+
+class ToggleCameraLabels(bpy.types.Operator):
+ """Toggle floating camera prompt labels in the 3D viewport.
+
+ Shows or hides the per-camera prompt text (from Collect Camera Prompts
+ or auto-generated view labels) next to each camera in the viewport."""
+ bl_idname = "object.toggle_camera_labels"
+ bl_label = "Toggle Camera Labels"
+ bl_options = {'REGISTER'}
+
+ @classmethod
+ def poll(cls, context):
+ return any(o.type == 'CAMERA' for o in context.scene.objects) and not sg_modal_active(context)
+
+ def execute(self, context):
+ if _overlays._sg_label_draw_handle is not None:
+ _sg_remove_label_overlay()
+ self.report({'INFO'}, "Camera labels hidden")
+ else:
+ _sg_ensure_label_overlay()
+ self.report({'INFO'}, "Camera labels visible")
+ return {'FINISHED'}
+
+
\ No newline at end of file
diff --git a/stablegen/cameras/overlays.py b/stablegen/cameras/overlays.py
new file mode 100644
index 0000000..29b34c2
--- /dev/null
+++ b/stablegen/cameras/overlays.py
@@ -0,0 +1,231 @@
+"""GPU draw handlers for camera crop overlays and view labels."""
+
+import math
+import bpy # pylint: disable=import-error
+import mathutils # pylint: disable=import-error
+import numpy as np
+import blf
+import gpu
+from gpu_extras.batch import batch_for_shader
+from .geometry import _get_camera_resolution
+
+_ADDON_PKG = __package__.rsplit('.', 1)[0]
+
+
+# ---- Per-camera crop overlay (GPU draw handler) -------------------------
+
+_sg_crop_draw_handle = None
+
+
+def _sg_draw_crop_overlays():
+ """SpaceView3D draw callback: renders a coloured rectangle inside each
+ camera's pyramid to visualise the actual (non-square) crop region."""
+ context = bpy.context
+ scene = context.scene
+
+ # Respect Blender's overlay toggle
+ space = context.space_data
+ if space and hasattr(space, 'overlay') and not space.overlay.show_overlays:
+ return
+
+ shader = gpu.shader.from_builtin('UNIFORM_COLOR')
+
+ for obj in scene.objects:
+ if obj.type != 'CAMERA' or 'sg_display_crop' not in obj:
+ continue
+ res_x = obj.get('sg_res_x', 0)
+ res_y = obj.get('sg_res_y', 0)
+ if res_x <= 0 or res_y <= 0:
+ continue
+
+ cam = obj.data
+ # Use whichever side of THIS camera is longer as the reference.
+ # The scene is set to a square of max_side, so the frustum is square
+ # and each side = half_w = half_h. The crop rectangle occupies
+ # res_x/cam_max × res_y/cam_max of that square.
+ cam_max = max(res_x, res_y)
+
+ frame = cam.view_frame(scene=scene)
+ corner = frame[0]
+ half_w = abs(corner[0])
+ half_h = abs(corner[1])
+ z_depth = corner[2]
+ if half_w < 1e-8 or half_h < 1e-8:
+ continue
+
+ sx = res_x / cam_max
+ sy = res_y / cam_max
+
+ new_hw = half_w * sx
+ new_hh = half_h * sy
+
+ crop_local = [
+ mathutils.Vector((+new_hw, +new_hh, z_depth)),
+ mathutils.Vector((-new_hw, +new_hh, z_depth)),
+ mathutils.Vector((-new_hw, -new_hh, z_depth)),
+ mathutils.Vector((+new_hw, -new_hh, z_depth)),
+ ]
+
+ wm = obj.matrix_world
+ world_pts = [wm @ v for v in crop_local]
+ coords = [(v.x, v.y, v.z) for v in world_pts]
+ indices = [(0, 1), (1, 2), (2, 3), (3, 0)]
+ batch = batch_for_shader(shader, 'LINES', {"pos": coords}, indices=indices)
+ shader.bind()
+ _prefs = bpy.context.preferences.addons.get(_ADDON_PKG)
+ _oc = _prefs.preferences.overlay_color if _prefs else (0.3, 0.5, 1.0)
+ shader.uniform_float("color", (_oc[0], _oc[1], _oc[2], 0.9))
+ gpu.state.line_width_set(2.0)
+ gpu.state.blend_set('ALPHA')
+ gpu.state.depth_test_set('LESS_EQUAL')
+ gpu.state.depth_mask_set(False)
+ batch.draw(shader)
+
+ gpu.state.depth_test_set('NONE')
+ gpu.state.depth_mask_set(True)
+ gpu.state.blend_set('NONE')
+ gpu.state.line_width_set(1.0)
+
+
+# ---- Per-camera view-label overlay (GPU text handler) --------------------
+
+_sg_label_draw_handle = None
+_sg_labels_user_visible = True
+
+
+def _sg_draw_view_labels():
+ """SpaceView3D POST_PIXEL callback: draws full camera prompt text
+ (from ``scene.camera_prompts``) near each camera in the viewport."""
+ context = bpy.context
+ scene = context.scene
+ region = context.region
+ rv3d = context.region_data
+ if region is None or rv3d is None:
+ return
+
+ # Respect Blender's overlay toggle
+ space = context.space_data
+ if space and hasattr(space, 'overlay') and not space.overlay.show_overlays:
+ return
+
+ from bpy_extras.view3d_utils import location_3d_to_region_2d
+
+ font_id = 0
+ blf.size(font_id, 13)
+ _prefs = bpy.context.preferences.addons.get(_ADDON_PKG)
+ _oc = _prefs.preferences.overlay_color if _prefs else (0.3, 0.5, 1.0)
+ blf.color(font_id, _oc[0], _oc[1], _oc[2], 0.95)
+
+ # Build a lookup of camera name -> prompt text
+ prompt_lookup = {item.name: item.prompt for item in scene.camera_prompts
+ if item.prompt}
+
+ for obj in scene.objects:
+ if obj.type != 'CAMERA':
+ continue
+ label = prompt_lookup.get(obj.name, '')
+ if not label:
+ continue
+ co_2d = location_3d_to_region_2d(region, rv3d, obj.location)
+ if co_2d is None:
+ continue
+ # Offset slightly below the camera marker
+ blf.position(font_id, co_2d.x - blf.dimensions(font_id, label)[0] * 0.5,
+ co_2d.y - 20, 0)
+ gpu.state.blend_set('ALPHA')
+ blf.draw(font_id, label)
+
+ gpu.state.blend_set('NONE')
+
+
+def _sg_ensure_crop_overlay():
+ """Register the crop overlay draw handler if not already active."""
+ global _sg_crop_draw_handle
+ if _sg_crop_draw_handle is None:
+ _sg_crop_draw_handle = bpy.types.SpaceView3D.draw_handler_add(
+ _sg_draw_crop_overlays, (), 'WINDOW', 'POST_VIEW')
+
+
+def _sg_remove_crop_overlay():
+ """Remove the crop overlay draw handler if active."""
+ global _sg_crop_draw_handle
+ if _sg_crop_draw_handle is not None:
+ bpy.types.SpaceView3D.draw_handler_remove(_sg_crop_draw_handle, 'WINDOW')
+ _sg_crop_draw_handle = None
+
+
+def _sg_ensure_label_overlay():
+ """Register the floating prompt-text label overlay if not already active."""
+ global _sg_label_draw_handle, _sg_labels_user_visible
+ _sg_labels_user_visible = True
+ if _sg_label_draw_handle is None:
+ _sg_label_draw_handle = bpy.types.SpaceView3D.draw_handler_add(
+ _sg_draw_view_labels, (), 'WINDOW', 'POST_PIXEL')
+ # Redraw all viewports so labels appear immediately
+ for area in (bpy.context.screen.areas if bpy.context.screen else []):
+ if area.type == 'VIEW_3D':
+ area.tag_redraw()
+
+
+def _sg_remove_label_overlay():
+ """Remove the floating prompt-text label overlay if active."""
+ global _sg_label_draw_handle, _sg_labels_user_visible
+ _sg_labels_user_visible = False
+ if _sg_label_draw_handle is not None:
+ bpy.types.SpaceView3D.draw_handler_remove(_sg_label_draw_handle, 'WINDOW')
+ _sg_label_draw_handle = None
+ for area in (bpy.context.screen.areas if bpy.context.screen else []):
+ if area.type == 'VIEW_3D':
+ area.tag_redraw()
+
+
+def _sg_hide_label_overlay():
+ """Temporarily hide the label overlay without changing the user's preference."""
+ global _sg_label_draw_handle
+ if _sg_label_draw_handle is not None:
+ bpy.types.SpaceView3D.draw_handler_remove(_sg_label_draw_handle, 'WINDOW')
+ _sg_label_draw_handle = None
+ for area in (bpy.context.screen.areas if bpy.context.screen else []):
+ if area.type == 'VIEW_3D':
+ area.tag_redraw()
+
+
+def _sg_restore_label_overlay():
+ """Restore the label overlay only if the user had it enabled."""
+ if _sg_labels_user_visible:
+ global _sg_label_draw_handle
+ if _sg_label_draw_handle is None:
+ _sg_label_draw_handle = bpy.types.SpaceView3D.draw_handler_add(
+ _sg_draw_view_labels, (), 'WINDOW', 'POST_PIXEL')
+ for area in (bpy.context.screen.areas if bpy.context.screen else []):
+ if area.type == 'VIEW_3D':
+ area.tag_redraw()
+
+
+def _sg_restore_square_display(scene):
+ """Restore scene resolution to the max-side square and tag viewports
+ for redraw so crop overlays reappear correctly."""
+ cameras = [o for o in scene.objects if o.type == 'CAMERA' and 'sg_res_x' in o]
+ if not cameras:
+ return
+ max_side = max(
+ max(int(c.get('sg_res_x', 0)), int(c.get('sg_res_y', 0)))
+ for c in cameras
+ )
+ if max_side > 0:
+ scene.render.resolution_x = max_side
+ scene.render.resolution_y = max_side
+ # Refresh viewports
+ for area in bpy.context.screen.areas if bpy.context.screen else []:
+ if area.type == 'VIEW_3D':
+ area.tag_redraw()
+
+
+def _setup_square_camera_display(cam_obj, res_x, res_y):
+ """Mark a camera for the crop overlay and enable passepartout."""
+ cam_data = cam_obj.data
+ cam_data.show_passepartout = True
+ cam_data.passepartout_alpha = 0.5
+ cam_obj["sg_display_crop"] = True
+ # Ensure the draw handler is alive
+ _sg_ensure_crop_overlay()
diff --git a/stablegen/cameras/placement.py b/stablegen/cameras/placement.py
new file mode 100644
index 0000000..2747c82
--- /dev/null
+++ b/stablegen/cameras/placement.py
@@ -0,0 +1,1360 @@
+"""AddCameras operator – multi-strategy camera placement."""
+
+import math
+import time
+import bpy, bmesh # pylint: disable=import-error
+import blf # pylint: disable=import-error
+import numpy as np
+import mathutils
+from mathutils import Matrix
+from ..utils import get_file_path, get_dir_path, sg_modal_active
+from .geometry import (
+ _existing_camera_directions, _filter_near_existing,
+ _fibonacci_sphere_points, _gather_target_meshes,
+ _get_mesh_face_data, _filter_bottom_faces, _get_mesh_verts_world,
+ _camera_basis, _rotation_from_basis, _compute_silhouette_distance,
+ _kmeans_on_sphere, _compute_pca_axes, _greedy_coverage_directions,
+ _build_bvh_trees, _ray_occluded, _greedy_select_from_visibility,
+ _occ_filter_faces_generator, _occ_vis_count_generator,
+ _occ_fom_generator, _occ_tpr_generator,
+ _sort_directions_spatially, _classify_camera_direction,
+ _compute_per_camera_aspect, _perspective_aspect, _resolution_from_aspect,
+ _get_resolution_align, _apply_auto_aspect,
+ _store_per_camera_resolution, _get_fov,
+)
+from .overlays import (
+ _sg_hide_label_overlay, _sg_remove_label_overlay,
+ _sg_restore_label_overlay, _setup_square_camera_display,
+)
+
+class AddCameras(bpy.types.Operator):
+ """Add cameras using various placement strategies and adjust their positions
+
+ Uses the active camera as a reference for settings. If there is no active camera, a new one is created based on the viewport.
+
+ Placement modes:
+ - Orbit Ring: cameras in a circle (inherits elevation from reference camera)
+ - Sphere Coverage: Fibonacci spiral for even sphere distribution
+ - Auto (Normal-Weighted): K-means clustering of mesh face normals
+ - Auto (PCA Axes): cameras along the mesh's principal component axes
+ - Auto (Greedy Coverage): iteratively adds cameras that maximise new visible surface area and auto-determines the count
+ - Fan from Camera: arc of cameras near the active camera
+
+ Tips:
+ - Try to frame the object / scene with minimal margin around it.
+ - Aim to achieve a uniform coverage of the object / scene.
+ - Areas not visible from any camera won't get textured. (Can still be UV-inpainted)
+ - Aspect ratio is set by Blender's output settings (or auto-computed)."""
+ bl_category = "ControlNet"
+ bl_idname = "object.add_cameras"
+ bl_label = "Add Cameras"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ placement_mode: bpy.props.EnumProperty(
+ name="Placement Mode",
+ description="Strategy for placing cameras around the subject",
+ items=[
+ ('orbit_ring', "Orbit Ring", "Place cameras in a circle around the center (original behaviour). Inherits elevation from the reference camera or viewport"),
+ ('hemisphere', "Sphere Coverage", "Distribute cameras evenly across a sphere using a Fibonacci spiral"),
+ ('normal_weighted', "Auto (Normal-Weighted)", "Automatically place cameras to cover the most surface area, using K-means on face normals weighted by area"),
+ ('pca_axes', "Auto (PCA Axes)", "Place cameras along the mesh's principal axes of variation"),
+ ('greedy_coverage', "Auto (Greedy Coverage)", "Iteratively add cameras that maximise new visible surface. Automatically determines the number of cameras needed"),
+ ('fan_from_camera', "Fan from Camera", "Spread cameras in an arc around the active camera's orbit position"),
+ ],
+ default='normal_weighted'
+ ) # type: ignore
+
+ num_cameras: bpy.props.IntProperty(
+ name="Number of Cameras",
+ description="Number of cameras to add (not used by Greedy Coverage which auto-determines count)",
+ default=4,
+ min=1,
+ max=100
+ ) # type: ignore
+
+ center_type: bpy.props.EnumProperty(
+ name="Center Type",
+ description="Type of center for the cameras",
+ items=[
+ ('object', "Object", "Use the active object as the center"),
+ ('view center', "View Center", "Use the view center as the center"),
+ ],
+ default='object'
+ ) # type: ignore
+
+ purge_others: bpy.props.BoolProperty(
+ name="Remove Existing Cameras",
+ description="Delete ALL existing cameras (including active) before adding new ones",
+ default=True
+ ) # type: ignore
+
+ consider_existing: bpy.props.BoolProperty(
+ name="Consider Existing Cameras",
+ description="Treat existing cameras as already-placed directions so auto modes avoid duplicating their coverage",
+ default=True
+ ) # type: ignore
+
+ fan_angle: bpy.props.FloatProperty(
+ name="Fan Angle",
+ description="Total angular spread of the fan in degrees",
+ default=90.0,
+ min=10.0,
+ max=350.0
+ ) # type: ignore
+
+ coverage_target: bpy.props.FloatProperty(
+ name="Coverage Target",
+ description="Stop adding cameras when this fraction of surface area is visible (Greedy mode)",
+ default=0.95,
+ min=0.5,
+ max=1.0,
+ subtype='FACTOR'
+ ) # type: ignore
+
+ max_auto_cameras: bpy.props.IntProperty(
+ name="Max Cameras",
+ description="Upper limit on cameras for Greedy Coverage mode",
+ default=12,
+ min=2,
+ max=50
+ ) # type: ignore
+
+ auto_aspect: bpy.props.EnumProperty(
+ name="Auto Aspect Ratio",
+ description="Automatically adjust render aspect ratio to match the mesh silhouette",
+ items=[
+ ('off', "Off", "Use current scene resolution for all cameras"),
+ ('shared', "Shared", "Average silhouette aspect across all cameras and set a single scene resolution"),
+ ('per_camera', "Per Camera", "Each camera gets its own optimal aspect ratio (resolution is swapped during generation)"),
+ ],
+ default='per_camera'
+ ) # type: ignore
+
+ exclude_bottom: bpy.props.BoolProperty(
+ name="Exclude Bottom Faces",
+ description="Ignore downward-facing geometry (e.g. flat building undersides) when placing cameras",
+ default=True
+ ) # type: ignore
+
+ exclude_bottom_angle: bpy.props.FloatProperty(
+ name="Bottom Angle Threshold",
+ description="Faces whose normal points more than this many degrees below the horizon are excluded",
+ default=1.5533, # 89 degrees in radians
+ min=0.1745, # 10 degrees
+ max=1.5708, # 90 degrees
+ subtype='ANGLE',
+ unit='ROTATION'
+ ) # type: ignore
+
+ auto_prompts: bpy.props.BoolProperty(
+ name="Auto View Prompts",
+ description="Automatically generate view-direction prompts (e.g. 'front view', 'rear view, from above') for each camera based on the viewport reference orientation",
+ default=False
+ ) # type: ignore
+
+ review_placement: bpy.props.BoolProperty(
+ name="Review Camera Placement",
+ description="After placing cameras, fly through each one for review. "
+ "When disabled the cameras are created immediately without the interactive fly-through",
+ default=True
+ ) # type: ignore
+
+ occlusion_mode: bpy.props.EnumProperty(
+ name="Occlusion Handling",
+ description="How to account for self-occlusion when choosing camera directions",
+ items=[
+ ('none', "None (Fast)",
+ "Back-face culling only – ignores self-occlusion. Fastest option"),
+ ('full_matrix', "Full Occlusion Matrix",
+ "Build a complete BVH-validated visibility matrix before greedy selection. Most accurate but slower"),
+ ('two_pass', "Two-Pass Refinement",
+ "Fast back-face pass, then targeted BVH refinement only for faces with zero true coverage"),
+ ('vis_weighted', "Visibility-Weighted",
+ "Weight faces by their visibility fraction from 200 directions. "
+ "Mostly-occluded faces have reduced influence on camera placement (linear). "
+ "Only affects Normal-Weighted mode; other modes fall back to Full Occlusion Matrix"),
+ ('vis_interactive', "Interactive Visibility",
+ "Like Visibility-Weighted but with a real-time preview: scroll to adjust "
+ "the occlusion balance and see cameras reposition instantly. "
+ "Only affects Normal-Weighted mode; other modes fall back to Full Occlusion Matrix"),
+ ],
+ default='none'
+ ) # type: ignore
+
+ clamp_elevation: bpy.props.BoolProperty(
+ name="Clamp Elevation",
+ description="Restrict camera elevation to avoid extreme top-down or bottom-up views "
+ "that diffusion models often struggle with",
+ default=False
+ ) # type: ignore
+
+ max_elevation_angle: bpy.props.FloatProperty(
+ name="Max Elevation",
+ description="Maximum upward elevation angle (degrees above horizon). "
+ "Cameras looking further up will be clamped to this angle",
+ default=1.2217, # 70 degrees in radians
+ min=0.0,
+ max=1.5708, # 90 degrees
+ subtype='ANGLE',
+ unit='ROTATION'
+ ) # type: ignore
+
+ min_elevation_angle: bpy.props.FloatProperty(
+ name="Min Elevation",
+ description="Minimum downward elevation angle (degrees below horizon). "
+ "Cameras looking further down will be clamped to this angle",
+ default=-0.1745, # -10 degrees in radians
+ min=-1.5708, # -90 degrees
+ max=0.0,
+ subtype='ANGLE',
+ unit='ROTATION'
+ ) # type: ignore
+
+ _timer = None
+ _camera_index = 0
+ _cameras = []
+ _initial_camera = None
+ _draw_handle = None
+ # Occlusion modal state
+ _occ_phase = False
+ _occ_gen = None
+ _occ_progress = 0.0
+ _occ_state = None
+ # Interactive visibility preview state
+ _vis_preview_phase = False
+ _vis_count = None
+ _vis_n_candidates = 200
+ _vis_balance = 0.2
+ _vis_state = None
+ _vis_directions = None
+
+ def draw(self, context):
+ """Custom dialog layout for the placement mode selector."""
+ layout = self.layout
+ layout.prop(self, "placement_mode")
+ layout.separator()
+ is_auto = self.placement_mode in ('hemisphere', 'normal_weighted', 'pca_axes', 'greedy_coverage')
+ if self.placement_mode == 'greedy_coverage':
+ layout.prop(self, "coverage_target")
+ layout.prop(self, "max_auto_cameras")
+ else:
+ layout.prop(self, "num_cameras")
+ if not is_auto:
+ layout.prop(self, "center_type")
+ if self.placement_mode == 'fan_from_camera':
+ layout.prop(self, "fan_angle")
+ if is_auto:
+ if self.placement_mode != 'hemisphere':
+ layout.prop(self, "occlusion_mode")
+ layout.prop(self, "auto_aspect")
+ layout.prop(self, "exclude_bottom")
+ if self.exclude_bottom:
+ layout.prop(self, "exclude_bottom_angle")
+ layout.prop(self, "auto_prompts")
+ layout.prop(self, "clamp_elevation")
+ if self.clamp_elevation:
+ row = layout.row(align=True)
+ row.prop(self, "min_elevation_angle", text="Min")
+ row.prop(self, "max_elevation_angle", text="Max")
+ layout.prop(self, "review_placement")
+ layout.prop(self, "purge_others")
+ if not self.purge_others and is_auto:
+ layout.prop(self, "consider_existing")
+
+ def draw_callback(self, context):
+ # Guard against operator being freed while draw handler is still registered
+ try:
+ _ = self._occ_phase # probe attribute access
+ except ReferenceError:
+ # Operator freed — remove the dangling draw handler
+ if AddCameras._draw_handle:
+ bpy.types.SpaceView3D.draw_handler_remove(
+ AddCameras._draw_handle, 'WINDOW')
+ AddCameras._draw_handle = None
+ return
+
+ # ── Occlusion progress display ───────────────────────────────────
+ if self._occ_phase:
+ font_id = 0
+ region = context.region
+ rw, rh = region.width, region.height
+ pct = self._occ_progress
+
+ # Progress bar background (dark)
+ bar_w, bar_h = 300, 18
+ bar_x = (rw - bar_w) / 2
+ bar_y = rh * 0.10
+
+ # Draw progress text above bar
+ msg = f"Computing occlusion… {pct * 100:.0f}%"
+ blf.size(font_id, 18)
+ tw, _th = blf.dimensions(font_id, msg)
+ blf.position(font_id, (rw - tw) / 2, bar_y + bar_h + 6, 0)
+ blf.color(font_id, 1.0, 0.85, 0.35, 0.95)
+ blf.draw(font_id, msg)
+
+ hint = "Press ESC to cancel"
+ blf.size(font_id, 13)
+ hw, _hh = blf.dimensions(font_id, hint)
+ blf.position(font_id, (rw - hw) / 2, bar_y - 18, 0)
+ blf.color(font_id, 0.7, 0.7, 0.7, 0.8)
+ blf.draw(font_id, hint)
+ blf.color(font_id, 1.0, 1.0, 1.0, 1.0)
+ return
+
+ # ── Interactive visibility preview HUD ───────────────────────────
+ if self._vis_preview_phase:
+ font_id = 0
+ region = context.region
+ rw, rh = region.width, region.height
+ balance = self._vis_balance
+
+ msg = f"Occlusion Balance: {balance:.0%}"
+ blf.size(font_id, 22)
+ tw, _th = blf.dimensions(font_id, msg)
+ blf.position(font_id, (rw - tw) / 2, rh * 0.13, 0)
+ blf.color(font_id, 1.0, 0.85, 0.35, 0.95)
+ blf.draw(font_id, msg)
+
+ n_cams = len(self._cameras) if self._cameras else 0
+ info = f"{n_cams} cameras"
+ blf.size(font_id, 16)
+ iw, _ih = blf.dimensions(font_id, info)
+ blf.position(font_id, (rw - iw) / 2, rh * 0.13 - 26, 0)
+ blf.color(font_id, 0.9, 0.9, 0.9, 0.9)
+ blf.draw(font_id, info)
+
+ hint = "Scroll \u2191\u2193 to adjust | ENTER to confirm | ESC to cancel"
+ blf.size(font_id, 13)
+ hw, _hh = blf.dimensions(font_id, hint)
+ blf.position(font_id, (rw - hw) / 2, rh * 0.13 - 48, 0)
+ blf.color(font_id, 0.7, 0.7, 0.7, 0.8)
+ blf.draw(font_id, hint)
+ blf.color(font_id, 1.0, 1.0, 1.0, 1.0)
+ return
+
+ # ── Fly-through HUD ──────────────────────────────────────────────
+ try:
+ count = len(self._cameras)
+ idx = self._camera_index
+ except Exception:
+ return
+ if count == 0:
+ return
+ font_id = 0
+ region = context.region
+ rw, rh = region.width, region.height
+
+ msg = f"Camera: {idx+1}/{count} | Press SPACE to confirm"
+ blf.size(font_id, 20)
+ text_width, text_height = blf.dimensions(font_id, msg)
+ x = (rw - text_width) / 2
+ y = rh * 0.10
+ blf.position(font_id, x, y, 0)
+ blf.draw(font_id, msg)
+
+ # Show auto-generated view label below the main HUD line
+ if idx < count:
+ cam_obj = self._cameras[idx]
+ view_label = cam_obj.get('sg_view_label', '')
+ if view_label:
+ blf.size(font_id, 16)
+ blf.color(font_id, 1.0, 0.85, 0.35, 0.95)
+ lw, _lh = blf.dimensions(font_id, view_label)
+ blf.position(font_id, (rw - lw) / 2, y - 28, 0)
+ blf.draw(font_id, view_label)
+ blf.color(font_id, 1.0, 1.0, 1.0, 1.0)
+
+ def execute(self, context):
+ # --- Delete existing cameras if requested ---
+ if self.purge_others:
+ scene = context.scene
+ to_remove = [obj for obj in bpy.data.objects if obj.type == 'CAMERA']
+ for cam in to_remove:
+ for col in list(cam.users_collection):
+ col.objects.unlink(cam)
+ bpy.data.objects.remove(cam, do_unlink=True)
+ for cam_data in list(bpy.data.cameras):
+ if not cam_data.users:
+ bpy.data.cameras.remove(cam_data)
+ scene.camera = None
+
+ # --- Validate mesh requirement for mesh-based modes ---
+ if self.placement_mode in ('hemisphere', 'normal_weighted', 'pca_axes', 'greedy_coverage'):
+ target_meshes = _gather_target_meshes(context)
+ if not target_meshes:
+ self.report({'ERROR'}, "No mesh objects found for this placement mode. Select meshes or ensure the scene has meshes.")
+ return {'CANCELLED'}
+ total_faces = sum(len(o.data.polygons) for o in target_meshes)
+ if total_faces == 0:
+ self.report({'ERROR'}, "Target meshes have no faces.")
+ return {'CANCELLED'}
+
+ # --- Fallback for center_type ---
+ if self.center_type == 'object':
+ obj = context.object
+ if not obj:
+ self.report({'WARNING'}, "No active object found. Using view center instead.")
+ self.center_type = 'view center'
+
+ # --- Add draw handler ---
+ if AddCameras._draw_handle is None:
+ AddCameras._draw_handle = bpy.types.SpaceView3D.draw_handler_add(
+ self.draw_callback, (context,), 'WINDOW', 'POST_PIXEL')
+
+ # --- Determine if this is a "manual" mode (needs ref camera for orbit)
+ # or an "auto" mode (computes its own positions) ---
+ is_auto_mode = self.placement_mode in ('hemisphere', 'normal_weighted', 'pca_axes', 'greedy_coverage')
+
+ # --- Determine center location ---
+ # Auto modes compute their own center from mesh vertices later, so
+ # only the manual modes (orbit_ring, fan_from_camera) need this.
+ if is_auto_mode:
+ # Placeholder – auto branch overrides center_location from verts
+ rv3d = getattr(context, 'region_data', None)
+ if rv3d is not None:
+ center_location = rv3d.view_location.copy()
+ else:
+ center_location = mathutils.Vector((0.0, 0.0, 0.0))
+ elif self.center_type == 'object':
+ obj = context.object
+ cursor_loc = context.scene.cursor.location.copy()
+ context.scene.cursor.location = obj.location.copy()
+ bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS')
+ center_location = obj.location.copy()
+ bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
+ context.scene.cursor.location = cursor_loc
+ center_location = obj.matrix_world.translation + obj.matrix_world.to_3x3() @ (center_location - obj.matrix_world.translation)
+ else:
+ rv3d = getattr(context, 'region_data', None)
+ if rv3d is not None:
+ center_location = rv3d.view_location.copy()
+ else:
+ center_location = mathutils.Vector((0.0, 0.0, 0.0))
+
+ # --- Set or create reference camera ---
+ self._initial_camera = context.scene.camera
+ using_viewport_ref = False
+ if not self._initial_camera and not is_auto_mode:
+ # Manual modes: create a camera from the viewport as Camera_0
+ rv3d = getattr(context, 'region_data', None)
+ if rv3d is None:
+ self.report({'ERROR'}, "No 3D viewport available for camera placement")
+ return {'CANCELLED'}
+ cam_data = bpy.data.cameras.new(name='Camera_0')
+ cam_obj = bpy.data.objects.new('Camera_0', cam_data)
+ context.collection.objects.link(cam_obj)
+ cam_obj.matrix_world = rv3d.view_matrix.inverted()
+ context.scene.camera = cam_obj
+ self._initial_camera = cam_obj
+ using_viewport_ref = True
+
+ # --- Capture reference camera settings (for lens/sensor/clip copy) ---
+ cam_settings = self._initial_camera.data if self._initial_camera else None
+
+ self._cameras.clear()
+ self._camera_index = 0
+
+ # --- Branch by placement mode ---
+ if self.placement_mode == 'orbit_ring':
+ ref_mat = self._initial_camera.matrix_world.copy()
+ initial_pos = self._initial_camera.location.copy()
+ radius = (initial_pos - center_location).length
+ if radius < 0.001:
+ radius = 5.0
+ self._cameras.append(self._initial_camera)
+ self._place_orbit_ring(context, center_location, ref_mat, initial_pos, cam_settings, radius, using_viewport_ref)
+ elif self.placement_mode == 'fan_from_camera':
+ ref_mat = self._initial_camera.matrix_world.copy()
+ initial_pos = self._initial_camera.location.copy()
+ radius = (initial_pos - center_location).length
+ if radius < 0.001:
+ radius = 5.0
+ self._cameras.append(self._initial_camera)
+ self._place_fan(context, center_location, ref_mat, initial_pos, cam_settings, radius, using_viewport_ref)
+ else:
+ # ============================================================
+ # Auto modes: per-camera optimal distance from mesh silhouette
+ # ============================================================
+ # target_meshes was validated above
+
+ # Borrow settings from existing camera or create temp defaults
+ temp_cam_data = None
+ if not cam_settings:
+ temp_cam_data = bpy.data.cameras.new(name='_sg_temp_cam')
+ cam_settings = temp_cam_data
+
+ # Pre-compute combined mesh data across all target meshes
+ verts_world = _get_mesh_verts_world(target_meshes)
+ mesh_center = verts_world.mean(axis=0)
+ center_location = mathutils.Vector(mesh_center.tolist())
+ mesh_names = ', '.join(o.name for o in target_meshes)
+ self.report({'INFO'}, f"Target meshes ({len(target_meshes)}): {mesh_names}")
+
+ # --- Collect existing camera directions (for "consider existing") ---
+ existing_dirs = None
+ if not self.purge_others and self.consider_existing:
+ existing_dirs = _existing_camera_directions(mesh_center)
+ if existing_dirs:
+ self.report({'INFO'},
+ f"Considering {len(existing_dirs)} existing camera(s)")
+
+ # --- Determine directions ---
+ if self.placement_mode == 'greedy_coverage':
+ normals, areas, centers = _get_mesh_face_data(target_meshes)
+ if self.exclude_bottom:
+ normals, areas, centers = _filter_bottom_faces(
+ normals, areas, centers, self.exclude_bottom_angle)
+
+ occ = self.occlusion_mode
+ if occ == 'none':
+ directions, coverage = _greedy_coverage_directions(
+ normals, areas,
+ max_cameras=self.max_auto_cameras,
+ coverage_target=self.coverage_target,
+ existing_dirs=existing_dirs)
+ directions = [np.array(d) for d in directions]
+ self.report({'INFO'},
+ f"Greedy coverage (no occlusion): "
+ f"{len(directions)} cameras, {coverage*100:.1f}% coverage")
+ else:
+ # Greedy doesn't use K-means; vis modes fall back to FOM
+ eff_occ = occ if occ in ('full_matrix', 'two_pass') else 'full_matrix'
+ # ── Async occlusion via modal ────────────────────────
+ depsgraph = context.evaluated_depsgraph_get()
+ bvh_trees = _build_bvh_trees(target_meshes, depsgraph)
+ gen_func = (_occ_fom_generator if eff_occ == 'full_matrix'
+ else _occ_tpr_generator)
+ self._occ_gen = gen_func(
+ normals, areas, centers, bvh_trees,
+ max_cameras=self.max_auto_cameras,
+ coverage_target=self.coverage_target,
+ existing_dirs=existing_dirs)
+ self._occ_phase = True
+ self._occ_progress = 0.0
+ self._occ_state = {
+ 'result_type': 'greedy',
+ 'verts_world': verts_world,
+ 'mesh_center': mesh_center,
+ 'cam_settings': cam_settings,
+ 'temp_cam_data': temp_cam_data,
+ 'occ_label': ('full occlusion' if occ == 'full_matrix'
+ else 'two-pass'),
+ }
+ context.window_manager.modal_handler_add(self)
+ self._timer = context.window_manager.event_timer_add(
+ 0.01, window=context.window)
+ return {'RUNNING_MODAL'}
+ elif self.placement_mode == 'hemisphere':
+ points = _fibonacci_sphere_points(self.num_cameras)
+ directions = [np.array(p) for p in points]
+ if self.exclude_bottom:
+ # Remove directions that point more than the threshold below the horizon
+ threshold_z = -math.cos(self.exclude_bottom_angle)
+ directions = [d for d in directions if d[2] >= threshold_z]
+ if existing_dirs:
+ directions = _filter_near_existing(directions, existing_dirs)
+ elif self.placement_mode == 'normal_weighted':
+ normals, areas, centers = _get_mesh_face_data(target_meshes)
+ if self.exclude_bottom:
+ normals, areas, centers = _filter_bottom_faces(
+ normals, areas, centers, self.exclude_bottom_angle)
+
+ occ = self.occlusion_mode
+ if occ in ('vis_weighted', 'vis_interactive'):
+ # Visibility-count generator (shared by both modes)
+ depsgraph = context.evaluated_depsgraph_get()
+ bvh_trees = _build_bvh_trees(target_meshes, depsgraph)
+ self._occ_gen = _occ_vis_count_generator(
+ normals, areas, centers, bvh_trees)
+ self._occ_phase = True
+ self._occ_progress = 0.0
+ rt = 'vis_kmeans' if occ == 'vis_weighted' else 'vis_interactive'
+ self._occ_state = {
+ 'result_type': rt,
+ 'normals': normals,
+ 'areas': areas,
+ 'centers': centers,
+ 'num_cameras': self.num_cameras,
+ 'verts_world': verts_world,
+ 'mesh_center': mesh_center,
+ 'cam_settings': cam_settings,
+ 'temp_cam_data': temp_cam_data,
+ 'existing_dirs': existing_dirs,
+ }
+ context.window_manager.modal_handler_add(self)
+ self._timer = context.window_manager.event_timer_add(
+ 0.01, window=context.window)
+ return {'RUNNING_MODAL'}
+ elif occ != 'none':
+ depsgraph = context.evaluated_depsgraph_get()
+ bvh_trees = _build_bvh_trees(target_meshes, depsgraph)
+ self._occ_gen = _occ_filter_faces_generator(
+ normals, areas, centers, bvh_trees)
+ self._occ_phase = True
+ self._occ_progress = 0.0
+ self._occ_state = {
+ 'result_type': 'filter_kmeans',
+ 'normals': normals,
+ 'areas': areas,
+ 'centers': centers,
+ 'num_cameras': self.num_cameras,
+ 'verts_world': verts_world,
+ 'mesh_center': mesh_center,
+ 'cam_settings': cam_settings,
+ 'temp_cam_data': temp_cam_data,
+ 'existing_dirs': existing_dirs,
+ }
+ context.window_manager.modal_handler_add(self)
+ self._timer = context.window_manager.event_timer_add(
+ 0.01, window=context.window)
+ return {'RUNNING_MODAL'}
+ else:
+ k = min(self.num_cameras, len(normals))
+ cluster_dirs = _kmeans_on_sphere(normals, areas, k)
+ directions = [cluster_dirs[i] for i in range(len(cluster_dirs))]
+ if existing_dirs:
+ directions = _filter_near_existing(directions, existing_dirs)
+ elif self.placement_mode == 'pca_axes':
+ normals, areas, centers = _get_mesh_face_data(target_meshes)
+ if self.exclude_bottom:
+ normals, areas, centers = _filter_bottom_faces(
+ normals, areas, centers, self.exclude_bottom_angle)
+
+ occ = self.occlusion_mode
+ # PCA doesn't use K-means; vis modes fall back to filter
+ if occ not in ('none',):
+ depsgraph = context.evaluated_depsgraph_get()
+ bvh_trees = _build_bvh_trees(target_meshes, depsgraph)
+ self._occ_gen = _occ_filter_faces_generator(
+ normals, areas, centers, bvh_trees)
+ self._occ_phase = True
+ self._occ_progress = 0.0
+ self._occ_state = {
+ 'result_type': 'filter_pca',
+ 'normals': normals,
+ 'areas': areas,
+ 'centers': centers,
+ 'num_cameras': self.num_cameras,
+ 'exclude_bottom': self.exclude_bottom,
+ 'exclude_bottom_angle': self.exclude_bottom_angle,
+ 'verts_world': verts_world,
+ 'mesh_center': mesh_center,
+ 'cam_settings': cam_settings,
+ 'temp_cam_data': temp_cam_data,
+ 'existing_dirs': existing_dirs,
+ }
+ context.window_manager.modal_handler_add(self)
+ self._timer = context.window_manager.event_timer_add(
+ 0.01, window=context.window)
+ return {'RUNNING_MODAL'}
+ else:
+ axes = _compute_pca_axes(verts_world)
+ directions = []
+ for axis in axes:
+ directions.append(axis)
+ directions.append(-axis)
+ if self.exclude_bottom:
+ threshold_z = -math.cos(self.exclude_bottom_angle)
+ directions = [d for d in directions if d[2] >= threshold_z]
+ directions = directions[:min(self.num_cameras, len(directions))]
+ if existing_dirs:
+ directions = _filter_near_existing(directions, existing_dirs)
+
+ # Finalize: sort, aspect ratio, camera creation, auto prompts
+ self._finalize_auto_cameras(
+ context, directions, verts_world, mesh_center,
+ cam_settings, temp_cam_data)
+
+ # --- Start fly-through review ---
+ if self.review_placement:
+ if not self._start_fly_review(context):
+ return {'CANCELLED'}
+ return {'RUNNING_MODAL'}
+ else:
+ # Skip review — just finish immediately
+ self._finish_without_review(context)
+ return {'FINISHED'}
+
+ # -------------------------------------------------------
+ # Auto-mode finalization helpers
+ # -------------------------------------------------------
+
+ def _finalize_auto_cameras(self, context, directions, verts_world,
+ mesh_center, cam_settings, temp_cam_data):
+ """Sort directions, compute aspect ratios, create cameras, and
+ generate auto prompts. Shared by both the synchronous (no-occlusion)
+ and asynchronous (modal occlusion) code paths."""
+ ref_dir = None
+ if directions:
+ rv3d = context.region_data
+ if rv3d:
+ view_dir = rv3d.view_rotation @ mathutils.Vector((0, 0, -1))
+ ref_dir = np.array([-view_dir.x, -view_dir.y, -view_dir.z])
+ directions = _sort_directions_spatially(directions, ref_dir)
+
+ # --- Clamp elevation angles to avoid extreme top/bottom views ---
+ if self.clamp_elevation and directions:
+ min_rad = self.min_elevation_angle
+ max_rad = self.max_elevation_angle
+ clamped = []
+ for d in directions:
+ d_np = np.array(d, dtype=float)
+ norm = np.linalg.norm(d_np)
+ if norm < 1e-8:
+ continue
+ d_np /= norm
+ elev = math.asin(float(np.clip(d_np[2], -1.0, 1.0)))
+ if min_rad <= elev <= max_rad:
+ clamped.append(d_np)
+ continue
+ elev = max(min_rad, min(max_rad, elev))
+ horiz = math.sqrt(float(d_np[0]**2 + d_np[1]**2))
+ if horiz < 1e-8:
+ # Pure vertical direction – pick an arbitrary azimuth
+ azimuth = 0.0
+ else:
+ azimuth = math.atan2(float(d_np[1]), float(d_np[0]))
+ cos_e = math.cos(elev)
+ d_np = np.array([cos_e * math.cos(azimuth),
+ cos_e * math.sin(azimuth),
+ math.sin(elev)])
+ clamped.append(d_np)
+
+ # Deduplicate near-identical directions that may have been created
+ # by clamping several cameras to the same elevation limit.
+ _MERGE_COS = math.cos(math.radians(8.0)) # merge within 8°
+ deduped = []
+ for d in clamped:
+ if all(float(np.dot(d, existing)) < _MERGE_COS
+ for existing in deduped):
+ deduped.append(d)
+ n_before = len(directions)
+ directions = deduped
+ if len(directions) < n_before:
+ self.report({'INFO'},
+ f"Elevation clamp: {n_before} → {len(directions)} cameras "
+ f"(merged {n_before - len(directions)} near-duplicates)")
+
+ render = context.scene.render
+ total_px = render.resolution_x * render.resolution_y
+ center_np_for_aspect = verts_world.mean(axis=0)
+
+ if self.auto_aspect == 'shared' and directions:
+ dirs_np = [d / np.linalg.norm(d) for d in directions]
+ old_x, old_y = render.resolution_x, render.resolution_y
+ new_x, new_y = _apply_auto_aspect(dirs_np, context, verts_world)
+ if (new_x, new_y) != (old_x, old_y):
+ self.report({'INFO'},
+ f"Aspect ratio adjusted: {old_x}x{old_y} → {new_x}x{new_y}")
+
+ if self.auto_aspect == 'per_camera' and directions:
+ self._create_cameras_per_aspect(
+ context, directions, mesh_center, verts_world,
+ cam_settings, total_px, center_np_for_aspect)
+ else:
+ fov_x, fov_y = _get_fov(cam_settings, context)
+ self._create_cameras_from_directions(
+ context, directions, mesh_center, verts_world,
+ cam_settings, fov_x, fov_y)
+
+ if temp_cam_data:
+ bpy.data.cameras.remove(temp_cam_data)
+
+ if self.auto_prompts and self._cameras:
+ ref_front = (ref_dir if ref_dir is not None
+ else np.array([0.0, 1.0, 0.0]))
+ mesh_center_np = np.array(mesh_center, dtype=float)
+ for cam_obj in self._cameras:
+ cam_pos = np.array(cam_obj.location, dtype=float)
+ cam_dir = cam_pos - mesh_center_np
+ label = _classify_camera_direction(cam_dir, ref_front)
+ cam_obj["sg_view_label"] = label
+ prompt_item = next(
+ (item for item in context.scene.camera_prompts
+ if item.name == cam_obj.name), None)
+ if not prompt_item:
+ prompt_item = context.scene.camera_prompts.add()
+ prompt_item.name = cam_obj.name
+ prompt_item.prompt = label
+ context.scene.use_camera_prompts = True
+ self.report({'INFO'},
+ f"Auto-prompts: assigned view labels to "
+ f"{len(self._cameras)} cameras")
+ elif not self.auto_prompts:
+ # Clear any pre-existing auto-prompts so stale labels don't persist
+ context.scene.camera_prompts.clear()
+ context.scene.use_camera_prompts = False
+ for cam_obj in self._cameras:
+ if 'sg_view_label' in cam_obj:
+ del cam_obj['sg_view_label']
+ _sg_remove_label_overlay()
+
+ def _update_vis_cameras(self, context):
+ """Regenerate cameras based on current visibility balance setting."""
+ # Remove existing preview cameras
+ for cam in list(self._cameras):
+ cam_data = cam.data
+ bpy.data.objects.remove(cam, do_unlink=True)
+ if cam_data and not cam_data.users:
+ bpy.data.cameras.remove(cam_data)
+ self._cameras.clear()
+
+ state = self._vis_state
+ vis_count = self._vis_count
+ n_cand = self._vis_n_candidates
+ vis_fraction = vis_count.astype(float) / n_cand
+ balance = self._vis_balance
+
+ exterior = vis_count > 0
+ normals = state['normals'][exterior]
+ areas_base = state['areas'][exterior]
+ vf = vis_fraction[exterior]
+ # weight = area × (vis_fraction + balance × (1 - vis_fraction))
+ # balance=0: weight = area × vis_fraction (full vis-weighting)
+ # balance=1: weight = area (no vis-weighting, all exterior equal)
+ weighted_areas = areas_base * (vf + balance * (1.0 - vf))
+
+ existing_dirs = state.get('existing_dirs')
+ k = min(state['num_cameras'], len(normals))
+ if k > 0 and len(normals) > 0:
+ cluster_dirs = _kmeans_on_sphere(normals, weighted_areas, k)
+ directions = [cluster_dirs[i] for i in range(len(cluster_dirs))]
+ if existing_dirs:
+ directions = _filter_near_existing(directions, existing_dirs)
+ else:
+ directions = []
+
+ # Sort directions
+ ref_dir = None
+ rv3d = context.region_data
+ if rv3d:
+ view_dir = rv3d.view_rotation @ mathutils.Vector((0, 0, -1))
+ ref_dir = np.array([-view_dir.x, -view_dir.y, -view_dir.z])
+ directions = _sort_directions_spatially(directions, ref_dir)
+ self._vis_directions = directions
+
+ # Quick camera creation (without full finalize overhead)
+ if directions:
+ cam_settings = state['cam_settings']
+ fov_x, fov_y = _get_fov(cam_settings, context)
+ self._create_cameras_from_directions(
+ context, directions, state['mesh_center'],
+ state['verts_world'], cam_settings, fov_x, fov_y)
+ if self._cameras:
+ context.scene.camera = self._cameras[0]
+
+ n_ext = int(exterior.sum())
+ context.area.header_text_set(
+ f"Occlusion Balance: {balance:.0%} | "
+ f"{len(directions)} cameras, {n_ext} visible faces | "
+ f"Scroll to adjust | ENTER to confirm | ESC to cancel")
+
+ def _cleanup_vis_preview(self, context):
+ """Clean up interactive visibility preview state."""
+ # Delete preview cameras
+ for cam in list(self._cameras):
+ cam_data = cam.data
+ bpy.data.objects.remove(cam, do_unlink=True)
+ if cam_data and not cam_data.users:
+ bpy.data.cameras.remove(cam_data)
+ self._cameras.clear()
+
+ # Clean up temp camera data
+ state = self._vis_state
+ if state and state.get('temp_cam_data'):
+ bpy.data.cameras.remove(state['temp_cam_data'])
+
+ self._vis_preview_phase = False
+ self._vis_state = None
+ self._vis_count = None
+ context.area.header_text_set(None)
+ if AddCameras._draw_handle:
+ bpy.types.SpaceView3D.draw_handler_remove(
+ AddCameras._draw_handle, 'WINDOW')
+ AddCameras._draw_handle = None
+
+ def _start_fly_review(self, context, add_modal_handler=True):
+ """Frame the first camera and start fly-through review.
+
+ *add_modal_handler* should be False when called from an already-running
+ modal (e.g. after occlusion computation finishes).
+ Returns True if fly-through started, False if no cameras were created.
+ """
+ if not self._cameras:
+ self.report({'WARNING'}, "No cameras were created.")
+ if AddCameras._draw_handle:
+ bpy.types.SpaceView3D.draw_handler_remove(
+ AddCameras._draw_handle, 'WINDOW')
+ AddCameras._draw_handle = None
+ return False
+
+ rv3d = context.region_data
+ context.scene.camera = self._cameras[0]
+ if rv3d.view_perspective != 'CAMERA':
+ bpy.ops.view3d.view_camera()
+ bpy.ops.view3d.view_center_camera()
+ try:
+ rv3d.view_camera_zoom = 1.0
+ except Exception:
+ pass
+
+ _sg_hide_label_overlay()
+ if add_modal_handler:
+ context.window_manager.modal_handler_add(self)
+ self._timer = context.window_manager.event_timer_add(
+ 0.5, window=context.window)
+ self._last_time = time.time()
+ bpy.ops.view3d.fly('INVOKE_DEFAULT')
+ return True
+
+ def _finish_without_review(self, context):
+ """Clean up draw handler and restore state without fly-through."""
+ if AddCameras._draw_handle:
+ bpy.types.SpaceView3D.draw_handler_remove(
+ AddCameras._draw_handle, 'WINDOW')
+ AddCameras._draw_handle = None
+ if self._cameras:
+ context.scene.camera = self._cameras[0]
+ _sg_restore_label_overlay()
+ self.report({'INFO'},
+ f"Cameras added successfully ({len(self._cameras)} cameras).")
+
+ # -------------------------------------------------------
+ # Placement methods
+ # -------------------------------------------------------
+
+ def _place_orbit_ring(self, context, center_location, ref_mat, initial_pos, cam_settings, radius, using_viewport_ref):
+ """Place cameras in a circle (original AddCameras behaviour)."""
+ total = self.num_cameras
+ count = total - 1 if using_viewport_ref else total
+ angle_initial = math.atan2(initial_pos.y - center_location.y, initial_pos.x - center_location.x)
+ angle_step = 2 * math.pi / (count + 1)
+
+ for i in range(count):
+ angle = (i + 1) * angle_step + angle_initial
+ cam_data_new = bpy.data.cameras.new(name=f'Camera_{i+1}')
+ cam_obj_new = bpy.data.objects.new(f'Camera_{i+1}', cam_data_new)
+ context.collection.objects.link(cam_obj_new)
+ if using_viewport_ref:
+ delta = (i + 1) * angle_step
+ T1 = Matrix.Translation(-center_location)
+ Rz = Matrix.Rotation(delta, 4, 'Z')
+ T2 = Matrix.Translation(center_location)
+ cam_obj_new.matrix_world = T2 @ Rz @ T1 @ ref_mat
+ else:
+ x = center_location.x + radius * math.cos(angle)
+ y = center_location.y + radius * math.sin(angle)
+ z = initial_pos.z
+ cam_obj_new.location = (x, y, z)
+ direction = center_location - cam_obj_new.location
+ rot = direction.to_track_quat('-Z', 'Y')
+ cam_obj_new.rotation_euler = rot.to_euler()
+ self._copy_camera_settings(cam_obj_new, cam_settings)
+ self._cameras.append(cam_obj_new)
+
+ def _place_fan(self, context, center_location, ref_mat, initial_pos, cam_settings, radius, using_viewport_ref):
+ """Spread cameras in an arc around the active camera's position."""
+ fan_rad = math.radians(self.fan_angle)
+ angle_initial = math.atan2(initial_pos.y - center_location.y, initial_pos.x - center_location.x)
+ total = self.num_cameras
+ count = total - 1 if using_viewport_ref else total
+ if count <= 0:
+ return
+ for i in range(count):
+ if count == 1:
+ t = 0.0
+ else:
+ t = (i / (count - 1)) - 0.5 # range -0.5 .. 0.5
+ angle = angle_initial + t * fan_rad
+ cam_data_new = bpy.data.cameras.new(name=f'Camera_fan_{i}')
+ cam_obj_new = bpy.data.objects.new(f'Camera_fan_{i}', cam_data_new)
+ context.collection.objects.link(cam_obj_new)
+ if using_viewport_ref:
+ delta = t * fan_rad
+ T1 = Matrix.Translation(-center_location)
+ Rz = Matrix.Rotation(delta, 4, 'Z')
+ T2 = Matrix.Translation(center_location)
+ cam_obj_new.matrix_world = T2 @ Rz @ T1 @ ref_mat
+ else:
+ x = center_location.x + radius * math.cos(angle)
+ y = center_location.y + radius * math.sin(angle)
+ z = initial_pos.z
+ cam_obj_new.location = (x, y, z)
+ direction = center_location - cam_obj_new.location
+ rot = direction.to_track_quat('-Z', 'Y')
+ cam_obj_new.rotation_euler = rot.to_euler()
+ self._copy_camera_settings(cam_obj_new, cam_settings)
+ self._cameras.append(cam_obj_new)
+
+ def _create_cameras_from_directions(self, context, directions, center_np,
+ verts_world, cam_settings, fov_x, fov_y):
+ """Shared camera creation for all auto-placement modes.
+ Each camera gets its own optimal distance computed from the mesh's
+ silhouette extent in that viewing direction."""
+ center_vec = mathutils.Vector(center_np.tolist())
+ prefix_map = {
+ 'hemisphere': 'Camera_sphere',
+ 'normal_weighted': 'Camera_auto',
+ 'pca_axes': 'Camera_pca',
+ 'greedy_coverage': 'Camera',
+ }
+ prefix = prefix_map.get(self.placement_mode, 'Camera')
+
+ for i, d in enumerate(directions):
+ d_np = np.array(d, dtype=float)
+ dist, aim_off = _compute_silhouette_distance(verts_world, center_np, d_np, fov_x, fov_y)
+ dir_vec = mathutils.Vector(d_np.tolist()).normalized()
+ aim_point = center_vec + mathutils.Vector(aim_off.tolist())
+ pos = aim_point + dir_vec * dist
+
+ cam_data = bpy.data.cameras.new(name=f'{prefix}_{i}')
+ cam_obj = bpy.data.objects.new(f'{prefix}_{i}', cam_data)
+ context.collection.objects.link(cam_obj)
+ cam_obj.location = pos
+ right, up_v, d_unit = _camera_basis(d_np)
+ cam_obj.rotation_euler = _rotation_from_basis(right, up_v, d_unit)
+ self._copy_camera_settings(cam_obj, cam_settings)
+ self._cameras.append(cam_obj)
+
+ def _create_cameras_per_aspect(self, context, directions, center_np,
+ verts_world, cam_settings, total_px,
+ center_for_aspect):
+ """Create cameras with per-camera optimal aspect ratio and distance.
+ Uses an iterative refinement: first computes aspect from the
+ orthographic silhouette, then refines it using the actual perspective
+ projection from the computed camera position."""
+ center_vec = mathutils.Vector(center_np.tolist())
+ prefix_map = {
+ 'hemisphere': 'Camera_sphere',
+ 'normal_weighted': 'Camera_auto',
+ 'pca_axes': 'Camera_pca',
+ 'greedy_coverage': 'Camera',
+ }
+ prefix = prefix_map.get(self.placement_mode, 'Camera')
+
+ for i, d in enumerate(directions):
+ d_np = np.array(d, dtype=float)
+ d_unit = d_np / np.linalg.norm(d_np)
+
+ # --- Pass 1: orthographic aspect as initial estimate ---
+ align = _get_resolution_align(context)
+ aspect = _compute_per_camera_aspect(d_unit, verts_world, center_for_aspect)
+ res_x, res_y = _resolution_from_aspect(aspect, total_px, align=align)
+ fov_x, fov_y = _get_fov(cam_settings, context, res_x, res_y)
+ dist, aim_off = _compute_silhouette_distance(
+ verts_world, center_np, d_np, fov_x, fov_y)
+
+ # --- Pass 2: refine aspect from actual perspective camera pos ---
+ aim_point_np = center_np + aim_off
+ cam_pos_np = aim_point_np + d_unit * dist
+ aspect = _perspective_aspect(verts_world, cam_pos_np, d_np)
+ res_x, res_y = _resolution_from_aspect(aspect, total_px, align=align)
+ fov_x, fov_y = _get_fov(cam_settings, context, res_x, res_y)
+ dist, aim_off = _compute_silhouette_distance(
+ verts_world, center_np, d_np, fov_x, fov_y)
+
+ dir_vec = mathutils.Vector(d_np.tolist()).normalized()
+ aim_point = center_vec + mathutils.Vector(aim_off.tolist())
+ pos = aim_point + dir_vec * dist
+
+ cam_data = bpy.data.cameras.new(name=f'{prefix}_{i}')
+ cam_obj = bpy.data.objects.new(f'{prefix}_{i}', cam_data)
+ context.collection.objects.link(cam_obj)
+ cam_obj.location = pos
+ right, up_v, d_unit_cam = _camera_basis(d_np)
+ cam_obj.rotation_euler = _rotation_from_basis(right, up_v, d_unit_cam)
+ self._copy_camera_settings(cam_obj, cam_settings)
+
+ # Store per-camera resolution
+ _store_per_camera_resolution(cam_obj, res_x, res_y)
+ _setup_square_camera_display(cam_obj, res_x, res_y)
+ self._cameras.append(cam_obj)
+
+ # Set scene to square resolution (max side length) for viewport display
+ max_side = max(
+ max(int(c.get('sg_res_x', 0)), int(c.get('sg_res_y', 0)))
+ for c in self._cameras if 'sg_res_x' in c
+ ) if self._cameras else max(context.scene.render.resolution_x,
+ context.scene.render.resolution_y)
+ if max_side > 0:
+ context.scene.render.resolution_x = max_side
+ context.scene.render.resolution_y = max_side
+
+ @staticmethod
+ def _copy_camera_settings(cam_obj, cam_settings):
+ """Copy lens / sensor / clip settings from a reference camera data block."""
+ cam_obj.data.type = cam_settings.type
+ cam_obj.data.lens = cam_settings.lens
+ cam_obj.data.sensor_width = cam_settings.sensor_width
+ cam_obj.data.sensor_height = cam_settings.sensor_height
+ cam_obj.data.clip_start = cam_settings.clip_start
+ cam_obj.data.clip_end = cam_settings.clip_end
+
+ def modal(self, context, event):
+ # ── Occlusion computation phase ──────────────────────────────────
+ if self._occ_phase:
+ if event.type in {'ESC', 'RIGHTMOUSE'}:
+ # Cancel occlusion computation
+ context.window_manager.event_timer_remove(self._timer)
+ self._timer = None
+ self._occ_gen = None
+ self._occ_phase = False
+ context.area.header_text_set(None)
+ if AddCameras._draw_handle:
+ bpy.types.SpaceView3D.draw_handler_remove(
+ AddCameras._draw_handle, 'WINDOW')
+ AddCameras._draw_handle = None
+ # Clean up temp camera data stored in state
+ state = self._occ_state
+ if state and state.get('temp_cam_data'):
+ bpy.data.cameras.remove(state['temp_cam_data'])
+ self._occ_state = None
+ self.report({'WARNING'}, "Occlusion computation cancelled.")
+ return {'CANCELLED'}
+
+ if event.type == 'TIMER':
+ try:
+ progress = next(self._occ_gen)
+ self._occ_progress = progress
+ context.area.header_text_set(
+ f"Computing occlusion visibility… "
+ f"{progress * 100:.0f}% (ESC to cancel)")
+ # Force viewport redraw so the GPU overlay updates too
+ for area in context.screen.areas:
+ if area.type == 'VIEW_3D':
+ area.tag_redraw()
+ except StopIteration as done:
+ # Generator finished – retrieve result
+ context.area.header_text_set(None)
+
+ # Clean up occlusion timer
+ context.window_manager.event_timer_remove(self._timer)
+ self._timer = None
+ self._occ_gen = None
+ self._occ_phase = False
+
+ state = self._occ_state
+ result_type = state.get('result_type', 'greedy')
+
+ if result_type == 'greedy':
+ # Greedy occlusion: result is (directions, coverage)
+ directions, coverage = done.value
+ directions = [np.array(d) for d in directions]
+ occ_label = state['occ_label']
+ self.report({'INFO'},
+ f"Greedy coverage ({occ_label}): "
+ f"{len(directions)} cameras, "
+ f"{coverage * 100:.1f}% coverage")
+ elif result_type == 'filter_kmeans':
+ # Face filter for K-means: result is exterior mask
+ exterior = done.value
+ normals = state['normals'][exterior]
+ areas = state['areas'][exterior]
+ n_removed = int((~exterior).sum())
+ self.report({'INFO'},
+ f"Occlusion filter: removed {n_removed} "
+ f"interior faces, {len(normals)} remain")
+ k = min(state['num_cameras'], len(normals))
+ if k > 0 and len(normals) > 0:
+ cluster_dirs = _kmeans_on_sphere(normals, areas, k)
+ directions = [cluster_dirs[i]
+ for i in range(len(cluster_dirs))]
+ else:
+ directions = []
+ elif result_type == 'filter_pca':
+ # Face filter for PCA: result is exterior mask
+ exterior = done.value
+ # PCA uses filtered verts (only exterior faces)
+ f_centers = state['centers'][exterior]
+ n_removed = int((~exterior).sum())
+ self.report({'INFO'},
+ f"Occlusion filter: removed {n_removed} "
+ f"interior faces, {len(f_centers)} remain")
+ if len(f_centers) >= 3:
+ axes = _compute_pca_axes(f_centers)
+ else:
+ axes = _compute_pca_axes(
+ state['verts_world'])
+ directions = []
+ for axis in axes:
+ directions.append(axis)
+ directions.append(-axis)
+ if state.get('exclude_bottom'):
+ threshold_z = -math.cos(
+ state['exclude_bottom_angle'])
+ directions = [
+ d for d in directions
+ if d[2] >= threshold_z]
+ directions = directions[
+ :min(state['num_cameras'],
+ len(directions))]
+
+ elif result_type == 'vis_kmeans':
+ # Visibility-weighted K-means: weight by fraction
+ vis_count = done.value
+ n_cand = 200
+ vis_fraction = vis_count.astype(float) / n_cand
+ exterior = vis_count > 0
+ normals = state['normals'][exterior]
+ areas_base = state['areas'][exterior]
+ weighted_areas = areas_base * vis_fraction[exterior]
+ n_ext = int(exterior.sum())
+ n_removed = len(vis_count) - n_ext
+ self.report({'INFO'},
+ f"Visibility-weighted: {n_ext} visible faces, "
+ f"{n_removed} fully occluded removed")
+ k = min(state['num_cameras'], len(normals))
+ if k > 0 and len(normals) > 0:
+ cluster_dirs = _kmeans_on_sphere(
+ normals, weighted_areas, k)
+ directions = [cluster_dirs[i]
+ for i in range(len(cluster_dirs))]
+ else:
+ directions = []
+
+ elif result_type == 'vis_interactive':
+ # Enter interactive preview phase
+ vis_count = done.value
+ self._vis_count = vis_count
+ self._vis_n_candidates = 200
+ self._vis_balance = 0.2
+ self._vis_preview_phase = True
+ self._vis_state = state
+ self._occ_state = None
+ self._update_vis_cameras(context)
+ return {'RUNNING_MODAL'}
+
+ # Angular dedup against existing cameras (filter paths)
+ ex_dirs = state.get('existing_dirs')
+ if ex_dirs and result_type in (
+ 'filter_kmeans', 'filter_pca', 'vis_kmeans'):
+ directions = _filter_near_existing(
+ directions, ex_dirs)
+
+ # Finalize cameras (sort, aspect, create, prompts)
+ self._finalize_auto_cameras(
+ context, directions,
+ state['verts_world'], state['mesh_center'],
+ state['cam_settings'], state['temp_cam_data'])
+ self._occ_state = None
+
+ # Transition to fly-through review
+ if self.review_placement:
+ if not self._start_fly_review(
+ context, add_modal_handler=False):
+ return {'CANCELLED'}
+ # Stay in modal for the fly-through phase
+ else:
+ self._finish_without_review(context)
+ return {'FINISHED'}
+ return {'RUNNING_MODAL'}
+
+ # Let Blender process UI events during occlusion
+ return {'RUNNING_MODAL'}
+
+ # ── Interactive visibility preview phase ─────────────────────────
+ if self._vis_preview_phase:
+ if event.type in {'ESC', 'RIGHTMOUSE'} and event.value == 'PRESS':
+ self._cleanup_vis_preview(context)
+ self.report({'WARNING'}, "Interactive visibility cancelled.")
+ return {'CANCELLED'}
+
+ changed = False
+ if event.type == 'WHEELUPMOUSE':
+ self._vis_balance = min(1.0, self._vis_balance + 0.05)
+ changed = True
+ elif event.type == 'WHEELDOWNMOUSE':
+ self._vis_balance = max(0.0, self._vis_balance - 0.05)
+ changed = True
+ elif event.type == 'NUMPAD_PLUS' and event.value == 'PRESS':
+ self._vis_balance = min(1.0, self._vis_balance + 0.05)
+ changed = True
+ elif event.type == 'NUMPAD_MINUS' and event.value == 'PRESS':
+ self._vis_balance = max(0.0, self._vis_balance - 0.05)
+ changed = True
+
+ if changed:
+ self._update_vis_cameras(context)
+ for area in context.screen.areas:
+ if area.type == 'VIEW_3D':
+ area.tag_redraw()
+ return {'RUNNING_MODAL'}
+
+ if event.type in {'RET', 'SPACE'} and event.value == 'PRESS':
+ # Confirm: finalize with current directions
+ self._vis_preview_phase = False
+ context.area.header_text_set(None)
+
+ # Delete preview cameras
+ for cam in list(self._cameras):
+ cam_data = cam.data
+ bpy.data.objects.remove(cam, do_unlink=True)
+ if cam_data and not cam_data.users:
+ bpy.data.cameras.remove(cam_data)
+ self._cameras.clear()
+
+ state = self._vis_state
+ self._finalize_auto_cameras(
+ context, self._vis_directions,
+ state['verts_world'], state['mesh_center'],
+ state['cam_settings'], state['temp_cam_data'])
+ self._vis_state = None
+ self._vis_count = None
+
+ # Transition to fly-through or finish
+ if self.review_placement:
+ if not self._start_fly_review(
+ context, add_modal_handler=False):
+ return {'CANCELLED'}
+ else:
+ self._finish_without_review(context)
+ return {'FINISHED'}
+ return {'RUNNING_MODAL'}
+
+ return {'RUNNING_MODAL'}
+
+ # ── Fly-through review phase ─────────────────────────────────────
+ if event.type == 'LEFTMOUSE' and event.value == 'RELEASE':
+ if time.time() - self._last_time < 0.2:
+ return {'RUNNING_MODAL'}
+
+ if event.type == 'TIMER':
+ fly_running = any(op.bl_idname == 'VIEW3D_OT_fly'
+ for w in context.window_manager.windows
+ for op in w.modal_operators)
+ if not fly_running:
+ self._camera_index += 1
+ if self._camera_index >= len(self._cameras):
+ context.window_manager.event_timer_remove(self._timer)
+ if AddCameras._draw_handle:
+ bpy.types.SpaceView3D.draw_handler_remove(AddCameras._draw_handle, 'WINDOW')
+ AddCameras._draw_handle = None
+ # Restore to initial camera, or first placed camera for auto modes
+ if self._initial_camera and self._initial_camera.name in [o.name for o in context.scene.objects]:
+ context.scene.camera = self._initial_camera
+ elif self._cameras:
+ context.scene.camera = self._cameras[0]
+ # Enable the floating prompt labels now that review is over
+ _sg_restore_label_overlay()
+ self.report({'INFO'}, "Cameras added successfully.")
+ return {'FINISHED'}
+ context.scene.camera = self._cameras[self._camera_index]
+ self._last_time = time.time()
+ bpy.ops.view3d.fly('INVOKE_DEFAULT')
+ return {'PASS_THROUGH'}
+
+ return {'PASS_THROUGH'}
+
+ @classmethod
+ def poll(cls, context):
+ if sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ return True
+
+ def invoke(self, context, event):
+ return context.window_manager.invoke_props_dialog(self)
+
+
+
\ No newline at end of file
diff --git a/stablegen/cameras/prompts.py b/stablegen/cameras/prompts.py
new file mode 100644
index 0000000..7772551
--- /dev/null
+++ b/stablegen/cameras/prompts.py
@@ -0,0 +1,364 @@
+"""Camera prompt items, ordering UI, and collection operators."""
+
+import math
+import bpy # pylint: disable=import-error
+import mathutils # pylint: disable=import-error
+import numpy as np
+from ..utils import get_dir_path, sg_modal_active
+from .operators import switch_viewport_to_camera
+from .overlays import _sg_ensure_label_overlay
+
+class CameraPromptItem(bpy.types.PropertyGroup):
+ name: bpy.props.StringProperty(
+ name="Camera Name",
+ description="Name of the camera object"
+ ) # type: ignore
+ prompt: bpy.props.StringProperty(
+ name="View Description",
+ description="Description of the view from this camera"
+ ) # type: ignore
+
+# ── Camera Generation Order ──────────────────────────────────────────────────
+
+class CameraOrderItem(bpy.types.PropertyGroup):
+ """One entry in the generation-order list. Stores the Blender camera name."""
+ name: bpy.props.StringProperty(
+ name="Camera Name",
+ description="Name of the camera object"
+ ) # type: ignore
+
+
+class SG_UL_CameraOrderList(bpy.types.UIList):
+ """UIList that shows cameras in their current generation order."""
+ bl_idname = "SG_UL_CameraOrderList"
+
+ def draw_item(self, _context, layout, _data, item, _icon,
+ _active_data, _active_propname, index):
+ cam_obj = bpy.data.objects.get(item.name)
+ if cam_obj is None:
+ layout.label(text=f"{item.name} (missing)", icon='ERROR')
+ return
+ # Show index, camera icon, name, and prompt if any
+ row = layout.row(align=True)
+ row.label(text=f"{index + 1}.")
+ row.label(text=item.name, icon='CAMERA_DATA')
+ # Show prompt preview if one exists
+ prompt_item = next(
+ (p for p in _context.scene.camera_prompts if p.name == item.name), None)
+ if prompt_item and prompt_item.prompt:
+ sub = row.row()
+ sub.scale_x = 1.5
+ sub.label(text=prompt_item.prompt, icon='SHORTDISPLAY')
+
+
+class SyncCameraOrder(bpy.types.Operator):
+ """Rebuild the generation order list from all cameras in the scene (sorted alphabetically)"""
+ bl_idname = "stablegen.sync_camera_order"
+ bl_label = "Sync Camera Order"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ @classmethod
+ def poll(cls, context):
+ return not sg_modal_active(context)
+
+ def execute(self, context):
+ scene = context.scene
+ cameras = sorted(
+ [obj for obj in scene.objects if obj.type == 'CAMERA'],
+ key=lambda x: x.name)
+ scene.sg_camera_order.clear()
+ for cam in cameras:
+ item = scene.sg_camera_order.add()
+ item.name = cam.name
+ scene.sg_camera_order_index = 0
+ self.report({'INFO'}, f"Synced {len(cameras)} cameras to generation order list.")
+ return {'FINISHED'}
+
+
+class MoveCameraOrder(bpy.types.Operator):
+ """Move the selected camera up or down in the generation order"""
+ bl_idname = "stablegen.move_camera_order"
+ bl_label = "Move Camera"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ direction: bpy.props.EnumProperty(
+ items=[('UP', "Up", ""), ('DOWN', "Down", "")],
+ name="Direction"
+ ) # type: ignore
+
+ @classmethod
+ def poll(cls, context):
+ return not sg_modal_active(context)
+
+ def execute(self, context):
+ scene = context.scene
+ idx = scene.sg_camera_order_index
+ total = len(scene.sg_camera_order)
+ if total < 2:
+ return {'CANCELLED'}
+ new_idx = idx - 1 if self.direction == 'UP' else idx + 1
+ if new_idx < 0 or new_idx >= total:
+ return {'CANCELLED'}
+ scene.sg_camera_order.move(idx, new_idx)
+ scene.sg_camera_order_index = new_idx
+ return {'FINISHED'}
+
+
+class ApplyCameraOrderPreset(bpy.types.Operator):
+ """Sort the generation order list using a preset strategy.
+ Cameras are first synced from the scene, then reordered."""
+ bl_idname = "stablegen.apply_camera_order_preset"
+ bl_label = "Apply Camera Order Preset"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ strategy: bpy.props.EnumProperty(
+ name="Strategy",
+ items=[
+ ('ALPHABETICAL', "Alphabetical (Original)",
+ "Sort cameras alphabetically by name (original order for auto-generated cameras)"),
+ ('FRONT_FIRST', "Front → Back → Sides",
+ "Front-facing cameras first, then back, then sides. "
+ "Prioritises the main view for sequential inpainting"),
+ ('BACK_FIRST', "Back → Front → Sides",
+ "Back-facing cameras first, then front, then sides"),
+ ('ALTERNATING', "Alternating (Opposites)",
+ "Alternate between opposing cameras (front, back, left, right…) "
+ "for maximum context spread between consecutive views"),
+ ('TOP_DOWN', "Top → Bottom",
+ "Cameras sorted from highest elevation to lowest"),
+ ('REVERSE', "Reverse Current",
+ "Reverse the current generation order"),
+ ],
+ default='FRONT_FIRST'
+ ) # type: ignore
+
+ @classmethod
+ def poll(cls, context):
+ return not sg_modal_active(context)
+
+ def execute(self, context):
+ scene = context.scene
+
+ # Always sync from scene first (picks up new/removed cameras)
+ cameras = sorted(
+ [obj for obj in scene.objects if obj.type == 'CAMERA'],
+ key=lambda x: x.name)
+
+ if not cameras:
+ self.report({'ERROR'}, "No cameras in the scene.")
+ return {'CANCELLED'}
+
+ if self.strategy == 'REVERSE':
+ # Reverse whatever is currently in the list
+ names = [item.name for item in scene.sg_camera_order]
+ if not names:
+ names = [c.name for c in cameras]
+ names.reverse()
+ elif self.strategy == 'ALPHABETICAL':
+ names = [c.name for c in cameras]
+ else:
+ # Compute mesh centre for direction classification
+ mesh_objects = [obj for obj in scene.objects
+ if obj.type == 'MESH' and not obj.hide_get()]
+ if mesh_objects:
+ centres = [obj.matrix_world @ (
+ sum((mathutils.Vector(v.co) for v in obj.data.vertices),
+ mathutils.Vector()) / max(len(obj.data.vertices), 1))
+ for obj in mesh_objects]
+ mesh_center = sum(centres, mathutils.Vector()) / len(centres)
+ else:
+ mesh_center = mathutils.Vector((0, 0, 0))
+
+ mc = np.array(mesh_center, dtype=float)
+
+ # Use the first camera (alphabetically) as the reference
+ # "front" direction — avoids assuming any world axis is front.
+ ref_pos = np.array(cameras[0].location, dtype=float)
+ ref_d = ref_pos - mc
+ ref_d_xy = ref_d[:2]
+ ref_xy_len = np.linalg.norm(ref_d_xy)
+ if ref_xy_len < 1e-8:
+ ref_d_xy = np.array([0.0, 1.0])
+ else:
+ ref_d_xy = ref_d_xy / ref_xy_len
+
+ # Compute direction angles for each camera
+ cam_data = []
+ for cam in cameras:
+ pos = np.array(cam.location, dtype=float)
+ d = pos - mc
+ norm = np.linalg.norm(d)
+ if norm < 1e-8:
+ d = np.array([0, 1, 0], dtype=float)
+ else:
+ d /= norm
+ # Elevation (angle above XY plane)
+ elev = math.degrees(math.asin(np.clip(d[2], -1, 1)))
+ # Azimuth relative to the first camera's XY direction
+ d_xy = d[:2]
+ d_xy_len = np.linalg.norm(d_xy)
+ if d_xy_len < 1e-8:
+ azimuth = 0.0
+ else:
+ d_xy = d_xy / d_xy_len
+ cos_a = float(np.clip(np.dot(d_xy, ref_d_xy), -1, 1))
+ cross = float(ref_d_xy[0] * d_xy[1]
+ - ref_d_xy[1] * d_xy[0])
+ azimuth = math.degrees(math.atan2(cross, cos_a))
+ cam_data.append((cam.name, elev, azimuth))
+
+ if self.strategy == 'FRONT_FIRST':
+ # Priority: front (small |azimuth|), then back (large |azimuth|), then sides
+ def _front_key(item):
+ _name, _elev, az = item
+ abs_az = abs(az)
+ if abs_az <= 45:
+ bucket = 0 # front
+ elif abs_az >= 135:
+ bucket = 1 # back
+ else:
+ bucket = 2 # sides
+ return (bucket, abs_az, -_elev)
+ cam_data.sort(key=_front_key)
+
+ elif self.strategy == 'BACK_FIRST':
+ def _back_key(item):
+ _name, _elev, az = item
+ abs_az = abs(az)
+ if abs_az >= 135:
+ bucket = 0 # back
+ elif abs_az <= 45:
+ bucket = 1 # front
+ else:
+ bucket = 2 # sides
+ return (bucket, -abs_az, -_elev)
+ cam_data.sort(key=_back_key)
+
+ elif self.strategy == 'ALTERNATING':
+ # Sort by azimuth, then pick from alternating ends
+ cam_data.sort(key=lambda x: x[2])
+ alternated = []
+ left, right = 0, len(cam_data) - 1
+ pick_left = True
+ while left <= right:
+ if pick_left:
+ alternated.append(cam_data[left])
+ left += 1
+ else:
+ alternated.append(cam_data[right])
+ right -= 1
+ pick_left = not pick_left
+ cam_data = alternated
+
+ elif self.strategy == 'TOP_DOWN':
+ cam_data.sort(key=lambda x: -x[1]) # highest elevation first
+
+ names = [item[0] for item in cam_data]
+
+ # Apply to collection
+ scene.sg_camera_order.clear()
+ for n in names:
+ item = scene.sg_camera_order.add()
+ item.name = n
+ scene.sg_camera_order_index = 0
+ self.report({'INFO'},
+ f"Applied '{self.strategy}' order to {len(names)} cameras.")
+ return {'FINISHED'}
+
+
+class CollectCameraPrompts(bpy.types.Operator):
+ """Collect viewpoint description prompts for the selected cameras (or all cameras if none selected).
+
+ - These prompts will be appended before the main prompt for each camera generation.
+ - Applicable for separate, sequential and refine (also within grid mode) modes.
+ - Select one or more cameras in the viewport to only prompt for those, or run with no selection to cycle through all cameras.
+ - Examples: 'front view', 'close-up on face', 'from above'."""
+ bl_idname = "object.collect_camera_prompts"
+ bl_label = "Collect Camera View Prompts"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ camera_prompt: bpy.props.StringProperty(
+ name="View Description",
+ description="Describe the view from this camera (e.g., 'front view', 'close-up on face', 'from above')",
+ default=""
+ ) # type: ignore
+
+ # Internal state
+ _cameras: list = []
+ _camera_index: int = 0
+
+ @classmethod
+ def poll(cls, context):
+ if sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ if not any(obj.type == 'CAMERA' for obj in context.scene.objects):
+ cls.poll_message_set("No cameras in the scene — use 'Add Cameras' first")
+ return False
+ return True
+
+ def invoke(self, context, event):
+ # Use selected cameras if any are selected, otherwise fall back to all
+ selected_cams = sorted(
+ [obj for obj in context.selected_objects if obj.type == 'CAMERA'],
+ key=lambda x: x.name,
+ )
+ if selected_cams:
+ self._cameras = selected_cams
+ else:
+ self._cameras = sorted(
+ [obj for obj in context.scene.objects if obj.type == 'CAMERA'],
+ key=lambda x: x.name,
+ )
+
+ if not self._cameras:
+ self.report({'ERROR'}, "No cameras found in the scene.")
+ return {'CANCELLED'}
+
+ # Initialize state
+ self._camera_index = 0
+
+ # Set the first camera and pre-fill prompt if exists
+ current_cam = self._cameras[self._camera_index]
+
+ # Find existing prompt or set default
+ existing_item = next((item for item in context.scene.camera_prompts if item.name == current_cam.name), None)
+ self.camera_prompt = existing_item.prompt if existing_item else ""
+
+ context.scene.camera = current_cam
+ switch_viewport_to_camera(context, current_cam) # Switch viewport
+ return context.window_manager.invoke_props_dialog(self, width=400)
+
+ def draw(self, context):
+ layout = self.layout
+ if self._camera_index < len(self._cameras):
+ layout.label(text=f"Camera: {self._cameras[self._camera_index].name} ({self._camera_index + 1}/{len(self._cameras)})")
+ layout.prop(self, "camera_prompt")
+
+ def execute(self, context):
+ cam_name = self._cameras[self._camera_index].name
+
+ # Find existing item or add a new one
+ prompt_item = next((item for item in context.scene.camera_prompts if item.name == cam_name), None)
+ if not prompt_item:
+ prompt_item = context.scene.camera_prompts.add()
+ prompt_item.name = cam_name
+
+ prompt_item.prompt = self.camera_prompt.strip() # Store trimmed prompt
+
+ self._camera_index += 1
+ if self._camera_index < len(self._cameras):
+ next_cam = self._cameras[self._camera_index]
+ # Pre-fill next prompt
+ existing_item = next((item for item in context.scene.camera_prompts if item.name == next_cam.name), None)
+ self.camera_prompt = existing_item.prompt if existing_item else ""
+ # Ensure scene camera is set for next dialog and switch view
+ context.scene.camera = next_cam
+ switch_viewport_to_camera(context, next_cam) # Switch viewport
+ return context.window_manager.invoke_props_dialog(self, width=400) # Show next dialog
+ else:
+ # Refresh the floating labels so edited prompts appear
+ _sg_ensure_label_overlay()
+ self.report({'INFO'}, f"Collected prompts for {len(self._cameras)} cameras.")
+ return {'FINISHED'}
+
\ No newline at end of file
diff --git a/stablegen/core/__init__.py b/stablegen/core/__init__.py
new file mode 100644
index 0000000..10c34e7
--- /dev/null
+++ b/stablegen/core/__init__.py
@@ -0,0 +1,17 @@
+"""Core infrastructure for the StableGen addon."""
+
+# The top-level addon package name, used for bpy preferences lookup.
+# Modules in sub-packages must use this instead of their own __package__.
+ADDON_PKG = __name__.rsplit('.', 1)[0] # "stablegen" or "bl_ext.user_default.stablegen"
+
+
+def get_addon_prefs(context=None):
+ """Return the StableGenAddonPreferences instance.
+
+ Convenience wrapper so every module doesn't need to know
+ the addon package name or handle KeyError.
+ """
+ import bpy # noqa: delayed import – safe for Blender
+ ctx = context or bpy.context
+ wrapper = ctx.preferences.addons.get(ADDON_PKG)
+ return wrapper.preferences if wrapper else None
diff --git a/stablegen/core/callbacks.py b/stablegen/core/callbacks.py
new file mode 100644
index 0000000..aea7de9
--- /dev/null
+++ b/stablegen/core/callbacks.py
@@ -0,0 +1,166 @@
+"""Architecture-mode and server-address callbacks.
+
+Property ``update=`` callbacks that react to architecture / mode changes
+and the server address preference. Extracted from the original
+``__init__.py`` to keep it slim.
+"""
+
+import bpy # pylint: disable=import-error
+from urllib.parse import urlparse
+
+from ..ui.presets import update_parameters
+from . import ADDON_PKG
+from .state import (
+ _cached_checkpoint_list,
+ _cached_lora_list,
+ _run_async,
+)
+from .server_api import (
+ check_pbr_available,
+ check_server_availability,
+ check_trellis2_available,
+)
+from ..timeout_config import get_timeout
+
+
+# ── Architecture-mode helpers ──────────────────────────────────────────────
+
+def update_architecture_mode(self, context):
+ """Called when the user changes the architecture_mode dropdown."""
+ scene = context.scene
+ mode = scene.architecture_mode
+
+ if mode != 'trellis2':
+ if scene.model_architecture != mode:
+ scene.model_architecture = mode # triggers update_combined
+ else:
+ _sync_trellis2_backbone(scene)
+
+ update_parameters(self, context)
+
+
+def _sync_trellis2_backbone(scene):
+ """Pick the right diffusion backbone for the current TRELLIS.2 state."""
+ tex_mode = getattr(scene, 'trellis2_texture_mode', 'native')
+ if tex_mode in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein'):
+ target = tex_mode
+ elif getattr(scene, 'trellis2_generate_from', 'image') == 'prompt':
+ target = getattr(scene, 'trellis2_initial_image_arch', 'sdxl')
+ else:
+ return
+ if scene.model_architecture != target:
+ scene.model_architecture = target
+
+
+def update_trellis2_texture_mode(self, context):
+ """Called when the user changes the texture generation mode inside TRELLIS.2."""
+ scene = context.scene
+ if getattr(scene, 'architecture_mode', '') != 'trellis2':
+ return
+
+ tex_mode = scene.trellis2_texture_mode
+ scene.trellis2_skip_texture = (tex_mode != 'native')
+
+ _sync_trellis2_backbone(scene)
+ update_parameters(self, context)
+
+
+def update_trellis2_initial_image_arch(self, context):
+ """Called when the user changes the initial-image architecture for TRELLIS.2."""
+ scene = context.scene
+ if getattr(scene, 'architecture_mode', '') != 'trellis2':
+ return
+ _sync_trellis2_backbone(scene)
+ update_parameters(self, context)
+
+
+def update_trellis2_generate_from(self, context):
+ """Called when the user switches between Image and Prompt input mode."""
+ scene = context.scene
+ if getattr(scene, 'architecture_mode', '') != 'trellis2':
+ return
+
+ if scene.trellis2_generate_from == 'prompt':
+ _sync_trellis2_backbone(scene)
+
+ update_parameters(self, context)
+
+
+# ── Server-address change callback ────────────────────────────────────────
+
+def update_combined(self, context):
+ """Master callback for server_address changes — pings server, refreshes models."""
+ # Import lazily to avoid circular ref at module level
+ from .load_handlers import load_handler
+
+ prefs = context.preferences.addons[ADDON_PKG].preferences
+ raw_address = prefs.server_address
+
+ if raw_address:
+ if not raw_address.startswith(('http://', 'https://')):
+ parsed_url = urlparse(f"http://{raw_address}")
+ else:
+ parsed_url = urlparse(raw_address)
+
+ clean_address = parsed_url.netloc
+
+ if clean_address and raw_address != clean_address:
+ prefs.server_address = clean_address
+ return None
+
+ server_address = prefs.server_address
+
+ if not server_address:
+ prefs.server_online = False
+ # Must mutate the lists in-place to keep existing references valid
+ import stablegen.core.state as _state
+ _state._cached_checkpoint_list = [("NO_SERVER", "Set Server Address", "...")]
+ _state._cached_lora_list = [("NO_SERVER", "Set Server Address", "...")]
+ return None
+
+ print("[StableGen] Server address changed, checking asynchronously...")
+
+ def _bg_work():
+ result = {}
+ result['online'] = check_server_availability(server_address, timeout=get_timeout('ping'))
+ if result['online']:
+ result['trellis2'] = check_trellis2_available(server_address, timeout=get_timeout('api'))
+ result['pbr'] = check_pbr_available(server_address, timeout=get_timeout('api'))
+ else:
+ result['trellis2'] = False
+ result['pbr'] = False
+ return result
+
+ def _on_done(result):
+ if result is None:
+ return
+ _prefs = bpy.context.preferences.addons[ADDON_PKG].preferences
+ _prefs.server_online = result.get('online', False)
+
+ if hasattr(bpy.context, 'scene') and bpy.context.scene:
+ bpy.context.scene.trellis2_available = result.get('trellis2', False)
+ bpy.context.scene.pbr_nodes_available = result.get('pbr', False)
+
+ if not result.get('online', False):
+ print("[StableGen] ComfyUI server is not reachable.")
+ return
+
+ update_parameters(None, bpy.context)
+ load_handler(None)
+
+ def _deferred_refresh():
+ try:
+ bpy.ops.stablegen.refresh_checkpoint_list('INVOKE_DEFAULT')
+ bpy.ops.stablegen.refresh_lora_list('INVOKE_DEFAULT')
+ bpy.ops.stablegen.refresh_controlnet_mappings('INVOKE_DEFAULT')
+ except Exception as e:
+ print(f"[StableGen] Error during deferred refresh: {e}")
+ return None
+ bpy.app.timers.register(_deferred_refresh, first_interval=0.1)
+
+ _run_async(_bg_work, _on_done, track_generation=True)
+
+ update_parameters(self, context)
+ load_handler(None)
+
+ return None
diff --git a/stablegen/core/load_handlers.py b/stablegen/core/load_handlers.py
new file mode 100644
index 0000000..440f5a3
--- /dev/null
+++ b/stablegen/core/load_handlers.py
@@ -0,0 +1,131 @@
+"""Persistent load_post handler for scene defaults and cache sync.
+
+The ``load_handler`` function runs on every .blend file load to:
+- set default ControlNet / LoRA units
+- re-register crop overlays
+- trigger a checkpoint cache refresh if the architecture changed
+- re-check TRELLIS.2 and PBR node availability (async)
+"""
+
+import bpy # pylint: disable=import-error
+from bpy.app.handlers import persistent
+
+from . import ADDON_PKG
+
+
+@persistent
+def load_handler(dummy):
+ """Set default ControlNet/LoRA units and sync checkpoint cache on file load."""
+ from ..ui.model_units import get_lora_models
+ from . import state as _state
+
+ if not bpy.context.scene:
+ return
+
+ scene = bpy.context.scene
+ addon_prefs = bpy.context.preferences.addons[ADDON_PKG].preferences
+
+ # Re-register aspect-ratio crop overlays
+ try:
+ from ..cameras.overlays import _sg_ensure_crop_overlay
+ for obj in scene.objects:
+ if obj.type == 'CAMERA' and 'sg_display_crop' in obj:
+ _sg_ensure_crop_overlay()
+ break
+ except Exception:
+ pass
+
+ # Default ControlNet unit
+ if hasattr(scene, "controlnet_units") and not scene.controlnet_units:
+ default_unit = scene.controlnet_units.add()
+ default_unit.unit_type = 'depth'
+
+ # Default LoRA unit
+ if hasattr(scene, "lora_units") and not scene.lora_units:
+ default_lora_filename_to_find = None
+ model_strength = 1.0
+ clip_strength = 1.0
+
+ if scene.model_architecture == 'sdxl':
+ default_lora_filename_to_find = 'sdxl_lightning_8step_lora.safetensors'
+ elif scene.model_architecture == 'qwen_image_edit':
+ default_lora_filename_to_find = 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors'
+ clip_strength = 0.0
+
+ if not default_lora_filename_to_find:
+ pass # No default LoRA for this architecture; skip LoRA setup
+ else:
+ all_available_loras_enums = get_lora_models(scene, bpy.context)
+
+ found_lora_identifier_to_load = None
+ for identifier, name, description in all_available_loras_enums:
+ if identifier.endswith(default_lora_filename_to_find):
+ if identifier not in ("NONE_AVAILABLE", "NO_COMFYUI_DIR_LORA", "NO_LORAS_SUBDIR",
+ "PERM_ERROR", "SCAN_ERROR", "NONE_FOUND"):
+ found_lora_identifier_to_load = identifier
+ break
+
+ if found_lora_identifier_to_load:
+ new_lora_unit = None
+ try:
+ new_lora_unit = scene.lora_units.add()
+ new_lora_unit.model_name = found_lora_identifier_to_load
+ new_lora_unit.model_strength = model_strength
+ new_lora_unit.clip_strength = clip_strength
+ except TypeError:
+ print(f"[StableGen] StableGen Load Handler: TypeError setting default LoRA "
+ f"'{found_lora_identifier_to_load}'. Enum items might not be fully ready.")
+ if new_lora_unit and scene.lora_units and new_lora_unit == scene.lora_units[-1]:
+ scene.lora_units.remove(len(scene.lora_units) - 1)
+ except Exception as e:
+ print(f"[StableGen] StableGen Load Handler: Unexpected error setting default LoRA "
+ f"'{found_lora_identifier_to_load}': {e}")
+ if new_lora_unit and scene.lora_units and new_lora_unit == scene.lora_units[-1]:
+ scene.lora_units.remove(len(scene.lora_units) - 1)
+
+ # Ensure checkpoint cache matches the scene architecture that just loaded
+ current_architecture = getattr(scene, "model_architecture", None)
+ prefs_wrapper = bpy.context.preferences.addons.get(ADDON_PKG)
+ if current_architecture and prefs_wrapper:
+ prefs = prefs_wrapper.preferences
+ if (prefs.server_address
+ and current_architecture != _state._cached_checkpoint_architecture
+ and _state._pending_checkpoint_refresh_architecture != current_architecture):
+
+ def _refresh_checkpoint_for_architecture():
+ try:
+ bpy.ops.stablegen.refresh_checkpoint_list('INVOKE_DEFAULT')
+ except Exception as timer_error:
+ print(f"[StableGen] StableGen Load Handler: Failed to refresh checkpoints for "
+ f"'{current_architecture}': {timer_error}")
+ finally:
+ _state._pending_checkpoint_refresh_architecture = None
+ return None
+
+ _state._pending_checkpoint_refresh_architecture = current_architecture
+ bpy.app.timers.register(_refresh_checkpoint_for_architecture, first_interval=0.2)
+
+ # Re-check TRELLIS.2 and PBR node availability asynchronously.
+ # Scene properties reset to False on file load; this restores them.
+ if prefs_wrapper:
+ server_address = prefs_wrapper.preferences.server_address
+ if server_address:
+ from .server_api import check_trellis2_available, check_pbr_available
+ from ..timeout_config import get_timeout
+
+ _timeout = get_timeout('api')
+
+ def _check_nodes():
+ result = {}
+ result['trellis2'] = check_trellis2_available(server_address, timeout=_timeout)
+ result['pbr'] = check_pbr_available(server_address, timeout=_timeout)
+ return result
+
+ def _apply_node_result(result):
+ if result is None:
+ return
+ if hasattr(bpy.context, 'scene') and bpy.context.scene:
+ bpy.context.scene.trellis2_available = result.get('trellis2', False)
+ bpy.context.scene.pbr_nodes_available = result.get('pbr', False)
+
+ _state._run_async(_check_nodes, _apply_node_result)
diff --git a/stablegen/core/preferences.py b/stablegen/core/preferences.py
new file mode 100644
index 0000000..2510235
--- /dev/null
+++ b/stablegen/core/preferences.py
@@ -0,0 +1,394 @@
+"""Addon preferences, server-status operator, and ControlNet mapping UI.
+
+Contains:
+- ``ControlNetModelMappingItem`` — PropertyGroup for CN model ↔ type mapping
+- ``StableGenAddonPreferences`` — the addon's Preferences panel
+- ``CheckServerStatus`` — operator that pings ComfyUI
+- ``STABLEGEN_UL_ControlNetMappingList`` — UIList for CN model table
+- ``RefreshControlNetMappings`` — operator to refresh CN models from server
+"""
+
+import bpy # pylint: disable=import-error
+
+from ..ui.presets import update_parameters
+from ..utils import sg_modal_active
+from . import ADDON_PKG
+from .callbacks import update_combined
+from .server_api import (
+ _fetch_api_list,
+ check_pbr_available,
+ check_server_availability,
+ check_trellis2_available,
+)
+from .state import (
+ _dec_pending_refreshes,
+ _inc_pending_refreshes,
+ _run_async,
+)
+from ..timeout_config import get_timeout
+
+
+# ── PropertyGroup for a single ControlNet model mapping ────────────────────
+
+class ControlNetModelMappingItem(bpy.types.PropertyGroup):
+ """Stores info about a detected ControlNet model and its supported types."""
+ name: bpy.props.StringProperty(name="Model Filename") # type: ignore
+
+ supports_depth: bpy.props.BoolProperty(
+ name="Depth",
+ description="Check if this model supports Depth guidance",
+ default=False
+ ) # type: ignore
+ supports_canny: bpy.props.BoolProperty(
+ name="Canny",
+ description="Check if this model supports Canny/Edge guidance",
+ default=False
+ ) # type: ignore
+ supports_normal: bpy.props.BoolProperty(
+ name="Normal",
+ description="Check if this model supports Normal map guidance",
+ default=False
+ ) # type: ignore
+
+
+# ── Addon Preferences ─────────────────────────────────────────────────────
+
+class StableGenAddonPreferences(bpy.types.AddonPreferences):
+ """Preferences for the StableGen addon."""
+ bl_idname = ADDON_PKG
+
+ server_address: bpy.props.StringProperty(
+ name="Server Address",
+ description="Address of the ComfyUI server",
+ default="127.0.0.1:8188",
+ update=update_combined
+ ) # type: ignore
+
+ output_dir: bpy.props.StringProperty(
+ name="Output Directory",
+ description="Directory to save generated outputs",
+ default="",
+ subtype='DIR_PATH',
+ update=update_parameters
+ ) # type: ignore
+
+ controlnet_model_mappings: bpy.props.CollectionProperty(
+ type=ControlNetModelMappingItem,
+ name="ControlNet Model Mappings"
+ ) # type: ignore
+
+ save_blend_file: bpy.props.BoolProperty(
+ name="Save Blend File",
+ description="Save the current Blender file with packed textures",
+ default=False,
+ update=update_parameters
+ ) # type: ignore
+
+ controlnet_mapping_index: bpy.props.IntProperty(
+ default=0, name="Active ControlNet Mapping Index"
+ ) # type: ignore
+
+ server_online: bpy.props.BoolProperty(
+ name="Server Online",
+ description="Indicates if the ComfyUI server is reachable",
+ default=False
+ ) # type: ignore
+
+ overlay_color: bpy.props.FloatVectorProperty(
+ name="Overlay Color",
+ description="Color used for the camera aspect-ratio crop rectangle and floating view labels in the viewport",
+ subtype='COLOR',
+ size=3,
+ min=0.0, max=1.0,
+ default=(0.3, 0.5, 1.0),
+ update=lambda self, ctx: [a.tag_redraw() for a in ctx.screen.areas if a.type == 'VIEW_3D'] if ctx.screen else None
+ ) # type: ignore
+
+ enable_debug: bpy.props.BoolProperty(
+ name="Enable Debug Settings",
+ description="Show diagnostic tools in the main panel for visualising projection weights, blending and coverage",
+ default=False
+ ) # type: ignore
+
+ show_advanced: bpy.props.BoolProperty(
+ name="Advanced Preferences",
+ description="Show advanced preferences",
+ default=False
+ ) # type: ignore
+
+ timeout_ping: bpy.props.FloatProperty(
+ name="Ping Timeout (s)",
+ description="Timeout for quick server availability checks (connectivity pings)",
+ default=1.0, min=0.1, max=30.0, step=10, precision=1,
+ ) # type: ignore
+
+ timeout_api: bpy.props.FloatProperty(
+ name="API Timeout (s)",
+ description="Timeout for standard API requests (VRAM flush, queue prompt, server info)",
+ default=10.0, min=1.0, max=120.0, step=100, precision=0,
+ ) # type: ignore
+
+ timeout_transfer: bpy.props.FloatProperty(
+ name="Transfer Timeout (s)",
+ description="Timeout for large data transfers (image upload, file download)",
+ default=120.0, min=10.0, max=600.0, step=100, precision=0,
+ ) # type: ignore
+
+ timeout_reboot: bpy.props.FloatProperty(
+ name="Reboot Timeout (s)",
+ description="How long to wait for ComfyUI server to come back after a reboot",
+ default=120.0, min=10.0, max=600.0, step=100, precision=0,
+ ) # type: ignore
+
+ timeout_mesh_gen: bpy.props.FloatProperty(
+ name="Mesh Generation Timeout (s)",
+ description="Timeout for TRELLIS.2 mesh generation WebSocket. "
+ "Mesh simplification / post-processing can take several "
+ "minutes without sending progress messages",
+ default=600.0, min=60.0, max=3600.0, step=100, precision=0,
+ ) # type: ignore
+
+ def draw(self, context):
+ layout = self.layout
+ layout.prop(self, "output_dir")
+ row = layout.row(align=True)
+ row.prop(self, "server_address")
+ row.operator("stablegen.check_server_status", text="", icon='FILE_REFRESH')
+
+ layout.prop(self, "save_blend_file")
+
+ layout.separator()
+
+ box = layout.box()
+ row = box.row()
+ row.label(text="ControlNet Model Assignments:")
+ row.operator("stablegen.refresh_controlnet_mappings", text="", icon='FILE_REFRESH')
+
+ if not self.controlnet_model_mappings:
+ box.label(text="No models found or list not refreshed.", icon='INFO')
+ else:
+ rows = max(1, min(len(self.controlnet_model_mappings), 5))
+ box.template_list(
+ "STABLEGEN_UL_ControlNetMappingList",
+ "",
+ self,
+ "controlnet_model_mappings",
+ self,
+ "controlnet_mapping_index",
+ rows=rows
+ )
+
+ layout.separator()
+ adv_box = layout.box()
+ row = adv_box.row()
+ row.prop(self, "show_advanced",
+ icon='TRIA_DOWN' if self.show_advanced else 'TRIA_RIGHT',
+ emboss=False, text="Advanced Preferences")
+ if self.show_advanced:
+ adv_box.prop(self, "overlay_color")
+ adv_box.prop(self, "enable_debug")
+
+ adv_box.separator()
+ adv_box.label(text="Timeouts:", icon="TIME")
+ col = adv_box.column(align=True)
+ col.prop(self, "timeout_ping")
+ col.prop(self, "timeout_api")
+ col.prop(self, "timeout_transfer")
+ col.prop(self, "timeout_reboot")
+ col.prop(self, "timeout_mesh_gen")
+
+
+# ── Server-status check operator ──────────────────────────────────────────
+
+class CheckServerStatus(bpy.types.Operator):
+ """Checks if the ComfyUI server is reachable."""
+ bl_idname = "stablegen.check_server_status"
+ bl_label = "Check Server Status"
+ bl_description = "Ping the ComfyUI server to check connectivity"
+
+ @classmethod
+ def poll(cls, context):
+ prefs = context.preferences.addons.get(ADDON_PKG)
+ if not prefs or not prefs.preferences.server_address:
+ cls.poll_message_set("Server address not configured (check addon preferences)")
+ return False
+ if sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ return True
+
+ def execute(self, context):
+ from .load_handlers import load_handler
+
+ prefs = context.preferences.addons[ADDON_PKG].preferences
+ server_addr = prefs.server_address
+
+ print(f"[StableGen] Checking server status at {server_addr}...")
+
+ def _bg_work():
+ result = {}
+ result['online'] = check_server_availability(server_addr, timeout=get_timeout('ping'))
+ if result['online']:
+ result['trellis2'] = check_trellis2_available(server_addr, timeout=get_timeout('api'))
+ result['pbr'] = check_pbr_available(server_addr, timeout=get_timeout('api'))
+ else:
+ result['trellis2'] = False
+ result['pbr'] = False
+ return result
+
+ def _on_done(result):
+ if result is None:
+ return
+ _prefs = bpy.context.preferences.addons[ADDON_PKG].preferences
+ _prefs.server_online = result.get('online', False)
+
+ if hasattr(bpy.context, 'scene') and bpy.context.scene:
+ bpy.context.scene.trellis2_available = result.get('trellis2', False)
+ bpy.context.scene.pbr_nodes_available = result.get('pbr', False)
+
+ if result.get('online', False):
+ print(f"[StableGen] ComfyUI server is online at {server_addr}.")
+ load_handler(None)
+ def _deferred_refresh():
+ try:
+ bpy.ops.stablegen.refresh_checkpoint_list('INVOKE_DEFAULT')
+ bpy.ops.stablegen.refresh_lora_list('INVOKE_DEFAULT')
+ bpy.ops.stablegen.refresh_controlnet_mappings('INVOKE_DEFAULT')
+ except Exception as e:
+ print(f"[StableGen] Error during deferred refresh: {e}")
+ return None
+ bpy.app.timers.register(_deferred_refresh, first_interval=0.1)
+ else:
+ print(f"[StableGen] ComfyUI server unreachable or timed out at {server_addr}.")
+
+ _run_async(_bg_work, _on_done, track_generation=True)
+ self.report({'INFO'}, f"Checking server at {server_addr}...")
+
+ return {'FINISHED'}
+
+
+# ── ControlNet mapping UIList ──────────────────────────────────────────────
+
+class STABLEGEN_UL_ControlNetMappingList(bpy.types.UIList):
+ """UIList for displaying ControlNet model mappings."""
+ def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
+ if self.layout_type in {'DEFAULT', 'COMPACT'}:
+ split = layout.split(factor=0.65)
+ col_name = split.column(align=True)
+ col_checks = split.column(align=True)
+
+ col_name.prop(item, "name", text="", emboss=False)
+
+ row = col_checks.row(align=True)
+ row.prop(item, "supports_depth", text="Depth", toggle=True)
+ row.prop(item, "supports_canny", text="Canny", toggle=True)
+ row.prop(item, "supports_normal", text="Normal", toggle=True)
+
+ elif self.layout_type == 'GRID':
+ layout.alignment = 'CENTER'
+ layout.label(text="", icon_value=icon)
+
+
+# ── Refresh ControlNet mappings operator ───────────────────────────────────
+
+class RefreshControlNetMappings(bpy.types.Operator):
+ """Fetches ControlNet models from ComfyUI API and updates the mapping list."""
+ bl_idname = "stablegen.refresh_controlnet_mappings"
+ bl_label = "Refresh ControlNet Model List"
+ bl_description = "Connect to ComfyUI server to get ControlNet models and update assignments"
+
+ @classmethod
+ def poll(cls, context):
+ prefs = context.preferences.addons.get(ADDON_PKG)
+ if not prefs or not prefs.preferences.server_address:
+ cls.poll_message_set("Server address not configured (check addon preferences)")
+ return False
+ if sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ return True
+
+ def execute(self, context):
+ prefs = context.preferences.addons.get(ADDON_PKG)
+ if not prefs:
+ self.report({'ERROR'}, "Cannot access addon preferences.")
+ return {'CANCELLED'}
+
+ server_address = prefs.preferences.server_address
+
+ existing_model_names = set()
+ for item in prefs.preferences.controlnet_model_mappings:
+ existing_model_names.add(item.name)
+
+ def _bg_work():
+ server_models = _fetch_api_list(server_address, "/models/controlnet")
+ return {'server_models': server_models,
+ 'existing_names': existing_model_names}
+
+ def _on_done(result):
+ _dec_pending_refreshes()
+ if result is None:
+ return
+ server_models = result.get('server_models')
+ existing_names = result.get('existing_names', set())
+
+ _prefs = bpy.context.preferences.addons.get(ADDON_PKG)
+ if not _prefs:
+ return
+ mappings = _prefs.preferences.controlnet_model_mappings
+
+ if server_models is None:
+ print("[StableGen] ControlNet refresh: cannot reach server.")
+ return
+
+ if not server_models:
+ print("[StableGen] ControlNet refresh: no models found, clearing list.")
+ mappings.clear()
+ else:
+ server_set = set(server_models)
+ current_set = set(item.name for item in mappings)
+
+ models_to_remove = current_set - server_set
+ indices_to_remove = []
+ for i, item in enumerate(mappings):
+ if item.name in models_to_remove:
+ indices_to_remove.append(i)
+ for i in sorted(indices_to_remove, reverse=True):
+ mappings.remove(i)
+ if _prefs.preferences.controlnet_mapping_index >= len(mappings):
+ _prefs.preferences.controlnet_mapping_index = max(0, len(mappings) - 1)
+
+ models_to_add = server_set - current_set
+ for model_name in sorted(models_to_add):
+ new_item = mappings.add()
+ new_item.name = model_name
+ name_lower = model_name.lower()
+ is_union = 'union' in name_lower or 'promax' in name_lower
+ if is_union:
+ new_item.supports_depth = True
+ new_item.supports_canny = True
+ new_item.supports_normal = True
+ print(f"[StableGen] Guessed '{model_name}' as Union (Depth, Canny, Normal).")
+ else:
+ if 'depth' in name_lower:
+ new_item.supports_depth = True
+ print(f"[StableGen] Guessed '{model_name}' as Depth.")
+ if 'canny' in name_lower or 'lineart' in name_lower or 'scribble' in name_lower:
+ new_item.supports_canny = True
+ print(f"[StableGen] Guessed '{model_name}' as Canny.")
+ if 'normal' in name_lower:
+ new_item.supports_normal = True
+ print(f"[StableGen] Guessed '{model_name}' as Normal.")
+ if not (new_item.supports_depth or new_item.supports_canny or new_item.supports_normal):
+ print(f"[StableGen] Could not guess type for '{model_name}'. Please assign manually.")
+
+ print(f"[StableGen] ControlNet refresh: {len(models_to_add)} added, {len(models_to_remove)} removed.")
+
+ for window in bpy.context.window_manager.windows:
+ for area in window.screen.areas:
+ area.tag_redraw()
+
+ _run_async(_bg_work, _on_done)
+ _inc_pending_refreshes()
+ self.report({'INFO'}, "Fetching ControlNet models...")
+ return {'FINISHED'}
diff --git a/stablegen/core/properties.py b/stablegen/core/properties.py
new file mode 100644
index 0000000..40a09b9
--- /dev/null
+++ b/stablegen/core/properties.py
@@ -0,0 +1,1626 @@
+"""Scene and WindowManager property definitions for the StableGen addon.
+
+All ``bpy.types.Scene.*`` and ``bpy.types.WindowManager.*`` properties that
+were previously defined inside the monolithic ``register()`` function are
+collected here in ``register_properties()`` and ``unregister_properties()``.
+"""
+
+import bpy # pylint: disable=import-error
+
+from ..ui.presets import update_parameters, get_preset_items
+from ..cameras.prompts import CameraPromptItem, CameraOrderItem
+from .callbacks import (
+ update_architecture_mode,
+ update_combined,
+ update_trellis2_generate_from,
+ update_trellis2_initial_image_arch,
+ update_trellis2_texture_mode,
+)
+from . import ADDON_PKG
+
+
+# ── Helper(s) used by properties ───────────────────────────────────────────
+
+def _get_ipadapter_mode_items(self, context):
+ items = [
+ ('first', 'Use first generated image', '', 0),
+ ('recent', 'Use most recent generated image', '', 1),
+ ]
+ if context:
+ is_local_edit = (
+ context.scene.generation_method == 'local_edit'
+ or (context.scene.model_architecture.startswith('qwen')
+ and context.scene.qwen_generation_method == 'local_edit')
+ )
+ if is_local_edit:
+ items.append(('original_render', 'Use original render',
+ 'Uses the existing texture render from each camera viewpoint as IPAdapter reference', 2))
+ if getattr(context.scene, 'architecture_mode', '') == 'trellis2':
+ items.append(('trellis2_input', 'Use TRELLIS.2 input image',
+ 'Uses the input image from TRELLIS.2 mesh generation as IPAdapter reference', 3))
+ return items
+
+
+# ── Registration ───────────────────────────────────────────────────────────
+
+def register_properties(update_model_list, ControlNetUnit, LoRAUnit,
+ SceneQueueItem, load_handler, _sg_queue_load_handler,
+ _sg_queue_load):
+ """Register all Scene / WindowManager properties and load_post handlers.
+
+ Parameters are passed in to avoid circular imports — the caller
+ (``__init__.register``) already has them resolved.
+ """
+
+ # --- Scene Queue properties (on WindowManager so they're global) ---
+ bpy.types.WindowManager.sg_scene_queue = bpy.props.CollectionProperty(
+ type=SceneQueueItem, name="Scene Queue"
+ )
+ bpy.types.WindowManager.sg_scene_queue_index = bpy.props.IntProperty(
+ name="Queue Index", default=0
+ )
+ bpy.types.WindowManager.sg_show_queue = bpy.props.BoolProperty(
+ name="Show Queue", default=False
+ )
+
+ # --- Queue GIF Export settings ---
+ bpy.types.WindowManager.sg_queue_gif_export = bpy.props.BoolProperty(
+ name="Export GIF After Each Item", default=False,
+ description="Automatically export an orbit GIF/MP4 after each queue item completes"
+ )
+ bpy.types.WindowManager.sg_queue_gif_duration = bpy.props.FloatProperty(
+ name="Duration (s)", default=5.0, min=0.1, max=60.0,
+ description="Duration of the 360-degree orbit"
+ )
+ bpy.types.WindowManager.sg_queue_gif_fps = bpy.props.IntProperty(
+ name="FPS", default=24, min=1, max=60,
+ description="Frames per second"
+ )
+ bpy.types.WindowManager.sg_queue_gif_resolution = bpy.props.IntProperty(
+ name="Resolution %", default=50, min=10, max=100, subtype='PERCENTAGE',
+ description="Percentage of scene render resolution"
+ )
+ bpy.types.WindowManager.sg_queue_gif_samples = bpy.props.IntProperty(
+ name="Samples", default=32, min=1, max=4096,
+ description="Render samples per frame"
+ )
+ bpy.types.WindowManager.sg_queue_gif_engine = bpy.props.EnumProperty(
+ name="Engine",
+ items=[
+ ('BLENDER_WORKBENCH', "Workbench", "Fast preview renderer — no lighting, flat shading"),
+ ('EEVEE', "Eevee", "Real-time renderer — good quality with fast render times"),
+ ('CYCLES', "Cycles", "Path-traced renderer — highest quality but slowest"),
+ ],
+ default='CYCLES'
+ )
+ bpy.types.WindowManager.sg_queue_gif_interpolation = bpy.props.EnumProperty(
+ name="Rotation Curve",
+ items=[
+ ('LINEAR', "Linear", "Constant rotation speed throughout the orbit"),
+ ('BEZIER', "Ease In/Out", "Smooth acceleration and deceleration for a polished look"),
+ ],
+ default='LINEAR'
+ )
+ bpy.types.WindowManager.sg_queue_gif_use_hdri = bpy.props.BoolProperty(
+ name="HDRI Environment", default=False
+ )
+ bpy.types.WindowManager.sg_queue_gif_hdri_path = bpy.props.StringProperty(
+ name="HDRI File",
+ description="Path to the HDRI image file (.hdr / .exr). "
+ "Applied via AddHDRI before each GIF export",
+ default="", subtype='FILE_PATH',
+ )
+ bpy.types.WindowManager.sg_queue_gif_hdri_strength = bpy.props.FloatProperty(
+ name="HDRI Strength",
+ description="Brightness of the HDRI environment lighting",
+ default=1.0, min=0.01, max=10.0,
+ )
+ bpy.types.WindowManager.sg_queue_gif_hdri_rotation = bpy.props.FloatProperty(
+ name="HDRI Rotation", default=0.0, min=0.0, max=360.0, subtype='ANGLE'
+ )
+ bpy.types.WindowManager.sg_queue_gif_env_mode = bpy.props.EnumProperty(
+ name="Environment Mode",
+ items=[
+ ('FIXED', "Fixed", "HDRI stays in place while the camera orbits"),
+ ('LOCKED', "Locked", "HDRI rotates with the camera to keep lighting consistent"),
+ ('COUNTER', "Counter-Rotate", "HDRI rotates opposite to the camera for dynamic lighting"),
+ ('ENV_ONLY', "Environment Only", "Render only the HDRI environment without the object"),
+ ],
+ default='FIXED'
+ )
+ bpy.types.WindowManager.sg_queue_gif_denoiser = bpy.props.BoolProperty(
+ name="Denoise", default=True
+ )
+ bpy.types.WindowManager.sg_queue_gif_use_gpu = bpy.props.BoolProperty(
+ name="GPU Compute", default=True,
+ description="Use GPU for Cycles rendering (Blender 5.1+ only)"
+ )
+ bpy.types.WindowManager.sg_queue_gif_also_no_pbr = bpy.props.BoolProperty(
+ name="Also Export Without PBR", default=False,
+ description="After the main GIF export, disable PBR, reproject, and export a second GIF with emission-only shading (1 sample)"
+ )
+
+ # --- Initial server refresh (timer) ---
+ def initial_refresh():
+ print("[StableGen] StableGen: Performing initial model list refresh...")
+ try:
+ bpy.ops.stablegen.check_server_status('INVOKE_DEFAULT')
+ prefs = bpy.context.preferences.addons.get(ADDON_PKG)
+ if not prefs or not prefs.preferences.server_online:
+ print("[StableGen] StableGen: Server not reachable during initial refresh.")
+ return None
+ if prefs and prefs.preferences.server_address:
+ bpy.ops.stablegen.refresh_checkpoint_list('INVOKE_DEFAULT')
+ bpy.ops.stablegen.refresh_lora_list('INVOKE_DEFAULT')
+ bpy.ops.stablegen.refresh_controlnet_mappings('INVOKE_DEFAULT')
+ else:
+ print("[StableGen] StableGen: Server address not set, skipping initial refresh.")
+ load_handler(None)
+ _sg_queue_load()
+ except Exception as e:
+ print(f"[StableGen] StableGen: Error during initial refresh: {e}")
+ return None
+
+ bpy.app.timers.register(initial_refresh, first_interval=1.0)
+
+ # ── Core generation properties ─────────────────────────────────────
+ bpy.types.Scene.comfyui_prompt = bpy.props.StringProperty(
+ name="ComfyUI Prompt",
+ description="Text prompt for generation (also used for texturing unless a separate texture prompt is provided)",
+ default="gold cube",
+ update=update_parameters
+ )
+ bpy.types.Scene.use_separate_texture_prompt = bpy.props.BoolProperty(
+ name="Use Separate Texture Prompt",
+ description="Enable a dedicated prompt field for the texturing pass instead of reusing the main prompt",
+ default=False,
+ update=update_parameters
+ )
+ bpy.types.Scene.texture_prompt = bpy.props.StringProperty(
+ name="Texture Prompt",
+ description="Prompt used for the per-camera texturing step. "
+ "Avoid view-specific details here (e.g. 'logo on the front') — "
+ "this prompt is applied from every camera angle. "
+ "Describe side-specific features in the main prompt or in per-camera prompts instead. "
+ "When empty, the main prompt is used",
+ default="",
+ update=update_parameters
+ )
+ bpy.types.Scene.comfyui_negative_prompt = bpy.props.StringProperty(
+ name="ComfyUI Negative Prompt",
+ description="Enter the negative text prompt for ComfyUI generation",
+ default="",
+ update=update_parameters
+ )
+ bpy.types.Scene.model_name = bpy.props.EnumProperty(
+ name="Model Name",
+ description="Select the SDXL checkpoint",
+ items=update_model_list,
+ update=update_parameters
+ )
+ bpy.types.Scene.sg_model_name_backup = bpy.props.StringProperty(
+ name="Model Name Backup",
+ description="Internal: stores the last known-good model_name identifier",
+ default="",
+ options={'HIDDEN'},
+ )
+ bpy.types.Scene.seed = bpy.props.IntProperty(
+ name="Seed",
+ description="Seed for image generation",
+ default=42, min=0, max=1000000,
+ update=update_parameters
+ )
+ bpy.types.Scene.control_after_generate = bpy.props.EnumProperty(
+ name="Control After Generate",
+ description="Control behavior after generation",
+ items=[
+ ('fixed', 'Fixed', 'Keep the same seed every generation'),
+ ('increment', 'Increment', 'Add 1 to the seed after each generation'),
+ ('decrement', 'Decrement', 'Subtract 1 from the seed after each generation'),
+ ('randomize', 'Randomize', 'Pick a random seed for every generation')
+ ],
+ default='fixed',
+ update=update_parameters
+ )
+ bpy.types.Scene.steps = bpy.props.IntProperty(
+ name="Steps",
+ description="Number of denoising steps. Higher values improve detail and coherence but take longer",
+ default=8, min=0, max=200,
+ update=update_parameters
+ )
+ bpy.types.Scene.cfg = bpy.props.FloatProperty(
+ name="CFG",
+ description="Classifier-Free Guidance scale. Higher values follow the prompt more strictly but may reduce quality; lower values give more creative freedom",
+ default=1.5, min=0.0, max=100.0,
+ update=update_parameters
+ )
+ bpy.types.Scene.sampler = bpy.props.EnumProperty(
+ name="Sampler",
+ description="Sampler for generation",
+ items=[
+ ('euler', 'Euler', ''),
+ ('euler_ancestral', 'Euler A', ''),
+ ('dpmpp_sde', 'DPM++ SDE', ''),
+ ('dpmpp_2m', 'DPM++ 2M', ''),
+ ('dpmpp_2s_ancestral', 'DPM++ 2S Ancestral', ''),
+ ],
+ default='dpmpp_2s_ancestral',
+ update=update_parameters
+ )
+ bpy.types.Scene.scheduler = bpy.props.EnumProperty(
+ name="Scheduler",
+ description="Scheduler for generation",
+ items=[
+ ('sgm_uniform', 'SGM Uniform', ''),
+ ('karras', 'Karras', ''),
+ ('beta', 'Beta', ''),
+ ('normal', 'Normal', ''),
+ ('simple', 'Simple', ''),
+ ],
+ default='sgm_uniform',
+ update=update_parameters
+ )
+ bpy.types.Scene.show_advanced_params = bpy.props.BoolProperty(
+ name="Show Advanced Parameters",
+ description="Show or hide advanced parameters",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.show_generation_params = bpy.props.BoolProperty(
+ name="Show Generation Parameters",
+ description="Toggle visibility of core generation settings (steps, CFG, sampler, scheduler, seed)",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.auto_rescale = bpy.props.BoolProperty(
+ name="Auto Rescale Resolution",
+ description="Automatically rescale resolution to appropriate size for the selected model",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.qwen_rescale_alignment = bpy.props.BoolProperty(
+ name="Qwen VL-Aligned Rescale",
+ description="Round resolution to multiples of 112 instead of 8 when auto-rescaling. "
+ "112 is the window size used by the Qwen2.5-VL vision encoder "
+ "(LCM of VAE factor 8, ViT patch 14, spatial merge 28). "
+ "The official diffusers pipeline rounds to 32; using 112 is stricter "
+ "and may reduce subtle zoom / pixel-shift artifacts in some cases",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.auto_rescale_target_mp = bpy.props.FloatProperty(
+ name="Target Megapixels",
+ description=(
+ "Target total megapixels when auto-rescaling the render resolution.\n\n"
+ "Default: 1.0 MP (~1024\u00d71024). This is the native resolution for SDXL "
+ "and the recommended default for Qwen Image Edit.\n\n"
+ "For SDXL / Flux models 1.0 MP is a safe default that matches "
+ "the training resolution. Higher values are possible if your "
+ "model and hardware support it, but may reduce quality or cause "
+ "out-of-memory issues.\n\n"
+ "For Qwen Image Edit the vision encoder becomes unreliable above "
+ "~1.4 MP and the model often fails to follow the prompt above "
+ "~1.2 MP. Values below ~0.3 MP also cause failures.\n\n"
+ "Adjust with care \u2013 staying close to 1.0 MP works best for most setups"
+ ),
+ default=1.0, min=0.1, max=4.0, step=10, precision=2,
+ update=update_parameters
+ )
+
+ # ── IPAdapter ──────────────────────────────────────────────────────
+ bpy.types.Scene.use_ipadapter = bpy.props.BoolProperty(
+ name="Use IPAdapter",
+ description="Use IPAdapter for image generation. Requires an external reference image. Can improve consistency, can be useful for generating images with similar styles.\n\n - Has priority over mode specific IPAdapter.",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.ipadapter_image = bpy.props.StringProperty(
+ name="Reference Image",
+ description="Path to the reference image",
+ default="", subtype='FILE_PATH',
+ update=update_parameters
+ )
+ bpy.types.Scene.ipadapter_strength = bpy.props.FloatProperty(
+ name="IPAdapter Strength",
+ description="Strength for IPAdapter",
+ default=1.0, min=-1.0, max=3.0,
+ update=update_parameters
+ )
+ bpy.types.Scene.ipadapter_start = bpy.props.FloatProperty(
+ name="IPAdapter Start",
+ description="Start percentage for IPAdapter (/100)",
+ default=0.0, min=0.0, max=1.0,
+ update=update_parameters
+ )
+ bpy.types.Scene.ipadapter_end = bpy.props.FloatProperty(
+ name="IPAdapter End",
+ description="End percentage for IPAdapter (/100)",
+ default=1.0, min=0.0, max=1.0,
+ update=update_parameters
+ )
+ bpy.types.Scene.ipadapter_weight_type = bpy.props.EnumProperty(
+ name="IPAdapter Weight Type",
+ description="Weight type for IPAdapter",
+ items=[
+ ('standard', 'Standard', ''),
+ ('prompt', 'Prompt is more important', ''),
+ ('style', 'Style transfer', ''),
+ ],
+ default='style',
+ update=update_parameters
+ )
+ bpy.types.Scene.sequential_ipadapter = bpy.props.BoolProperty(
+ name="Use IPAdapter",
+ description="Uses IPAdapter to improve consistency between images.\n\n - Applicable for Separate, Sequential and Refine modes.\n - Uses either the first generated image or the most recent one as a reference for the rest of the images.\n - If 'Regenerate IPAdapter' is enabled, the first viewpoint will be regenerated with IPAdapter to match the rest of the images.\n - If 'Use IPAdapter (External Image)' is enabled, this setting is effectively overriden.",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.sequential_ipadapter_mode = bpy.props.EnumProperty(
+ name="IPAdapter Mode",
+ description="Mode for IPAdapter in sequential generation",
+ items=_get_ipadapter_mode_items,
+ update=update_parameters
+ )
+ bpy.types.Scene.sequential_desaturate_factor = bpy.props.FloatProperty(
+ name="Desaturate Recent Image",
+ description="Desaturation factor for the 'most recent' image to prevent color stacking. 0.0 is no change, 1.0 is fully desaturated",
+ default=0.0, min=0.0, max=1.0,
+ update=update_parameters
+ )
+ bpy.types.Scene.sequential_contrast_factor = bpy.props.FloatProperty(
+ name="Reduce Contrast of Recent Image",
+ description="Contrast reduction factor for the 'most recent' image to prevent contrast stacking. 0.0 is no change, 1.0 is maximum reduction (grey)",
+ default=0.0, min=0.0, max=1.0,
+ update=update_parameters
+ )
+ bpy.types.Scene.sequential_ipadapter_regenerate = bpy.props.BoolProperty(
+ name="Regenerate IPAdapter",
+ description="IPAdapter generations may differ from the original image. This option regenerates the first viewpoint with IPAdapter to match the rest of the images.",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.sequential_ipadapter_regenerate_wo_controlnet = bpy.props.BoolProperty(
+ name="Generate IPAdapter reference without ControlNet",
+ description="Generate the first viewpoint with IPAdapter without ControlNet. This is useful for generating a reference image that is not affected by ControlNet. Can possibly generate higher quality reference.",
+ default=False, update=update_parameters
+ )
+
+ # ── Generation method ──────────────────────────────────────────────
+ bpy.types.Scene.generation_method = bpy.props.EnumProperty(
+ name="Generation Mode",
+ description="Choose the mode for generating images",
+ items=[
+ ('separate', 'Generate Separately', 'Generates images one by one for each viewpoint. Each image is generated independently using only its own control signals (e.g., depth map) without context from other views. All images are applied at the end.'),
+ ('sequential', 'Generate Sequentially', 'Generates images viewpoint by viewpoint. After the first view, each subsequent view is generated using inpainting, guided by a visibility mask and an RGB render of the texture projected from previous viewpoints to maintain consistency.'),
+ ('grid', 'Generate Using Grid', 'Combines control signals from all viewpoints into a single grid, generates a single image, then splits it back into individual viewpoint textures. Faster but lower resolution per view. Includes an optional second pass to refine each split image individually at full resolution for improved quality.'),
+ ('refine', 'Refine/Restyle Texture (Img2Img)', 'Uses the current rendered texture appearance as input for an img2img generation pass. Replaces the previous material with the new result. Good for restyling or globally changing the look of an existing texture. Works on any existing material setup.'),
+ ('local_edit', 'Local Edit', 'Make localized changes to an existing texture. Point cameras at areas you want to modify — the new generation blends over the original using angle and vignette-based feathering, preserving untouched areas. Works only with StableGen generated textures.'),
+ ('uv_inpaint', 'UV Inpaint Missing Areas', 'Identifies untextured areas on a standard UV map using a visibility calculation. Performs baking if not baked already. Performs diffusion inpainting directly on the UV texture map to fill only these missing regions, using the surrounding texture as context.'),
+ ],
+ default='sequential',
+ update=update_parameters
+ )
+ bpy.types.Scene.qwen_generation_method = bpy.props.EnumProperty(
+ name="Generation Mode",
+ description="Choose the mode for generating images with Qwen",
+ items=[
+ ('generate', 'Generate', 'Standard generation mode'),
+ ('refine', 'Refine', 'Refine/restyle the entire texture using Qwen Image Edit. Replaces the existing material with the new result. Describe the desired look in the prompt — you can completely change the style, color scheme, or overall appearance.'),
+ ('local_edit', 'Local Edit', 'Make targeted changes to specific areas of the texture. Point cameras at what you want to change and describe the edit — you can change colors, add details, sharpen, alter text, or restyle selected parts. Untouched areas are preserved.'),
+ ],
+ default='generate',
+ update=update_parameters
+ )
+ bpy.types.Scene.qwen_refine_use_prev_ref = bpy.props.BoolProperty(
+ name="Use Previous Refined View",
+ description="Use the previous modified/refined view output as a second image for reference (Image 2)",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.qwen_refine_use_depth = bpy.props.BoolProperty(
+ name="Use Depth Map",
+ description="Include depth map as an additional reference image (Image 3)",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.qwen_timestep_zero_ref = bpy.props.BoolProperty(
+ name="Timestep-Zero References",
+ description="Tell the model that reference images are fully clean (timestep 0) instead of noisy. "
+ "Reduces color shift and over-saturation, especially with Qwen 2511. "
+ "Requires the FluxKontextMultiReferenceLatentMethod node in ComfyUI",
+ default=False, update=update_parameters
+ )
+
+ # ── Refine settings ────────────────────────────────────────────────
+ bpy.types.Scene.refine_images = bpy.props.BoolProperty(
+ name="Refine Images",
+ description="Refine images after generation",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.refine_steps = bpy.props.IntProperty(
+ name="Refine Steps", description="Number of steps for refining",
+ default=8, min=0, max=200, update=update_parameters
+ )
+ bpy.types.Scene.refine_sampler = bpy.props.EnumProperty(
+ name="Refine Sampler", description="Sampler for refining",
+ items=[
+ ('euler', 'Euler', ''),
+ ('euler_ancestral', 'Euler A', ''),
+ ('dpmpp_sde', 'DPM++ SDE', ''),
+ ('dpmpp_2m', 'DPM++ 2M', ''),
+ ('dpmpp_2s_ancestral', 'DPM++ 2S Ancestral', ''),
+ ],
+ default='dpmpp_2s_ancestral', update=update_parameters
+ )
+ bpy.types.Scene.refine_scheduler = bpy.props.EnumProperty(
+ name="Refine Scheduler", description="Scheduler for refining",
+ items=[
+ ('sgm_uniform', 'SGM Uniform', ''),
+ ('karras', 'Karras', ''),
+ ('beta', 'Beta', ''),
+ ('normal', 'Normal', ''),
+ ('simple', 'Simple', ''),
+ ],
+ default='sgm_uniform', update=update_parameters
+ )
+ bpy.types.Scene.denoise = bpy.props.FloatProperty(
+ name="Denoise", description="Denoise level for refining",
+ default=0.8, min=0.0, max=1.0, update=update_parameters
+ )
+ bpy.types.Scene.refine_cfg = bpy.props.FloatProperty(
+ name="Refine CFG", description="Classifier-Free Guidance scale for refining",
+ default=1.5, min=0.0, max=100.0, update=update_parameters
+ )
+ bpy.types.Scene.refine_prompt = bpy.props.StringProperty(
+ name="Refine Prompt",
+ description="Prompt for refining (leave empty to use same prompt as generation)",
+ default="", update=update_parameters
+ )
+ bpy.types.Scene.refine_upscale_method = bpy.props.EnumProperty(
+ name="Refine Upscale Method", description="Upscale method for refining",
+ items=[
+ ('nearest-exact', 'Nearest Exact', ''),
+ ('bilinear', 'Bilinear', ''),
+ ('bicubic', 'Bicubic', ''),
+ ('lanczos', 'Lanczos', ''),
+ ],
+ default='lanczos', update=update_parameters
+ )
+
+ # ── Status / progress ──────────────────────────────────────────────
+ bpy.types.Scene.generation_status = bpy.props.EnumProperty(
+ name="Generation Status", description="Status of the generation process",
+ items=[
+ ('idle', 'Idle', ''),
+ ('running', 'Running', ''),
+ ('waiting', 'Waiting for cancel', ''),
+ ('error', 'Error', '')
+ ],
+ default='idle', update=update_parameters
+ )
+ bpy.types.Scene.sg_last_gen_error = bpy.props.BoolProperty(
+ name="Last Generation Error",
+ description="True if the most recent generation ended with an error",
+ default=False,
+ )
+ bpy.types.Scene.generation_progress = bpy.props.FloatProperty(
+ name="Generation Progress", description="Current progress of image generation",
+ default=0.0, min=0.0, max=100.0, update=update_parameters
+ )
+
+ # ── Material / blending ────────────────────────────────────────────
+ bpy.types.Scene.overwrite_material = bpy.props.BoolProperty(
+ name="Overwrite Material", description="Overwrite existing material",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.bake_visibility_weights = bpy.props.BoolProperty(
+ name="Bake Visibility Weights",
+ description="Pre-compute per-vertex visibility weights in Python instead of "
+ "using shader Raycast nodes. Makes projected textures survive "
+ "object transforms (move/rotate/scale) at the cost of vertex-level "
+ "weight resolution instead of per-pixel. Recommended for meshes "
+ "with >500 faces",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.discard_factor = bpy.props.FloatProperty(
+ name="Discard Factor",
+ description="If the texture is facing the camera at an angle greater than this value, it will be discarded. This is useful for preventing artifacts from the very edge of the generated texture appearing when keeping high discard factor (use ~65 for best results when generating textures around an object)",
+ default=90.0, min=0.0, max=180.0, update=update_parameters
+ )
+ bpy.types.Scene.discard_factor_generation_only = bpy.props.BoolProperty(
+ name="Reset Discard Angle After Generation",
+ description="If enabled, the 'Discard Factor' will be reset to a specified value after generation completes. Useful for sequential/Qwen modes where a low discard angle is needed during generation but not for final blending",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.discard_factor_after_generation = bpy.props.FloatProperty(
+ name="Discard Factor After Generation",
+ description="The value to set the 'Discard Factor' to after generation is complete",
+ default=90.0, min=0.0, max=180.0, update=update_parameters
+ )
+ bpy.types.Scene.weight_exponent_generation_only = bpy.props.BoolProperty(
+ name="Reset Exponent After Generation",
+ description="If enabled, the Weight Exponent will be reset to a specified value after generation completes. Useful for Voronoi projection mode where a high exponent produces sharp segmentation during generation but softer blending is preferred for the final result",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.weight_exponent_after_generation = bpy.props.FloatProperty(
+ name="Exponent After Generation",
+ description="The value to set the Weight Exponent to after generation is complete",
+ default=15.0, min=0.01, max=1000.0, update=update_parameters
+ )
+ bpy.types.Scene.view_blend_use_color_match = bpy.props.BoolProperty(
+ name="Match Colors to Viewport",
+ description="Match each generated view's colors to the current viewport texture before blending",
+ default=False, update=update_parameters,
+ )
+ bpy.types.Scene.view_blend_color_match_method = bpy.props.EnumProperty(
+ name="Color Match Method",
+ description="Algorithm used when matching view colors to the viewport texture",
+ items=[
+ ("mkl", "MKL", ""),
+ ("hm", "Histogram", ""),
+ ("reinhard", "Reinhard", ""),
+ ("mvgd", "MVGD", ""),
+ ("hm-mvgd-hm", "HM\u2013MVGD\u2013HM", ""),
+ ("hm-mkl-hm", "HM\u2013MKL\u2013HM", ""),
+ ],
+ default="hm-mvgd-hm", update=update_parameters,
+ )
+ bpy.types.Scene.view_blend_color_match_strength = bpy.props.FloatProperty(
+ name="Match Strength",
+ description="Blend between original and viewport-matched colors",
+ default=1.0, min=0.0, max=2.0, update=update_parameters,
+ )
+ bpy.types.Scene.weight_exponent = bpy.props.FloatProperty(
+ name="Weight Exponent",
+ description="Controls the falloff curve for viewpoint weighting based on the angle to the surface normal (\u03b8). "
+ "Weight = |cos(\u03b8)|^Exponent. Higher values prioritize straight-on views more strongly, creating sharper transitions. "
+ "1.0 = standard |cos(\u03b8)| weighting.",
+ default=3.0, min=0.1, max=1000.0, update=update_parameters
+ )
+ bpy.types.Scene.allow_modify_existing_textures = bpy.props.BoolProperty(
+ name="Allow modifying existing textures",
+ description="Disconnect compare node in export_visibility so that smooth output is not pure 1 areas",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.ask_object_prompts = bpy.props.BoolProperty(
+ name="Ask for object prompts",
+ description="Use object-specific prompts; if disabled, the normal prompt is used for all objects",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.fallback_color = bpy.props.FloatVectorProperty(
+ name="Fallback Color",
+ description="Color to use as fallback in texture generation",
+ subtype='COLOR', default=(0.0, 0.0, 0.0), min=0.0, max=1.0,
+ update=update_parameters
+ )
+
+ # ── Sequential / masking / inpainting ──────────────────────────────
+ bpy.types.Scene.sequential_smooth = bpy.props.BoolProperty(
+ name="Sequential Smooth",
+ description="Use smooth visibility map for sequential generation mode. Disabling this uses a binary visibility map and may need more mask blurring to reduce artifacts.\n\n - Visibility map is a mask that indicates which pixels have textures already projected from previous viewpoints.\n - Both methods are using weights which are calculated based on the angle between the surface normal and the camera view direction.\n - 'Smooth' uses these calculated weights directly (0.0-1.0 range, giving gradual transitions). The transition point can be further tuned by the 'Smooth Factor' parameters.\n - Disabling 'Smooth' thresholds these weights to create a hard-edged binary mask (0.0 or 1.0).",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.weight_exponent_mask = bpy.props.BoolProperty(
+ name="Weight Exponent Mask",
+ description="Use weight exponent for visibility map generation. Uses 1.0 if disabled.",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.canny_threshold_low = bpy.props.IntProperty(
+ name="Canny Threshold Low", description="Low threshold for Canny edge detection",
+ default=0, min=0, max=255, update=update_parameters
+ )
+ bpy.types.Scene.canny_threshold_high = bpy.props.IntProperty(
+ name="Canny Threshold High", description="High threshold for Canny edge detection",
+ default=80, min=0, max=255, update=update_parameters
+ )
+ bpy.types.Scene.sequential_factor_smooth = bpy.props.FloatProperty(
+ name="Smooth Visibility Black Point",
+ description="Controls the black point (start) of the Color Ramp used for the smooth visibility mask in sequential mode. Defines the weight threshold below which areas are considered fully invisible/untextured from previous views. Higher values create a sharper transition start.",
+ default=0.15, min=0.0, max=1.0, update=update_parameters
+ )
+ bpy.types.Scene.sequential_factor_smooth_2 = bpy.props.FloatProperty(
+ name="Smooth Visibility White Point",
+ description="Controls the white point (end) of the Color Ramp used for the smooth visibility mask in sequential mode. Defines the weight threshold above which areas are considered fully visible/textured from previous views. Lower values create a sharper transition end.",
+ default=1.0, min=0.0, max=1.0, update=update_parameters
+ )
+ bpy.types.Scene.sequential_factor = bpy.props.FloatProperty(
+ name="Binary Visibility Threshold",
+ description="Threshold value used when 'Sequential Smooth' is OFF. Calculated visibility weights below this value are treated as 0 (invisible), and those above as 1 (visible), creating a hard-edged binary mask.",
+ default=0.7, min=0.0, max=1.0, update=update_parameters
+ )
+ bpy.types.Scene.differential_noise = bpy.props.BoolProperty(
+ name="Differential Noise",
+ description="Adds latent noise mask to the image before inpainting. This must be used with low factor smooth mask or with a high blur mask radius. Disabling this effectively discrads the mask and only uses the inapaint conditioning.",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.grow_mask_by = bpy.props.IntProperty(
+ name="Grow Mask By", description="Grow mask by this amount (ComfyUI)",
+ default=3, min=0, update=update_parameters
+ )
+ bpy.types.Scene.mask_blocky = bpy.props.BoolProperty(
+ name="Blocky Visibility Map",
+ description="Uses a blocky visibility map. This will downscale the visibility map according to the 8x8 grid which Stable Diffusion uses in latent space. Highly experimental.",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.visibility_vignette = bpy.props.BoolProperty(
+ name="Feather Visibility Edges",
+ description="Blend refinement edges using a vignette mask to reduce seams",
+ default=True, update=update_parameters,
+ )
+ bpy.types.Scene.visibility_vignette_width = bpy.props.FloatProperty(
+ name="Vignette Width",
+ description="Fraction of the image radius used as a feather band (0 = no feather, 0.5 = very soft edges)",
+ default=0.15, min=0.0, max=0.5, update=update_parameters,
+ )
+ bpy.types.Scene.visibility_vignette_softness = bpy.props.FloatProperty(
+ name="Vignette Softness",
+ description="Exponent shaping feather falloff (<1 = sharper transition, >1 = softer/wider transition)",
+ default=1.0, min=0.1, max=5.0, update=update_parameters,
+ )
+ bpy.types.Scene.visibility_vignette_blur = bpy.props.BoolProperty(
+ name="Blur Vignette Mask",
+ description="Apply a Gaussian blur to the vignette mask to soften edges",
+ default=False, update=update_parameters,
+ )
+ bpy.types.Scene.sg_silhouette_margin = bpy.props.IntProperty(
+ name="Silhouette Margin",
+ description="Pixel margin around occluder silhouettes where projection is suppressed. "
+ "Prevents thin outlines of foreground geometry (e.g. hands) bleeding onto surfaces behind them. 0 = disabled",
+ default=3, min=0, max=20, update=update_parameters,
+ )
+ 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,
+ )
+ bpy.types.Scene.sg_silhouette_rays = bpy.props.EnumProperty(
+ name="Silhouette Rays",
+ description="Number of offset directions for silhouette detection. 4 = cardinal only (faster), 8 = cardinal + diagonal (better coverage)",
+ items=[
+ ('4', "4 (Cardinal)", "Right, Left, Up, Down"),
+ ('8', "8 (Cardinal + Diagonal)", "Cardinal + 4 diagonal directions"),
+ ],
+ default='4', update=update_parameters,
+ )
+
+ # ── Refine mode ramp controls ──────────────────────────────────────
+ bpy.types.Scene.refine_angle_ramp_active = bpy.props.BoolProperty(
+ name="Use Angle Ramp", description="Blend refinement based on surface angle",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.refine_angle_ramp_pos_0 = bpy.props.FloatProperty(
+ name="Angle Ramp Black Point", description="Position of the black point (Invisible) for the angle ramp",
+ default=0.0, min=0.0, max=1.0, update=update_parameters
+ )
+ bpy.types.Scene.refine_angle_ramp_pos_1 = bpy.props.FloatProperty(
+ name="Angle Ramp White Point", description="Position of the white point (Visible) for the angle ramp",
+ default=0.05, min=0.0, max=1.0, update=update_parameters
+ )
+ bpy.types.Scene.refine_feather_ramp_pos_0 = bpy.props.FloatProperty(
+ name="Feather Ramp Black Point", description="Position of the black point (Invisible) for the feather ramp",
+ default=0.0, min=0.0, max=1.0, update=update_parameters
+ )
+ bpy.types.Scene.refine_feather_ramp_pos_1 = bpy.props.FloatProperty(
+ name="Feather Ramp White Point", description="Position of the white point (Visible) for the feather ramp",
+ default=0.6, min=0.0, max=1.0, update=update_parameters
+ )
+ bpy.types.Scene.refine_edge_feather_projection = bpy.props.BoolProperty(
+ name="Edge Feather (Projection)",
+ description="Add a screen-space distance-transform ramp at the projection silhouette boundary as an additional multiplier on top of the angle\u00d7feather weight. Interior surfaces keep full strength; only the geometric edge is softened",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.refine_edge_feather_width = bpy.props.IntProperty(
+ name="Edge Feather Width",
+ description="Width in pixels of the transition band at projection boundaries. Larger values produce a wider blend zone",
+ default=30, min=1, max=200, update=update_parameters
+ )
+ bpy.types.Scene.refine_edge_feather_softness = bpy.props.FloatProperty(
+ name="Edge Feather Softness",
+ description="Rounds off the sharp corners at both ends of the linear feather ramp using a Gaussian blur. 0 = raw linear ramp (hard kinks at edge and interior boundary). 1 = moderate smoothing. Higher values give progressively gentler transitions without shifting the ramp position",
+ default=1.0, min=0.0, max=5.0, step=10, precision=2, update=update_parameters
+ )
+ bpy.types.Scene.differential_diffusion = bpy.props.BoolProperty(
+ name="Differential Diffusion",
+ description="Replace standard inpainting with a differential diffusion based workflow\n\n - Generally works better and reduces artifacts.\n - Using a Smooth Visibilty Map is recommended for Sequential Mode.",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.blur_mask = bpy.props.BoolProperty(
+ name="Blur Mask", description="Blur mask before inpainting (ComfyUI)",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.blur_mask_radius = bpy.props.IntProperty(
+ name="Blur Mask Radius", description="Radius for mask blurring (ComfyUI)",
+ default=1, min=1, max=31, update=update_parameters
+ )
+ bpy.types.Scene.blur_mask_sigma = bpy.props.FloatProperty(
+ name="Blur Mask Sigma", description="Sigma for mask blurring (ComfyUI)",
+ default=1.0, min=0.1, update=update_parameters
+ )
+ bpy.types.Scene.sequential_custom_camera_order = bpy.props.StringProperty(
+ name="Custom Camera Order",
+ description="Custom camera order for Sequential Mode. Format: 'index1,index2,index3,...'\n\n - This will permanently change the order of the cameras in the scene.",
+ default="", update=update_parameters
+ )
+ bpy.types.Scene.clip_skip = bpy.props.IntProperty(
+ name="CLIP Skip", description="CLIP skip value for generation",
+ default=1, min=1, update=update_parameters
+ )
+
+ # ── Presets ────────────────────────────────────────────────────────
+ bpy.types.Scene.stablegen_preset = bpy.props.EnumProperty(
+ name="Preset", description="Select a preset for easy mode",
+ items=get_preset_items, default=0
+ )
+ bpy.types.Scene.active_preset = bpy.props.StringProperty(
+ name="Active Preset", default="DEFAULT"
+ )
+
+ # ── Architecture ───────────────────────────────────────────────────
+ bpy.types.Scene.model_architecture = bpy.props.EnumProperty(
+ name="Model Architecture",
+ description="Select the model architecture to use for generation",
+ items=[
+ ('sdxl', 'SDXL', ''),
+ ('flux1', 'FLUX.1', ''),
+ ('qwen_image_edit', 'Qwen Image Edit', ''),
+ ('flux2_klein', 'FLUX.2 Klein', '')
+ ],
+ default='sdxl',
+ update=update_combined
+ )
+ bpy.types.Scene.architecture_mode = bpy.props.EnumProperty(
+ name="Architecture",
+ description="Select the overall generation architecture",
+ items=[
+ ('sdxl', 'SDXL', 'Stable Diffusion XL'),
+ ('flux1', 'FLUX.1', 'FLUX.1 architecture'),
+ ('qwen_image_edit', 'Qwen Image Edit', 'Qwen Image Edit architecture'),
+ ('flux2_klein', 'FLUX.2 Klein', 'FLUX.2 Klein multi-reference edit model (Apache 2.0)'),
+ ('trellis2', 'TRELLIS.2', 'Image to 3D mesh generation with TRELLIS.2'),
+ ],
+ default='sdxl',
+ update=update_architecture_mode
+ )
+
+ # ── TRELLIS.2 ──────────────────────────────────────────────────────
+ bpy.types.Scene.trellis2_generate_from = bpy.props.EnumProperty(
+ name="Generate From", description="Source for the TRELLIS.2 input",
+ items=[
+ ('image', 'Image', 'Use an existing image as input'),
+ ('prompt', 'Prompt', 'Generate an image from the prompt first, then feed to TRELLIS.2'),
+ ],
+ default='image', update=update_trellis2_generate_from
+ )
+ bpy.types.Scene.trellis2_texture_mode = bpy.props.EnumProperty(
+ name="Texture Mode", description="How to generate textures for the 3D mesh",
+ items=[
+ ('none', 'None', 'Shape only, no texture generation'),
+ ('native', 'Native (TRELLIS.2)', 'Use TRELLIS.2 built-in texture generation'),
+ ('sdxl', 'SDXL', 'Use SDXL for camera-based texture projection'),
+ ('flux1', 'FLUX.1', 'Use FLUX.1 for camera-based texture projection'),
+ ('qwen_image_edit', 'Qwen Image Edit', 'Use Qwen for camera-based texture projection'),
+ ('flux2_klein', 'FLUX.2 Klein', 'Use FLUX.2 Klein for camera-based texture projection'),
+ ],
+ default='native', update=update_trellis2_texture_mode
+ )
+ bpy.types.Scene.trellis2_initial_image_arch = bpy.props.EnumProperty(
+ name="Initial Image Architecture",
+ description="Diffusion architecture used to generate the initial image when Generate From is set to Prompt and Texture Mode is Native or None",
+ items=[
+ ('sdxl', 'SDXL', 'Stable Diffusion XL'),
+ ('flux1', 'FLUX.1', 'FLUX.1 architecture'),
+ ('qwen_image_edit', 'Qwen Image Edit', 'Qwen Image Edit architecture'),
+ ('flux2_klein', 'FLUX.2 Klein', 'FLUX.2 Klein multi-reference edit'),
+ ],
+ default='sdxl', update=update_trellis2_initial_image_arch
+ )
+ bpy.types.Scene.trellis2_camera_count = bpy.props.IntProperty(
+ name="Camera Count", description="Number of cameras to place around the generated mesh for texture projection",
+ default=8, min=2, max=32, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_placement_mode = bpy.props.EnumProperty(
+ name="Placement Mode", description="Strategy for placing cameras around the generated mesh",
+ items=[
+ ('orbit_ring', "Orbit Ring", "Place cameras in a circle around the center"),
+ ('hemisphere', "Sphere Coverage", "Distribute cameras evenly across a sphere using a Fibonacci spiral"),
+ ('normal_weighted', "Normal-Weighted", "Automatically place cameras to cover the most surface area, using K-means on face normals weighted by area"),
+ ('pca_axes', "PCA Axes", "Place cameras along the mesh's principal axes of variation"),
+ ('greedy_coverage', "Greedy Coverage", "Iteratively add cameras that maximise new visible surface. Automatically determines the number of cameras needed"),
+ ('fan_from_camera', "Fan from Camera", "Spread cameras in an arc around the active camera's orbit position"),
+ ],
+ default='normal_weighted', update=update_parameters
+ )
+ bpy.types.Scene.trellis2_auto_prompts = bpy.props.BoolProperty(
+ name="Auto View Prompts", description="Automatically generate view-direction prompts for each camera (e.g. 'front view', 'rear view, from above')",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_exclude_bottom = bpy.props.BoolProperty(
+ name="Exclude Bottom Faces", description="Ignore downward-facing geometry when placing cameras",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_exclude_bottom_angle = bpy.props.FloatProperty(
+ name="Bottom Angle Threshold",
+ description="Faces whose normal points more than this many degrees below the horizon are excluded",
+ default=1.5533, min=0.1745, max=1.5708, subtype='ANGLE', unit='ROTATION', update=update_parameters
+ )
+ bpy.types.Scene.trellis2_auto_aspect = bpy.props.EnumProperty(
+ name="Auto Aspect Ratio", description="Automatically adjust render aspect ratio to match the mesh silhouette",
+ items=[
+ ('off', "Off", "Use current scene resolution for all cameras"),
+ ('shared', "Shared", "Average silhouette aspect across all cameras and set a single scene resolution"),
+ ('per_camera', "Per Camera", "Each camera gets its own optimal aspect ratio"),
+ ],
+ default='per_camera', update=update_parameters
+ )
+ bpy.types.Scene.trellis2_occlusion_mode = bpy.props.EnumProperty(
+ name="Occlusion Handling", description="How to account for self-occlusion when choosing camera directions",
+ items=[
+ ('none', "None (Fast)", "Back-face culling only \u2014 ignores self-occlusion"),
+ ('full_matrix', "Full Occlusion Matrix", "Build a complete BVH-validated visibility matrix. Most accurate but slower"),
+ ('two_pass', "Two-Pass Refinement", "Fast back-face pass, then targeted BVH refinement"),
+ ('vis_weighted', "Visibility-Weighted", "Weight faces by visibility fraction; mostly-occluded faces have reduced influence"),
+ ],
+ default='none', update=update_parameters
+ )
+ bpy.types.Scene.trellis2_consider_existing = bpy.props.BoolProperty(
+ name="Consider Existing Cameras",
+ description="Treat existing cameras as already-placed directions so auto modes avoid duplicating their coverage",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_delete_cameras = bpy.props.BoolProperty(
+ name="Delete Cameras After",
+ description="Automatically delete cameras placed during the TRELLIS.2 pipeline once generation and texturing are complete",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_coverage_target = bpy.props.FloatProperty(
+ name="Coverage Target", description="Stop adding cameras when this fraction of surface area is visible (Greedy mode)",
+ default=0.95, min=0.5, max=1.0, subtype='FACTOR', update=update_parameters
+ )
+ bpy.types.Scene.trellis2_max_auto_cameras = bpy.props.IntProperty(
+ name="Max Cameras (Greedy)", description="Upper limit on cameras for Greedy Coverage mode",
+ default=12, min=2, max=50, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_fan_angle = bpy.props.FloatProperty(
+ name="Fan Angle", description="Total angular spread of the fan in degrees",
+ default=90.0, min=10.0, max=350.0, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_import_scale = bpy.props.FloatProperty(
+ name="Import Scale (BU)",
+ description="Scale imported TRELLIS.2 meshes so the longest axis equals this many Blender units. Set to 0 to keep the original size",
+ default=2.0, min=0.0, max=100.0, soft_min=0.0, soft_max=10.0, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_shade_mode = bpy.props.EnumProperty(
+ name="Shading", description="Shading mode applied to imported TRELLIS.2 meshes",
+ items=[
+ ('flat', "Flat", "Keep flat shading (default)"),
+ ('smooth', "Smooth", "Apply smooth shading to the entire mesh"),
+ ('auto_smooth', "Auto Smooth", "Apply auto smooth shading by angle"),
+ ],
+ default='flat', update=update_parameters
+ )
+ bpy.types.Scene.trellis2_clamp_elevation = bpy.props.BoolProperty(
+ name="Clamp Elevation",
+ description="Restrict camera elevation to avoid extreme top-down or bottom-up views that diffusion models often struggle with",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_max_elevation = bpy.props.FloatProperty(
+ name="Max Elevation", description="Maximum upward elevation angle. Cameras looking further up will be clamped",
+ default=1.2217, min=0.0, max=1.5708, subtype='ANGLE', unit='ROTATION', update=update_parameters
+ )
+ bpy.types.Scene.trellis2_min_elevation = bpy.props.FloatProperty(
+ name="Min Elevation", description="Minimum downward elevation angle. Cameras looking further down will be clamped",
+ default=-0.1745, min=-1.5708, max=0.0, subtype='ANGLE', unit='ROTATION', update=update_parameters
+ )
+ bpy.types.Scene.trellis2_preview_gallery_enabled = bpy.props.BoolProperty(
+ name="Preview Gallery",
+ description="When in prompt mode, generate multiple images with different seeds and let you pick the best one before proceeding to 3D generation",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_preview_gallery_count = bpy.props.IntProperty(
+ name="Gallery Count", description="Number of images to generate per batch in the preview gallery",
+ default=4, min=1, max=16, update=update_parameters
+ )
+
+ # ── Qwen-specific ──────────────────────────────────────────────────
+ bpy.types.Scene.qwen_guidance_map_type = bpy.props.EnumProperty(
+ name="Guidance Map", description="The type of guidance map to use for Qwen Image Edit",
+ items=[
+ ('depth', 'Depth Map', 'Use depth map for structural guidance'),
+ ('normal', 'Normal Map', 'Use normal map for structural guidance'),
+ ('workbench', 'Workbench Render', 'Use workbench render for structural guidance'),
+ ('viewport', 'Viewport Render', 'Use viewport render (OpenGL) for structural guidance')
+ ],
+ default='depth', update=update_parameters
+ )
+ bpy.types.Scene.qwen_voronoi_mode = bpy.props.BoolProperty(
+ name="Voronoi Projection",
+ description="Instead of zeroing weights of non-generated cameras, keep natural angle-based weights and project magenta from cameras that have not been generated yet. Use a high Weight Exponent to achieve Voronoi-like segmentation where each surface point is dominated by its closest camera. This eliminates the need for low discard-over-angle thresholds",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.qwen_context_render_mode = bpy.props.EnumProperty(
+ name="Context Render", description="How to use the RGB context render in sequential mode for Qwen",
+ items=[
+ ('NONE', 'Disabled', 'Do not use the RGB context render'),
+ ('REPLACE_STYLE', 'Replace Style Image', 'Use context render instead of the previous generated image as the style reference'),
+ ('ADDITIONAL', 'Additional Context', 'Use context render as an additional image input (image 3) for context')
+ ],
+ default='NONE', update=update_parameters
+ )
+ bpy.types.Scene.qwen_use_external_style_image = bpy.props.BoolProperty(
+ name="Use External Style Image",
+ description="Use a separate, external image as the style reference for all viewpoints",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.qwen_external_style_image = bpy.props.StringProperty(
+ name="Style Reference Image", description="Path to the external style reference image",
+ default="", subtype='FILE_PATH', update=update_parameters
+ )
+ bpy.types.Scene.qwen_external_style_initial_only = bpy.props.BoolProperty(
+ name="External for Initial Only",
+ description="Use the external style image for the first image, then use the previously generated image for subsequent images",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.qwen_use_custom_prompts = bpy.props.BoolProperty(
+ name="Use Custom Qwen Prompts",
+ description="Enable to override the default guidance prompts for the Qwen Image Edit workflow",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.qwen_custom_prompt_initial = bpy.props.StringProperty(
+ name="Initial Image Prompt",
+ description="Custom prompt for the first generated image. Use {main_prompt} to insert the main prompt text.",
+ default="Change and transfer the format of '{main_prompt}' in image 1 to the style from image 2",
+ update=update_parameters
+ )
+ bpy.types.Scene.qwen_custom_prompt_seq_none = bpy.props.StringProperty(
+ name="Sequential Prompt (No Context)",
+ description="Custom prompt for subsequent images when Context Render is 'Disabled'. Use {main_prompt} to insert the main prompt text.",
+ default="Change and transfer the format of '{main_prompt}' in image 1 to the style from image 2",
+ update=update_parameters
+ )
+ bpy.types.Scene.qwen_custom_prompt_seq_replace = bpy.props.StringProperty(
+ name="Sequential Prompt (Replace Style)",
+ description="Custom prompt for subsequent images when Context Render is 'Replace Style'. Use {main_prompt} to insert the main prompt text.",
+ default="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.",
+ update=update_parameters
+ )
+ bpy.types.Scene.qwen_custom_prompt_seq_additional = bpy.props.StringProperty(
+ name="Sequential Prompt (Additional Context)",
+ description="Custom prompt for subsequent images when Context Render is 'Additional Context'. Use {main_prompt} to insert the main prompt text.",
+ default="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.",
+ 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.qwen_prompt_gray_background = bpy.props.BoolProperty(
+ name="Gray Background Prompt",
+ description="Include 'Replace the background with solid gray' in the default context-render prompt. Disable to let the model choose the background freely",
+ default=True, update=update_parameters
+ )
+
+ # ── Misc ───────────────────────────────────────────────────────────
+ bpy.types.Scene.output_timestamp = bpy.props.StringProperty(
+ name="Output Timestamp", description="Timestamp for generation output directory", default=""
+ )
+ bpy.types.Scene.camera_prompts = bpy.props.CollectionProperty(
+ type=CameraPromptItem, name="Camera Prompts",
+ description="Stores viewpoint descriptions for each camera"
+ )
+ bpy.types.Scene.use_camera_prompts = bpy.props.BoolProperty(
+ name="Use Camera Prompts", description="Use camera prompts for generating images",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.sg_camera_order = bpy.props.CollectionProperty(
+ type=CameraOrderItem, name="Camera Generation Order",
+ description="Defines the order in which cameras are processed during generation"
+ )
+ bpy.types.Scene.sg_camera_order_index = bpy.props.IntProperty(
+ name="Active Camera Order Index", default=0
+ )
+ bpy.types.Scene.sg_use_custom_camera_order = bpy.props.BoolProperty(
+ name="Use Custom Camera Order",
+ description="When enabled, generation uses the custom camera order list instead of alphabetical sorting",
+ default=False, update=update_parameters
+ )
+
+ # ── UI section toggles ─────────────────────────────────────────────
+ bpy.types.Scene.show_core_settings = bpy.props.BoolProperty(
+ name="Core Generation Settings",
+ description="Parameters used for the image generation process. Also includes LoRAs for faster generation.",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.show_lora_settings = bpy.props.BoolProperty(
+ name="LoRA Settings", description="Settings for custom LoRA management.",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.show_camera_options = bpy.props.BoolProperty(
+ name="Camera Settings", description="Camera prompt and generation order settings.",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.show_scene_understanding_settings = bpy.props.BoolProperty(
+ name="Viewpoint Blending Settings",
+ description="Settings for how the addon blends different viewpoints together.",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.show_output_material_settings = bpy.props.BoolProperty(
+ name="Output & Material Settings",
+ description="Settings for output characteristics and material handling, including texture processing and final image resolution.",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.show_image_guidance_settings = bpy.props.BoolProperty(
+ name="Image Guidance (IPAdapter & ControlNet)",
+ description="Configuration for advanced image guidance techniques, allowing more precise control via reference images or structural inputs.",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.show_masking_inpainting_settings = bpy.props.BoolProperty(
+ name="Inpainting Options",
+ description="Parameters for inpainting and mask manipulation to refine specific image areas. (Visible for UV Inpaint & Sequential modes).",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.show_mode_specific_settings = bpy.props.BoolProperty(
+ name="Generation Mode Specifics",
+ description="Parameters exclusively available for the selected Generation Mode, allowing tailored control over mode-dependent behaviors.",
+ default=False, update=update_parameters
+ )
+
+ # ── Generation mode / priority ─────────────────────────────────────
+ bpy.types.Scene.generation_mode = bpy.props.EnumProperty(
+ name="Generation Mode", description="Controls the generation behavior",
+ items=[
+ ('standard', 'Standard', 'Standard generation process'),
+ ('regenerate_selected', 'Regenerate Selected', 'Regenerate only specific viewpoints, keeping the rest from the previous run'),
+ ('project_only', 'Project Only', 'Only project existing textures onto the model without generating new ones')
+ ],
+ default='standard', update=update_parameters
+ )
+ bpy.types.Scene.early_priority_strength = bpy.props.FloatProperty(
+ name="Prioritize Initial Views",
+ description="Strength of the priority applied to initial views. Higher values will make the earlier cameras more important than the later ones. Every view will be prioritized over the next one.\n - Very high values may cause various artifacts.",
+ default=0.5, min=0.0, max=1.0, update=update_parameters
+ )
+ bpy.types.Scene.early_priority = bpy.props.BoolProperty(
+ name="Priority Strength",
+ description="Enable blending priority for earlier cameras.\n - This may prevent artifacts caused by later cameras overwriting earlier ones.\n - You will have to place the important cameras first.",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.texture_objects = bpy.props.EnumProperty(
+ name="Objects to Texture", description="Select the objects to texture",
+ items=[
+ ('all', 'All Visible', 'Texture all visible objects in the scene'),
+ ('selected', 'Selected', 'Texture only selected objects'),
+ ],
+ default='all', update=update_parameters
+ )
+ bpy.types.Scene.use_flux_lora = bpy.props.BoolProperty(
+ name="Use FLUX Depth LoRA",
+ description="Use FLUX.1-Depth-dev LoRA for depth conditioning instead of ControlNet. This disables all ControlNet units.",
+ default=True, update=update_parameters
+ )
+
+ # ── ControlNet / LoRA collections ──────────────────────────────────
+ bpy.types.Scene.controlnet_units = bpy.props.CollectionProperty(type=ControlNetUnit)
+ bpy.types.Scene.lora_units = bpy.props.CollectionProperty(type=LoRAUnit)
+ bpy.types.Scene.controlnet_units_index = bpy.props.IntProperty(default=0)
+ bpy.types.Scene.lora_units_index = bpy.props.IntProperty(default=0)
+ bpy.app.handlers.load_post.append(load_handler)
+ bpy.app.handlers.load_post.append(_sg_queue_load_handler)
+
+ # ── TRELLIS.2 status / flags ───────────────────────────────────────
+ bpy.types.Scene.trellis2_available = bpy.props.BoolProperty(
+ name="TRELLIS.2 Available",
+ description="Whether TRELLIS.2 nodes are available on the ComfyUI server (auto-detected)",
+ default=False
+ )
+ bpy.types.Scene.pbr_nodes_available = bpy.props.BoolProperty(
+ name="PBR Nodes Available",
+ description="Whether PBR decomposition nodes (Marigold IID + StableDelight) are available on the ComfyUI server (auto-detected)",
+ default=False
+ )
+ bpy.types.Scene.show_trellis2_params = bpy.props.BoolProperty(
+ name="Show TRELLIS.2 Section", description="Toggle TRELLIS.2 Image-to-3D section", default=False
+ )
+ bpy.types.Scene.show_trellis2_advanced = bpy.props.BoolProperty(
+ name="Show TRELLIS.2 Settings", description="Toggle TRELLIS.2 advanced settings", default=False
+ )
+ bpy.types.Scene.show_trellis2_mesh_settings = bpy.props.BoolProperty(
+ name="Show Mesh Generation Settings", description="Toggle TRELLIS.2 mesh generation advanced settings", default=False
+ )
+ bpy.types.Scene.show_trellis2_texture_settings = bpy.props.BoolProperty(
+ name="Show Texture Settings", description="Toggle TRELLIS.2 native texture settings", default=False
+ )
+ bpy.types.Scene.show_trellis2_camera_settings = bpy.props.BoolProperty(
+ name="Show Camera Placement Settings", description="Toggle TRELLIS.2 camera placement settings", default=False
+ )
+ bpy.types.Scene.trellis2_last_input_image = bpy.props.StringProperty(
+ name="TRELLIS.2 Last Input Image",
+ description="Path to the image most recently used as TRELLIS.2 input (set automatically after generation)",
+ subtype='FILE_PATH', default=""
+ )
+ bpy.types.Scene.qwen_use_trellis2_style = bpy.props.BoolProperty(
+ name="Use TRELLIS.2 Input as Style",
+ description="Use the TRELLIS.2 input image as the Qwen style reference",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.qwen_trellis2_style_initial_only = bpy.props.BoolProperty(
+ name="TRELLIS.2 Style for Initial Only",
+ description="Use the TRELLIS.2 input image only for the first image, then fall back to sequential style for subsequent images",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_pipeline_active = bpy.props.BoolProperty(
+ name="TRELLIS.2 Pipeline Active",
+ description="True while the TRELLIS.2 pipeline (incl. texturing) is running",
+ default=False
+ )
+ bpy.types.Scene.trellis2_pipeline_phase_start_pct = bpy.props.FloatProperty(
+ name="Phase Start %", description="Overall-% at which the current texturing phase begins",
+ default=0.0, min=0.0, max=100.0
+ )
+ bpy.types.Scene.trellis2_pipeline_total_phases = bpy.props.IntProperty(
+ name="Total Phases", description="Total number of phases in the TRELLIS.2 pipeline",
+ default=3, min=1, max=3
+ )
+ bpy.types.Scene.trellis2_input_image = bpy.props.StringProperty(
+ name="Input Image", description="Path to the reference image for 3D mesh generation",
+ subtype='FILE_PATH', default=""
+ )
+ bpy.types.Scene.trellis2_resolution = bpy.props.EnumProperty(
+ name="Resolution", description="Model resolution for generation. Higher values use more VRAM",
+ items=[
+ ('512', '512', 'Low resolution, fast, less VRAM'),
+ ('1024', '1024', 'Direct 1024 generation with higher sparse structure resolution'),
+ ('1024_cascade', '1024 Cascade', 'Medium resolution with cascade (recommended)'),
+ ('1536_cascade', '1536 Cascade', 'High resolution with cascade, most VRAM'),
+ ],
+ default='1024_cascade', update=update_parameters
+ )
+ bpy.types.Scene.trellis2_vram_mode = bpy.props.EnumProperty(
+ name="VRAM Mode", description="Controls model offloading strategy to manage VRAM usage",
+ items=[
+ ('keep_loaded', 'Keep Loaded', 'Keep all models in VRAM (fastest, ~12GB VRAM)'),
+ ('disk_offload', 'Disk Offload', 'Load models on demand from disk (recommended for <=16GB VRAM)'),
+ ],
+ default='disk_offload', update=update_parameters
+ )
+ bpy.types.Scene.trellis2_attn_backend = bpy.props.EnumProperty(
+ name="Attention Backend", description="Attention implementation to use. flash_attn is fastest but requires CUDA",
+ items=[
+ ('flash_attn', 'Flash Attention', 'Fastest (requires flash-attn package)'),
+ ('xformers', 'xFormers', 'Fast (requires xformers package)'),
+ ('sdpa', 'SDPA', 'PyTorch native, always available'),
+ ('sageattn', 'SageAttention', 'SageAttention backend (requires sageattn package)'),
+ ],
+ default='flash_attn', update=update_parameters
+ )
+ bpy.types.Scene.trellis2_seed = bpy.props.IntProperty(
+ name="Seed", description="Random seed for generation (0 = random)",
+ default=0, min=0, max=2147483647
+ )
+ bpy.types.Scene.trellis2_ss_guidance = bpy.props.FloatProperty(
+ name="SS Guidance", description="Sparse structure CFG scale. Higher = stronger adherence to input image",
+ default=7.5, min=1.0, max=20.0, step=10, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_ss_steps = bpy.props.IntProperty(
+ name="SS Steps", description="Sparse structure sampling steps. More steps = better quality but slower",
+ default=12, min=1, max=50, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_shape_guidance = bpy.props.FloatProperty(
+ name="Shape Guidance", description="Shape CFG scale. Higher = stronger adherence to input image",
+ default=7.5, min=1.0, max=20.0, step=10, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_shape_steps = bpy.props.IntProperty(
+ name="Shape Steps", description="Shape sampling steps. More steps = better quality but slower",
+ default=12, min=1, max=50, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_tex_guidance = bpy.props.FloatProperty(
+ name="Texture Guidance", description="Texture CFG scale. Higher = stronger adherence to input image",
+ default=7.5, min=1.0, max=20.0, step=10, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_tex_steps = bpy.props.IntProperty(
+ name="Texture Steps", description="Texture sampling steps. More steps = better quality but slower",
+ default=12, min=1, max=50, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_max_tokens = bpy.props.IntProperty(
+ name="Max Tokens",
+ description="Max sparse-voxel tokens during cascade upsampling (only affects cascade modes). "
+ "Higher = more detail but more VRAM. Increase to 49152 for maximum detail if VRAM allows",
+ default=32768, min=16384, max=65536, step=4096, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_texture_size = bpy.props.IntProperty(
+ name="Texture Size",
+ description="Resolution of the UV-baked texture in pixels. Higher values reduce aliasing at UV seams. "
+ "The native voxel grid is 1024, so values above 1024 interpolate existing data",
+ default=4096, min=512, max=8192, step=512, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_decimation = bpy.props.IntProperty(
+ name="Decimation Target", description="Target polygon count for mesh simplification. Lower = simpler mesh",
+ default=100000, min=1000, max=5000000, step=10000, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_remesh = bpy.props.BoolProperty(
+ name="Remesh", description="Apply remeshing for cleaner topology",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_post_processing_enabled = bpy.props.BoolProperty(
+ name="Post-Processing",
+ description="Run ComfyUI-side mesh post-processing (decimation + remeshing). Disable to import the raw mesh for manual retopology",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_auto_lighting = bpy.props.BoolProperty(
+ name="Auto Studio Lighting",
+ description="Create a three-point studio lighting rig (key, fill, rim) after import to showcase PBR materials",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_skip_texture = bpy.props.BoolProperty(
+ name="Skip Texture", description="Export shape-only mesh (no PBR textures). Much faster and uses less VRAM",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_bg_removal = bpy.props.EnumProperty(
+ name="Background Removal", description="Method for removing the image background before TRELLIS.2 processing",
+ items=[
+ ('auto', 'Auto (BiRefNet)', 'Automatically remove background using BiRefNet model'),
+ ('skip', 'Skip (Use Alpha)', 'Skip background removal \u2014 use the input image\'s alpha channel as mask. '
+ 'Use this when your image already has a transparent background'),
+ ],
+ default='auto', update=update_parameters
+ )
+ bpy.types.Scene.trellis2_background_color = bpy.props.EnumProperty(
+ name="Background Color", description="Background color for image conditioning",
+ items=[
+ ('black', 'Black', 'Black background (default)'),
+ ('gray', 'Gray', 'Gray background'),
+ ('white', 'White', 'White background'),
+ ],
+ default='black', update=update_parameters
+ )
+
+ # ── PBR Decomposition ──────────────────────────────────────────────
+ bpy.types.Scene.pbr_decomposition = bpy.props.BoolProperty(
+ name="PBR Decomposition",
+ description="Run Marigold decomposition on each generated image to produce PBR material maps (albedo, roughness, metallic, normal, depth)",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.pbr_albedo_source = bpy.props.EnumProperty(
+ name="Albedo Source", description="Model to use for extracting the Base Color / albedo map",
+ items=[
+ ('marigold', "Marigold IID (Flat Albedo)", "True albedo from Marigold IID-Appearance \u2014 removes all lighting but may lose texture detail"),
+ ('delight', "StableDelight (Delighted)", "Specular-removed image via StableDelight \u2014 preserves diffuse shading and texture detail, only strips highlights"),
+ ('lighting', "Marigold IID-Lighting (Vibrant)", "Albedo from Marigold IID-Lighting \u2014 tends to produce more vibrant, saturated colours than IID-Appearance"),
+ ],
+ default='marigold', update=update_parameters,
+ )
+ bpy.types.Scene.pbr_map_albedo = bpy.props.BoolProperty(
+ name="Albedo", description="Extract albedo (diffuse colour without lighting) and use it as Base Color",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.pbr_map_roughness = bpy.props.BoolProperty(
+ name="Roughness", description="Extract roughness map (0 = mirror, 1 = rough)",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.pbr_map_metallic = bpy.props.BoolProperty(
+ name="Metallic", description="Extract metallic map (0 = dielectric, 1 = metal)",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.pbr_map_normal = bpy.props.BoolProperty(
+ name="Normal",
+ description="Extract surface normal map for detail and bump. Warning: may cause triangle artifacts on voxel-remeshed geometry",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.pbr_normal_strength = bpy.props.FloatProperty(
+ name="Normal Strength",
+ description="How strongly the normal map perturbs the surface shading. Lower values reduce triangle artifacts on poor topology",
+ default=1.0, min=0.0, max=2.0, step=0.05, update=update_parameters
+ )
+ bpy.types.Scene.pbr_delight_strength = bpy.props.FloatProperty(
+ name="Delight Strength",
+ description="How strongly StableDelight removes specular reflections. Lower values preserve more original texture detail. 1.0 = full delighting, 0.5 = subtle",
+ default=1.0, min=0.01, max=5.0, step=0.1, update=update_parameters
+ )
+ bpy.types.Scene.pbr_map_height = bpy.props.BoolProperty(
+ name="Height", description="Extract height/displacement map via Marigold depth estimation",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.pbr_height_scale = bpy.props.FloatProperty(
+ name="Height Scale", description="Displacement scale for the height map. Higher values produce more pronounced surface displacement",
+ default=0.1, min=0.001, max=2.0, step=0.01, precision=3, update=update_parameters
+ )
+ bpy.types.Scene.pbr_map_ao = bpy.props.BoolProperty(
+ name="AO", description="Bake an Ambient Occlusion map from mesh geometry (uses Blender's built-in bake, no ML required)",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.pbr_ao_samples = bpy.props.IntProperty(
+ name="AO Samples", description="Number of samples for AO baking. Higher = cleaner but slower",
+ default=16, min=1, max=128, update=update_parameters
+ )
+ bpy.types.Scene.pbr_ao_distance = bpy.props.FloatProperty(
+ name="AO Distance", description="Maximum ray distance for AO baking. 0 = scene size (automatic)",
+ default=0.0, min=0.0, max=100.0, step=0.1, update=update_parameters
+ )
+ bpy.types.Scene.pbr_map_emission = bpy.props.BoolProperty(
+ name="Emission", description="Extract an emission map (experimental). Identifies self-illuminating regions in the generated texture",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.pbr_emission_method = bpy.props.EnumProperty(
+ name="Emission Method", description="Algorithm used to detect emissive regions",
+ items=[
+ ('residual', "IID-Lighting Residual", "Run Marigold IID-Lighting and extract the residual (image \u2212 albedo \u00d7 shading). Most accurate but requires an additional model pass"),
+ ('hsv', "HSV Threshold", "Isolate pixels with high saturation + high value in HSV space. Fast, zero model cost, best for neon/sci-fi styles"),
+ ],
+ default='residual', update=update_parameters
+ )
+ bpy.types.Scene.pbr_emission_threshold = bpy.props.FloatProperty(
+ name="Emission Threshold",
+ description="Minimum value to consider as emission. In Residual mode, fades out low-luminance residuals. In HSV mode, zeroes the blurred mask below this cutoff. Higher = fewer false positives, lower = catches subtle glow",
+ default=0.2, min=0.0, max=1.0, step=0.05, precision=2, update=update_parameters
+ )
+ bpy.types.Scene.pbr_emission_saturation_min = bpy.props.FloatProperty(
+ name="Saturation Min",
+ description="(HSV method) Minimum saturation for a pixel to be considered emissive. Glowing objects retain colour intensity while lit surfaces wash out",
+ default=0.5, min=0.0, max=1.0, step=0.05, precision=2, update=update_parameters
+ )
+ bpy.types.Scene.pbr_emission_value_min = bpy.props.FloatProperty(
+ name="Value Min", description="(HSV method) Minimum value/brightness for a pixel to be considered emissive",
+ default=0.85, min=0.0, max=1.0, step=0.05, precision=2, update=update_parameters
+ )
+ bpy.types.Scene.pbr_emission_bloom = bpy.props.FloatProperty(
+ name="Bloom Radius",
+ description="(HSV method) Gaussian blur radius applied to the emission mask to simulate bloom / glow bleed. 0 = sharp mask, higher = more bloom",
+ default=5.0, min=0.0, max=50.0, step=1.0, precision=1, update=update_parameters
+ )
+ bpy.types.Scene.pbr_emission_strength = bpy.props.FloatProperty(
+ name="Emission Strength", description="Strength of the emission channel in the Principled BSDF",
+ default=2.5, min=0.0, max=100.0, step=0.5, precision=1, update=update_parameters
+ )
+ bpy.types.Scene.pbr_use_native_resolution = bpy.props.BoolProperty(
+ name="Use Native Resolution",
+ description="Process PBR maps at the image's native resolution (longest edge, rounded to 64px). Produces sharper results but uses more VRAM. When disabled, the fixed Processing Resolution is used",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.pbr_tiling = bpy.props.EnumProperty(
+ name="Tiling", description="Tile-based super-resolution for PBR maps. Each tile is upscaled to the full image resolution before processing, producing N\u00b2\u00d7 the effective detail",
+ items=[
+ ('off', "Off", "No tiling \u2014 process the full image in one pass"),
+ ('selective', "Selective", "Tile albedo only (StableDelight and/or IID albedo). Normals, roughness, metallic and height are processed normally"),
+ ('all', "All", "Tile every PBR model including normals and height"),
+ ('custom', "Custom", "Choose which maps to tile individually. Models are run as efficiently as possible"),
+ ],
+ default='selective', update=update_parameters
+ )
+ bpy.types.Scene.pbr_tile_albedo = bpy.props.BoolProperty(
+ name="Tile Albedo", description="Tile the albedo map for higher detail",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.pbr_tile_material = bpy.props.BoolProperty(
+ name="Tile Material", description="Tile the roughness and metallic maps (both come from the same IID-Appearance material output)",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.pbr_tile_normal = bpy.props.BoolProperty(
+ name="Tile Normal", description="Tile the normal map for higher detail",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.pbr_tile_height = bpy.props.BoolProperty(
+ name="Tile Height", description="Tile the height/displacement map for higher detail",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.pbr_tile_emission = bpy.props.BoolProperty(
+ name="Tile Emission",
+ description="Tile the IID-Lighting residual used for emission (only applies to the Residual emission method)",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.pbr_tile_grid = bpy.props.IntProperty(
+ name="Tile Grid",
+ description="N\u00d7N grid size for tiling. 2 = 4 tiles (4\u00d7 detail), 3 = 9 tiles (9\u00d7 detail), 4 = 16 tiles (16\u00d7 detail). Processing time scales with N\u00b2",
+ default=2, min=2, max=4, update=update_parameters
+ )
+ bpy.types.Scene.pbr_tile_superres = bpy.props.BoolProperty(
+ name="Super Resolution",
+ description="When enabled, the stitched PBR maps are kept at the upscaled tile resolution (~N\u00d7 the original image size). When disabled (default), tiles are scaled back to the original image resolution \u2014 still higher detail from tiled processing, but matching the source texture size",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.pbr_processing_resolution = bpy.props.IntProperty(
+ name="Processing Resolution",
+ description="Internal processing resolution for the Marigold model. Output is upscaled back to the original resolution. 768 is the default for the model's training resolution",
+ default=768, min=256, max=2048, step=64, update=update_parameters
+ )
+ bpy.types.Scene.pbr_denoise_steps = bpy.props.IntProperty(
+ name="Denoise Steps",
+ description="Number of denoising steps. More steps = better quality but slower. 4 is a good balance",
+ default=4, min=1, max=50, update=update_parameters
+ )
+ bpy.types.Scene.pbr_ensemble_size = bpy.props.IntProperty(
+ name="Ensemble Size",
+ description="Number of ensemble predictions to average. Higher = more stable results but linearly slower. 1 is usually sufficient",
+ default=1, min=1, max=10, update=update_parameters
+ )
+ bpy.types.Scene.pbr_albedo_auto_saturation = bpy.props.BoolProperty(
+ name="Correct Albedo Saturation",
+ description="Automatically correct albedo saturation by comparing the PBR albedo against the original rendered image and boosting to match. Averaged across all cameras for uniform results by default. Recommended for Marigold IID (Flat Albedo) which tends to desaturate. Usually not needed for StableDelight or IID-Lighting",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.pbr_albedo_saturation_mode = bpy.props.EnumProperty(
+ name="Saturation Mode",
+ description="How to compute the albedo saturation correction ratio. Mean averages all camera ratios (sensitive to outliers). Median takes the middle value (robust to outliers). Per Camera applies an individual ratio to each camera",
+ items=[
+ ('MEDIAN', "Median", "Use the median ratio across cameras (recommended, outlier-resistant)"),
+ ('MEAN', "Mean", "Use the arithmetic mean of all camera ratios"),
+ ('PER_CAMERA', "Per Camera", "Compute and apply an individual ratio for each camera"),
+ ],
+ default='MEDIAN', update=update_parameters
+ )
+ bpy.types.Scene.pbr_replace_color_with_albedo = bpy.props.BoolProperty(
+ name="Use Albedo as Base Color",
+ description="Replace the projected colour texture with the albedo map. This effectively delights the texture",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.pbr_auto_lighting = bpy.props.BoolProperty(
+ name="Studio Lighting",
+ description="Create a three-point studio lighting rig (key, fill, rim) after PBR projection to showcase PBR materials",
+ default=False, update=update_parameters
+ )
+
+
+# ── Unregistration ─────────────────────────────────────────────────────────
+
+def unregister_properties(load_handler, _sg_queue_load_handler):
+ """Remove all properties registered by ``register_properties``."""
+
+ if hasattr(bpy.types.Scene, 'controlnet_model_mappings'):
+ del bpy.types.Scene.controlnet_model_mappings
+ if hasattr(bpy.types.Scene, 'controlnet_mapping_index'):
+ del bpy.types.Scene.controlnet_mapping_index
+
+ _simple_scene_props = [
+ 'use_flux_lora',
+ 'comfyui_prompt', 'use_separate_texture_prompt', 'texture_prompt',
+ 'comfyui_negative_prompt', 'model_name', 'sg_model_name_backup',
+ 'seed', 'control_after_generate', 'steps', 'cfg',
+ 'sampler', 'scheduler', 'show_advanced_params', 'show_generation_params',
+ 'auto_rescale', 'qwen_rescale_alignment', 'auto_rescale_target_mp',
+ 'generation_method', 'use_ipadapter',
+ 'refine_images', 'refine_steps', 'refine_sampler', 'refine_scheduler',
+ 'denoise', 'refine_cfg', 'refine_prompt', 'refine_upscale_method',
+ 'generation_status', 'sg_last_gen_error', 'generation_progress',
+ 'overwrite_material', 'bake_visibility_weights',
+ 'discard_factor', 'discard_factor_generation_only', 'discard_factor_after_generation',
+ 'weight_exponent', 'weight_exponent_generation_only', 'weight_exponent_after_generation',
+ 'view_blend_use_color_match', 'view_blend_color_match_method', 'view_blend_color_match_strength',
+ 'allow_modify_existing_textures', 'ask_object_prompts', 'fallback_color',
+ 'controlnet_units', 'controlnet_units_index', 'lora_units', 'lora_units_index',
+ 'weight_exponent_mask', 'sequential_smooth',
+ 'canny_threshold_low', 'canny_threshold_high',
+ 'sequential_factor_smooth', 'sequential_factor_smooth_2', 'sequential_factor',
+ 'grow_mask_by', 'mask_blocky',
+ 'visibility_vignette', 'visibility_vignette_width', 'visibility_vignette_softness',
+ 'visibility_vignette_blur',
+ 'sg_silhouette_margin', 'sg_silhouette_depth', 'sg_silhouette_rays',
+ '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',
+ 'differential_diffusion', 'differential_noise',
+ 'blur_mask', 'blur_mask_radius', 'blur_mask_sigma',
+ 'sequential_custom_camera_order',
+ 'ipadapter_strength', 'ipadapter_start', 'ipadapter_end',
+ 'ipadapter_image', 'ipadapter_weight_type',
+ 'sequential_ipadapter', 'sequential_ipadapter_mode',
+ 'sequential_desaturate_factor', 'sequential_contrast_factor',
+ 'sequential_ipadapter_regenerate', 'sequential_ipadapter_regenerate_wo_controlnet',
+ 'clip_skip', 'stablegen_preset', 'active_preset',
+ 'model_architecture', 'architecture_mode',
+ 'output_timestamp', 'camera_prompts', 'use_camera_prompts',
+ 'sg_camera_order', 'sg_camera_order_index', 'sg_use_custom_camera_order',
+ 'show_core_settings', 'show_lora_settings', 'show_camera_options',
+ 'show_scene_understanding_settings', 'show_output_material_settings',
+ 'show_image_guidance_settings', 'show_masking_inpainting_settings',
+ 'show_mode_specific_settings',
+ 'generation_mode', 'early_priority_strength', 'early_priority', 'texture_objects',
+ 'qwen_generation_method', 'qwen_refine_use_prev_ref', 'qwen_refine_use_depth',
+ 'qwen_timestep_zero_ref',
+ 'qwen_guidance_map_type', 'qwen_voronoi_mode', 'qwen_context_render_mode',
+ 'qwen_use_external_style_image', 'qwen_external_style_image',
+ 'qwen_external_style_initial_only',
+ 'qwen_use_custom_prompts', 'qwen_custom_prompt_initial',
+ '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',
+ 'qwen_prompt_gray_background',
+ ]
+ for prop in _simple_scene_props:
+ if hasattr(bpy.types.Scene, prop):
+ delattr(bpy.types.Scene, prop)
+
+ # --- PBR Decomposition Properties ---
+ pbr_props = [
+ 'pbr_decomposition', 'pbr_albedo_source',
+ 'pbr_map_albedo', 'pbr_map_roughness', 'pbr_map_metallic',
+ 'pbr_map_normal', 'pbr_map_height', 'pbr_height_scale',
+ 'pbr_map_ao', 'pbr_ao_samples', 'pbr_ao_distance',
+ 'pbr_map_emission', 'pbr_emission_method',
+ 'pbr_emission_threshold', 'pbr_emission_saturation_min',
+ 'pbr_emission_value_min', 'pbr_emission_bloom',
+ 'pbr_emission_strength',
+ 'pbr_normal_strength', 'pbr_delight_strength',
+ 'pbr_use_native_resolution', 'pbr_tiling',
+ 'pbr_tile_albedo', 'pbr_tile_material', 'pbr_tile_normal', 'pbr_tile_height',
+ 'pbr_tile_emission', 'pbr_tile_grid', 'pbr_tile_superres',
+ 'pbr_processing_resolution', 'pbr_denoise_steps', 'pbr_ensemble_size',
+ 'pbr_albedo_auto_saturation', 'pbr_albedo_saturation_mode',
+ 'pbr_replace_color_with_albedo', 'pbr_auto_lighting',
+ 'pbr_model_variant', # legacy, kept for compat
+ ]
+ for prop in pbr_props:
+ if hasattr(bpy.types.Scene, prop):
+ delattr(bpy.types.Scene, prop)
+
+ # --- TRELLIS.2 Properties ---
+ trellis2_props = [
+ 'trellis2_available', 'pbr_nodes_available',
+ 'show_trellis2_params', 'show_trellis2_advanced',
+ 'show_trellis2_mesh_settings', 'show_trellis2_texture_settings',
+ 'show_trellis2_camera_settings',
+ 'trellis2_last_input_image', 'qwen_use_trellis2_style', 'qwen_trellis2_style_initial_only',
+ 'trellis2_pipeline_active', 'trellis2_pipeline_phase_start_pct',
+ 'trellis2_pipeline_total_phases',
+ 'trellis2_generate_from', 'trellis2_texture_mode', 'trellis2_initial_image_arch',
+ 'trellis2_camera_count',
+ 'trellis2_placement_mode', 'trellis2_auto_prompts',
+ 'trellis2_exclude_bottom', 'trellis2_exclude_bottom_angle',
+ 'trellis2_auto_aspect', 'trellis2_occlusion_mode',
+ 'trellis2_consider_existing', 'trellis2_delete_cameras',
+ 'trellis2_coverage_target',
+ 'trellis2_max_auto_cameras', 'trellis2_fan_angle',
+ 'trellis2_import_scale', 'trellis2_shade_mode',
+ 'trellis2_clamp_elevation', 'trellis2_max_elevation', 'trellis2_min_elevation',
+ 'trellis2_preview_gallery_enabled', 'trellis2_preview_gallery_count',
+ 'trellis2_input_image', 'trellis2_resolution', 'trellis2_vram_mode',
+ 'trellis2_attn_backend', 'trellis2_seed', 'trellis2_ss_guidance',
+ 'trellis2_ss_steps', 'trellis2_shape_guidance', 'trellis2_shape_steps',
+ 'trellis2_tex_guidance', 'trellis2_tex_steps', 'trellis2_max_tokens',
+ 'trellis2_texture_size', 'trellis2_decimation', 'trellis2_remesh',
+ 'trellis2_post_processing_enabled',
+ 'trellis2_auto_lighting',
+ 'trellis2_skip_texture', 'trellis2_bg_removal', 'trellis2_background_color',
+ ]
+ for prop in trellis2_props:
+ if hasattr(bpy.types.Scene, prop):
+ delattr(bpy.types.Scene, prop)
+
+ # --- Scene Queue cleanup ---
+ if hasattr(bpy.types.WindowManager, 'sg_scene_queue'):
+ del bpy.types.WindowManager.sg_scene_queue
+ if hasattr(bpy.types.WindowManager, 'sg_scene_queue_index'):
+ del bpy.types.WindowManager.sg_scene_queue_index
+ if hasattr(bpy.types.WindowManager, 'sg_show_queue'):
+ del bpy.types.WindowManager.sg_show_queue
+
+ for attr in (
+ 'sg_queue_gif_export', 'sg_queue_gif_duration', 'sg_queue_gif_fps',
+ 'sg_queue_gif_resolution', 'sg_queue_gif_samples', 'sg_queue_gif_engine',
+ 'sg_queue_gif_interpolation', 'sg_queue_gif_use_hdri',
+ 'sg_queue_gif_hdri_path', 'sg_queue_gif_hdri_strength',
+ 'sg_queue_gif_hdri_rotation', 'sg_queue_gif_env_mode',
+ 'sg_queue_gif_denoiser', 'sg_queue_gif_use_gpu',
+ 'sg_queue_gif_also_no_pbr',
+ ):
+ if hasattr(bpy.types.WindowManager, attr):
+ delattr(bpy.types.WindowManager, attr)
+
+ # --- Load handlers ---
+ if load_handler in bpy.app.handlers.load_post:
+ bpy.app.handlers.load_post.remove(load_handler)
+ if _sg_queue_load_handler in bpy.app.handlers.load_post:
+ bpy.app.handlers.load_post.remove(_sg_queue_load_handler)
diff --git a/stablegen/core/server_api.py b/stablegen/core/server_api.py
new file mode 100644
index 0000000..43dcdaf
--- /dev/null
+++ b/stablegen/core/server_api.py
@@ -0,0 +1,264 @@
+"""ComfyUI server communication helpers.
+
+Network functions that check server availability, query model lists,
+and fetch data from ComfyUI's REST API. All functions are safe to call
+from a background thread (they do not access ``bpy.context``).
+"""
+
+import json
+import os
+
+import requests
+
+from ..timeout_config import get_timeout
+from . import ADDON_PKG, get_addon_prefs
+
+
+# ── Server availability checks ────────────────────────────────────────────
+
+def check_server_availability(server_address, timeout=0.5):
+ """Quickly checks if the ComfyUI server is responding.
+
+ Args:
+ server_address (str): The address:port of the ComfyUI server.
+ timeout (float): Strict timeout in seconds for this check.
+
+ Returns:
+ bool: True if the server responds quickly, False otherwise.
+ """
+ if not server_address:
+ return False
+
+ url = f"http://{server_address}/queue"
+ try:
+ response = requests.head(url, timeout=timeout)
+ response.raise_for_status()
+ return True
+ except requests.exceptions.Timeout:
+ print(f"[StableGen] Initial server check failed: Timeout ({timeout}s).")
+ return False
+ except requests.exceptions.ConnectionError:
+ print("[StableGen] Initial server check failed: Connection Error.")
+ return False
+
+
+def check_trellis2_available(server_address, timeout=1.0):
+ """Checks if TRELLIS.2 custom nodes are installed in ComfyUI.
+
+ Args:
+ server_address (str): The address:port of the ComfyUI server.
+ timeout (float): Timeout in seconds.
+
+ Returns:
+ bool: True if TRELLIS.2 nodes are detected, False otherwise.
+ """
+ if not server_address:
+ return False
+ try:
+ url = f"http://{server_address}/object_info/Trellis2ImageToShape"
+ response = requests.get(url, timeout=timeout)
+ if response.status_code == 200:
+ data = response.json()
+ if 'Trellis2ImageToShape' in data:
+ print("[TRELLIS2] Auto-detect: TRELLIS.2 nodes found in ComfyUI.")
+ return True
+ print("[TRELLIS2] Auto-detect: TRELLIS.2 nodes NOT found in ComfyUI.")
+ return False
+ except Exception as e:
+ print(f"[TRELLIS2] Auto-detect failed: {e}")
+ return False
+ except requests.exceptions.RequestException as e:
+ print(f"[StableGen] Initial server check failed: Request Error ({e}).")
+ return False
+ except Exception as e:
+ print(f"[StableGen] Initial server check failed: Unexpected Error ({e}).")
+ return False
+
+
+def check_pbr_available(server_address, timeout=1.0):
+ """Checks if PBR decomposition custom nodes are installed in ComfyUI.
+
+ Looks for MarigoldModelLoader (ComfyUI-Marigold) and
+ LoadStableDelightModel (ComfyUI_StableDelight_ll).
+
+ Args:
+ server_address (str): The address:port of the ComfyUI server.
+ timeout (float): Timeout in seconds.
+
+ Returns:
+ bool: True if both node packages are detected, False otherwise.
+ """
+ if not server_address:
+ return False
+ required = ['MarigoldModelLoader', 'LoadStableDelightModel']
+ try:
+ for node_class in required:
+ url = f"http://{server_address}/object_info/{node_class}"
+ response = requests.get(url, timeout=timeout)
+ if response.status_code != 200 or node_class not in response.json():
+ print(f"[PBR] Auto-detect: node '{node_class}' NOT found in ComfyUI.")
+ return False
+ print("[PBR] Auto-detect: PBR decomposition nodes found in ComfyUI.")
+ return True
+ except Exception as e:
+ print(f"[PBR] Auto-detect failed: {e}")
+ return False
+
+
+# ── API data retrieval ─────────────────────────────────────────────────────
+
+def fetch_from_comfyui_api(context, endpoint):
+ """Fetches data from a specified ComfyUI API endpoint.
+
+ Args:
+ context: Blender context to access addon preferences.
+ endpoint (str): The API endpoint path (e.g., "/models/checkpoints").
+
+ Returns:
+ list: A list of items returned by the API (usually filenames),
+ or an empty list if the request fails or returns invalid data.
+ Returns None if the server address is not set.
+ """
+ addon_prefs = context.preferences.addons.get(ADDON_PKG)
+ if not addon_prefs:
+ print("[StableGen] Error: Could not access StableGen addon preferences.")
+ return None
+
+ server_address = addon_prefs.preferences.server_address
+ if not server_address:
+ print("[StableGen] Error: ComfyUI Server Address is not set in preferences.")
+ return None
+
+ if not check_server_availability(server_address, timeout=get_timeout('ping')):
+ return None
+
+ if not endpoint.startswith('/'):
+ endpoint = '/' + endpoint
+
+ url = f"http://{server_address}{endpoint}"
+
+ try:
+ response = requests.get(url, timeout=get_timeout('api'))
+ response.raise_for_status()
+ data = response.json()
+
+ if isinstance(data, list):
+ if all(isinstance(item, str) for item in data):
+ return data
+ elif data:
+ print(f"[StableGen] Warning: API endpoint {endpoint} returned a list, but it contains non-string items: {data[:5]}...")
+ string_items = [item for item in data if isinstance(item, str)]
+ if string_items:
+ return string_items
+ else:
+ print(f"[StableGen] Error: No valid string filenames found in list from {endpoint}.")
+ return []
+ else:
+ return []
+ else:
+ print(f"[StableGen] Error: API endpoint {endpoint} did not return a JSON list. Received: {type(data)}")
+ return []
+
+ except requests.exceptions.Timeout:
+ print(f"[StableGen] Error: Timeout connecting to {url}.")
+ except requests.exceptions.ConnectionError:
+ print(f"[StableGen] Error: Connection failed to {url}. Is ComfyUI running and accessible?")
+ except requests.exceptions.RequestException as e:
+ print(f"[StableGen] Error fetching from {url}: {e}")
+ except json.JSONDecodeError:
+ print(f"[StableGen] Error: Could not decode JSON response from {url}. Response text: {response.text}")
+ except Exception as e:
+ print(f"[StableGen] An unexpected error occurred fetching from {url}: {e}")
+
+ return []
+
+
+def _fetch_api_list(server_address, endpoint):
+ """Thread-safe variant of *fetch_from_comfyui_api* — takes an explicit
+ *server_address* instead of reading from bpy context, so it can run in a
+ background thread.
+
+ Returns a list of strings on success, ``None`` on connection/config error,
+ or ``[]`` when the server returns an empty or invalid list.
+ """
+ if not server_address:
+ return None
+
+ if not check_server_availability(server_address, timeout=get_timeout('ping')):
+ return None
+
+ if not endpoint.startswith('/'):
+ endpoint = '/' + endpoint
+
+ url = f"http://{server_address}{endpoint}"
+ try:
+ response = requests.get(url, timeout=get_timeout('api'))
+ response.raise_for_status()
+ data = response.json()
+
+ if isinstance(data, list):
+ string_items = [item for item in data if isinstance(item, str)]
+ return string_items
+ else:
+ print(f"[StableGen] Error: API endpoint {endpoint} did not return a JSON list.")
+ return []
+ except requests.exceptions.Timeout:
+ print(f"[StableGen] Error: Timeout connecting to {url}.")
+ except requests.exceptions.ConnectionError:
+ print(f"[StableGen] Error: Connection failed to {url}.")
+ except requests.exceptions.RequestException as e:
+ print(f"[StableGen] Error fetching from {url}: {e}")
+ except json.JSONDecodeError:
+ print(f"[StableGen] Error: Could not decode JSON from {url}.")
+ except Exception as e:
+ print(f"[StableGen] Unexpected error fetching from {url}: {e}")
+
+ return []
+
+
+# ── Local model scanning helpers ───────────────────────────────────────────
+
+def get_models_from_directory(scan_root_path: str, valid_extensions: tuple,
+ type_for_description: str, path_prefix_for_id: str = ""):
+ """Scans a given root directory for model files.
+
+ Returns paths relative to *scan_root_path*, optionally prefixed.
+ """
+ items = []
+ if not (scan_root_path and os.path.isdir(scan_root_path)):
+ return items
+
+ try:
+ for root, _, files in os.walk(scan_root_path):
+ for f_name in files:
+ if f_name.lower().endswith(valid_extensions):
+ full_path = os.path.join(root, f_name)
+ relative_path = os.path.relpath(full_path, scan_root_path)
+ identifier = path_prefix_for_id + relative_path
+ display_name = identifier
+ items.append((identifier, display_name, f"{type_for_description}: {display_name}"))
+ except PermissionError:
+ print(f"[StableGen] Permission Denied for {scan_root_path}")
+ except Exception as e:
+ print(f"[StableGen] Error Scanning {scan_root_path}: {e}")
+
+ return items
+
+
+def merge_and_deduplicate_models(model_lists: list):
+ """Merges multiple lists of model items and de-duplicates by identifier."""
+ merged_items = []
+ seen_identifiers = set()
+ for model_list in model_lists:
+ for identifier, name, description in model_list:
+ if identifier.startswith("NO_") or identifier.startswith("PERM_") or identifier.startswith("SCAN_") or identifier == "NONE_FOUND":
+ continue
+ if identifier not in seen_identifiers:
+ merged_items.append((identifier, name, description))
+ seen_identifiers.add(identifier)
+
+ if not merged_items:
+ merged_items.append(("NONE_AVAILABLE", "No Models Found", "Check ComfyUI and External Directories in Preferences"))
+
+ merged_items.sort(key=lambda x: x[1])
+ return merged_items
diff --git a/stablegen/core/state.py b/stablegen/core/state.py
new file mode 100644
index 0000000..c657483
--- /dev/null
+++ b/stablegen/core/state.py
@@ -0,0 +1,101 @@
+"""Shared mutable state and async helpers for the StableGen addon.
+
+Global caches for model lists, pending-refresh counters, and the
+``_run_async`` helper that runs blocking I/O on a background thread
+and delivers results back on Blender's main thread.
+"""
+
+import threading as _threading
+import time as _time_mod
+import traceback as _traceback
+
+import bpy # pylint: disable=import-error
+
+
+# ── Model-list caches (populated by refresh operators) ────────────────────
+_cached_checkpoint_list = [("NONE_AVAILABLE", "None available", "Fetch models from server")]
+_cached_lora_list = [("NONE_AVAILABLE", "None available", "Fetch models from server")]
+_cached_checkpoint_architecture = None
+_pending_checkpoint_refresh_architecture = None
+
+
+# ── In-flight refresh counter ─────────────────────────────────────────────
+_pending_refreshes = 0
+_refresh_started_at = 0.0 # monotonic timestamp of first in-flight refresh
+_REFRESH_TIMEOUT = 30.0 # seconds before we force-clear a stuck indicator
+
+
+def _inc_pending_refreshes():
+ """Increment the in-flight refresh counter (call from main thread)."""
+ global _pending_refreshes, _refresh_started_at
+ if _pending_refreshes <= 0:
+ _refresh_started_at = _time_mod.monotonic()
+ _pending_refreshes += 1
+
+
+def _dec_pending_refreshes():
+ """Decrement the in-flight refresh counter (call from main thread)."""
+ global _pending_refreshes, _refresh_started_at
+ _pending_refreshes = max(0, _pending_refreshes - 1)
+ if _pending_refreshes <= 0:
+ _refresh_started_at = 0.0
+
+
+# ── Async network helper ─────────────────────────────────────────────────
+
+# Monotonically incrementing token so we can discard stale results when the
+# server address changes while a request is still in-flight.
+_async_generation = 0
+
+
+def _run_async(work_fn, done_fn, poll_interval=0.25, track_generation=False):
+ """Run *work_fn* in a background thread; call *done_fn(result)* on the
+ main thread via ``bpy.app.timers`` when finished.
+
+ *work_fn* receives no arguments and should return a result dict/object.
+ *done_fn* receives that result. Both run without any lock — *done_fn*
+ is guaranteed to execute on the main Blender thread.
+
+ If *track_generation* is True the call increments the global
+ ``_async_generation`` counter and the result is silently discarded if
+ a newer tracked call was started before this one finishes. Use this
+ only for server-address-change callbacks where stale results must be
+ dropped; refresh operators should leave it False so their results are
+ never accidentally discarded.
+ """
+ global _async_generation
+ if track_generation:
+ _async_generation += 1
+ gen = _async_generation
+
+ container = {} # mutable box for the thread to deposit its result
+
+ def _worker():
+ try:
+ container['result'] = work_fn()
+ except Exception:
+ _traceback.print_exc()
+ container['result'] = None
+
+ t = _threading.Thread(target=_worker, daemon=True)
+ t.start()
+
+ def _poll():
+ # Discard result if the server address changed after we started.
+ if gen != _async_generation:
+ # Still call done_fn so it can clean up (e.g. decrement
+ # _pending_refreshes). Pass None to signal stale/no result.
+ try:
+ done_fn(None)
+ except Exception:
+ _traceback.print_exc()
+ return None # stop polling — stale
+ if t.is_alive():
+ return poll_interval # keep polling
+ try:
+ done_fn(container.get('result'))
+ except Exception:
+ _traceback.print_exc()
+ return None # done
+
+ bpy.app.timers.register(_poll, first_interval=poll_interval)
diff --git a/stablegen/debug_tools.py b/stablegen/debug_tools.py
index 4859294..3af8d0c 100644
--- a/stablegen/debug_tools.py
+++ b/stablegen/debug_tools.py
@@ -16,17 +16,15 @@ import colorsys
import numpy as np
from mathutils import Vector
-from .project import (
+from .texturing.projection import (
project_image,
create_native_raycast_visibility,
create_native_feather,
_SG_BUFFER_UV_NAME,
_copy_uv_to_attribute,
)
-from .render_tools import (
- export_visibility,
- _get_camera_resolution,
-)
+from .texturing.rendering import export_visibility
+from .cameras.geometry import _get_camera_resolution
from .utils import get_file_path, get_generation_dirs, ensure_dirs_exist, sg_modal_active
diff --git a/stablegen/generator.py b/stablegen/generator.py
deleted file mode 100644
index 1831c16..0000000
--- a/stablegen/generator.py
+++ /dev/null
@@ -1,4672 +0,0 @@
-import os
-import bpy # pylint: disable=import-error
-import mathutils # pylint: disable=import-error
-import numpy as np
-import cv2
-
-import uuid
-import json
-import urllib.request
-import urllib.parse
-import socket
-import threading
-import requests
-import traceback
-import io
-from datetime import datetime
-import math
-import colorsys
-from PIL import Image, ImageEnhance
-
-import gpu # pylint: disable=import-error
-import blf # pylint: disable=import-error
-from gpu_extras.batch import batch_for_shader # pylint: disable=import-error
-
-from .util.helpers import prompt_text, prompt_text_img2img, prompt_text_qwen_image_edit # pylint: disable=relative-beyond-top-level
-from .render_tools import export_emit_image, export_visibility, export_canny, bake_texture, prepare_baking, unwrap, export_render, export_viewport, render_edge_feather_mask, _SGCameraResolution, _get_camera_resolution, _sg_restore_square_display, _sg_remove_crop_overlay, _sg_ensure_crop_overlay, _sg_hide_label_overlay, _sg_restore_label_overlay # pylint: disable=relative-beyond-top-level
-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 # pylint: disable=relative-beyond-top-level
-from .project import project_image, reinstate_compare_nodes # pylint: disable=relative-beyond-top-level
-from .workflows import WorkflowManager
-from .util.mirror_color import MirrorReproject, _get_viewport_ref_np, _apply_color_match_to_file
-from .timeout_config import get_timeout
-
-# Import wheels
-import websocket
-
-def redraw_ui(context):
- """Redraws the UI to reflect changes in the operator's progress and status."""
- for area in context.screen.areas:
- area.tag_redraw()
-
-
-def setup_studio_lighting(context, scale=1.0):
- """Create a three-point studio lighting rig (key, fill, rim).
-
- Re-usable from any generation mode (TRELLIS.2, PBR decomposition, etc.).
-
- Args:
- context: Blender context.
- scale: Scene scale factor — lights are placed at ``scale * 2.5`` distance.
- """
- S = max(scale, 0.5)
- dist = S * 2.5
-
- light_defs = [
- ("SG_Key", 200, 1.5 * S, (1.0, 0.96, 0.90), 45, 40),
- ("SG_Fill", 80, 2.5 * S, (0.90, 0.94, 1.0), -60, 15),
- ("SG_Rim", 120, 0.8 * S, (1.0, 1.0, 1.0), 170, 55),
- ]
-
- collection = context.collection
- created = []
-
- for name, power, size, color, az_deg, el_deg in light_defs:
- old = bpy.data.objects.get(name)
- if old:
- bpy.data.objects.remove(old, do_unlink=True)
-
- az = math.radians(az_deg)
- el = math.radians(el_deg)
- x = dist * math.cos(el) * math.sin(az)
- y = -dist * math.cos(el) * math.cos(az)
- z = dist * math.sin(el)
-
- light_data = bpy.data.lights.new(name=name, type='AREA')
- light_data.energy = power
- light_data.size = size
- light_data.color = color
-
- light_obj = bpy.data.objects.new(name=name, object_data=light_data)
- collection.objects.link(light_obj)
-
- light_obj.location = (x, y, z)
- direction = mathutils.Vector((0, 0, 0)) - mathutils.Vector((x, y, z))
- rot = direction.to_track_quat('-Z', 'Y')
- light_obj.rotation_euler = rot.to_euler()
-
- created.append(light_obj)
-
- print(f"[StableGen] Studio lighting created: {[o.name for o in created]}")
- return created
-
-
-def _pbr_setup_studio_lights(context, to_texture):
- """Calculate scene scale from target objects and set up studio lights."""
- max_dim = 1.0
- for obj in to_texture:
- if hasattr(obj, 'dimensions'):
- max_dim = max(max_dim, *obj.dimensions)
- setup_studio_lighting(context, scale=max_dim)
-
-
-class Regenerate(bpy.types.Operator):
- """Regenerate textures for selected cameras / viewpoints
- - Works for sequential and separate generation modes
- - Generates new images for the selected cameras only, keeping existing images for unselected cameras
- - This can be used with different prompts or settings to refine specific viewpoints without affecting others"""
- bl_idname = "object.stablegen_regenerate"
- bl_label = "Regenerate Selected Viewpoints"
- bl_options = {'REGISTER', 'UNDO'}
-
- _original_method = None
- _original_overwrite_material = None
- _timer = None
- _to_texture = None
- @classmethod
- def poll(cls, context):
- addon_prefs = context.preferences.addons[__package__].preferences
- if not os.path.exists(addon_prefs.output_dir):
- cls.poll_message_set("Output directory not set or does not exist")
- return False
- if not addon_prefs.server_address or not addon_prefs.server_online:
- cls.poll_message_set("ComfyUI server is not connected")
- return False
- if not (context.scene.generation_method == 'sequential' or context.scene.generation_method == 'separate'):
- cls.poll_message_set("Regenerate is only available in Sequential or Separate mode")
- return False
- if context.scene.output_timestamp == "":
- cls.poll_message_set("No previous generation to regenerate from")
- return False
- if context.scene.generation_status == 'waiting' or sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- return True
-
- def execute(self, context):
- """
- Executes the operator.
- :param context: Blender context.
- :return: {'FINISHED'}
- """
-
- self._original_overwrite_material = context.scene.overwrite_material
- # Set the flag to reproject
- context.scene.generation_mode = 'regenerate_selected'
- # Set the generation method to 'separate' to avoid generating new images
- context.scene.overwrite_material = True
- # Set timer to 1 seconds to give some time for the generate to start
- context.window_manager.modal_handler_add(self)
- self._timer = context.window_manager.event_timer_add(1.0, window=context.window)
- # Revert to original discard angle in material nodes in case it was reset after generation
- if context.scene.texture_objects == 'selected':
- self._to_texture = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
- # If empty, cancel the operation
- if not self._to_texture:
- self.report({'ERROR'}, "No mesh objects selected. Select mesh objects in the viewport before regenerating.")
- context.scene.generation_status = 'idle'
- ComfyUIGenerate._is_running = False
- 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()]
- # Revert discard angle
- new_discard_angle = context.scene.discard_factor
- 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
- # Run the generation operator (bypass modal poll guard since we are
- # already registered as a modal operator ourselves)
- from . import utils as _sg_utils
- _sg_utils._sg_bypass_modal_check = True
- try:
- bpy.ops.object.test_stable('INVOKE_DEFAULT')
- finally:
- _sg_utils._sg_bypass_modal_check = False
-
- # Switch to modal and wait for completion
- print("Going modal")
- return {'RUNNING_MODAL'}
-
- def modal(self, context, event):
- """
- Handles modal events.
- :param context: Blender context.
- :param event: Blender event.
- :return: {'PASS_THROUGH'}
- """
- if event.type == 'TIMER':
- running = False
- if ComfyUIGenerate._is_running:
- running = True
- if not running:
- # Reset the generation method and overwrite material flag
- context.scene.overwrite_material = self._original_overwrite_material
- # Reset the project only flag
- context.scene.generation_mode = 'standard'
- # Remove the modal handler
- context.window_manager.event_timer_remove(self._timer)
- # Report completion
- self.report({'INFO'}, "Regeneration complete.")
- return {'FINISHED'}
- return {'PASS_THROUGH'}
-
-class Reproject(bpy.types.Operator):
- """Rerun projection of existing images
- - Uses the Generate operator to reproject images, new textures will respect new Viewpoint Blending Settings
- - Will not work with textures which used refine mode with the preserve parameter enabled"""
- bl_idname = "object.stablegen_reproject"
- bl_label = "Reproject Images"
- bl_options = {'REGISTER', 'UNDO'}
-
- _original_method = None
- _original_overwrite_material = None
- _timer = None
- @classmethod
- def poll(cls, context):
- if context.scene.output_timestamp == "":
- cls.poll_message_set("No previous generation to reproject from")
- return False
- if context.scene.generation_status == 'waiting' or sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- return True
-
- def execute(self, context):
- """
- Executes the operator.
- :param context: Blender context.
- :return: {'FINISHED'}
- """
- if context.scene.texture_objects == 'all':
- to_texture = [obj for obj in bpy.context.view_layer.objects if obj.type == 'MESH' and not obj.hide_get()]
- else: # selected
- to_texture = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
-
- # Search for largest material id
- max_id = -1
- for obj in to_texture:
- mat_id = get_last_material_index(obj)
- if mat_id > max_id:
- max_id = mat_id
-
- cameras = [obj for obj in bpy.context.scene.objects if obj.type == 'CAMERA']
- for i, _ in enumerate(cameras):
- # Check if the camera has a corresponding generated image
- image_path = get_file_path(context, "generated", camera_id=i, material_id=max_id)
- if not os.path.exists(image_path):
- # Try to recover from a packed/embedded Blender image
- # (e.g. after saving and reopening the .blend file when the
- # original output directory no longer exists).
- recovered = self._recover_image_from_blend(
- image_path, to_texture, max_id)
- if not recovered:
- self.report(
- {'ERROR'},
- f"Camera {i} does not have a corresponding "
- f"generated image.")
- print(f"{image_path} does not exist")
- return {'CANCELLED'}
-
- self._original_method = context.scene.generation_method
- self._original_overwrite_material = context.scene.overwrite_material
- # Set the flag to reproject
- context.scene.generation_mode = 'project_only'
- # Set the generation method to 'separate' to avoid generating new images
- context.scene.generation_method = 'separate'
- context.scene.overwrite_material = True
- # Set timer to 1 seconds to give some time for the generate to start
- context.window_manager.modal_handler_add(self)
- self._timer = context.window_manager.event_timer_add(1.0, window=context.window)
- # Run the generation operator (bypass modal poll guard since we are
- # already registered as a modal operator ourselves)
- from . import utils as _sg_utils
- _sg_utils._sg_bypass_modal_check = True
- try:
- bpy.ops.object.test_stable('INVOKE_DEFAULT')
- finally:
- _sg_utils._sg_bypass_modal_check = False
-
- # Switch to modal and wait for completion
- print("Going modal")
- return {'RUNNING_MODAL'}
-
- @staticmethod
- def _recover_image_from_blend(image_path, to_texture, material_id):
- """Try to recover a generated image from bpy.data.images.
-
- When a .blend file is saved after generation, the generated
- textures are packed/embedded as Blender image data-blocks. If
- the original output directory no longer exists (e.g. moved PC,
- temp folder cleared), we can find the image in the material's
- node tree and re-save it to the expected path.
-
- Returns True if the file was successfully recovered.
- """
- target_name = os.path.basename(image_path)
-
- def _try_save_image(img):
- """Attempt to write an image data-block to *image_path*."""
- os.makedirs(os.path.dirname(image_path), exist_ok=True)
-
- # Case 1: packed file — write raw bytes
- if img.packed_file:
- try:
- with open(image_path, 'wb') as f:
- f.write(img.packed_file.data)
- print(f"[StableGen] Recovered packed image: "
- f"{target_name}")
- return True
- except Exception as err:
- print(f"[StableGen] Packed write failed: {err}")
-
- # Case 2: image has a valid filepath elsewhere — copy it
- existing_path = bpy.path.abspath(img.filepath_raw)
- if existing_path and os.path.isfile(existing_path):
- try:
- import shutil
- shutil.copy2(existing_path, image_path)
- print(f"[StableGen] Copied image from "
- f"{existing_path} → {image_path}")
- return True
- except Exception as err:
- print(f"[StableGen] File copy failed: {err}")
-
- # Case 3: pixel data in memory — save via render
- try:
- if img.has_data or len(img.pixels) > 0:
- img.save_render(image_path)
- print(f"[StableGen] Recovered image via save_render: "
- f"{target_name}")
- return True
- except Exception:
- pass
-
- return False
-
- # Strategy 1: scan Image Texture nodes in the target materials
- # (most reliable — images are directly referenced by the object
- # being reprojected, so we won't accidentally pick up images
- # from a different model/object that share the same filename).
- for obj in to_texture:
- if not hasattr(obj, 'data') or not obj.data.materials:
- continue
- for mat in obj.data.materials:
- if not mat or not mat.use_nodes:
- continue
- for node in mat.node_tree.nodes:
- if node.type == 'TEX_IMAGE' and node.image:
- node_filename = os.path.basename(
- node.image.filepath_raw)
- if node_filename == target_name:
- if _try_save_image(node.image):
- return True
-
- # Strategy 2: direct name lookup in bpy.data.images (fallback)
- for img in bpy.data.images:
- if img.name == target_name or os.path.basename(
- img.filepath_raw) == target_name:
- if _try_save_image(img):
- return True
-
- return False
-
- def modal(self, context, event):
- if event.type == 'TIMER':
- running = False
- if ComfyUIGenerate._is_running:
- running = True
- if not running:
- # Reset the generation method and overwrite material flag
- context.scene.generation_method = self._original_method
- context.scene.overwrite_material = self._original_overwrite_material
- # Reset the project only flag
- context.scene.generation_mode = 'standard'
- # Remove the modal handler
- context.window_manager.event_timer_remove(self._timer)
- # Report completion
- self.report({'INFO'}, "Reprojection complete.")
- return {'FINISHED'}
- return {'PASS_THROUGH'}
-
-def upload_image_to_comfyui(server_address, image_path, image_type="input"):
- """
- Uploads an image file to the ComfyUI server's /upload/image endpoint.
-
- Args:
- server_address (str): The address:port of the ComfyUI server (e.g., "127.0.0.1:8188").
- image_path (str): The local path to the image file to upload.
- image_type (str): The type parameter for the upload (usually "input").
-
- Returns:
- dict: A dictionary containing the server's response (e.g., {'name': 'filename.png', 'subfolder': '', 'type': 'input'})
- Returns None if the upload fails or file doesn't exist.
- """
- if not os.path.exists(image_path):
- # This is expected for optional files, so don't log as an error
- # print(f"Debug: Image file not found at {image_path}, cannot upload.")
- return None
- if not os.path.isfile(image_path):
- print(f"Error: Path exists but is not a file: {image_path}")
- return None
-
- upload_url = f"http://{server_address}/upload/image"
- print(f"Uploading {os.path.basename(image_path)} to {upload_url}...")
-
- try:
- with open(image_path, 'rb') as f:
- # Determine mime type based on extension
- mime_type = 'application/octet-stream' # Default fallback
- if image_path.lower().endswith('.png'):
- mime_type = 'image/png'
- elif image_path.lower().endswith(('.jpg', '.jpeg')):
- mime_type = 'image/jpeg'
- elif image_path.lower().endswith('.webp'):
- mime_type = 'image/webp'
- # Add other types if needed (e.g., .bmp, .gif)
-
- files = {'image': (os.path.basename(image_path), f, mime_type)}
- # 'overwrite': 'true' prevents errors if the same filename is uploaded again
- # useful for re-running generations with the same intermediate files.
- data = {'overwrite': 'true', 'type': image_type}
-
- # Increased timeout for potentially large images or slow networks
- response = requests.post(upload_url, files=files, data=data, timeout=get_timeout('transfer'))
- response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
-
- response_data = response.json()
- print(f" Upload successful for '{os.path.basename(image_path)}'. Server response: {response_data}")
-
- # Crucial Validation
- if 'name' not in response_data:
- print(f" Error: ComfyUI upload response for {os.path.basename(image_path)} missing 'name'. Response: {response_data}")
- return None
- # End Validation
-
- return response_data # Should contain 'name', often 'subfolder', 'type'
-
- except requests.exceptions.Timeout:
- print(f" Error: Timeout uploading image {os.path.basename(image_path)} to {upload_url}.")
- except requests.exceptions.ConnectionError:
- print(f" Error: Connection failed when uploading image {os.path.basename(image_path)} to {upload_url}. Is ComfyUI running and accessible?")
- except requests.exceptions.HTTPError as e:
- print(f" Error: HTTP Error {e.response.status_code} uploading image {os.path.basename(image_path)} to {upload_url}.")
- print(f" Server response content: {e.response.text}") # Show response body on error
- except requests.exceptions.RequestException as e:
- print(f" Error uploading image {os.path.basename(image_path)} to {upload_url}: {e}")
- except json.JSONDecodeError:
- print(f" Error decoding ComfyUI response after uploading {os.path.basename(image_path)}. Response text: {response.text}")
- except Exception as e:
- print(f" An unexpected error occurred during image upload of {os.path.basename(image_path)}: {e}")
- traceback.print_exc() # Print full traceback for unexpected errors
-
- return None
-
-class ComfyUIGenerate(bpy.types.Operator):
- """Generate textures using ComfyUI (to all mesh objects using all cameras in the scene)
-
- - Multiple modes are available. Choose by setting Generation Mode in the UI.
- - This includes texture generation and projection to the mesh objects.
- - By default, the generated textures will only be visible in the Rendered viewport shading mode."""
- bl_idname = "object.test_stable"
- bl_label = "Generate using ComfyUI"
- bl_options = {'REGISTER', 'UNDO'}
-
- _timer = None
- _progress = 0
- _error = None
- _is_running = False
- _active_ws = None # WebSocket reference for cancel-time close
- _threads_left = 0
- _cameras = None
- _selected_camera_ids = None
- _grid_width = 0
- _grid_height = 0
- _material_id = -1
- _to_texture = None
- _original_visibility = None
- _generation_method_on_start = None
- _uploaded_images_cache: dict = {}
- workflow_manager: object = None
-
- # Add properties to track progress
- _progress = 0.0
- _stage = ""
- _current_image = 0
- _total_images = 0
- _wait_event = None
- # PBR progress (model-level steps across all cameras)
- _pbr_active = False
- _pbr_step = 0
- _pbr_total_steps = 0
- _pbr_cam = 0 # current camera index within current step
- _pbr_cam_total = 1 # total cameras in current step
-
- # Add new properties at the top of the class
- _object_prompts: dict = {}
- show_prompt_dialog: bpy.props.BoolProperty(default=True)
- current_object_name: bpy.props.StringProperty()
- current_object_prompt: bpy.props.StringProperty(
- name="Object Prompt",
- description="Enter a specific prompt for this object",
- default=""
- ) # type: ignore
- # New properties for prompt collection
- _mesh_objects: list = []
- mesh_index: int = 0
-
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self._total_images = 0
- self._current_image = 0
- self._stage = ""
- self._progress = 0
- self._pbr_active = False
- self._pbr_step = 0
- self._pbr_total_steps = 0
- self._pbr_cam = 0
- self._pbr_cam_total = 1
- self._wait_event = threading.Event()
- self.workflow_manager = WorkflowManager(self)
-
- def _run_on_main_thread(self, func):
- """Execute *func* on Blender's main thread via a timer callback and
- block until it completes. Sets ``self._error`` on failure."""
- def _callback():
- try:
- func()
- except Exception as exc:
- self._error = str(exc)
- traceback.print_exc()
- self._wait_event.set()
- return None # one-shot timer
- bpy.app.timers.register(_callback)
- self._wait_event.wait()
- self._wait_event.clear()
-
- 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):
- if cls._is_running:
- return True # Allow cancellation
- # Check for other modal operators
- if context.scene.generation_status == 'waiting' or sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- addon_prefs = context.preferences.addons[__package__].preferences
- if not os.path.exists(addon_prefs.output_dir):
- cls.poll_message_set("Output directory not set or does not exist (check addon preferences)")
- return False
- if not addon_prefs.server_address or not addon_prefs.server_online:
- cls.poll_message_set("ComfyUI server is not connected")
- return False
- if bpy.app.online_access == False:
- cls.poll_message_set("Blender's online access is disabled (File → Preferences → System)")
- return False
- return True
-
- def execute(self, context):
- """
- Executes the operator.
- :param context: Blender context.
- :return: {'RUNNING_MODAL'}
- """
- if ComfyUIGenerate._is_running:
- self.cancel_generate(context)
- return {'FINISHED'}
-
- self._generation_method_on_start = context.scene.generation_method
-
- # Clear the upload cache at the start of a new generation
- self._uploaded_images_cache.clear()
-
- # Timestamp for output directory
- if context.scene.generation_mode == 'standard':
- context.scene.output_timestamp = datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
-
- # If UV inpainting and we're in prompt collection mode, collect prompts first.
- if context.scene.generation_method == 'uv_inpaint' and self.show_prompt_dialog:
- self._object_prompts[self.current_object_name] = self.current_object_prompt
- if self.mesh_index < len(self._to_texture) - 1:
- self.mesh_index += 1
- self.current_object_name = self._to_texture[self.mesh_index]
- self.current_object_prompt = ""
- return context.window_manager.invoke_props_dialog(self, width=400)
- else:
- self.show_prompt_dialog = False
-
-
- context.scene.generation_status = 'running'
- context.scene.sg_last_gen_error = False
- ComfyUIGenerate._is_running = True
-
- print("Executing ComfyUI Generation")
-
- if context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein') and not context.scene.generation_mode == 'project_only':
- context.scene.generation_method = 'sequential' # Force sequential for edit models
-
- render = bpy.context.scene.render
-
- # Force 100% resolution scale so depth/guide map renders match the
- # base resolution_x × resolution_y. The Qwen workflow derives its
- # output size from the uploaded guidance map, so a non-100% percentage
- # causes it to generate at the wrong resolution.
- self._original_resolution_percentage = render.resolution_percentage
- self._original_resolution_x = render.resolution_x
- self._original_resolution_y = render.resolution_y
- render.resolution_percentage = 100
-
- resolution_x = render.resolution_x
- resolution_y = render.resolution_y
- total_pixels = resolution_x * resolution_y
-
- # Qwen Image Edit benefits from 112-aligned resolution (LCM of VAE=8,
- # ViT patch=14, spatial merge=2×14=28, ViT window=112) to avoid
- # subtle pixel shifts between the latent, VAE and CLIP grids.
- # FLUX.2 Klein uses 16x latent downscale, so 16-aligned is needed.
- use_qwen_alignment = (
- context.scene.model_architecture.startswith('qwen')
- and getattr(context.scene, 'qwen_rescale_alignment', False)
- )
- if use_qwen_alignment:
- align_step = 112
- elif context.scene.model_architecture == 'flux2_klein':
- align_step = 16
- else:
- align_step = 8
-
- target_px = int(getattr(context.scene, 'auto_rescale_target_mp', 1.0) * 1_000_000)
- upper_bound = int(target_px * 1.2)
- lower_bound = int(target_px * 0.8)
-
- if context.scene.auto_rescale and ((total_pixels > upper_bound or total_pixels < lower_bound) or (resolution_x % align_step != 0 or resolution_y % align_step != 0)):
- scale_factor = (target_px / total_pixels) ** 0.5
- render.resolution_x = int(resolution_x * scale_factor)
- render.resolution_y = int(resolution_y * scale_factor)
- # Round down to nearest multiple of align_step
- render.resolution_x -= render.resolution_x % align_step
- render.resolution_y -= render.resolution_y % align_step
- self.report({'INFO'}, f"Resolution automatically rescaled to {render.resolution_x}x{render.resolution_y}.")
-
- elif total_pixels > upper_bound:
- target_mp = target_px / 1_000_000
- self.report({'WARNING'}, f"High resolution detected. Resolutions above {target_mp:.1f} MP may reduce performance and quality.")
-
- self._cameras = [obj for obj in bpy.context.scene.objects if obj.type == 'CAMERA']
- if not self._cameras:
- self.report({'ERROR'}, "No cameras found in the scene. Use 'Add Cameras' to create them.")
- context.scene.generation_status = 'idle'
- context.scene.sg_last_gen_error = True
- ComfyUIGenerate._is_running = False
- render.resolution_percentage = self._original_resolution_percentage
- render.resolution_x = self._original_resolution_x
- render.resolution_y = self._original_resolution_y
- return {'CANCELLED'}
- # Sort cameras by name
- self._cameras.sort(key=lambda x: x.name)
-
- # Apply custom generation order if enabled (non-destructive reorder)
- if context.scene.sg_use_custom_camera_order and len(context.scene.sg_camera_order) > 0:
- order_names = [item.name for item in context.scene.sg_camera_order]
- cam_by_name = {cam.name: cam for cam in self._cameras}
- ordered = []
- for name in order_names:
- if name in cam_by_name:
- ordered.append(cam_by_name.pop(name))
- # Append any cameras not in the order list (newly added cameras)
- for cam in self._cameras:
- if cam.name in cam_by_name:
- ordered.append(cam)
- self._cameras = ordered
-
- # Hide crop and label overlays during generation
- _sg_remove_crop_overlay()
- _sg_hide_label_overlay()
-
- # Auto-rescale per-camera resolutions (if any cameras have sg_res_x/y)
- if context.scene.auto_rescale:
- for cam in self._cameras:
- if "sg_res_x" in cam and "sg_res_y" in cam:
- crx, cry = int(cam["sg_res_x"]), int(cam["sg_res_y"])
- c_total = crx * cry
- if (c_total > upper_bound or c_total < lower_bound) or (crx % align_step != 0 or cry % align_step != 0):
- sf = (target_px / c_total) ** 0.5
- crx = int(crx * sf)
- cry = int(cry * sf)
- crx -= crx % align_step
- cry -= cry % align_step
- cam["sg_res_x"] = crx
- cam["sg_res_y"] = cry
- self._selected_camera_ids = [i for i, cam in enumerate(self._cameras) if cam in bpy.context.selected_objects] #TEST
- if len(self._selected_camera_ids) == 0:
- self._selected_camera_ids = list(range(len(self._cameras))) # All cameras selected if none are selected
-
- # Check if there is at least one ControlNet unit
- controlnet_units = getattr(context.scene, "controlnet_units", [])
- if not controlnet_units and not (context.scene.use_flux_lora and context.scene.model_architecture == 'flux1') and context.scene.model_architecture != 'flux2_klein':
- self.report({'ERROR'}, "At least one ControlNet unit is required. Add one in the Guidance section below.")
- context.scene.generation_status = 'idle'
- context.scene.sg_last_gen_error = True
- ComfyUIGenerate._is_running = False
- render.resolution_percentage = self._original_resolution_percentage
- render.resolution_x = self._original_resolution_x
- render.resolution_y = self._original_resolution_y
- return {'CANCELLED'}
-
- # If there are curves within the scene, warn the user
- if any(obj.type == 'CURVE' for obj in bpy.context.view_layer.objects):
- self.report({'WARNING'}, "Curves detected in the scene. Consider using 'Convert Curves to Mesh' in the Tools section.")
-
- if context.scene.generation_mode == 'project_only':
- print(f"Reprojecting images for {len(self._cameras)} cameras")
- elif context.scene.generation_mode == 'standard':
- print(f"Generating images for {len(self._cameras)} cameras")
- else:
- print(f"Regenerating images for {len(self._selected_camera_ids)} selected cameras")
-
- if context.scene.texture_objects == 'selected':
- self._to_texture = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
- # If empty, cancel the operation
- if not self._to_texture:
- self.report({'ERROR'}, "No mesh objects selected for texturing.")
- context.scene.generation_status = 'idle'
- context.scene.sg_last_gen_error = True
- ComfyUIGenerate._is_running = False
- render.resolution_percentage = self._original_resolution_percentage
- render.resolution_x = self._original_resolution_x
- render.resolution_y = self._original_resolution_y
- 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()]
-
- # Find all mesh objects, check their material ids and store the highest one
- for obj in self._to_texture:
- for slot in obj.material_slots:
- material_id = get_last_material_index(obj)
- if (material_id > self._material_id):
- self._material_id = material_id
- # Check if there's room for the projection buffer UV map (only 1 slot needed)
- # Projection UV data is stored as attributes (no slot limit), but we need
- # 1 temporary buffer UV slot for the UV Project modifier
- has_buffer = obj.data.uv_layers.get("_SG_ProjectionBuffer") is not None
- if not has_buffer and len(obj.data.uv_layers) >= 8:
- self.report({'ERROR'}, "Not enough UV map slots (max 8). Remove an existing UV map on the object to free a slot for the projection buffer.")
- context.scene.generation_status = 'idle'
- context.scene.sg_last_gen_error = True
- ComfyUIGenerate._is_running = False
- render.resolution_percentage = self._original_resolution_percentage
- render.resolution_x = self._original_resolution_x
- render.resolution_y = self._original_resolution_y
- 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')):
- self._material_id += 1
-
- self._controlnet_units = list(controlnet_units)
-
- # Prepare for generating
- if context.scene.generation_method == 'grid':
- self._threads_left = 1
- if context.scene.generation_method == 'uv_inpaint':
- self._threads_left = len(self._to_texture)
- else:
- self._threads_left = len(self._cameras)
-
- self._original_visibility = {}
- if context.scene.texture_objects == 'selected':
- # Hide unselected objects for rendering
- for obj in bpy.context.view_layer.objects:
- if obj.type == 'MESH' and obj not in self._to_texture:
- # Save original visibility
- self._original_visibility[obj.name] = obj.hide_render
- obj.hide_render = True
-
-
- # UV inpainting mode preparation
- if context.scene.generation_method == 'uv_inpaint':
- # Check if there are baked textures for all objects
-
- if self.show_prompt_dialog:
- # Start the prompt collection process with the first object
- if not self._object_prompts: # Only if prompts haven't been collected
- self.current_object_name = self._to_texture[0].name
- return context.window_manager.invoke_props_dialog(self, width=400)
-
- # Continue with normal execution if all prompts are collected
- for obj in self._to_texture:
- # Use get_file_path to check for baked texture existence
- baked_texture_path = get_file_path(context, "baked", object_name=obj.name)
- if not os.path.exists(baked_texture_path):
- # Bake the texture if it doesn't exist
- self._stage = f"Baking UV Textures ({obj.name})"
- prepare_baking(context)
- unwrap(obj, method='pack', overlap_only=True)
- bake_texture(context, obj, texture_resolution=2048, output_dir=get_dir_path(context, "baked"))
-
- # Check if the material is compatible (uses projection shader)
- active_material = obj.active_material
- if not active_material or not active_material.use_nodes:
- error = True
- else:
- # Check if the last node before the output is a color mix node or a bsdf shader node with a color mix node before it
- output_node = None
- for node in active_material.node_tree.nodes:
- if node.type == 'OUTPUT_MATERIAL':
- output_node = node
- break
- if not output_node:
- error = True
- else:
- # Check if the last node before the output is a color mix node or a bsdf shader node with a color mix node before it
- for link in output_node.inputs[0].links:
- if link.from_node.type == 'MIX_RGB' or (link.from_node.type == 'BSDF_PRINCIPLED' and any(n.type == 'MIX_RGB' for n in link.from_node.inputs)):
- error = False
- break
- else:
- error = True
- if error:
- self.report({'ERROR'}, f"Cannot use UV inpainting with the material of object '{obj.name}'. The generated StableGen material must be the active material.")
- context.scene.generation_status = 'idle'
- ComfyUIGenerate._is_running = False
- render.resolution_percentage = self._original_resolution_percentage
- render.resolution_x = self._original_resolution_x
- render.resolution_y = self._original_resolution_y
- return {'CANCELLED'}
-
- # Export visibility masks for each object
- self._stage = f"Computing Visibility ({obj.name})"
- export_visibility(context, None, obj)
-
- if context.scene.view_blend_use_color_match and self._to_texture:
- self._stage = "Matching Colors"
- # Use the first target object as the reference for viewport color
- ref_np = _get_viewport_ref_np(self._to_texture[0])
- if ref_np is not None:
- # Apply color match to ALL generated camera images for this material
- for cam_idx, cam in enumerate(self._cameras):
- image_path = get_file_path(
- context,
- "generated",
- camera_id=cam_idx,
- material_id=self._material_id,
- )
- _apply_color_match_to_file(
- image_path=image_path,
- ref_rgb=ref_np,
- scene=context.scene,
- )
-
- self.prompt_text = context.scene.comfyui_prompt
-
- self._progress = 0.0
- if context.scene.generation_mode == 'project_only':
- self._stage = "Reprojecting"
- else:
- self._stage = "Starting"
- redraw_ui(context)
- self._current_image = 0
- self._total_images = len(self._cameras)
- if context.scene.generation_method == 'grid':
- self._total_images = 1
- if context.scene.refine_images:
- self._total_images += len(self._cameras) # Add refinement steps
- elif context.scene.generation_method == 'uv_inpaint':
- self._total_images = len(self._to_texture)
-
- # Regenerate mode preparation
- if context.scene.generation_mode == 'regenerate_selected':
- if context.scene.generation_method == 'sequential':
- # Sequential regeneration: reset all cameras from the first
- # selected onward so the projection sequence replays correctly.
- # Non-selected cameras reuse their existing images but still
- # get reprojected, keeping subsequent cameras' context intact.
- first_selected = min(self._selected_camera_ids)
- ids = [(cid, self._material_id)
- for cid in range(first_selected, len(self._cameras))]
- reinstate_compare_nodes(context, self._to_texture, ids)
- self._current_image = first_selected
- self._threads_left = len(self._cameras) - first_selected
- else:
- # Non-sequential modes: only reset selected cameras
- ids = [(cid, self._material_id)
- for cid in self._selected_camera_ids]
- reinstate_compare_nodes(context, self._to_texture, ids)
-
- # Add modal timer
- context.window_manager.modal_handler_add(self)
- self._timer = context.window_manager.event_timer_add(0.5, window=context.window)
- print("Starting thread")
- if context.scene.generation_method == 'grid':
- self._thread = threading.Thread(target=self.async_generate, args=(context,))
- else:
- _start_cam = self._current_image # 0 normally, first_selected for sequential regen
- self._thread = threading.Thread(target=self.async_generate, args=(context, _start_cam))
-
- self._thread.start()
-
- return {'RUNNING_MODAL'}
-
-
- def modal(self, context, event):
- """
- Handles modal events.
- :param context: Blender context.
- :param event: Blender event.
- :return: {'PASS_THROUGH'}
- """
- if event.type == 'TIMER':
- redraw_ui(context)
-
- if not self._thread.is_alive():
- context.window_manager.event_timer_remove(self._timer)
- ComfyUIGenerate._is_running = False
- # Restore resolution_percentage that was forced to 100 in execute()
- if hasattr(self, '_original_resolution_percentage'):
- bpy.context.scene.render.resolution_percentage = self._original_resolution_percentage
- bpy.context.scene.render.resolution_x = self._original_resolution_x
- bpy.context.scene.render.resolution_y = self._original_resolution_y
- # Restore original visibility for non-selected objects
- if context.scene.texture_objects == 'selected':
- 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]
- if self._error:
- if self._error == "'25'" or self._error == "'111'" or self._error == "'5'":
- # Probably canceled by user, quietly return
- context.scene.generation_status = 'idle'
- context.scene.sg_last_gen_error = True
- self.report({'WARNING'}, "Generation cancelled.")
- _sg_restore_square_display(context.scene)
- _sg_ensure_crop_overlay()
- _sg_restore_label_overlay()
- remove_empty_dirs(context)
- return {'CANCELLED'}
- self.report({'ERROR'}, self._error)
- _sg_restore_square_display(context.scene)
- _sg_ensure_crop_overlay()
- _sg_restore_label_overlay()
- remove_empty_dirs(context)
- context.scene.generation_status = 'idle'
- context.scene.sg_last_gen_error = True
- return {'CANCELLED'}
- 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"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("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"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
- for node in nodes:
- # OSL script nodes: update 'Power' input
- if node.type == 'SCRIPT':
- if 'Power' in node.inputs:
- node.inputs['Power'].default_value = new_exponent
- # Native MATH POWER nodes (Blender 5.1+ native path)
- elif node.type == 'MATH' and node.operation == 'POWER' and node.label == 'power_weight':
- node.inputs[1].default_value = new_exponent
-
- print("Weight exponent reset complete.")
-
- # 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'
- context.area.spaces.active.shading.type = 'RENDERED'
- context.scene.display_settings.display_device = 'sRGB'
- context.scene.view_settings.view_transform = 'Standard'
- _sg_restore_square_display(context.scene)
- _sg_ensure_crop_overlay()
- _sg_restore_label_overlay()
- context.scene.generation_status = 'idle'
- context.scene.sg_last_gen_error = False
- # Clear output directories which are not needed anymore
- addon_prefs = context.preferences.addons[__package__].preferences
- # Save blend file in the output directory if enabled
- if addon_prefs.save_blend_file:
- blend_dir = get_dir_path(context, "revision")
- # Save the current blend file in the output directory
- scene_name = os.path.splitext(os.path.basename(bpy.data.filepath))[0]
- if not scene_name:
- scene_name = context.scene.name
- blend_file_path = os.path.join(blend_dir, f"{scene_name}_{context.scene.output_timestamp}.blend")
- # Clean-up unused data blocks
- bpy.ops.outliner.orphans_purge(do_recursive=True)
- # Pack resources and save the blend file
- bpy.ops.file.pack_all()
- bpy.ops.wm.save_as_mainfile(filepath=blend_file_path, copy=True)
- remove_empty_dirs(context)
- return {'FINISHED'}
-
- # Handle prompt collection for UV inpainting
- if context.scene.generation_method == 'uv_inpaint' and self.show_prompt_dialog:
- current_index = next((i for i, obj in enumerate(self._to_texture)
- if obj.name == self.current_object_name), -1)
-
- # Store the current prompt
- self._object_prompts[self.current_object_name] = self.current_object_prompt
-
- # Move to next object or finish
- if current_index < len(self._to_texture) - 1:
- self.current_object_name = self._to_texture[current_index + 1].name
- self.current_object_prompt = ""
- return context.window_manager.invoke_props_dialog(self, width=400)
- else:
- self.show_prompt_dialog = False
- return self.execute(context)
-
- return {'PASS_THROUGH'}
-
- def cancel_generate(self, context):
- """
- Cancels the generation process using api.interupt().
- :param context: Blender context.
- :return: None
- """
- server_address = context.preferences.addons[__package__].preferences.server_address
- client_id = str(uuid.uuid4())
- data = json.dumps({"client_id": client_id}).encode('utf-8')
- req = urllib.request.Request("http://{}/interrupt".format(server_address), data=data)
- context.scene.generation_status = 'waiting'
- ComfyUIGenerate._is_running = False
- urllib.request.urlopen(req)
- # Close active WebSocket to unblock recv() immediately
- ws = getattr(ComfyUIGenerate, '_active_ws', None)
- if ws:
- try:
- ws.close()
- except Exception:
- pass
- ComfyUIGenerate._active_ws = None
- remove_empty_dirs(context)
-
- # ------------------------------------------------------------------
- # Map preparation — runs from the async thread via timer callbacks
- # so that _stage updates are visible in real time through the modal.
- # ------------------------------------------------------------------
- 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
- ``_run_on_main_thread`` so the progress bar can update between calls."""
- controlnet_units = self._controlnet_units
- cameras = self._cameras
-
- if context.scene.generation_mode in ('standard', 'regenerate_selected'):
- need_depth = (
- any(u["unit_type"] == "depth" for u in controlnet_units)
- or (context.scene.use_flux_lora and context.scene.model_architecture == 'flux1')
- or (context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein')
- and context.scene.qwen_guidance_map_type == 'depth')
- or (context.scene.model_architecture.startswith('qwen')
- and context.scene.qwen_generation_method in ('refine', 'local_edit')
- and context.scene.qwen_refine_use_depth)
- )
- 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
- with _SGCameraResolution(context, _cam):
- self.export_depthmap(context, camera_id=_i)
- self._run_on_main_thread(_render_depth)
- if self._error:
- return
- if context.scene.generation_method == 'grid':
- self._run_on_main_thread(
- lambda: self.combine_maps(context, cameras, type="depth"))
- if self._error:
- return
-
- need_canny = any(u["unit_type"] == "canny" for u in controlnet_units)
- if need_canny and context.scene.generation_method != 'uv_inpaint':
- for i, camera in enumerate(cameras):
- 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
- with _SGCameraResolution(context, _cam):
- export_canny(context, camera_id=_i,
- low_threshold=context.scene.canny_threshold_low,
- high_threshold=context.scene.canny_threshold_high)
- self._run_on_main_thread(_render_canny)
- if self._error:
- return
- if context.scene.generation_method == 'grid':
- self._run_on_main_thread(
- lambda: self.combine_maps(context, cameras, type="canny"))
- if self._error:
- return
-
- need_normal = (
- any(u["unit_type"] == "normal" for u in controlnet_units)
- or (context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein')
- and context.scene.qwen_guidance_map_type == 'normal')
- )
- if need_normal and context.scene.generation_method != 'uv_inpaint':
- for i, camera in enumerate(cameras):
- 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
- with _SGCameraResolution(context, _cam):
- self.export_normal(context, camera_id=_i)
- self._run_on_main_thread(_render_normal)
- if self._error:
- return
- if context.scene.generation_method == 'grid':
- self._run_on_main_thread(
- lambda: self.combine_maps(context, cameras, type="normal"))
- if self._error:
- return
-
- # Qwen guidance using Workbench
- if (context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein')
- and context.scene.qwen_guidance_map_type == 'workbench'
- and context.scene.generation_method != 'uv_inpaint'):
- workbench_dir = get_dir_path(context, "controlnet")["workbench"]
- for i, camera in enumerate(cameras):
- 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
- with _SGCameraResolution(context, _cam):
- export_render(context, camera_id=_i,
- output_dir=workbench_dir, filename=f"render{_i}")
- self._run_on_main_thread(_render_wb)
- if self._error:
- return
- if context.scene.generation_method == 'grid':
- self._run_on_main_thread(
- lambda: self.combine_maps(context, cameras, type="workbench"))
- if self._error:
- return
-
- # Qwen guidance using Viewport
- elif (context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein')
- and context.scene.qwen_guidance_map_type == 'viewport'
- and context.scene.generation_method != 'uv_inpaint'):
- viewport_dir = get_dir_path(context, "controlnet")["viewport"]
- for i, camera in enumerate(cameras):
- 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
- with _SGCameraResolution(context, _cam):
- export_viewport(context, camera_id=_i,
- output_dir=viewport_dir, filename=f"viewport{_i}")
- self._run_on_main_thread(_render_vp)
- if self._error:
- return
- if context.scene.generation_method == 'grid':
- self._run_on_main_thread(
- lambda: self.combine_maps(context, cameras, type="viewport"))
- if self._error:
- return
-
- # Refine / Local Edit mode — emit images + edge feather masks
- is_refine = (
- context.scene.generation_method in ('refine', 'local_edit')
- or (context.scene.model_architecture.startswith('qwen')
- and context.scene.qwen_generation_method in ('refine', 'local_edit'))
- )
- if is_refine:
- need_feather = (
- context.scene.refine_edge_feather_projection
- and (context.scene.generation_method == 'local_edit'
- or (context.scene.model_architecture.startswith('qwen')
- and context.scene.qwen_generation_method == 'local_edit'))
- )
- for i, camera in enumerate(cameras):
- 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
- with _SGCameraResolution(context, _cam):
- export_emit_image(context, self._to_texture, camera_id=_i)
- if need_feather:
- render_edge_feather_mask(
- context, self._to_texture, _cam, _i,
- feather_width=context.scene.refine_edge_feather_width,
- softness=context.scene.refine_edge_feather_softness)
- self._run_on_main_thread(_render_refine)
- if self._error:
- return
-
- def async_generate(self, context, camera_id = None):
- """
- Asynchronously generates the image using ComfyUI.
- :param context: Blender context.
- :return: None
- """
- self._error = None
- self._pbr_maps = {} # camera_key → {map_name: file_path}
- try:
- # --- Render ControlNet / refine maps with live progress ---
- self._prepare_maps(context)
- if self._error:
- return
-
- while self._threads_left > 0 and ComfyUIGenerate._is_running and not context.scene.generation_mode == 'project_only':
- # Swap scene resolution to per-camera values if stored.
- # Must use a timer callback so the write happens on the
- # main thread; writing RNA from a background thread would
- # trigger DEG_id_tag_update on a NULL depsgraph and crash.
- if camera_id is not None and camera_id < len(self._cameras):
- _cam = self._cameras[camera_id]
- _rx, _ry = _get_camera_resolution(_cam, context.scene)
- def _swap_resolution():
- try:
- context.scene.render.resolution_x = _rx
- context.scene.render.resolution_y = _ry
- except Exception as e:
- self._error = str(e)
- traceback.print_exc()
- self._wait_event.set()
- return None
- bpy.app.timers.register(_swap_resolution)
- self._wait_event.wait()
- self._wait_event.clear()
- if self._error:
- return
-
- # Sequential regeneration: non-selected cameras skip AI
- # generation but still reproject their existing image so
- # subsequent cameras see the correct incremental texture state.
- _is_seq_reproject = (
- context.scene.generation_mode == 'regenerate_selected'
- and camera_id not in self._selected_camera_ids
- and context.scene.generation_method == 'sequential'
- )
-
- if _is_seq_reproject:
- self._stage = "Reprojecting Image"
- self._progress = 0
- # project_image patches the material tree for this camera,
- # loading the existing generated image from disk.
- def image_reproject_callback():
- try:
- redraw_ui(context)
- project_image(context, self._to_texture, self._material_id, stop_index=self._current_image)
- except Exception as e:
- self._error = str(e)
- traceback.print_exc()
- self._wait_event.set()
- return None
- bpy.app.timers.register(image_reproject_callback)
- self._wait_event.wait()
- self._wait_event.clear()
- if self._error:
- return
-
- elif context.scene.steps != 0 and not (context.scene.generation_mode == 'regenerate_selected' and camera_id not in self._selected_camera_ids):
- # Prepare Image Info for Upload
- controlnet_info = {}
- mask_info = None
- render_info = None
- ipadapter_ref_info = None
-
- # Get info for controlnet images for the current camera or grid
- if context.scene.generation_method != 'uv_inpaint':
- controlnet_info["depth"] = self._get_uploaded_image_info(context, "controlnet", subtype="depth", camera_id=camera_id)
- controlnet_info["canny"] = self._get_uploaded_image_info(context, "controlnet", subtype="canny", camera_id=camera_id)
- controlnet_info["normal"] = self._get_uploaded_image_info(context, "controlnet", subtype="normal", camera_id=camera_id)
- else: # UV Inpainting
- current_obj_name = self._to_texture[self._current_image].name
- mask_info = self._get_uploaded_image_info(context, "uv_inpaint", subtype="visibility", object_name=current_obj_name)
- render_info = self._get_uploaded_image_info(context, "baked", object_name=current_obj_name)
-
- # Get info for refine/sequential render/mask inputs
- if context.scene.generation_method in ('refine', 'local_edit'):
- render_info = self._get_uploaded_image_info(context, "inpaint", subtype="render", camera_id=camera_id)
- elif context.scene.generation_method == 'sequential' and self._current_image > 0:
- render_info = self._get_uploaded_image_info(context, "inpaint", subtype="render", camera_id=self._current_image)
- mask_info = self._get_uploaded_image_info(context, "inpaint", subtype="visibility", camera_id=self._current_image)
-
- # Get info for IPAdapter reference image
- if context.scene.use_ipadapter:
- ipadapter_ref_info = self._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(context.scene.ipadapter_image))
- elif context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_mode == 'trellis2_input':
- # Use the TRELLIS.2 input image as IPAdapter reference
- t2_path = getattr(context.scene, 'trellis2_last_input_image', '')
- if t2_path and os.path.exists(bpy.path.abspath(t2_path)):
- ipadapter_ref_info = self._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(t2_path))
- elif context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_mode == 'original_render' and context.scene.generation_method == 'local_edit':
- # Use the existing texture render from this camera's viewpoint as IPAdapter reference
- ipadapter_ref_info = self._get_uploaded_image_info(context, "inpaint", subtype="render", camera_id=camera_id)
- elif context.scene.sequential_ipadapter and self._current_image > 0:
- cam_id = 0 if context.scene.sequential_ipadapter_mode == 'first' else self._current_image - 1
- ipadapter_ref_info = self._get_uploaded_image_info(context, "generated", camera_id=cam_id, material_id=self._material_id)
-
- # Filter out None values from controlnet_info
- controlnet_info = {k: v for k, v in controlnet_info.items() if v is not None}
- # End Prepare Image Info
-
- # Generate image without ControlNet if needed
- if context.scene.generation_mode == 'standard' and camera_id == 0 and (context.scene.generation_method == 'sequential' or context.scene.generation_method in ('refine', 'local_edit'))\
- and context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_regenerate and not context.scene.use_ipadapter and context.scene.sequential_ipadapter_mode == 'first'\
- and context.scene.sequential_ipadapter_mode != 'trellis2_input':
- self._stage = "Generating Reference Image"
- # Don't use ControlNet for the first image if sequential_ipadapter_regenerate_wo_controlnet is enabled
- if context.scene.sequential_ipadapter_regenerate_wo_controlnet:
- original_strengths = [unit.strength for unit in context.scene.controlnet_units]
- for unit in context.scene.controlnet_units:
- unit.strength = 0.0
- else:
- self._stage = "Uploading to Server"
- self._progress = 0
-
- # Generate the image
- if context.scene.generation_method in ('refine', 'local_edit'):
- if context.scene.model_architecture == 'flux1':
- image = self.workflow_manager.refine_flux(context, controlnet_info=controlnet_info, render_info=render_info, ipadapter_ref_info=ipadapter_ref_info)
- else:
- image = self.workflow_manager.refine(context, controlnet_info=controlnet_info, render_info=render_info, ipadapter_ref_info=ipadapter_ref_info)
- elif context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method in ('refine', 'local_edit'):
- image = self.workflow_manager.generate_qwen_refine(context, camera_id=camera_id)
- elif context.scene.generation_method == 'uv_inpaint':
- if context.scene.model_architecture == 'flux1':
- image = self.workflow_manager.refine_flux(context, mask_info=mask_info, render_info=render_info)
- else:
- image = self.workflow_manager.refine(context, mask_info=mask_info, render_info=render_info)
- elif context.scene.generation_method == 'sequential':
- if self._current_image == 0:
- if context.scene.model_architecture == 'flux1':
- image = self.workflow_manager.generate_flux(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)
- elif context.scene.model_architecture == 'qwen_image_edit':
- image = self.workflow_manager.generate_qwen_edit(context, camera_id=camera_id)
- elif context.scene.model_architecture == 'flux2_klein':
- image = self.workflow_manager.generate_flux2_klein(context, camera_id=camera_id)
- else:
- image = self.workflow_manager.generate(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)
- else:
- self._stage = "Preparing Next Camera"
- self._progress = 0
- def context_callback():
- try:
- # Export visibility mask and render for the current camera, we need to use a callback to be in the main thread
- # Visibility is rendered from the *current* camera's viewpoint
- # (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
- with _SGCameraResolution(context, _cur_cam):
- export_visibility(context, self._to_texture, camera_visibility=_vis_cam) # Export mask for current view
- _emit_cam = _cur_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)
- 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:
- # Use a gray (neutral) background and fallback for other architectures
- export_emit_image(context, self._to_texture, camera_id=self._current_image, bg_color=(0.5, 0.5, 0.5), fallback_color=(0.5, 0.5, 0.5))
- except Exception as e:
- self._error = str(e)
- traceback.print_exc()
- self._wait_event.set()
- return None
- bpy.app.timers.register(context_callback)
- self._wait_event.wait()
- self._wait_event.clear()
- if self._error:
- return
- self._stage = "Uploading to Server"
- # Get info for the previous render and mask
- render_info = self._get_uploaded_image_info(context, "inpaint", subtype="render", camera_id=self._current_image)
- mask_info = self._get_uploaded_image_info(context, "inpaint", subtype="visibility", camera_id=self._current_image)
-
- if context.scene.model_architecture == 'flux1':
- image = self.workflow_manager.refine_flux(context, controlnet_info=controlnet_info, render_info=render_info, mask_info=mask_info, ipadapter_ref_info=ipadapter_ref_info)
- elif context.scene.model_architecture == 'qwen_image_edit':
- image = self.workflow_manager.generate_qwen_edit(context, camera_id=camera_id)
- elif context.scene.model_architecture == 'flux2_klein':
- image = self.workflow_manager.generate_flux2_klein(context, camera_id=camera_id)
- else:
- image = self.workflow_manager.refine(context, controlnet_info=controlnet_info, render_info=render_info, mask_info=mask_info, ipadapter_ref_info=ipadapter_ref_info)
- else: # Grid or Separate
- if context.scene.model_architecture == 'flux1':
- image = self.workflow_manager.generate_flux(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)
- elif context.scene.model_architecture == 'qwen_image_edit':
- image = self.workflow_manager.generate_qwen_edit(context, camera_id=camera_id)
- elif context.scene.model_architecture == 'flux2_klein':
- image = self.workflow_manager.generate_flux2_klein(context, camera_id=camera_id)
- else:
- image = self.workflow_manager.generate(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)
-
- if image == {"error": "conn_failed"}:
- if not self._error:
- 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)
- elif camera_id is not None:
- image_path = get_file_path(context, "generated", camera_id=camera_id, material_id=self._material_id)
- else: # Grid mode initial generation
- image_path = get_file_path(context, "generated", filename="generated_image_grid") # Save grid to a specific name
-
- with open(image_path, 'wb') as f:
- f.write(image)
-
-
- # Use hack to re-generate the image using IPAdapter to match IPAdapter style
- if camera_id == 0 and (context.scene.generation_method == 'sequential' or context.scene.generation_method == 'separate' or context.scene.generation_method in ('refine', 'local_edit'))\
- and context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_regenerate and not context.scene.use_ipadapter and context.scene.sequential_ipadapter_mode == 'first':
-
- # Restore original strengths
- if context.scene.sequential_ipadapter_regenerate_wo_controlnet:
- for i, unit in enumerate(context.scene.controlnet_units):
- unit.strength = original_strengths[i]
- self._stage = "Generating Image"
- context.scene.use_ipadapter = True
- context.scene.ipadapter_image = image_path
- ipadapter_ref_info = self._get_uploaded_image_info(context, "custom", filename=image_path)
- if context.scene.model_architecture == "sdxl":
- if context.scene.generation_method in ("refine", "local_edit"):
- image = self.workflow_manager.refine(context, controlnet_info=controlnet_info, render_info=render_info, mask_info=mask_info, ipadapter_ref_info=ipadapter_ref_info)
- else:
- image = self.workflow_manager.generate(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)
- elif context.scene.model_architecture == "flux1":
- if context.scene.generation_method in ("refine", "local_edit"):
- image = self.workflow_manager.refine_flux(context, controlnet_info=controlnet_info, render_info=render_info, mask_info=mask_info, ipadapter_ref_info=ipadapter_ref_info)
- else:
- image = self.workflow_manager.generate_flux(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)
- context.scene.use_ipadapter = False
- image_path = image_path.replace(".png", "_ipadapter.png")
- with open(image_path, 'wb') as f:
- f.write(image)
-
- # ── PBR decomposition is deferred until after ALL cameras ──
-
- # Sequential mode callback
- if context.scene.generation_method == 'sequential':
- self._stage = "Projecting Image"
- def image_project_callback():
- try:
- redraw_ui(context)
- project_image(context, self._to_texture, self._material_id, stop_index=self._current_image)
- except Exception as e:
- self._error = str(e)
- traceback.print_exc()
- # Set the event to signal the end of the process
- self._wait_event.set()
- return None
- bpy.app.timers.register(image_project_callback)
- # Wait for the event to be set
- self._wait_event.wait()
- self._wait_event.clear()
- if self._error:
- return
- # Update info for the next iteration (if any)
- if self._current_image < len(self._cameras) - 1:
- next_camera_id = self._current_image + 1
- # ControlNet info will be re-fetched at the start of the next loop iteration
- else: # steps == 0, skip generation
- pass # No image generation needed
-
- if context.scene.generation_method in ('separate', 'refine', 'local_edit', 'sequential') or (context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method in ('refine', 'local_edit')):
- self._current_image += 1
- self._threads_left -= 1
- if self._threads_left > 0:
- self._progress = 0
- if camera_id is not None: # Increment camera_id only if it was initially provided
- camera_id += 1
-
- elif context.scene.generation_method == 'uv_inpaint':
- self._current_image += 1
- self._threads_left -= 1
- if self._threads_left > 0:
- self._progress = 0
-
- elif context.scene.generation_method == 'grid':
- # Split the generated grid image back into multiple images
- self.split_generated_grid(context, self._cameras)
- if context.scene.refine_images:
- for i, _ in enumerate(self._cameras):
- self._stage = f"Refining Image {i+1}/{len(self._cameras)}"
- self._current_image = i + 1
- # Refine the split images
- refine_cn_info = {
- "depth": self._get_uploaded_image_info(context, "controlnet", subtype="depth", camera_id=i),
- "canny": self._get_uploaded_image_info(context, "controlnet", subtype="canny", camera_id=i),
- "normal": self._get_uploaded_image_info(context, "controlnet", subtype="normal", camera_id=i)
- }
- refine_cn_info = {k: v for k, v in refine_cn_info.items() if v is not None}
- refine_render_info = self._get_uploaded_image_info(context, "generated", camera_id=i, material_id=self._material_id)
-
- if context.scene.model_architecture == 'flux1':
- image = self.workflow_manager.refine_flux(context, controlnet_info=refine_cn_info, render_info=refine_render_info)
- else:
- image = self.workflow_manager.refine(context, controlnet_info=refine_cn_info, render_info=refine_render_info)
-
- if image == {"error": "conn_failed"}:
- self._error = "Failed to connect to ComfyUI server."
- return
- # Overwrite the split image with the refined one
- image_path = get_file_path(context, "generated", camera_id=i, material_id=self._material_id)
- with open(image_path, 'wb') as f:
- f.write(image)
- self._threads_left = 0
-
- except Exception as e:
- self._error = str(e)
- traceback.print_exc()
- return
-
- # ── PBR Decomposition (runs after ALL cameras are generated) ──
- if getattr(context.scene, 'pbr_decomposition', False):
- self._pbr_maps = {}
- # Collect camera images that need PBR decomposition
- camera_images = {} # cam_idx → image_path
- per_camera_missing = {} # cam_idx → set of missing map names (reproject only)
- num_cameras = len(self._cameras)
- for cam_idx in range(num_cameras):
- cam_image_path = get_file_path(
- context, "generated", camera_id=cam_idx,
- material_id=self._material_id,
- )
- if os.path.exists(cam_image_path):
- # In project_only (reproject) mode, reuse existing PBR
- # maps when all enabled maps are already on disk. This
- # avoids re-running the slow ComfyUI decomposition.
- if context.scene.generation_mode == 'project_only':
- existing, missing = self._find_existing_pbr_maps(
- context, cam_idx)
- if not missing:
- # All maps present — fully reuse
- self._ensure_raw_copies(
- context, cam_idx, existing)
- self._pbr_maps[cam_idx] = existing
- print(f"[StableGen] Reusing existing PBR maps "
- f"for camera {cam_idx}")
- continue
- # Some maps exist, some are missing — seed existing
- # maps so the batched function only generates what's
- # missing.
- if existing:
- self._ensure_raw_copies(
- context, cam_idx, existing)
- self._pbr_maps[cam_idx] = existing
- per_camera_missing[cam_idx] = missing
- print(f"[StableGen] PBR maps missing for camera "
- f"{cam_idx}: {missing}, running selective decomposition…")
- camera_images[cam_idx] = cam_image_path
- if camera_images:
- self._run_pbr_decomposition_batched(
- context, camera_images,
- per_camera_missing=per_camera_missing if per_camera_missing else None)
- if self._error:
- return
-
- # ── Batch post-processing (albedo sat/contrast, roughness scale) ──
- # Runs after ALL cameras have raw maps saved, so auto-saturation
- # can average across all cameras uniformly.
- if self._pbr_maps:
- self._apply_albedo_postprocessing_batch(context)
-
- # ── Persist per-map settings hashes so future reprojects can
- # detect stale maps and only regenerate what changed. ──
- if camera_images:
- for cam_idx in camera_images:
- self._save_pbr_settings(context, cam_idx)
-
- def image_project_callback():
- if context.scene.generation_method == 'sequential':
- # In sequential mode, projection happened per-camera inside the loop.
- # PBR is projected onto the existing material after all cameras.
- if getattr(context.scene, 'pbr_decomposition', False) and hasattr(self, '_pbr_maps') and self._pbr_maps:
- from .project import project_pbr_to_bsdf
- project_pbr_to_bsdf(
- context, self._to_texture, self._pbr_maps,
- material_id=self._material_id
- )
- if getattr(context.scene, 'pbr_auto_lighting', False):
- _pbr_setup_studio_lights(context, self._to_texture)
- 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)
- else:
- # Apply the UV inpainted textures to each mesh
- from .render_tools import apply_uv_inpaint_texture
- for obj in self._to_texture:
- texture_path = get_file_path(
- context, "generated_baked", object_name=obj.name, material_id=self._material_id
- )
- apply_uv_inpaint_texture(context, obj, texture_path)
-
- # Project PBR maps onto material after color projection
- if getattr(context.scene, 'pbr_decomposition', False) and hasattr(self, '_pbr_maps') and self._pbr_maps:
- from .project import project_pbr_to_bsdf
- project_pbr_to_bsdf(
- context, self._to_texture, self._pbr_maps,
- material_id=self._material_id
- )
- if getattr(context.scene, 'pbr_auto_lighting', False):
- _pbr_setup_studio_lights(context, self._to_texture)
-
- return None
-
- if context.scene.view_blend_use_color_match and self._to_texture:
- self._stage = "Matching Colors"
- # Use the first object in the target list as the color reference
- ref_np = _get_viewport_ref_np(self._to_texture[0])
- if ref_np is not None:
- # Loop all cameras we generated for
- for cam_idx, cam in enumerate(self._cameras):
- image_path = get_file_path(
- context,
- "generated",
- camera_id=cam_idx,
- material_id=self._material_id,
- )
- _apply_color_match_to_file(
- image_path=image_path,
- ref_rgb=ref_np,
- scene=context.scene,
- )
-
- bpy.app.timers.register(image_project_callback)
-
- # Update seed based on control parameter
- if context.scene.control_after_generate == 'increment':
- context.scene.seed += 1
- elif context.scene.control_after_generate == 'decrement':
- context.scene.seed -= 1
- elif context.scene.control_after_generate == 'randomize':
- context.scene.seed = np.random.randint(0, 1000000)
-
- def draw(self, context):
- layout = self.layout
- if context.scene.generation_method == 'uv_inpaint' and self.show_prompt_dialog:
- layout.label(text=f"Enter prompt for object: {self.current_object_name}")
- layout.prop(self, "current_object_prompt", text="")
-
- def invoke(self, context, event):
- if context.scene.generation_method == 'uv_inpaint':
- # Reset object prompts on every run
- self.show_prompt_dialog = True
- self._object_prompts = {}
- self._to_texture = [obj.name for obj in bpy.context.view_layer.objects if obj.type == 'MESH']
- if context.scene.texture_objects == 'selected':
- self._to_texture = [obj.name for obj in bpy.context.selected_objects if obj.type == 'MESH']
- self.mesh_index = 0
- self.current_object_name = self._to_texture[0] if self._to_texture else ""
- # If "Ask for object prompts" is disabled, don’t prompt per object
- if not context.scene.ask_object_prompts or self._is_running:
- self.show_prompt_dialog = False
- return self.execute(context)
- 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)
-
- # ── PBR Decomposition ──────────────────────────────────────────────
-
- @staticmethod
- def _pbr_settings_hash(settings_dict):
- """Return a short hex hash for a dict of PBR settings."""
- import hashlib
- raw = json.dumps(settings_dict, sort_keys=True, default=str)
- return hashlib.md5(raw.encode()).hexdigest()[:12]
-
- def _pbr_per_map_settings(self, context):
- """Compute per-map settings dicts that affect each PBR output.
-
- Returns ``{map_name: {setting_key: value, ...}}`` for every
- map type. Two dicts compare equal iff the same generation would
- produce identical output.
-
- Tiling modes are resolved to effective booleans so that e.g.
- ``selective`` (albedo-only) and ``custom`` with only albedo
- enabled produce identical hashes for every map.
- """
- scene = context.scene
- # ── Common model settings (affect all server-side outputs) ────
- common = {
- 'resolution': getattr(scene, 'pbr_processing_resolution', 768),
- 'native_res': getattr(scene, 'pbr_use_native_resolution', True),
- 'denoise_steps': getattr(scene, 'pbr_denoise_steps', 4),
- 'ensemble_size': getattr(scene, 'pbr_ensemble_size', 1),
- }
-
- albedo_source = getattr(scene, 'pbr_albedo_source', 'delight')
-
- # ── Resolve effective per-map tiling booleans ─────────────────
- tile_mode = getattr(scene, 'pbr_tiling', 'off')
- tile_grid = getattr(scene, 'pbr_tile_grid', 2)
- tile_superres = getattr(scene, 'pbr_tile_superres', False)
-
- tile_albedo = getattr(scene, 'pbr_tile_albedo', True) if tile_mode == 'custom' else (tile_mode in ('selective', 'all'))
- tile_material = getattr(scene, 'pbr_tile_material', False) if tile_mode == 'custom' else (tile_mode == 'all')
- tile_normal = getattr(scene, 'pbr_tile_normal', False) if tile_mode == 'custom' else (tile_mode == 'all')
- tile_height = getattr(scene, 'pbr_tile_height', False) if tile_mode == 'custom' else (tile_mode == 'all')
- tile_emission = getattr(scene, 'pbr_tile_emission', False) if tile_mode == 'custom' else False
-
- def tiling_dict(is_tiled):
- """Return tiling sub-dict; grid/superres only matter when tiled."""
- if is_tiled:
- return {'tiled': True, 'tile_grid': tile_grid,
- 'tile_superres': tile_superres}
- return {'tiled': False}
-
- settings = {}
-
- settings['albedo'] = {
- **common,
- **tiling_dict(tile_albedo),
- 'source': albedo_source,
- 'delight_strength': getattr(scene, 'pbr_delight_strength', 1.0) if albedo_source == 'delight' else None,
- 'auto_saturation': getattr(scene, 'pbr_albedo_auto_saturation', False),
- 'saturation_mode': getattr(scene, 'pbr_albedo_saturation_mode', 'MEDIAN') if getattr(scene, 'pbr_albedo_auto_saturation', False) else None,
- }
- settings['roughness'] = {
- **common,
- **tiling_dict(tile_material),
- }
- settings['metallic'] = {
- **common,
- **tiling_dict(tile_material),
- }
- settings['normal'] = {
- **common,
- **tiling_dict(tile_normal),
- }
- settings['height'] = {
- **common,
- **tiling_dict(tile_height),
- }
-
- emission_method = getattr(scene, 'pbr_emission_method', 'residual')
- emission_base = {
- 'method': emission_method,
- 'threshold': getattr(scene, 'pbr_emission_threshold', 0.2),
- }
- if emission_method == 'hsv':
- emission_base.update({
- 'sat_min': getattr(scene, 'pbr_emission_saturation_min', 0.5),
- 'val_min': getattr(scene, 'pbr_emission_value_min', 0.85),
- 'bloom': getattr(scene, 'pbr_emission_bloom', 5.0),
- })
- else:
- # Residual method uses a model pass
- emission_base.update({
- **common,
- **tiling_dict(tile_emission),
- })
- settings['emission'] = emission_base
-
- return settings
-
- def _pbr_sidecar_path(self, context):
- """Return the path for the single PBR settings sidecar JSON file."""
- dirs = get_generation_dirs(context)
- base_dir = dirs["pbr"]
- material_suffix = f"-{self._material_id}" if self._material_id is not None else ""
- return os.path.join(base_dir, f"pbr_settings{material_suffix}.json")
-
- def _save_pbr_settings(self, context, camera_id):
- """Write current per-map settings hashes for *camera_id* into
- the shared sidecar JSON file (merges with existing data)."""
- settings = self._pbr_per_map_settings(context)
- hashes = {k: self._pbr_settings_hash(v) for k, v in settings.items()}
- path = self._pbr_sidecar_path(context)
- # Load existing data to merge
- data = {}
- if os.path.exists(path):
- try:
- with open(path, 'r') as f:
- data = json.load(f)
- except Exception:
- pass
- data[str(camera_id)] = hashes
- try:
- os.makedirs(os.path.dirname(path), exist_ok=True)
- with open(path, 'w') as f:
- json.dump(data, f, indent=1)
- except Exception as e:
- print(f"[StableGen] Warning: could not write PBR settings "
- f"sidecar {path}: {e}")
-
- def _load_pbr_settings(self, context, camera_id):
- """Load stored per-map settings hashes for *camera_id* from the
- shared sidecar JSON file.
-
- Returns a dict ``{map_name: hash_string}`` or ``None`` if no
- sidecar or camera entry exists.
- """
- path = self._pbr_sidecar_path(context)
- if not os.path.exists(path):
- return None
- try:
- with open(path, 'r') as f:
- data = json.load(f)
- return data.get(str(camera_id))
- except Exception:
- return None
-
- def _find_existing_pbr_maps(self, context, camera_id):
- """Check which enabled PBR maps already exist on disk for a camera.
-
- Returns ``(existing, missing)`` where *existing* is a dict
- ``{map_name: file_path}`` of maps already on disk **whose settings
- hash matches the current configuration**, and *missing* is a set of
- map names that are enabled but either not found or stale.
- """
- scene = context.scene
- existing = {}
- missing = set()
-
- map_checks = []
- if getattr(scene, 'pbr_map_albedo', True):
- map_checks.append('albedo')
- if getattr(scene, 'pbr_map_roughness', True):
- map_checks.append('roughness')
- if getattr(scene, 'pbr_map_metallic', True):
- map_checks.append('metallic')
- if getattr(scene, 'pbr_map_normal', True):
- map_checks.append('normal')
- if getattr(scene, 'pbr_map_height', False):
- map_checks.append('height')
- if getattr(scene, 'pbr_map_emission', False):
- map_checks.append('emission')
-
- if not map_checks:
- return existing, missing
-
- # Load stored hashes and compute current ones for comparison
- stored_hashes = self._load_pbr_settings(context, camera_id)
- current_settings = self._pbr_per_map_settings(context)
- current_hashes = {k: self._pbr_settings_hash(v)
- for k, v in current_settings.items()}
-
- for map_name in map_checks:
- path = get_file_path(
- context, "pbr", subtype=map_name,
- camera_id=camera_id, material_id=self._material_id
- )
- if os.path.exists(path):
- # Check if settings have changed since last generation
- if (stored_hashes is not None
- and stored_hashes.get(map_name) == current_hashes.get(map_name)):
- existing[map_name] = path
- else:
- # File exists but settings changed → stale, regenerate
- missing.add(map_name)
- else:
- missing.add(map_name)
-
- return existing, missing
-
- # ── Tiled PBR processing ──────────────────────────────────────────
-
- @staticmethod
- def _create_tile_blend_mask(h, w, overlap_x, overlap_y,
- fade_left, fade_right,
- fade_top, fade_bottom):
- """Return a float32 [H, W, 1] weight mask with cosine fade on inner edges.
-
- Only the edges that border a neighbouring tile (indicated by the
- ``fade_*`` booleans) get a cosine-weighted ramp.
- ``overlap_x`` controls left/right ramp length,
- ``overlap_y`` controls top/bottom ramp length.
- Image-boundary edges stay at full weight (1.0).
- """
- mask = np.ones((h, w, 1), dtype=np.float32)
-
- if overlap_x > 0:
- ramp_x = np.linspace(0.0, np.pi, overlap_x, dtype=np.float32)
- ramp_x = (1.0 - np.cos(ramp_x)) * 0.5 # 0 → 1 cosine ease
- if fade_left and overlap_x <= w:
- mask[:, :overlap_x, 0] *= ramp_x[np.newaxis, :]
- if fade_right and overlap_x <= w:
- mask[:, -overlap_x:, 0] *= ramp_x[np.newaxis, ::-1]
-
- if overlap_y > 0:
- ramp_y = np.linspace(0.0, np.pi, overlap_y, dtype=np.float32)
- ramp_y = (1.0 - np.cos(ramp_y)) * 0.5
- if fade_top and overlap_y <= h:
- mask[:overlap_y, :, 0] *= ramp_y[:, np.newaxis]
- if fade_bottom and overlap_y <= h:
- mask[-overlap_y:, :, 0] *= ramp_y[::-1, np.newaxis]
-
- return mask
-
- def _process_model_tiled(self, context, image_path, model_name=None,
- process_fn=None):
- """Run a model on an N×N grid of overlapping tiles.
-
- Each tile is **upscaled to the full image's longest edge** before
- processing, so the model spends its full resolution budget on
- only 1/N² of the spatial area — effectively N²× the detail.
- The stitched output is at the **upscaled** resolution (~N× the
- original), producing super-resolution PBR maps.
-
- Args:
- model_name: Passed to ``generate_pbr_maps()``.
- process_fn: Optional callable ``(context, tile_path) → result``.
- If given, called instead of ``generate_pbr_maps``.
- May return ``bytes`` (single image) or ``list[bytes]``.
-
- Returns the same ``list[bytes]`` as ``generate_pbr_maps()``.
- """
- import tempfile
-
- OVERLAP = 64 # pixels each tile extends beyond its boundary
- MIN_DIM = 256 # skip tiling if either dimension is too small
-
- scene = context.scene
- N = getattr(scene, 'pbr_tile_grid', 2)
-
- src = Image.open(image_path)
- W, H = src.size
- longest = max(W, H)
-
- if W < MIN_DIM or H < MIN_DIM:
- src.close()
- print(f"[StableGen] Image {W}×{H} too small for tiling, "
- f"processing normally")
- if process_fn is not None:
- fb = process_fn(context, image_path)
- return [fb] if isinstance(fb, bytes) else fb
- return self.workflow_manager.generate_pbr_maps(
- context, image_path, model_name=model_name)
-
- # Compute tile boundaries in original-image coordinates
- x_bounds = [W * c // N for c in range(N + 1)] # [0, W/N, 2W/N, …, W]
- y_bounds = [H * r // N for r in range(N + 1)]
-
- tile_info = [] # list of (l, t, r, b, fade_left, fade_right, fade_top, fade_bottom)
- for row in range(N):
- for col in range(N):
- l = x_bounds[col] - (OVERLAP if col > 0 else 0)
- r = x_bounds[col+1] + (OVERLAP if col < N - 1 else 0)
- t = y_bounds[row] - (OVERLAP if row > 0 else 0)
- b = y_bounds[row+1] + (OVERLAP if row < N - 1 else 0)
- # Clamp to image bounds
- l = max(l, 0); r = min(r, W)
- t = max(t, 0); b = min(b, H)
- tile_info.append((
- l, t, r, b,
- col > 0, # fade_left
- col < N - 1, # fade_right
- row > 0, # fade_top
- row < N - 1, # fade_bottom
- ))
-
- total_tiles = len(tile_info)
-
- # Uniform scale factor — upscale tiles so longest edge ≈ full image
- sample_tw = tile_info[0][2] - tile_info[0][0]
- sample_th = tile_info[0][3] - tile_info[0][1]
- sample_longest = max(sample_tw, sample_th)
- scale = longest / sample_longest if sample_longest < longest else 1.0
-
- # ── Process each tile ─────────────────────────────────
- all_tile_results = [] # list of list[bytes]
- tile_up_sizes = [] # (up_w, up_h) per tile
- tmp_dir = tempfile.gettempdir()
-
- for i, (l, t, r, b, fl, fr, ft, fb) in enumerate(tile_info):
- tw, th = r - l, b - t
- tile_img = src.crop((l, t, r, b))
-
- tile_longest = max(tw, th)
- if tile_longest < longest:
- up_w = int(round(tw * scale))
- up_h = int(round(th * scale))
- tile_img = tile_img.resize(
- (up_w, up_h), Image.LANCZOS)
- print(f"[StableGen] Tile {i+1}/{total_tiles}: "
- f"{tw}×{th} → {up_w}×{up_h} "
- f"region ({l},{t})→({r},{b})")
- else:
- up_w, up_h = tw, th
- print(f"[StableGen] Tile {i+1}/{total_tiles}: "
- f"{tw}×{th} region ({l},{t})→({r},{b})")
- tile_up_sizes.append((up_w, up_h))
-
- tile_path = os.path.join(tmp_dir, f"sg_tile_{i}.png")
- tile_img.save(tile_path)
-
- if process_fn is not None:
- result = process_fn(context, tile_path)
- # Normalise to list[bytes] (StableDelight returns bytes)
- if isinstance(result, bytes):
- result = [result]
- else:
- result = self.workflow_manager.generate_pbr_maps(
- context, tile_path, model_name=model_name,
- force_native_resolution=True)
-
- try:
- os.remove(tile_path)
- except OSError:
- pass
-
- if isinstance(result, dict):
- print(f"[StableGen] Tile {i+1} failed, falling back "
- f"to full-image processing")
- src.close()
- if process_fn is not None:
- fb = process_fn(context, image_path)
- return [fb] if isinstance(fb, bytes) else fb
- return self.workflow_manager.generate_pbr_maps(
- context, image_path, model_name=model_name)
-
- # Resize model output to expected upscaled tile dims
- resized = []
- for map_bytes in result:
- map_img = Image.open(io.BytesIO(map_bytes))
- if map_img.size != (up_w, up_h):
- map_img = map_img.resize((up_w, up_h), Image.LANCZOS)
- buf = io.BytesIO()
- map_img.save(buf, format='PNG')
- resized.append(buf.getvalue())
- all_tile_results.append(resized)
-
- src.close()
-
- # ── Stitch tiles ──────────────────────────────────────
- superres = getattr(scene, 'pbr_tile_superres', False)
-
- if superres:
- # Super-resolution: stitch at the upscaled tile size (~N× original)
- out_W = int(round(W * scale))
- out_H = int(round(H * scale))
- scaled_overlap = int(round(OVERLAP * scale))
- else:
- # Original resolution: downscale tiles back before stitching
- out_W, out_H = W, H
- scaled_overlap = OVERLAP # overlap in original coords
-
- num_maps = len(all_tile_results[0])
- stitched = []
-
- for map_idx in range(num_maps):
- canvas = np.zeros((out_H, out_W, 3), dtype=np.float32)
- weight = np.zeros((out_H, out_W, 1), dtype=np.float32)
-
- for i, (l, t, r, b, fl, fr, ft, fb) in enumerate(tile_info):
- tile_bytes = all_tile_results[i][map_idx]
- tile_img = Image.open(io.BytesIO(tile_bytes)).convert('RGB')
-
- if superres:
- up_w, up_h = tile_up_sizes[i]
- sl = int(round(l * scale))
- st = int(round(t * scale))
- sr = min(sl + up_w, out_W)
- sb = min(st + up_h, out_H)
- else:
- # Downscale tile back to original crop dimensions
- orig_w, orig_h = r - l, b - t
- if tile_img.size != (orig_w, orig_h):
- tile_img = tile_img.resize(
- (orig_w, orig_h), Image.LANCZOS)
- sl, st, sr, sb = l, t, r, b
-
- tile_arr = np.asarray(tile_img, dtype=np.float32)
- tile_arr = tile_arr[:sb - st, :sr - sl]
-
- overlap_px = 2 * scaled_overlap
- mask = self._create_tile_blend_mask(
- sb - st, sr - sl,
- overlap_x=overlap_px,
- overlap_y=overlap_px,
- fade_left=fl, fade_right=fr,
- fade_top=ft, fade_bottom=fb)
-
- canvas[st:sb, sl:sr] += tile_arr * mask
- weight[st:sb, sl:sr] += mask
-
- canvas /= np.maximum(weight, 1e-6)
- canvas = np.clip(canvas, 0, 255).astype(np.uint8)
-
- out_img = Image.fromarray(canvas, mode='RGB')
- buf = io.BytesIO()
- out_img.save(buf, format='PNG')
- stitched.append(buf.getvalue())
-
- sr_label = " (super-res)" if superres else ""
- print(f"[StableGen] Stitched {N}×{N} output: {out_W}×{out_H}"
- f"{sr_label}")
-
- return stitched
-
- def _convert_normals_cam_to_world(self, map_bytes, camera_id):
- """Convert a Marigold camera-space normal map to world space.
-
- Marigold normals are in camera space (X = right, Y = up,
- Z = toward the camera). The PNG encodes them as
- ``(N + 1) / 2``, mapping [-1, 1] to [0, 1].
-
- This method:
- 1. Decodes the PNG into an RGB float array.
- 2. Converts [0, 1] → [-1, 1].
- 3. Rotates each normal by the camera's world rotation
- (Marigold OpenGL convention and Blender camera-local
- convention agree: X-right, Y-up, Z-toward-viewer).
- 4. Re-normalises and re-encodes to [0, 1] PNG bytes.
-
- Returns PNG bytes, or ``None`` on failure.
- """
- try:
- img = Image.open(io.BytesIO(map_bytes)).convert('RGB')
- arr = np.asarray(img, dtype=np.float32) / 255.0 # [H, W, 3] in [0,1]
-
- # Decode to [-1, 1]
- normals = arr * 2.0 - 1.0 # [H, W, 3] (X, Y, Z) camera-space
-
- # Marigold (OpenGL) convention:
- # X = right, Y = up, Z = toward camera (out of screen)
- # Blender camera-local convention:
- # X = right, Y = up, Z = behind camera (camera looks -Z)
- #
- # These AGREE: a surface facing the camera has Z = +1 in
- # both systems (+Z_local points toward the viewer).
- # No axis flip is needed.
-
- # Get camera rotation (camera-local → world)
- cam = self._cameras[camera_id]
- cam_rot = np.array(cam.matrix_world.to_3x3(), dtype=np.float32) # 3×3
-
- # Rotate all normals: N_world = cam_rot @ N_local
- # Reshape to [H*W, 3] for batch matrix multiply
- H, W, _ = normals.shape
- flat = normals.reshape(-1, 3) # [H*W, 3]
- flat_world = (cam_rot @ flat.T).T # [H*W, 3]
-
- # Re-normalise (avoid div-by-zero)
- norms = np.linalg.norm(flat_world, axis=1, keepdims=True)
- norms = np.maximum(norms, 1e-6)
- flat_world /= norms
-
- # Encode back to [0, 1] PNG
- world_normals = flat_world.reshape(H, W, 3)
- encoded = np.clip((world_normals + 1.0) / 2.0, 0.0, 1.0)
- encoded_u8 = (encoded * 255.0).astype(np.uint8)
-
- out_img = Image.fromarray(encoded_u8, mode='RGB')
- buf = io.BytesIO()
- out_img.save(buf, format='PNG')
- print(f" Converted normal map to world space for camera {camera_id}")
- return buf.getvalue()
- except Exception as err:
- print(f" Warning: camera→world normal conversion failed: {err}")
- return None
-
- # ── PBR post-processing helpers ───────────────────────────────────
-
- @staticmethod
- def _compute_image_mean_saturation(image_path_or_bytes, sample_limit=100_000):
- """Compute the mean HSV saturation for a single image.
-
- *image_path_or_bytes* can be a file path (str) or raw PNG bytes.
- Skips very dark pixels whose saturation is unreliable.
- Returns a float in [0, 1], or 0.0 on error.
- """
- import colorsys
- from PIL import Image
-
- try:
- if isinstance(image_path_or_bytes, (str, bytes)) and isinstance(image_path_or_bytes, str):
- img = Image.open(image_path_or_bytes).convert('RGB')
- else:
- img = Image.open(io.BytesIO(image_path_or_bytes)).convert('RGB')
-
- pixels = list(img.getdata())
- step = max(1, len(pixels) // sample_limit)
- total_s, n = 0.0, 0
- for i in range(0, len(pixels), step):
- r, g, b = pixels[i]
- _, s, v = colorsys.rgb_to_hsv(r / 255, g / 255, b / 255)
- if v > 0.05:
- total_s += s
- n += 1
- return total_s / n if n > 0 else 0.0
- except Exception as err:
- print(f" Warning: saturation computation failed: {err}")
- return 0.0
-
- def _compute_uniform_saturation(self, context, use_median=True):
- """Compute a uniform auto-saturation ratio across ALL cameras.
-
- Compares each camera's original rendered image against its raw
- PBR albedo, computes per-camera ratios, then aggregates them
- using the **median** (robust to outlier cameras) or arithmetic
- **mean**, depending on *use_median*.
-
- Returns a float multiplier (1.0 = no correction needed).
- """
- import statistics
-
- ratios = []
- num_cameras = len(self._cameras) if hasattr(self, '_cameras') else 0
-
- for cam_idx in range(num_cameras):
- original_path = get_file_path(
- context, "generated", camera_id=cam_idx,
- material_id=self._material_id)
- raw_path = get_file_path(
- context, "pbr", subtype="raw_albedo",
- camera_id=cam_idx,
- material_id=self._material_id)
-
- if not os.path.exists(original_path) or not os.path.exists(raw_path):
- continue
-
- s_orig = self._compute_image_mean_saturation(original_path)
- s_albedo = self._compute_image_mean_saturation(raw_path)
-
- if s_albedo < 0.01:
- continue # skip grayscale albedos
-
- ratio = s_orig / s_albedo
- ratio = max(0.5, min(ratio, 3.0))
- ratios.append(ratio)
-
- if not ratios:
- return 1.0
-
- if use_median:
- agg = statistics.median(ratios)
- label = "median"
- else:
- agg = sum(ratios) / len(ratios)
- label = "mean"
-
- print(f"[StableGen] Auto saturation: per-camera ratios = "
- f"{[f'{r:.2f}' for r in ratios]}, {label} = {agg:.2f}")
- return agg
-
- def _apply_albedo_postprocessing_batch(self, context):
- """Apply auto-saturation correction to all camera albedos.
-
- Must be called AFTER decomposition saves raw files for all cameras.
- By default computes the averaged saturation ratio across all cameras
- for uniform correction. With per-camera mode, each camera gets its
- own individual ratio.
-
- When correction is disabled, restores raw (unprocessed) albedos
- so that a previously corrected active file doesn't persist.
- """
- auto_on = getattr(context.scene, 'pbr_albedo_auto_saturation', True)
-
- if not auto_on:
- self._restore_raw_albedos(context)
- return
-
- sat_mode = getattr(context.scene, 'pbr_albedo_saturation_mode', 'MEDIAN')
-
- if sat_mode == 'PER_CAMERA':
- # Per-camera mode: individual ratio per camera
- self._apply_per_camera_saturation(context)
- else:
- # Uniform mode (MEDIAN or MEAN)
- effective_sat = self._compute_uniform_saturation(
- context, use_median=(sat_mode == 'MEDIAN'))
-
- if effective_sat == 1.0:
- print("[StableGen] Albedo saturation correction: no-op (ratio ≈ 1.0)")
- self._restore_raw_albedos(context)
- return
-
- for cam_idx in sorted(self._pbr_maps.keys()):
- cam_maps = self._pbr_maps[cam_idx]
- if 'albedo' not in cam_maps:
- continue
-
- raw_path = get_file_path(
- context, "pbr", subtype="raw_albedo",
- camera_id=cam_idx,
- material_id=self._material_id)
- if not os.path.exists(raw_path):
- continue
-
- with open(raw_path, 'rb') as f:
- raw_bytes = f.read()
- processed = self._postprocess_albedo(raw_bytes, effective_sat)
- with open(cam_maps['albedo'], 'wb') as f:
- f.write(processed)
- print(f" Corrected albedo saturation cam {cam_idx} "
- f"(uniform ×{effective_sat:.2f})")
-
- def _apply_per_camera_saturation(self, context):
- """Apply individual saturation correction for each camera."""
- for cam_idx in sorted(self._pbr_maps.keys()):
- cam_maps = self._pbr_maps[cam_idx]
- if 'albedo' not in cam_maps:
- continue
-
- raw_path = get_file_path(
- context, "pbr", subtype="raw_albedo",
- camera_id=cam_idx,
- material_id=self._material_id)
- original_path = get_file_path(
- context, "generated", camera_id=cam_idx,
- material_id=self._material_id)
-
- if not os.path.exists(raw_path) or not os.path.exists(original_path):
- continue
-
- s_orig = self._compute_image_mean_saturation(original_path)
- s_albedo = self._compute_image_mean_saturation(raw_path)
-
- if s_albedo < 0.01:
- continue
-
- ratio = max(0.5, min(s_orig / s_albedo, 3.0))
-
- with open(raw_path, 'rb') as f:
- raw_bytes = f.read()
- processed = self._postprocess_albedo(raw_bytes, ratio)
- with open(cam_maps['albedo'], 'wb') as f:
- f.write(processed)
- print(f" Corrected albedo saturation cam {cam_idx} "
- f"(per-camera ×{ratio:.2f})")
-
- def _restore_raw_albedos(self, context):
- """Copy raw (unprocessed) albedos back to active paths.
-
- Ensures that disabling saturation correction and reprojecting
- uses the original decomposition output, not a previously corrected file.
- """
- import shutil
- for cam_idx in sorted(self._pbr_maps.keys()):
- cam_maps = self._pbr_maps[cam_idx]
- if 'albedo' not in cam_maps:
- continue
- raw_path = get_file_path(
- context, "pbr", subtype="raw_albedo",
- camera_id=cam_idx,
- material_id=self._material_id)
- if os.path.exists(raw_path) and os.path.exists(cam_maps['albedo']):
- shutil.copy2(raw_path, cam_maps['albedo'])
- print(f" Restored raw albedo for camera {cam_idx}")
-
- @staticmethod
- def _postprocess_albedo(image_bytes, saturation=1.0):
- """Apply saturation adjustment to albedo PNG bytes.
-
- Returns post-processed PNG bytes, or the original bytes unchanged
- if saturation is 1.0 (no-op).
- """
- if saturation == 1.0:
- return image_bytes
- from PIL import Image, ImageEnhance
- img = Image.open(io.BytesIO(image_bytes)).convert('RGB')
- img = ImageEnhance.Color(img).enhance(saturation)
- buf = io.BytesIO()
- img.save(buf, format='PNG')
- return buf.getvalue()
-
- def _save_raw_copy(self, context, subtype, camera_id, data_bytes):
- """Save an unprocessed (raw) copy of a PBR map for later re-processing."""
- raw_path = get_file_path(
- context, "pbr", subtype=f"raw_{subtype}",
- camera_id=camera_id,
- material_id=self._material_id)
- try:
- with open(raw_path, 'wb') as f:
- f.write(data_bytes)
- except Exception as err:
- print(f" Warning: failed to save raw {subtype}: {err}")
-
- def _ensure_raw_copies(self, context, camera_id, existing_maps):
- """Ensure a raw albedo copy exists for backward compatibility.
-
- For files generated before the saturation correction feature, the
- active albedo IS the unprocessed original. Copy it to the raw slot
- so that the batch saturation correction can read from it.
- """
- if 'albedo' not in existing_maps:
- return
- raw_path = get_file_path(
- context, "pbr", subtype="raw_albedo",
- camera_id=camera_id,
- material_id=self._material_id)
- if not os.path.exists(raw_path) and os.path.exists(existing_maps['albedo']):
- try:
- import shutil
- shutil.copy2(existing_maps['albedo'], raw_path)
- print(f" Created missing raw albedo from active file "
- f"(camera {camera_id})")
- except Exception as err:
- print(f" Warning: could not create raw albedo copy: {err}")
-
- def _run_pbr_decomposition_batched(self, context, camera_images,
- per_camera_missing=None):
- """Run PBR decomposition **model-first** across all cameras.
-
- Instead of iterating cameras → models (which forces repeated model
- loading/unloading), this iterates models → cameras so each model's
- weights are loaded once and reused for every camera image.
-
- Args:
- context: Blender context.
- camera_images: ``{cam_idx: image_path}`` for cameras that need
- processing.
- per_camera_missing: Optional ``{cam_idx: set_of_missing_map_names}``.
- When provided (reproject mode), only the models needed to
- produce the missing maps are executed, and cameras that
- already have a model's outputs are skipped.
- """
- scene = context.scene
- self._stage = "PBR Decomposition"
- self._progress = 0
- num_cams = len(camera_images)
- cam_ids = sorted(camera_images.keys())
-
- # When per_camera_missing is provided, compute the union of all
- # missing maps to determine which models we actually need.
- if per_camera_missing:
- all_missing = set()
- for missing_set in per_camera_missing.values():
- all_missing |= missing_set
- print(f"[StableGen] Running selective PBR decomposition on "
- f"{num_cams} camera(s), missing maps: {all_missing}")
- else:
- all_missing = None
- print(f"[StableGen] Running batched PBR decomposition on "
- f"{num_cams} camera(s)…")
-
- # Free VRAM before loading PBR models — the previous generation
- # model may still be cached, and Marigold/StableDelight need
- # GPU memory.
- try:
- server_address = context.preferences.addons[
- __package__].preferences.server_address
- self.workflow_manager._flush_comfyui_vram(
- server_address, retries=1, label="PBR pre-load")
- except Exception as e:
- print(f"[StableGen] VRAM flush before PBR failed (non-fatal): {e}")
-
- want_albedo = getattr(scene, 'pbr_map_albedo', True)
- want_roughness = getattr(scene, 'pbr_map_roughness', True)
- want_metallic = getattr(scene, 'pbr_map_metallic', True)
- want_normal = getattr(scene, 'pbr_map_normal', True)
- want_height = getattr(scene, 'pbr_map_height', False)
- want_emission = getattr(scene, 'pbr_map_emission', False)
- emission_method = getattr(scene, 'pbr_emission_method', 'residual')
- albedo_source = getattr(scene, 'pbr_albedo_source', 'delight')
-
- use_delight = (want_albedo and albedo_source == 'delight')
- use_lighting_albedo = (want_albedo and albedo_source == 'lighting')
-
- # ── Determine which Marigold models to run ────────────────────
- # When all_missing is set (selective rerun), only models whose
- # outputs intersect with the missing set are scheduled.
- need_appearance = (
- (want_albedo and albedo_source == 'marigold')
- or want_roughness
- or want_metallic
- )
- need_normals = want_normal
- need_height = want_height
- # IID-Lighting is needed for 'residual' emission OR 'lighting' albedo
- need_iid_lighting = (
- (want_emission and emission_method == 'residual')
- or use_lighting_albedo
- )
-
- if all_missing is not None:
- # Selective mode: narrow down to only models producing missing maps
- need_appearance = need_appearance and bool(
- all_missing & {'albedo', 'roughness', 'metallic'})
- need_normals = need_normals and 'normal' in all_missing
- need_height = need_height and 'height' in all_missing
- need_iid_lighting = need_iid_lighting and bool(
- all_missing & {'emission', 'albedo'})
- use_delight = use_delight and 'albedo' in all_missing
-
- models_to_run = []
- if need_appearance:
- models_to_run.append((
- 'prs-eth/marigold-iid-appearance-v1-1',
- ['albedo', 'material'],
- ))
- if need_normals:
- models_to_run.append((
- 'prs-eth/marigold-normals-lcm-v0-1',
- ['normal'],
- ))
- if need_height:
- models_to_run.append((
- 'prs-eth/marigold-depth-lcm-v1-0',
- ['height'],
- ))
- if need_iid_lighting:
- models_to_run.append((
- 'prs-eth/marigold-iid-lighting-v1-1',
- ['_lighting_albedo', '_lighting_shading', '_lighting_residual'],
- ))
-
- # Count extra post-processing steps (HSV emission)
- extra_emission_steps = 0
- if want_emission and emission_method == 'hsv':
- extra_emission_steps = 1
-
- total_model_steps = (len(models_to_run)
- + (1 if use_delight else 0)
- + extra_emission_steps)
- if total_model_steps == 0:
- print("[StableGen] No PBR maps enabled, skipping decomposition")
- return
-
- # Activate PBR progress tracking for the UI
- self._pbr_active = True
- self._pbr_step = 0
- self._pbr_total_steps = total_model_steps
- self._pbr_cam_total = num_cams
-
- # Ensure every camera has an entry in _pbr_maps
- for cam_idx in cam_ids:
- if cam_idx not in self._pbr_maps:
- self._pbr_maps[cam_idx] = {}
-
- # ── Tiling settings ───────────────────────────────────────────
- tiling_mode = getattr(scene, 'pbr_tiling', 'off')
- tile_grid = getattr(scene, 'pbr_tile_grid', 2)
-
- tile_model_keys = set()
-
- # Custom mode per-map toggles (read once, used later)
- custom_tile_albedo = False
- custom_tile_material = False
- custom_tile_normal = False
- custom_tile_height = False
- custom_tile_emission = False
- # Whether IID-Appearance is the albedo provider
- appearance_provides_albedo = (not use_delight
- and not use_lighting_albedo)
-
- if tiling_mode == 'selective':
- # When using lighting albedo, don't tile appearance
- # (its albedo will be discarded — only material channels needed).
- if not use_lighting_albedo:
- tile_model_keys.add('appearance')
- if use_lighting_albedo:
- tile_model_keys.add('lighting')
- elif tiling_mode == 'all':
- tile_model_keys = {'appearance', 'normals', 'depth', 'lighting'}
- elif tiling_mode == 'custom':
- custom_tile_albedo = getattr(scene, 'pbr_tile_albedo', True)
- custom_tile_material = getattr(scene, 'pbr_tile_material', False)
- custom_tile_normal = getattr(scene, 'pbr_tile_normal', False)
- custom_tile_height = getattr(scene, 'pbr_tile_height', False)
- custom_tile_emission = (
- getattr(scene, 'pbr_tile_emission', False)
- and want_emission
- and emission_method == 'residual'
- )
-
- # IID-Appearance: tile if ANY of its outputs are tiled
- need_tile_appearance = (
- custom_tile_material
- or (custom_tile_albedo and appearance_provides_albedo)
- )
- if need_tile_appearance:
- tile_model_keys.add('appearance')
-
- # IID-Lighting: tile if albedo or residual/emission output
- # needs tiling
- need_tile_lighting = (
- (use_lighting_albedo and custom_tile_albedo)
- or custom_tile_emission
- )
- if need_tile_lighting:
- tile_model_keys.add('lighting')
-
- # Single-output models
- if custom_tile_normal:
- tile_model_keys.add('normals')
- if custom_tile_height:
- tile_model_keys.add('depth')
-
- model_step = 0
-
- # Helper: map model short names to the final PBR map names they
- # produce, used for per-camera skipping in selective mode.
- _MODEL_OUTPUT_MAPS = {
- 'appearance': {'albedo', 'roughness', 'metallic'},
- 'normals': {'normal'},
- 'depth': {'height'},
- 'lighting': {'emission', 'albedo'}, # residual → emission, lighting → albedo
- }
-
- def _camera_needs_model(cam_idx, model_key):
- """Return True if this camera still needs outputs from *model_key*."""
- if per_camera_missing is None or cam_idx not in per_camera_missing:
- return True # full run, always needed
- return bool(per_camera_missing[cam_idx] & _MODEL_OUTPUT_MAPS.get(model_key, set()))
-
- # ── StableDelight pass (all cameras) ──────────────────────────
- if use_delight:
- model_step += 1
- self._pbr_step = model_step
- tile_delight = (tiling_mode in ('selective', 'all')
- or (tiling_mode == 'custom' and custom_tile_albedo))
- tile_label = f" (tiled {tile_grid}×{tile_grid})" if tile_delight else ""
- print(f"[StableGen] Model {model_step}/{total_model_steps}: "
- f"StableDelight{tile_label}")
-
- for ci, cam_idx in enumerate(cam_ids):
- # Skip camera if albedo already exists in selective mode
- if per_camera_missing and cam_idx in per_camera_missing:
- if 'albedo' not in per_camera_missing[cam_idx]:
- print(f" Camera {cam_idx}: albedo exists, skipping StableDelight")
- continue
- image_path = camera_images[cam_idx]
- self._pbr_cam = ci
- self._stage = (f"PBR: StableDelight "
- f"(cam {ci+1}/{num_cams})")
- self._progress = 0
- print(f" Camera {cam_idx} ({ci+1}/{num_cams})…")
-
- if tile_delight:
- result = self._process_model_tiled(
- context, image_path,
- process_fn=self.workflow_manager.generate_delight_map)
- if isinstance(result, list) and result:
- result = result[0]
- else:
- result = self.workflow_manager.generate_delight_map(
- context, image_path
- )
-
- if isinstance(result, dict) and "error" in result:
- print(f" StableDelight error: {result['error']}")
- self._error = f"StableDelight failed: {result['error']}"
- return
- elif isinstance(result, bytes):
- pbr_path = get_file_path(
- context, "pbr", subtype="albedo",
- camera_id=cam_idx,
- material_id=self._material_id)
- try:
- self._save_raw_copy(context, "albedo", cam_idx, result)
- with open(pbr_path, 'wb') as f:
- f.write(result)
- self._pbr_maps[cam_idx]['albedo'] = pbr_path
- print(f" Saved albedo (delight) → "
- f"{os.path.basename(pbr_path)}")
- except Exception as err:
- print(f" Failed to save delight albedo: {err}")
-
- # ── Marigold model passes (all cameras per model) ─────────────
- for model_name, map_names in models_to_run:
- model_step += 1
- self._pbr_step = model_step
- model_short = model_name.split('/')[-1]
- should_tile = any(k in model_short for k in tile_model_keys)
- is_iid = 'appearance' in model_short
- is_iid_lighting = 'lighting' in model_short
-
- tile_label = f" (tiled {tile_grid}×{tile_grid})" if should_tile else ""
- print(f"[StableGen] Model {model_step}/{total_model_steps}: "
- f"{model_name}{tile_label}")
-
- for ci, cam_idx in enumerate(cam_ids):
- # Selective mode: skip camera if it doesn't need this model
- model_key = ('appearance' if is_iid
- else 'lighting' if is_iid_lighting
- else 'normals' if 'normals' in model_short
- else 'depth')
- if not _camera_needs_model(cam_idx, model_key):
- print(f" Camera {cam_idx}: already has outputs for "
- f"{model_key}, skipping")
- continue
- image_path = camera_images[cam_idx]
- self._pbr_cam = ci
- self._stage = (f"PBR: {model_short} "
- f"(cam {ci+1}/{num_cams})")
- self._progress = 0
-
- # ── Dual-run logic ─────────────────────────────────
- # Dual-run = run tiled + untiled, then stitch outputs.
- # Needed when some outputs of a multi-output model
- # should be tiled and others should not.
- dual_run_iid = False
- dual_run_iid_reverse = False # tile material, untile albedo
- dual_run_lighting = False
-
- if tiling_mode == 'selective':
- dual_run_iid = (
- should_tile and is_iid and not use_delight
- )
- dual_run_lighting = (
- should_tile and is_iid_lighting
- and use_lighting_albedo
- )
- elif tiling_mode == 'custom' and should_tile:
- if is_iid:
- # IID-Appearance: [albedo, material]
- want_tiled_albedo = (custom_tile_albedo
- and appearance_provides_albedo)
- want_tiled_material = custom_tile_material
- # Dual-run only when there's a mismatch
- if want_tiled_albedo and not want_tiled_material:
- dual_run_iid = True
- elif want_tiled_material and not want_tiled_albedo:
- dual_run_iid = True
- dual_run_iid_reverse = True
- # If both tiled → full tile (no dual-run)
- # If neither → shouldn't reach here (should_tile=False)
- elif is_iid_lighting:
- # IID-Lighting: [albedo, shading, residual]
- # Determine per-output tiling needs
- want_tiled_l_albedo = (use_lighting_albedo
- and custom_tile_albedo)
- want_tiled_l_residual = custom_tile_emission
- # Shading is always untiled (no benefit).
- # Dual-run when at least one output is tiled
- # but not all (shading is never tiled, so dual-run
- # is needed whenever ANY lighting output is tiled).
- any_tiled = want_tiled_l_albedo or want_tiled_l_residual
- if any_tiled:
- dual_run_lighting = True
-
- extra = ""
- if dual_run_iid and not dual_run_iid_reverse:
- extra = " + untiled material"
- elif dual_run_iid and dual_run_iid_reverse:
- extra = " + untiled albedo"
- elif dual_run_lighting:
- extra = " + untiled shading"
- print(f" Camera {cam_idx} ({ci+1}/{num_cams}){extra}…")
-
- if dual_run_iid:
- result_tiled = self._process_model_tiled(
- context, image_path, model_name)
- result_untiled = self.workflow_manager.generate_pbr_maps(
- context, image_path, model_name=model_name)
- if (isinstance(result_tiled, list)
- and len(result_tiled) >= 2
- and isinstance(result_untiled, list)
- and len(result_untiled) >= 2):
- if dual_run_iid_reverse:
- # Tile material, keep albedo untiled
- result = [result_untiled[0], result_tiled[1]]
- else:
- # Tile albedo, keep material untiled
- result = [result_tiled[0], result_untiled[1]]
- elif isinstance(result_tiled, dict):
- result = result_tiled
- elif isinstance(result_untiled, dict):
- result = result_untiled
- else:
- result = (result_tiled
- if isinstance(result_tiled, list)
- else result_untiled)
- elif dual_run_lighting:
- # IID-Lighting: stitch per-output from tiled/untiled
- result_tiled = self._process_model_tiled(
- context, image_path, model_name)
- result_untiled = self.workflow_manager.generate_pbr_maps(
- context, image_path, model_name=model_name)
- if (isinstance(result_tiled, list)
- and len(result_tiled) >= 3
- and isinstance(result_untiled, list)
- and len(result_untiled) >= 3):
- # Per-output: pick tiled or untiled based on toggles
- if tiling_mode == 'custom':
- tile_l_albedo = (use_lighting_albedo
- and custom_tile_albedo)
- tile_l_residual = custom_tile_emission
- else:
- # selective: tile albedo only
- tile_l_albedo = True
- tile_l_residual = False
- r_albedo = (result_tiled[0] if tile_l_albedo
- else result_untiled[0])
- r_shading = result_untiled[1] # always untiled
- r_residual = (result_tiled[2] if tile_l_residual
- else result_untiled[2])
- result = [r_albedo, r_shading, r_residual]
- elif isinstance(result_tiled, dict):
- result = result_tiled
- elif isinstance(result_untiled, dict):
- result = result_untiled
- else:
- result = (result_tiled
- if isinstance(result_tiled, list)
- else result_untiled)
- elif should_tile:
- # Tile the model even when StableDelight handles albedo —
- # the IID-Appearance albedo output is discarded by
- # _save_pbr_map_outputs, but roughness/metallic still
- # benefit from tiling.
- result = self._process_model_tiled(
- context, image_path, model_name)
- else:
- result = self.workflow_manager.generate_pbr_maps(
- context, image_path, model_name=model_name)
-
- if isinstance(result, dict) and "error" in result:
- print(f" PBR model error: {result['error']}")
- self._error = (f"PBR decomposition failed: "
- f"{result['error']}")
- return
-
- # ── Save each output component ────────────────────
- self._save_pbr_map_outputs(
- context, result, map_names, cam_idx,
- want_roughness=want_roughness,
- want_metallic=want_metallic,
- use_delight=use_delight,
- use_lighting_albedo=use_lighting_albedo)
-
- # ── Post-Marigold emission passes ─────────────────────────────
- if want_emission:
- if emission_method == 'residual':
- # IID-Lighting residual was already saved by the model
- # loop above – now gate it with roughness/metallic.
- model_step += 1
- self._pbr_step = model_step
- self._progress = 0
- print(f"[StableGen] Post-processing emission (residual "
- f"gating) – step {model_step}/{total_model_steps}")
- for ci, cam_idx in enumerate(cam_ids):
- if per_camera_missing and cam_idx in per_camera_missing:
- if 'emission' not in per_camera_missing[cam_idx]:
- continue
- self._pbr_cam = ci
- self._stage = (f"PBR: Emission gating "
- f"(cam {ci+1}/{num_cams})")
- self._progress = (ci / num_cams) * 100
- self._gate_emission_residual(context, cam_idx)
- self._progress = 100
-
- elif emission_method == 'hsv':
- model_step += 1
- self._pbr_step = model_step
- self._progress = 0
- print(f"[StableGen] Emission via HSV threshold "
- f"– step {model_step}/{total_model_steps}")
- for ci, cam_idx in enumerate(cam_ids):
- if per_camera_missing and cam_idx in per_camera_missing:
- if 'emission' not in per_camera_missing[cam_idx]:
- continue
- self._pbr_cam = ci
- self._stage = (f"PBR: HSV emission "
- f"(cam {ci+1}/{num_cams})")
- self._progress = (ci / num_cams) * 100
- image_path = camera_images[cam_idx]
- self._generate_emission_hsv(context, cam_idx, image_path)
- self._progress = 100
-
- # Deactivate PBR progress tracking
- self._pbr_active = False
- self._pbr_step = 0
- self._pbr_total_steps = 0
-
- print(f"[StableGen] PBR decomposition complete for "
- f"{num_cams} camera(s)")
-
- def _save_pbr_map_outputs(self, context, result, map_names, camera_id,
- want_roughness=True, want_metallic=True,
- use_delight=False, use_lighting_albedo=False):
- """Save the output images from a single model run to disk.
-
- Handles the IID material channel split (R=roughness, G=metallic)
- and the camera→world normal conversion.
-
- Args:
- result: list of bytes (one per output map).
- map_names: list of names corresponding to each output.
- camera_id: Camera index.
- want_roughness / want_metallic: Whether to save those channels.
- use_delight: Whether StableDelight handles albedo (skip IID albedo).
- """
- scene = context.scene
- for i, map_bytes in enumerate(result):
- map_name = map_names[i] if i < len(map_names) else f"component_{i}"
-
- # ── IID-Appearance "material" channel split ───────────
- if map_name == 'material':
- try:
- mat_img = Image.open(io.BytesIO(map_bytes)).convert('RGB')
- except Exception as err:
- print(f" Failed to decode material image: {err}")
- continue
-
- if want_roughness:
- rough_img = mat_img.getchannel('R').convert('L')
- rough_path = get_file_path(
- context, "pbr", subtype="roughness",
- camera_id=camera_id,
- material_id=self._material_id)
- try:
- rough_img.save(rough_path)
- self._pbr_maps[camera_id]['roughness'] = rough_path
- print(f" Saved roughness (material R) → "
- f"{os.path.basename(rough_path)}")
- except Exception as err:
- print(f" Failed to save roughness: {err}")
-
- if want_metallic:
- metal_img = mat_img.getchannel('G').convert('L')
- metal_path = get_file_path(
- context, "pbr", subtype="metallic",
- camera_id=camera_id,
- material_id=self._material_id)
- try:
- metal_img.save(metal_path)
- self._pbr_maps[camera_id]['metallic'] = metal_path
- print(f" Saved metallic (material G) → "
- f"{os.path.basename(metal_path)}")
- except Exception as err:
- print(f" Failed to save metallic: {err}")
- continue
-
- # ── IID-Lighting intermediate outputs ─────────────────
- # These are prefixed with '_lighting_' to avoid toggle
- # checks. The residual is saved for later gating; albedo
- # and shading are discarded unless using lighting albedo.
- if map_name.startswith('_lighting_'):
- lighting_key = map_name # e.g. '_lighting_residual'
- lighting_path = get_file_path(
- context, "pbr", subtype=lighting_key.lstrip('_'),
- camera_id=camera_id,
- material_id=self._material_id)
- try:
- with open(lighting_path, 'wb') as f:
- f.write(map_bytes)
- self._pbr_maps[camera_id][lighting_key] = lighting_path
- print(f" Saved {lighting_key} → "
- f"{os.path.basename(lighting_path)}")
-
- # When using IID-Lighting as albedo source, copy the
- # lighting albedo to the main albedo slot as well.
- if map_name == '_lighting_albedo' and use_lighting_albedo:
- albedo_path = get_file_path(
- context, "pbr", subtype="albedo",
- camera_id=camera_id,
- material_id=self._material_id)
- self._save_raw_copy(context, "albedo", camera_id, map_bytes)
- with open(albedo_path, 'wb') as f:
- f.write(map_bytes)
- self._pbr_maps[camera_id]['albedo'] = albedo_path
- print(f" Saved albedo (IID-Lighting) → "
- f"{os.path.basename(albedo_path)}")
- except Exception as err:
- print(f" Failed to save {lighting_key}: {err}")
- continue
-
- # Skip maps the user didn't enable
- toggle_attr = f"pbr_map_{map_name}"
- if hasattr(scene, toggle_attr) and not getattr(scene, toggle_attr):
- continue
-
- # Skip Marigold IID-Appearance albedo when another source handles it
- if map_name == 'albedo' and (use_delight or use_lighting_albedo):
- continue
-
- pbr_path = get_file_path(
- context, "pbr", subtype=map_name,
- camera_id=camera_id,
- material_id=self._material_id)
- try:
- # ── Camera→world-space conversion for normals ─────
- if map_name == 'normal':
- converted = self._convert_normals_cam_to_world(
- map_bytes, camera_id)
- if converted is not None:
- map_bytes = converted
-
- # ── Raw save for albedo (post-processing applied in batch) ─
- if map_name == 'albedo':
- self._save_raw_copy(context, "albedo", camera_id, map_bytes)
-
- with open(pbr_path, 'wb') as f:
- f.write(map_bytes)
- self._pbr_maps[camera_id][map_name] = pbr_path
- print(f" Saved {map_name} → "
- f"{os.path.basename(pbr_path)}")
- except Exception as err:
- print(f" Failed to save PBR map {map_name}: {err}")
-
- # ── Emission extraction methods ───────────────────────────────────
-
- def _gate_emission_residual(self, context, camera_id):
- """Gate the IID-Lighting residual with roughness/metallic to
- produce a cleaner emission map.
-
- emission = residual × (1 − metallic) × roughness_mask, then
- threshold. High-metallic + low-roughness regions are likely
- specular reflections, not true emission.
- """
- scene = context.scene
- cam_maps = self._pbr_maps.get(camera_id, {})
- residual_path = cam_maps.get('_lighting_residual')
- if not residual_path or not os.path.exists(residual_path):
- print(f" Emission residual not found for camera {camera_id}")
- return
-
- threshold = getattr(scene, 'pbr_emission_threshold', 0.2)
-
- try:
- residual = np.array(Image.open(residual_path).convert('RGB')).astype(np.float32) / 255.0
-
- # Optionally gate with metallic if available — high metallic
- # surfaces produce strong specular that isn't true emission.
- metal_path = cam_maps.get('metallic')
- if metal_path and os.path.exists(metal_path):
- metal_img = Image.open(metal_path).convert('L')
- # Resize metallic to match residual dims (they may differ
- # when tiling/super-res settings aren't identical).
- res_h, res_w = residual.shape[:2]
- if metal_img.size != (res_w, res_h):
- metal_img = metal_img.resize(
- (res_w, res_h), Image.Resampling.BILINEAR)
- metallic = np.array(metal_img).astype(np.float32) / 255.0
- gate = (1.0 - metallic)[:, :, np.newaxis]
- residual = residual * gate
-
- # Apply soft threshold — pixels below the threshold are
- # smoothly faded out rather than hard-clipped.
- luminance = np.mean(residual, axis=2)
- # Smooth fade: rescale [0, threshold] → 0, [threshold, 1] → preserved
- fade = np.clip((luminance - threshold * 0.5) / max(threshold * 0.5, 1e-6),
- 0.0, 1.0)[:, :, np.newaxis]
- emission = residual * fade
-
- emission_img = Image.fromarray(
- np.clip(emission * 255, 0, 255).astype(np.uint8), mode='RGB')
- emission_path = get_file_path(
- context, "pbr", subtype="emission",
- camera_id=camera_id,
- material_id=self._material_id)
- emission_img.save(emission_path)
- self._pbr_maps[camera_id]['emission'] = emission_path
- print(f" Saved emission (residual gated) → "
- f"{os.path.basename(emission_path)}")
- except Exception as err:
- print(f" Failed to generate emission (residual): {err}")
- import traceback; traceback.print_exc()
-
- def _generate_emission_hsv(self, context, camera_id, image_path):
- """Extract emission via high-saturation + high-value thresholding
- in HSV space. Glowing objects retain colour intensity while
- normally-lit surfaces desaturate under bright light.
- """
- scene = context.scene
- sat_min = getattr(scene, 'pbr_emission_saturation_min', 0.5)
- val_min = getattr(scene, 'pbr_emission_value_min', 0.85)
- bloom_radius = getattr(scene, 'pbr_emission_bloom', 5.0)
- threshold = getattr(scene, 'pbr_emission_threshold', 0.3)
-
- try:
- original = np.array(Image.open(image_path).convert('RGB'))
- rgb_f = original.astype(np.float32) / 255.0
-
- # RGB → HSV
- maxc = rgb_f.max(axis=2)
- minc = rgb_f.min(axis=2)
- delta = maxc - minc
- saturation = np.where(maxc > 1e-6, delta / maxc, 0.0)
- value = maxc
-
- # Build binary mask: high saturation AND high value
- mask = ((saturation >= sat_min) & (value >= val_min)).astype(np.float32)
-
- # Apply bloom (Gaussian blur) to soften edges
- if bloom_radius > 0:
- try:
- import cv2
- ksize = int(bloom_radius * 4) | 1 # must be odd
- mask = cv2.GaussianBlur(mask, (ksize, ksize), bloom_radius)
- except ImportError:
- # Fallback: simple box blur via PIL
- from PIL import ImageFilter
- mask_pil = Image.fromarray(
- (mask * 255).astype(np.uint8), mode='L')
- mask_pil = mask_pil.filter(
- ImageFilter.GaussianBlur(radius=bloom_radius))
- mask = np.array(mask_pil).astype(np.float32) / 255.0
-
- # Apply threshold on the blurred mask
- mask = np.where(mask > threshold, mask, 0.0)
-
- # Multiply mask by original RGB to get coloured emission
- emission = rgb_f * mask[:, :, np.newaxis]
-
- emission_img = Image.fromarray(
- np.clip(emission * 255, 0, 255).astype(np.uint8), mode='RGB')
- emission_path = get_file_path(
- context, "pbr", subtype="emission",
- camera_id=camera_id,
- material_id=self._material_id)
- emission_img.save(emission_path)
- self._pbr_maps[camera_id]['emission'] = emission_path
- print(f" Saved emission (HSV threshold) → "
- f"{os.path.basename(emission_path)}")
- except Exception as err:
- print(f" Failed to generate emission (HSV): {err}")
- import traceback; traceback.print_exc()
-
- 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.
- :param context: Blender context.
- :param camera_id: ID of the camera.
- :return: None
- """
- print("Exporting depth map")
- # Save original settings to restore later.
- original_engine = bpy.context.scene.render.engine
- original_view_transform = bpy.context.scene.view_settings.view_transform
- original_film_transparent = bpy.context.scene.render.film_transparent
- 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"
-
- # Ensure the directory exists
- if not os.path.exists(output_dir):
- os.makedirs(output_dir)
-
- # Get the active view layer
- view_layer = bpy.context.view_layer
-
- # Switch to WORKBENCH render engine
- bpy.context.scene.render.engine = 'BLENDER_WORKBENCH'
-
- bpy.context.scene.display_settings.display_device = 'sRGB'
- bpy.context.scene.view_settings.view_transform = 'Raw'
-
- original_pass_z = view_layer.use_pass_z
-
- # Enable depth pass in the render settings
- view_layer.use_pass_z = True
-
- # Enable compositor pipeline (may have been disabled by prior GIF export)
- bpy.context.scene.render.use_compositing = True
- bpy.context.scene.use_nodes = True
- node_tree = get_compositor_node_tree(bpy.context.scene)
- nodes = node_tree.nodes
- links = node_tree.links
-
- # 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)
-
- # Add a normalize node (to scale depth values between 0 and 1)
- normalize_node = nodes.new(type="CompositorNodeNormalize")
- normalize_node.location = (200, 0)
- links.new(render_layers_node.outputs["Depth"], normalize_node.inputs[0])
-
- # Add an invert node to flip the depth map values
- invert_node = nodes.new(type="CompositorNodeInvert")
- invert_node.location = (400, 0)
- # Blender 5.x uses named "Color" input, 4.x uses index 1
- color_input = invert_node.inputs["Color"] if "Color" in invert_node.inputs else invert_node.inputs[1]
- links.new(normalize_node.outputs[0], color_input)
-
- # Add an output file node
- output_node = nodes.new(type="CompositorNodeOutputFile")
- output_node.location = (600, 0)
- configure_output_node_paths(output_node, output_dir, output_file)
- links.new(invert_node.outputs[0], output_node.inputs[0])
-
- # Render the scene
- bpy.ops.render.render(write_still=True)
-
- bpy.context.scene.view_settings.view_transform = 'Standard'
-
- print(f"Depth map saved to: {os.path.join(output_dir, output_file)}.png")
-
- # Restore original settings
- bpy.context.scene.render.engine = original_engine
- bpy.context.scene.view_settings.view_transform = original_view_transform
- bpy.context.scene.render.film_transparent = original_film_transparent
- bpy.context.scene.render.use_compositing = original_use_compositing
- bpy.context.scene.render.filepath = original_filepath
- view_layer.use_pass_z = original_pass_z
-
- def export_normal(self, context, camera_id=None):
- """
- Exports the normal map of the scene.
- Areas without geometry will show the neutral color (0.5, 0.5, 1.0).
- :param context: Blender context.
- :param camera_id: ID of the camera.
- :return: None
- """
- print("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"
-
- if not os.path.exists(output_dir):
- os.makedirs(output_dir)
-
- view_layer = bpy.context.view_layer
- original_pass_normal = view_layer.use_pass_normal
- view_layer.use_pass_normal = True
-
- # Store original settings to restore later.
- original_engine = bpy.context.scene.render.engine
- original_view_transform = bpy.context.scene.view_settings.view_transform
- original_film_transparent = bpy.context.scene.render.film_transparent
- original_use_compositing = bpy.context.scene.render.use_compositing
- original_filepath = bpy.context.scene.render.filepath
-
- bpy.context.scene.render.engine = get_eevee_engine_id()
- bpy.context.scene.view_settings.view_transform = 'Raw'
- bpy.context.scene.render.film_transparent = True
- bpy.context.scene.render.use_compositing = True
- bpy.context.scene.use_nodes = True
-
- # Clear existing nodes.
- 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")
- render_layers_node.location = (0, 0)
-
- # Create an RGB node set to the neutral normal color (0.5, 0.5, 1.0, 1.0).
- bg_node = nodes.new(type="CompositorNodeRGB")
- bg_node.outputs[0].default_value = (0.5, 0.5, 1.0, 1.0)
- bg_node.location = (0, -200)
-
- alpha_over_node = nodes.new(type="CompositorNodeAlphaOver")
- alpha_over_node.location = (200, 0)
- # Link the normal pass to the top input.
- links.new(render_layers_node.outputs["Normal"], alpha_over_node.inputs[2])
- # Link the neutral background to the bottom input.
- links.new(bg_node.outputs[0], alpha_over_node.inputs[1])
-
- # Create the Output File node.
- output_node = nodes.new(type="CompositorNodeOutputFile")
- output_node.location = (400, 0)
- configure_output_node_paths(output_node, output_dir, output_file)
- links.new(alpha_over_node.outputs[0], output_node.inputs[0])
- links.new(render_layers_node.outputs["Alpha"], alpha_over_node.inputs[0])
-
- bpy.ops.render.render(write_still=True)
-
- # Restore original settings.
- bpy.context.scene.render.engine = original_engine
- bpy.context.scene.view_settings.view_transform = original_view_transform
- bpy.context.scene.render.film_transparent = original_film_transparent
- bpy.context.scene.render.use_compositing = original_use_compositing
- bpy.context.scene.render.filepath = original_filepath
-
- view_layer.use_pass_normal = original_pass_normal
-
- print(f"Normal map saved to: {os.path.join(output_dir, output_file)}.png")
-
- def combine_maps(self, context, cameras, type):
- """Combines depth maps into a grid."""
- if type == 'depth':
- grid_image_path = get_file_path(context, "controlnet", subtype="depth", camera_id=None, material_id=self._material_id)
- elif type == 'canny':
- grid_image_path = get_file_path(context, "controlnet", subtype="canny", camera_id=None, material_id=self._material_id)
- elif type == 'normal':
- grid_image_path = get_file_path(context, "controlnet", subtype="normal", camera_id=None, material_id=self._material_id)
- elif type == 'workbench':
- grid_image_path = get_file_path(context, "controlnet", subtype="workbench", camera_id=None, material_id=self._material_id)
- elif type == 'viewport':
- grid_image_path = get_file_path(context, "controlnet", subtype="viewport", camera_id=None, material_id=self._material_id)
-
- # Render depth maps for each camera and combine them into a grid
- depth_maps = []
- for i, camera in enumerate(cameras):
- bpy.context.scene.camera = camera
- if type == 'depth':
- depth_map_path = get_file_path(context, "controlnet", subtype="depth", camera_id=i, material_id=self._material_id)
- elif type == 'canny':
- depth_map_path = get_file_path(context, "controlnet", subtype="canny", camera_id=i, material_id=self._material_id)
- elif type == 'normal':
- depth_map_path = get_file_path(context, "controlnet", subtype="normal", camera_id=i, material_id=self._material_id)
- elif type == 'workbench':
- depth_map_path = get_file_path(context, "controlnet", subtype="workbench", camera_id=i, material_id=self._material_id)
- elif type == 'viewport':
- depth_map_path = get_file_path(context, "controlnet", subtype="viewport", camera_id=i, material_id=self._material_id)
- depth_maps.append(depth_map_path)
-
- # Combine depth maps into a grid
- grid_image = self.create_grid_image(depth_maps)
- grid_image = self.rescale_to_1mp(grid_image)
- grid_image.save(grid_image_path)
- print(f"Combined depth map grid saved to: {grid_image_path}")
-
- def create_grid_image(self, image_paths):
- """Creates a grid image from a list of image paths."""
- images = [Image.open(path) for path in image_paths]
- widths, heights = zip(*(i.size for i in images))
-
- # Calculate grid dimensions to make it as square as possible
- num_images = len(images)
- grid_width = math.ceil(math.sqrt(num_images))
- grid_height = math.ceil(num_images / grid_width)
-
- max_width = max(widths)
- max_height = max(heights)
-
- total_width = grid_width * max_width
- total_height = grid_height * max_height
-
- grid_image = Image.new('RGB', (total_width, total_height))
-
- x_offset = 0
- y_offset = 0
- for i, img in enumerate(images):
- grid_image.paste(img, (x_offset, y_offset))
- x_offset += max_width
- if (i + 1) % grid_width == 0:
- x_offset = 0
- y_offset += max_height
-
- return grid_image
-
- def rescale_to_1mp(self, image):
- """Rescales the image to approximately 1MP."""
-
- width, height = image.size
- total_pixels = width * height
- scale_factor = (1_000_000 / total_pixels) ** 0.5
-
- new_width = int(width * scale_factor)
- new_height = int(height * scale_factor)
-
- # Ensure the new dimensions are divisible by 8 (ComfyUI requirement)
- new_width -= new_width % 8
- new_height -= new_height % 8
-
- self._grid_height = new_height
- self._grid_width = new_width
-
- return image.resize((new_width, new_height), Image.LANCZOS)
-
- def split_generated_grid(self, context, cameras):
- """Splits the generated grid image back into multiple images."""
- grid_image_path = get_file_path(context, "generated", camera_id=None, material_id=self._material_id)
-
- # Load the generated grid image
- grid_image = Image.open(grid_image_path)
-
- # Calculate grid dimensions to make it as square as possible
- num_images = len(cameras)
- grid_width = math.ceil(math.sqrt(num_images))
- grid_height = math.ceil(num_images / grid_width)
-
- max_width = grid_image.width // grid_width
- max_height = grid_image.height // grid_height
-
- x_offset = 0
- y_offset = 0
- for i in range(num_images):
- bbox = (x_offset, y_offset, x_offset + max_width, y_offset + max_height)
- individual_image = grid_image.crop(bbox)
- individual_image_path = get_file_path(context, "generated", camera_id=i, material_id=self._material_id)
- individual_image.save(individual_image_path)
- print(f"Generated image for camera {i+1} saved to: {individual_image_path}")
- x_offset += max_width
- if (i + 1) % grid_width == 0:
- x_offset = 0
- y_offset += max_height
-
- def _get_uploaded_image_info(self, context, file_type, subtype=None, filename=None, camera_id=None, object_name=None, material_id=None):
- """
- Gets local path, uploads if needed, caches, and returns ComfyUI upload info.
- Intended to be called within the ComfyUIGenerate operator instance.
-
- Args:
- self: The instance of the ComfyUIGenerate operator.
- context: Blender context.
- file_type: Type of file (e.g., "controlnet", "generated", "baked").
- subtype: Subtype (e.g., "depth", "render").
- filename: Specific filename if overriding default naming.
- camera_id: Camera index.
- object_name: Object name.
- material_id: Material index.
-
- Returns:
- dict: Upload info from ComfyUI (containing 'name', etc.) or None if failed/not found.
- """
- effective_material_id = material_id
-
- # Use the existing get_file_path to determine the canonical local path
- if not file_type == "custom": # Custom files use provided filename directly
- local_path = get_file_path(context, file_type, subtype, filename, camera_id, object_name, effective_material_id)
- else:
- local_path = filename
-
- # --- Image Modification for 'recent' sequential mode ---
- # Check if we need to modify the image before uploading
- is_recent_mode_ref = (
- file_type == "generated" and
- context.scene.sequential_ipadapter_mode == 'recent' and
- (context.scene.sequential_ipadapter or context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein'))
- )
-
- temp_image_path = None
- upload_path = local_path
-
- if is_recent_mode_ref:
- desaturate = context.scene.sequential_desaturate_factor
- contrast = context.scene.sequential_contrast_factor
-
- if desaturate > 0.0 or contrast > 0.0:
- try:
- with Image.open(local_path) as img:
- if desaturate > 0.0:
- enhancer = ImageEnhance.Color(img)
- img = enhancer.enhance(1.0 - desaturate)
-
- if contrast > 0.0:
- enhancer = ImageEnhance.Contrast(img)
- img = enhancer.enhance(1.0 - contrast)
-
- # Save to a temporary file for upload
- temp_dir = get_dir_path(context, "temp")
- os.makedirs(temp_dir, exist_ok=True)
- temp_image_path = os.path.join(temp_dir, f"temp_{os.path.basename(local_path)}")
- img.save(temp_image_path)
- upload_path = temp_image_path
- except Exception as e:
- print(f"Error modifying image {local_path}: {e}. Uploading original.")
- upload_path = local_path # Fallback to original on error
- # --- End Image Modification ---
-
- # Use the operator's instance cache variable (self._uploaded_images_cache)
- if not hasattr(self, '_uploaded_images_cache') or self._uploaded_images_cache is None:
- # Initialize cache if it doesn't exist (e.g., first call in execute)
- # Although clearing in execute() is preferred
- self._uploaded_images_cache = {}
- print("Warning: _uploaded_images_cache not found, initializing. Should be cleared in execute().")
-
-
- # Check cache first using the absolute local path as the key
- absolute_local_path = os.path.abspath(upload_path)
- cached_info = self._uploaded_images_cache.get(absolute_local_path)
- if cached_info is not None: # Can be None if previous upload failed
- # print(f"Debug: Using cached upload info for: {absolute_local_path}")
- return cached_info # Return cached info (could be None if failed before)
-
- # File exists locally? If not, we can't upload. Return None. Cache this result.
- if not os.path.exists(absolute_local_path) or not os.path.isfile(absolute_local_path):
- # print(f"Debug: Local file not found or not a file, cannot upload: {absolute_local_path}")
- self._uploaded_images_cache[absolute_local_path] = None # Cache the fact that it's missing/invalid
- return None
-
- # Not cached and file exists, try to upload it
- server_address = context.preferences.addons[__package__].preferences.server_address
- uploaded_info = upload_image_to_comfyui(server_address, absolute_local_path)
-
- # Store result (the info dict or None if upload failed) in cache
- self._uploaded_images_cache[absolute_local_path] = uploaded_info
-
- # Clean up the temporary file after upload
- if temp_image_path and os.path.exists(temp_image_path):
- os.remove(temp_image_path)
-
- if uploaded_info:
- return uploaded_info
- else:
- # Upload failed, error message was printed by upload_image_to_comfyui
- # Returning None allows optional inputs to be skipped gracefully.
- # If a *required* image fails to upload, the workflow submission
- # will likely fail later when ComfyUI can't find the input.
- return None
-
-
-# ── Preview Gallery helpers ───────────────────────────────────────────
-
-def _draw_rect_2d(x1, y1, x2, y2, color):
- """Draw a filled rectangle in 2D screen-space."""
- shader = gpu.shader.from_builtin('UNIFORM_COLOR')
- batch = batch_for_shader(
- shader, 'TRIS',
- {"pos": ((x1, y1), (x2, y1), (x2, y2), (x1, y2))},
- indices=((0, 1, 2), (0, 2, 3)),
- )
- shader.bind()
- shader.uniform_float("color", color)
- gpu.state.blend_set('ALPHA')
- batch.draw(shader)
- gpu.state.blend_set('NONE')
-
-
-def _draw_texture_2d(texture, x1, y1, x2, y2):
- """Draw a textured rectangle in 2D screen-space."""
- shader = gpu.shader.from_builtin('IMAGE')
- batch = batch_for_shader(
- shader, 'TRIS',
- {"pos": ((x1, y1), (x2, y1), (x2, y2), (x1, y2)),
- "texCoord": ((0, 0), (1, 0), (1, 1), (0, 1))},
- indices=((0, 1, 2), (0, 2, 3)),
- )
- shader.bind()
- shader.uniform_sampler("image", texture)
- gpu.state.blend_set('ALPHA')
- batch.draw(shader)
- gpu.state.blend_set('NONE')
-
-
-class _PreviewGalleryOverlay:
- """Viewport overlay that shows N generated images and lets the user pick one.
-
- Lifecycle:
- 1. ``__init__`` — creates bpy.data.images + GPU textures, registers draw handler.
- 2. ``handle_mouse_move`` / ``handle_click`` — called from Trellis2Generate.modal().
- 3. ``update_images`` — called when "Generate More" produces a new batch.
- 4. ``cleanup`` — removes draw handler and temp images.
- """
-
- def __init__(self, pil_images, seeds):
- """*pil_images*: list[PIL.Image] *seeds*: list[int]"""
- self._pil_images = list(pil_images)
- self._seeds = list(seeds)
- self._n = len(pil_images)
- self._hover_idx = -1
- self._more_hover = False
- self._cancel_hover = False
- self._selected_idx = -1
- self.action = None # 'select' | 'more' | 'cancel'
- self._cols = max(1, math.ceil(math.sqrt(self._n)))
- self._rows = max(1, math.ceil(self._n / self._cols))
- self._cell_rects: list[tuple] = []
- self._more_rect: tuple | None = None
- self._cancel_rect: tuple | None = None
- self._bpy_images: list = []
- self._textures: list = []
- self._setup_textures()
- self._draw_handle = bpy.types.SpaceView3D.draw_handler_add(
- self._draw, (), 'WINDOW', 'POST_PIXEL')
-
- # ── texture management ──
-
- def _setup_textures(self):
- self._clear_textures()
- for i, pil_img in enumerate(self._pil_images):
- name = f"_sg_gallery_{i}"
- if name in bpy.data.images:
- bpy.data.images.remove(bpy.data.images[name])
- rgba = pil_img.convert('RGBA')
- w, h = rgba.size
- bpy_img = bpy.data.images.new(name, w, h, alpha=True)
- # Mark as Non-Color so gpu.texture.from_image() stores raw
- # sRGB values — the viewport's own output transform will
- # apply the single correct sRGB curve on display.
- bpy_img.colorspace_settings.name = 'Non-Color'
- # Blender images are stored bottom-to-top; PIL is top-to-bottom
- flipped = rgba.transpose(Image.FLIP_TOP_BOTTOM)
- flat = np.array(flipped, dtype=np.float32).ravel() / 255.0
- bpy_img.pixels.foreach_set(flat)
- bpy_img.pack()
- self._bpy_images.append(bpy_img)
- self._textures.append(gpu.texture.from_image(bpy_img))
-
- def _clear_textures(self):
- self._textures.clear()
- for img in self._bpy_images:
- if img.name in bpy.data.images:
- bpy.data.images.remove(img)
- self._bpy_images.clear()
-
- def update_images(self, pil_images, seeds):
- """Replace the gallery with a new batch."""
- self._pil_images = list(pil_images)
- self._seeds = list(seeds)
- self._n = len(pil_images)
- self._cols = max(1, math.ceil(math.sqrt(self._n)))
- self._rows = max(1, math.ceil(self._n / self._cols))
- self._hover_idx = -1
- self._more_hover = False
- self._cancel_hover = False
- self._selected_idx = -1
- self.action = None
- self._setup_textures()
-
- def cleanup(self):
- if self._draw_handle:
- bpy.types.SpaceView3D.draw_handler_remove(self._draw_handle, 'WINDOW')
- self._draw_handle = None
- self._clear_textures()
-
- # ── interaction ──
-
- def handle_mouse_move(self, mx, my):
- """Update hover state. Returns True if display should redraw."""
- old = (self._hover_idx, self._more_hover, self._cancel_hover)
- self._hover_idx = -1
- self._more_hover = False
- self._cancel_hover = False
- for i, rect in enumerate(self._cell_rects):
- x1, y1, x2, y2 = rect
- if x1 <= mx <= x2 and y1 <= my <= y2:
- self._hover_idx = i
- break
- if self._more_rect:
- x1, y1, x2, y2 = self._more_rect
- if x1 <= mx <= x2 and y1 <= my <= y2:
- self._more_hover = True
- if self._cancel_rect:
- x1, y1, x2, y2 = self._cancel_rect
- if x1 <= mx <= x2 and y1 <= my <= y2:
- self._cancel_hover = True
- return old != (self._hover_idx, self._more_hover, self._cancel_hover)
-
- def handle_click(self, mx, my):
- """Returns 'select', 'more', 'cancel', or None."""
- if self._more_hover:
- return 'more'
- if self._cancel_hover:
- return 'cancel'
- if self._hover_idx >= 0:
- self._selected_idx = self._hover_idx
- return 'select'
- return None
-
- @property
- def selected_seed(self):
- if 0 <= self._selected_idx < len(self._seeds):
- return self._seeds[self._selected_idx]
- return None
-
- @property
- def selected_image_bytes(self):
- """Return PNG bytes of the selected image (for TRELLIS workflow)."""
- if 0 <= self._selected_idx < len(self._pil_images):
- buf = io.BytesIO()
- self._pil_images[self._selected_idx].save(buf, format='PNG')
- return buf.getvalue()
- return None
-
- # ── drawing ──
-
- def _draw(self):
- """POST_PIXEL callback — draws the full-screen gallery overlay."""
- context = bpy.context
- region = context.region
- if not region:
- return
- vw, vh = region.width, region.height
- if vw < 100 or vh < 100:
- return
-
- # Detect sidebar (N-panel) width so the grid avoids being covered.
- sidebar_w = 0
- area = context.area
- if area:
- for r in area.regions:
- if r.type == 'UI' and r.width > 1:
- sidebar_w = r.width
- break
-
- # Layout constants
- pad = 20
- btn_h = 40
- title_h = 30
- bottom_area = btn_h + pad * 2
- usable_w = vw - sidebar_w # exclude area behind the N-panel
-
- # --- dark backdrop ---
- _draw_rect_2d(0, 0, vw, vh, (0.08, 0.08, 0.08, 0.90))
-
- # --- title ---
- blf.size(0, 20)
- title = "Select an image — or generate more"
- tw, th = blf.dimensions(0, title)
- blf.position(0, (usable_w - tw) / 2, vh - pad - th, 0)
- blf.color(0, 1.0, 1.0, 1.0, 1.0)
- blf.draw(0, title)
-
- grid_top = vh - pad - title_h - pad
- available_w = usable_w - pad * 2
- available_h = grid_top - bottom_area - pad
-
- cell_w = max(1, available_w // self._cols)
- cell_h = max(1, available_h // self._rows)
-
- # Maintain image aspect ratio
- if self._pil_images:
- img_aspect = self._pil_images[0].width / max(1, self._pil_images[0].height)
- desired_h = cell_w / img_aspect
- if desired_h > cell_h:
- cell_w = int(cell_h * img_aspect)
- else:
- cell_h = int(desired_h)
-
- inner = 6
- total_grid_w = self._cols * cell_w
- offset_x = (usable_w - total_grid_w) / 2.0
-
- self._cell_rects.clear()
-
- for i in range(self._n):
- row = i // self._cols
- col = i % self._cols
- x1 = offset_x + col * cell_w + inner
- y2 = grid_top - row * cell_h - inner
- x2 = x1 + cell_w - inner * 2
- y1 = y2 - cell_h + inner * 2
- self._cell_rects.append((x1, y1, x2, y2))
-
- # Hover highlight ring
- if i == self._hover_idx:
- _draw_rect_2d(x1 - 3, y1 - 3, x2 + 3, y2 + 3, (0.35, 0.65, 1.0, 0.85))
-
- # Image texture
- if i < len(self._textures):
- _draw_texture_2d(self._textures[i], x1, y1, x2, y2)
-
- # Number badge
- blf.size(0, 15)
- label = str(i + 1)
- lw, lh = blf.dimensions(0, label)
- _draw_rect_2d(x1, y2 - lh - 8, x1 + lw + 12, y2, (0.0, 0.0, 0.0, 0.70))
- blf.position(0, x1 + 6, y2 - lh - 3, 0)
- blf.color(0, 1.0, 1.0, 1.0, 1.0)
- blf.draw(0, label)
-
- # Seed label
- blf.size(0, 11)
- seed_txt = f"Seed: {self._seeds[i]}"
- sw, sh = blf.dimensions(0, seed_txt)
- _draw_rect_2d(x1, y1, x1 + sw + 10, y1 + sh + 6, (0, 0, 0, 0.6))
- blf.position(0, x1 + 5, y1 + 3, 0)
- blf.color(0, 0.7, 0.7, 0.7, 1.0)
- blf.draw(0, seed_txt)
-
- # --- buttons ---
- btn_w = 160
- btn_y1 = pad
- btn_y2 = pad + btn_h
-
- # "Generate More"
- center_x = usable_w / 2.0
- mx1 = center_x - btn_w - 10
- mx2 = center_x - 10
- c = (0.25, 0.55, 0.85, 0.95) if self._more_hover else (0.20, 0.35, 0.55, 0.85)
- _draw_rect_2d(mx1, btn_y1, mx2, btn_y2, c)
- self._more_rect = (mx1, btn_y1, mx2, btn_y2)
- blf.size(0, 15)
- mt = "Generate More"
- mtw, mth = blf.dimensions(0, mt)
- blf.position(0, (mx1 + mx2 - mtw) / 2, (btn_y1 + btn_y2 - mth) / 2, 0)
- blf.color(0, 1.0, 1.0, 1.0, 1.0)
- blf.draw(0, mt)
-
- # "Cancel"
- cx1 = center_x + 10
- cx2 = center_x + btn_w + 10
- c = (0.75, 0.25, 0.25, 0.95) if self._cancel_hover else (0.45, 0.20, 0.20, 0.85)
- _draw_rect_2d(cx1, btn_y1, cx2, btn_y2, c)
- self._cancel_rect = (cx1, btn_y1, cx2, btn_y2)
- blf.size(0, 15)
- ct = "Cancel"
- ctw, cth = blf.dimensions(0, ct)
- blf.position(0, (cx1 + cx2 - ctw) / 2, (btn_y1 + btn_y2 - cth) / 2, 0)
- blf.color(0, 1.0, 1.0, 1.0, 1.0)
- blf.draw(0, ct)
-
-
-class Trellis2Generate(bpy.types.Operator):
- """Generate a 3D mesh from a reference image using TRELLIS.2 via ComfyUI.
-
- Requires the PozzettiAndrea/ComfyUI-TRELLIS2 custom node pack installed on the ComfyUI server.
- Uploads the input image, runs the full TRELLIS.2 pipeline (background removal, conditioning,
- shape generation, texture generation, GLB export), downloads the resulting GLB file, and
- imports it into the Blender scene."""
- bl_idname = "object.trellis2_generate"
- bl_label = "Generate 3D Mesh (TRELLIS.2)"
- bl_options = {'REGISTER', 'UNDO'}
-
- _timer = None
- _thread = None
- _error = None
- _glb_data = None
- _is_running = False
- _cancelled = False
- _active_ws = None # WebSocket reference for cancel-time close
- _progress = 0.0
- _stage = "Initializing"
- workflow_manager: object = None
-
- # ── Preview gallery state ─────────────────────────────────────────
- _gallery_overlay: _PreviewGalleryOverlay | None = None
- _gallery_event: threading.Event | None = None
- _gallery_ready: bool = False
- _gallery_action: str | None = None # 'select' | 'more' | 'cancel'
- _gallery_selected_bytes: bytes | None = None
- _gallery_selected_seed: int | None = None
- _progress_remap: tuple | None = None # (base, span) for gallery sub-range scaling
-
- # ── 3-tier progress ──────────────────────────────────────────────
- _overall_progress: float = 0.0
- _overall_stage: str = "Initializing"
- _phase_progress: float = 0.0
- _phase_stage: str = ""
- _detail_progress: float = 0.0
- _detail_stage: str = ""
- _current_phase: int = 0
- _total_phases: int = 3 # 2 when gen_from == 'image'
-
- def _update_overall(self):
- """Recompute *_overall_progress* from current phase + phase progress."""
- layout = getattr(self, '_phase_layout', '')
- if layout == 'txt2img+trellis+texturing': # 3 phases
- starts = {1: 0, 2: 15, 3: 65}
- weights = {1: 15, 2: 50, 3: 35}
- elif layout == 'trellis+texturing': # 2 phases: big mesh, then texturing
- starts = {1: 0, 2: 65}
- weights = {1: 65, 2: 35}
- elif layout == 'txt2img+trellis': # 2 phases: quick txt2img, then big mesh+native tex
- starts = {1: 0, 2: 15}
- weights = {1: 15, 2: 85}
- else: # Single phase — scale to full 0-100
- starts = {1: 0}
- weights = {1: 100}
- s = starts.get(self._current_phase, 0)
- w = weights.get(self._current_phase, 0)
- self._overall_progress = s + (self._phase_progress / 100.0) * w
- self._overall_progress = max(0.0, min(self._overall_progress, 100.0))
- # Keep legacy _progress in sync for any code that reads it
- self._progress = self._overall_progress
-
- @classmethod
- def poll(cls, context):
- if cls._is_running:
- return True # Allow cancellation
- addon_prefs = context.preferences.addons[__package__].preferences
- if not addon_prefs.server_address or not addon_prefs.server_online:
- cls.poll_message_set("ComfyUI server is not connected")
- return False
- if not os.path.exists(addon_prefs.output_dir):
- cls.poll_message_set("Output directory not set or does not exist (check addon preferences)")
- return False
- if bpy.app.online_access == False:
- cls.poll_message_set("Blender's online access is disabled (File → Preferences → System)")
- return False
- gen_from = getattr(context.scene, 'trellis2_generate_from', 'image')
- if gen_from == 'image' and not context.scene.trellis2_input_image:
- cls.poll_message_set("No input image selected for TRELLIS.2 generation")
- return False
- if sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- return True
-
- def execute(self, context):
- if Trellis2Generate._is_running:
- # Cancel — tell the server to stop and close the WebSocket
- # so the background thread unblocks from ws.recv().
- Trellis2Generate._cancelled = True
- Trellis2Generate._is_running = False
-
- # Send /interrupt to ComfyUI (same as standard texturing cancel)
- try:
- server_address = context.preferences.addons[__package__].preferences.server_address
- data = json.dumps({"client_id": str(uuid.uuid4())}).encode('utf-8')
- req = urllib.request.Request("http://{}/interrupt".format(server_address), data=data)
- urllib.request.urlopen(req)
- except Exception:
- pass # Best effort — server may already be gone
-
- # Close the active WebSocket so the thread's ws.recv() raises
- ws = Trellis2Generate._active_ws
- if ws:
- try:
- ws.close()
- except Exception:
- pass
- Trellis2Generate._active_ws = None
-
- # Wake up the gallery event in case the thread is blocked there
- if self._gallery_event:
- self._gallery_action = 'cancel'
- self._gallery_event.set()
-
- self.report({'WARNING'}, "TRELLIS.2 generation cancelled")
- return {'FINISHED'}
-
- scene = context.scene
- gen_from = getattr(scene, 'trellis2_generate_from', 'image')
- tex_mode = getattr(scene, 'trellis2_texture_mode', 'native')
-
- # Validate input image (only required in image mode)
- image_path = None
- if gen_from == 'image':
- image_path = bpy.path.abspath(scene.trellis2_input_image)
- if not os.path.exists(image_path):
- self.report({'ERROR'}, f"Input image not found: {image_path}")
- return {'CANCELLED'}
-
- Trellis2Generate._is_running = True
- context.scene.sg_last_gen_error = False
- self._error = None
- self._glb_data = None
- self._progress = 0.0
- self._stage = "Initializing"
- self._texture_mode = tex_mode
- self.workflow_manager = WorkflowManager(self)
-
- # Gallery state reset
- self._gallery_overlay = None
- self._gallery_event = threading.Event()
- self._gallery_ready = False
- self._gallery_action = None
- self._gallery_selected_bytes = None
- self._gallery_selected_seed = None
-
- # 3-tier progress init
- has_txt2img = (gen_from == 'prompt')
- has_texturing = (tex_mode in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein'))
- if has_txt2img and has_texturing:
- self._total_phases = 3
- self._phase_layout = 'txt2img+trellis+texturing'
- elif has_txt2img:
- self._total_phases = 2
- self._phase_layout = 'txt2img+trellis'
- elif has_texturing:
- self._total_phases = 2
- self._phase_layout = 'trellis+texturing'
- else:
- self._total_phases = 1
- self._phase_layout = 'trellis_only'
- self._current_phase = 0
- self._overall_progress = 0.0
- self._overall_stage = "Initializing"
- self._phase_progress = 0.0
- self._phase_stage = ""
- self._detail_progress = 0.0
- self._detail_stage = ""
-
- # Compute revision directory on the main thread (may write output_timestamp)
- from .utils import get_generation_dirs
- gen_dirs = get_generation_dirs(context)
- revision_dir = gen_dirs.get("revision", "")
-
- # Start generation in background thread
- self._thread = threading.Thread(
- target=self._run_trellis2,
- args=(context, image_path, gen_from, revision_dir),
- daemon=True
- )
- self._thread.start()
-
- # Register modal timer
- context.window_manager.modal_handler_add(self)
- self._timer = context.window_manager.event_timer_add(0.5, window=context.window)
-
- return {'RUNNING_MODAL'}
-
- def _cleanup_gallery(self):
- """Remove the gallery overlay and free GPU resources."""
- if self._gallery_overlay:
- self._gallery_overlay.cleanup()
- self._gallery_overlay = None
-
- def modal(self, context, event):
- # ── Gallery mode: intercept mouse + keyboard ──────────────
- if self._gallery_overlay is not None:
- if event.type == 'MOUSEMOVE':
- if self._gallery_overlay.handle_mouse_move(
- event.mouse_region_x, event.mouse_region_y):
- for area in context.screen.areas:
- area.tag_redraw()
- return {'RUNNING_MODAL'}
-
- if event.type == 'LEFTMOUSE' and event.value == 'PRESS':
- action = self._gallery_overlay.handle_click(
- event.mouse_region_x, event.mouse_region_y)
- if action == 'select':
- self._gallery_selected_bytes = self._gallery_overlay.selected_image_bytes
- self._gallery_selected_seed = self._gallery_overlay.selected_seed
- self._gallery_action = 'select'
- self._cleanup_gallery()
- self._gallery_ready = False
- self._gallery_event.set()
- return {'RUNNING_MODAL'}
- elif action == 'more':
- self._gallery_action = 'more'
- self._cleanup_gallery()
- self._gallery_ready = False
- self._gallery_event.set()
- return {'RUNNING_MODAL'}
- elif action == 'cancel':
- self._gallery_action = 'cancel'
- self._cleanup_gallery()
- self._gallery_ready = False
- self._gallery_event.set()
- return {'RUNNING_MODAL'}
- return {'RUNNING_MODAL'}
-
- if event.type == 'ESC' and event.value == 'PRESS':
- self._gallery_action = 'cancel'
- self._cleanup_gallery()
- self._gallery_ready = False
- self._gallery_event.set()
- return {'RUNNING_MODAL'}
-
- if event.type == 'TIMER':
- for area in context.screen.areas:
- area.tag_redraw()
- return {'RUNNING_MODAL'}
-
- # ── Normal mode ───────────────────────────────────────────
- if event.type != 'TIMER':
- return {'PASS_THROUGH'}
-
- # Redraw UI for progress updates
- for area in context.screen.areas:
- area.tag_redraw()
-
- # Check if gallery is ready (thread waiting for user input)
- if self._gallery_ready and self._gallery_overlay is None:
- gallery_data = getattr(self, '_gallery_data', None)
- if gallery_data:
- pil_imgs, seeds = gallery_data
- self._gallery_overlay = _PreviewGalleryOverlay(pil_imgs, seeds)
- for area in context.screen.areas:
- area.tag_redraw()
- return {'RUNNING_MODAL'}
-
- # Check if thread is still running
- if self._thread and self._thread.is_alive():
- return {'RUNNING_MODAL'}
-
- # Thread finished - clean up timer
- context.window_manager.event_timer_remove(self._timer)
- self._timer = None
- was_cancelled = Trellis2Generate._cancelled
- Trellis2Generate._is_running = False
- Trellis2Generate._cancelled = False
- Trellis2Generate._active_ws = None
- self._cleanup_gallery()
-
- # User cancelled — exit silently (no error toast)
- if was_cancelled:
- context.scene.generation_status = 'idle'
- context.scene.sg_last_gen_error = True
- return {'FINISHED'}
-
- if self._error:
- self.report({'ERROR'}, f"TRELLIS.2 error: {self._error}")
- context.scene.sg_last_gen_error = True
- return {'CANCELLED'}
-
- if self._glb_data is None or (isinstance(self._glb_data, dict) and "error" in self._glb_data):
- error_msg = self._glb_data.get("error", "Unknown error") if isinstance(self._glb_data, dict) else "No data received"
- self.report({'ERROR'}, f"TRELLIS.2 failed: {error_msg}")
- context.scene.sg_last_gen_error = True
- return {'CANCELLED'}
-
- # Surface mesh-corruption warning to the user (set by workflows.py
- # when the GLB validator detects artifacts but recovery failed).
- _mesh_warning = getattr(self, '_warning', None)
- if _mesh_warning:
- self.report({'WARNING'}, _mesh_warning)
- self._warning = None # consumed
-
- # Save GLB to revision directory and import into Blender
- try:
- from .utils import get_generation_dirs
- gen_dirs = get_generation_dirs(context)
- save_dir = gen_dirs.get("revision", "")
- if not save_dir:
- save_dir = context.preferences.addons[__package__].preferences.output_dir
- os.makedirs(save_dir, exist_ok=True)
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
- glb_filename = f"trellis2_{timestamp}.glb"
- glb_path = os.path.join(save_dir, glb_filename)
-
- with open(glb_path, 'wb') as f:
- f.write(self._glb_data)
-
- # Store the TRELLIS.2 input image path for downstream use (IPAdapter/Qwen style)
- input_img = getattr(self, '_input_image_path', None)
- if input_img:
- context.scene.trellis2_last_input_image = input_img
-
- print(f"[TRELLIS2] Saved GLB to: {glb_path} ({len(self._glb_data)} bytes)")
-
- # Import GLB into Blender
- bpy.ops.import_scene.gltf(filepath=glb_path)
-
- # --- Normalise imported mesh to a reasonable Blender-unit size ---
- target_bu = getattr(context.scene, 'trellis2_import_scale', 2.0)
- if target_bu > 0:
- imported_objects = [obj for obj in context.selected_objects]
- if imported_objects:
- # Compute combined world-space bounding box across all
- # imported objects (meshes, empties, armatures …).
- all_corners = []
- for obj in imported_objects:
- for corner in obj.bound_box:
- all_corners.append(obj.matrix_world @ mathutils.Vector(corner))
- if all_corners:
- xs = [c.x for c in all_corners]
- ys = [c.y for c in all_corners]
- zs = [c.z for c in all_corners]
- extent = max(
- max(xs) - min(xs),
- max(ys) - min(ys),
- max(zs) - min(zs),
- )
- if extent > 1e-6:
- scale_factor = target_bu / extent
- # Find the root objects (those without an imported parent)
- roots = [o for o in imported_objects if o.parent not in imported_objects]
- for root in roots:
- root.scale *= scale_factor
- # Apply scale so downstream code sees unit scale
- bpy.ops.object.select_all(action='DESELECT')
- for obj in imported_objects:
- obj.select_set(True)
- bpy.context.view_layer.objects.active = imported_objects[0]
- bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
- print(f"[TRELLIS2] Scaled mesh to {target_bu} BU (factor {scale_factor:.4f})")
-
- # --- Apply shading mode to imported meshes ---
- _shade_mode = getattr(context.scene, 'trellis2_shade_mode', 'flat')
- if _shade_mode == 'smooth':
- bpy.ops.object.shade_smooth()
- elif _shade_mode == 'auto_smooth':
- bpy.ops.object.shade_auto_smooth()
-
- # --- Optional studio lighting for native PBR textures ---
- tex_mode = getattr(self, '_texture_mode', 'native')
- if tex_mode == 'native' and getattr(context.scene, 'trellis2_auto_lighting', False):
- self._setup_studio_lighting(context, target_bu)
-
- # --- Phase 3: If diffusion texturing, auto-place cameras + start generation ---
- if tex_mode in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein'):
- # Place cameras NOW (while operator context is still valid)
- camera_count = getattr(context.scene, 'trellis2_camera_count', 8)
- imported_objects = [obj for obj in context.selected_objects]
-
- if imported_objects:
- bpy.context.view_layer.objects.active = imported_objects[0]
- bpy.ops.object.select_all(action='DESELECT')
- for obj in imported_objects:
- obj.select_set(True)
-
- # Force viewport to standard front view so AddCameras uses a
- # consistent reference direction for sorting and auto-prompts.
- # TRELLIS.2 always imports meshes in standard orientation so the
- # viewport should match.
- # Find the 3D viewport area + WINDOW region so add_cameras
- # gets a full context (region_data etc.) even when invoked
- # from a timer-driven modal callback.
- _v3d_area = None
- _v3d_region = None
- for area in context.screen.areas:
- if area.type == 'VIEW_3D':
- for space in area.spaces:
- if space.type == 'VIEW_3D':
- rv3d = space.region_3d
- if rv3d:
- # Blender front view (Numpad 1): -Y looking at +Y
- rv3d.view_rotation = mathutils.Quaternion(
- (0.7071068, 0.7071068, 0.0, 0.0)
- )
- rv3d.view_perspective = 'PERSP'
- _v3d_area = area
- for reg in area.regions:
- if reg.type == 'WINDOW':
- _v3d_region = reg
- break
- break
-
- try:
- _pm = getattr(context.scene, 'trellis2_placement_mode', 'normal_weighted')
- _cam_kwargs = {
- 'placement_mode': _pm,
- 'num_cameras': camera_count,
- 'auto_prompts': getattr(context.scene, 'trellis2_auto_prompts', True),
- 'review_placement': False,
- 'purge_others': True,
- 'exclude_bottom': getattr(context.scene, 'trellis2_exclude_bottom', True),
- 'exclude_bottom_angle': getattr(context.scene, 'trellis2_exclude_bottom_angle', 1.5533),
- 'auto_aspect': getattr(context.scene, 'trellis2_auto_aspect', 'per_camera'),
- 'occlusion_mode': getattr(context.scene, 'trellis2_occlusion_mode', 'none'),
- 'consider_existing': getattr(context.scene, 'trellis2_consider_existing', True),
- 'clamp_elevation': getattr(context.scene, 'trellis2_clamp_elevation', False),
- 'max_elevation_angle': getattr(context.scene, 'trellis2_max_elevation', 1.2217),
- 'min_elevation_angle': getattr(context.scene, 'trellis2_min_elevation', -0.1745),
- }
- if _pm == 'greedy_coverage':
- _cam_kwargs['coverage_target'] = getattr(context.scene, 'trellis2_coverage_target', 0.95)
- _cam_kwargs['max_auto_cameras'] = getattr(context.scene, 'trellis2_max_auto_cameras', 12)
- if _pm == 'fan_from_camera':
- _cam_kwargs['fan_angle'] = getattr(context.scene, 'trellis2_fan_angle', 90.0)
-
- # Use temp_override so add_cameras gets proper region_data.
- # Temporarily bypass the sg_modal_active() poll guard so
- # add_cameras can run while this TRELLIS.2 modal is active.
- from . import utils as _sg_utils
- _sg_utils._sg_bypass_modal_check = True
- try:
- if _v3d_area and _v3d_region:
- with bpy.context.temp_override(area=_v3d_area, region=_v3d_region):
- bpy.ops.object.add_cameras(**_cam_kwargs)
- else:
- bpy.ops.object.add_cameras(**_cam_kwargs)
- finally:
- _sg_utils._sg_bypass_modal_check = False
-
- except Exception as cam_err:
- print(f"[TRELLIS2] Warning: Camera placement failed: {cam_err}")
- traceback.print_exc()
-
- # Defer texture generation so Blender digests the new cameras
- self._schedule_texture_generation(context)
- self.report({'INFO'}, f"TRELLIS.2: Mesh imported. Camera placement done, texture generation starting...")
- else:
- self.report({'INFO'}, f"TRELLIS.2: Imported 3D mesh from {glb_filename}")
-
- return {'FINISHED'}
-
- except Exception as e:
- self.report({'ERROR'}, f"Failed to import GLB: {e}")
- traceback.print_exc()
- return {'CANCELLED'}
-
- # -----------------------------------------------------------------
- # Studio lighting (three-point rig for PBR showcase)
- # -----------------------------------------------------------------
- def _setup_studio_lighting(self, context, import_scale):
- """Create a three-point studio lighting setup around the imported mesh."""
- return setup_studio_lighting(context, scale=import_scale)
-
- def _schedule_texture_generation(self, context):
- """Defer texture generation via a timer so Blender can digest the new cameras.
-
- Camera placement has already happened in ``modal()``. This only
- selects all cameras and starts ``object.test_stable``.
- Sets scene-level pipeline flags so the UI can show the overall
- progress bar on top of the ComfyUIGenerate bars.
- """
- # Compute the overall-% at which texturing begins
- if self._total_phases == 3:
- phase_start = 65.0
- elif self._total_phases == 2:
- phase_start = 65.0
- else:
- phase_start = 0.0
-
- scene = context.scene
- scene.trellis2_pipeline_active = True
- scene.trellis2_pipeline_phase_start_pct = phase_start
- scene.trellis2_pipeline_total_phases = self._total_phases
-
- def _deferred_generate():
- try:
- # Defensive VRAM flush before loading the diffusion checkpoint.
- # The TRELLIS post-generation flush should have freed VRAM,
- # but if it silently failed the models are still resident (Gap C).
- # Also clear history to release cached node outputs.
- try:
- srv = bpy.context.preferences.addons[__package__].preferences.server_address
- # 1. Set unload flags
- flush_data = json.dumps({"unload_models": True, "free_memory": True}).encode('utf-8')
- flush_req = urllib.request.Request(
- f"http://{srv}/free", data=flush_data,
- headers={"Content-Type": "application/json"}
- )
- urllib.request.urlopen(flush_req, timeout=get_timeout('api'))
- # 2. Clear history/cache
- hist_data = json.dumps({"clear": True}).encode('utf-8')
- hist_req = urllib.request.Request(
- f"http://{srv}/history", data=hist_data,
- headers={"Content-Type": "application/json"}
- )
- urllib.request.urlopen(hist_req, timeout=get_timeout('api'))
- # 3. Wait for VRAM release
- import time
- time.sleep(3)
- print("[TRELLIS2] Pre-texturing VRAM flush sent (unload+history clear)")
- except Exception as flush_err:
- print(f"[TRELLIS2] Pre-texturing flush warning: {flush_err}")
-
- bpy.ops.object.select_all(action='DESELECT')
- scene_cams = [obj for obj in bpy.context.scene.objects if obj.type == 'CAMERA']
- for obj in scene_cams:
- obj.select_set(True)
-
- from . import utils as _sg_utils
- _sg_utils._sg_bypass_modal_check = True
- try:
- bpy.ops.object.test_stable('INVOKE_DEFAULT')
- finally:
- _sg_utils._sg_bypass_modal_check = False
- print("[TRELLIS2] Texture generation started")
- except Exception as e:
- print(f"[TRELLIS2] Warning: Texture generation failed to start: {e}")
- traceback.print_exc()
- return None # Run once
-
- # Remember which cameras exist before texturing so we can delete
- # the ones we placed if the user opted in.
- _pre_tex_cameras = {obj.name for obj in bpy.context.scene.objects if obj.type == 'CAMERA'}
-
- def _pipeline_watcher():
- """Clear the pipeline flag when texturing finishes or is cancelled."""
- if bpy.context.scene.generation_status in ('idle', 'waiting'):
- bpy.context.scene.trellis2_pipeline_active = False
- print("[TRELLIS2] Pipeline complete — overall bar removed")
-
- # Delete auto-placed cameras if the user requested it
- if getattr(bpy.context.scene, 'trellis2_delete_cameras', False):
- to_remove = [obj for obj in bpy.context.scene.objects
- if obj.type == 'CAMERA' and obj.name in _pre_tex_cameras]
- if to_remove:
- bpy.ops.object.select_all(action='DESELECT')
- for obj in to_remove:
- obj.select_set(True)
- bpy.ops.object.delete()
- print(f"[TRELLIS2] Deleted {len(to_remove)} auto-placed cameras")
-
- return None # Stop timer
- return 1.0 # Check again in 1s
-
- bpy.app.timers.register(_deferred_generate, first_interval=0.5)
- bpy.app.timers.register(_pipeline_watcher, first_interval=2.0)
-
- def _run_trellis2(self, context, image_path, gen_from, revision_dir):
- """Background thread: runs the TRELLIS.2 pipeline.
-
- If *gen_from* is ``'prompt'`` the method first generates an input
- image via a lightweight txt2img ComfyUI workflow, saves it to the
- revision directory and passes that to the TRELLIS.2 mesh workflow.
-
- When the preview gallery is enabled (``trellis2_preview_gallery_enabled``),
- the prompt path generates N images with different seeds and pauses to
- let the user pick one via the viewport overlay before continuing.
- """
- import random as _rng
- try:
- # --- Phase 1: Image acquisition ---
- if gen_from == 'prompt':
- self._current_phase = 1
- self._phase_stage = "Generating Input Image"
- self._phase_progress = 0
- self._detail_progress = 0
- self._detail_stage = "Flushing stale models"
- self._overall_stage = f"Phase 1/{self._total_phases}: Input Image"
- self._update_overall()
-
- # Flush any stale models from prior runs before loading a
- # diffusion checkpoint for txt2img (Gap A).
- try:
- server_addr = context.preferences.addons[__package__].preferences.server_address
- self.workflow_manager._flush_comfyui_vram(server_addr, label="Pre-txt2img")
- except Exception:
- pass
-
- self._detail_stage = "Starting txt2img"
-
- gallery_enabled = getattr(context.scene, 'trellis2_preview_gallery_enabled', False)
- gallery_count = max(1, int(getattr(context.scene, 'trellis2_preview_gallery_count', 4)))
-
- if gallery_enabled and gallery_count >= 1:
- # ── Preview gallery loop ──────────────────────────
- img_result = None # will hold the chosen image bytes
-
- # Seed a local RNG for deterministic gallery sequences.
- # Same scene seed ➜ same gallery images every run.
- base_seed = int(getattr(context.scene, 'seed', 0))
- if base_seed == 0:
- gallery_rng = _rng.Random() # truly random
- else:
- gallery_rng = _rng.Random(base_seed) # deterministic
-
- while True:
- pil_images = []
- seeds = []
- # Reset progress for each batch
- self._phase_progress = 0
- self._update_overall()
- for i in range(gallery_count):
- self._detail_stage = f"Generating preview {i + 1}/{gallery_count}"
- # Set up remapping so WebSocket progress (0-100 per image)
- # maps to the correct slice of the overall phase bar.
- base = (i / gallery_count) * 90
- span = (1 / gallery_count) * 90
- self._progress_remap = (base, span)
- self._phase_progress = base
- self._update_overall()
-
- rand_seed = gallery_rng.randint(1, 2**31 - 1)
- result = self.workflow_manager.generate_txt2img(
- context, seed_override=rand_seed)
- if isinstance(result, dict) and "error" in result:
- self._error = f"txt2img failed (seed {rand_seed}): {result['error']}"
- return
-
- pil_img = Image.open(io.BytesIO(result))
- pil_images.append(pil_img)
- seeds.append(rand_seed)
-
- # Clear remapping before waiting
- self._progress_remap = None
-
- # Hand off to the main thread for user selection
- self._gallery_data = (pil_images, seeds)
- self._gallery_ready = True
- self._detail_stage = "Waiting for selection"
- self._phase_progress = 95
- self._update_overall()
-
- # Block until the modal sets the event
- self._gallery_event.wait()
- self._gallery_event.clear()
-
- if self._gallery_action == 'select':
- img_result = self._gallery_selected_bytes
- chosen_seed = self._gallery_selected_seed
- if chosen_seed is not None:
- context.scene.seed = chosen_seed
- break
- elif self._gallery_action == 'more':
- # Loop around and generate another batch
- continue
- else: # cancel
- self._error = "Preview gallery cancelled"
- return
-
- if img_result is None:
- self._error = "No image selected from gallery"
- return
- else:
- # ── Single image (legacy path) ───────────────────
- img_result = self.workflow_manager.generate_txt2img(context)
- if isinstance(img_result, dict) and "error" in img_result:
- self._error = f"txt2img failed: {img_result['error']}"
- return
-
- # Phase 1 complete
- self._phase_progress = 100
- self._update_overall()
-
- # Early exit if cancelled during txt2img
- if self._cancelled:
- return
-
- # Save the generated image bytes to the revision directory
- save_dir = revision_dir if revision_dir else (
- context.preferences.addons[__package__].preferences.output_dir
- )
- os.makedirs(save_dir, exist_ok=True)
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
- image_path = os.path.join(save_dir, f"trellis2_input_{timestamp}.png")
- with open(image_path, 'wb') as f:
- f.write(img_result)
- print(f"[TRELLIS2] Saved txt2img result to: {image_path}")
-
- # Flush VRAM so the txt2img model (SDXL/Flux) is evicted
- # before TRELLIS loads its own models via raw PyTorch.
- # Without this, both models coexist and OOM on <=16 GB GPUs.
- # (Gap B – between txt2img and TRELLIS Phase 1)
- self._detail_stage = "Flushing txt2img models"
- try:
- server_addr = context.preferences.addons[__package__].preferences.server_address
- self.workflow_manager._flush_comfyui_vram(server_addr, label="Post-txt2img")
- except Exception:
- pass
-
- # Early exit if cancelled during txt2img
- if self._cancelled:
- return
-
- # --- Phase 2 (or 1 if no txt2img): TRELLIS.2 mesh generation ---
- trellis_phase = 2 if gen_from == 'prompt' else 1
- self._current_phase = trellis_phase
- self._phase_stage = "TRELLIS.2 Mesh Generation"
- self._phase_progress = 0
- self._detail_progress = 0
- self._detail_stage = "Uploading image"
- self._overall_stage = f"Phase {trellis_phase}/{self._total_phases}: 3D Mesh"
- self._update_overall()
-
- # Store the final input image path for later use (IPAdapter/Qwen style)
- self._input_image_path = image_path
-
- result = self.workflow_manager.generate_trellis2(context, image_path)
-
- # Suppress error reporting when the user cancelled
- if self._cancelled:
- return
-
- if isinstance(result, dict) and "error" in result:
- self._error = result["error"]
- else:
- self._glb_data = result
-
- except Exception as e:
- if self._cancelled:
- return # Swallow exceptions caused by cancel-time WS close
- self._error = str(e)
- traceback.print_exc()
diff --git a/stablegen/mesh_gen/__init__.py b/stablegen/mesh_gen/__init__.py
new file mode 100644
index 0000000..cde2428
--- /dev/null
+++ b/stablegen/mesh_gen/__init__.py
@@ -0,0 +1,3 @@
+"""mesh_gen – 3D mesh generation (TRELLIS.2)."""
+
+from .trellis2 import Trellis2Generate # noqa: F401
diff --git a/stablegen/mesh_gen/trellis2.py b/stablegen/mesh_gen/trellis2.py
new file mode 100644
index 0000000..2f2d057
--- /dev/null
+++ b/stablegen/mesh_gen/trellis2.py
@@ -0,0 +1,763 @@
+"""TRELLIS.2 mesh generation operator."""
+
+import os
+import bpy # pylint: disable=import-error
+import mathutils # pylint: disable=import-error
+import json
+import uuid
+import urllib.request
+import urllib.parse
+import threading
+import traceback
+from datetime import datetime
+import math
+import io
+import websocket
+from PIL import Image
+
+from ..utils import get_generation_dirs, sg_modal_active
+from ..timeout_config import get_timeout
+from .._generator_utils import setup_studio_lighting, redraw_ui, upload_image_to_comfyui
+from ..texturing.gallery import _PreviewGalleryOverlay
+
+_ADDON_PKG = __package__.rsplit('.', 1)[0]
+
+class Trellis2Generate(bpy.types.Operator):
+ """Generate a 3D mesh from a reference image using TRELLIS.2 via ComfyUI.
+
+ Requires the PozzettiAndrea/ComfyUI-TRELLIS2 custom node pack installed on the ComfyUI server.
+ Uploads the input image, runs the full TRELLIS.2 pipeline (background removal, conditioning,
+ shape generation, texture generation, GLB export), downloads the resulting GLB file, and
+ imports it into the Blender scene."""
+ bl_idname = "object.trellis2_generate"
+ bl_label = "Generate 3D Mesh (TRELLIS.2)"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ _timer = None
+ _thread = None
+ _error = None
+ _glb_data = None
+ _is_running = False
+ _cancelled = False
+ _active_ws = None # WebSocket reference for cancel-time close
+ _progress = 0.0
+ _stage = "Initializing"
+ workflow_manager: object = None
+
+ # ── Preview gallery state ─────────────────────────────────────────
+ _gallery_overlay: _PreviewGalleryOverlay | None = None
+ _gallery_event: threading.Event | None = None
+ _gallery_ready: bool = False
+ _gallery_action: str | None = None # 'select' | 'more' | 'cancel'
+ _gallery_selected_bytes: bytes | None = None
+ _gallery_selected_seed: int | None = None
+ _progress_remap: tuple | None = None # (base, span) for gallery sub-range scaling
+
+ # ── 3-tier progress ──────────────────────────────────────────────
+ _overall_progress: float = 0.0
+ _overall_stage: str = "Initializing"
+ _phase_progress: float = 0.0
+ _phase_stage: str = ""
+ _detail_progress: float = 0.0
+ _detail_stage: str = ""
+ _current_phase: int = 0
+ _total_phases: int = 3 # 2 when gen_from == 'image'
+
+ def _update_overall(self):
+ """Recompute *_overall_progress* from current phase + phase progress."""
+ layout = getattr(self, '_phase_layout', '')
+ if layout == 'txt2img+trellis+texturing': # 3 phases
+ starts = {1: 0, 2: 15, 3: 65}
+ weights = {1: 15, 2: 50, 3: 35}
+ elif layout == 'trellis+texturing': # 2 phases: big mesh, then texturing
+ starts = {1: 0, 2: 65}
+ weights = {1: 65, 2: 35}
+ elif layout == 'txt2img+trellis': # 2 phases: quick txt2img, then big mesh+native tex
+ starts = {1: 0, 2: 15}
+ weights = {1: 15, 2: 85}
+ else: # Single phase — scale to full 0-100
+ starts = {1: 0}
+ weights = {1: 100}
+ s = starts.get(self._current_phase, 0)
+ w = weights.get(self._current_phase, 0)
+ self._overall_progress = s + (self._phase_progress / 100.0) * w
+ self._overall_progress = max(0.0, min(self._overall_progress, 100.0))
+ # Keep legacy _progress in sync for any code that reads it
+ self._progress = self._overall_progress
+
+ @classmethod
+ def poll(cls, context):
+ if cls._is_running:
+ return True # Allow cancellation
+ addon_prefs = context.preferences.addons[_ADDON_PKG].preferences
+ if not addon_prefs.server_address or not addon_prefs.server_online:
+ cls.poll_message_set("ComfyUI server is not connected")
+ return False
+ if not os.path.exists(addon_prefs.output_dir):
+ cls.poll_message_set("Output directory not set or does not exist (check addon preferences)")
+ return False
+ if bpy.app.online_access == False:
+ cls.poll_message_set("Blender's online access is disabled (File → Preferences → System)")
+ return False
+ gen_from = getattr(context.scene, 'trellis2_generate_from', 'image')
+ if gen_from == 'image' and not context.scene.trellis2_input_image:
+ cls.poll_message_set("No input image selected for TRELLIS.2 generation")
+ return False
+ if sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ return True
+
+ def execute(self, context):
+ if Trellis2Generate._is_running:
+ # Cancel — tell the server to stop and close the WebSocket
+ # so the background thread unblocks from ws.recv().
+ Trellis2Generate._cancelled = True
+ Trellis2Generate._is_running = False
+
+ # Send /interrupt to ComfyUI (same as standard texturing cancel)
+ try:
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ data = json.dumps({"client_id": str(uuid.uuid4())}).encode('utf-8')
+ req = urllib.request.Request("http://{}/interrupt".format(server_address), data=data)
+ urllib.request.urlopen(req)
+ except Exception:
+ pass # Best effort — server may already be gone
+
+ # Close the active WebSocket so the thread's ws.recv() raises
+ ws = Trellis2Generate._active_ws
+ if ws:
+ try:
+ ws.close()
+ except Exception:
+ pass
+ Trellis2Generate._active_ws = None
+
+ # Wake up the gallery event in case the thread is blocked there
+ if self._gallery_event:
+ self._gallery_action = 'cancel'
+ self._gallery_event.set()
+
+ self.report({'WARNING'}, "TRELLIS.2 generation cancelled")
+ return {'FINISHED'}
+
+ scene = context.scene
+ gen_from = getattr(scene, 'trellis2_generate_from', 'image')
+ tex_mode = getattr(scene, 'trellis2_texture_mode', 'native')
+
+ # Validate input image (only required in image mode)
+ image_path = None
+ if gen_from == 'image':
+ image_path = bpy.path.abspath(scene.trellis2_input_image)
+ if not os.path.exists(image_path):
+ self.report({'ERROR'}, f"Input image not found: {image_path}")
+ return {'CANCELLED'}
+
+ Trellis2Generate._is_running = True
+ context.scene.sg_last_gen_error = False
+ self._error = None
+ self._glb_data = None
+ self._progress = 0.0
+ self._stage = "Initializing"
+ self._texture_mode = tex_mode
+ from ..workflows import WorkflowManager
+ self.workflow_manager = WorkflowManager(self)
+
+ # Gallery state reset
+ self._gallery_overlay = None
+ self._gallery_event = threading.Event()
+ self._gallery_ready = False
+ self._gallery_action = None
+ self._gallery_selected_bytes = None
+ self._gallery_selected_seed = None
+
+ # 3-tier progress init
+ has_txt2img = (gen_from == 'prompt')
+ has_texturing = (tex_mode in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein'))
+ if has_txt2img and has_texturing:
+ self._total_phases = 3
+ self._phase_layout = 'txt2img+trellis+texturing'
+ elif has_txt2img:
+ self._total_phases = 2
+ self._phase_layout = 'txt2img+trellis'
+ elif has_texturing:
+ self._total_phases = 2
+ self._phase_layout = 'trellis+texturing'
+ else:
+ self._total_phases = 1
+ self._phase_layout = 'trellis_only'
+ self._current_phase = 0
+ self._overall_progress = 0.0
+ self._overall_stage = "Initializing"
+ self._phase_progress = 0.0
+ self._phase_stage = ""
+ self._detail_progress = 0.0
+ self._detail_stage = ""
+
+ # Compute revision directory on the main thread (may write output_timestamp)
+ from ..utils import get_generation_dirs
+ gen_dirs = get_generation_dirs(context)
+ revision_dir = gen_dirs.get("revision", "")
+
+ # Start generation in background thread
+ self._thread = threading.Thread(
+ target=self._run_trellis2,
+ args=(context, image_path, gen_from, revision_dir),
+ daemon=True
+ )
+ self._thread.start()
+
+ # Register modal timer
+ context.window_manager.modal_handler_add(self)
+ self._timer = context.window_manager.event_timer_add(0.5, window=context.window)
+
+ return {'RUNNING_MODAL'}
+
+ def _cleanup_gallery(self):
+ """Remove the gallery overlay and free GPU resources."""
+ if self._gallery_overlay:
+ self._gallery_overlay.cleanup()
+ self._gallery_overlay = None
+
+ def modal(self, context, event):
+ # ── Gallery mode: intercept mouse + keyboard ──────────────
+ if self._gallery_overlay is not None:
+ if event.type == 'MOUSEMOVE':
+ if self._gallery_overlay.handle_mouse_move(
+ event.mouse_region_x, event.mouse_region_y):
+ for area in context.screen.areas:
+ area.tag_redraw()
+ return {'RUNNING_MODAL'}
+
+ if event.type == 'LEFTMOUSE' and event.value == 'PRESS':
+ action = self._gallery_overlay.handle_click(
+ event.mouse_region_x, event.mouse_region_y)
+ if action == 'select':
+ self._gallery_selected_bytes = self._gallery_overlay.selected_image_bytes
+ self._gallery_selected_seed = self._gallery_overlay.selected_seed
+ self._gallery_action = 'select'
+ self._cleanup_gallery()
+ self._gallery_ready = False
+ self._gallery_event.set()
+ return {'RUNNING_MODAL'}
+ elif action == 'more':
+ self._gallery_action = 'more'
+ self._cleanup_gallery()
+ self._gallery_ready = False
+ self._gallery_event.set()
+ return {'RUNNING_MODAL'}
+ elif action == 'cancel':
+ self._gallery_action = 'cancel'
+ self._cleanup_gallery()
+ self._gallery_ready = False
+ self._gallery_event.set()
+ return {'RUNNING_MODAL'}
+ return {'RUNNING_MODAL'}
+
+ if event.type == 'ESC' and event.value == 'PRESS':
+ self._gallery_action = 'cancel'
+ self._cleanup_gallery()
+ self._gallery_ready = False
+ self._gallery_event.set()
+ return {'RUNNING_MODAL'}
+
+ if event.type == 'TIMER':
+ for area in context.screen.areas:
+ area.tag_redraw()
+ return {'RUNNING_MODAL'}
+
+ # ── Normal mode ───────────────────────────────────────────
+ if event.type != 'TIMER':
+ return {'PASS_THROUGH'}
+
+ # Redraw UI for progress updates
+ for area in context.screen.areas:
+ area.tag_redraw()
+
+ # Check if gallery is ready (thread waiting for user input)
+ if self._gallery_ready and self._gallery_overlay is None:
+ gallery_data = getattr(self, '_gallery_data', None)
+ if gallery_data:
+ pil_imgs, seeds = gallery_data
+ self._gallery_overlay = _PreviewGalleryOverlay(pil_imgs, seeds)
+ for area in context.screen.areas:
+ area.tag_redraw()
+ return {'RUNNING_MODAL'}
+
+ # Check if thread is still running
+ if self._thread and self._thread.is_alive():
+ return {'RUNNING_MODAL'}
+
+ # Thread finished - clean up timer
+ context.window_manager.event_timer_remove(self._timer)
+ self._timer = None
+ was_cancelled = Trellis2Generate._cancelled
+ Trellis2Generate._is_running = False
+ Trellis2Generate._cancelled = False
+ Trellis2Generate._active_ws = None
+ self._cleanup_gallery()
+
+ # User cancelled — exit silently (no error toast)
+ if was_cancelled:
+ context.scene.generation_status = 'idle'
+ context.scene.sg_last_gen_error = True
+ return {'FINISHED'}
+
+ if self._error:
+ self.report({'ERROR'}, f"TRELLIS.2 error: {self._error}")
+ context.scene.sg_last_gen_error = True
+ return {'CANCELLED'}
+
+ if self._glb_data is None or (isinstance(self._glb_data, dict) and "error" in self._glb_data):
+ error_msg = self._glb_data.get("error", "Unknown error") if isinstance(self._glb_data, dict) else "No data received"
+ self.report({'ERROR'}, f"TRELLIS.2 failed: {error_msg}")
+ context.scene.sg_last_gen_error = True
+ return {'CANCELLED'}
+
+ # Surface mesh-corruption warning to the user (set by workflows.py
+ # when the GLB validator detects artifacts but recovery failed).
+ _mesh_warning = getattr(self, '_warning', None)
+ if _mesh_warning:
+ self.report({'WARNING'}, _mesh_warning)
+ self._warning = None # consumed
+
+ # Save GLB to revision directory and import into Blender
+ try:
+ from ..utils import get_generation_dirs
+ gen_dirs = get_generation_dirs(context)
+ save_dir = gen_dirs.get("revision", "")
+ if not save_dir:
+ save_dir = context.preferences.addons[_ADDON_PKG].preferences.output_dir
+ os.makedirs(save_dir, exist_ok=True)
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
+ glb_filename = f"trellis2_{timestamp}.glb"
+ glb_path = os.path.join(save_dir, glb_filename)
+
+ with open(glb_path, 'wb') as f:
+ f.write(self._glb_data)
+
+ # Store the TRELLIS.2 input image path for downstream use (IPAdapter/Qwen style)
+ input_img = getattr(self, '_input_image_path', None)
+ if input_img:
+ context.scene.trellis2_last_input_image = input_img
+
+ print(f"[TRELLIS2] Saved GLB to: {glb_path} ({len(self._glb_data)} bytes)")
+
+ # Import GLB into Blender
+ bpy.ops.import_scene.gltf(filepath=glb_path)
+
+ # --- Normalise imported mesh to a reasonable Blender-unit size ---
+ target_bu = getattr(context.scene, 'trellis2_import_scale', 2.0)
+ if target_bu > 0:
+ imported_objects = [obj for obj in context.selected_objects]
+ if imported_objects:
+ # Compute combined world-space bounding box across all
+ # imported objects (meshes, empties, armatures …).
+ all_corners = []
+ for obj in imported_objects:
+ for corner in obj.bound_box:
+ all_corners.append(obj.matrix_world @ mathutils.Vector(corner))
+ if all_corners:
+ xs = [c.x for c in all_corners]
+ ys = [c.y for c in all_corners]
+ zs = [c.z for c in all_corners]
+ extent = max(
+ max(xs) - min(xs),
+ max(ys) - min(ys),
+ max(zs) - min(zs),
+ )
+ if extent > 1e-6:
+ scale_factor = target_bu / extent
+ # Find the root objects (those without an imported parent)
+ roots = [o for o in imported_objects if o.parent not in imported_objects]
+ for root in roots:
+ root.scale *= scale_factor
+ # Apply scale so downstream code sees unit scale
+ bpy.ops.object.select_all(action='DESELECT')
+ for obj in imported_objects:
+ obj.select_set(True)
+ bpy.context.view_layer.objects.active = imported_objects[0]
+ bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
+ print(f"[TRELLIS2] Scaled mesh to {target_bu} BU (factor {scale_factor:.4f})")
+
+ # --- Apply shading mode to imported meshes ---
+ _shade_mode = getattr(context.scene, 'trellis2_shade_mode', 'flat')
+ if _shade_mode == 'smooth':
+ bpy.ops.object.shade_smooth()
+ elif _shade_mode == 'auto_smooth':
+ bpy.ops.object.shade_auto_smooth()
+
+ # --- Optional studio lighting for native PBR textures ---
+ tex_mode = getattr(self, '_texture_mode', 'native')
+ if tex_mode == 'native' and getattr(context.scene, 'trellis2_auto_lighting', False):
+ self._setup_studio_lighting(context, target_bu)
+
+ # --- Phase 3: If diffusion texturing, auto-place cameras + start generation ---
+ if tex_mode in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein'):
+ # Place cameras NOW (while operator context is still valid)
+ camera_count = getattr(context.scene, 'trellis2_camera_count', 8)
+ imported_objects = [obj for obj in context.selected_objects]
+
+ if imported_objects:
+ bpy.context.view_layer.objects.active = imported_objects[0]
+ bpy.ops.object.select_all(action='DESELECT')
+ for obj in imported_objects:
+ obj.select_set(True)
+
+ # Force viewport to standard front view so AddCameras uses a
+ # consistent reference direction for sorting and auto-prompts.
+ # TRELLIS.2 always imports meshes in standard orientation so the
+ # viewport should match.
+ # Find the 3D viewport area + WINDOW region so add_cameras
+ # gets a full context (region_data etc.) even when invoked
+ # from a timer-driven modal callback.
+ _v3d_area = None
+ _v3d_region = None
+ for area in context.screen.areas:
+ if area.type == 'VIEW_3D':
+ for space in area.spaces:
+ if space.type == 'VIEW_3D':
+ rv3d = space.region_3d
+ if rv3d:
+ # Blender front view (Numpad 1): -Y looking at +Y
+ rv3d.view_rotation = mathutils.Quaternion(
+ (0.7071068, 0.7071068, 0.0, 0.0)
+ )
+ rv3d.view_perspective = 'PERSP'
+ _v3d_area = area
+ for reg in area.regions:
+ if reg.type == 'WINDOW':
+ _v3d_region = reg
+ break
+ break
+
+ try:
+ _pm = getattr(context.scene, 'trellis2_placement_mode', 'normal_weighted')
+ _cam_kwargs = {
+ 'placement_mode': _pm,
+ 'num_cameras': camera_count,
+ 'auto_prompts': getattr(context.scene, 'trellis2_auto_prompts', True),
+ 'review_placement': False,
+ 'purge_others': True,
+ 'exclude_bottom': getattr(context.scene, 'trellis2_exclude_bottom', True),
+ 'exclude_bottom_angle': getattr(context.scene, 'trellis2_exclude_bottom_angle', 1.5533),
+ 'auto_aspect': getattr(context.scene, 'trellis2_auto_aspect', 'per_camera'),
+ 'occlusion_mode': getattr(context.scene, 'trellis2_occlusion_mode', 'none'),
+ 'consider_existing': getattr(context.scene, 'trellis2_consider_existing', True),
+ 'clamp_elevation': getattr(context.scene, 'trellis2_clamp_elevation', False),
+ 'max_elevation_angle': getattr(context.scene, 'trellis2_max_elevation', 1.2217),
+ 'min_elevation_angle': getattr(context.scene, 'trellis2_min_elevation', -0.1745),
+ }
+ if _pm == 'greedy_coverage':
+ _cam_kwargs['coverage_target'] = getattr(context.scene, 'trellis2_coverage_target', 0.95)
+ _cam_kwargs['max_auto_cameras'] = getattr(context.scene, 'trellis2_max_auto_cameras', 12)
+ if _pm == 'fan_from_camera':
+ _cam_kwargs['fan_angle'] = getattr(context.scene, 'trellis2_fan_angle', 90.0)
+
+ # Use temp_override so add_cameras gets proper region_data.
+ # Temporarily bypass the sg_modal_active() poll guard so
+ # add_cameras can run while this TRELLIS.2 modal is active.
+ from .. import utils as _sg_utils
+ _sg_utils._sg_bypass_modal_check = True
+ try:
+ if _v3d_area and _v3d_region:
+ with bpy.context.temp_override(area=_v3d_area, region=_v3d_region):
+ bpy.ops.object.add_cameras(**_cam_kwargs)
+ else:
+ bpy.ops.object.add_cameras(**_cam_kwargs)
+ finally:
+ _sg_utils._sg_bypass_modal_check = False
+
+ except Exception as cam_err:
+ print(f"[TRELLIS2] Warning: Camera placement failed: {cam_err}")
+ traceback.print_exc()
+
+ # Defer texture generation so Blender digests the new cameras
+ self._schedule_texture_generation(context)
+ self.report({'INFO'}, f"TRELLIS.2: Mesh imported. Camera placement done, texture generation starting...")
+ else:
+ self.report({'INFO'}, f"TRELLIS.2: Imported 3D mesh from {glb_filename}")
+
+ return {'FINISHED'}
+
+ except Exception as e:
+ self.report({'ERROR'}, f"Failed to import GLB: {e}")
+ traceback.print_exc()
+ return {'CANCELLED'}
+
+ # -----------------------------------------------------------------
+ # Studio lighting (three-point rig for PBR showcase)
+ # -----------------------------------------------------------------
+ def _setup_studio_lighting(self, context, import_scale):
+ """Create a three-point studio lighting setup around the imported mesh."""
+ return setup_studio_lighting(context, scale=import_scale)
+
+ def _schedule_texture_generation(self, context):
+ """Defer texture generation via a timer so Blender can digest the new cameras.
+
+ Camera placement has already happened in ``modal()``. This only
+ selects all cameras and starts ``object.test_stable``.
+ Sets scene-level pipeline flags so the UI can show the overall
+ progress bar on top of the ComfyUIGenerate bars.
+ """
+ # Compute the overall-% at which texturing begins
+ if self._total_phases == 3:
+ phase_start = 65.0
+ elif self._total_phases == 2:
+ phase_start = 65.0
+ else:
+ phase_start = 0.0
+
+ scene = context.scene
+ scene.trellis2_pipeline_active = True
+ scene.trellis2_pipeline_phase_start_pct = phase_start
+ scene.trellis2_pipeline_total_phases = self._total_phases
+
+ def _deferred_generate():
+ try:
+ # Defensive VRAM flush before loading the diffusion checkpoint.
+ # The TRELLIS post-generation flush should have freed VRAM,
+ # but if it silently failed the models are still resident (Gap C).
+ # Also clear history to release cached node outputs.
+ try:
+ srv = bpy.context.preferences.addons[_ADDON_PKG].preferences.server_address
+ # 1. Set unload flags
+ flush_data = json.dumps({"unload_models": True, "free_memory": True}).encode('utf-8')
+ flush_req = urllib.request.Request(
+ f"http://{srv}/free", data=flush_data,
+ headers={"Content-Type": "application/json"}
+ )
+ urllib.request.urlopen(flush_req, timeout=get_timeout('api'))
+ # 2. Clear history/cache
+ hist_data = json.dumps({"clear": True}).encode('utf-8')
+ hist_req = urllib.request.Request(
+ f"http://{srv}/history", data=hist_data,
+ headers={"Content-Type": "application/json"}
+ )
+ urllib.request.urlopen(hist_req, timeout=get_timeout('api'))
+ # 3. Wait for VRAM release
+ import time
+ time.sleep(3)
+ print("[TRELLIS2] Pre-texturing VRAM flush sent (unload+history clear)")
+ except Exception as flush_err:
+ print(f"[TRELLIS2] Pre-texturing flush warning: {flush_err}")
+
+ bpy.ops.object.select_all(action='DESELECT')
+ scene_cams = [obj for obj in bpy.context.scene.objects if obj.type == 'CAMERA']
+ for obj in scene_cams:
+ obj.select_set(True)
+
+ from .. import utils as _sg_utils
+ _sg_utils._sg_bypass_modal_check = True
+ try:
+ bpy.ops.object.test_stable('INVOKE_DEFAULT')
+ finally:
+ _sg_utils._sg_bypass_modal_check = False
+ print("[TRELLIS2] Texture generation started")
+ except Exception as e:
+ print(f"[TRELLIS2] Warning: Texture generation failed to start: {e}")
+ traceback.print_exc()
+ return None # Run once
+
+ # Remember which cameras exist before texturing so we can delete
+ # the ones we placed if the user opted in.
+ _pre_tex_cameras = {obj.name for obj in bpy.context.scene.objects if obj.type == 'CAMERA'}
+
+ def _pipeline_watcher():
+ """Clear the pipeline flag when texturing finishes or is cancelled."""
+ if bpy.context.scene.generation_status in ('idle', 'waiting'):
+ bpy.context.scene.trellis2_pipeline_active = False
+ print("[TRELLIS2] Pipeline complete — overall bar removed")
+
+ # Delete auto-placed cameras if the user requested it
+ if getattr(bpy.context.scene, 'trellis2_delete_cameras', False):
+ to_remove = [obj for obj in bpy.context.scene.objects
+ if obj.type == 'CAMERA' and obj.name in _pre_tex_cameras]
+ if to_remove:
+ bpy.ops.object.select_all(action='DESELECT')
+ for obj in to_remove:
+ obj.select_set(True)
+ bpy.ops.object.delete()
+ print(f"[TRELLIS2] Deleted {len(to_remove)} auto-placed cameras")
+
+ return None # Stop timer
+ return 1.0 # Check again in 1s
+
+ bpy.app.timers.register(_deferred_generate, first_interval=0.5)
+ bpy.app.timers.register(_pipeline_watcher, first_interval=2.0)
+
+ def _run_trellis2(self, context, image_path, gen_from, revision_dir):
+ """Background thread: runs the TRELLIS.2 pipeline.
+
+ If *gen_from* is ``'prompt'`` the method first generates an input
+ image via a lightweight txt2img ComfyUI workflow, saves it to the
+ revision directory and passes that to the TRELLIS.2 mesh workflow.
+
+ When the preview gallery is enabled (``trellis2_preview_gallery_enabled``),
+ the prompt path generates N images with different seeds and pauses to
+ let the user pick one via the viewport overlay before continuing.
+ """
+ import random as _rng
+ try:
+ # --- Phase 1: Image acquisition ---
+ if gen_from == 'prompt':
+ self._current_phase = 1
+ self._phase_stage = "Generating Input Image"
+ self._phase_progress = 0
+ self._detail_progress = 0
+ self._detail_stage = "Flushing stale models"
+ self._overall_stage = f"Phase 1/{self._total_phases}: Input Image"
+ self._update_overall()
+
+ # Flush any stale models from prior runs before loading a
+ # diffusion checkpoint for txt2img (Gap A).
+ try:
+ server_addr = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ self.workflow_manager._flush_comfyui_vram(server_addr, label="Pre-txt2img")
+ except Exception:
+ pass
+
+ self._detail_stage = "Starting txt2img"
+
+ gallery_enabled = getattr(context.scene, 'trellis2_preview_gallery_enabled', False)
+ gallery_count = max(1, int(getattr(context.scene, 'trellis2_preview_gallery_count', 4)))
+
+ if gallery_enabled and gallery_count >= 1:
+ # ── Preview gallery loop ──────────────────────────
+ img_result = None # will hold the chosen image bytes
+
+ # Seed a local RNG for deterministic gallery sequences.
+ # Same scene seed ➜ same gallery images every run.
+ base_seed = int(getattr(context.scene, 'seed', 0))
+ if base_seed == 0:
+ gallery_rng = _rng.Random() # truly random
+ else:
+ gallery_rng = _rng.Random(base_seed) # deterministic
+
+ while True:
+ pil_images = []
+ seeds = []
+ # Reset progress for each batch
+ self._phase_progress = 0
+ self._update_overall()
+ for i in range(gallery_count):
+ self._detail_stage = f"Generating preview {i + 1}/{gallery_count}"
+ # Set up remapping so WebSocket progress (0-100 per image)
+ # maps to the correct slice of the overall phase bar.
+ base = (i / gallery_count) * 90
+ span = (1 / gallery_count) * 90
+ self._progress_remap = (base, span)
+ self._phase_progress = base
+ self._update_overall()
+
+ rand_seed = gallery_rng.randint(1, 2**31 - 1)
+ result = self.workflow_manager.generate_txt2img(
+ context, seed_override=rand_seed)
+ if isinstance(result, dict) and "error" in result:
+ self._error = f"txt2img failed (seed {rand_seed}): {result['error']}"
+ return
+
+ pil_img = Image.open(io.BytesIO(result))
+ pil_images.append(pil_img)
+ seeds.append(rand_seed)
+
+ # Clear remapping before waiting
+ self._progress_remap = None
+
+ # Hand off to the main thread for user selection
+ self._gallery_data = (pil_images, seeds)
+ self._gallery_ready = True
+ self._detail_stage = "Waiting for selection"
+ self._phase_progress = 95
+ self._update_overall()
+
+ # Block until the modal sets the event
+ self._gallery_event.wait()
+ self._gallery_event.clear()
+
+ if self._gallery_action == 'select':
+ img_result = self._gallery_selected_bytes
+ chosen_seed = self._gallery_selected_seed
+ if chosen_seed is not None:
+ context.scene.seed = chosen_seed
+ break
+ elif self._gallery_action == 'more':
+ # Loop around and generate another batch
+ continue
+ else: # cancel
+ self._error = "Preview gallery cancelled"
+ return
+
+ if img_result is None:
+ self._error = "No image selected from gallery"
+ return
+ else:
+ # ── Single image (legacy path) ───────────────────
+ img_result = self.workflow_manager.generate_txt2img(context)
+ if isinstance(img_result, dict) and "error" in img_result:
+ self._error = f"txt2img failed: {img_result['error']}"
+ return
+
+ # Phase 1 complete
+ self._phase_progress = 100
+ self._update_overall()
+
+ # Early exit if cancelled during txt2img
+ if self._cancelled:
+ return
+
+ # Save the generated image bytes to the revision directory
+ save_dir = revision_dir if revision_dir else (
+ context.preferences.addons[_ADDON_PKG].preferences.output_dir
+ )
+ os.makedirs(save_dir, exist_ok=True)
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
+ image_path = os.path.join(save_dir, f"trellis2_input_{timestamp}.png")
+ with open(image_path, 'wb') as f:
+ f.write(img_result)
+ print(f"[TRELLIS2] Saved txt2img result to: {image_path}")
+
+ # Flush VRAM so the txt2img model (SDXL/Flux) is evicted
+ # before TRELLIS loads its own models via raw PyTorch.
+ # Without this, both models coexist and OOM on <=16 GB GPUs.
+ # (Gap B – between txt2img and TRELLIS Phase 1)
+ self._detail_stage = "Flushing txt2img models"
+ try:
+ server_addr = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ self.workflow_manager._flush_comfyui_vram(server_addr, label="Post-txt2img")
+ except Exception:
+ pass
+
+ # Early exit if cancelled during txt2img
+ if self._cancelled:
+ return
+
+ # --- Phase 2 (or 1 if no txt2img): TRELLIS.2 mesh generation ---
+ trellis_phase = 2 if gen_from == 'prompt' else 1
+ self._current_phase = trellis_phase
+ self._phase_stage = "TRELLIS.2 Mesh Generation"
+ self._phase_progress = 0
+ self._detail_progress = 0
+ self._detail_stage = "Uploading image"
+ self._overall_stage = f"Phase {trellis_phase}/{self._total_phases}: 3D Mesh"
+ self._update_overall()
+
+ # Store the final input image path for later use (IPAdapter/Qwen style)
+ self._input_image_path = image_path
+
+ result = self.workflow_manager.generate_trellis2(context, image_path)
+
+ # Suppress error reporting when the user cancelled
+ if self._cancelled:
+ return
+
+ if isinstance(result, dict) and "error" in result:
+ self._error = result["error"]
+ else:
+ self._glb_data = result
+
+ except Exception as e:
+ if self._cancelled:
+ return # Swallow exceptions caused by cancel-time WS close
+ self._error = str(e)
+ traceback.print_exc()
\ No newline at end of file
diff --git a/stablegen/mesh_gen/workflows.py b/stablegen/mesh_gen/workflows.py
new file mode 100644
index 0000000..799fd8f
--- /dev/null
+++ b/stablegen/mesh_gen/workflows.py
@@ -0,0 +1,634 @@
+import os
+import json
+import uuid
+import websocket
+import urllib.request
+import urllib.parse
+from datetime import datetime, timedelta
+
+from ..timeout_config import get_timeout
+from ..utils import get_generation_dirs
+from ..util.workflow_templates import prompt_text_trellis2, prompt_text_trellis2_shape_only
+
+from io import BytesIO
+import numpy as np
+from PIL import Image
+
+_ADDON_PKG = __package__.rsplit('.', 1)[0]
+
+
+class _Trellis2WorkflowMixin:
+ """TRELLIS.2 workflow methods for WorkflowManager."""
+
+ @staticmethod
+ def _cleanup_trellis2_temp_files():
+ """Remove stale TRELLIS2 IPC temp directories and voxelgrid cache.
+
+ The ComfyUI-TRELLIS2 custom node creates one temp directory
+ (``trellis2_<random>``) per worker subprocess via a module-level
+ global and reuses it for the entire ComfyUI session. Deleting
+ that directory while the worker is alive causes "Parent directory
+ does not exist" errors on the next generation.
+
+ Since only the most recently created directory can belong to the
+ current (running) worker, we delete all *older* ``trellis2_*``
+ directories entirely and only clean up the stale ``.pt`` tensor
+ files inside the newest one, leaving the directory itself intact.
+ """
+ import tempfile
+ import glob
+ import shutil
+
+ tmp_root = tempfile.gettempdir()
+
+ dirs = sorted(
+ (d for d in glob.glob(os.path.join(tmp_root, 'trellis2_*'))
+ if os.path.isdir(d)),
+ key=os.path.getctime,
+ )
+
+ if not dirs:
+ return
+
+ # Everything except the newest is from a dead worker — remove entirely.
+ for d in dirs[:-1]:
+ try:
+ shutil.rmtree(d)
+ except OSError:
+ pass
+
+ # Newest directory may be in use — only purge .pt files inside it.
+ newest = dirs[-1]
+ for f in os.listdir(newest):
+ if f.endswith('.pt'):
+ try:
+ os.remove(os.path.join(newest, f))
+ except OSError:
+ pass
+
+ # Voxelgrid cache (Windows: C:\tmp\trellis2_cache)
+ for cache_dir in ('/tmp/trellis2_cache', r'C:\tmp\trellis2_cache'):
+ if os.path.isdir(cache_dir):
+ try:
+ shutil.rmtree(cache_dir)
+ except OSError:
+ pass
+
+ @staticmethod
+ def _is_local_server(server_address):
+ """Return True if server_address points to localhost."""
+ host = server_address.split(':')[0].strip()
+ return host in ('127.0.0.1', 'localhost', '0.0.0.0', '::1', '')
+
+ def generate_trellis2(self, context, input_image_path):
+ """
+ Generates a 3D mesh using TRELLIS.2 via ComfyUI.
+
+ Uploads an input image, runs the TRELLIS.2 pipeline (background removal,
+ conditioning, shape generation, optionally texture generation, GLB export),
+ and downloads the resulting GLB file from the ComfyUI server.
+ VRAM is always flushed before AND after generation.
+
+ Args:
+ context: Blender context.
+ input_image_path: Local path to the input image file.
+
+ Returns:
+ bytes: GLB file binary data on success.
+ dict: {"error": "message"} on failure.
+ """
+ import time
+
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+
+ # Pre-generation flush: free any loaded diffusion/other models so
+ # TRELLIS.2 has maximum VRAM available.
+ print("[TRELLIS2] Pre-generation VRAM flush — freeing loaded models...")
+ self._flush_comfyui_vram(server_address, label="Pre-generation")
+
+ time.sleep(1) # Brief pause for CUDA memory to be released
+
+ # Verify the server is alive before proceeding
+ if not self._check_server_alive(server_address):
+ return {"error": "ComfyUI server is not responding. Please restart it and try again."}
+
+ try:
+ client_id = str(uuid.uuid4())
+ result = self._generate_trellis2_inner(
+ context, input_image_path, server_address, client_id)
+ return result
+ finally:
+ self._flush_comfyui_vram(server_address, label="Post-generation")
+ self._cleanup_trellis2_temp_files()
+
+ def _generate_trellis2_inner(self, context, input_image_path, server_address, client_id):
+ """Inner implementation of generate_trellis2, called within a try/finally VRAM flush."""
+ import urllib.parse
+
+ # Upload the input image to ComfyUI
+ from .._generator_utils import upload_image_to_comfyui
+ image_info = upload_image_to_comfyui(server_address, input_image_path)
+ if image_info is None:
+ self.operator._error = f"Failed to upload input image: {input_image_path}"
+ return {"error": self.operator._error}
+
+ scene = context.scene
+ skip_texture = scene.trellis2_skip_texture
+
+ # Load the appropriate workflow template
+ if skip_texture:
+ prompt = json.loads(prompt_text_trellis2_shape_only)
+ NODES = {
+ 'input_image': '1',
+ 'load_models': '2',
+ 'remove_bg': '3',
+ 'get_conditioning': '4',
+ 'image_to_shape': '5',
+ 'simplify': '6',
+ 'export_trimesh': '7',
+ }
+ export_node_key = 'export_trimesh'
+ else:
+ prompt = json.loads(prompt_text_trellis2)
+ NODES = {
+ 'input_image': '1',
+ 'load_models': '2',
+ 'remove_bg': '3',
+ 'get_conditioning': '4',
+ 'image_to_shape': '5',
+ 'shape_to_textured_mesh': '6',
+ 'export_glb': '7',
+ }
+ export_node_key = 'export_glb'
+
+ # Set input image
+ prompt[NODES['input_image']]["inputs"]["image"] = image_info['name']
+
+ # Configure model settings from scene properties
+ prompt[NODES['load_models']]["inputs"]["resolution"] = scene.trellis2_resolution
+ prompt[NODES['load_models']]["inputs"]["vram_mode"] = scene.trellis2_vram_mode
+ prompt[NODES['load_models']]["inputs"]["attn_backend"] = scene.trellis2_attn_backend
+
+ # Configure background removal (or bypass it)
+ skip_bg = getattr(scene, 'trellis2_bg_removal', 'auto') == 'skip'
+ if skip_bg:
+ # Remove the RemoveBackground node from the workflow.
+ # Wire GetConditioning to use LoadImage outputs directly:
+ # LoadImage[0] → image, LoadImage[1] → mask (alpha channel).
+ # If the input has no alpha, ComfyUI's LoadImage provides an
+ # all-white mask (= entire image is foreground).
+ remove_bg_node_id = NODES.pop('remove_bg')
+ del prompt[remove_bg_node_id]
+ prompt[NODES['get_conditioning']]["inputs"]["image"] = [NODES['input_image'], 0]
+ prompt[NODES['get_conditioning']]["inputs"]["mask"] = [NODES['input_image'], 1]
+ else:
+ prompt[NODES['remove_bg']]["inputs"]["low_vram"] = False
+
+ # Configure conditioning
+ prompt[NODES['get_conditioning']]["inputs"]["background_color"] = scene.trellis2_background_color
+ # Auto-determine whether 1024 conditioning is needed from resolution mode
+ prompt[NODES['get_conditioning']]["inputs"]["include_1024"] = scene.trellis2_resolution != '512'
+
+ # Configure shape generation
+ seed = scene.trellis2_seed
+ prompt[NODES['image_to_shape']]["inputs"]["seed"] = seed
+ prompt[NODES['image_to_shape']]["inputs"]["ss_guidance_strength"] = scene.trellis2_ss_guidance
+ prompt[NODES['image_to_shape']]["inputs"]["ss_sampling_steps"] = scene.trellis2_ss_steps
+ prompt[NODES['image_to_shape']]["inputs"]["shape_guidance_strength"] = scene.trellis2_shape_guidance
+ prompt[NODES['image_to_shape']]["inputs"]["shape_sampling_steps"] = scene.trellis2_shape_steps
+ prompt[NODES['image_to_shape']]["inputs"]["max_tokens"] = scene.trellis2_max_tokens
+
+ # Configure export with unique prefix for identification
+ unique_prefix = f"trellis2_{uuid.uuid4().hex[:8]}"
+
+ # When post-processing is disabled, maximize the decimation target
+ # and disable remeshing so the user gets the rawest possible mesh.
+ use_pp = getattr(scene, 'trellis2_post_processing_enabled', True)
+
+ if skip_texture:
+ # Configure simplify node
+ if use_pp:
+ prompt[NODES['simplify']]["inputs"]["target_face_count"] = scene.trellis2_decimation
+ prompt[NODES['simplify']]["inputs"]["remesh"] = scene.trellis2_remesh
+ else:
+ # Raw mesh — max faces, no remesh
+ prompt[NODES['simplify']]["inputs"]["target_face_count"] = 5000000
+ prompt[NODES['simplify']]["inputs"]["remesh"] = False
+ prompt[NODES['simplify']]["inputs"]["fill_holes"] = False
+ prompt[NODES['export_trimesh']]["inputs"]["filename_prefix"] = unique_prefix
+ else:
+ # Configure texture generation
+ prompt[NODES['shape_to_textured_mesh']]["inputs"]["seed"] = seed
+ prompt[NODES['shape_to_textured_mesh']]["inputs"]["tex_guidance_strength"] = scene.trellis2_tex_guidance
+ prompt[NODES['shape_to_textured_mesh']]["inputs"]["tex_sampling_steps"] = scene.trellis2_tex_steps
+
+ # Configure GLB export
+ if use_pp:
+ prompt[NODES['export_glb']]["inputs"]["decimation_target"] = scene.trellis2_decimation
+ prompt[NODES['export_glb']]["inputs"]["remesh"] = scene.trellis2_remesh
+ else:
+ prompt[NODES['export_glb']]["inputs"]["decimation_target"] = 5000000
+ prompt[NODES['export_glb']]["inputs"]["remesh"] = False
+ prompt[NODES['export_glb']]["inputs"]["texture_size"] = scene.trellis2_texture_size
+ prompt[NODES['export_glb']]["inputs"]["filename_prefix"] = unique_prefix
+
+ # Save prompt for debugging
+ revision_dir = get_generation_dirs(context).get("revision", "")
+ if revision_dir:
+ self._save_prompt_to_file(prompt, revision_dir)
+
+ # --- Two-phase VRAM management for textured path ---
+ # NOTE: Two-phase execution (shape first, flush, then texture) was
+ # removed because ComfyUI requires at least one OUTPUT_NODE per prompt
+ # and the shape-only subset has none. The TRELLIS pipeline stages
+ # handle VRAM management internally (unload_shape_pipeline before
+ # loading texture models), so a single full-prompt submission works.
+
+ # Connect WebSocket
+ ws = self._connect_to_websocket(server_address, client_id)
+ if ws is None:
+ return {"error": "conn_failed"}
+
+ # TRELLIS.2 simplification / post-processing can take several
+ # minutes without sending any WS messages. Use the user-
+ # configurable mesh generation timeout.
+ ws.settimeout(get_timeout('mesh_gen'))
+
+ # Let the operator close this WS on cancel
+ if hasattr(self.operator, '_active_ws'):
+ self.operator._active_ws = ws
+
+ prompt_id = None
+ try:
+ # Queue prompt
+ prompt_id = self._queue_prompt(prompt, client_id, server_address)
+
+ # Node-level progress (isolated subprocess doesn't emit within-node progress)
+ # Weights approximate actual time spent per node
+ if skip_texture:
+ NODE_PROGRESS = {
+ NODES['input_image']: 2,
+ NODES['load_models']: 3,
+ NODES['get_conditioning']: 25,
+ NODES['image_to_shape']: 80,
+ NODES['simplify']: 90,
+ NODES['export_trimesh']: 98,
+ }
+ if not skip_bg:
+ NODE_PROGRESS[NODES['remove_bg']] = 12
+ else:
+ # Full textured pipeline (single submission)
+ NODE_PROGRESS = {
+ NODES['input_image']: 2,
+ NODES['load_models']: 3,
+ NODES['get_conditioning']: 18,
+ NODES['image_to_shape']: 55,
+ NODES['shape_to_textured_mesh']: 85,
+ NODES['export_glb']: 98,
+ }
+ if not skip_bg:
+ NODE_PROGRESS[NODES['remove_bg']] = 10
+
+ # Wait for execution to complete via WebSocket
+ # Also capture 'executed' events which may contain the output path
+ export_node_id = NODES[export_node_key]
+ glb_output_path = None
+
+ # Friendly node labels for the progress bars
+ NODE_LABELS = {
+ 'input_image': 'Loading Image',
+ 'load_models': 'Loading Models',
+ 'remove_bg': 'Removing Background',
+ 'get_conditioning': 'Conditioning',
+ 'image_to_shape': 'Generating Shape',
+ 'shape_to_textured_mesh': 'Generating Texture',
+ 'simplify': 'Simplifying Mesh',
+ 'export_trimesh': 'Exporting Mesh',
+ 'export_glb': 'Exporting GLB',
+ }
+
+ # Build a lookup of known step counts so we can identify
+ # which sampling sub-phase a within-node progress event
+ # belongs to (the TRELLIS nodes may emit separate runs of
+ # progress events for SS, shape, and texture sampling).
+ _ss_steps = scene.trellis2_ss_steps
+ _shape_steps = scene.trellis2_shape_steps
+ _tex_steps = getattr(scene, 'trellis2_tex_steps', 0)
+ _current_exec_node = None # node-key of the currently executing node
+
+ # Track accumulated sub-phases within a node so we can
+ # compute a node-internal overall progress.
+ _sub_phase_idx = 0 # resets when the executing node changes
+ _sub_phase_count = 1 # how many sub-phases this node has
+
+ while True:
+ try:
+ out = ws.recv()
+ except websocket.WebSocketTimeoutException:
+ # Timeout on recv — server is still alive but a heavy
+ # step (e.g. simplification) took longer than expected.
+ # Keep waiting instead of treating it as a crash.
+ print("[TRELLIS2] WebSocket recv timed out — "
+ "server may still be processing. Retrying...")
+ continue
+ except (ConnectionError, OSError, Exception) as ws_err:
+ err_name = type(ws_err).__name__
+ print(f"[TRELLIS2] WebSocket died ({err_name}): {ws_err}")
+ # Distinguish user-initiated cancel from a real crash
+ if getattr(self.operator, '_cancelled', False):
+ return {"error": "cancelled"}
+ return {"error": (
+ "ComfyUI server crashed during TRELLIS.2 generation "
+ "(likely VRAM exhaustion). Please restart ComfyUI, "
+ "reduce max_tokens or resolution, and try again."
+ )}
+ if isinstance(out, str):
+ message = json.loads(out)
+
+ if message['type'] == 'executing':
+ data = message['data']
+ if data['prompt_id'] == prompt_id:
+ if data['node'] is None:
+ self.operator._phase_progress = 100
+ self.operator._detail_progress = 100
+ if hasattr(self.operator, '_update_overall'):
+ self.operator._update_overall()
+ break # Execution complete
+ else:
+ node_id = data['node']
+ node_names = {v: k for k, v in NODES.items()}
+ node_key = node_names.get(node_id, node_id)
+ node_label = NODE_LABELS.get(node_key, node_key)
+ print(f"[TRELLIS2] Executing: {node_label}")
+ self.operator._phase_stage = f"TRELLIS.2: {node_label}"
+ # Update progress based on node weight
+ if node_id in NODE_PROGRESS:
+ self.operator._phase_progress = NODE_PROGRESS[node_id]
+ if hasattr(self.operator, '_update_overall'):
+ self.operator._update_overall()
+ self.operator._detail_stage = node_label
+ self.operator._detail_progress = 0
+
+ # Reset sub-phase tracking for the new node
+ _current_exec_node = node_key
+ _sub_phase_idx = 0
+ if node_key == 'image_to_shape':
+ # SS sampling + shape sampling = 2 sub-phases
+ _sub_phase_count = 2
+ elif node_key == 'shape_to_textured_mesh':
+ _sub_phase_count = 1
+ else:
+ _sub_phase_count = 1
+
+ elif message['type'] == 'executed':
+ # Capture output from export node (newer ComfyUI versions)
+ data = message.get('data', {})
+ if data.get('node') == export_node_id:
+ ws_output = data.get('output', {})
+ if ws_output:
+ for key in ['glb_path', 'file_path', 'text', 'string']:
+ val = ws_output.get(key)
+ if val:
+ if isinstance(val, list) and len(val) > 0:
+ val = val[0]
+ if isinstance(val, str) and val:
+ glb_output_path = val
+ print(f"[TRELLIS2] Got path from WS executed event: {glb_output_path}")
+ break
+ # Also check any key with a file-like value
+ if not glb_output_path:
+ for key, val in ws_output.items():
+ if isinstance(val, list) and len(val) > 0:
+ val = val[0]
+ if isinstance(val, str) and val.endswith(('.glb', '.obj', '.ply')):
+ glb_output_path = val
+ print(f"[TRELLIS2] Got path from WS (key={key}): {glb_output_path}")
+ break
+
+ elif message['type'] == 'progress':
+ # Within-node progress (sampler steps).
+ # TRELLIS.2 nodes may emit separate runs of progress
+ # events for each internal sampling phase. We detect
+ # the sub-phase by matching ``max`` against known step
+ # counts (SS, shape, texture).
+ p_data = message['data']
+ p_value = p_data['value']
+ p_max = p_data['max']
+ step_progress = (p_value / p_max) * 100 if p_max else 0
+
+ # ── Sub-phase identification ──
+ sub_label = ""
+ if _current_exec_node == 'image_to_shape':
+ if p_max == _ss_steps:
+ sub_label = "Sampling SS"
+ # First sub-phase: reset index if we see it again
+ if _sub_phase_idx == 0 or p_value == 1:
+ _sub_phase_idx = 0
+ elif p_max == _shape_steps:
+ sub_label = "Sampling Shape SLat"
+ if _sub_phase_idx < 1:
+ _sub_phase_idx = 1
+ else:
+ # Unknown max — show generic label
+ sub_label = "Sampling"
+ elif _current_exec_node == 'shape_to_textured_mesh':
+ if p_max == _tex_steps:
+ sub_label = "Sampling Texture"
+ else:
+ sub_label = "Sampling"
+ else:
+ sub_label = "Processing"
+
+ # Compute node-internal overall progress accounting for sub-phases
+ if _sub_phase_count > 1:
+ node_overall = ((_sub_phase_idx + step_progress / 100.0)
+ / _sub_phase_count) * 100.0
+ else:
+ node_overall = step_progress
+
+ if step_progress != 0:
+ self.operator._detail_progress = node_overall
+ self.operator._detail_stage = (
+ f"{sub_label}: Step {p_value}/{p_max}"
+ )
+ print(f"[TRELLIS2] {sub_label}: Step {p_value}/{p_max} ({step_progress:.0f}%)")
+
+ elif message['type'] == 'execution_error':
+ error_data = message.get('data', {})
+ error_msg = error_data.get('exception_message', 'Unknown error')
+ self.operator._error = f"TRELLIS.2 execution error: {error_msg}"
+ print(f"[TRELLIS2] Error: {self.operator._error}")
+ return {"error": self.operator._error}
+ finally:
+ if hasattr(self.operator, '_active_ws'):
+ self.operator._active_ws = None
+ if ws:
+ try:
+ ws.close()
+ except Exception:
+ pass
+
+ # ── Retrieve the GLB file ──────────────────────────────────────
+ # Strategy 1: path from WS 'executed' event (already captured above)
+ # Strategy 2: path from history API
+ # Strategy 3: direct file read from disk (local ComfyUI)
+ # Strategy 4: HTTP download via /view endpoint
+ # Strategy 5: scan ComfyUI output dir for our prefix
+
+ glb_source_path = glb_output_path # May already be set from WS event
+
+ # Helper: bail early when the user hits Cancel
+ def _cancelled():
+ return getattr(self.operator, '_cancelled', False)
+
+ # Strategy 2: Query history for the output path
+ if not glb_source_path and not _cancelled():
+ try:
+ history_url = f"http://{server_address}/history/{prompt_id}"
+ history_response = json.loads(urllib.request.urlopen(
+ history_url, timeout=get_timeout('api')).read())
+
+ if prompt_id in history_response:
+ outputs = history_response[prompt_id].get("outputs", {})
+ export_output = outputs.get(export_node_id, {})
+ print(f"[TRELLIS2] History output for node {export_node_id}: {export_output}")
+
+ for key in ['glb_path', 'file_path', 'text', 'string']:
+ val = export_output.get(key)
+ if val:
+ if isinstance(val, list) and len(val) > 0:
+ val = val[0]
+ if isinstance(val, str) and val:
+ glb_source_path = val
+ print(f"[TRELLIS2] Got path from history (key={key}): {glb_source_path}")
+ break
+
+ # Check any key with a path-like value
+ if not glb_source_path:
+ for key, val in export_output.items():
+ if isinstance(val, list) and len(val) > 0:
+ val = val[0]
+ if isinstance(val, str) and val.endswith(('.glb', '.obj', '.ply')):
+ glb_source_path = val
+ print(f"[TRELLIS2] Got path from history (key={key}): {glb_source_path}")
+ break
+ except Exception as e:
+ print(f"[TRELLIS2] History query failed: {e}")
+
+ # Strategy 3: Read directly from disk (works when ComfyUI is local)
+ if glb_source_path and not _cancelled() and os.path.isfile(glb_source_path):
+ try:
+ print(f"[TRELLIS2] Reading GLB directly from disk: {glb_source_path}")
+ with open(glb_source_path, 'rb') as f:
+ glb_data = f.read()
+ print(f"[TRELLIS2] Read {len(glb_data)} bytes from disk")
+ if glb_data and len(glb_data) > 0:
+ return glb_data
+ except Exception as e:
+ print(f"[TRELLIS2] Direct file read failed: {e}")
+
+ # Strategy 4: HTTP download via /view endpoint
+ if glb_source_path and not _cancelled():
+ glb_filename = os.path.basename(glb_source_path)
+ try:
+ view_url = f"http://{server_address}/view?filename={urllib.parse.quote(glb_filename)}&type=output"
+ print(f"[TRELLIS2] Downloading GLB via HTTP: {view_url}")
+ glb_response = urllib.request.urlopen(view_url, timeout=get_timeout('transfer'))
+ glb_data = glb_response.read()
+ print(f"[TRELLIS2] Downloaded GLB: {len(glb_data)} bytes")
+ if glb_data and len(glb_data) > 0:
+ return glb_data
+ except Exception as e:
+ print(f"[TRELLIS2] HTTP download failed: {e}")
+
+ if _cancelled():
+ return {"error": "cancelled"}
+
+ # Strategy 5: Scan ComfyUI output directory for files matching our prefix
+ # This handles the case where the export node path wasn't captured via API
+ print(f"[TRELLIS2] Scanning for files with prefix '{unique_prefix}'...")
+
+ # 5a: Try to discover ComfyUI's output directory from the server
+ comfyui_output_dir = None
+ if not _cancelled():
+ try:
+ system_url = f"http://{server_address}/system_stats"
+ system_response = json.loads(urllib.request.urlopen(
+ system_url, timeout=get_timeout('api')).read())
+ # Some ComfyUI versions include directory info
+ comfyui_output_dir = system_response.get("output_dir")
+ except Exception:
+ pass
+
+ # 5b: Try common local paths relative to server
+ if not comfyui_output_dir:
+ # Check if server is localhost - if so, try to find output dir
+ host = server_address.split(':')[0]
+ if host in ('127.0.0.1', 'localhost', '0.0.0.0', '::1'):
+ # Try to discover via the /view endpoint with a known file
+ # Or just check common ComfyUI locations
+ common_paths = [
+ os.path.join(os.environ.get('COMFYUI_PATH', ''), 'output'),
+ 'E:/tools/ComfyUI/output',
+ 'C:/ComfyUI/output',
+ os.path.expanduser('~/ComfyUI/output'),
+ ]
+ for candidate in common_paths:
+ if candidate and os.path.isdir(candidate):
+ comfyui_output_dir = candidate
+ break
+
+ if comfyui_output_dir and os.path.isdir(comfyui_output_dir):
+ print(f"[TRELLIS2] Scanning output dir: {comfyui_output_dir}")
+ try:
+ matching_files = sorted(
+ [f for f in os.listdir(comfyui_output_dir)
+ if f.startswith(unique_prefix) and f.endswith(('.glb', '.obj', '.ply'))],
+ key=lambda f: os.path.getmtime(os.path.join(comfyui_output_dir, f)),
+ reverse=True
+ )
+ if matching_files:
+ found_path = os.path.join(comfyui_output_dir, matching_files[0])
+ print(f"[TRELLIS2] Found matching file: {found_path}")
+ with open(found_path, 'rb') as f:
+ glb_data = f.read()
+ print(f"[TRELLIS2] Read {len(glb_data)} bytes from disk")
+ if glb_data and len(glb_data) > 0:
+ return glb_data
+ except Exception as e:
+ print(f"[TRELLIS2] Output dir scan failed: {e}")
+
+ # 5c: Try HTTP download with timestamp-based filename guesses
+ # The export nodes use format: {prefix}_{YYYYMMDD_HHMMSS}.glb
+ # Use a short timeout per request so slow remote connections don't
+ # block for minutes, and check for cancellation each iteration.
+ is_remote = not self._is_local_server(server_address)
+ scan_range = 30 if is_remote else 120 # fewer guesses for remote
+ scan_timeout = 5 if is_remote else 10 # per-request timeout (seconds)
+ now = datetime.now()
+ for delta_seconds in range(0, scan_range):
+ if _cancelled():
+ return {"error": "cancelled"}
+ for delta in [timedelta(seconds=-delta_seconds), timedelta(seconds=delta_seconds)]:
+ candidate_time = now + delta
+ candidate_name = f"{unique_prefix}_{candidate_time.strftime('%Y%m%d_%H%M%S')}.glb"
+ try:
+ view_url = f"http://{server_address}/view?filename={urllib.parse.quote(candidate_name)}&type=output"
+ glb_response = urllib.request.urlopen(view_url, timeout=scan_timeout)
+ glb_data = glb_response.read()
+ if glb_data and len(glb_data) > 0:
+ print(f"[TRELLIS2] Found GLB via timestamp scan: {candidate_name} ({len(glb_data)} bytes)")
+ return glb_data
+ except Exception:
+ continue
+
+ if _cancelled():
+ return {"error": "cancelled"}
+
+ self.operator._error = (
+ f"Failed to retrieve GLB from ComfyUI. "
+ f"The workflow completed but the output file could not be located. "
+ f"Prefix: {unique_prefix}"
+ )
+ return {"error": self.operator._error}
+
diff --git a/stablegen/render_tools.py b/stablegen/render_tools.py
deleted file mode 100644
index 937f412..0000000
--- a/stablegen/render_tools.py
+++ /dev/null
@@ -1,6725 +0,0 @@
-""" This file contains the operators and panels for the StableGen addon """
-# disable import-error because pylint doesn't recognize the blenders internal modules
-import os
-import bpy, bmesh # pylint: disable=import-error
-import numpy as np
-import math
-import time
-import mathutils
-from mathutils import Matrix
-import blf
-from .utils import get_file_path, get_dir_path, remove_empty_dirs, get_compositor_node_tree, configure_output_node_paths, get_eevee_engine_id, sg_modal_active
-import gpu
-from gpu_extras.batch import batch_for_shader
-import tempfile
-import shutil
-from PIL import Image
-
-# Import wheels
-import cv2
-import imageio
-import imageio_ffmpeg
-
-def purge_orphans():
- """
- Purge unused datablocks (images, materials, node groups, etc).
- Uses Blender's Outliner orphan purge.
- """
- try:
- # Blender 3.x/4.x: call recursively a few times to fully purge
- for _ in range(5):
- result = bpy.ops.outliner.orphans_purge(do_recursive=True)
- # If it reports "CANCELLED" or does nothing, we're done
- if 'CANCELLED' in result:
- break
- except Exception as e:
- print(f"[StableGen] Orphan purge failed: {e}")
-
-def apply_vignette_to_mask(mask_file_path, feather_width=0.15, gamma=1.0, blur=True):
- """
- Soften hard edges in a grayscale visibility mask.
-
- Instead of only darkening a thin border at the image edges, this applies
- a Gaussian blur whose radius is proportional to the image size. That means
- *any* 0→1 transition in the mask (occlusion edges, camera frustum edges,
- etc.) becomes a smooth ramp, which the shader can use for a soft blend.
-
- feather_width: fraction of min(image_w, image_h) used as blur radius.
- 0.0 = no blur, 0.5 = very soft edges.
- gamma: optional gamma applied to the blurred mask (1.0 = none).
- blur: whether to apply Gaussian blur.
- """
- log_prefix = "[StableGen] Vignette:"
-
- # -------------------------------------------------------------------------
- # Basic guards
- # -------------------------------------------------------------------------
- if feather_width <= 0.0:
- return
-
- if not isinstance(mask_file_path, str):
- print(f"{log_prefix} mask_file_path must be a string, got {type(mask_file_path)}")
- return
-
- if not os.path.exists(mask_file_path):
- print(f"{log_prefix} mask file not found: {mask_file_path}")
- return
-
- img = cv2.imread(mask_file_path, cv2.IMREAD_GRAYSCALE)
- if img is None:
- print(f"{log_prefix} failed to read mask: {mask_file_path}")
- return
-
- # -------------------------------------------------------------------------
- # Blur-based feathering
- # -------------------------------------------------------------------------
- h, w = img.shape[:2]
- base = img.astype(np.float32) / 255.0
-
- # Radius as a fraction of the smallest dimension
- min_dim = float(min(h, w))
- radius = max(1.0, feather_width * min_dim)
-
- # Kernel size must be odd and at least 3
- ksize = int(max(3, int(radius) | 1))
-
- if blur:
- blurred = cv2.GaussianBlur(base, (ksize, ksize), 0)
- else:
- blurred = base
-
- if gamma != 1.0:
- blurred = np.power(blurred, gamma)
-
- result = np.clip(blurred, 0.0, 1.0)
- result_u8 = (result * 255.0).astype(np.uint8)
- cv2.imwrite(mask_file_path, result_u8)
-
- print(
- f"{log_prefix} soft-edge blur applied to mask: {mask_file_path} "
- f"(ksize={ksize}, fw={feather_width}, gamma={gamma}, blur={blur})"
- )
-
-
-def create_edge_feathered_mask(mask_path, feather_width=30):
- """Create an edge-feathered version of a visibility mask for projection blending.
-
- Uses a distance transform on the binary mask so that:
- - Interior pixels (far from any edge) → 1.0 (full new texture)
- - Edge pixels (near visibility boundary) → ramp 0→1 over *feather_width* px
- - Invisible pixels → 0.0 (keep original texture)
-
- The result is saved next to the original with an ``_edgefeather`` suffix.
- Returns the output path, or *None* on failure.
- """
- if not isinstance(mask_path, str) or not os.path.exists(mask_path):
- print(f"[StableGen] Edge-feather: mask not found: {mask_path}")
- return None
-
- mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
- if mask is None:
- print(f"[StableGen] Edge-feather: failed to read mask: {mask_path}")
- return None
-
- # Threshold to hard binary (visible = white, invisible = black)
- _, binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
-
- # Distance transform: each white pixel → distance to nearest black pixel
- dist = cv2.distanceTransform(binary, cv2.DIST_L2, 5)
-
- # Normalise into [0, 1] ramp over feather_width pixels
- feathered = np.clip(dist / max(feather_width, 1), 0.0, 1.0)
-
- feathered_u8 = (feathered * 255).astype(np.uint8)
-
- output_path = mask_path.replace('.png', '_edgefeather.png')
- cv2.imwrite(output_path, feathered_u8)
- print(f"[StableGen] Edge-feather mask saved: {output_path} (width={feather_width}px)")
- return output_path
-
-
-def render_edge_feather_mask(context, to_export, camera, camera_index, feather_width=30, softness=1.0):
- """Render a geometry silhouette from *camera* and apply distance-transform
- edge feathering.
-
- All target objects render as white (Emission), non-target mesh objects are
- hidden, and the world is set to black. The resulting binary silhouette is
- distance-transformed so that interior pixels = 1.0, boundary pixels ramp
- 0→1 over *feather_width* pixels, and background = 0.0.
-
- The final mask is saved to ``inpaint/visibility/render{camera_index}_edgefeather.png``.
- Returns the output path, or *None* on failure.
- """
- output_dir = get_dir_path(context, "inpaint")["visibility"]
- os.makedirs(output_dir, exist_ok=True)
- raw_file = f"render{camera_index}_geomask"
-
- # ── Save original state ─────────────────────────────────────────────────
- original_camera = context.scene.camera
- original_engine = context.scene.render.engine
- original_transparent = context.scene.render.film_transparent
- original_samples = context.scene.cycles.samples
- original_view_transform = bpy.context.scene.view_settings.view_transform
- original_use_compositing = context.scene.render.use_compositing
- original_filepath = context.scene.render.filepath
-
- world = context.scene.world
- if not world:
- world = bpy.data.worlds.new("World")
- context.scene.world = world
- original_use_nodes = world.use_nodes
- original_color = world.color.copy()
- original_bg_node_color = None
- original_bg_node_strength = None
- if world.use_nodes and world.node_tree:
- for wn in world.node_tree.nodes:
- if wn.type == 'BACKGROUND':
- original_bg_node_color = tuple(wn.inputs["Color"].default_value)
- original_bg_node_strength = wn.inputs["Strength"].default_value
- break
-
- # ── Camera ──────────────────────────────────────────────────────────────
- 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)
-
- # ── Hide non-target mesh objects ────────────────────────────────────────
- hidden_restore = {}
- for obj in context.scene.objects:
- if obj.type == 'MESH' and obj not in to_export:
- hidden_restore[obj] = obj.hide_render
- obj.hide_render = True
-
- # ── World → black background ────────────────────────────────────────────
- if bpy.app.version >= (5, 0, 0):
- world.use_nodes = True
- if world.node_tree:
- for wn in world.node_tree.nodes:
- if wn.type == 'BACKGROUND':
- wn.inputs["Color"].default_value = (0.0, 0.0, 0.0, 1.0)
- wn.inputs["Strength"].default_value = 1.0
- break
- else:
- world.color = (0, 0, 0)
- world.use_nodes = False
-
- # ── Render settings ─────────────────────────────────────────────────────
- context.scene.render.engine = 'CYCLES'
- context.scene.render.film_transparent = False
- context.scene.cycles.samples = 1
- bpy.context.scene.display_settings.display_device = 'sRGB'
- bpy.context.scene.view_settings.view_transform = 'Raw'
-
- view_layer = context.view_layer
- view_layer.use_pass_emit = True
- view_layer.use_pass_environment = True
-
- # ── Compositor ──────────────────────────────────────────────────────────
- context.scene.render.use_compositing = True
- context.scene.use_nodes = True
- 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')
- except Exception:
- mix_node = comp_nodes.new('ShaderNodeMixRGB')
- mix_node.blend_type = 'ADD'
- mix_node.inputs[0].default_value = 1
- output_node = comp_nodes.new('CompositorNodeOutputFile')
- configure_output_node_paths(output_node, output_dir, raw_file)
-
- if bpy.app.version < (5, 0, 0):
- comp_links.new(render_layers.outputs['Emit'], mix_node.inputs[1])
- comp_links.new(render_layers.outputs['Env'], mix_node.inputs[2])
- else:
- comp_links.new(render_layers.outputs['Emission'], mix_node.inputs[1])
- comp_links.new(render_layers.outputs['Environment'], mix_node.inputs[2])
- comp_links.new(mix_node.outputs[0], output_node.inputs[0])
-
- # ── Render ──────────────────────────────────────────────────────────────
- bpy.ops.render.render(write_still=True)
-
- # ── Determine actual output path ────────────────────────────────────────
- 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
-
- # ── Restore render / world settings ─────────────────────────────────────
- context.scene.camera = original_camera
- context.scene.render.engine = original_engine
- context.scene.render.film_transparent = original_transparent
- context.scene.cycles.samples = original_samples
- bpy.context.scene.view_settings.view_transform = original_view_transform
- context.scene.render.use_compositing = original_use_compositing
- context.scene.render.filepath = original_filepath
- world.use_nodes = original_use_nodes
- world.color = original_color
- if original_bg_node_color is not None and world.node_tree:
- for wn in world.node_tree.nodes:
- if wn.type == 'BACKGROUND':
- wn.inputs["Color"].default_value = original_bg_node_color
- wn.inputs["Strength"].default_value = original_bg_node_strength
- break
-
- # ── Distance-transform edge feathering ──────────────────────────────────
- if not os.path.exists(raw_path):
- print(f"[StableGen] Edge-feather: raw mask not found after render: {raw_path}")
- return None
-
- mask_img = cv2.imread(raw_path, cv2.IMREAD_GRAYSCALE)
- if mask_img is None:
- print(f"[StableGen] Edge-feather: failed to read raw mask: {raw_path}")
- return None
-
- _, binary = cv2.threshold(mask_img, 127, 255, cv2.THRESH_BINARY)
- dist = cv2.distanceTransform(binary, cv2.DIST_L2, 5)
- linear = np.clip(dist / max(feather_width, 1), 0.0, 1.0).astype(np.float32)
-
- # Gaussian-blur the linear ramp to round off the kinks at both ends.
- # The linear ramp has sharp slope discontinuities at dist=0 (edge) and
- # dist=feather_width (interior plateau). A small Gaussian blur smooths
- # these transitions without shifting the ramp position or compressing the
- # transition zone the way smoothstep does.
- # softness == 0 → raw linear ramp (kinks preserved)
- # softness == 1 → moderate rounding (sigma ≈ 25% of feather width)
- # softness > 1 → stronger rounding / wider smooth zone
- if softness > 0.01:
- blur_sigma = softness * feather_width * 0.25
- feathered = cv2.GaussianBlur(linear, (0, 0), sigmaX=blur_sigma)
- feathered = np.clip(feathered, 0.0, 1.0)
- else:
- feathered = linear
-
- feathered_u8 = (np.clip(feathered, 0.0, 1.0) * 255).astype(np.uint8)
-
- ef_path = os.path.join(output_dir, f"render{camera_index}_edgefeather.png")
- cv2.imwrite(ef_path, feathered_u8)
-
- # Clean up raw mask
- try:
- os.remove(raw_path)
- except OSError:
- pass
-
- print(f"[StableGen] Edge-feather mask saved: {ef_path} (width={feather_width}px, softness={softness:.2f})")
- return ef_path
-
-
-def apply_uv_inpaint_texture(context, obj, baked_image_path):
- """
- Apply a UV inpainted/baked texture to the active material.
-
- Priority:
- 1) StableGen projection chain: replace LAST MixRGB with "Projection" in name (Color2).
- 2) Fallback: traverse from Material Output and find a suitable MIX_RGB (Color2 unlinked
- or linked from TEX_IMAGE), then replace that (Color2).
-
- In both cases:
- - Insert TexImage + UVMap (first non-ProjectionUV layer) feeding Color2.
- - Remove existing Color2 links.
- - If replacing a projection input, also remove the old TexImage node and orphaned image.
- """
-
- mat = obj.active_material
- if not mat or not mat.use_nodes or not mat.node_tree:
- print("[StableGen] No active material or no node tree.")
- return
-
- nodes = mat.node_tree.nodes
- links = mat.node_tree.links
-
- # -----------------------------
- # Load baked image
- # -----------------------------
- try:
- img = bpy.data.images.load(baked_image_path)
- except Exception as e:
- print("[StableGen] Failed to load baked image", baked_image_path, e)
- return
-
- # -----------------------------
- # Helpers
- # -----------------------------
- def pick_uv_name():
- uv_layers = getattr(obj.data, "uv_layers", None)
- if not uv_layers or len(uv_layers) == 0:
- return None
- for uv_layer in uv_layers:
- if not uv_layer.name.startswith("ProjectionUV") and uv_layer.name != "_SG_ProjectionBuffer":
- return uv_layer.name
- return uv_layers.active.name
-
- def clear_color2_links(mix_node, cleanup_teximage=False):
- """Remove links to Color2. Optionally remove upstream TexImage nodes & orphaned images."""
- for link in list(mix_node.inputs["Color2"].links):
- src = link.from_node
- links.remove(link)
-
- if cleanup_teximage and src and src.type == "TEX_IMAGE":
- old_img = src.image
- try:
- nodes.remove(src)
- except Exception:
- pass
- if old_img and old_img.users == 0:
- try:
- bpy.data.images.remove(old_img)
- except Exception:
- pass
-
- def inject_tex_uv_into_color2(mix_node):
- """Create TexImage+UVMap and connect into mix_node Color2."""
- tex = nodes.new("ShaderNodeTexImage")
- tex.image = img
- tex.label = "BakedProjection"
- tex.location = (mix_node.location[0] - 300, mix_node.location[1])
-
- uv = nodes.new("ShaderNodeUVMap")
- uv_name = pick_uv_name()
- if uv_name:
- uv.uv_map = uv_name
- uv.location = (tex.location[0] - 300, tex.location[1] - 200)
-
- links.new(uv.outputs["UV"], tex.inputs["Vector"])
- links.new(tex.outputs["Color"], mix_node.inputs["Color2"])
-
- # ============================================================
- # 1) StableGen projection chain path
- # ============================================================
- proj_mix_nodes = [n for n in nodes if n.type == "MIX_RGB" and "Projection" in n.name]
- if proj_mix_nodes:
- mix_node = sorted(proj_mix_nodes, key=lambda n: n.name)[-1]
-
- # Remove previous projection input, clean up old TexImage nodes
- clear_color2_links(mix_node, cleanup_teximage=True)
-
- # Inject baked texture
- inject_tex_uv_into_color2(mix_node)
-
- print(f"[StableGen] Injected baked projection into projection chain ({mix_node.name}) on {obj.name}")
- return
-
- # ============================================================
- # 2) Fallback traversal path (your original logic)
- # ============================================================
- output_node = next((n for n in nodes if n.type == "OUTPUT_MATERIAL"), None)
- if not output_node or not output_node.inputs["Surface"].links:
- print("[StableGen] No Material Output surface link found for fallback.")
- return
-
- before_output = output_node.inputs["Surface"].links[0].from_node
-
- if before_output.type == "BSDF_PRINCIPLED":
- # Follow: Output.Surface -> Principled -> Base Color -> upstream node
- base_color = before_output.inputs.get("Base Color")
- if base_color and base_color.links:
- current_node = base_color.links[0].from_node
- else:
- current_node = None
- else:
- current_node = before_output
-
- mix_node = None
- visited = set()
-
- while current_node and current_node.as_pointer() not in visited:
- visited.add(current_node.as_pointer())
-
- if current_node.type == "MIX_RGB":
- c2 = current_node.inputs.get("Color2")
- if c2:
- if (not c2.is_linked) or (c2.is_linked and c2.links and c2.links[0].from_node.type == "TEX_IMAGE"):
- mix_node = current_node
- break
-
- c2 = current_node.inputs.get("Color2")
- if c2 and c2.links:
- current_node = c2.links[0].from_node
- else:
- current_node = None
-
- if not mix_node:
- print("[StableGen] No suitable fallback MixRGB node found.")
- return
-
- # Remove any existing links on Color2 (fallback does NOT aggressively delete nodes/images)
- clear_color2_links(mix_node, cleanup_teximage=False)
-
- # Insert baked texture + UVMap
- inject_tex_uv_into_color2(mix_node)
-
- print(f"[StableGen] Injected baked projection into fallback chain ({mix_node.name}) on {obj.name}")
-
-
-def flatten_projection_material_for_refine(context, obj, baked_image_path):
- """
- Replace the StableGen ProjectionMaterial on this object with a minimal
- baked-base material that still matches the expectations of:
- - export_emit_image / _setup_emit_material
- - project_image local_edit logic
-
- Final graph:
- UV Map -> Baked Image -> MixRGB -> Principled BSDF -> Output
- """
- import os
- try:
- img = bpy.data.images.load(baked_image_path)
- except Exception as e:
- print(f"[StableGen] Failed to load baked image for {obj.name}: {baked_image_path} ({e})")
- return
-
- # -------------------------------------------------------------------------
- # 1) Find or create a suitable material
- # -------------------------------------------------------------------------
- target_mat = None
- for slot in obj.material_slots:
- mat = slot.material
- if mat and mat.name.startswith("ProjectionMaterial"):
- target_mat = mat
- break
-
- if target_mat is None:
- target_mat = obj.active_material
-
- if target_mat is None:
- target_mat = bpy.data.materials.new(name="ProjectionMaterial")
- obj.data.materials.append(target_mat)
-
- obj.active_material = target_mat
- if obj.active_material_index < 0:
- if target_mat not in obj.data.materials:
- obj.data.materials.append(target_mat)
- obj.active_material_index = obj.material_slots.find(target_mat.name)
-
- # -------------------------------------------------------------------------
- # 2) Build: UV -> Tex -> MixRGB -> Principled -> Output
- # -------------------------------------------------------------------------
- target_mat.use_nodes = True
- nodes = target_mat.node_tree.nodes
- links = target_mat.node_tree.links
-
- # Clear old nodes
- for node in list(nodes):
- nodes.remove(node)
-
- # Output
- output_node = nodes.new("ShaderNodeOutputMaterial")
- output_node.location = (800, 0)
-
- # Principled
- principled_node = nodes.new("ShaderNodeBsdfPrincipled")
- principled_node.location = (500, 0)
- principled_node.inputs["Roughness"].default_value = 1.0
-
- # MixRGB that _setup_emit_material expects to sit before the Principled
- mix_node = nodes.new("ShaderNodeMixRGB")
- mix_node.location = (200, 0)
- mix_node.use_clamp = True
- # Fac=0 -> output = Color1 (baked tex)
- mix_node.inputs["Fac"].default_value = 0.0
-
- # Baked texture
- tex_node = nodes.new("ShaderNodeTexImage")
- tex_node.location = (-100, 0)
- tex_node.image = img
-
- # UV map
- uv_node = nodes.new("ShaderNodeUVMap")
- uv_node.location = (-400, -150)
-
- # -------------------------------------------------------------------------
- # 3) Choose a stable UV map (avoid ProjectionUV)
- # -------------------------------------------------------------------------
- uv_name = None
- for uv in obj.data.uv_layers:
- if not uv.name.startswith("ProjectionUV") and uv.name != "_SG_ProjectionBuffer":
- uv_name = uv.name
- break
-
- if not uv_name:
- uv_names = [uv.name for uv in obj.data.uv_layers]
- if "BakeUV" in uv_names:
- uv_name = "BakeUV"
- else:
- uv_layer = obj.data.uv_layers.new(name="BakeUV")
- uv_name = uv_layer.name
-
- uv_node.uv_map = uv_name
-
- # -------------------------------------------------------------------------
- # 4) Wire up the graph
- # -------------------------------------------------------------------------
- links.new(uv_node.outputs["UV"], tex_node.inputs["Vector"])
- links.new(tex_node.outputs["Color"], mix_node.inputs["Color1"])
-
- links.new(mix_node.outputs["Color"], principled_node.inputs["Base Color"])
-
- # Drive emission too (Blender 4.x uses "Emission Color")
- if "Emission Color" in principled_node.inputs:
- links.new(mix_node.outputs["Color"], principled_node.inputs["Emission Color"])
- elif "Emission" in principled_node.inputs:
- links.new(mix_node.outputs["Color"], principled_node.inputs["Emission"])
-
- if "Emission Strength" in principled_node.inputs:
- principled_node.inputs["Emission Strength"].default_value = 1.0
-
- links.new(principled_node.outputs["BSDF"], output_node.inputs["Surface"])
-
-
- 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)):
- """
- Exports a emit-only render of the scene from a camera's perspective.
- :param context: Blender context.
- :param camera_id: ID of the camera.
- :return: None
- """
- print("Exporting emit render")
- # Set animation frame to 1
- bpy.context.scene.frame_set(1)
-
- # Store original materials and create temporary ones
- original_materials = {}
- original_active_material = {}
- temporary_materials = {}
-
- # Check if there is BSDF applied
-
- # We need to temporarily disconnect BDSF nodes and connect their inputs directly to the output
-
- for obj in to_export:
- # Store original materials
- original_materials[obj] = list(obj.data.materials)
- original_active_material[obj] = obj.active_material
-
- # Copy active material and switch to it
- mat = obj.active_material
- if not mat:
- continue
- mat_copy = mat.copy()
-
- # Clear materials and assign temp material
- obj.data.materials.clear()
- obj.data.materials.append(mat_copy)
-
- # Store the temporary material for later deletion
- temporary_materials[obj] = mat_copy
-
- # Enable use of nodes
- mat_copy.use_nodes = True
- nodes = mat_copy.node_tree.nodes
- links = mat_copy.node_tree.links
-
- # Find the output node
- output = None
- for node in nodes:
- if node.type == 'OUTPUT_MATERIAL':
- output = node
- break
-
- if not output or not output.inputs[0].links:
- continue
-
- # Check the type of the node which connects to output
- before_output = output.inputs[0].links[0].from_node
- if before_output.type == 'BSDF_PRINCIPLED':
- # Find the last color mix node
- color_mix = output.inputs[0].links[0].from_node.inputs[0].links[0].from_node
- # Set color 2 to fallback color
- if not "visibility" in str(camera_id):
- color_mix.inputs["Color2"].default_value = (fallback_color[0], fallback_color[1], fallback_color[2], 1.0)
- else:
- # Already a color mix node
- color_mix = before_output
- # Set color 2 to fallback color
- if not "visibility" in str(camera_id):
- color_mix.inputs["Color2"].default_value = (fallback_color[0], fallback_color[1], fallback_color[2], 1.0)
-
- # Blender 5.0+: Wrap color output with Emission shader so the Emit pass picks it up
- if bpy.app.version >= (5, 0, 0):
- emission_node = nodes.new("ShaderNodeEmission")
- emission_node.location = (output.location.x - 200, output.location.y)
- links.new(color_mix.outputs[0], emission_node.inputs["Color"])
- links.new(emission_node.outputs[0], output.inputs["Surface"])
- continue
-
- # Find the last color mix node
- color_mix = output.inputs[0].links[0].from_node.inputs[0].links[0].from_node
- # Connect the color mix node directly to the output
- if bpy.app.version >= (5, 0, 0):
- # Blender 5.0+: Wrap in Emission shader for Emit pass
- emission_node = nodes.new("ShaderNodeEmission")
- emission_node.location = (output.location.x - 200, output.location.y)
- links.new(color_mix.outputs[0], emission_node.inputs["Color"])
- links.new(emission_node.outputs[0], output.inputs["Surface"])
- else:
- links.new(color_mix.outputs[0], output.inputs[0])
-
- output_dir = get_dir_path(context, "inpaint")["visibility"] if "visibility" in str(camera_id) else get_dir_path(context, "inpaint")["render"]
- output_file = f"ctx_render{camera_id}" if camera_id is not None else "ctx_render"
-
- # Store and set world settings
- world = context.scene.world
- if not world:
- world = bpy.data.worlds.new("World")
- context.scene.world = world
-
- # Store original settings
- original_engine = context.scene.render.engine
- original_film_transparent = context.scene.render.film_transparent
- original_use_compositing = context.scene.render.use_compositing
- original_filepath = context.scene.render.filepath
- original_use_nodes = world.use_nodes
- original_color = world.color.copy()
- original_bg_node_color = None
- original_bg_node_strength = None
- if world.use_nodes and world.node_tree:
- for wn in world.node_tree.nodes:
- if wn.type == 'BACKGROUND':
- original_bg_node_color = tuple(wn.inputs["Color"].default_value)
- original_bg_node_strength = wn.inputs["Strength"].default_value
- break
-
- # Set world background color
- if bpy.app.version >= (5, 0, 0):
- # Blender 5.0+: Use shader nodes for world background (required for Environment pass)
- world.use_nodes = True
- if world.node_tree:
- for wn in world.node_tree.nodes:
- if wn.type == 'BACKGROUND':
- wn.inputs["Color"].default_value = (*bg_color[:3], 1.0)
- wn.inputs["Strength"].default_value = 1.0
- break
- else:
- world.color = bg_color
- world.use_nodes = False
-
- # Switch to CYCLES render engine (needed for emission pass rendering)
- context.scene.render.engine = 'CYCLES'
- # Force CPU + OSL only for Blender < 5.1 (native Raycast nodes don't need it)
- if bpy.app.version < (5, 1, 0):
- if hasattr(context.scene.cycles, 'shading_system'):
- context.scene.cycles.shading_system = True
- else:
- context.scene.cycles.use_osl = True
- context.scene.cycles.device = 'CPU'
- context.scene.render.film_transparent = False
- # Change color management to standard
- bpy.context.scene.display_settings.display_device = 'sRGB'
- bpy.context.scene.view_settings.view_transform = view_transform
- context.scene.cycles.samples = 1 # Minimum samples for speed
- # Configure view layer settings for diffuse-only
- view_layer = context.view_layer
- view_layer.use_pass_diffuse_color = False
- view_layer.use_pass_diffuse_direct = False
- view_layer.use_pass_diffuse_indirect = False
-
- # Disable all other passes
- view_layer.use_pass_ambient_occlusion = False
- view_layer.use_pass_shadow = False
- view_layer.use_pass_emit = True
- view_layer.use_pass_environment = True
-
-
- # Set up compositor nodes
- context.scene.render.use_compositing = True
- context.scene.use_nodes = True
- node_tree = get_compositor_node_tree(context.scene)
- nodes = node_tree.nodes
- links = node_tree.links
-
- # Clear existing nodes
- nodes.clear()
-
- # Create nodes
- render_layers = nodes.new('CompositorNodeRLayers')
- try:
- mix_node = nodes.new('CompositorNodeMixRGB')
- except:
- mix_node = nodes.new('ShaderNodeMixRGB')
- mix_node.blend_type = 'ADD'
- mix_node.inputs[0].default_value = 1
- output_node = nodes.new('CompositorNodeOutputFile')
- configure_output_node_paths(output_node, output_dir, output_file)
-
- # Connect emission to output
- # Blender 5.0+ renamed pass names: Emit -> Emission, Env -> Environment
- if bpy.app.version < (5, 0, 0):
- links.new(render_layers.outputs['Emit'], mix_node.inputs[1])
- links.new(render_layers.outputs['Env'], mix_node.inputs[2])
- else:
- links.new(render_layers.outputs['Emission'], mix_node.inputs[1])
- links.new(render_layers.outputs['Environment'], mix_node.inputs[2])
- links.new(mix_node.outputs[0], output_node.inputs[0])
-
- # Render
- bpy.ops.render.render(write_still=True)
-
- # Post-processing for visibility masks
- if "visibility" in str(camera_id):
- # Determine the actual file path (Blender 4.x appends 0001, 5.x does not)
- if bpy.app.version >= (5, 0, 0):
- final_path = os.path.join(output_dir, f"{output_file}.png")
- else:
- final_path = os.path.join(output_dir, f"{output_file}0001.png")
-
- if context.scene.visibility_vignette and (context.scene.generation_method == 'local_edit' or (context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method == 'local_edit')):
- # Smooth edge feathering, no blocky mask
- apply_vignette_to_mask(
- final_path,
- feather_width=context.scene.visibility_vignette_width,
- gamma=1.0,
- blur=context.scene.visibility_vignette_blur
- )
- elif context.scene.mask_blocky:
- # Only do blocky mask if vignette is OFF
- expanded_mask = expand_mask_to_blocks(final_path, block_size=8)
- if expanded_mask is not None:
- expanded_mask_u8 = (expanded_mask * 255).astype(np.uint8)
- cv2.imwrite(final_path, expanded_mask_u8)
-
- # Restore original settings
- context.scene.render.engine = original_engine
- context.scene.render.film_transparent = original_film_transparent
- context.scene.render.use_compositing = original_use_compositing
- context.scene.render.filepath = original_filepath
- world.use_nodes = original_use_nodes
- world.color = original_color
- if original_bg_node_color is not None and world.node_tree:
- for wn in world.node_tree.nodes:
- if wn.type == 'BACKGROUND':
- wn.inputs["Color"].default_value = original_bg_node_color
- wn.inputs["Strength"].default_value = original_bg_node_strength
- break
- bpy.context.scene.view_settings.view_transform = 'Standard'
-
- # Restore original materials
- for obj, materials in original_materials.items():
- obj.data.materials.clear()
- # First append the original active material
- if original_active_material[obj]:
- obj.data.materials.append(original_active_material[obj])
- for mat in materials:
- if mat != original_active_material[obj]:
- obj.data.materials.append(mat)
-
- # Clean up temporary materials
- for _, temp_mat in temporary_materials.items():
- if temp_mat and temp_mat.name in bpy.data.materials:
- bpy.data.materials.remove(temp_mat)
-
- print(f"Emmision render saved to: {os.path.join(output_dir, output_file)}.png")
-
-
-def export_render(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.
- :param context: Blender context.
- :param camera_id: ID of the camera for the output filename.
- :param output_dir: Optional output directory.
- :param filename: Optional filename (without component/frame suffix).
- :return: None
- """
- print("Exporting render using Workbench")
-
- # Store original materials and create temporary ones
- original_materials = {}
- original_active_material = {}
- for obj in context.view_layer.objects:
- if obj.type == 'MESH':
- # Store original materials
- original_materials[obj] = list(obj.data.materials)
- original_active_material[obj] = obj.active_material
-
- # Create temporary material
- temp_mat = bpy.data.materials.new(name="TempRenderMaterial")
- 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")
-
- if filename is None:
- output_file = f"render{camera_id}" if camera_id is not None else "render"
- else:
- output_file = filename
-
- # Store original render settings
- original_engine = context.scene.render.engine
- original_workbench_settings = {
- 'lighting': context.scene.display.shading.light,
- 'color_type': context.scene.display.shading.color_type
- }
- original_render_filepath = context.scene.render.filepath
- original_image_settings = context.scene.render.image_settings.file_format
- original_use_compositing = context.scene.render.use_compositing
-
- # Switch to WORKBENCH render engine and configure settings
- context.scene.render.engine = 'BLENDER_WORKBENCH'
- # Configure Workbench for a flat, consistent look if needed
- context.scene.display.shading.light = 'STUDIO'
- context.scene.display.shading.color_type = 'SINGLE'
-
- render_layer = context.view_layer
- original_combined = render_layer.use_pass_combined
-
- # Enable combined pass for Workbench
- render_layer.use_pass_combined = True
-
- # Set up output nodes (Compositor setup remains the same)
- context.scene.render.use_compositing = True
- context.scene.use_nodes = True
- 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)
- links.new(render_layers.outputs['Image'], output_node.inputs[0])
-
- # Render
- bpy.ops.render.render(write_still=True)
-
- # Restore original materials
- for obj, materials in original_materials.items():
- obj.data.materials.clear()
- # First append the original active material
- if original_active_material[obj]:
- obj.data.materials.append(original_active_material[obj])
- for mat in materials:
- if mat != original_active_material[obj]:
- obj.data.materials.append(mat)
-
- # Restore original render settings
- render_layer.use_pass_combined = original_combined
-
- # Clean up temporary materials
- # Use a while loop to safely remove materials while iterating
- temp_mats = [m for m in bpy.data.materials if m.name.startswith("TempRenderMaterial")]
- for mat in temp_mats:
- bpy.data.materials.remove(mat)
-
-
- # Restore original render settings
- context.scene.render.engine = original_engine
- context.scene.display.shading.light = original_workbench_settings['lighting']
- context.scene.display.shading.color_type = original_workbench_settings['color_type']
- context.scene.render.filepath = original_render_filepath
- context.scene.render.image_settings.file_format = original_image_settings
- context.scene.render.use_compositing = original_use_compositing
-
-
- print(f"Render saved to: {os.path.join(output_dir, output_file)}0001.png") # Blender adds frame number
-
-
-def export_viewport(context, camera_id=None, output_dir=None, filename=None):
- """
- Renders the scene using viewport OpenGL render to include overlays.
- :param context: Blender context.
- :param camera_id: ID of the camera for the output filename.
- :param output_dir: Optional output directory.
- :param filename: Optional filename (without component/frame suffix).
- :return: None
- """
- print("Exporting render using Viewport (OpenGL)")
-
- # Setup output path
- if output_dir is None:
- output_dir = get_dir_path(context, "misc")
-
- if filename is None:
- output_file = f"render{camera_id}" if camera_id is not None else "render"
- else:
- output_file = filename
-
- # Store original render settings
- original_engine = context.scene.render.engine
- original_render_filepath = context.scene.render.filepath
- original_image_settings = context.scene.render.image_settings.file_format
-
- # Switch to WORKBENCH render engine for consistent viewport shading
- context.scene.render.engine = 'BLENDER_WORKBENCH'
-
- # Find a viewport
- viewport_area = None
- viewport_region = None
- viewport_space = None
- viewport_region_3d = None
- viewport_window = None
-
- for window in context.window_manager.windows:
- for area in window.screen.areas:
- if area.type == 'VIEW_3D':
- viewport_area = area
- viewport_window = window
- for region in area.regions:
- if region.type == 'WINDOW':
- viewport_region = region
- break
- viewport_space = area.spaces.active
- viewport_region_3d = viewport_space.region_3d if viewport_space else None
- break
- if viewport_area:
- break
-
- if not (viewport_area and viewport_region and viewport_space and viewport_region_3d and viewport_window):
- print("Viewport render failed: no VIEW_3D area found.")
- context.scene.render.engine = original_engine
- return
-
- # Store viewport settings
- original_view_perspective = viewport_region_3d.view_perspective
- original_shading_type = viewport_space.shading.type
- original_overlay_show = viewport_space.overlay.show_overlays
-
- # Configure viewport for camera render with overlays
- viewport_region_3d.view_perspective = 'CAMERA'
- viewport_space.shading.type = 'RENDERED'
- viewport_space.overlay.show_overlays = True
-
- # Configure output filepath
- context.scene.render.filepath = os.path.join(output_dir, f"{output_file}.png")
- context.scene.render.image_settings.file_format = 'PNG'
-
- override = {
- 'window': viewport_window,
- 'screen': viewport_window.screen,
- 'area': viewport_area,
- 'region': viewport_region,
- 'scene': context.scene,
- 'space_data': viewport_space,
- 'region_data': viewport_region_3d,
- }
- with bpy.context.temp_override(**override):
- bpy.ops.render.opengl(write_still=True, view_context=True)
-
- # Restore viewport settings
- viewport_region_3d.view_perspective = original_view_perspective
- viewport_space.shading.type = original_shading_type
- viewport_space.overlay.show_overlays = original_overlay_show
-
- # Restore original render settings
- context.scene.render.engine = original_engine
- context.scene.render.filepath = original_render_filepath
- context.scene.render.image_settings.file_format = original_image_settings
-
- print(f"Viewport render saved to: {os.path.join(output_dir, output_file)}.png")
-
-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.
- :param context: Blender context.
- :param camera_id: ID of the camera for the output filename.
- :param low_threshold: Low threshold for edge detection.
- :param high_threshold: High threshold for edge detection.
- :return: None
- """
- # Render the scene
- export_render(context, camera_id)
-
- # Load the rendered image
- output_dir_render = get_dir_path(context, "misc")
- output_dir_canny = get_dir_path(context, "controlnet")["canny"]
- # Blender 4.x appends 0001 frame suffix, 5.x does not
- frame_suffix = "0001" if bpy.app.version < (5, 0, 0) else ""
- output_file = f"render{camera_id}{frame_suffix}" if camera_id is not None else "render"
- image_path = os.path.join(output_dir_render, f"{output_file}.png")
- image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
-
- # Apply Canny edge detection
- edges = cv2.Canny(image, low_threshold, high_threshold)
-
- # Save the edge detection image
- output_file = f"canny{camera_id}{frame_suffix}" if camera_id is not None else "canny"
- cv2.imwrite(os.path.join(output_dir_canny, f"{output_file}.png"), edges)
-
- print(f"Canny edge detection saved to: {os.path.join(output_dir_canny, output_file)}.png")
-
-
-def expand_mask_to_blocks(mask_file_path, block_size=8):
- """
- Loads a mask image from a file path and processes it so that any
- block_size x block_size grid cell containing any non-black pixel (value > 0)
- becomes fully white (1.0).
-
- Args:
- mask_file_path (str): The path to the mask image file.
- block_size (int): The size of the grid blocks (default: 8).
-
- Returns:
- np.ndarray | None: The processed mask as a NumPy array (float32, normalized [0, 1]).
- Returns None if the file cannot be loaded or processed.
- """
- if not isinstance(mask_file_path, str):
- print(f"Error: mask_file_path must be a string. Got: {type(mask_file_path)}")
- return None
- if not os.path.exists(mask_file_path):
- print(f"Error: Mask file not found at {mask_file_path}")
- return None
- if not os.path.isfile(mask_file_path):
- print(f"Error: Path provided is not a file: {mask_file_path}")
- return None
-
- try:
- # Load the image using Pillow
- with Image.open(mask_file_path) as img:
- # Convert to grayscale ('L') which typically represents intensity
- mask_pil = img.convert("L")
- # Convert PIL Image to numpy array
- mask_array = np.array(mask_pil)
-
- # --- Normalize mask to float32 [0, 1] ---
- # Ensures comparison with 0.0 is reliable regardless of original bit depth
- if mask_array.dtype == np.uint8:
- mask_array = mask_array.astype(np.float32) / 255.0
- elif np.issubdtype(mask_array.dtype, np.integer):
- max_val = np.iinfo(mask_array.dtype).max
- mask_array = mask_array.astype(np.float32) / max_val if max_val > 0 else mask_array.astype(np.float32)
- elif not np.issubdtype(mask_array.dtype, np.floating):
- print(f"Warning: Unsupported mask dtype {mask_array.dtype} after loading. Trying to convert.")
- mask_array = mask_array.astype(np.float32) # Attempt conversion
- # Ensure it's precisely within [0, 1] after potential float conversion
- mask_array = np.clip(mask_array, 0.0, 1.0)
- # --- End normalization ---
-
- max_value = 1.0 # Output will be normalized float [0, 1]
-
- height, width = mask_array.shape
- # Create output mask initialized to zeros (black), ensure float32 type
- output_mask = np.zeros_like(mask_array, dtype=np.float32)
-
- # --- Core block processing logic ---
- for y in range(0, height, block_size):
- for x in range(0, width, block_size):
- # Define block boundaries, handle image edges
- y_start = y
- y_end = min(y + block_size, height)
- x_start = x
- x_end = min(x + block_size, width)
-
- # Extract the current block
- block = mask_array[y_start:y_end, x_start:x_end]
-
- # Check if *any* pixel value in the block is greater than 0.0
- if np.any(block > 0.0):
- # If yes, set the corresponding block in the output mask to 1.0 (white)
- output_mask[y_start:y_end, x_start:x_end] = max_value
- # --- End of core logic ---
-
- return output_mask
-
- except FileNotFoundError:
- print(f"Error: Mask file not found at {mask_file_path}")
- return None
- except Exception as e:
- print(f"Error loading or processing mask file {mask_file_path}: {e}")
- return None
-
-
-def export_visibility(context, to_export, obj=None, camera_visibility=None, prepare_only=False):
- """
- Exports the visibility of the mesh by temporarily altering the shading nodes.
- :param context: Blender context.
- :param filepath: Path to the output file.
- :param obj: Blender object.
- :param camera_visibility: Camera object for visibility calculation.
- :param prepare_only: If True, only prepare the visibility material without
- baking/rendering and without restoring the original materials.
- Used by debug tools to inspect the material in the viewport.
- :return: None
- """
- # Store original materials and create temporary ones
- original_materials = {}
- original_active_material = {}
- temporary_materials = {}
-
- def prepare_material(obj):
- mat = obj.active_material
- if not mat:
- return False
-
- # Store original materials
- original_materials[obj] = list(obj.data.materials)
- original_active_material[obj] = obj.active_material # Store original active material
-
- # Store original active mat
-
- # Copy active material and switch to it
- mat = obj.active_material
- if not mat:
- return False
- mat_copy = mat.copy()
-
- # Clear materials and assign temp material
- obj.data.materials.clear()
- obj.data.materials.append(mat_copy)
-
- # Store temporary material for later deletion
- temporary_materials[obj] = mat_copy
-
- # Enable use of nodes
- mat_copy.use_nodes = True
- nodes = mat_copy.node_tree.nodes
- links = mat_copy.node_tree.links
-
- # Find the output node
- output = None
- for node in nodes:
- if node.type == 'OUTPUT_MATERIAL':
- output = node
- break
-
- if not output:
- return False
-
- if not output.inputs[0].links:
- return False
-
- # 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':
- principled = output.inputs[0].links[0].from_node
- if not principled.inputs[0].links:
- # Principled BSDF exists but nothing is connected to its
- # Base Color – this is not a projection material.
- return False
- color_mix = principled.inputs[0].links[0].from_node
- input = principled.inputs[0]
- else:
- color_mix = output.inputs[0].links[0].from_node
- input = output.inputs[0]
- # Add equal node between color mix and bsdf
- equal = nodes.new("ShaderNodeMath")
-
- # Use compare operation to filter to only 1 or 0
-
- if context.scene.generation_method == 'sequential' and context.scene.sequential_smooth:
- # Add color ramp node
- compare = nodes.new("ShaderNodeValToRGB")
- compare.color_ramp.interpolation = 'LINEAR'
- compare.color_ramp.elements[0].color = (0.0, 0.0, 0.0, 1.0)
- compare.color_ramp.elements[0].position = context.scene.sequential_factor_smooth if context.scene.generation_method == 'sequential' else 0.5
- compare.color_ramp.elements[1].color = (1.0, 1.0, 1.0, 1.0)
- compare.color_ramp.elements[1].position = context.scene.sequential_factor_smooth_2 if context.scene.generation_method == 'sequential' else 1.0
- links.new(color_mix.outputs[0], compare.inputs[0])
- links.new(compare.outputs[0], input)
- elif not context.scene.allow_modify_existing_textures or context.scene.generation_method == 'sequential':
- equal.operation = 'COMPARE'
- equal.inputs[1].default_value = 1
- equal.inputs[2].default_value = context.scene.sequential_factor if context.scene.generation_method == 'sequential' else (1e-5 if bpy.app.version >= (5, 1, 0) else 0.0) # Small epsilon needed for blender 5.1+
- equal.location = (color_mix.location[0], color_mix.location[1])
- links.new(color_mix.outputs[0], equal.inputs[0])
- links.new(equal.outputs[0], input)
- else:
- links.new(color_mix.outputs[0], input)
-
- while True:
- # Remove color ramp connected to fac, connect directly to color ramp's fac
- if not color_mix.inputs[0].links:
- break
- color_ramp = color_mix.inputs[0].links[0].from_node
- if not color_ramp.inputs[0].links:
- # Color ramp Fac is unlinked (constant value) — remove it and
- # fall through so the rest of the chain is wired correctly.
- nodes.remove(color_ramp)
- break
- fac_node = color_ramp.inputs[0].links[0].from_node
- # Add subtract node
- subtract = nodes.new("ShaderNodeMath")
- subtract.operation = 'SUBTRACT'
- subtract.location = (color_ramp.location[0], color_ramp.location[1])
- subtract.inputs[0].default_value = 1
- links.new(fac_node.outputs[0], subtract.inputs[1])
- links.new(subtract.outputs[0], color_mix.inputs["Fac"])
- nodes.remove(color_ramp)
- # Disconnect color1
- links.remove(color_mix.inputs[1].links[0])
- color_mix.inputs["Color1"].default_value = (0, 0, 0, 1)
- if not (color_mix.inputs["Color2"].links and (color_mix.inputs[2].links[0].from_node.type == 'MIX_RGB')):
- color_mix.inputs["Color2"].default_value = (1, 1, 1, 1)
- break
- else:
- color_mix = color_mix.inputs["Color2"].links[0].from_node
- # If there is previous tex_image node, remove it (for cases when this function is called multiple times)
- if color_mix.inputs["Color2"].is_linked:
- nodes.remove(color_mix.inputs["Color2"].links[0].from_node)
-
- for node in nodes:
- if node.type == 'SCRIPT' and "Power" in node.inputs:
- try:
- # Set according to weight_exponent_mask
- node.inputs["Power"].default_value = context.scene.weight_exponent if context.scene.weight_exponent_mask else 1.0
- except Exception as e:
- print(f" - Warning: Failed to set Power for node '{node.name}'. Error: {e}")
- # Also handle native Raycast path (Blender 5.1+) where power is a MATH POWER node
- elif node.type == 'MATH' and node.operation == 'POWER' and node.label == 'power_weight':
- try:
- node.inputs[1].default_value = context.scene.weight_exponent if context.scene.weight_exponent_mask else 1.0
- except Exception as e:
- print(f" - Warning: Failed to set Power for native node '{node.name}'. Error: {e}")
-
- # When the normalization chain exists (multi-camera), the node
- # chain is: power_weight(exp=1) → base_weight → NormW(÷max) →
- # SharpW(pow exp) → mix tree.
- # For the visibility map we need the *original* un-normalized
- # weight: pow(cos(θ), target_exp) × binary_gates.
- # We achieve this by:
- # 1. Setting power_weight to target_exp (restores original weight)
- # 2. Making SharpW a passthrough (exp=1) and rerouting its input
- # from NormW's source (the base weight) so the DIVIDE-by-max
- # is bypassed entirely.
- has_norm_nodes = any(
- n.type == 'MATH' and n.operation == 'DIVIDE'
- and n.label.startswith('NormW-')
- for n in nodes
- )
- if has_norm_nodes:
- target_exp = context.scene.weight_exponent if context.scene.weight_exponent_mask else 1.0
- for node in nodes:
- # Restore per-camera power to the desired exponent
- if (node.type == 'MATH' and node.operation == 'POWER'
- and node.label == 'power_weight'):
- try:
- node.inputs[1].default_value = target_exp
- except Exception as e:
- print(f" - Warning: Failed to set power_weight '{node.name}'. Error: {e}")
- elif node.type == 'SCRIPT' and "Power" in node.inputs:
- try:
- node.inputs["Power"].default_value = target_exp
- except Exception as e:
- print(f" - Warning: Failed to set OSL Power '{node.name}'. Error: {e}")
-
- # Bypass NormW+SharpW: reroute SharpW to read from
- # NormW's source (the base weight), set exponent to 1.0
- elif (node.type == 'MATH' and node.operation == 'POWER'
- and node.label.startswith('SharpW-')):
- try:
- # SharpW.inputs[0] ← NormW.outputs[0]
- # NormW.inputs[0] ← base_weight_output
- # Reroute: SharpW.inputs[0] ← base_weight_output
- if node.inputs[0].links:
- norm_node = node.inputs[0].links[0].from_node
- if (norm_node.label.startswith('NormW-')
- and norm_node.inputs[0].links):
- base_out = norm_node.inputs[0].links[0].from_socket
- links.remove(node.inputs[0].links[0])
- links.new(base_out, node.inputs[0])
- node.inputs[1].default_value = 1.0
- except Exception as e:
- print(f" - Warning: Failed to bypass SharpW '{node.name}'. Error: {e}")
-
- # Also bypass standalone NormW nodes (when user_exponent
- # was 1.0 at build time, there's no SharpW — the NormW
- # DIVIDE node is used directly in the mix tree).
- elif (node.type == 'MATH' and node.operation == 'DIVIDE'
- and node.label.startswith('NormW-')):
- try:
- # Check if this NormW feeds directly into the
- # mix tree (no SharpW after it).
- feeds_sharp = False
- for link in node.outputs[0].links:
- if (link.to_node.type == 'MATH'
- and link.to_node.operation == 'POWER'
- and link.to_node.label.startswith('SharpW-')):
- feeds_sharp = True
- break
- if not feeds_sharp:
- # This NormW feeds the mix tree directly.
- # Reroute its downstream links to its source.
- if node.inputs[0].links:
- base_out = node.inputs[0].links[0].from_socket
- for link in list(node.outputs[0].links):
- to_socket = link.to_socket
- links.remove(link)
- links.new(base_out, to_socket)
- except Exception as e:
- print(f" - Warning: Failed to bypass NormW '{node.name}'. Error: {e}")
-
- return True
-
- # Prepare the material
-
- if not obj:
- # Prepare for all objects
- for obj in to_export:
- if not prepare_material(obj):
- return False
- else:
- if not prepare_material(obj):
- return False
-
- if prepare_only:
- # Debug mode: leave the visibility material applied for viewport inspection
- # Rename the temp materials so they are identifiable as debug materials
- for obj_key, temp_mat in temporary_materials.items():
- temp_mat.name = f"SG_Debug_Visibility_{obj_key.name}"
- return True
-
- # Bake or render the texture
- if not camera_visibility:
- output_dir = get_dir_path(context, "uv_inpaint")["visibility"]
- output_file = f"{obj.name}_baked_visibility"
- prepare_baking(context)
- bake_texture(context, obj, suffix="_visibility", texture_resolution=1024, view_transform='Raw', output_dir=output_dir)
- # Use openCV to normalize the image
- image_path = os.path.join(output_dir, f"{output_file}.png")
- image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
- # Normalize the image
- image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX)
- # Save the image
- cv2.imwrite(image_path, image)
-
- if context.scene.visibility_vignette and (context.scene.generation_method == 'local_edit' or (context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method == 'local_edit')):
- apply_vignette_to_mask(
- image_path,
- feather_width=context.scene.visibility_vignette_width,
- gamma=1.0,
- blur=context.scene.visibility_vignette_blur
- )
- else:
- # Make sure the camera is active and set to render
- cameras = [obj for obj in context.scene.objects if obj.type == 'CAMERA']
- cameras.sort(key=lambda x: x.name)
- camera_visibility_index = [i for i, camera in enumerate(cameras) if camera == camera_visibility][0]
- camera_render_index = (camera_visibility_index + 1) % len(cameras)
- camera_render = cameras[camera_render_index]
- 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))
-
- # Restore original materials
- for obj, materials in original_materials.items():
- obj.data.materials.clear()
- # First append the original active material
- if original_active_material[obj]:
- obj.data.materials.append(original_active_material[obj])
- for mat in materials:
- if mat != original_active_material[obj]:
- obj.data.materials.append(mat)
-
- # Clean up temporary materials
- for _, temp_mat in temporary_materials.items():
- if temp_mat and temp_mat.name in bpy.data.materials:
- bpy.data.materials.remove(temp_mat)
-
- return True
-
-
-# =========================================================
-# Camera Placement Helper Functions
-# =========================================================
-
-
-def _existing_camera_directions(mesh_center):
- """Return unit-direction vectors from *mesh_center* toward each existing
- camera in the scene. Used by 'Consider existing cameras' to treat
- pre-existing cameras as already-placed directions."""
- dirs = []
- center = np.array(mesh_center, dtype=float)
- for obj in bpy.data.objects:
- if obj.type == 'CAMERA':
- pos = np.array(obj.location, dtype=float)
- d = pos - center
- norm = np.linalg.norm(d)
- if norm > 1e-6:
- dirs.append(d / norm)
- return dirs
-
-
-def _filter_near_existing(directions, existing_dirs, min_angle_deg=30.0):
- """Remove directions that are within *min_angle_deg* of any existing
- camera direction. Both *directions* and *existing_dirs* should be
- lists of unit-length numpy arrays."""
- if not existing_dirs or not directions:
- return directions
- cos_thresh = math.cos(math.radians(min_angle_deg))
- existing_np = np.array(existing_dirs) # (M, 3)
- filtered = []
- for d in directions:
- d_np = np.asarray(d, dtype=float)
- n = np.linalg.norm(d_np)
- if n < 1e-12:
- continue
- d_unit = d_np / n
- dots = existing_np @ d_unit # (M,)
- if dots.max() < cos_thresh:
- filtered.append(d)
- return filtered
-
-def _fibonacci_sphere_points(n):
- """Generate *n* approximately evenly-spaced unit vectors on a sphere
- using a Fibonacci spiral. Returns list of (x, y, z) tuples."""
- points = []
- golden_ratio = (1 + math.sqrt(5)) / 2
- for i in range(n):
- theta = math.acos(1 - 2 * (i + 0.5) / n)
- phi = 2 * math.pi * i / golden_ratio
- x = math.sin(theta) * math.cos(phi)
- y = math.sin(theta) * math.sin(phi)
- z = math.cos(theta)
- points.append((x, y, z))
- return points
-
-
-def _gather_target_meshes(context):
- """Return a list of mesh objects to cover.
- - If any selected objects are meshes, use those.
- - Otherwise, use ALL mesh objects in the scene.
- """
- selected = [o for o in context.selected_objects if o.type == 'MESH']
- if selected:
- return selected
- return [o for o in context.scene.objects if o.type == 'MESH']
-
-
-def _get_mesh_face_data(objs):
- """Return world-space face (normals, areas, centers) as numpy arrays.
- *objs* can be a single object or a list of mesh objects."""
- if not isinstance(objs, (list, tuple)):
- objs = [objs]
- all_normals, all_areas, all_centers = [], [], []
- for obj in objs:
- mesh = obj.data
- mat = obj.matrix_world
- rot = mat.to_3x3()
- scale_det = abs(rot.determinant())
- n = len(mesh.polygons)
- normals = np.empty((n, 3))
- areas = np.empty(n)
- centers = np.empty((n, 3))
- for idx, poly in enumerate(mesh.polygons):
- wn = rot @ poly.normal
- wn.normalize()
- normals[idx] = (wn.x, wn.y, wn.z)
- wc = mat @ poly.center
- centers[idx] = (wc.x, wc.y, wc.z)
- areas[idx] = poly.area * (scale_det ** 0.5)
- all_normals.append(normals)
- all_areas.append(areas)
- all_centers.append(centers)
- return np.vstack(all_normals), np.concatenate(all_areas), np.vstack(all_centers)
-
-
-def _filter_bottom_faces(normals, areas, centers, angle_rad):
- """Remove faces whose normals point more than *angle_rad* below the
- horizon (negative-Z). Returns filtered (normals, areas, centers).
-
- *angle_rad* = 80° (≈1.40 rad) removes faces whose normal is > 80° below
- horizontal, i.e. nearly straight down. The Z-component threshold is
- ``-cos(angle_rad)``."""
- threshold_z = -math.cos(angle_rad)
- mask = normals[:, 2] >= threshold_z # keep faces *above* the threshold
- return normals[mask], areas[mask], centers[mask]
-
-
-def _get_mesh_verts_world(objs):
- """Return world-space vertex positions as (N, 3) numpy array.
- *objs* can be a single object or a list of mesh objects."""
- if not isinstance(objs, (list, tuple)):
- objs = [objs]
- parts = []
- for obj in objs:
- mesh = obj.data
- mat = obj.matrix_world
- n = len(mesh.vertices)
- verts = np.empty((n, 3))
- for i, v in enumerate(mesh.vertices):
- wv = mat @ v.co
- verts[i] = (wv.x, wv.y, wv.z)
- parts.append(verts)
- return np.vstack(parts)
-
-
-def _camera_basis(cam_dir_np):
- """Build an orthonormal camera basis from a centre-to-camera direction.
-
- Returns ``(right, up, d_unit)`` where
- * *right* – camera local X (numpy unit vector)
- * *up* – camera local Y (numpy unit vector)
- * *d_unit* – normalised centre-to-camera direction (camera local Z)
-
- When the direction is nearly vertical the world-up fallback switches
- from Z to Y, giving a deterministic orientation that avoids
- ``to_track_quat`` gimbal degeneracy.
- """
- d = cam_dir_np / np.linalg.norm(cam_dir_np)
- forward = -d
- world_up = np.array([0.0, 0.0, 1.0])
- if abs(np.dot(d, world_up)) > 0.99:
- world_up = np.array([0.0, 1.0, 0.0])
- right = np.cross(forward, world_up)
- right /= np.linalg.norm(right)
- up = np.cross(right, forward)
- up /= np.linalg.norm(up)
- return right, up, d
-
-
-def _rotation_from_basis(right, up, d_unit):
- """Build a Blender rotation (Euler) from camera basis vectors.
-
- The resulting rotation aligns the camera's local axes:
- * local X → *right*
- * local Y → *up*
- * local Z → *d_unit* (away from mesh; -Z is the look direction)
- """
- rot_mat = mathutils.Matrix((
- (right[0], up[0], d_unit[0]),
- (right[1], up[1], d_unit[1]),
- (right[2], up[2], d_unit[2]),
- ))
- return rot_mat.to_euler()
-
-
-def _compute_silhouette_distance(verts_world, center_np, cam_dir_np, fov_x, fov_y, margin=0.10):
- """Compute the optimal distance along *cam_dir_np* so every mesh vertex
- fits inside the camera frame with *margin* breathing room.
-
- Uses a perspective-correct formula so objects with depth along the
- viewing direction are never clipped.
-
- Returns ``(distance, aim_offset)`` where *aim_offset* is a 3-element numpy
- vector that shifts from *center_np* to the visual center of the silhouette.
- The caller should aim the camera at ``center_np + aim_offset`` and place
- it at ``center_np + aim_offset + direction * distance``.
- """
- right, up, d = _camera_basis(cam_dir_np)
- forward = -d
-
- # Project every vertex onto the camera's right / up plane
- rel = verts_world - center_np
- proj_r = rel @ right
- proj_u = rel @ up
-
- r_min, r_max = float(proj_r.min()), float(proj_r.max())
- u_min, u_max = float(proj_u.min()), float(proj_u.max())
-
- # Visual centre of the silhouette (midpoint of extents)
- mid_r = (r_max + r_min) / 2.0
- mid_u = (u_max + u_min) / 2.0
-
- # Aim offset: shifts from mesh center to silhouette visual centre
- aim_offset = mid_r * right + mid_u * up
-
- eff_fov_x = fov_x * (1.0 - margin)
- eff_fov_y = fov_y * (1.0 - margin)
-
- tan_hx = math.tan(eff_fov_x / 2) if eff_fov_x > 0.02 else 1e-6
- tan_hy = math.tan(eff_fov_y / 2) if eff_fov_y > 0.02 else 1e-6
-
- # Perspective-correct distance: for each vertex the minimum camera
- # distance along the view direction is |lateral| / tan(half_fov) - depth
- # where depth is signed distance from the aim point along the view ray.
- aim_point = center_np + aim_offset
- rel_aim = verts_world - aim_point
- pr = rel_aim @ right
- pu = rel_aim @ up
- # Depth: positive = in front of aim point (toward camera)
- pd = rel_aim @ d # dot with camera direction (away from mesh)
-
- min_dist_r = np.abs(pr) / tan_hx + pd
- min_dist_u = np.abs(pu) / tan_hy + pd
-
- dist = max(float(min_dist_r.max()), float(min_dist_u.max()), 0.5)
-
- # --- Refine aim_offset using perspective angular centre ---
- # The orthographic midpoint doesn't account for perspective foreshortening.
- # Compute where the visual centre actually is from the camera position and
- # shift the aim to centre it, then recompute the distance.
- cam_pos = aim_point + d * dist
- rel_cam = verts_world - cam_pos
- depth = -(rel_cam @ d) # positive = in front of camera
- depth = np.maximum(depth, 0.001)
- ang_r = np.arctan2(rel_cam @ right, depth)
- ang_u = np.arctan2(rel_cam @ up, depth)
- ang_mid_r = (float(ang_r.max()) + float(ang_r.min())) / 2.0
- ang_mid_u = (float(ang_u.max()) + float(ang_u.min())) / 2.0
- # Convert angular offset to world-space shift (at the computed distance)
- aim_offset = aim_offset + dist * math.tan(ang_mid_r) * right + dist * math.tan(ang_mid_u) * up
-
- # Recompute distance with refined aim
- aim_point = center_np + aim_offset
- rel_aim = verts_world - aim_point
- pr = rel_aim @ right
- pu = rel_aim @ up
- pd = rel_aim @ d
- min_dist_r = np.abs(pr) / tan_hx + pd
- min_dist_u = np.abs(pu) / tan_hy + pd
- dist = max(float(min_dist_r.max()), float(min_dist_u.max()), 0.5)
-
- return dist, aim_offset
-
-
-def _kmeans_on_sphere(directions, weights, k, max_iter=50):
- """Spherical K-means: cluster unit vectors weighted by area.
- Returns (k, 3) numpy array of cluster-centre unit vectors."""
- n_pts = len(directions)
- if n_pts == 0 or k == 0:
- return np.zeros((max(k, 1), 3))
- k = min(k, n_pts)
- rng = np.random.default_rng(42)
- probs = weights / weights.sum()
- indices = rng.choice(n_pts, size=k, replace=False, p=probs)
- centers = directions[indices].copy()
- for _ in range(max_iter):
- dots = directions @ centers.T
- labels = np.argmax(dots, axis=1)
- new_centers = np.zeros_like(centers)
- for j in range(k):
- mask = labels == j
- if mask.any():
- ws = (directions[mask] * weights[mask, np.newaxis]).sum(axis=0)
- nrm = np.linalg.norm(ws)
- new_centers[j] = ws / nrm if nrm > 0 else centers[j]
- else:
- new_centers[j] = centers[j]
- if np.allclose(centers, new_centers, atol=1e-6):
- break
- centers = new_centers
- return centers
-
-
-def _compute_pca_axes(verts):
- """Return the 3 principal axes of a (N, 3) vertex array (rows of a
- 3x3 array, sorted by descending eigenvalue)."""
- mean = verts.mean(axis=0)
- centered = verts - mean
- cov = np.cov(centered.T)
- eigenvalues, eigenvectors = np.linalg.eigh(cov)
- idx = np.argsort(eigenvalues)[::-1]
- return eigenvectors[:, idx].T # rows = principal axes
-
-
-def _greedy_coverage_directions(normals, areas, max_cameras=12,
- coverage_target=0.95, n_candidates=200,
- existing_dirs=None):
- """Greedy set-cover: iteratively pick the camera direction that adds the
- most newly-visible surface area (back-face culling only, no occlusion,
- for speed). Returns (selected_directions, final_coverage_fraction)."""
- total_area = areas.sum()
- if total_area <= 0:
- return [], 0.0
-
- candidates = np.array(_fibonacci_sphere_points(n_candidates))
-
- # visibility[i, j] = True if face i faces toward candidate j
- # cos(75°) ≈ 0.26 – ignore near-grazing faces that won't texture well
- visibility = normals @ candidates.T > 0.26 # (n_faces, n_candidates)
-
- covered = np.zeros(len(areas), dtype=bool)
- # Pre-seed coverage from existing cameras
- if existing_dirs:
- for edir in existing_dirs:
- covered |= normals @ np.asarray(edir, dtype=float) > 0.26
- selected = []
-
- for _ in range(max_cameras):
- uncovered = ~covered
- # Vectorised: for each candidate sum the area of faces that are
- # both visible from it AND not yet covered
- new_vis = visibility & uncovered[:, np.newaxis] # broadcast
- new_areas = (new_vis * areas[:, np.newaxis]).sum(axis=0)
-
- best_idx = int(np.argmax(new_areas))
- if new_areas[best_idx] < total_area * 0.005: # < 0.5 % new coverage
- break
-
- selected.append(candidates[best_idx].copy())
- covered |= visibility[:, best_idx]
-
- coverage = float(areas[covered].sum() / total_area)
- if coverage >= coverage_target:
- break
-
- final_coverage = float(areas[covered].sum() / total_area) if covered.any() else 0.0
- return selected, final_coverage
-
-
-# ──────────────────────────────────────────────────────────────────────
-# BVH occlusion helpers
-# ──────────────────────────────────────────────────────────────────────
-
-def _build_bvh_trees(objs, depsgraph):
- """Build a list of BVHTree objects (one per mesh) for raycasting.
-
- Returns
- -------
- list[mathutils.bvhtree.BVHTree]
- One BVH per object, in world space.
- """
- from mathutils.bvhtree import BVHTree
- trees = []
- for obj in objs:
- bm = bmesh.new()
- bm.from_object(obj, depsgraph)
- bm.transform(obj.matrix_world)
- tree = BVHTree.FromBMesh(bm)
- bm.free()
- trees.append(tree)
- return trees
-
-
-def _ray_occluded(bvh_trees, origin, direction, max_dist):
- """Return True if *any* BVH tree has a hit between *origin* and
- *origin + direction * max_dist* (exclusive of the starting face).
-
- *origin* should already be slightly offset along the face normal to avoid
- self-intersection.
- """
- for tree in bvh_trees:
- hit_loc, _normal, _index, _dist = tree.ray_cast(
- mathutils.Vector(origin), mathutils.Vector(direction), max_dist)
- if hit_loc is not None:
- return True
- return False
-
-
-def _greedy_select_from_visibility(vis, areas, max_cameras, coverage_target,
- candidates, existing_dirs=None,
- normals=None):
- """Run greedy set-cover on a pre-computed visibility matrix.
-
- Parameters
- ----------
- vis : ndarray (n_faces, n_candidates), bool
- areas : ndarray (n_faces,)
- max_cameras : int
- coverage_target : float
- candidates : ndarray (n_candidates, 3)
- existing_dirs : list[ndarray] or None
- Directions of existing cameras to pre-seed coverage.
- normals : ndarray (n_faces, 3) or None
- Face normals, required when *existing_dirs* is given.
-
- Returns
- -------
- (selected_directions, final_coverage)
- """
- total_area = areas.sum()
- if total_area <= 0:
- return [], 0.0
-
- covered = np.zeros(len(areas), dtype=bool)
- # Pre-seed coverage from existing cameras
- if existing_dirs and normals is not None:
- for edir in existing_dirs:
- covered |= normals @ np.asarray(edir, dtype=float) > 0.26
- selected = []
-
- for _ in range(max_cameras):
- uncovered = ~covered
- new_vis = vis & uncovered[:, np.newaxis]
- new_areas = (new_vis * areas[:, np.newaxis]).sum(axis=0)
-
- best_idx = int(np.argmax(new_areas))
- if new_areas[best_idx] < total_area * 0.005:
- break
-
- selected.append(candidates[best_idx].copy())
- covered |= vis[:, best_idx]
-
- coverage = float(areas[covered].sum() / total_area)
- if coverage >= coverage_target:
- break
-
- final_cov = float(areas[covered].sum() / total_area) if covered.any() else 0.0
- return selected, final_cov
-
-
-# ──────────────────────────────────────────────────────────────────────
-# Occlusion generators (modal-friendly, yield progress)
-# ──────────────────────────────────────────────────────────────────────
-
-def _occ_filter_faces_generator(normals, areas, centers, bvh_trees,
- n_candidates=200):
- """Generator: determine which faces are visible from at least one
- camera direction (exterior faces).
-
- Yields progress floats in [0, 1]. Final result (via
- ``StopIteration.value``) is a boolean mask ``(n_faces,)`` where True
- means the face is visible from at least one direction.
-
- Uses early-exit per face: as soon as one unoccluded direction is found,
- the face is marked exterior and skipped for remaining candidates.
- """
- n_faces = len(normals)
- candidates = np.array(_fibonacci_sphere_points(n_candidates))
- backface_vis = normals @ candidates.T > 0.26 # (F, C)
- exterior = np.zeros(n_faces, dtype=bool)
- epsilon = 0.001
- BATCH = 5
-
- for j in range(n_candidates):
- cam_dir = candidates[j]
- for i in range(n_faces):
- if exterior[i]:
- continue # already proven exterior
- if not backface_vis[i, j]:
- continue
- origin = centers[i] + normals[i] * epsilon
- if not _ray_occluded(bvh_trees, origin, cam_dir, 1e6):
- exterior[i] = True
- if (j + 1) % BATCH == 0 or j == n_candidates - 1:
- yield (j + 1) / n_candidates
- # Early termination: all faces proven exterior
- if exterior.all():
- break
-
- return exterior
-
-
-def _occ_vis_count_generator(normals, areas, centers, bvh_trees,
- n_candidates=200):
- """Generator: count how many candidate directions can see each face.
-
- Like ``_occ_filter_faces_generator`` but returns per-face visibility
- *counts* instead of a boolean mask. This enables continuous weighting
- rather than binary keep/remove filtering.
-
- Yields progress floats in [0, 1]. Final result (via
- ``StopIteration.value``) is an int array ``(n_faces,)`` with the number
- of unoccluded candidate directions per face.
- """
- n_faces = len(normals)
- candidates = np.array(_fibonacci_sphere_points(n_candidates))
- backface_vis = normals @ candidates.T > 0.26 # (F, C)
- vis_count = np.zeros(n_faces, dtype=int)
- epsilon = 0.001
- BATCH = 5
-
- for j in range(n_candidates):
- cam_dir = candidates[j]
- for i in range(n_faces):
- if not backface_vis[i, j]:
- continue
- origin = centers[i] + normals[i] * epsilon
- if not _ray_occluded(bvh_trees, origin, cam_dir, 1e6):
- vis_count[i] += 1
- if (j + 1) % BATCH == 0 or j == n_candidates - 1:
- yield (j + 1) / n_candidates
-
- return vis_count
-
-
-def _occ_fom_generator(normals, areas, centers, bvh_trees,
- max_cameras, coverage_target, n_candidates=200,
- existing_dirs=None):
- """Generator: Full Occlusion Matrix approach.
-
- Yields progress floats in [0, 1]. Final result is available via
- ``StopIteration.value`` as ``(directions, coverage)``.
- """
- total_area = areas.sum()
- if total_area <= 0:
- yield 1.0
- return [], 0.0
-
- candidates = np.array(_fibonacci_sphere_points(n_candidates))
- backface_vis = normals @ candidates.T > 0.26
- n_faces = len(normals)
- vis = np.zeros((n_faces, n_candidates), dtype=bool)
- epsilon = 0.001
- BATCH = 5
-
- for j in range(n_candidates):
- cam_dir = candidates[j]
- for i in range(n_faces):
- if not backface_vis[i, j]:
- continue
- origin = centers[i] + normals[i] * epsilon
- if not _ray_occluded(bvh_trees, origin, cam_dir, 1e6):
- vis[i, j] = True
- if (j + 1) % BATCH == 0 or j == n_candidates - 1:
- yield (j + 1) / n_candidates
-
- return _greedy_select_from_visibility(
- vis, areas, max_cameras, coverage_target, candidates,
- existing_dirs=existing_dirs, normals=normals)
-
-
-def _occ_tpr_generator(normals, areas, centers, bvh_trees,
- max_cameras, coverage_target, n_candidates=200,
- existing_dirs=None):
- """Generator: Two-Pass Refinement approach.
-
- Yields progress floats in [0, 1]. Final result is available via
- ``StopIteration.value`` as ``(directions, coverage)``.
- """
- total_area = areas.sum()
- if total_area <= 0:
- yield 1.0
- return [], 0.0
-
- candidates = np.array(_fibonacci_sphere_points(n_candidates))
- backface_vis = normals @ candidates.T > 0.26
-
- # ── Pass 1: fast greedy (back-face only, instant) ─────────────────
- covered_bf = np.zeros(len(areas), dtype=bool)
- # Pre-seed coverage from existing cameras
- if existing_dirs:
- for edir in existing_dirs:
- covered_bf |= normals @ np.asarray(edir, dtype=float) > 0.26
- selected_indices = []
-
- for _ in range(max_cameras):
- uncov = ~covered_bf
- new_vis = backface_vis & uncov[:, np.newaxis]
- new_areas_arr = (new_vis * areas[:, np.newaxis]).sum(axis=0)
- best = int(np.argmax(new_areas_arr))
- if new_areas_arr[best] < total_area * 0.005:
- break
- selected_indices.append(best)
- covered_bf |= backface_vis[:, best]
- if float(areas[covered_bf].sum() / total_area) >= coverage_target:
- break
-
- if not selected_indices:
- yield 1.0
- return [], 0.0
-
- yield 0.0 # pass-1 done
-
- # ── Phase 2a: BVH validate selected set (0 % → 30 %) ─────────────
- epsilon = 0.001
- true_covered = np.zeros(len(areas), dtype=bool)
- n_sel = len(selected_indices)
- for idx, ci in enumerate(selected_indices):
- cam_dir = candidates[ci]
- for fi in range(len(normals)):
- if not backface_vis[fi, ci]:
- continue
- origin = centers[fi] + normals[fi] * epsilon
- if not _ray_occluded(bvh_trees, origin, cam_dir, 1e6):
- true_covered[fi] = True
- yield 0.3 * (idx + 1) / n_sel
-
- # ── Phase 2b: patch phantom-uncovered faces (30 % → 100 %) ───────
- phantom_uncovered = covered_bf & ~true_covered
-
- if phantom_uncovered.any():
- remaining_budget = max_cameras - len(selected_indices)
- if remaining_budget > 0:
- used_set = set(selected_indices)
- unused_mask = np.array([i not in used_set
- for i in range(n_candidates)])
- unused_indices = np.where(unused_mask)[0]
-
- if len(unused_indices) > 0:
- phantom_indices = np.where(phantom_uncovered)[0]
- ph_normals = normals[phantom_indices]
- ph_centers = centers[phantom_indices]
- ph_areas = areas[phantom_indices]
- ph_bf = ph_normals @ candidates[unused_indices].T > 0.26
-
- ph_vis = np.zeros_like(ph_bf)
- n_unused = len(unused_indices)
- BATCH = 5
- for j_local, j_global in enumerate(unused_indices):
- cam_dir = candidates[j_global]
- for i_local in range(len(phantom_indices)):
- if not ph_bf[i_local, j_local]:
- continue
- origin = (ph_centers[i_local]
- + ph_normals[i_local] * epsilon)
- if not _ray_occluded(bvh_trees, origin,
- cam_dir, 1e6):
- ph_vis[i_local, j_local] = True
- if (j_local + 1) % BATCH == 0 or j_local == n_unused - 1:
- yield 0.3 + 0.7 * (j_local + 1) / n_unused
-
- # Greedy on sub-matrix (instant)
- ph_covered = np.zeros(len(ph_areas), dtype=bool)
- for _ in range(remaining_budget):
- uncov = ~ph_covered
- nv = ph_vis & uncov[:, np.newaxis]
- na = (nv * ph_areas[:, np.newaxis]).sum(axis=0)
- best_local = int(np.argmax(na))
- if na[best_local] < total_area * 0.005:
- break
- best_global = int(unused_indices[best_local])
- selected_indices.append(best_global)
- ph_covered |= ph_vis[:, best_local]
- for i_local in range(len(phantom_indices)):
- if ph_vis[i_local, best_local]:
- true_covered[phantom_indices[i_local]] = True
-
- yield 1.0 # ensure caller sees 100 %
- selected = [candidates[ci].copy() for ci in selected_indices]
- final_cov = (float(areas[true_covered].sum() / total_area)
- if true_covered.any() else 0.0)
- return selected, final_cov
-
-
-def _sort_directions_spatially(directions, ref_direction=None):
- """Sort direction vectors by azimuth angle so cameras progress smoothly
- around the subject. The camera whose azimuth is closest to
- *ref_direction* (typically the viewport look direction) becomes the first
- entry. Important for sequential generation mode where each camera needs
- spatial context from the previous one."""
- if len(directions) <= 1:
- return directions
- # Azimuth = atan2(y, x) gives a smooth circular ordering
- angles = [math.atan2(float(d[1]), float(d[0])) for d in directions]
- paired = sorted(zip(angles, directions), key=lambda p: p[0])
-
- if ref_direction is not None:
- # Use full 3D angular distance (dot product) so elevation is
- # considered when choosing the start camera, not just azimuth.
- ref_v = mathutils.Vector(ref_direction).normalized()
- best_idx = 0
- best_dot = -2.0
- for idx, (_, d) in enumerate(paired):
- dot = mathutils.Vector(d).normalized().dot(ref_v)
- if dot > best_dot:
- best_dot = dot
- best_idx = idx
- # Rotate so closest-to-ref is first
- paired = paired[best_idx:] + paired[:best_idx]
-
- return [d for _, d in paired]
-
-
-def _classify_camera_direction(cam_dir_np, ref_front_np):
- """Classify a camera direction into a human-readable view label.
-
- Uses Option-B scheme: elevation tiers first (top/bottom if >60°),
- then 4 azimuth quadrants (front/right/left/rear, 90° each),
- with 'from above'/'from below' modifiers for 30–60° elevation.
-
- Parameters
- ----------
- cam_dir_np : array-like, shape (3,)
- Centre-to-camera unit vector (where the camera is placed relative to
- the mesh centre).
- ref_front_np : array-like, shape (3,)
- The reference "front" direction (centre-to-camera direction that
- corresponds to the user's viewport, i.e. Camera_0's neighbourhood).
-
- Returns
- -------
- str
- A prompt-friendly label such as ``"front view"``,
- ``"right side view, from above"``, ``"top view"``, etc.
- """
- d = np.array(cam_dir_np, dtype=float)
- d /= max(np.linalg.norm(d), 1e-12)
-
- # --- Elevation (angle above / below the horizontal XY plane) ---
- elevation_rad = math.asin(np.clip(d[2], -1.0, 1.0))
- elevation_deg = math.degrees(elevation_rad)
-
- if elevation_deg > 60.0:
- return "top view"
- if elevation_deg < -60.0:
- return "bottom view"
-
- # --- Azimuth relative to ref_front (projected onto XY plane) ---
- ref = np.array(ref_front_np, dtype=float)
- # Project both onto XY
- d_h = np.array([d[0], d[1]], dtype=float)
- r_h = np.array([ref[0], ref[1]], dtype=float)
- d_h_len = np.linalg.norm(d_h)
- r_h_len = np.linalg.norm(r_h)
- if d_h_len < 1e-8 or r_h_len < 1e-8:
- # Nearly vertical – should have been caught by the elevation check,
- # but fall back just in case.
- return "top view" if d[2] >= 0 else "bottom view"
- d_h /= d_h_len
- r_h /= r_h_len
-
- # Signed angle: positive = counterclockwise from above = to the user's right
- sin_a = r_h[0] * d_h[1] - r_h[1] * d_h[0]
- cos_a = r_h[0] * d_h[0] + r_h[1] * d_h[1]
- azimuth_deg = math.degrees(math.atan2(sin_a, cos_a))
-
- # Quadrant classification (each 90°)
- abs_az = abs(azimuth_deg)
- if abs_az <= 45.0:
- base = "front view"
- elif abs_az >= 135.0:
- base = "rear view"
- elif azimuth_deg > 0:
- base = "right side view"
- else:
- base = "left side view"
-
- # Elevation modifier
- if elevation_deg > 30.0:
- return f"{base}, from above"
- if elevation_deg < -30.0:
- return f"{base}, from below"
- return base
-
-
-def _compute_per_camera_aspect(direction_np, verts_world, center):
- """Compute the silhouette aspect ratio (width / height) from a single
- camera direction. Returns the aspect ratio as a float."""
- right, up, _d = _camera_basis(direction_np)
- rel = verts_world - center
- proj_r = rel @ right
- proj_u = rel @ up
- w = max(float(proj_r.max() - proj_r.min()), 0.001)
- h = max(float(proj_u.max() - proj_u.min()), 0.001)
- return w / h
-
-
-def _perspective_aspect(verts_world, cam_pos_np, cam_dir_np):
- """Compute the visible angular aspect ratio (width / height) as seen
- from a perspective camera at *cam_pos_np* looking along *cam_dir_np*."""
- right, up, d = _camera_basis(cam_dir_np)
- forward = -d
-
- rel = verts_world - cam_pos_np
- depth = rel @ forward # positive = in front of camera
- depth = np.maximum(depth, 0.001)
- pr = rel @ right
- pu = rel @ up
-
- angle_r = np.arctan2(pr, depth)
- angle_u = np.arctan2(pu, depth)
-
- angular_w = float(angle_r.max() - angle_r.min())
- angular_h = float(angle_u.max() - angle_u.min())
- if angular_h < 0.001:
- return 1.0
- return angular_w / angular_h
-
-
-def _resolution_from_aspect(aspect, total_px, align=8):
- """Compute (res_x, res_y) for a given *aspect* ratio (w/h) keeping
- approximately *total_px* pixels, snapped to *align*."""
- new_x = math.sqrt(total_px * aspect)
- new_y = total_px / new_x
- new_x = max(align, int(round(new_x / align)) * align)
- new_y = max(align, int(round(new_y / align)) * align)
- return new_x, new_y
-
-
-def _get_resolution_align(context):
- """Return the resolution alignment step: 112 for Qwen with alignment
- enabled, 8 otherwise."""
- if (context.scene.model_architecture.startswith('qwen')
- and getattr(context.scene, 'qwen_rescale_alignment', False)):
- return 112
- return 8
-
-
-def _apply_auto_aspect(directions_np, context, verts_world):
- """Adjust scene render resolution to match the mesh's average apparent
- aspect ratio across the given camera *directions_np*. This is the
- 'shared' mode — all cameras use the same resolution.
- Keeps total pixel count approximately constant and snaps to alignment step.
- Returns (new_res_x, new_res_y)."""
- center = verts_world.mean(axis=0)
- aspects = [_compute_per_camera_aspect(d, verts_world, center)
- for d in directions_np]
- avg_aspect = float(np.mean(aspects))
-
- render = context.scene.render
- total_px = render.resolution_x * render.resolution_y
- align = _get_resolution_align(context)
- new_x, new_y = _resolution_from_aspect(avg_aspect, total_px, align=align)
- render.resolution_x = new_x
- render.resolution_y = new_y
- return new_x, new_y
-
-
-def _store_per_camera_resolution(cam_obj, res_x, res_y):
- """Store per-camera resolution as custom properties on the camera object."""
- cam_obj["sg_res_x"] = res_x
- cam_obj["sg_res_y"] = res_y
-
-
-def _get_camera_resolution(cam_obj, scene):
- """Return (res_x, res_y) for a camera. Falls back to scene render
- resolution if no per-camera resolution is stored."""
- if "sg_res_x" in cam_obj and "sg_res_y" in cam_obj:
- return int(cam_obj["sg_res_x"]), int(cam_obj["sg_res_y"])
- return scene.render.resolution_x, scene.render.resolution_y
-
-
-class _SGCameraResolution:
- """Context manager: temporarily set scene render resolution to a camera's
- per-camera values (if stored), and restore the original on exit.
-
- Usage::
-
- with _SGCameraResolution(context, camera_obj):
- bpy.ops.render.render(write_still=True)
- """
- def __init__(self, context, cam_obj):
- self._render = context.scene.render
- self._cam = cam_obj
- self._scene = context.scene
- self._orig_x = self._render.resolution_x
- self._orig_y = self._render.resolution_y
-
- def __enter__(self):
- rx, ry = _get_camera_resolution(self._cam, self._scene)
- self._render.resolution_x = rx
- self._render.resolution_y = ry
- return self
-
- def __exit__(self, *exc):
- self._render.resolution_x = self._orig_x
- self._render.resolution_y = self._orig_y
- return False
-
-
-# ---- Per-camera crop overlay (GPU draw handler) -------------------------
-
-_sg_crop_draw_handle = None
-
-
-def _sg_draw_crop_overlays():
- """SpaceView3D draw callback: renders a coloured rectangle inside each
- camera's pyramid to visualise the actual (non-square) crop region."""
- context = bpy.context
- scene = context.scene
-
- # Respect Blender's overlay toggle
- space = context.space_data
- if space and hasattr(space, 'overlay') and not space.overlay.show_overlays:
- return
-
- shader = gpu.shader.from_builtin('UNIFORM_COLOR')
-
- for obj in scene.objects:
- if obj.type != 'CAMERA' or 'sg_display_crop' not in obj:
- continue
- res_x = obj.get('sg_res_x', 0)
- res_y = obj.get('sg_res_y', 0)
- if res_x <= 0 or res_y <= 0:
- continue
-
- cam = obj.data
- # Use whichever side of THIS camera is longer as the reference.
- # The scene is set to a square of max_side, so the frustum is square
- # and each side = half_w = half_h. The crop rectangle occupies
- # res_x/cam_max × res_y/cam_max of that square.
- cam_max = max(res_x, res_y)
-
- frame = cam.view_frame(scene=scene)
- corner = frame[0]
- half_w = abs(corner[0])
- half_h = abs(corner[1])
- z_depth = corner[2]
- if half_w < 1e-8 or half_h < 1e-8:
- continue
-
- sx = res_x / cam_max
- sy = res_y / cam_max
-
- new_hw = half_w * sx
- new_hh = half_h * sy
-
- crop_local = [
- mathutils.Vector((+new_hw, +new_hh, z_depth)),
- mathutils.Vector((-new_hw, +new_hh, z_depth)),
- mathutils.Vector((-new_hw, -new_hh, z_depth)),
- mathutils.Vector((+new_hw, -new_hh, z_depth)),
- ]
-
- wm = obj.matrix_world
- world_pts = [wm @ v for v in crop_local]
- coords = [(v.x, v.y, v.z) for v in world_pts]
- indices = [(0, 1), (1, 2), (2, 3), (3, 0)]
- batch = batch_for_shader(shader, 'LINES', {"pos": coords}, indices=indices)
- shader.bind()
- _prefs = bpy.context.preferences.addons.get(__package__)
- _oc = _prefs.preferences.overlay_color if _prefs else (0.3, 0.5, 1.0)
- shader.uniform_float("color", (_oc[0], _oc[1], _oc[2], 0.9))
- gpu.state.line_width_set(2.0)
- gpu.state.blend_set('ALPHA')
- gpu.state.depth_test_set('LESS_EQUAL')
- gpu.state.depth_mask_set(False)
- batch.draw(shader)
-
- gpu.state.depth_test_set('NONE')
- gpu.state.depth_mask_set(True)
- gpu.state.blend_set('NONE')
- gpu.state.line_width_set(1.0)
-
-
-# ---- Per-camera view-label overlay (GPU text handler) --------------------
-
-_sg_label_draw_handle = None
-_sg_labels_user_visible = True
-
-
-def _sg_draw_view_labels():
- """SpaceView3D POST_PIXEL callback: draws full camera prompt text
- (from ``scene.camera_prompts``) near each camera in the viewport."""
- context = bpy.context
- scene = context.scene
- region = context.region
- rv3d = context.region_data
- if region is None or rv3d is None:
- return
-
- # Respect Blender's overlay toggle
- space = context.space_data
- if space and hasattr(space, 'overlay') and not space.overlay.show_overlays:
- return
-
- from bpy_extras.view3d_utils import location_3d_to_region_2d
-
- font_id = 0
- blf.size(font_id, 13)
- _prefs = bpy.context.preferences.addons.get(__package__)
- _oc = _prefs.preferences.overlay_color if _prefs else (0.3, 0.5, 1.0)
- blf.color(font_id, _oc[0], _oc[1], _oc[2], 0.95)
-
- # Build a lookup of camera name -> prompt text
- prompt_lookup = {item.name: item.prompt for item in scene.camera_prompts
- if item.prompt}
-
- for obj in scene.objects:
- if obj.type != 'CAMERA':
- continue
- label = prompt_lookup.get(obj.name, '')
- if not label:
- continue
- co_2d = location_3d_to_region_2d(region, rv3d, obj.location)
- if co_2d is None:
- continue
- # Offset slightly below the camera marker
- blf.position(font_id, co_2d.x - blf.dimensions(font_id, label)[0] * 0.5,
- co_2d.y - 20, 0)
- gpu.state.blend_set('ALPHA')
- blf.draw(font_id, label)
-
- gpu.state.blend_set('NONE')
-
-
-def _sg_ensure_crop_overlay():
- """Register the crop overlay draw handler if not already active."""
- global _sg_crop_draw_handle
- if _sg_crop_draw_handle is None:
- _sg_crop_draw_handle = bpy.types.SpaceView3D.draw_handler_add(
- _sg_draw_crop_overlays, (), 'WINDOW', 'POST_VIEW')
-
-
-def _sg_remove_crop_overlay():
- """Remove the crop overlay draw handler if active."""
- global _sg_crop_draw_handle
- if _sg_crop_draw_handle is not None:
- bpy.types.SpaceView3D.draw_handler_remove(_sg_crop_draw_handle, 'WINDOW')
- _sg_crop_draw_handle = None
-
-
-def _sg_ensure_label_overlay():
- """Register the floating prompt-text label overlay if not already active."""
- global _sg_label_draw_handle, _sg_labels_user_visible
- _sg_labels_user_visible = True
- if _sg_label_draw_handle is None:
- _sg_label_draw_handle = bpy.types.SpaceView3D.draw_handler_add(
- _sg_draw_view_labels, (), 'WINDOW', 'POST_PIXEL')
- # Redraw all viewports so labels appear immediately
- for area in (bpy.context.screen.areas if bpy.context.screen else []):
- if area.type == 'VIEW_3D':
- area.tag_redraw()
-
-
-def _sg_remove_label_overlay():
- """Remove the floating prompt-text label overlay if active."""
- global _sg_label_draw_handle, _sg_labels_user_visible
- _sg_labels_user_visible = False
- if _sg_label_draw_handle is not None:
- bpy.types.SpaceView3D.draw_handler_remove(_sg_label_draw_handle, 'WINDOW')
- _sg_label_draw_handle = None
- for area in (bpy.context.screen.areas if bpy.context.screen else []):
- if area.type == 'VIEW_3D':
- area.tag_redraw()
-
-
-def _sg_hide_label_overlay():
- """Temporarily hide the label overlay without changing the user's preference."""
- global _sg_label_draw_handle
- if _sg_label_draw_handle is not None:
- bpy.types.SpaceView3D.draw_handler_remove(_sg_label_draw_handle, 'WINDOW')
- _sg_label_draw_handle = None
- for area in (bpy.context.screen.areas if bpy.context.screen else []):
- if area.type == 'VIEW_3D':
- area.tag_redraw()
-
-
-def _sg_restore_label_overlay():
- """Restore the label overlay only if the user had it enabled."""
- if _sg_labels_user_visible:
- global _sg_label_draw_handle
- if _sg_label_draw_handle is None:
- _sg_label_draw_handle = bpy.types.SpaceView3D.draw_handler_add(
- _sg_draw_view_labels, (), 'WINDOW', 'POST_PIXEL')
- for area in (bpy.context.screen.areas if bpy.context.screen else []):
- if area.type == 'VIEW_3D':
- area.tag_redraw()
-
-
-def _sg_restore_square_display(scene):
- """Restore scene resolution to the max-side square and tag viewports
- for redraw so crop overlays reappear correctly."""
- cameras = [o for o in scene.objects if o.type == 'CAMERA' and 'sg_res_x' in o]
- if not cameras:
- return
- max_side = max(
- max(int(c.get('sg_res_x', 0)), int(c.get('sg_res_y', 0)))
- for c in cameras
- )
- if max_side > 0:
- scene.render.resolution_x = max_side
- scene.render.resolution_y = max_side
- # Refresh viewports
- for area in bpy.context.screen.areas if bpy.context.screen else []:
- if area.type == 'VIEW_3D':
- area.tag_redraw()
-
-
-def _setup_square_camera_display(cam_obj, res_x, res_y):
- """Mark a camera for the crop overlay and enable passepartout."""
- cam_data = cam_obj.data
- cam_data.show_passepartout = True
- cam_data.passepartout_alpha = 0.5
- cam_obj["sg_display_crop"] = True
- # Ensure the draw handler is alive
- _sg_ensure_crop_overlay()
-
-
-def _get_fov(cam_settings, context, res_x=None, res_y=None):
- """Return (fov_x, fov_y) in radians for the given camera data block.
- If *res_x* / *res_y* are not provided, reads from scene render settings."""
- fov_x = cam_settings.angle_x
- if res_x is None:
- res_x = context.scene.render.resolution_x
- if res_y is None:
- res_y = context.scene.render.resolution_y
- if res_y > res_x:
- fov_x = 2 * math.atan(math.tan(fov_x / 2) * res_x / res_y)
- fov_y = 2 * math.atan(math.tan(fov_x / 2) * res_y / res_x)
- return fov_x, fov_y
-
-
-class AddCameras(bpy.types.Operator):
- """Add cameras using various placement strategies and adjust their positions
-
- Uses the active camera as a reference for settings. If there is no active camera, a new one is created based on the viewport.
-
- Placement modes:
- - Orbit Ring: cameras in a circle (inherits elevation from reference camera)
- - Sphere Coverage: Fibonacci spiral for even sphere distribution
- - Auto (Normal-Weighted): K-means clustering of mesh face normals
- - Auto (PCA Axes): cameras along the mesh's principal component axes
- - Auto (Greedy Coverage): iteratively adds cameras that maximise new visible surface area and auto-determines the count
- - Fan from Camera: arc of cameras near the active camera
-
- Tips:
- - Try to frame the object / scene with minimal margin around it.
- - Aim to achieve a uniform coverage of the object / scene.
- - Areas not visible from any camera won't get textured. (Can still be UV-inpainted)
- - Aspect ratio is set by Blender's output settings (or auto-computed)."""
- bl_category = "ControlNet"
- bl_idname = "object.add_cameras"
- bl_label = "Add Cameras"
- bl_options = {'REGISTER', 'UNDO'}
-
- placement_mode: bpy.props.EnumProperty(
- name="Placement Mode",
- description="Strategy for placing cameras around the subject",
- items=[
- ('orbit_ring', "Orbit Ring", "Place cameras in a circle around the center (original behaviour). Inherits elevation from the reference camera or viewport"),
- ('hemisphere', "Sphere Coverage", "Distribute cameras evenly across a sphere using a Fibonacci spiral"),
- ('normal_weighted', "Auto (Normal-Weighted)", "Automatically place cameras to cover the most surface area, using K-means on face normals weighted by area"),
- ('pca_axes', "Auto (PCA Axes)", "Place cameras along the mesh's principal axes of variation"),
- ('greedy_coverage', "Auto (Greedy Coverage)", "Iteratively add cameras that maximise new visible surface. Automatically determines the number of cameras needed"),
- ('fan_from_camera', "Fan from Camera", "Spread cameras in an arc around the active camera's orbit position"),
- ],
- default='normal_weighted'
- ) # type: ignore
-
- num_cameras: bpy.props.IntProperty(
- name="Number of Cameras",
- description="Number of cameras to add (not used by Greedy Coverage which auto-determines count)",
- default=4,
- min=1,
- max=100
- ) # type: ignore
-
- center_type: bpy.props.EnumProperty(
- name="Center Type",
- description="Type of center for the cameras",
- items=[
- ('object', "Object", "Use the active object as the center"),
- ('view center', "View Center", "Use the view center as the center"),
- ],
- default='object'
- ) # type: ignore
-
- purge_others: bpy.props.BoolProperty(
- name="Remove Existing Cameras",
- description="Delete ALL existing cameras (including active) before adding new ones",
- default=True
- ) # type: ignore
-
- consider_existing: bpy.props.BoolProperty(
- name="Consider Existing Cameras",
- description="Treat existing cameras as already-placed directions so auto modes avoid duplicating their coverage",
- default=True
- ) # type: ignore
-
- fan_angle: bpy.props.FloatProperty(
- name="Fan Angle",
- description="Total angular spread of the fan in degrees",
- default=90.0,
- min=10.0,
- max=350.0
- ) # type: ignore
-
- coverage_target: bpy.props.FloatProperty(
- name="Coverage Target",
- description="Stop adding cameras when this fraction of surface area is visible (Greedy mode)",
- default=0.95,
- min=0.5,
- max=1.0,
- subtype='FACTOR'
- ) # type: ignore
-
- max_auto_cameras: bpy.props.IntProperty(
- name="Max Cameras",
- description="Upper limit on cameras for Greedy Coverage mode",
- default=12,
- min=2,
- max=50
- ) # type: ignore
-
- auto_aspect: bpy.props.EnumProperty(
- name="Auto Aspect Ratio",
- description="Automatically adjust render aspect ratio to match the mesh silhouette",
- items=[
- ('off', "Off", "Use current scene resolution for all cameras"),
- ('shared', "Shared", "Average silhouette aspect across all cameras and set a single scene resolution"),
- ('per_camera', "Per Camera", "Each camera gets its own optimal aspect ratio (resolution is swapped during generation)"),
- ],
- default='per_camera'
- ) # type: ignore
-
- exclude_bottom: bpy.props.BoolProperty(
- name="Exclude Bottom Faces",
- description="Ignore downward-facing geometry (e.g. flat building undersides) when placing cameras",
- default=True
- ) # type: ignore
-
- exclude_bottom_angle: bpy.props.FloatProperty(
- name="Bottom Angle Threshold",
- description="Faces whose normal points more than this many degrees below the horizon are excluded",
- default=1.5533, # 89 degrees in radians
- min=0.1745, # 10 degrees
- max=1.5708, # 90 degrees
- subtype='ANGLE',
- unit='ROTATION'
- ) # type: ignore
-
- auto_prompts: bpy.props.BoolProperty(
- name="Auto View Prompts",
- description="Automatically generate view-direction prompts (e.g. 'front view', 'rear view, from above') for each camera based on the viewport reference orientation",
- default=False
- ) # type: ignore
-
- review_placement: bpy.props.BoolProperty(
- name="Review Camera Placement",
- description="After placing cameras, fly through each one for review. "
- "When disabled the cameras are created immediately without the interactive fly-through",
- default=True
- ) # type: ignore
-
- occlusion_mode: bpy.props.EnumProperty(
- name="Occlusion Handling",
- description="How to account for self-occlusion when choosing camera directions",
- items=[
- ('none', "None (Fast)",
- "Back-face culling only – ignores self-occlusion. Fastest option"),
- ('full_matrix', "Full Occlusion Matrix",
- "Build a complete BVH-validated visibility matrix before greedy selection. Most accurate but slower"),
- ('two_pass', "Two-Pass Refinement",
- "Fast back-face pass, then targeted BVH refinement only for faces with zero true coverage"),
- ('vis_weighted', "Visibility-Weighted",
- "Weight faces by their visibility fraction from 200 directions. "
- "Mostly-occluded faces have reduced influence on camera placement (linear). "
- "Only affects Normal-Weighted mode; other modes fall back to Full Occlusion Matrix"),
- ('vis_interactive', "Interactive Visibility",
- "Like Visibility-Weighted but with a real-time preview: scroll to adjust "
- "the occlusion balance and see cameras reposition instantly. "
- "Only affects Normal-Weighted mode; other modes fall back to Full Occlusion Matrix"),
- ],
- default='none'
- ) # type: ignore
-
- clamp_elevation: bpy.props.BoolProperty(
- name="Clamp Elevation",
- description="Restrict camera elevation to avoid extreme top-down or bottom-up views "
- "that diffusion models often struggle with",
- default=False
- ) # type: ignore
-
- max_elevation_angle: bpy.props.FloatProperty(
- name="Max Elevation",
- description="Maximum upward elevation angle (degrees above horizon). "
- "Cameras looking further up will be clamped to this angle",
- default=1.2217, # 70 degrees in radians
- min=0.0,
- max=1.5708, # 90 degrees
- subtype='ANGLE',
- unit='ROTATION'
- ) # type: ignore
-
- min_elevation_angle: bpy.props.FloatProperty(
- name="Min Elevation",
- description="Minimum downward elevation angle (degrees below horizon). "
- "Cameras looking further down will be clamped to this angle",
- default=-0.1745, # -10 degrees in radians
- min=-1.5708, # -90 degrees
- max=0.0,
- subtype='ANGLE',
- unit='ROTATION'
- ) # type: ignore
-
- _timer = None
- _camera_index = 0
- _cameras = []
- _initial_camera = None
- _draw_handle = None
- # Occlusion modal state
- _occ_phase = False
- _occ_gen = None
- _occ_progress = 0.0
- _occ_state = None
- # Interactive visibility preview state
- _vis_preview_phase = False
- _vis_count = None
- _vis_n_candidates = 200
- _vis_balance = 0.2
- _vis_state = None
- _vis_directions = None
-
- def draw(self, context):
- """Custom dialog layout for the placement mode selector."""
- layout = self.layout
- layout.prop(self, "placement_mode")
- layout.separator()
- is_auto = self.placement_mode in ('hemisphere', 'normal_weighted', 'pca_axes', 'greedy_coverage')
- if self.placement_mode == 'greedy_coverage':
- layout.prop(self, "coverage_target")
- layout.prop(self, "max_auto_cameras")
- else:
- layout.prop(self, "num_cameras")
- if not is_auto:
- layout.prop(self, "center_type")
- if self.placement_mode == 'fan_from_camera':
- layout.prop(self, "fan_angle")
- if is_auto:
- if self.placement_mode != 'hemisphere':
- layout.prop(self, "occlusion_mode")
- layout.prop(self, "auto_aspect")
- layout.prop(self, "exclude_bottom")
- if self.exclude_bottom:
- layout.prop(self, "exclude_bottom_angle")
- layout.prop(self, "auto_prompts")
- layout.prop(self, "clamp_elevation")
- if self.clamp_elevation:
- row = layout.row(align=True)
- row.prop(self, "min_elevation_angle", text="Min")
- row.prop(self, "max_elevation_angle", text="Max")
- layout.prop(self, "review_placement")
- layout.prop(self, "purge_others")
- if not self.purge_others and is_auto:
- layout.prop(self, "consider_existing")
-
- def draw_callback(self, context):
- # Guard against operator being freed while draw handler is still registered
- try:
- _ = self._occ_phase # probe attribute access
- except ReferenceError:
- # Operator freed — remove the dangling draw handler
- if AddCameras._draw_handle:
- bpy.types.SpaceView3D.draw_handler_remove(
- AddCameras._draw_handle, 'WINDOW')
- AddCameras._draw_handle = None
- return
-
- # ── Occlusion progress display ───────────────────────────────────
- if self._occ_phase:
- font_id = 0
- region = context.region
- rw, rh = region.width, region.height
- pct = self._occ_progress
-
- # Progress bar background (dark)
- bar_w, bar_h = 300, 18
- bar_x = (rw - bar_w) / 2
- bar_y = rh * 0.10
-
- # Draw progress text above bar
- msg = f"Computing occlusion… {pct * 100:.0f}%"
- blf.size(font_id, 18)
- tw, _th = blf.dimensions(font_id, msg)
- blf.position(font_id, (rw - tw) / 2, bar_y + bar_h + 6, 0)
- blf.color(font_id, 1.0, 0.85, 0.35, 0.95)
- blf.draw(font_id, msg)
-
- hint = "Press ESC to cancel"
- blf.size(font_id, 13)
- hw, _hh = blf.dimensions(font_id, hint)
- blf.position(font_id, (rw - hw) / 2, bar_y - 18, 0)
- blf.color(font_id, 0.7, 0.7, 0.7, 0.8)
- blf.draw(font_id, hint)
- blf.color(font_id, 1.0, 1.0, 1.0, 1.0)
- return
-
- # ── Interactive visibility preview HUD ───────────────────────────
- if self._vis_preview_phase:
- font_id = 0
- region = context.region
- rw, rh = region.width, region.height
- balance = self._vis_balance
-
- msg = f"Occlusion Balance: {balance:.0%}"
- blf.size(font_id, 22)
- tw, _th = blf.dimensions(font_id, msg)
- blf.position(font_id, (rw - tw) / 2, rh * 0.13, 0)
- blf.color(font_id, 1.0, 0.85, 0.35, 0.95)
- blf.draw(font_id, msg)
-
- n_cams = len(self._cameras) if self._cameras else 0
- info = f"{n_cams} cameras"
- blf.size(font_id, 16)
- iw, _ih = blf.dimensions(font_id, info)
- blf.position(font_id, (rw - iw) / 2, rh * 0.13 - 26, 0)
- blf.color(font_id, 0.9, 0.9, 0.9, 0.9)
- blf.draw(font_id, info)
-
- hint = "Scroll \u2191\u2193 to adjust | ENTER to confirm | ESC to cancel"
- blf.size(font_id, 13)
- hw, _hh = blf.dimensions(font_id, hint)
- blf.position(font_id, (rw - hw) / 2, rh * 0.13 - 48, 0)
- blf.color(font_id, 0.7, 0.7, 0.7, 0.8)
- blf.draw(font_id, hint)
- blf.color(font_id, 1.0, 1.0, 1.0, 1.0)
- return
-
- # ── Fly-through HUD ──────────────────────────────────────────────
- try:
- count = len(self._cameras)
- idx = self._camera_index
- except Exception:
- return
- if count == 0:
- return
- font_id = 0
- region = context.region
- rw, rh = region.width, region.height
-
- msg = f"Camera: {idx+1}/{count} | Press SPACE to confirm"
- blf.size(font_id, 20)
- text_width, text_height = blf.dimensions(font_id, msg)
- x = (rw - text_width) / 2
- y = rh * 0.10
- blf.position(font_id, x, y, 0)
- blf.draw(font_id, msg)
-
- # Show auto-generated view label below the main HUD line
- if idx < count:
- cam_obj = self._cameras[idx]
- view_label = cam_obj.get('sg_view_label', '')
- if view_label:
- blf.size(font_id, 16)
- blf.color(font_id, 1.0, 0.85, 0.35, 0.95)
- lw, _lh = blf.dimensions(font_id, view_label)
- blf.position(font_id, (rw - lw) / 2, y - 28, 0)
- blf.draw(font_id, view_label)
- blf.color(font_id, 1.0, 1.0, 1.0, 1.0)
-
- def execute(self, context):
- # --- Delete existing cameras if requested ---
- if self.purge_others:
- scene = context.scene
- to_remove = [obj for obj in bpy.data.objects if obj.type == 'CAMERA']
- for cam in to_remove:
- for col in list(cam.users_collection):
- col.objects.unlink(cam)
- bpy.data.objects.remove(cam, do_unlink=True)
- for cam_data in list(bpy.data.cameras):
- if not cam_data.users:
- bpy.data.cameras.remove(cam_data)
- scene.camera = None
-
- # --- Validate mesh requirement for mesh-based modes ---
- if self.placement_mode in ('hemisphere', 'normal_weighted', 'pca_axes', 'greedy_coverage'):
- target_meshes = _gather_target_meshes(context)
- if not target_meshes:
- self.report({'ERROR'}, "No mesh objects found for this placement mode. Select meshes or ensure the scene has meshes.")
- return {'CANCELLED'}
- total_faces = sum(len(o.data.polygons) for o in target_meshes)
- if total_faces == 0:
- self.report({'ERROR'}, "Target meshes have no faces.")
- return {'CANCELLED'}
-
- # --- Fallback for center_type ---
- if self.center_type == 'object':
- obj = context.object
- if not obj:
- self.report({'WARNING'}, "No active object found. Using view center instead.")
- self.center_type = 'view center'
-
- # --- Add draw handler ---
- if AddCameras._draw_handle is None:
- AddCameras._draw_handle = bpy.types.SpaceView3D.draw_handler_add(
- self.draw_callback, (context,), 'WINDOW', 'POST_PIXEL')
-
- # --- Determine if this is a "manual" mode (needs ref camera for orbit)
- # or an "auto" mode (computes its own positions) ---
- is_auto_mode = self.placement_mode in ('hemisphere', 'normal_weighted', 'pca_axes', 'greedy_coverage')
-
- # --- Determine center location ---
- # Auto modes compute their own center from mesh vertices later, so
- # only the manual modes (orbit_ring, fan_from_camera) need this.
- if is_auto_mode:
- # Placeholder – auto branch overrides center_location from verts
- rv3d = getattr(context, 'region_data', None)
- if rv3d is not None:
- center_location = rv3d.view_location.copy()
- else:
- center_location = mathutils.Vector((0.0, 0.0, 0.0))
- elif self.center_type == 'object':
- obj = context.object
- cursor_loc = context.scene.cursor.location.copy()
- context.scene.cursor.location = obj.location.copy()
- bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS')
- center_location = obj.location.copy()
- bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
- context.scene.cursor.location = cursor_loc
- center_location = obj.matrix_world.translation + obj.matrix_world.to_3x3() @ (center_location - obj.matrix_world.translation)
- else:
- rv3d = getattr(context, 'region_data', None)
- if rv3d is not None:
- center_location = rv3d.view_location.copy()
- else:
- center_location = mathutils.Vector((0.0, 0.0, 0.0))
-
- # --- Set or create reference camera ---
- self._initial_camera = context.scene.camera
- using_viewport_ref = False
- if not self._initial_camera and not is_auto_mode:
- # Manual modes: create a camera from the viewport as Camera_0
- rv3d = getattr(context, 'region_data', None)
- if rv3d is None:
- self.report({'ERROR'}, "No 3D viewport available for camera placement")
- return {'CANCELLED'}
- cam_data = bpy.data.cameras.new(name='Camera_0')
- cam_obj = bpy.data.objects.new('Camera_0', cam_data)
- context.collection.objects.link(cam_obj)
- cam_obj.matrix_world = rv3d.view_matrix.inverted()
- context.scene.camera = cam_obj
- self._initial_camera = cam_obj
- using_viewport_ref = True
-
- # --- Capture reference camera settings (for lens/sensor/clip copy) ---
- cam_settings = self._initial_camera.data if self._initial_camera else None
-
- self._cameras.clear()
- self._camera_index = 0
-
- # --- Branch by placement mode ---
- if self.placement_mode == 'orbit_ring':
- ref_mat = self._initial_camera.matrix_world.copy()
- initial_pos = self._initial_camera.location.copy()
- radius = (initial_pos - center_location).length
- if radius < 0.001:
- radius = 5.0
- self._cameras.append(self._initial_camera)
- self._place_orbit_ring(context, center_location, ref_mat, initial_pos, cam_settings, radius, using_viewport_ref)
- elif self.placement_mode == 'fan_from_camera':
- ref_mat = self._initial_camera.matrix_world.copy()
- initial_pos = self._initial_camera.location.copy()
- radius = (initial_pos - center_location).length
- if radius < 0.001:
- radius = 5.0
- self._cameras.append(self._initial_camera)
- self._place_fan(context, center_location, ref_mat, initial_pos, cam_settings, radius, using_viewport_ref)
- else:
- # ============================================================
- # Auto modes: per-camera optimal distance from mesh silhouette
- # ============================================================
- # target_meshes was validated above
-
- # Borrow settings from existing camera or create temp defaults
- temp_cam_data = None
- if not cam_settings:
- temp_cam_data = bpy.data.cameras.new(name='_sg_temp_cam')
- cam_settings = temp_cam_data
-
- # Pre-compute combined mesh data across all target meshes
- verts_world = _get_mesh_verts_world(target_meshes)
- mesh_center = verts_world.mean(axis=0)
- center_location = mathutils.Vector(mesh_center.tolist())
- mesh_names = ', '.join(o.name for o in target_meshes)
- self.report({'INFO'}, f"Target meshes ({len(target_meshes)}): {mesh_names}")
-
- # --- Collect existing camera directions (for "consider existing") ---
- existing_dirs = None
- if not self.purge_others and self.consider_existing:
- existing_dirs = _existing_camera_directions(mesh_center)
- if existing_dirs:
- self.report({'INFO'},
- f"Considering {len(existing_dirs)} existing camera(s)")
-
- # --- Determine directions ---
- if self.placement_mode == 'greedy_coverage':
- normals, areas, centers = _get_mesh_face_data(target_meshes)
- if self.exclude_bottom:
- normals, areas, centers = _filter_bottom_faces(
- normals, areas, centers, self.exclude_bottom_angle)
-
- occ = self.occlusion_mode
- if occ == 'none':
- directions, coverage = _greedy_coverage_directions(
- normals, areas,
- max_cameras=self.max_auto_cameras,
- coverage_target=self.coverage_target,
- existing_dirs=existing_dirs)
- directions = [np.array(d) for d in directions]
- self.report({'INFO'},
- f"Greedy coverage (no occlusion): "
- f"{len(directions)} cameras, {coverage*100:.1f}% coverage")
- else:
- # Greedy doesn't use K-means; vis modes fall back to FOM
- eff_occ = occ if occ in ('full_matrix', 'two_pass') else 'full_matrix'
- # ── Async occlusion via modal ────────────────────────
- depsgraph = context.evaluated_depsgraph_get()
- bvh_trees = _build_bvh_trees(target_meshes, depsgraph)
- gen_func = (_occ_fom_generator if eff_occ == 'full_matrix'
- else _occ_tpr_generator)
- self._occ_gen = gen_func(
- normals, areas, centers, bvh_trees,
- max_cameras=self.max_auto_cameras,
- coverage_target=self.coverage_target,
- existing_dirs=existing_dirs)
- self._occ_phase = True
- self._occ_progress = 0.0
- self._occ_state = {
- 'result_type': 'greedy',
- 'verts_world': verts_world,
- 'mesh_center': mesh_center,
- 'cam_settings': cam_settings,
- 'temp_cam_data': temp_cam_data,
- 'occ_label': ('full occlusion' if occ == 'full_matrix'
- else 'two-pass'),
- }
- context.window_manager.modal_handler_add(self)
- self._timer = context.window_manager.event_timer_add(
- 0.01, window=context.window)
- return {'RUNNING_MODAL'}
- elif self.placement_mode == 'hemisphere':
- points = _fibonacci_sphere_points(self.num_cameras)
- directions = [np.array(p) for p in points]
- if self.exclude_bottom:
- # Remove directions that point more than the threshold below the horizon
- threshold_z = -math.cos(self.exclude_bottom_angle)
- directions = [d for d in directions if d[2] >= threshold_z]
- if existing_dirs:
- directions = _filter_near_existing(directions, existing_dirs)
- elif self.placement_mode == 'normal_weighted':
- normals, areas, centers = _get_mesh_face_data(target_meshes)
- if self.exclude_bottom:
- normals, areas, centers = _filter_bottom_faces(
- normals, areas, centers, self.exclude_bottom_angle)
-
- occ = self.occlusion_mode
- if occ in ('vis_weighted', 'vis_interactive'):
- # Visibility-count generator (shared by both modes)
- depsgraph = context.evaluated_depsgraph_get()
- bvh_trees = _build_bvh_trees(target_meshes, depsgraph)
- self._occ_gen = _occ_vis_count_generator(
- normals, areas, centers, bvh_trees)
- self._occ_phase = True
- self._occ_progress = 0.0
- rt = 'vis_kmeans' if occ == 'vis_weighted' else 'vis_interactive'
- self._occ_state = {
- 'result_type': rt,
- 'normals': normals,
- 'areas': areas,
- 'centers': centers,
- 'num_cameras': self.num_cameras,
- 'verts_world': verts_world,
- 'mesh_center': mesh_center,
- 'cam_settings': cam_settings,
- 'temp_cam_data': temp_cam_data,
- 'existing_dirs': existing_dirs,
- }
- context.window_manager.modal_handler_add(self)
- self._timer = context.window_manager.event_timer_add(
- 0.01, window=context.window)
- return {'RUNNING_MODAL'}
- elif occ != 'none':
- depsgraph = context.evaluated_depsgraph_get()
- bvh_trees = _build_bvh_trees(target_meshes, depsgraph)
- self._occ_gen = _occ_filter_faces_generator(
- normals, areas, centers, bvh_trees)
- self._occ_phase = True
- self._occ_progress = 0.0
- self._occ_state = {
- 'result_type': 'filter_kmeans',
- 'normals': normals,
- 'areas': areas,
- 'centers': centers,
- 'num_cameras': self.num_cameras,
- 'verts_world': verts_world,
- 'mesh_center': mesh_center,
- 'cam_settings': cam_settings,
- 'temp_cam_data': temp_cam_data,
- 'existing_dirs': existing_dirs,
- }
- context.window_manager.modal_handler_add(self)
- self._timer = context.window_manager.event_timer_add(
- 0.01, window=context.window)
- return {'RUNNING_MODAL'}
- else:
- k = min(self.num_cameras, len(normals))
- cluster_dirs = _kmeans_on_sphere(normals, areas, k)
- directions = [cluster_dirs[i] for i in range(len(cluster_dirs))]
- if existing_dirs:
- directions = _filter_near_existing(directions, existing_dirs)
- elif self.placement_mode == 'pca_axes':
- normals, areas, centers = _get_mesh_face_data(target_meshes)
- if self.exclude_bottom:
- normals, areas, centers = _filter_bottom_faces(
- normals, areas, centers, self.exclude_bottom_angle)
-
- occ = self.occlusion_mode
- # PCA doesn't use K-means; vis modes fall back to filter
- if occ not in ('none',):
- depsgraph = context.evaluated_depsgraph_get()
- bvh_trees = _build_bvh_trees(target_meshes, depsgraph)
- self._occ_gen = _occ_filter_faces_generator(
- normals, areas, centers, bvh_trees)
- self._occ_phase = True
- self._occ_progress = 0.0
- self._occ_state = {
- 'result_type': 'filter_pca',
- 'normals': normals,
- 'areas': areas,
- 'centers': centers,
- 'num_cameras': self.num_cameras,
- 'exclude_bottom': self.exclude_bottom,
- 'exclude_bottom_angle': self.exclude_bottom_angle,
- 'verts_world': verts_world,
- 'mesh_center': mesh_center,
- 'cam_settings': cam_settings,
- 'temp_cam_data': temp_cam_data,
- 'existing_dirs': existing_dirs,
- }
- context.window_manager.modal_handler_add(self)
- self._timer = context.window_manager.event_timer_add(
- 0.01, window=context.window)
- return {'RUNNING_MODAL'}
- else:
- axes = _compute_pca_axes(verts_world)
- directions = []
- for axis in axes:
- directions.append(axis)
- directions.append(-axis)
- if self.exclude_bottom:
- threshold_z = -math.cos(self.exclude_bottom_angle)
- directions = [d for d in directions if d[2] >= threshold_z]
- directions = directions[:min(self.num_cameras, len(directions))]
- if existing_dirs:
- directions = _filter_near_existing(directions, existing_dirs)
-
- # Finalize: sort, aspect ratio, camera creation, auto prompts
- self._finalize_auto_cameras(
- context, directions, verts_world, mesh_center,
- cam_settings, temp_cam_data)
-
- # --- Start fly-through review ---
- if self.review_placement:
- if not self._start_fly_review(context):
- return {'CANCELLED'}
- return {'RUNNING_MODAL'}
- else:
- # Skip review — just finish immediately
- self._finish_without_review(context)
- return {'FINISHED'}
-
- # -------------------------------------------------------
- # Auto-mode finalization helpers
- # -------------------------------------------------------
-
- def _finalize_auto_cameras(self, context, directions, verts_world,
- mesh_center, cam_settings, temp_cam_data):
- """Sort directions, compute aspect ratios, create cameras, and
- generate auto prompts. Shared by both the synchronous (no-occlusion)
- and asynchronous (modal occlusion) code paths."""
- ref_dir = None
- if directions:
- rv3d = context.region_data
- if rv3d:
- view_dir = rv3d.view_rotation @ mathutils.Vector((0, 0, -1))
- ref_dir = np.array([-view_dir.x, -view_dir.y, -view_dir.z])
- directions = _sort_directions_spatially(directions, ref_dir)
-
- # --- Clamp elevation angles to avoid extreme top/bottom views ---
- if self.clamp_elevation and directions:
- min_rad = self.min_elevation_angle
- max_rad = self.max_elevation_angle
- clamped = []
- for d in directions:
- d_np = np.array(d, dtype=float)
- norm = np.linalg.norm(d_np)
- if norm < 1e-8:
- continue
- d_np /= norm
- elev = math.asin(float(np.clip(d_np[2], -1.0, 1.0)))
- if min_rad <= elev <= max_rad:
- clamped.append(d_np)
- continue
- elev = max(min_rad, min(max_rad, elev))
- horiz = math.sqrt(float(d_np[0]**2 + d_np[1]**2))
- if horiz < 1e-8:
- # Pure vertical direction – pick an arbitrary azimuth
- azimuth = 0.0
- else:
- azimuth = math.atan2(float(d_np[1]), float(d_np[0]))
- cos_e = math.cos(elev)
- d_np = np.array([cos_e * math.cos(azimuth),
- cos_e * math.sin(azimuth),
- math.sin(elev)])
- clamped.append(d_np)
-
- # Deduplicate near-identical directions that may have been created
- # by clamping several cameras to the same elevation limit.
- _MERGE_COS = math.cos(math.radians(8.0)) # merge within 8°
- deduped = []
- for d in clamped:
- if all(float(np.dot(d, existing)) < _MERGE_COS
- for existing in deduped):
- deduped.append(d)
- n_before = len(directions)
- directions = deduped
- if len(directions) < n_before:
- self.report({'INFO'},
- f"Elevation clamp: {n_before} → {len(directions)} cameras "
- f"(merged {n_before - len(directions)} near-duplicates)")
-
- render = context.scene.render
- total_px = render.resolution_x * render.resolution_y
- center_np_for_aspect = verts_world.mean(axis=0)
-
- if self.auto_aspect == 'shared' and directions:
- dirs_np = [d / np.linalg.norm(d) for d in directions]
- old_x, old_y = render.resolution_x, render.resolution_y
- new_x, new_y = _apply_auto_aspect(dirs_np, context, verts_world)
- if (new_x, new_y) != (old_x, old_y):
- self.report({'INFO'},
- f"Aspect ratio adjusted: {old_x}x{old_y} → {new_x}x{new_y}")
-
- if self.auto_aspect == 'per_camera' and directions:
- self._create_cameras_per_aspect(
- context, directions, mesh_center, verts_world,
- cam_settings, total_px, center_np_for_aspect)
- else:
- fov_x, fov_y = _get_fov(cam_settings, context)
- self._create_cameras_from_directions(
- context, directions, mesh_center, verts_world,
- cam_settings, fov_x, fov_y)
-
- if temp_cam_data:
- bpy.data.cameras.remove(temp_cam_data)
-
- if self.auto_prompts and self._cameras:
- ref_front = (ref_dir if ref_dir is not None
- else np.array([0.0, 1.0, 0.0]))
- mesh_center_np = np.array(mesh_center, dtype=float)
- for cam_obj in self._cameras:
- cam_pos = np.array(cam_obj.location, dtype=float)
- cam_dir = cam_pos - mesh_center_np
- label = _classify_camera_direction(cam_dir, ref_front)
- cam_obj["sg_view_label"] = label
- prompt_item = next(
- (item for item in context.scene.camera_prompts
- if item.name == cam_obj.name), None)
- if not prompt_item:
- prompt_item = context.scene.camera_prompts.add()
- prompt_item.name = cam_obj.name
- prompt_item.prompt = label
- context.scene.use_camera_prompts = True
- self.report({'INFO'},
- f"Auto-prompts: assigned view labels to "
- f"{len(self._cameras)} cameras")
- elif not self.auto_prompts:
- # Clear any pre-existing auto-prompts so stale labels don't persist
- context.scene.camera_prompts.clear()
- context.scene.use_camera_prompts = False
- for cam_obj in self._cameras:
- if 'sg_view_label' in cam_obj:
- del cam_obj['sg_view_label']
- _sg_remove_label_overlay()
-
- def _update_vis_cameras(self, context):
- """Regenerate cameras based on current visibility balance setting."""
- # Remove existing preview cameras
- for cam in list(self._cameras):
- cam_data = cam.data
- bpy.data.objects.remove(cam, do_unlink=True)
- if cam_data and not cam_data.users:
- bpy.data.cameras.remove(cam_data)
- self._cameras.clear()
-
- state = self._vis_state
- vis_count = self._vis_count
- n_cand = self._vis_n_candidates
- vis_fraction = vis_count.astype(float) / n_cand
- balance = self._vis_balance
-
- exterior = vis_count > 0
- normals = state['normals'][exterior]
- areas_base = state['areas'][exterior]
- vf = vis_fraction[exterior]
- # weight = area × (vis_fraction + balance × (1 - vis_fraction))
- # balance=0: weight = area × vis_fraction (full vis-weighting)
- # balance=1: weight = area (no vis-weighting, all exterior equal)
- weighted_areas = areas_base * (vf + balance * (1.0 - vf))
-
- existing_dirs = state.get('existing_dirs')
- k = min(state['num_cameras'], len(normals))
- if k > 0 and len(normals) > 0:
- cluster_dirs = _kmeans_on_sphere(normals, weighted_areas, k)
- directions = [cluster_dirs[i] for i in range(len(cluster_dirs))]
- if existing_dirs:
- directions = _filter_near_existing(directions, existing_dirs)
- else:
- directions = []
-
- # Sort directions
- ref_dir = None
- rv3d = context.region_data
- if rv3d:
- view_dir = rv3d.view_rotation @ mathutils.Vector((0, 0, -1))
- ref_dir = np.array([-view_dir.x, -view_dir.y, -view_dir.z])
- directions = _sort_directions_spatially(directions, ref_dir)
- self._vis_directions = directions
-
- # Quick camera creation (without full finalize overhead)
- if directions:
- cam_settings = state['cam_settings']
- fov_x, fov_y = _get_fov(cam_settings, context)
- self._create_cameras_from_directions(
- context, directions, state['mesh_center'],
- state['verts_world'], cam_settings, fov_x, fov_y)
- if self._cameras:
- context.scene.camera = self._cameras[0]
-
- n_ext = int(exterior.sum())
- context.area.header_text_set(
- f"Occlusion Balance: {balance:.0%} | "
- f"{len(directions)} cameras, {n_ext} visible faces | "
- f"Scroll to adjust | ENTER to confirm | ESC to cancel")
-
- def _cleanup_vis_preview(self, context):
- """Clean up interactive visibility preview state."""
- # Delete preview cameras
- for cam in list(self._cameras):
- cam_data = cam.data
- bpy.data.objects.remove(cam, do_unlink=True)
- if cam_data and not cam_data.users:
- bpy.data.cameras.remove(cam_data)
- self._cameras.clear()
-
- # Clean up temp camera data
- state = self._vis_state
- if state and state.get('temp_cam_data'):
- bpy.data.cameras.remove(state['temp_cam_data'])
-
- self._vis_preview_phase = False
- self._vis_state = None
- self._vis_count = None
- context.area.header_text_set(None)
- if AddCameras._draw_handle:
- bpy.types.SpaceView3D.draw_handler_remove(
- AddCameras._draw_handle, 'WINDOW')
- AddCameras._draw_handle = None
-
- def _start_fly_review(self, context, add_modal_handler=True):
- """Frame the first camera and start fly-through review.
-
- *add_modal_handler* should be False when called from an already-running
- modal (e.g. after occlusion computation finishes).
- Returns True if fly-through started, False if no cameras were created.
- """
- if not self._cameras:
- self.report({'WARNING'}, "No cameras were created.")
- if AddCameras._draw_handle:
- bpy.types.SpaceView3D.draw_handler_remove(
- AddCameras._draw_handle, 'WINDOW')
- AddCameras._draw_handle = None
- return False
-
- rv3d = context.region_data
- context.scene.camera = self._cameras[0]
- if rv3d.view_perspective != 'CAMERA':
- bpy.ops.view3d.view_camera()
- bpy.ops.view3d.view_center_camera()
- try:
- rv3d.view_camera_zoom = 1.0
- except Exception:
- pass
-
- _sg_hide_label_overlay()
- if add_modal_handler:
- context.window_manager.modal_handler_add(self)
- self._timer = context.window_manager.event_timer_add(
- 0.5, window=context.window)
- self._last_time = time.time()
- bpy.ops.view3d.fly('INVOKE_DEFAULT')
- return True
-
- def _finish_without_review(self, context):
- """Clean up draw handler and restore state without fly-through."""
- if AddCameras._draw_handle:
- bpy.types.SpaceView3D.draw_handler_remove(
- AddCameras._draw_handle, 'WINDOW')
- AddCameras._draw_handle = None
- if self._cameras:
- context.scene.camera = self._cameras[0]
- _sg_restore_label_overlay()
- self.report({'INFO'},
- f"Cameras added successfully ({len(self._cameras)} cameras).")
-
- # -------------------------------------------------------
- # Placement methods
- # -------------------------------------------------------
-
- def _place_orbit_ring(self, context, center_location, ref_mat, initial_pos, cam_settings, radius, using_viewport_ref):
- """Place cameras in a circle (original AddCameras behaviour)."""
- total = self.num_cameras
- count = total - 1 if using_viewport_ref else total
- angle_initial = math.atan2(initial_pos.y - center_location.y, initial_pos.x - center_location.x)
- angle_step = 2 * math.pi / (count + 1)
-
- for i in range(count):
- angle = (i + 1) * angle_step + angle_initial
- cam_data_new = bpy.data.cameras.new(name=f'Camera_{i+1}')
- cam_obj_new = bpy.data.objects.new(f'Camera_{i+1}', cam_data_new)
- context.collection.objects.link(cam_obj_new)
- if using_viewport_ref:
- delta = (i + 1) * angle_step
- T1 = Matrix.Translation(-center_location)
- Rz = Matrix.Rotation(delta, 4, 'Z')
- T2 = Matrix.Translation(center_location)
- cam_obj_new.matrix_world = T2 @ Rz @ T1 @ ref_mat
- else:
- x = center_location.x + radius * math.cos(angle)
- y = center_location.y + radius * math.sin(angle)
- z = initial_pos.z
- cam_obj_new.location = (x, y, z)
- direction = center_location - cam_obj_new.location
- rot = direction.to_track_quat('-Z', 'Y')
- cam_obj_new.rotation_euler = rot.to_euler()
- self._copy_camera_settings(cam_obj_new, cam_settings)
- self._cameras.append(cam_obj_new)
-
- def _place_fan(self, context, center_location, ref_mat, initial_pos, cam_settings, radius, using_viewport_ref):
- """Spread cameras in an arc around the active camera's position."""
- fan_rad = math.radians(self.fan_angle)
- angle_initial = math.atan2(initial_pos.y - center_location.y, initial_pos.x - center_location.x)
- total = self.num_cameras
- count = total - 1 if using_viewport_ref else total
- if count <= 0:
- return
- for i in range(count):
- if count == 1:
- t = 0.0
- else:
- t = (i / (count - 1)) - 0.5 # range -0.5 .. 0.5
- angle = angle_initial + t * fan_rad
- cam_data_new = bpy.data.cameras.new(name=f'Camera_fan_{i}')
- cam_obj_new = bpy.data.objects.new(f'Camera_fan_{i}', cam_data_new)
- context.collection.objects.link(cam_obj_new)
- if using_viewport_ref:
- delta = t * fan_rad
- T1 = Matrix.Translation(-center_location)
- Rz = Matrix.Rotation(delta, 4, 'Z')
- T2 = Matrix.Translation(center_location)
- cam_obj_new.matrix_world = T2 @ Rz @ T1 @ ref_mat
- else:
- x = center_location.x + radius * math.cos(angle)
- y = center_location.y + radius * math.sin(angle)
- z = initial_pos.z
- cam_obj_new.location = (x, y, z)
- direction = center_location - cam_obj_new.location
- rot = direction.to_track_quat('-Z', 'Y')
- cam_obj_new.rotation_euler = rot.to_euler()
- self._copy_camera_settings(cam_obj_new, cam_settings)
- self._cameras.append(cam_obj_new)
-
- def _create_cameras_from_directions(self, context, directions, center_np,
- verts_world, cam_settings, fov_x, fov_y):
- """Shared camera creation for all auto-placement modes.
- Each camera gets its own optimal distance computed from the mesh's
- silhouette extent in that viewing direction."""
- center_vec = mathutils.Vector(center_np.tolist())
- prefix_map = {
- 'hemisphere': 'Camera_sphere',
- 'normal_weighted': 'Camera_auto',
- 'pca_axes': 'Camera_pca',
- 'greedy_coverage': 'Camera',
- }
- prefix = prefix_map.get(self.placement_mode, 'Camera')
-
- for i, d in enumerate(directions):
- d_np = np.array(d, dtype=float)
- dist, aim_off = _compute_silhouette_distance(verts_world, center_np, d_np, fov_x, fov_y)
- dir_vec = mathutils.Vector(d_np.tolist()).normalized()
- aim_point = center_vec + mathutils.Vector(aim_off.tolist())
- pos = aim_point + dir_vec * dist
-
- cam_data = bpy.data.cameras.new(name=f'{prefix}_{i}')
- cam_obj = bpy.data.objects.new(f'{prefix}_{i}', cam_data)
- context.collection.objects.link(cam_obj)
- cam_obj.location = pos
- right, up_v, d_unit = _camera_basis(d_np)
- cam_obj.rotation_euler = _rotation_from_basis(right, up_v, d_unit)
- self._copy_camera_settings(cam_obj, cam_settings)
- self._cameras.append(cam_obj)
-
- def _create_cameras_per_aspect(self, context, directions, center_np,
- verts_world, cam_settings, total_px,
- center_for_aspect):
- """Create cameras with per-camera optimal aspect ratio and distance.
- Uses an iterative refinement: first computes aspect from the
- orthographic silhouette, then refines it using the actual perspective
- projection from the computed camera position."""
- center_vec = mathutils.Vector(center_np.tolist())
- prefix_map = {
- 'hemisphere': 'Camera_sphere',
- 'normal_weighted': 'Camera_auto',
- 'pca_axes': 'Camera_pca',
- 'greedy_coverage': 'Camera',
- }
- prefix = prefix_map.get(self.placement_mode, 'Camera')
-
- for i, d in enumerate(directions):
- d_np = np.array(d, dtype=float)
- d_unit = d_np / np.linalg.norm(d_np)
-
- # --- Pass 1: orthographic aspect as initial estimate ---
- align = _get_resolution_align(context)
- aspect = _compute_per_camera_aspect(d_unit, verts_world, center_for_aspect)
- res_x, res_y = _resolution_from_aspect(aspect, total_px, align=align)
- fov_x, fov_y = _get_fov(cam_settings, context, res_x, res_y)
- dist, aim_off = _compute_silhouette_distance(
- verts_world, center_np, d_np, fov_x, fov_y)
-
- # --- Pass 2: refine aspect from actual perspective camera pos ---
- aim_point_np = center_np + aim_off
- cam_pos_np = aim_point_np + d_unit * dist
- aspect = _perspective_aspect(verts_world, cam_pos_np, d_np)
- res_x, res_y = _resolution_from_aspect(aspect, total_px, align=align)
- fov_x, fov_y = _get_fov(cam_settings, context, res_x, res_y)
- dist, aim_off = _compute_silhouette_distance(
- verts_world, center_np, d_np, fov_x, fov_y)
-
- dir_vec = mathutils.Vector(d_np.tolist()).normalized()
- aim_point = center_vec + mathutils.Vector(aim_off.tolist())
- pos = aim_point + dir_vec * dist
-
- cam_data = bpy.data.cameras.new(name=f'{prefix}_{i}')
- cam_obj = bpy.data.objects.new(f'{prefix}_{i}', cam_data)
- context.collection.objects.link(cam_obj)
- cam_obj.location = pos
- right, up_v, d_unit_cam = _camera_basis(d_np)
- cam_obj.rotation_euler = _rotation_from_basis(right, up_v, d_unit_cam)
- self._copy_camera_settings(cam_obj, cam_settings)
-
- # Store per-camera resolution
- _store_per_camera_resolution(cam_obj, res_x, res_y)
- _setup_square_camera_display(cam_obj, res_x, res_y)
- self._cameras.append(cam_obj)
-
- # Set scene to square resolution (max side length) for viewport display
- max_side = max(
- max(int(c.get('sg_res_x', 0)), int(c.get('sg_res_y', 0)))
- for c in self._cameras if 'sg_res_x' in c
- ) if self._cameras else max(context.scene.render.resolution_x,
- context.scene.render.resolution_y)
- if max_side > 0:
- context.scene.render.resolution_x = max_side
- context.scene.render.resolution_y = max_side
-
- @staticmethod
- def _copy_camera_settings(cam_obj, cam_settings):
- """Copy lens / sensor / clip settings from a reference camera data block."""
- cam_obj.data.type = cam_settings.type
- cam_obj.data.lens = cam_settings.lens
- cam_obj.data.sensor_width = cam_settings.sensor_width
- cam_obj.data.sensor_height = cam_settings.sensor_height
- cam_obj.data.clip_start = cam_settings.clip_start
- cam_obj.data.clip_end = cam_settings.clip_end
-
- def modal(self, context, event):
- # ── Occlusion computation phase ──────────────────────────────────
- if self._occ_phase:
- if event.type in {'ESC', 'RIGHTMOUSE'}:
- # Cancel occlusion computation
- context.window_manager.event_timer_remove(self._timer)
- self._timer = None
- self._occ_gen = None
- self._occ_phase = False
- context.area.header_text_set(None)
- if AddCameras._draw_handle:
- bpy.types.SpaceView3D.draw_handler_remove(
- AddCameras._draw_handle, 'WINDOW')
- AddCameras._draw_handle = None
- # Clean up temp camera data stored in state
- state = self._occ_state
- if state and state.get('temp_cam_data'):
- bpy.data.cameras.remove(state['temp_cam_data'])
- self._occ_state = None
- self.report({'WARNING'}, "Occlusion computation cancelled.")
- return {'CANCELLED'}
-
- if event.type == 'TIMER':
- try:
- progress = next(self._occ_gen)
- self._occ_progress = progress
- context.area.header_text_set(
- f"Computing occlusion visibility… "
- f"{progress * 100:.0f}% (ESC to cancel)")
- # Force viewport redraw so the GPU overlay updates too
- for area in context.screen.areas:
- if area.type == 'VIEW_3D':
- area.tag_redraw()
- except StopIteration as done:
- # Generator finished – retrieve result
- context.area.header_text_set(None)
-
- # Clean up occlusion timer
- context.window_manager.event_timer_remove(self._timer)
- self._timer = None
- self._occ_gen = None
- self._occ_phase = False
-
- state = self._occ_state
- result_type = state.get('result_type', 'greedy')
-
- if result_type == 'greedy':
- # Greedy occlusion: result is (directions, coverage)
- directions, coverage = done.value
- directions = [np.array(d) for d in directions]
- occ_label = state['occ_label']
- self.report({'INFO'},
- f"Greedy coverage ({occ_label}): "
- f"{len(directions)} cameras, "
- f"{coverage * 100:.1f}% coverage")
- elif result_type == 'filter_kmeans':
- # Face filter for K-means: result is exterior mask
- exterior = done.value
- normals = state['normals'][exterior]
- areas = state['areas'][exterior]
- n_removed = int((~exterior).sum())
- self.report({'INFO'},
- f"Occlusion filter: removed {n_removed} "
- f"interior faces, {len(normals)} remain")
- k = min(state['num_cameras'], len(normals))
- if k > 0 and len(normals) > 0:
- cluster_dirs = _kmeans_on_sphere(normals, areas, k)
- directions = [cluster_dirs[i]
- for i in range(len(cluster_dirs))]
- else:
- directions = []
- elif result_type == 'filter_pca':
- # Face filter for PCA: result is exterior mask
- exterior = done.value
- # PCA uses filtered verts (only exterior faces)
- f_centers = state['centers'][exterior]
- n_removed = int((~exterior).sum())
- self.report({'INFO'},
- f"Occlusion filter: removed {n_removed} "
- f"interior faces, {len(f_centers)} remain")
- if len(f_centers) >= 3:
- axes = _compute_pca_axes(f_centers)
- else:
- axes = _compute_pca_axes(
- state['verts_world'])
- directions = []
- for axis in axes:
- directions.append(axis)
- directions.append(-axis)
- if state.get('exclude_bottom'):
- threshold_z = -math.cos(
- state['exclude_bottom_angle'])
- directions = [
- d for d in directions
- if d[2] >= threshold_z]
- directions = directions[
- :min(state['num_cameras'],
- len(directions))]
-
- elif result_type == 'vis_kmeans':
- # Visibility-weighted K-means: weight by fraction
- vis_count = done.value
- n_cand = 200
- vis_fraction = vis_count.astype(float) / n_cand
- exterior = vis_count > 0
- normals = state['normals'][exterior]
- areas_base = state['areas'][exterior]
- weighted_areas = areas_base * vis_fraction[exterior]
- n_ext = int(exterior.sum())
- n_removed = len(vis_count) - n_ext
- self.report({'INFO'},
- f"Visibility-weighted: {n_ext} visible faces, "
- f"{n_removed} fully occluded removed")
- k = min(state['num_cameras'], len(normals))
- if k > 0 and len(normals) > 0:
- cluster_dirs = _kmeans_on_sphere(
- normals, weighted_areas, k)
- directions = [cluster_dirs[i]
- for i in range(len(cluster_dirs))]
- else:
- directions = []
-
- elif result_type == 'vis_interactive':
- # Enter interactive preview phase
- vis_count = done.value
- self._vis_count = vis_count
- self._vis_n_candidates = 200
- self._vis_balance = 0.2
- self._vis_preview_phase = True
- self._vis_state = state
- self._occ_state = None
- self._update_vis_cameras(context)
- return {'RUNNING_MODAL'}
-
- # Angular dedup against existing cameras (filter paths)
- ex_dirs = state.get('existing_dirs')
- if ex_dirs and result_type in (
- 'filter_kmeans', 'filter_pca', 'vis_kmeans'):
- directions = _filter_near_existing(
- directions, ex_dirs)
-
- # Finalize cameras (sort, aspect, create, prompts)
- self._finalize_auto_cameras(
- context, directions,
- state['verts_world'], state['mesh_center'],
- state['cam_settings'], state['temp_cam_data'])
- self._occ_state = None
-
- # Transition to fly-through review
- if self.review_placement:
- if not self._start_fly_review(
- context, add_modal_handler=False):
- return {'CANCELLED'}
- # Stay in modal for the fly-through phase
- else:
- self._finish_without_review(context)
- return {'FINISHED'}
- return {'RUNNING_MODAL'}
-
- # Let Blender process UI events during occlusion
- return {'RUNNING_MODAL'}
-
- # ── Interactive visibility preview phase ─────────────────────────
- if self._vis_preview_phase:
- if event.type in {'ESC', 'RIGHTMOUSE'} and event.value == 'PRESS':
- self._cleanup_vis_preview(context)
- self.report({'WARNING'}, "Interactive visibility cancelled.")
- return {'CANCELLED'}
-
- changed = False
- if event.type == 'WHEELUPMOUSE':
- self._vis_balance = min(1.0, self._vis_balance + 0.05)
- changed = True
- elif event.type == 'WHEELDOWNMOUSE':
- self._vis_balance = max(0.0, self._vis_balance - 0.05)
- changed = True
- elif event.type == 'NUMPAD_PLUS' and event.value == 'PRESS':
- self._vis_balance = min(1.0, self._vis_balance + 0.05)
- changed = True
- elif event.type == 'NUMPAD_MINUS' and event.value == 'PRESS':
- self._vis_balance = max(0.0, self._vis_balance - 0.05)
- changed = True
-
- if changed:
- self._update_vis_cameras(context)
- for area in context.screen.areas:
- if area.type == 'VIEW_3D':
- area.tag_redraw()
- return {'RUNNING_MODAL'}
-
- if event.type in {'RET', 'SPACE'} and event.value == 'PRESS':
- # Confirm: finalize with current directions
- self._vis_preview_phase = False
- context.area.header_text_set(None)
-
- # Delete preview cameras
- for cam in list(self._cameras):
- cam_data = cam.data
- bpy.data.objects.remove(cam, do_unlink=True)
- if cam_data and not cam_data.users:
- bpy.data.cameras.remove(cam_data)
- self._cameras.clear()
-
- state = self._vis_state
- self._finalize_auto_cameras(
- context, self._vis_directions,
- state['verts_world'], state['mesh_center'],
- state['cam_settings'], state['temp_cam_data'])
- self._vis_state = None
- self._vis_count = None
-
- # Transition to fly-through or finish
- if self.review_placement:
- if not self._start_fly_review(
- context, add_modal_handler=False):
- return {'CANCELLED'}
- else:
- self._finish_without_review(context)
- return {'FINISHED'}
- return {'RUNNING_MODAL'}
-
- return {'RUNNING_MODAL'}
-
- # ── Fly-through review phase ─────────────────────────────────────
- if event.type == 'LEFTMOUSE' and event.value == 'RELEASE':
- if time.time() - self._last_time < 0.2:
- return {'RUNNING_MODAL'}
-
- if event.type == 'TIMER':
- fly_running = any(op.bl_idname == 'VIEW3D_OT_fly'
- for w in context.window_manager.windows
- for op in w.modal_operators)
- if not fly_running:
- self._camera_index += 1
- if self._camera_index >= len(self._cameras):
- context.window_manager.event_timer_remove(self._timer)
- if AddCameras._draw_handle:
- bpy.types.SpaceView3D.draw_handler_remove(AddCameras._draw_handle, 'WINDOW')
- AddCameras._draw_handle = None
- # Restore to initial camera, or first placed camera for auto modes
- if self._initial_camera and self._initial_camera.name in [o.name for o in context.scene.objects]:
- context.scene.camera = self._initial_camera
- elif self._cameras:
- context.scene.camera = self._cameras[0]
- # Enable the floating prompt labels now that review is over
- _sg_restore_label_overlay()
- self.report({'INFO'}, "Cameras added successfully.")
- return {'FINISHED'}
- context.scene.camera = self._cameras[self._camera_index]
- self._last_time = time.time()
- bpy.ops.view3d.fly('INVOKE_DEFAULT')
- return {'PASS_THROUGH'}
-
- return {'PASS_THROUGH'}
-
- @classmethod
- def poll(cls, context):
- if sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- return True
-
- def invoke(self, context, event):
- return context.window_manager.invoke_props_dialog(self)
-
-
-
-def switch_viewport_to_camera(context, camera):
- """Switches the first found 3D viewport to the specified camera's view."""
- if not camera:
- return
- for area in context.screen.areas:
- if area.type == 'VIEW_3D':
- # Ensure we are in the right space and region
- for space in area.spaces:
- if space.type == 'VIEW_3D':
- space.region_3d.view_perspective = 'CAMERA'
- # Make sure the scene's active camera is set
- context.scene.camera = camera
- area.tag_redraw()
- break
- break
-
-class CloneCamera(bpy.types.Operator):
- """Create a new camera at the active camera's position and enter fly mode to reposition it.
-
- If no camera exists, one is created from the current viewport.
- Useful for incrementally adding cameras one at a time."""
- bl_idname = "object.clone_camera"
- bl_label = "Clone Camera"
- bl_options = {'REGISTER', 'UNDO'}
-
- _timer = None
- _camera = None
- _original_camera = None
-
- @classmethod
- def poll(cls, context):
- if sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- return True
-
- def execute(self, context):
- ref_cam = context.scene.camera
- self._original_camera = ref_cam
-
- if not ref_cam:
- # Create from viewport
- rv3d = context.region_data
- cam_data = bpy.data.cameras.new(name='Camera_clone')
- cam_obj = bpy.data.objects.new('Camera_clone', cam_data)
- context.collection.objects.link(cam_obj)
- cam_obj.matrix_world = rv3d.view_matrix.inverted()
- else:
- # Clone from active camera
- cam_data = bpy.data.cameras.new(name=f'{ref_cam.name}_clone')
- cam_obj = bpy.data.objects.new(cam_data.name, cam_data)
- context.collection.objects.link(cam_obj)
- cam_obj.matrix_world = ref_cam.matrix_world.copy()
- cam_obj.data.type = ref_cam.data.type
- cam_obj.data.lens = ref_cam.data.lens
- cam_obj.data.sensor_width = ref_cam.data.sensor_width
- cam_obj.data.sensor_height = ref_cam.data.sensor_height
- cam_obj.data.clip_start = ref_cam.data.clip_start
- cam_obj.data.clip_end = ref_cam.data.clip_end
-
- self._camera = cam_obj
- context.scene.camera = cam_obj
-
- rv3d = context.region_data
- if rv3d.view_perspective != 'CAMERA':
- bpy.ops.view3d.view_camera()
- bpy.ops.view3d.view_center_camera()
- try:
- rv3d.view_camera_zoom = 1.0
- except Exception:
- pass
-
- context.window_manager.modal_handler_add(self)
- self._timer = context.window_manager.event_timer_add(0.5, window=context.window)
- bpy.ops.view3d.fly('INVOKE_DEFAULT')
- return {'RUNNING_MODAL'}
-
- def modal(self, context, event):
- if event.type == 'TIMER':
- fly_running = any(op.bl_idname == 'VIEW3D_OT_fly'
- for w in context.window_manager.windows
- for op in w.modal_operators)
- if not fly_running:
- context.window_manager.event_timer_remove(self._timer)
- if self._original_camera:
- context.scene.camera = self._original_camera
- self.report({'INFO'}, f"Camera cloned: {self._camera.name}")
- return {'FINISHED'}
- return {'PASS_THROUGH'}
- return {'PASS_THROUGH'}
-
-
-class MirrorCamera(bpy.types.Operator):
- """Create a mirror of the active camera across a chosen axis through the object/scene center.
-
- The new camera is placed symmetrically on the opposite side and oriented to look at the center."""
- bl_idname = "object.mirror_camera"
- bl_label = "Mirror Camera"
- bl_options = {'REGISTER', 'UNDO'}
-
- mirror_axis: bpy.props.EnumProperty(
- name="Mirror Axis",
- description="Axis to mirror across",
- items=[
- ('X', "X Axis", "Mirror left / right"),
- ('Y', "Y Axis", "Mirror front / back"),
- ('Z', "Z Axis", "Mirror top / bottom"),
- ],
- default='X'
- ) # type: ignore
-
- @classmethod
- def poll(cls, context):
- return context.scene.camera is not None and not sg_modal_active(context)
-
- def invoke(self, context, event):
- return context.window_manager.invoke_props_dialog(self)
-
- def execute(self, context):
- cam = context.scene.camera
- obj = context.object
-
- # Determine center point
- if obj and obj.type == 'MESH':
- center = obj.matrix_world.translation.copy()
- else:
- center = mathutils.Vector((0, 0, 0))
-
- # Mirror position
- pos = cam.location.copy()
- axis_idx = 'XYZ'.index(self.mirror_axis)
- delta = pos - center
- delta[axis_idx] = -delta[axis_idx]
- new_pos = center + delta
-
- # Create new camera
- cam_data = bpy.data.cameras.new(name=f'{cam.name}_mirror_{self.mirror_axis}')
- cam_obj = bpy.data.objects.new(cam_data.name, cam_data)
- context.collection.objects.link(cam_obj)
-
- # Copy settings
- cam_obj.data.type = cam.data.type
- cam_obj.data.lens = cam.data.lens
- cam_obj.data.sensor_width = cam.data.sensor_width
- cam_obj.data.sensor_height = cam.data.sensor_height
- cam_obj.data.clip_start = cam.data.clip_start
- cam_obj.data.clip_end = cam.data.clip_end
-
- cam_obj.location = new_pos
- direction = center - new_pos
- rot_quat = direction.to_track_quat('-Z', 'Y')
- cam_obj.rotation_euler = rot_quat.to_euler()
-
- self.report({'INFO'}, f"Mirrored camera created: {cam_obj.name}")
- return {'FINISHED'}
-
-
-class ToggleCameraLabels(bpy.types.Operator):
- """Toggle floating camera prompt labels in the 3D viewport.
-
- Shows or hides the per-camera prompt text (from Collect Camera Prompts
- or auto-generated view labels) next to each camera in the viewport."""
- bl_idname = "object.toggle_camera_labels"
- bl_label = "Toggle Camera Labels"
- bl_options = {'REGISTER'}
-
- @classmethod
- def poll(cls, context):
- return any(o.type == 'CAMERA' for o in context.scene.objects) and not sg_modal_active(context)
-
- def execute(self, context):
- global _sg_label_draw_handle
- if _sg_label_draw_handle is not None:
- _sg_remove_label_overlay()
- self.report({'INFO'}, "Camera labels hidden")
- else:
- _sg_ensure_label_overlay()
- self.report({'INFO'}, "Camera labels visible")
- return {'FINISHED'}
-
-
-# Define the PropertyGroup to store camera prompts
-class CameraPromptItem(bpy.types.PropertyGroup):
- name: bpy.props.StringProperty(
- name="Camera Name",
- description="Name of the camera object"
- ) # type: ignore
- prompt: bpy.props.StringProperty(
- name="View Description",
- description="Description of the view from this camera"
- ) # type: ignore
-
-# ── Camera Generation Order ──────────────────────────────────────────────────
-
-class CameraOrderItem(bpy.types.PropertyGroup):
- """One entry in the generation-order list. Stores the Blender camera name."""
- name: bpy.props.StringProperty(
- name="Camera Name",
- description="Name of the camera object"
- ) # type: ignore
-
-
-class SG_UL_CameraOrderList(bpy.types.UIList):
- """UIList that shows cameras in their current generation order."""
- bl_idname = "SG_UL_CameraOrderList"
-
- def draw_item(self, _context, layout, _data, item, _icon,
- _active_data, _active_propname, index):
- cam_obj = bpy.data.objects.get(item.name)
- if cam_obj is None:
- layout.label(text=f"{item.name} (missing)", icon='ERROR')
- return
- # Show index, camera icon, name, and prompt if any
- row = layout.row(align=True)
- row.label(text=f"{index + 1}.")
- row.label(text=item.name, icon='CAMERA_DATA')
- # Show prompt preview if one exists
- prompt_item = next(
- (p for p in _context.scene.camera_prompts if p.name == item.name), None)
- if prompt_item and prompt_item.prompt:
- sub = row.row()
- sub.scale_x = 1.5
- sub.label(text=prompt_item.prompt, icon='SHORTDISPLAY')
-
-
-class SyncCameraOrder(bpy.types.Operator):
- """Rebuild the generation order list from all cameras in the scene (sorted alphabetically)"""
- bl_idname = "stablegen.sync_camera_order"
- bl_label = "Sync Camera Order"
- bl_options = {'REGISTER', 'UNDO'}
-
- @classmethod
- def poll(cls, context):
- return not sg_modal_active(context)
-
- def execute(self, context):
- scene = context.scene
- cameras = sorted(
- [obj for obj in scene.objects if obj.type == 'CAMERA'],
- key=lambda x: x.name)
- scene.sg_camera_order.clear()
- for cam in cameras:
- item = scene.sg_camera_order.add()
- item.name = cam.name
- scene.sg_camera_order_index = 0
- self.report({'INFO'}, f"Synced {len(cameras)} cameras to generation order list.")
- return {'FINISHED'}
-
-
-class MoveCameraOrder(bpy.types.Operator):
- """Move the selected camera up or down in the generation order"""
- bl_idname = "stablegen.move_camera_order"
- bl_label = "Move Camera"
- bl_options = {'REGISTER', 'UNDO'}
-
- direction: bpy.props.EnumProperty(
- items=[('UP', "Up", ""), ('DOWN', "Down", "")],
- name="Direction"
- ) # type: ignore
-
- @classmethod
- def poll(cls, context):
- return not sg_modal_active(context)
-
- def execute(self, context):
- scene = context.scene
- idx = scene.sg_camera_order_index
- total = len(scene.sg_camera_order)
- if total < 2:
- return {'CANCELLED'}
- new_idx = idx - 1 if self.direction == 'UP' else idx + 1
- if new_idx < 0 or new_idx >= total:
- return {'CANCELLED'}
- scene.sg_camera_order.move(idx, new_idx)
- scene.sg_camera_order_index = new_idx
- return {'FINISHED'}
-
-
-class ApplyCameraOrderPreset(bpy.types.Operator):
- """Sort the generation order list using a preset strategy.
- Cameras are first synced from the scene, then reordered."""
- bl_idname = "stablegen.apply_camera_order_preset"
- bl_label = "Apply Camera Order Preset"
- bl_options = {'REGISTER', 'UNDO'}
-
- strategy: bpy.props.EnumProperty(
- name="Strategy",
- items=[
- ('ALPHABETICAL', "Alphabetical (Original)",
- "Sort cameras alphabetically by name (original order for auto-generated cameras)"),
- ('FRONT_FIRST', "Front → Back → Sides",
- "Front-facing cameras first, then back, then sides. "
- "Prioritises the main view for sequential inpainting"),
- ('BACK_FIRST', "Back → Front → Sides",
- "Back-facing cameras first, then front, then sides"),
- ('ALTERNATING', "Alternating (Opposites)",
- "Alternate between opposing cameras (front, back, left, right…) "
- "for maximum context spread between consecutive views"),
- ('TOP_DOWN', "Top → Bottom",
- "Cameras sorted from highest elevation to lowest"),
- ('REVERSE', "Reverse Current",
- "Reverse the current generation order"),
- ],
- default='FRONT_FIRST'
- ) # type: ignore
-
- @classmethod
- def poll(cls, context):
- return not sg_modal_active(context)
-
- def execute(self, context):
- scene = context.scene
-
- # Always sync from scene first (picks up new/removed cameras)
- cameras = sorted(
- [obj for obj in scene.objects if obj.type == 'CAMERA'],
- key=lambda x: x.name)
-
- if not cameras:
- self.report({'ERROR'}, "No cameras in the scene.")
- return {'CANCELLED'}
-
- if self.strategy == 'REVERSE':
- # Reverse whatever is currently in the list
- names = [item.name for item in scene.sg_camera_order]
- if not names:
- names = [c.name for c in cameras]
- names.reverse()
- elif self.strategy == 'ALPHABETICAL':
- names = [c.name for c in cameras]
- else:
- # Compute mesh centre for direction classification
- mesh_objects = [obj for obj in scene.objects
- if obj.type == 'MESH' and not obj.hide_get()]
- if mesh_objects:
- centres = [obj.matrix_world @ (
- sum((mathutils.Vector(v.co) for v in obj.data.vertices),
- mathutils.Vector()) / max(len(obj.data.vertices), 1))
- for obj in mesh_objects]
- mesh_center = sum(centres, mathutils.Vector()) / len(centres)
- else:
- mesh_center = mathutils.Vector((0, 0, 0))
-
- mc = np.array(mesh_center, dtype=float)
-
- # Use the first camera (alphabetically) as the reference
- # "front" direction — avoids assuming any world axis is front.
- ref_pos = np.array(cameras[0].location, dtype=float)
- ref_d = ref_pos - mc
- ref_d_xy = ref_d[:2]
- ref_xy_len = np.linalg.norm(ref_d_xy)
- if ref_xy_len < 1e-8:
- ref_d_xy = np.array([0.0, 1.0])
- else:
- ref_d_xy = ref_d_xy / ref_xy_len
-
- # Compute direction angles for each camera
- cam_data = []
- for cam in cameras:
- pos = np.array(cam.location, dtype=float)
- d = pos - mc
- norm = np.linalg.norm(d)
- if norm < 1e-8:
- d = np.array([0, 1, 0], dtype=float)
- else:
- d /= norm
- # Elevation (angle above XY plane)
- elev = math.degrees(math.asin(np.clip(d[2], -1, 1)))
- # Azimuth relative to the first camera's XY direction
- d_xy = d[:2]
- d_xy_len = np.linalg.norm(d_xy)
- if d_xy_len < 1e-8:
- azimuth = 0.0
- else:
- d_xy = d_xy / d_xy_len
- cos_a = float(np.clip(np.dot(d_xy, ref_d_xy), -1, 1))
- cross = float(ref_d_xy[0] * d_xy[1]
- - ref_d_xy[1] * d_xy[0])
- azimuth = math.degrees(math.atan2(cross, cos_a))
- cam_data.append((cam.name, elev, azimuth))
-
- if self.strategy == 'FRONT_FIRST':
- # Priority: front (small |azimuth|), then back (large |azimuth|), then sides
- def _front_key(item):
- _name, _elev, az = item
- abs_az = abs(az)
- if abs_az <= 45:
- bucket = 0 # front
- elif abs_az >= 135:
- bucket = 1 # back
- else:
- bucket = 2 # sides
- return (bucket, abs_az, -_elev)
- cam_data.sort(key=_front_key)
-
- elif self.strategy == 'BACK_FIRST':
- def _back_key(item):
- _name, _elev, az = item
- abs_az = abs(az)
- if abs_az >= 135:
- bucket = 0 # back
- elif abs_az <= 45:
- bucket = 1 # front
- else:
- bucket = 2 # sides
- return (bucket, -abs_az, -_elev)
- cam_data.sort(key=_back_key)
-
- elif self.strategy == 'ALTERNATING':
- # Sort by azimuth, then pick from alternating ends
- cam_data.sort(key=lambda x: x[2])
- alternated = []
- left, right = 0, len(cam_data) - 1
- pick_left = True
- while left <= right:
- if pick_left:
- alternated.append(cam_data[left])
- left += 1
- else:
- alternated.append(cam_data[right])
- right -= 1
- pick_left = not pick_left
- cam_data = alternated
-
- elif self.strategy == 'TOP_DOWN':
- cam_data.sort(key=lambda x: -x[1]) # highest elevation first
-
- names = [item[0] for item in cam_data]
-
- # Apply to collection
- scene.sg_camera_order.clear()
- for n in names:
- item = scene.sg_camera_order.add()
- item.name = n
- scene.sg_camera_order_index = 0
- self.report({'INFO'},
- f"Applied '{self.strategy}' order to {len(names)} cameras.")
- return {'FINISHED'}
-
-
-class CollectCameraPrompts(bpy.types.Operator):
- """Collect viewpoint description prompts for the selected cameras (or all cameras if none selected).
-
- - These prompts will be appended before the main prompt for each camera generation.
- - Applicable for separate, sequential and refine (also within grid mode) modes.
- - Select one or more cameras in the viewport to only prompt for those, or run with no selection to cycle through all cameras.
- - Examples: 'front view', 'close-up on face', 'from above'."""
- bl_idname = "object.collect_camera_prompts"
- bl_label = "Collect Camera View Prompts"
- bl_options = {'REGISTER', 'UNDO'}
-
- camera_prompt: bpy.props.StringProperty(
- name="View Description",
- description="Describe the view from this camera (e.g., 'front view', 'close-up on face', 'from above')",
- default=""
- ) # type: ignore
-
- # Internal state
- _cameras: list = []
- _camera_index: int = 0
-
- @classmethod
- def poll(cls, context):
- if sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- if not any(obj.type == 'CAMERA' for obj in context.scene.objects):
- cls.poll_message_set("No cameras in the scene — use 'Add Cameras' first")
- return False
- return True
-
- def invoke(self, context, event):
- # Use selected cameras if any are selected, otherwise fall back to all
- selected_cams = sorted(
- [obj for obj in context.selected_objects if obj.type == 'CAMERA'],
- key=lambda x: x.name,
- )
- if selected_cams:
- self._cameras = selected_cams
- else:
- self._cameras = sorted(
- [obj for obj in context.scene.objects if obj.type == 'CAMERA'],
- key=lambda x: x.name,
- )
-
- if not self._cameras:
- self.report({'ERROR'}, "No cameras found in the scene.")
- return {'CANCELLED'}
-
- # Initialize state
- self._camera_index = 0
-
- # Set the first camera and pre-fill prompt if exists
- current_cam = self._cameras[self._camera_index]
-
- # Find existing prompt or set default
- existing_item = next((item for item in context.scene.camera_prompts if item.name == current_cam.name), None)
- self.camera_prompt = existing_item.prompt if existing_item else ""
-
- context.scene.camera = current_cam
- switch_viewport_to_camera(context, current_cam) # Switch viewport
- return context.window_manager.invoke_props_dialog(self, width=400)
-
- def draw(self, context):
- layout = self.layout
- if self._camera_index < len(self._cameras):
- layout.label(text=f"Camera: {self._cameras[self._camera_index].name} ({self._camera_index + 1}/{len(self._cameras)})")
- layout.prop(self, "camera_prompt")
-
- def execute(self, context):
- cam_name = self._cameras[self._camera_index].name
-
- # Find existing item or add a new one
- prompt_item = next((item for item in context.scene.camera_prompts if item.name == cam_name), None)
- if not prompt_item:
- prompt_item = context.scene.camera_prompts.add()
- prompt_item.name = cam_name
-
- prompt_item.prompt = self.camera_prompt.strip() # Store trimmed prompt
-
- self._camera_index += 1
- if self._camera_index < len(self._cameras):
- next_cam = self._cameras[self._camera_index]
- # Pre-fill next prompt
- existing_item = next((item for item in context.scene.camera_prompts if item.name == next_cam.name), None)
- self.camera_prompt = existing_item.prompt if existing_item else ""
- # Ensure scene camera is set for next dialog and switch view
- context.scene.camera = next_cam
- switch_viewport_to_camera(context, next_cam) # Switch viewport
- return context.window_manager.invoke_props_dialog(self, width=400) # Show next dialog
- else:
- # Refresh the floating labels so edited prompts appear
- _sg_ensure_label_overlay()
- self.report({'INFO'}, f"Collected prompts for {len(self._cameras)} cameras.")
- return {'FINISHED'}
-
-
-class SwitchMaterial(bpy.types.Operator):
- """Switches the material of all objects to a desired index."""
-
- bl_idname = "object.switch_material"
- bl_label = "Switch Material"
- bl_options = {'REGISTER', 'UNDO'}
-
- material_index: bpy.props.IntProperty(
- name="Material Index",
- description="Index of the material to switch to",
- default=0,
- min=0
- ) # type: ignore
-
- @classmethod
- def poll(cls, context):
- if sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- return True
-
- def invoke(self, context, event):
- return context.window_manager.invoke_props_dialog(self)
-
- def execute(self, context):
- """
- Executes the operator.
- :param context: Blender context.
- :return: {'FINISHED'}
- """
- # Select all objects
- bpy.ops.object.select_all(action='SELECT')
-
-
- for obj in context.selected_objects:
- if obj.type != 'MESH':
- continue
- if self.material_index < len(obj.data.materials):
- obj.active_material_index = self.material_index
- # Store original materials
- original_materials = list(obj.data.materials)
- # Store the material to be set as active
- to_be_active_material = obj.active_material
- # Clear materials and assign the material at the specified index
- obj.data.materials.clear()
- obj.data.materials.append(to_be_active_material)
- # Restore original materials
- for mat in original_materials:
- if mat != to_be_active_material:
- obj.data.materials.append(mat)
-
- # Deselct all objects
- bpy.ops.object.select_all(action='DESELECT')
- return {'FINISHED'}
-
-def prepare_baking(context):
- bpy.context.scene.render.engine = 'CYCLES'
- # Force CPU + OSL only for Blender < 5.1 (native Raycast nodes don't need it)
- if bpy.app.version < (5, 1, 0):
- if hasattr(bpy.context.scene.cycles, 'shading_system'):
- bpy.context.scene.cycles.shading_system = True
- else:
- bpy.context.scene.cycles.use_osl = True
- bpy.context.scene.cycles.device = 'CPU'
- else:
- # Blender 5.1+: prefer GPU baking for massive speedup on high-poly.
- # Setting cycles.device = 'GPU' alone is not enough — we must also
- # set compute_device_type on Cycles preferences and ensure the GPU
- # device is enabled, otherwise Blender silently falls back to CPU.
- try:
- cycles_prefs = bpy.context.preferences.addons['cycles'].preferences
- # Probe all available backends in order of preference
- for backend in ('CUDA', 'OPTIX', 'HIP', 'ONEAPI', 'METAL'):
- try:
- cycles_prefs.compute_device_type = backend
- cycles_prefs.get_devices()
- gpu_devs = [d for d in cycles_prefs.devices
- if d.type == backend]
- if gpu_devs:
- for d in gpu_devs:
- d.use = True
- bpy.context.scene.cycles.device = 'GPU'
- print(f"[StableGen] Baking with {backend} GPU: "
- f"{', '.join(d.name for d in gpu_devs)}")
- break
- except TypeError:
- # This backend is not available on this platform
- continue
- else:
- print("[StableGen] No GPU backend found, baking on CPU")
- except Exception:
- pass # Fall back to whatever the user had
-
- # Set bake type to diffuse and contributions to color only
- bpy.context.scene.cycles.bake_type = 'DIFFUSE'
- bpy.context.scene.render.bake.use_pass_direct = False
- bpy.context.scene.render.bake.use_pass_indirect = False
- bpy.context.scene.render.bake.use_pass_color = True
- bpy.context.scene.render.bake.view_from = 'ABOVE_SURFACE'
-
- # Set steps to 1 for faster baking
- bpy.context.scene.cycles.samples = 1
-
- # Minimize light bounces — not needed for EMIT or color-only DIFFUSE bakes
- bpy.context.scene.cycles.max_bounces = 0
-
- # Keep the BVH in memory between consecutive bakes (huge win for PBR
- # where we bake 5-7 channels of the same mesh back-to-back)
- bpy.context.scene.render.use_persistent_data = True
-
-def unwrap(obj, method, overlap_only):
- """
- Unwraps the UVs of the given object using the selected method.
- :param obj: Blender object.
- :return: None
- """
- if method == 'none':
- return
-
- # Deselect all objects
- bpy.ops.object.select_all(action='DESELECT')
- # Set object as active and select it
- bpy.context.view_layer.objects.active = obj
- obj.select_set(True)
-
- is_new = False
-
- # If all UV maps are ProjectionUV or buffer, add new one
- if all(["ProjectionUV" in uv.name or uv.name == "_SG_ProjectionBuffer" for uv in obj.data.uv_layers]):
- # Add a new UV map
- obj.data.uv_layers.new(name=f"BakeUV")
- is_new = True
- obj.data.uv_layers.active_index = len(obj.data.uv_layers) - 1
- # Set it for rendering
- obj.data.uv_layers.active = obj.data.uv_layers[-1]
- else:
- # Ensure the active UV is a non-ProjectionUV/buffer map
- for uv_layer in obj.data.uv_layers:
- if "ProjectionUV" not in uv_layer.name and uv_layer.name != "_SG_ProjectionBuffer":
- obj.data.uv_layers.active = uv_layer
- break
-
- bpy.ops.object.mode_set(mode='EDIT')
-
- # Ensure UV selection sync is OFF
- bpy.context.scene.tool_settings.use_uv_select_sync = False
-
- if overlap_only:
- # Deselect
- bpy.ops.uv.select_all(action='DESELECT')
- # Check if the object has overlapping UVs, if not, skip unwrapping
- bpy.ops.uv.select_overlap()
- # Get a BMesh representation of the mesh
- bm = bmesh.from_edit_mesh(obj.data)
- bm.faces.ensure_lookup_table()
- # Use the active UV layer (this is a BMUVLayer)
- uv_layer = bm.loops.layers.uv.active
- uv_layer = bm.loops.layers.uv.active
- if not uv_layer:
- bpy.ops.object.mode_set(mode='OBJECT')
- return
-
- # Check for ANY selected UV elements
- if bpy.app.version >= (5, 0, 0):
- # Blender 5.0+ uses loop.uv_select_vert
- has_overlap = any(
- loop.uv_select_vert
- for face in bm.faces
- for loop in face.loops
- )
- else:
- has_overlap = any(
- loop[uv_layer].select
- for face in bm.faces
- for loop in face.loops
- )
-
- if not has_overlap:
- bpy.ops.object.mode_set(mode='OBJECT')
- return
-
- bpy.context.scene.tool_settings.use_uv_select_sync = True
- # Select all faces
- bpy.ops.mesh.select_all(action='SELECT')
-
- if method == 'basic':
- bpy.ops.uv.unwrap(method='ANGLE_BASED', margin=0.001)
- elif method == 'smart':
- bpy.ops.uv.smart_project()
- elif method == 'cube':
- bpy.ops.uv.cube_project(cube_size=1.0)
- elif method == 'lightmap':
- bpy.ops.uv.lightmap_pack()
- elif method == 'pack':
- bpy.ops.uv.pack_islands()
-
- bpy.ops.uv.select_all(action='DESELECT')
- bpy.ops.object.mode_set(mode='OBJECT')
-
-def bake_texture(context, obj, texture_resolution, suffix = "", view_transform = 'Standard', output_dir = None):
- """
- Bakes the texture of the given object.
- :param context: Blender context.
- :param obj: Blender object.
- :return: True if successful, False otherwise.
- """
- bpy.context.scene.display_settings.display_device = 'sRGB'
- # Backup original view transform
- original_view_transform = bpy.context.scene.view_settings.view_transform
- # Backup whether the object was enabled for rendering
- original_render = obj.hide_render
-
- bpy.context.scene.view_settings.view_transform = view_transform
- # Set the object to be rendered
- obj.hide_render = False
-
- # Create a new image for baking
- image_name = f"{obj.name}_baked{suffix}"
- image = bpy.data.images.new(name=image_name, width=texture_resolution, height=texture_resolution)
-
- # Create a new texture node in the object's material
- if not obj.data.materials:
- # Cancel
- print("No materials found")
- return False
- else:
- # Temporarily remove all original materials
- mat = obj.active_material.copy()
- original_materials = list(obj.data.materials)
- original_active_material = obj.active_material
- obj.data.materials.clear()
- obj.data.materials.append(mat)
-
- mat.use_nodes = True
- nodes = mat.node_tree.nodes
- links = mat.node_tree.links
-
- tex_image = nodes.new("ShaderNodeTexImage")
- tex_image.image = image
- tex_image.location = (0, 0)
-
- # Deselect all objects
- bpy.ops.object.select_all(action='DESELECT')
- # Set object as active and select it
- bpy.context.view_layer.objects.active = obj
- obj.select_set(True)
- # Set the image node as the active bake target
- nodes.active = tex_image
-
- # Ensure the active UV is a non-ProjectionUV/buffer map for correct baking
- for uv_layer in obj.data.uv_layers:
- if "ProjectionUV" not in uv_layer.name and uv_layer.name != "_SG_ProjectionBuffer":
- obj.data.uv_layers.active = uv_layer
- break
-
- # Check if there is a BSDF node before the output node
- output = None
- had_bsdf = False
- for node in nodes:
- if node.type == 'OUTPUT_MATERIAL':
- output = node
- break
-
- before_output = output.inputs[0].links[0].from_node if output and output.inputs[0].is_linked else None
- if before_output and before_output.type != 'BSDF_PRINCIPLED':
- bsdf_node = nodes.new("ShaderNodeBsdfPrincipled")
- bsdf_node.location = (before_output.location[0] + 200, before_output.location[1])
- # Connect the before output node to the BSDF node
- links.new(before_output.outputs[0], bsdf_node.inputs[0])
- # Connect the BSDF node to the output node
- links.new(bsdf_node.outputs[0], output.inputs[0])
-
- # Disconnect Metallic so the DIFFUSE color-only pass returns the
- # true Base Color. Principled BSDF multiplies diffuse by
- # (1 - Metallic), which would dim metallic areas incorrectly.
- for node in nodes:
- if node.type == 'BSDF_PRINCIPLED':
- met = node.inputs.get("Metallic")
- if met:
- for link in list(met.links):
- links.remove(link)
- met.default_value = 0.0
- break
-
- # Bake the texture
- # Ensure OBJECT mode — bake fails silently in EDIT mode
- if bpy.context.mode != 'OBJECT':
- bpy.ops.object.mode_set(mode='OBJECT')
- bpy.ops.object.bake(type='DIFFUSE', width=texture_resolution, height=texture_resolution)
-
- # Save the image if required
- if not os.path.exists(output_dir):
- os.makedirs(output_dir)
- if suffix == "":
- image.filepath_raw = os.path.join(output_dir, f"{obj.name}.png")
- else:
- image.filepath_raw = os.path.join(output_dir, f"{obj.name}_baked{suffix}.png")
- image.file_format = 'PNG'
- image.save()
-
- # Remove tex_image node
- nodes.remove(tex_image)
-
- # Restore original view transform
- bpy.context.scene.view_settings.view_transform = original_view_transform
- # Restore original render settings
- obj.hide_render = original_render
-
- # Restore original materials
- obj.data.materials.clear()
- # First append the original active material
- if original_active_material:
- obj.data.materials.append(original_active_material)
- for mat_item in original_materials:
- if mat_item != original_active_material:
- obj.data.materials.append(mat_item)
-
- # Remove the temporary material copy (prevent leak)
- bpy.data.materials.remove(mat)
-
- return True
-
-def _has_non_projection_uv(obj):
- """Return True if the object has at least one UV map that can be used
- for baking (not a StableGen projection UV or buffer)."""
- if not obj or obj.type != 'MESH':
- return False
- if not obj.data.uv_layers:
- return False
- for uv in obj.data.uv_layers:
- if (not uv.name.startswith("ProjectionUV")
- and uv.name != "_SG_ProjectionBuffer"):
- return True
- return False
-
-def _uvs_likely_overlap(obj, uv_name=None):
- me = obj.data
- if not me.uv_layers:
- return False
- uv_layer = me.uv_layers.get(uv_name) if uv_name else me.uv_layers.active
- if not uv_layer:
- return False
-
- seen = set()
- for poly in me.polygons:
- for li in poly.loop_indices:
- uv = uv_layer.data[li].uv
- key = (round(uv.x, 5), round(uv.y, 5))
- if key in seen:
- return True
- seen.add(key)
- return False
-
-
-# ── PBR Channel Baking Helpers ─────────────────────────────────────────
-
-# Standard export suffixes per PBR channel
-_PBR_CHANNEL_SUFFIXES = {
- 'base_color': 'BaseColor',
- 'roughness': 'Roughness',
- 'metallic': 'Metallic',
- 'normal': 'Normal',
- 'emission': 'Emission',
- 'height': 'Height',
- 'ao': 'AO',
-}
-
-
-def _find_pbr_bake_sources(mat):
- """Inspect a Principled BSDF material and return bakeable PBR channel sources.
-
- Returns:
- dict: ``{channel_name: output_socket}`` for each connected PBR
- channel on the material's Principled BSDF.
- """
- if not mat or not mat.use_nodes:
- return {}
-
- nodes = mat.node_tree.nodes
- sources = {}
-
- # Find Principled BSDF
- principled = None
- for node in nodes:
- if node.type == 'BSDF_PRINCIPLED':
- principled = node
- break
- if not principled:
- return {}
-
- # ── Base Color + AO ──────────────────────────────────────────────
- base_input = principled.inputs.get("Base Color")
- if base_input and base_input.links:
- src_node = base_input.links[0].from_node
- is_ao_mult = (
- getattr(src_node, 'label', '') == 'AO Multiply'
- or (src_node.type == 'MIX'
- and getattr(src_node, 'data_type', '') == 'RGBA'
- and getattr(src_node, 'blend_type', '') == 'MULTIPLY'))
- if is_ao_mult:
- # A input (index 6) = base colour, B input (index 7) = AO
- a_in = src_node.inputs[6]
- if a_in.links:
- sources['base_color'] = a_in.links[0].from_socket
- b_in = src_node.inputs[7]
- if b_in.links:
- sources['ao'] = b_in.links[0].from_socket
- else:
- sources['base_color'] = base_input.links[0].from_socket
-
- # ── Roughness ────────────────────────────────────────────────────
- ri = principled.inputs.get("Roughness")
- if ri and ri.links:
- sources['roughness'] = ri.links[0].from_socket
-
- # ── Metallic ─────────────────────────────────────────────────────
- mi = principled.inputs.get("Metallic")
- if mi and mi.links:
- sources['metallic'] = mi.links[0].from_socket
-
- # ── Normal (raw colours before Normal Map node) ──────────────────
- ni = principled.inputs.get("Normal")
- if ni and ni.links:
- n_node = ni.links[0].from_node
- if n_node.type == 'NORMAL_MAP':
- ci = n_node.inputs.get("Color")
- if ci and ci.links:
- sources['normal'] = ci.links[0].from_socket
-
- # ── Emission ─────────────────────────────────────────────────────
- ei = principled.inputs.get("Emission Color") or principled.inputs.get("Emission")
- if ei and ei.links:
- sources['emission'] = ei.links[0].from_socket
-
- # ── Height (from Displacement node → Material Output) ────────────
- for node in nodes:
- if node.type == 'OUTPUT_MATERIAL':
- di = node.inputs.get("Displacement")
- if di and di.links:
- d_node = di.links[0].from_node
- if d_node.type == 'DISPLACEMENT':
- hi = d_node.inputs.get("Height")
- if hi and hi.links:
- sources['height'] = hi.links[0].from_socket
- break
-
- return sources
-
-
-def _restore_materials(obj, original_materials, original_active):
- """Restore an object's material slots back to their original state."""
- obj.data.materials.clear()
- if original_active:
- obj.data.materials.append(original_active)
- for m in original_materials:
- if m != original_active:
- obj.data.materials.append(m)
-
-
-def bake_pbr_channel(context, obj, channel_name, texture_resolution, output_dir):
- """Bake one PBR channel via the Emit technique.
-
- Copies the material, finds the source socket for *channel_name*,
- rewires it through an Emission shader to the Material Output, bakes
- ``EMIT``, and saves the result to ``output_dir/{obj.name}_{Suffix}.png``.
-
- Returns:
- File path of the baked image, or ``None`` on failure.
- """
- if not obj.data.materials or not obj.active_material:
- return None
-
- # ── Save state ─────────────────────────────────────────────────
- orig_vt = context.scene.view_settings.view_transform
- orig_render = obj.hide_render
- context.scene.view_settings.view_transform = 'Standard'
- obj.hide_render = False
-
- # ── Copy material onto the object ──────────────────────────────
- original_materials = list(obj.data.materials)
- original_active = obj.active_material
- mat_copy = original_active.copy()
- obj.data.materials.clear()
- obj.data.materials.append(mat_copy)
- mat_copy.use_nodes = True
- nodes = mat_copy.node_tree.nodes
- links = mat_copy.node_tree.links
-
- # ── Find source socket in the copy ─────────────────────────────
- sources = _find_pbr_bake_sources(mat_copy)
- source_socket = sources.get(channel_name)
- if source_socket is None:
- _restore_materials(obj, original_materials, original_active)
- bpy.data.materials.remove(mat_copy)
- context.scene.view_settings.view_transform = orig_vt
- obj.hide_render = orig_render
- return None
-
- # ── Find Output node ───────────────────────────────────────────
- output_node = None
- for n in nodes:
- if n.type == 'OUTPUT_MATERIAL':
- output_node = n
- break
- if not output_node:
- _restore_materials(obj, original_materials, original_active)
- bpy.data.materials.remove(mat_copy)
- context.scene.view_settings.view_transform = orig_vt
- obj.hide_render = orig_render
- return None
-
- # ── Rewire material for baking ───────────────────────────────
- # Normal channel: use Cycles' built-in NORMAL bake which automatically
- # converts to tangent space — game-engine / glTF compatible regardless
- # of the original normal mode (world, bump, tangent).
- # All other channels: rewire source → Emission → Output for EMIT bake.
- bake_type = 'EMIT'
- if channel_name == 'normal':
- # Keep the Principled BSDF wired to the Output with the Normal
- # input connected. Cycles NORMAL bake evaluates the shader's
- # final shading normal and converts it to tangent space for us.
- bake_type = 'NORMAL'
- # Ensure the normal influence is at full strength for baking
- principled = None
- for n in nodes:
- if n.type == 'BSDF_PRINCIPLED':
- principled = n
- break
- if principled and principled.inputs.get("Normal") and principled.inputs["Normal"].links:
- nmap_node = principled.inputs["Normal"].links[0].from_node
- if nmap_node.type == 'NORMAL_MAP':
- nmap_node.inputs["Strength"].default_value = 1.0
- else:
- for link in list(output_node.inputs["Surface"].links):
- links.remove(link)
-
- emit_node = nodes.new("ShaderNodeEmission")
- emit_node.location = (output_node.location[0] - 200,
- output_node.location[1])
- links.new(source_socket, emit_node.inputs["Color"])
- links.new(emit_node.outputs["Emission"],
- output_node.inputs["Surface"])
-
- # ── Bake target image ──────────────────────────────────────────
- suffix = _PBR_CHANNEL_SUFFIXES.get(channel_name, channel_name)
- img_name = f"{obj.name}_{suffix}"
- bake_img = bpy.data.images.new(name=img_name,
- width=texture_resolution,
- height=texture_resolution)
-
- # Non-color channels (roughness, metallic, normal, height, AO) must be
- # stored in linear / Non-Color so the round-trip is lossless:
- # Cycles linear → raw 8-bit → PNG → load as Non-Color → linear.
- # Without this, the default sRGB image applies gamma encoding on write,
- # but add_baked_material() loads them as Non-Color (no decode) →
- # values are gamma-shifted (e.g. roughness 0.22 → 0.50).
- if channel_name not in ('base_color', 'emission'):
- bake_img.colorspace_settings.name = 'Non-Color'
-
- tex_node = nodes.new("ShaderNodeTexImage")
- tex_node.image = bake_img
- nodes.active = tex_node
-
- # ── Select object + non-projection UV ──────────────────────────
- bpy.ops.object.select_all(action='DESELECT')
- context.view_layer.objects.active = obj
- obj.select_set(True)
- for uv in obj.data.uv_layers:
- if "ProjectionUV" not in uv.name and uv.name != "_SG_ProjectionBuffer":
- obj.data.uv_layers.active = uv
- break
-
- # ── Bake ─────────────────────────────────────────────────────
- # Ensure OBJECT mode — bake fails silently in EDIT mode
- if bpy.context.mode != 'OBJECT':
- bpy.ops.object.mode_set(mode='OBJECT')
- if bake_type == 'NORMAL':
- bpy.ops.object.bake(type='NORMAL',
- normal_space='TANGENT',
- width=texture_resolution,
- height=texture_resolution)
- else:
- bpy.ops.object.bake(type='EMIT',
- width=texture_resolution,
- height=texture_resolution)
-
- # ── Save ───────────────────────────────────────────────────────
- os.makedirs(output_dir, exist_ok=True)
- out_path = os.path.join(output_dir, f"{obj.name}_{suffix}.png")
- bake_img.filepath_raw = out_path
- bake_img.file_format = 'PNG'
- bake_img.save()
- print(f" Saved PBR channel: {out_path}")
-
- # ── Cleanup ────────────────────────────────────────────────────
- _restore_materials(obj, original_materials, original_active)
- bpy.data.materials.remove(mat_copy)
- context.scene.view_settings.view_transform = orig_vt
- obj.hide_render = orig_render
- return out_path
-
-
-def _flip_normal_green(filepath):
- """Flip the green channel of a normal map (OpenGL ↔ DirectX)."""
- if not os.path.exists(filepath):
- return
- try:
- import numpy as np
- img = bpy.data.images.load(filepath, check_existing=False)
- w, h = img.size
- px = np.array(img.pixels[:]).reshape(h, w, 4)
- px[:, :, 1] = 1.0 - px[:, :, 1]
- img.pixels = px.flatten().tolist()
- img.save()
- bpy.data.images.remove(img)
- print(f" Flipped normal green channel (DirectX): {filepath}")
- except Exception as e:
- print(f"[StableGen] Failed to flip normal green channel: {e}")
-
-
-def _pack_orm_texture(output_dir, obj_name):
- """Pack AO, Roughness, Metallic into an ORM texture.
-
- Channel layout: R = AO, G = Roughness, B = Metallic (Unreal / glTF).
- Requires at least Roughness and Metallic to exist.
- """
- import numpy as np
-
- rough_path = os.path.join(output_dir, f"{obj_name}_Roughness.png")
- metal_path = os.path.join(output_dir, f"{obj_name}_Metallic.png")
- if not os.path.exists(rough_path) or not os.path.exists(metal_path):
- return None
-
- rough_img = bpy.data.images.load(rough_path, check_existing=True)
- metal_img = bpy.data.images.load(metal_path, check_existing=True)
- w, h = rough_img.size
-
- rough_px = np.array(rough_img.pixels[:]).reshape(h, w, 4)
- metal_px = np.array(metal_img.pixels[:]).reshape(h, w, 4)
-
- ao_path = os.path.join(output_dir, f"{obj_name}_AO.png")
- if os.path.exists(ao_path):
- ao_img = bpy.data.images.load(ao_path, check_existing=True)
- ao_px = np.array(ao_img.pixels[:]).reshape(h, w, 4)
- ao_chan = ao_px[:, :, 0]
- else:
- ao_chan = np.ones((h, w), dtype=np.float32)
-
- orm = np.ones((h, w, 4), dtype=np.float32)
- orm[:, :, 0] = ao_chan
- orm[:, :, 1] = rough_px[:, :, 0]
- orm[:, :, 2] = metal_px[:, :, 0]
-
- orm_img = bpy.data.images.new(name=f"{obj_name}_ORM", width=w, height=h)
- orm_img.pixels = orm.flatten().tolist()
-
- orm_path = os.path.join(output_dir, f"{obj_name}_ORM.png")
- orm_img.filepath_raw = orm_path
- orm_img.file_format = 'PNG'
- orm_img.save()
- print(f" Packed ORM texture: {orm_path}")
- return orm_path
-
-
-class BakeTextures(bpy.types.Operator):
- """Bakes textures using the cycles render engine.
-
- - This will convert the textures to use a UV map. The first non-projection UV map will be used. If there are none, a new one will be created.
- - Textures will be output to the "baked" directory in the output path.
- - This will make the generated textures available in the Material Preview viewport shading mode."""
- bl_idname = "object.bake_textures"
- bl_label = "Bake Textures"
- bl_options = {'REGISTER', 'UNDO'}
-
- texture_resolution: bpy.props.IntProperty(
- name="Texture Resolution",
- description="Resolution of the baked textures",
- default=2048,
- min=128,
- max=8192
- ) # type: ignore
-
- try_unwrap: bpy.props.EnumProperty(
- name="Unwrap Method",
- description="Method to unwrap UVs before baking",
- items=[
- ('none', 'None', 'Skip UV unwrapping'),
- ('cube', 'Cube Project', 'Fast cube projection — great for high-poly / TRELLIS meshes (handles 1M+ faces instantly)'),
- ('smart', 'Smart UV Project', 'Smart UV Project — good balance of quality and speed'),
- ('basic', 'Basic Unwrap', 'Angle-based unwrap — best quality but very slow on high-poly meshes'),
- ('lightmap', 'Lightmap Pack', 'Use Lightmap Pack with default parameters'),
- ('pack', 'Pack Islands', 'Use Pack Islands with default parameters')
- ],
- default='smart'
- ) # type: ignore
-
- add_material: bpy.props.BoolProperty(
- name="Add Material",
- description="Add the baked texture as a material to the objects",
- default=True
- ) # type: ignore
-
- flatten_for_refine: bpy.props.BoolProperty(
- name="Bake & Continue Refining",
- description="After baking, apply the baked texture to the StableGen projection material and clean up previous projection images",
- default=False
- ) # type: ignore
-
- overlap_only: bpy.props.BoolProperty(
- name="Overlap Only",
- description="Only unwrap objects with overlapping UVs",
- default=False
- ) # type: ignore
-
- bake_pbr: bpy.props.BoolProperty(
- name="Bake PBR Maps",
- description="Bake individual PBR channel maps (BaseColor, Roughness, Metallic, Normal, Emission, Height, AO) "
- "with standard naming for game-engine import",
- default=True
- ) # type: ignore
-
- export_orm: bpy.props.BoolProperty(
- name="Pack ORM Texture",
- description="Create a packed ORM texture (R=AO, G=Roughness, B=Metallic) for Unreal Engine / glTF workflows",
- default=False
- ) # type: ignore
-
- normal_convention: bpy.props.EnumProperty(
- name="Normal Convention",
- description="Normal map Y-axis convention",
- items=[
- ('opengl', 'OpenGL (Y+)', 'Standard OpenGL / glTF / Unity / Blender convention'),
- ('directx', 'DirectX (Y-)', 'DirectX / Unreal Engine convention (flips green channel)'),
- ],
- default='opengl'
- ) # type: ignore
-
- _timer = None
- _objects = []
- _current_index = 0
- _phase = 'unwrap'
-
- # Add properties to track progress
- _progress = 0.0
- _stage = ""
- _current_object = 0
- _total_objects = 0
-
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self._progress = 0
- self._stage = ""
- self._current_object = 0
- self._total_objects = 0
-
- @classmethod
- def poll(self, context):
- if sg_modal_active(context):
- self.poll_message_set("Another operation is in progress")
- return False
- addon_prefs = context.preferences.addons[__package__].preferences
- if not os.path.exists(addon_prefs.output_dir):
- self.poll_message_set("Output directory not set or does not exist (check addon preferences)")
- return False
- return True
-
- def draw(self, context):
- layout = self.layout
- layout.prop(self, "texture_resolution")
- layout.prop(self, "try_unwrap")
- layout.prop(self, "overlap_only")
- layout.prop(self, "add_material")
- layout.prop(self, "flatten_for_refine")
- layout.separator()
- layout.label(text="PBR Export:")
- layout.prop(self, "bake_pbr")
- pbr_col = layout.column()
- pbr_col.enabled = self.bake_pbr
- pbr_col.prop(self, "export_orm")
- pbr_col.prop(self, "normal_convention")
-
- def invoke(self, context, event):
- """
- Invokes the operator.
- :param context: Blender context.
- :param event: Blender event.
- :return: {'RUNNING_MODAL'}
- """
- if context.scene.texture_objects == 'all':
- self._objects = [obj for obj in context.view_layer.objects if obj.type == 'MESH' and not obj.hide_get()]
- else: # 'selected'
- self._objects = [obj for obj in context.selected_objects if obj.type == 'MESH']
- self._current_index = 0
- self._phase = 'unwrap'
- self._total_objects = len(self._objects)
- self._stage = "Preparing"
- return context.window_manager.invoke_props_dialog(self)
-
- def execute(self, context):
- """
- Executes the operator.
- :param context: Blender context.
- :return: {'RUNNING_MODAL'}
- """
-
- self.original_engine = bpy.context.scene.render.engine
- self.original_shading = bpy.context.space_data.shading.type
- self.original_device = bpy.context.scene.cycles.device
- self.original_samples = bpy.context.scene.cycles.samples
- self.original_max_bounces = bpy.context.scene.cycles.max_bounces
- self.original_persistent = bpy.context.scene.render.use_persistent_data
- # Save Cycles compute_device_type so we can restore after GPU bake
- try:
- self._original_compute_device_type = (
- bpy.context.preferences.addons['cycles'].preferences.compute_device_type
- )
- except Exception:
- self._original_compute_device_type = None
- # Set render engine to CYCLES (required for baking)
- bpy.context.scene.render.engine = 'CYCLES'
- # Switch to Solid shading to avoid conflicts with Rendered mode
- # (Rendered viewport + bake = crash). Solid is engine-independent
- # and won't cause pink textures like Material Preview (EEVEE) does.
- for area in context.screen.areas:
- if area.type == 'VIEW_3D':
- for space in area.spaces:
- if space.type == 'VIEW_3D':
- space.shading.type = 'SOLID'
- break
- prepare_baking(context)
-
- # Start modal operation
- context.window_manager.modal_handler_add(self)
- self._timer = context.window_manager.event_timer_add(0.1, window=context.window)
- return {'RUNNING_MODAL'}
-
- def modal(self, context, event):
- """
- Handles modal events.
- :param context: Blender context.
- :param event: Blender event.
- :return: {'PASS_THROUGH'}
- """
- if event.type == 'TIMER':
- def redraw():
- for area in context.screen.areas:
- area.tag_redraw()
- redraw()
-
- # ── PBR channel bake (flat queue, one item per tick) ──────
- if self._phase == 'bake_pbr':
- if self._pbr_queue_idx < len(self._pbr_queue):
- obj, channel = self._pbr_queue[self._pbr_queue_idx]
- nice = _PBR_CHANNEL_SUFFIXES.get(channel, channel)
- self._stage = f"PBR {nice} — {obj.name}"
- self._progress = (self._pbr_queue_idx / len(self._pbr_queue)) * 100
- redraw()
- bake_pbr_channel(
- context, obj, channel,
- self.texture_resolution,
- get_dir_path(context, "baked"))
- self._pbr_queue_idx += 1
- else:
- # DirectX normal flip
- if self.normal_convention == 'directx':
- out_dir = get_dir_path(context, "baked")
- for obj in self._objects:
- _flip_normal_green(
- os.path.join(out_dir,
- f"{obj.name}_Normal.png"))
- # Pack ORM
- if self.export_orm:
- self.report({'INFO'}, "Packing ORM textures...")
- self._phase = 'pack_orm'
- self._current_index = 0
- else:
- self.report({'INFO'}, "Applying materials...")
- self._phase = 'apply_material'
- self._current_index = 0
- return {'PASS_THROUGH'}
-
- if self._current_index < len(self._objects):
- obj = self._objects[self._current_index]
- if self._phase == 'unwrap':
- self._stage = f"Unwrapping {obj.name}"
- self._progress = 0
- redraw()
-
- if self.try_unwrap != 'none':
- if not _has_non_projection_uv(obj):
- # No usable UV exists — must unwrap
- unwrap(obj, self.try_unwrap, self.overlap_only)
- elif self.overlap_only:
- # Only re-unwrap if existing UVs overlap
- if _uvs_likely_overlap(obj):
- unwrap(obj, self.try_unwrap, self.overlap_only)
- else:
- # Always re-unwrap when overlap_only is disabled
- unwrap(obj, self.try_unwrap, self.overlap_only)
-
- self._progress = 100
- elif self._phase == 'bake':
- if self.bake_pbr:
- # PBR mode bakes BaseColor via EMIT — skip the
- # redundant DIFFUSE bake entirely.
- self._progress = 100
- else:
- self._stage = f"Baking {obj.name}"
- self._progress = 0
- redraw()
- if not bake_texture(context, obj, self.texture_resolution, output_dir=get_dir_path(context, "baked")):
- self.report({'ERROR'}, f"Failed to bake texture for {obj.name}. No materials found.")
- context.window_manager.event_timer_remove(self._timer)
- return {'CANCELLED'}
-
- # NEW: optionally flatten into the projection material so you can keep refining
- if self.flatten_for_refine:
- try:
- baked_path = get_file_path(context, "baked", object_name=obj.name)
- flatten_projection_material_for_refine(context, obj, baked_path)
- purge_orphans()
- except Exception as e:
- print(f"[StableGen] Failed to flatten projection material for {obj.name}: {e}")
-
- self._progress = 100
- elif self._phase == 'pack_orm':
- self._stage = f"Packing ORM — {obj.name}"
- self._progress = 0
- redraw()
- _pack_orm_texture(get_dir_path(context, "baked"), obj.name)
- self._progress = 100
- elif self._phase == 'apply_material' and self.add_material:
- self._stage = f"Applying Material to {obj.name}"
- self._progress = 0
- redraw()
- self.add_baked_material(context, obj)
- self._progress = 100
- self._current_index += 1
- if self._current_index < len(self._objects):
- self._current_object = self._current_index
- else:
- if self._phase == 'unwrap':
- self.report({'INFO'}, "Baking textures...")
- self._phase = 'bake'
- self._current_index = 0
- elif self._phase == 'bake':
- if self.bake_pbr:
- self.report({'INFO'}, "Baking PBR channels...")
- self._phase = 'bake_pbr'
- self._pbr_queue = []
- for obj in self._objects:
- if obj.active_material:
- for ch in _find_pbr_bake_sources(obj.active_material):
- self._pbr_queue.append((obj, ch))
- self._pbr_queue_idx = 0
- if not self._pbr_queue:
- # No PBR sources — skip straight to apply
- self.report({'INFO'}, "No PBR channels found, skipping PBR bake...")
- self._phase = 'apply_material'
- self._current_index = 0
- else:
- self.report({'INFO'}, "Applying materials...")
- self._phase = 'apply_material'
- self._current_index = 0
- elif self._phase == 'pack_orm':
- self.report({'INFO'}, "Applying materials...")
- self._phase = 'apply_material'
- self._current_index = 0
- else:
- context.window_manager.event_timer_remove(self._timer)
- bpy.context.scene.render.engine = self.original_engine
- bpy.context.scene.cycles.device = self.original_device
- bpy.context.scene.cycles.samples = self.original_samples
- bpy.context.scene.cycles.max_bounces = self.original_max_bounces
- bpy.context.scene.render.use_persistent_data = self.original_persistent
- # Restore Cycles compute_device_type
- if self._original_compute_device_type is not None:
- try:
- bpy.context.preferences.addons['cycles'].preferences.compute_device_type = (
- self._original_compute_device_type
- )
- except Exception:
- pass
- # Restore original viewport shading
- for area in context.screen.areas:
- if area.type == 'VIEW_3D':
- for space in area.spaces:
- if space.type == 'VIEW_3D':
- space.shading.type = self.original_shading
- break
- self.report({'INFO'}, "Textures baked successfully.")
- remove_empty_dirs(context)
- return {'FINISHED'}
- return {'PASS_THROUGH'}
-
- def add_baked_material(self, context, obj):
- """Create a baked material with standard PBR textures (game-engine ready).
-
- When ``bake_pbr`` is enabled, looks for ``{obj.name}_{Channel}.png``
- files in the baked output directory and wires them to a Principled
- BSDF. Falls back to the single-texture path when PBR maps are absent.
- """
- output_dir = get_dir_path(context, "baked")
-
- mat = bpy.data.materials.new(name=f"{obj.name}_baked")
- obj.data.materials.append(mat)
- mat.use_nodes = True
- nodes = mat.node_tree.nodes
- links = mat.node_tree.links
- for node in nodes:
- nodes.remove(node)
-
- # Assign the new material to all faces
- obj.active_material_index = len(obj.material_slots) - 1
- bpy.context.view_layer.objects.active = obj
- bpy.ops.object.mode_set(mode='EDIT')
- bpy.ops.mesh.select_all(action='SELECT')
- bpy.ops.object.material_slot_assign()
- bpy.ops.object.mode_set(mode='OBJECT')
-
- # Output node
- output_node = nodes.new("ShaderNodeOutputMaterial")
- output_node.location = (800, 0)
-
- # Principled BSDF
- principled = nodes.new("ShaderNodeBsdfPrincipled")
- principled.location = (400, 0)
- # Fallback values when PBR textures are absent:
- # max roughness + zero metallic → matte, non-reflective surface.
- principled.inputs["Roughness"].default_value = 1.0
- principled.inputs["Metallic"].default_value = 0.0
-
- # UV Map
- uv_node = nodes.new("ShaderNodeUVMap")
- if "BakeUV" in [uv.name for uv in obj.data.uv_layers]:
- uv_node.uv_map = "BakeUV"
- else:
- uv_node.uv_map = obj.data.uv_layers[0].name
- uv_node.location = (-600, 0)
-
- # ── Base Color ────────────────────────────────────────────────
- base_path = os.path.join(output_dir, f"{obj.name}_BaseColor.png")
- if not os.path.exists(base_path):
- # Fallback to standard diffuse bake
- base_path = get_file_path(context, "baked", object_name=obj.name)
-
- has_base = os.path.exists(base_path)
- if has_base:
- tex_base = nodes.new("ShaderNodeTexImage")
- tex_base.image = bpy.data.images.load(base_path)
- tex_base.location = (-200, 200)
- tex_base.label = "BaseColor"
- links.new(uv_node.outputs["UV"], tex_base.inputs["Vector"])
-
- links.new(tex_base.outputs["Color"],
- principled.inputs["Base Color"])
- links.new(principled.outputs[0],
- output_node.inputs["Surface"])
- else:
- links.new(principled.outputs[0],
- output_node.inputs["Surface"])
-
- if not self.bake_pbr:
- return
-
- # ── PBR channel textures ──────────────────────────────────────
- y_pos = -100
-
- # Roughness
- rough_path = os.path.join(output_dir, f"{obj.name}_Roughness.png")
- if os.path.exists(rough_path):
- y_pos -= 250
- tex = nodes.new("ShaderNodeTexImage")
- tex.image = bpy.data.images.load(rough_path)
- tex.image.colorspace_settings.name = 'Non-Color'
- tex.location = (-200, y_pos)
- tex.label = "Roughness"
- links.new(uv_node.outputs["UV"], tex.inputs["Vector"])
- links.new(tex.outputs["Color"],
- principled.inputs["Roughness"])
-
- # Metallic
- metal_path = os.path.join(output_dir, f"{obj.name}_Metallic.png")
- if os.path.exists(metal_path):
- y_pos -= 250
- tex = nodes.new("ShaderNodeTexImage")
- tex.image = bpy.data.images.load(metal_path)
- tex.image.colorspace_settings.name = 'Non-Color'
- tex.location = (-200, y_pos)
- tex.label = "Metallic"
- links.new(uv_node.outputs["UV"], tex.inputs["Vector"])
- links.new(tex.outputs["Color"],
- principled.inputs["Metallic"])
-
- # Normal
- normal_path = os.path.join(output_dir, f"{obj.name}_Normal.png")
- if os.path.exists(normal_path):
- y_pos -= 250
- tex = nodes.new("ShaderNodeTexImage")
- tex.image = bpy.data.images.load(normal_path)
- tex.image.colorspace_settings.name = 'Non-Color'
- tex.location = (-200, y_pos)
- tex.label = "Normal"
- links.new(uv_node.outputs["UV"], tex.inputs["Vector"])
- nmap = nodes.new("ShaderNodeNormalMap")
- nmap.location = (100, y_pos)
- nmap.label = "Normal Map"
- # The normal bake uses Cycles NORMAL pass with tangent space,
- # so the texture is always tangent-space regardless of the
- # original projection material's normal mode (world/bump/tangent).
- nmap.space = 'TANGENT'
-
- links.new(tex.outputs["Color"], nmap.inputs["Color"])
- if "Normal" in principled.inputs:
- links.new(nmap.outputs["Normal"],
- principled.inputs["Normal"])
-
- # Emission
- emission_path = os.path.join(output_dir,
- f"{obj.name}_Emission.png")
- if os.path.exists(emission_path):
- y_pos -= 250
- tex = nodes.new("ShaderNodeTexImage")
- tex.image = bpy.data.images.load(emission_path)
- tex.location = (-200, y_pos)
- tex.label = "Emission"
- links.new(uv_node.outputs["UV"], tex.inputs["Vector"])
- em_in = (principled.inputs.get("Emission Color")
- or principled.inputs.get("Emission"))
- if em_in:
- links.new(tex.outputs["Color"], em_in)
- if "Emission Strength" in principled.inputs:
- principled.inputs[
- "Emission Strength"].default_value = 1.0
-
- # Height → Displacement
- height_path = os.path.join(output_dir,
- f"{obj.name}_Height.png")
- if os.path.exists(height_path):
- y_pos -= 250
- tex = nodes.new("ShaderNodeTexImage")
- tex.image = bpy.data.images.load(height_path)
- tex.image.colorspace_settings.name = 'Non-Color'
- tex.location = (-200, y_pos)
- tex.label = "Height"
- links.new(uv_node.outputs["UV"], tex.inputs["Vector"])
- disp = nodes.new("ShaderNodeDisplacement")
- disp.location = (600, -300)
- disp.label = "Displacement"
- disp.inputs["Scale"].default_value = 0.1
- disp.inputs["Midlevel"].default_value = 0.5
- links.new(tex.outputs["Color"], disp.inputs["Height"])
- links.new(disp.outputs["Displacement"],
- output_node.inputs["Displacement"])
-
- # AO → multiply with Base Color
- ao_path = os.path.join(output_dir, f"{obj.name}_AO.png")
- if (os.path.exists(ao_path)
- and principled.inputs["Base Color"].links):
- y_pos -= 250
- tex_ao = nodes.new("ShaderNodeTexImage")
- tex_ao.image = bpy.data.images.load(ao_path)
- tex_ao.image.colorspace_settings.name = 'Non-Color'
- tex_ao.location = (-200, y_pos)
- tex_ao.label = "AO"
- links.new(uv_node.outputs["UV"], tex_ao.inputs["Vector"])
- # Intercept Base Color → Principled with a Multiply
- old_src = principled.inputs[
- "Base Color"].links[0].from_socket
- mix = nodes.new("ShaderNodeMix")
- mix.data_type = 'RGBA'
- mix.blend_type = 'MULTIPLY'
- mix.inputs["Factor"].default_value = 1.0
- mix.location = (200, 150)
- mix.label = "AO Multiply"
- links.new(old_src, mix.inputs[6]) # A
- links.new(tex_ao.outputs["Color"], mix.inputs[7]) # B
- links.new(mix.outputs[2],
- principled.inputs["Base Color"])
-
-
-class ExportOrbitGIF(bpy.types.Operator):
- """Exports a GIF and MP4 animation orbiting the active object"""
- bl_idname = "object.export_orbit_gif"
- bl_label = "Export Orbit GIF/MP4"
- bl_options = {'REGISTER', 'UNDO'}
-
- duration: bpy.props.FloatProperty(
- name="Duration (seconds)",
- description="Duration of the 360-degree orbit",
- default=5.0,
- min=0.1,
- max=60.0
- ) # type: ignore
-
- frame_rate: bpy.props.IntProperty(
- name="Frame Rate (fps)",
- description="Frames per second for the animation",
- default=24,
- min=1,
- max=60
- ) # type: ignore
-
- resolution_percentage: bpy.props.IntProperty(
- name="Resolution %",
- description="Percentage of the scene's render resolution to use",
- default=50,
- min=10,
- max=100,
- subtype='PERCENTAGE'
- ) # type: ignore
-
- samples: bpy.props.IntProperty(
- name="Samples",
- description="Number of render samples per frame",
- default=32,
- min=1,
- max=4096
- ) # type: ignore
-
- engine: bpy.props.EnumProperty(
- name="Render Engine",
- description="Render engine to use",
- items=[
- ('BLENDER_WORKBENCH', "Workbench", "Use Workbench render engine"),
- ('EEVEE', "Eevee", "Use Eevee render engine"),
- ('CYCLES', "Cycles", "Use Cycles render engine")
- ],
- default='CYCLES'
- ) # type: ignore
-
- interpolation: bpy.props.EnumProperty(
- name="Rotation Curve",
- description="Keyframe interpolation for the orbit rotation",
- items=[
- ('LINEAR', "Linear", "Constant speed throughout the orbit"),
- ('BEZIER', "Ease In/Out", "Smooth acceleration and deceleration"),
- ],
- default='LINEAR'
- ) # type: ignore
-
- # ── PBR showcase settings ─────────────────────────────────────────
- use_hdri: bpy.props.BoolProperty(
- name="HDRI Environment",
- description="Use the scene's existing HDRI world (set up via the "
- "Add HDRI operator) for realistic environment lighting",
- default=False
- ) # type: ignore
-
- hdri_rotation: bpy.props.FloatProperty(
- name="Environment Rotation",
- description="Initial Z-rotation offset for the HDRI (degrees)",
- default=0.0,
- min=0.0,
- max=360.0,
- subtype='ANGLE'
- ) # type: ignore
-
- env_mode: bpy.props.EnumProperty(
- name="Environment Mode",
- description="How the environment lighting interacts with the orbit",
- items=[
- ('FIXED', "Fixed",
- "Camera orbits, HDRI stays fixed — reflections shift naturally"),
- ('LOCKED', "Locked",
- "HDRI co-rotates with the camera — lighting stays identical "
- "from every angle (consistent showcase)"),
- ('COUNTER', "Counter-Rotate",
- "HDRI rotates opposite to camera — 2× apparent light change"),
- ('ENV_ONLY', "Environment Only",
- "Camera stays fixed, only the HDRI rotates around the model"),
- ],
- default='FIXED'
- ) # type: ignore
-
- use_denoiser: bpy.props.BoolProperty(
- name="Denoise",
- description="Enable Cycles denoising for cleaner specular "
- "highlights with fewer samples",
- default=True
- ) # type: ignore
-
- use_gpu: bpy.props.BoolProperty(
- name="GPU Compute",
- description="Use GPU for Cycles rendering (Blender 5.1+ only). "
- "Disable to force CPU rendering",
- default=True
- ) # type: ignore
-
- filename_suffix: bpy.props.StringProperty(
- name="Filename Suffix",
- description="Internal: appended to the output filename (e.g. '_no_pbr')",
- default="",
- options={'HIDDEN'}
- ) # type: ignore
-
- _timer = None
- _rendering = False
- _cancelled = False # Added flag to track cancellation
- _handle_complete = None
- _handle_cancel = None
- _initial_settings = {}
- _temp_empty = None # Added for the pivot empty
- _env_mapping_node = None # For HDRI rotation animation
- _original_world_data = None # Store entire world setup for restore
- _output_path = "" # Internal variable to store the final GIF path
- _output_path_mp4 = "" # Internal variable to store the final MP4 path
- _temp_dir = "" # Temporary directory for frames
- _frame_paths = [] # List to store paths of rendered frames
- _original_camera_parent = None # Store original camera parent
- _original_camera_matrix = None # Store original camera matrix
- _original_compute_device_type = None # For GPU/CPU toggle restore
-
- @classmethod
- def poll(cls, context):
- try:
- import imageio
- except ImportError:
- cls.poll_message_set("Python module 'imageio' not installed")
- return False
- if context.active_object is None or context.active_object.type not in {'MESH', 'EMPTY'}:
- cls.poll_message_set("Select a mesh or empty object first")
- return False
- if context.scene.camera is None:
- cls.poll_message_set("No active camera in the scene")
- return False
- if sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- return True
-
- def invoke(self, context, event):
- # Check dependency again in invoke to provide feedback
- try:
- import imageio
- # Check if ffmpeg is likely available for MP4 export
- if not imageio.plugins.ffmpeg.is_available():
- self.report({'WARNING'}, "FFmpeg plugin for imageio not found. MP4 export might fail. Install 'imageio-ffmpeg'.")
- except ImportError:
- self.report({'ERROR'}, "Python module 'imageio' not found. Please install it (e.g., 'pip install imageio imageio-ffmpeg').")
- return {'CANCELLED'}
- except Exception as e:
- self.report({'WARNING'}, f"Could not check imageio ffmpeg availability: {e}. MP4 export might fail.")
-
-
- # Check for active camera
- if not context.scene.camera:
- self.report({'ERROR'}, "No active camera found in the scene.")
- return {'CANCELLED'}
-
- # Determine output paths using get_dir_path
- try:
- revision_dir = get_dir_path(context, "revision")
- os.makedirs(revision_dir, exist_ok=True)
- suffix = self.filename_suffix or ""
- self._output_path = os.path.join(revision_dir, f"orbit{suffix}.gif")
- self._output_path_mp4 = os.path.join(revision_dir, f"orbit{suffix}.mp4") # MP4 path
- except Exception as e:
- self.report({'ERROR'}, f"Could not determine output directory: {e}")
- return {'CANCELLED'}
-
- # Create temporary directory for frames
- try:
- self._temp_dir = tempfile.mkdtemp(prefix="blender_gif_")
- except Exception as e:
- self.report({'ERROR'}, f"Could not create temporary directory: {e}")
- self.cleanup(context) # Clean up if temp dir fails
- return {'CANCELLED'}
-
- return context.window_manager.invoke_props_dialog(self)
-
- @staticmethod
- def _force_fcurve_interpolation(obj, interpolation_type):
- """Set interpolation on all keyframe points of *obj*'s fcurves.
-
- Works on Blender 4.x (action.fcurves) and 5.x+ (layered actions
- with slots / channelbags).
- """
- anim = obj.animation_data
- if not anim or not anim.action:
- return
-
- action = anim.action
- fcurves = []
-
- # Blender 5.x layered-action API (slots → channelbags → fcurves)
- action_slot = getattr(anim, "action_slot", None)
- if action_slot is not None:
- # Try the helper shipped in bpy_extras first
- try:
- from bpy_extras.anim_utils import action_get_channelbag_for_slot
- bag = action_get_channelbag_for_slot(action, action_slot)
- if bag is not None:
- fcurves = list(bag.fcurves)
- except Exception:
- pass
- # Manual fallback: iterate channelbags on the *action*
- if not fcurves:
- for attr in ("channelbags", "channel_bags"):
- bags = getattr(action, attr, None)
- if bags is not None:
- for bag in bags:
- fcurves.extend(bag.fcurves)
- break
-
- # Blender 4.x legacy path
- if not fcurves and hasattr(action, "fcurves"):
- fcurves = list(action.fcurves)
-
- for fc in fcurves:
- for kf in fc.keyframe_points:
- kf.interpolation = interpolation_type
-
- # ── PBR Showcase helpers ──────────────────────────────────────────
-
- def _setup_hdri_environment(self, context):
- """Prepare the scene's existing HDRI world for orbit rendering.
-
- Assumes the world was already set up by the AddHDRI operator
- (or any setup with an Environment Texture node). This method:
-
- 1. Stores the original world state for ``cleanup()``.
- 2. Injects a Mapping node before the Environment Texture to
- allow rotation animation (counter-rotate / env-only modes).
- 3. Applies the user's initial rotation offset.
-
- If no Environment Texture node is found, falls back to creating
- a procedural Nishita sky with a Mapping node.
- """
- world = context.scene.world
- if not world:
- world = bpy.data.worlds.new("SG_OrbitWorld")
- context.scene.world = world
-
- # Store original state for restore
- self._original_world_data = {
- 'world_ref': world,
- 'use_nodes': world.use_nodes,
- 'color': world.color.copy(),
- }
-
- world.use_nodes = True
- tree = world.node_tree
-
- # Find the existing Environment Texture node
- env_tex = None
- for node in tree.nodes:
- if node.type == 'TEX_ENVIRONMENT':
- env_tex = node
- break
-
- if env_tex is None:
- # No HDRI world set up — create a *separate* procedural sky world
- # so the original world node tree is never destroyed.
- print("[StableGen] Orbit GIF: No HDRI found, creating sky fallback "
- "(original world preserved)")
- fallback_world = bpy.data.worlds.new("SG_OrbitSkyFallback")
- fallback_world.use_nodes = True
- fb_tree = fallback_world.node_tree
- fb_tree.nodes.clear()
-
- tex_coord = fb_tree.nodes.new("ShaderNodeTexCoord")
- tex_coord.location = (-800, 300)
-
- mapping = fb_tree.nodes.new("ShaderNodeMapping")
- mapping.location = (-600, 300)
- mapping.inputs["Rotation"].default_value[2] = self.hdri_rotation
- self._env_mapping_node = mapping
-
- sky_tex = fb_tree.nodes.new("ShaderNodeTexSky")
- sky_tex.location = (-300, 300)
- # Set sky type — Blender 5.x replaced 'NISHITA' with new names
- for sky_type in ('NISHITA', 'HOSEK_WILKIE', 'PREETHAM'):
- try:
- sky_tex.sky_type = sky_type
- break
- except TypeError:
- continue
- # Sun attributes vary by sky type / version
- try:
- sky_tex.sun_elevation = math.radians(30)
- sky_tex.sun_rotation = math.radians(45)
- except AttributeError:
- pass
-
- bg_node = fb_tree.nodes.new("ShaderNodeBackground")
- bg_node.location = (0, 300)
-
- output_node = fb_tree.nodes.new("ShaderNodeOutputWorld")
- output_node.location = (200, 300)
-
- fb_tree.links.new(tex_coord.outputs["Generated"],
- mapping.inputs["Vector"])
- fb_tree.links.new(mapping.outputs["Vector"],
- sky_tex.inputs["Vector"])
- fb_tree.links.new(sky_tex.outputs["Color"],
- bg_node.inputs["Color"])
- fb_tree.links.new(bg_node.outputs["Background"],
- output_node.inputs["Surface"])
-
- # Swap the scene world to the fallback; original is stored for restore
- context.scene.world = fallback_world
- self._original_world_data['created_fallback'] = True
- self._original_world_data['fallback_world'] = fallback_world
- return
-
- # ── Inject Mapping node before the Environment Texture ────────
- # Store original Vector input link/socket for restore
- existing_vec_link = None
- if env_tex.inputs["Vector"].links:
- existing_vec_link = env_tex.inputs["Vector"].links[0]
- self._original_world_data['env_vec_from_socket'] = (
- existing_vec_link.from_socket)
-
- # Create TexCoord + Mapping chain
- tex_coord = tree.nodes.new("ShaderNodeTexCoord")
- tex_coord.name = "SG_OrbitTexCoord"
- tex_coord.location = (env_tex.location[0] - 500,
- env_tex.location[1])
-
- mapping = tree.nodes.new("ShaderNodeMapping")
- mapping.name = "SG_OrbitMapping"
- mapping.location = (env_tex.location[0] - 250,
- env_tex.location[1])
- mapping.inputs["Rotation"].default_value[2] = self.hdri_rotation
- self._env_mapping_node = mapping
-
- tree.links.new(tex_coord.outputs["Generated"],
- mapping.inputs["Vector"])
-
- # Remove existing vector link and insert mapping before env tex
- if existing_vec_link:
- # Rewire: old_source → Mapping.Vector, Mapping.out → EnvTex.Vector
- old_source = existing_vec_link.from_socket
- tree.links.remove(existing_vec_link)
- tree.links.new(old_source, mapping.inputs["Vector"])
- # else: TexCoord → Mapping is already connected above
-
- tree.links.new(mapping.outputs["Vector"],
- env_tex.inputs["Vector"])
-
- print("[StableGen] Orbit GIF: using existing HDRI world"
- f" (rotation offset={math.degrees(self.hdri_rotation):.0f}°)")
-
- def _animate_hdri_rotation(self, context, total_frames):
- """Animate the HDRI Mapping node rotation for counter-rotate
- or environment-only modes."""
- if not self._env_mapping_node:
- return
-
- mapping = self._env_mapping_node
- prefs = bpy.context.preferences.edit
- prev_interp = prefs.keyframe_new_interpolation_type
- prefs.keyframe_new_interpolation_type = self.interpolation
- try:
- # Start rotation = current value (may include user offset)
- start_z = mapping.inputs["Rotation"].default_value[2]
- mapping.inputs["Rotation"].default_value[2] = start_z
- mapping.inputs["Rotation"].keyframe_insert(
- data_path="default_value", index=2, frame=1)
-
- # For counter-rotate: rotate in opposite direction to camera
- # For env-only: rotate in positive direction
- if self.env_mode == 'COUNTER':
- end_z = start_z - math.radians(360)
- elif self.env_mode == 'LOCKED':
- # Co-rotate: same direction as camera orbit (+Z)
- end_z = start_z + math.radians(360)
- else:
- end_z = start_z + math.radians(360)
-
- mapping.inputs["Rotation"].default_value[2] = end_z
- mapping.inputs["Rotation"].keyframe_insert(
- data_path="default_value", index=2, frame=total_frames + 1)
- finally:
- prefs.keyframe_new_interpolation_type = prev_interp
-
- # Force interpolation on the world node tree's fcurves
- world = context.scene.world
- if world and world.node_tree:
- self._force_fcurve_interpolation(
- world.node_tree, self.interpolation)
-
- def setup_animation(self, context):
- obj = context.active_object
- scene = context.scene
- active_camera = scene.camera # Use the scene's active camera
-
- if not active_camera: # Should be caught by poll/invoke, but double-check
- raise RuntimeError("No active camera found during setup.")
-
- # Store original camera state
- self._original_camera_parent = active_camera.parent
- self._original_camera_matrix = active_camera.matrix_world.copy()
-
- # Calculate Center (Center of Mass or Bounds)
- cursor_location = scene.cursor.location.copy()
- # Use object bounds center for better visual centering
- bpy.ops.object.select_all(action='DESELECT')
- context.view_layer.objects.active = obj
- obj.select_set(True)
- # Use geometry center for pivot, less prone to being skewed by outliers than bounds
- bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS')
- center_location = obj.matrix_world.translation.copy()
- # Restore cursor location (origin setting might change cursor)
- scene.cursor.location = cursor_location
-
- # Create Temporary Empty at Center
- self._temp_empty = bpy.data.objects.new("OrbitPivot", None)
- self._temp_empty.location = center_location
- scene.collection.objects.link(self._temp_empty)
- # Update matrices after linking and setting location
- context.view_layer.update()
-
- # Calculate camera's local transform relative to the empty *before* parenting
- cam_original_world_matrix = self._original_camera_matrix.copy()
- empty_world_matrix_inv = self._temp_empty.matrix_world.inverted()
- cam_local_matrix = empty_world_matrix_inv @ cam_original_world_matrix
-
- # Parent Active Camera to Empty
- active_camera.parent = self._temp_empty
- # Set the camera's local transform (matrix_basis)
- active_camera.matrix_basis = cam_local_matrix
-
- # Set Up Animation Timing
- total_frames = int(self.duration * self.frame_rate)
- scene.frame_start = 1
- scene.frame_end = total_frames
- scene.render.fps = self.frame_rate
-
- # Animate Empty's Rotation (skip if ENV_ONLY — camera stays fixed)
- if self.env_mode != 'ENV_ONLY':
- # Set keyframe interpolation type before inserting so keyframes
- # are created with the correct curve.
- prefs = bpy.context.preferences.edit
- prev_interp = prefs.keyframe_new_interpolation_type
- prefs.keyframe_new_interpolation_type = self.interpolation
- try:
- self._temp_empty.rotation_euler = (0, 0, 0)
- self._temp_empty.keyframe_insert(data_path="rotation_euler", index=2, frame=1)
-
- self._temp_empty.rotation_euler = (0, 0, math.radians(360))
- self._temp_empty.keyframe_insert(data_path="rotation_euler", index=2, frame=total_frames + 1)
- finally:
- prefs.keyframe_new_interpolation_type = prev_interp
-
- # Post-insert: force the chosen interpolation on all keyframe points.
- # The preference alone doesn't always take effect on Blender 5.x.
- self._force_fcurve_interpolation(self._temp_empty, self.interpolation)
-
- # Animate HDRI environment rotation (locked, counter-rotate, or env-only)
- if self.use_hdri and self.env_mode in ('LOCKED', 'COUNTER', 'ENV_ONLY'):
- self._animate_hdri_rotation(context, total_frames)
-
-
- def execute(self, context):
- scene = context.scene
- render = scene.render
- cycles = scene.cycles # Get cycles settings
-
- # When called via EXEC_DEFAULT (e.g. from the queue) invoke()
- # is skipped, so initialise paths / temp dir here if needed.
- if not self._temp_dir:
- try:
- revision_dir = get_dir_path(context, "revision")
- os.makedirs(revision_dir, exist_ok=True)
- suffix = self.filename_suffix or ""
- self._output_path = os.path.join(revision_dir, f"orbit{suffix}.gif")
- self._output_path_mp4 = os.path.join(revision_dir, f"orbit{suffix}.mp4")
- except Exception as e:
- self.report({'ERROR'}, f"Could not determine output directory: {e}")
- return {'CANCELLED'}
- try:
- self._temp_dir = tempfile.mkdtemp(prefix="blender_gif_")
- except Exception as e:
- self.report({'ERROR'}, f"Could not create temporary directory: {e}")
- return {'CANCELLED'}
-
- # Store Initial Settings
- self._initial_settings = {
- 'frame_start': scene.frame_start,
- 'frame_end': scene.frame_end,
- 'fps': render.fps,
- 'camera': scene.camera, # Store the camera object itself
- 'filepath': render.filepath,
- 'file_format': render.image_settings.file_format,
- 'color_mode': render.image_settings.color_mode,
- 'resolution_x': render.resolution_x,
- 'resolution_y': render.resolution_y,
- 'resolution_percentage': render.resolution_percentage,
- 'use_overwrite': render.use_overwrite,
- 'use_placeholder': render.use_placeholder,
- 'samples': cycles.samples, # Store original samples
- 'engine': scene.render.engine, # Store original engine
- 'film_transparent': render.film_transparent, # Store original film transparency
- 'light': scene.display.shading.light, # Store original shading light
- 'color_type': scene.display.shading.color_type, # Store original shading color type
- 'use_compositing': render.use_compositing, # Store compositor pipeline state
- }
-
- scene.render.engine = get_eevee_engine_id() if self.engine == 'EEVEE' else self.engine
-
- if scene.render.engine == 'BLENDER_WORKBENCH':
- context.scene.display.shading.light = 'STUDIO'
- context.scene.display.shading.color_type = 'SINGLE'
-
- # Disable compositing in the render pipeline so Blender writes
- # the raw ViewLayer result to render.filepath. StableGen's
- # compositor uses File Output nodes without a Composite node,
- # which causes the Composite output (and thus saved frames) to be
- # blank/transparent.
- render.use_compositing = False
-
- # Apply Render Settings for PNG sequence
- # Use a fixed 1024×1024 base so GIF resolution is independent of
- # whatever the generation pipeline left in resolution_x/y.
- render.resolution_x = 1024
- render.resolution_y = 1024
- render.filepath = os.path.join(self._temp_dir, "frame_") # Base path for frames
- render.image_settings.file_format = 'PNG' # Render as PNG sequence
- render.image_settings.color_mode = 'RGBA' # Use RGBA (needed for potential alpha, even if we make background opaque)
- render.resolution_percentage = self.resolution_percentage
- render.use_overwrite = True
- render.use_placeholder = False
- render.film_transparent = True
- cycles.samples = self.samples # Set samples for rendering
-
- # ── GPU / CPU Compute (Blender 5.1+) ───────────────────────────
- if bpy.app.version >= (5, 1, 0) and scene.render.engine == 'CYCLES':
- try:
- cycles_prefs = bpy.context.preferences.addons['cycles'].preferences
- self._original_compute_device_type = cycles_prefs.compute_device_type
- self._initial_settings['cycles_device'] = scene.cycles.device
- if self.use_gpu:
- # Probe GPU backends, pick the first that has devices
- for backend in ('CUDA', 'OPTIX', 'HIP', 'METAL', 'ONEAPI'):
- try:
- cycles_prefs.compute_device_type = backend
- cycles_prefs.get_devices()
- gpu_devs = [d for d in cycles_prefs.devices if d.type == backend]
- if gpu_devs:
- for d in gpu_devs:
- d.use = True
- scene.cycles.device = 'GPU'
- print(f"[GIF Export] Using {backend} GPU: "
- f"{', '.join(d.name for d in gpu_devs)}")
- break
- except Exception:
- continue
- else:
- # No GPU found, fall back to CPU
- cycles_prefs.compute_device_type = 'NONE'
- scene.cycles.device = 'CPU'
- print("[GIF Export] No GPU backend found, falling back to CPU")
- else:
- cycles_prefs.compute_device_type = 'NONE'
- scene.cycles.device = 'CPU'
- except Exception as e:
- print(f"[GIF Export] Could not configure compute device: {e}")
- self._original_compute_device_type = None
-
- # ── PBR Showcase: HDRI Environment ────────────────────────────
- if self.use_hdri:
- self._setup_hdri_environment(context)
-
- # ── PBR Showcase: Cycles Denoiser ─────────────────────────────
- if self.use_denoiser and scene.render.engine == 'CYCLES':
- self._initial_settings['use_denoising'] = cycles.use_denoising
- self._initial_settings['denoiser'] = cycles.denoiser
- cycles.use_denoising = True
- # Prefer OpenImageDenoise (CPU-based, always available)
- try:
- cycles.denoiser = 'OPENIMAGEDENOISE'
- except TypeError:
- pass # Fallback to whatever is default
-
- # Setup Animation
- try:
- self.setup_animation(context)
- except Exception as e:
- # Clean up partially created objects if setup fails
- self.cleanup(context)
- self.report({'ERROR'}, f"Animation setup failed: {e}")
- # Print traceback for debugging
- import traceback
- traceback.print_exc()
- return {'CANCELLED'}
-
- # Start Rendering
- self._rendering = True
- ExportOrbitGIF._rendering = True # class-level flag for external polling
- self._cancelled = False # Reset cancellation flag
- self._frame_paths.clear() # Clear list for new render
- self._handle_complete = bpy.app.handlers.render_complete.append(self.render_complete_handler)
- self._handle_cancel = bpy.app.handlers.render_cancel.append(self.render_cancel_handler)
- # Add handler for each rendered frame to collect paths
- bpy.app.handlers.render_post.append(self.render_post_handler)
-
-
- # Use timer to check render status without blocking UI completely
- self._timer = context.window_manager.event_timer_add(0.5, window=context.window)
- context.window_manager.modal_handler_add(self)
-
- # Start the render process
- bpy.ops.render.render('INVOKE_DEFAULT', animation=True)
-
- self.report({'INFO'}, f"Rendering frames to temporary directory...")
- return {'RUNNING_MODAL'}
-
- def modal(self, context, event):
- if event.type == 'TIMER':
- if not self._rendering:
- # Render finished or cancelled
- context.window_manager.event_timer_remove(self._timer)
-
- files_created = False
- # Check if frames exist AND it wasn't cancelled before trying to create files
- if self._frame_paths and not self._cancelled:
- files_created = self.create_output_files() # Call create_output_files
-
- # Perform cleanup regardless
- self.cleanup(context)
-
- # Check for error AFTER cleanup, using the cancellation flag
- if not files_created and not self._cancelled: # Use self._cancelled flag
- self.report({'ERROR'}, "No frames rendered or collected, or output file creation failed.")
- elif files_created: # Report success only if files were made
- self.report({'INFO'}, f"GIF saved to {self._output_path}, MP4 saved to {self._output_path_mp4}")
-
-
- return {'FINISHED'}
- # Allow render window events to pass through
- return {'PASS_THROUGH'}
-
-
- def cleanup(self, context):
- # Remove handlers first to prevent them running during cleanup
- if self._handle_complete in bpy.app.handlers.render_complete:
- bpy.app.handlers.render_complete.remove(self._handle_complete)
- if self._handle_cancel in bpy.app.handlers.render_cancel:
- bpy.app.handlers.render_cancel.remove(self._handle_cancel)
- if self.render_post_handler in bpy.app.handlers.render_post:
- bpy.app.handlers.render_post.remove(self.render_post_handler)
- self._handle_complete = None
- self._handle_cancel = None
-
- # Restore initial settings
- scene = context.scene
- render = scene.render
- cycles = scene.cycles # Get cycles settings
- original_camera = self._initial_settings.get('camera')
-
- # Restore camera parent and transform BEFORE restoring scene.camera setting
- if original_camera and original_camera.name in bpy.data.objects:
- try:
- # Check if it's still parented to the temp empty before unparenting
- if original_camera.parent == self._temp_empty:
- original_camera.parent = self._original_camera_parent
- # Restore world matrix after unparenting
- original_camera.matrix_world = self._original_camera_matrix
- else:
- # If parent changed unexpectedly, just restore matrix
- print("Warning: Camera parent changed during render, restoring world matrix only.")
- original_camera.matrix_world = self._original_camera_matrix
- # Clear potentially stale parent inverse matrix
- original_camera.matrix_parent_inverse.identity()
-
- except Exception as e:
- print(f"Warning: Could not restore camera state for '{original_camera.name}': {e}")
-
- # Restore other settings
- for key, value in self._initial_settings.items():
- if key == 'camera':
- # Restore the scene's active camera object if it exists
- if value and value.name in bpy.data.objects:
- scene.camera = value
- continue # Already handled transform/parent above
- try:
- # Handle nested properties correctly
- if key == 'file_format':
- setattr(render.image_settings, 'file_format', value)
- elif key == 'color_mode':
- setattr(render.image_settings, 'color_mode', value)
- elif key in ('samples', 'use_denoising', 'denoiser'):
- setattr(cycles, key, value)
- elif key == 'cycles_device':
- cycles.device = value
- elif key == 'engine': # Restore engine
- setattr(render, key, value)
- elif key == 'film_transparent': # Restore film transparency
- setattr(render, key, value)
- elif key == 'use_compositing': # Restore compositor pipeline state
- render.use_compositing = value
- elif key in ('resolution_x', 'resolution_y'):
- setattr(render, key, value)
- elif hasattr(render, key):
- setattr(render, key, value)
- elif hasattr(scene, key):
- setattr(scene, key, value)
- except Exception as e:
- print(f"Warning: Could not restore setting '{key}': {e}")
-
- self._initial_settings = {} # Clear stored settings
- self._original_camera_parent = None
- self._original_camera_matrix = None
-
- # Restore GPU/CPU compute device type (Blender 5.1+)
- if self._original_compute_device_type is not None:
- try:
- cycles_prefs = bpy.context.preferences.addons['cycles'].preferences
- cycles_prefs.compute_device_type = self._original_compute_device_type
- except Exception as e:
- print(f"Warning: Could not restore compute device type: {e}")
- self._original_compute_device_type = None
-
- # Remove temporary empty
- if self._temp_empty:
- # Remove animation data
- if self._temp_empty.animation_data and self._temp_empty.animation_data.action:
- action = self._temp_empty.animation_data.action
- # Check if action exists before removing
- if action and action.name in bpy.data.actions:
- bpy.data.actions.remove(action)
- # Unlink and remove object
- if self._temp_empty.name in context.scene.collection.objects:
- context.scene.collection.objects.unlink(self._temp_empty)
- if self._temp_empty.name in bpy.data.objects:
- bpy.data.objects.remove(self._temp_empty, do_unlink=True)
- self._temp_empty = None
-
- # Restore the world environment (remove injected mapping nodes)
- if self._original_world_data:
- try:
- world = self._original_world_data['world_ref']
-
- if self._original_world_data.get('created_fallback'):
- # We swapped in a separate fallback world — restore original
- fallback_world = self._original_world_data.get(
- 'fallback_world')
-
- # Clean up animation data on the fallback world
- if (fallback_world and fallback_world.node_tree
- and fallback_world.node_tree.animation_data):
- action = fallback_world.node_tree.animation_data.action
- if action and action.name in bpy.data.actions:
- bpy.data.actions.remove(action)
- fallback_world.node_tree.animation_data_clear()
-
- # Restore the original world
- if world and world.name in bpy.data.worlds:
- bpy.context.scene.world = world
- # Remove the temporary fallback world datablock
- if (fallback_world
- and fallback_world.name in bpy.data.worlds):
- bpy.data.worlds.remove(fallback_world)
- elif world and world.name in bpy.data.worlds:
- # Remove HDRI animation keyframes on the original world
- if world.node_tree and world.node_tree.animation_data:
- action = world.node_tree.animation_data.action
- if action and action.name in bpy.data.actions:
- bpy.data.actions.remove(action)
- world.node_tree.animation_data_clear()
-
- # We injected SG_OrbitMapping + SG_OrbitTexCoord
- # into the existing tree. Remove them and restore
- # the original vector link.
- tree = world.node_tree
- if tree:
- # Find the env tex and restore its vector input
- env_tex = None
- for node in tree.nodes:
- if node.type == 'TEX_ENVIRONMENT':
- env_tex = node
- break
-
- orig_socket = self._original_world_data.get(
- 'env_vec_from_socket')
- if env_tex and orig_socket:
- tree.links.new(orig_socket,
- env_tex.inputs["Vector"])
- elif env_tex:
- # Had no vector link originally — just remove
- for link in list(env_tex.inputs["Vector"].links):
- tree.links.remove(link)
-
- # Remove injected nodes
- for name in ("SG_OrbitMapping", "SG_OrbitTexCoord"):
- if name in tree.nodes:
- tree.nodes.remove(tree.nodes[name])
- except Exception as e:
- print(f"Warning: Could not restore world environment: {e}")
- self._original_world_data = None
- self._env_mapping_node = None
-
-
- self._rendering = False # Ensure rendering flag is reset
- ExportOrbitGIF._rendering = False # class-level flag for external polling
- self._cancelled = False # Reset cancellation flag
-
- # Clean up temporary directory if it exists
- if self._temp_dir and os.path.exists(self._temp_dir):
- try:
- shutil.rmtree(self._temp_dir)
- print(f"Removed temporary directory: {self._temp_dir}")
- except Exception as e:
- print(f"Warning: Could not remove temporary directory '{self._temp_dir}': {e}")
- self._temp_dir = ""
- self._frame_paths.clear() # Clear frame paths
-
-
- def render_post_handler(self, scene, _):
- """Called after each frame is rendered."""
- if self._rendering and self._temp_dir: # Check temp_dir exists
- frame_num = scene.frame_current
- # Construct the expected filename based on Blender's padding
- filename = f"frame_{frame_num:04d}.png"
- filepath = os.path.join(self._temp_dir, filename)
- if os.path.exists(filepath):
- self._frame_paths.append(filepath)
- else:
- # Check if render path uses frame number suffix differently
- # Use scene.render.frame_path() which respects output settings
- alt_filepath = scene.render.frame_path(frame=frame_num)
- if os.path.exists(alt_filepath):
- self._frame_paths.append(alt_filepath)
- # print(f"Used alternative frame path: {alt_filepath}") # Less verbose
- else:
- print(f"Warning: Frame file not found after render: {filepath} or {alt_filepath}")
-
-
- def render_complete_handler(self, scene, _):
- try:
- # Only respond if we're still in the rendering phase
- if not getattr(self, "_rendering", False):
- return
-
- print("Frame rendering complete.")
- # Signal that rendering is done so modal can wrap up
- self._rendering = False
-
- # Unregister this handler so it won't fire after operator finishes
- # Check if handler exists before removing
- if hasattr(self, 'render_complete_handler') and self.render_complete_handler in bpy.app.handlers.render_complete:
- try:
- bpy.app.handlers.render_complete.remove(self.render_complete_handler)
- except ValueError:
- pass # Ignore if already removed elsewhere
- except ReferenceError:
- # RNA is gone ignore
- pass
- except Exception as e:
- print(f"Error in render_complete_handler: {e}") # Log other potential errors
-
-
- def render_cancel_handler(self, scene, _):
- if self._rendering: # Check if it was our render job that was cancelled
- self.report({'WARNING'}, "Render cancelled by user.")
- self._cancelled = True # Set the cancellation flag
- self._rendering = False # Signal modal loop to finish and cleanup
- # Unregister this handler
- if hasattr(self, 'render_cancel_handler') and self.render_cancel_handler in bpy.app.handlers.render_cancel:
- try:
- bpy.app.handlers.render_cancel.remove(self.render_cancel_handler)
- except ValueError:
- pass # Ignore if already removed
-
-
- def create_output_files(self):
- """Creates the GIF and MP4 from the rendered frames using imageio. Returns True on success, False otherwise."""
- if not self._frame_paths:
- # Error is reported in modal loop if needed
- return False
-
- print(f"Assembling output files from {len(self._frame_paths)} frames...")
- gif_success = False
- mp4_success = False
-
- try:
- # Sort frames numerically just in case paths weren't added perfectly in order
- self._frame_paths.sort()
-
- images = []
- for filename in self._frame_paths:
- try:
- images.append(imageio.imread(filename))
- except FileNotFoundError:
- print(f"Warning: Frame file disappeared before processing: {filename}")
- continue # Skip missing frame
-
- if not images: # Check if any images were actually loaded
- self.report({'ERROR'}, "No valid frame images found to create output files.")
- return False
-
- # Calculate duration per frame for imageio (in seconds)
- frame_duration = 1.0 / self.frame_rate
-
- # --- Create GIF ---
- print(f"Creating GIF: {self._output_path}")
- try:
- # imageio v3+ uses 'duration' in seconds per frame
- imageio.mimsave(self._output_path, images, fps=self.frame_rate, loop=0, disposal=2) # loop=0 means infinite loop
- print(f"GIF saved successfully.")
- gif_success = True
- except TypeError:
- # Fallback if 'duration' is not accepted
- imageio.mimsave(self._output_path, images, duration=frame_duration, loop=0)
- print(f"GIF saved successfully (fallback duration).")
- gif_success = True
- except Exception as e:
- self.report({'ERROR'}, f"Failed to create GIF: {e}")
- # Print traceback for debugging GIF errors
- import traceback
- traceback.print_exc()
-
- # --- Create MP4 ---
- print(f"Creating MP4: {self._output_path_mp4}")
- try:
- imageio.mimsave(self._output_path_mp4, images, format='mp4', fps=self.frame_rate, quality=8)
- print(f"MP4 saved successfully.")
- mp4_success = True
- except ImportError:
- self.report({'ERROR'}, "Python module 'imageio' or its 'ffmpeg' plugin not found/configured correctly. Cannot create MP4.")
- except Exception as e:
- self.report({'ERROR'}, f"Failed to create MP4: {e}")
- # Print traceback for debugging MP4 errors
- import traceback
- traceback.print_exc()
-
-
- return gif_success and mp4_success # Return True only if both succeed
-
- except ImportError:
- # This top-level catch handles if imageio itself wasn't imported initially
- self.report({'ERROR'}, "Python module 'imageio' not found. Cannot create output files.")
- return False
- except Exception as e:
- # Catch other potential errors during image loading or sorting
- self.report({'ERROR'}, f"Failed during output file creation process: {e}")
- import traceback
- traceback.print_exc()
- return False
-
-
-class ExportForGameEngine(bpy.types.Operator):
- """Export textured objects for game engines with PBR textures.
-
- Wraps Blender's glTF / FBX exporters with game-engine-optimal settings
- and auto-saves to the StableGen output directory."""
- bl_idname = "object.export_game_engine"
- bl_label = "Export for Game Engine"
- bl_options = {'REGISTER'}
-
- export_format: bpy.props.EnumProperty(
- name="Format",
- description="Export file format",
- items=[
- ('GLB', 'glTF Binary (.glb)',
- 'Single-file binary glTF — best for web, Unity, Godot'),
- ('GLTF_SEPARATE', 'glTF + Textures (.gltf)',
- 'Separate .gltf, .bin and texture files'),
- ('FBX', 'FBX (.fbx)',
- 'Autodesk FBX with embedded textures — Unreal Engine'),
- ],
- default='GLB'
- ) # type: ignore
-
- export_scope: bpy.props.EnumProperty(
- name="Objects",
- description="Which objects to export",
- items=[
- ('SELECTED', 'Selected', 'Export selected mesh objects'),
- ('ALL_MESH', 'All Mesh', 'Export all visible mesh objects'),
- ],
- default='SELECTED'
- ) # type: ignore
-
- apply_transforms: bpy.props.BoolProperty(
- name="Apply Transforms",
- description="Apply location, rotation and scale before export",
- default=True
- ) # type: ignore
-
- export_animations: bpy.props.BoolProperty(
- name="Export Animations",
- description="Include animations in the export",
- default=False
- ) # type: ignore
-
- @classmethod
- def poll(cls, context):
- addon_prefs = context.preferences.addons[__package__].preferences
- if not os.path.exists(addon_prefs.output_dir):
- cls.poll_message_set("Output directory not set or does not exist (check addon preferences)")
- return False
- if sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- return True
-
- def draw(self, context):
- layout = self.layout
- layout.prop(self, "export_format")
- layout.prop(self, "export_scope")
- layout.prop(self, "apply_transforms")
- layout.prop(self, "export_animations")
-
- def invoke(self, context, event):
- return context.window_manager.invoke_props_dialog(self)
-
- def execute(self, context):
- # ── Prepare selection ─────────────────────────────────────────
- original_selection = list(context.selected_objects)
- original_active = context.view_layer.objects.active
-
- if self.export_scope == 'ALL_MESH':
- bpy.ops.object.select_all(action='DESELECT')
- meshes = [o for o in context.view_layer.objects
- if o.type == 'MESH' and not o.hide_get()]
- for o in meshes:
- o.select_set(True)
- if meshes:
- context.view_layer.objects.active = meshes[0]
- else:
- meshes = [o for o in context.selected_objects
- if o.type == 'MESH']
- if not meshes:
- self.report({'ERROR'}, "No mesh objects selected.")
- return {'CANCELLED'}
-
- # ── Output path ───────────────────────────────────────────────
- output_base = get_dir_path(context, "baked")
- os.makedirs(output_base, exist_ok=True)
-
- # Build filename from first object name
- base_name = meshes[0].name if meshes else "export"
-
- if self.export_format == 'FBX':
- ext = ".fbx"
- elif self.export_format == 'GLTF_SEPARATE':
- ext = ".gltf"
- else:
- ext = ".glb"
- filepath = os.path.join(output_base, f"{base_name}{ext}")
-
- # ── Apply transforms if requested ─────────────────────────────
- if self.apply_transforms:
- for o in meshes:
- o.select_set(True)
- bpy.ops.object.transform_apply(
- location=True, rotation=True, scale=True)
-
- # ── Export ────────────────────────────────────────────────────
- try:
- if self.export_format in ('GLB', 'GLTF_SEPARATE'):
- bpy.ops.export_scene.gltf(
- filepath=filepath,
- export_format=self.export_format,
- use_selection=True,
- export_materials='EXPORT',
- export_image_format='AUTO',
- export_tangents=True,
- export_yup=True,
- export_apply=False,
- export_animations=self.export_animations,
- )
- else: # FBX
- bpy.ops.export_scene.fbx(
- filepath=filepath,
- use_selection=True,
- apply_scale_options='FBX_SCALE_ALL',
- path_mode='COPY',
- embed_textures=True,
- bake_anim=self.export_animations,
- mesh_smooth_type='FACE',
- )
-
- self.report({'INFO'},
- f"Exported to {filepath}")
- except Exception as e:
- self.report({'ERROR'}, f"Export failed: {e}")
- import traceback
- traceback.print_exc()
-
- # ── Restore selection ─────────────────────────────────────────
- bpy.ops.object.select_all(action='DESELECT')
- for o in original_selection:
- try:
- o.select_set(True)
- except ReferenceError:
- pass
- if original_active:
- try:
- context.view_layer.objects.active = original_active
- except ReferenceError:
- pass
-
- return {'FINISHED'}
\ No newline at end of file
diff --git a/stablegen/stablegen.py b/stablegen/stablegen.py
deleted file mode 100644
index 9600154..0000000
--- a/stablegen/stablegen.py
+++ /dev/null
@@ -1,3561 +0,0 @@
-""" This file contains the operators and panels for the StableGen addon """
-# disable import-error because pylint doesn't recognize the blenders internal modules
-import os
-import sys
-import bpy # pylint: disable=import-error
-import mathutils # pylint: disable=import-error
-import math # pylint: disable=import-error
-from .utils import sg_modal_active
-
-
-def _is_refreshing():
- """Return True while async model-list refreshes are in-flight.
-
- If the counter has been stuck for longer than ``_REFRESH_TIMEOUT``
- seconds (e.g. due to a lost timer), force-reset it to 0 so the UI
- is not permanently blocked.
- """
- import time as _time
- pkg = sys.modules.get(__package__)
- count = getattr(pkg, '_pending_refreshes', 0)
- if count <= 0:
- return False
- started = getattr(pkg, '_refresh_started_at', 0.0)
- if started > 0 and (_time.monotonic() - started) > getattr(pkg, '_REFRESH_TIMEOUT', 30.0):
- # Safety net: force-clear a stuck counter
- print(f"[StableGen] Refreshing model lists stuck for >{getattr(pkg, '_REFRESH_TIMEOUT', 30.0):.0f}s – resetting.")
- pkg._pending_refreshes = 0
- pkg._refresh_started_at = 0.0
- return False
- return True
-
-# Stock presets
-PRESETS = {
- "DEFAULT": {"description": "Default settings for general purpose generation", "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": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "hm-mvgd-hm", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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": False, "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, "pbr_decomposition": False, "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': 'sdxl_lightning_8step_lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]},
- "MODEL IS IMPORTANT": {"description": "Same as default, but is more guided by the model", "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": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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": False, "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, "pbr_decomposition": False, "controlnet_units": [{'unit_type': 'depth', 'model_name': 'controlnet_depth_sdxl.safetensors', 'strength': 0.75, '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}]},
- "CHARACTERS": {"description": "Optimized settings for character generation", "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": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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_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, "pbr_decomposition": False, "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': 'sdxl_lightning_8step_lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]},
- "CHARACTERS (ALTERNATIVE MASKING)": {"description": "Optimized for character generation. Uses alternative masking parameters to be more consistent between images, but may produce more artifacts. Try if \"Characters\" fails.", "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": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.5, "sequential_factor_smooth": 0.3499999940395355, "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": 10, "blur_mask_sigma": 1.0, "grow_mask_by": 3, "canny_threshold_low": 0, "canny_threshold_high": 80, "pbr_decomposition": False, "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': 'sdxl_lightning_8step_lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]},
- "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, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "grid", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "pbr_decomposition": False, "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, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "uv_inpaint", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "pbr_decomposition": False, "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, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "separate", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "pbr_decomposition": False, "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 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","qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": [{'model_name': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
- "QWEN 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", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 6.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": [{'model_name': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
- "QWEN 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", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "early_priority": True, "early_priority_strength": 0.5, "pbr_decomposition": False, "lora_units": [{'model_name': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
- "QWEN VORONOI": {"description": "Voronoi projection mode with exponent 1000 for hard camera segmentation during generation, then resets to 15 for softer blending. Each surface point is dominated by its closest camera. Based on Qwen Precise.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 1000.0, "weight_exponent_generation_only": True, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": True, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": [{'model_name': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
- "QWEN PRECISE (NUNCHAKU)": {"description": "Precise detail using Nunchaku. Meant to be used with the Nunchaku model which has the 4-step Lightning LoRA included. Requires Nunchaku nodes.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": []},
- "QWEN SAFE (NUNCHAKU)": {"description": "Safer fallback using Nunchaku. Meant to be used with the Nunchaku model which has the 4-step Lightning LoRA included. Requires Nunchaku nodes.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 6.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": []},
- "QWEN ALT (NUNCHAKU)": {"description": "Balanced option using Nunchaku. Meant to be used with the Nunchaku model which has the 4-step Lightning LoRA included. Requires Nunchaku nodes.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "early_priority": True, "early_priority_strength": 0.5, "pbr_decomposition": False, "lora_units": []},
- "QWEN VORONOI (NUNCHAKU)": {"description": "Voronoi projection mode using Nunchaku. Exponent 1000 for hard camera segmentation during generation, then resets to 15 for softer blending. Requires Nunchaku nodes.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 1000.0, "weight_exponent_generation_only": True, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": True, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": []},
- "KLEIN PRECISE": {"description": "FLUX.2 Klein with precise detail when camera overlap is good. Uses depth reference images with CFGGuider (cfg=1). Best for well-covered geometry.", "control_after_generate": "fixed", "model_architecture": "flux2_klein", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour.", "qwen_custom_prompt_seq_none": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour, adopting the visual style from image 2.", "qwen_custom_prompt_seq_replace": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray.", "qwen_custom_prompt_seq_additional": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray. Image 3 represents the overall style.", "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": []},
- "KLEIN SAFE": {"description": "FLUX.2 Klein safer fallback when coverage is limited. Uses the previous view instead of context renders with depth references and CFGGuider (cfg=1).", "control_after_generate": "fixed", "model_architecture": "flux2_klein", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 6.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour.", "qwen_custom_prompt_seq_none": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour, adopting the visual style from image 2.", "qwen_custom_prompt_seq_replace": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray.", "qwen_custom_prompt_seq_additional": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray. Image 3 represents the overall style.", "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": []},
- "KLEIN ALT": {"description": "FLUX.2 Klein balanced option that mixes additional context renders with sequential references. Uses depth references and CFGGuider (cfg=1).", "control_after_generate": "fixed", "model_architecture": "flux2_klein", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour.", "qwen_custom_prompt_seq_none": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour, adopting the visual style from image 2.", "qwen_custom_prompt_seq_replace": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray.", "qwen_custom_prompt_seq_additional": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray. Image 3 represents the overall style.", "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "early_priority": True, "early_priority_strength": 0.5, "pbr_decomposition": False, "lora_units": []},
- "KLEIN VORONOI": {"description": "FLUX.2 Klein voronoi projection mode. Exponent 1000 for hard camera segmentation during generation, then resets to 15 for softer blending. Uses depth references and CFGGuider (cfg=1).", "control_after_generate": "fixed", "model_architecture": "flux2_klein", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 1000.0, "weight_exponent_generation_only": True, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": True, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour.", "qwen_custom_prompt_seq_none": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour, adopting the visual style from image 2.", "qwen_custom_prompt_seq_replace": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray.", "qwen_custom_prompt_seq_additional": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray. Image 3 represents the overall style.", "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": []},
- "LOCAL REFINE": {"description": "Uses the SDXL local edit mode to improve detail / refine specific areas. Use new set of cameras or a single camera pointed at the area you want to refine.", "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, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "local_edit", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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.10000000149011612, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "original_render", "sequential_desaturate_factor": 0.10000000149011612, "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_voronoi_mode": True, "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 and transfer the format of '{main_prompt}' in image 1 to the style from image 2", "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": 5.0, "qwen_context_cleanup_value_adjust": 0.0, "qwen_context_fallback_dilation": 1, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05, "visibility_vignette": True, "visibility_vignette_width": 0.3, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "pbr_decomposition": False, "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': 'sdxl_lightning_8step_lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]},
- "LOCAL EDIT (QWEN)": {"description": "Uses Qwen to make targeted edits to specific areas. Point cameras at what you want to change — you can alter colors, style, rewrite text, add details, and more. Untouched areas are preserved.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "local_edit", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": 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": False, "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, "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_voronoi_mode": False, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": False, "refine_angle_ramp_active": False, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05, "visibility_vignette": True, "visibility_vignette_width": 0.1, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6, "refine_edge_feather_projection": True, "refine_edge_feather_width": 15, "refine_edge_feather_softness": 1.0, "pbr_decomposition": False, "lora_units": [{'model_name': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
- "REFINE (QWEN)": {"description": "Uses Qwen to restyle or globally modify the entire existing texture. Applies changes uniformly across all camera views — ideal for changing the overall color scheme, art style, or surface appearance.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "refine", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": 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": False, "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, "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_voronoi_mode": False, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": False, "pbr_decomposition": False, "lora_units": [{'model_name': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
- "DEFAULT (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and SDXL to generate a textured mesh. Optimized for general object generation. May not work ideally for the cases which have their specialized presets.", "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": 75.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 85.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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.10000000149011612, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "trellis2_input", "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, "early_priority": False, "early_priority_strength": 0.5, "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, "qwen_guidance_map_type": "depth", "qwen_voronoi_mode": False, "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 and transfer the format of '{main_prompt}' in image 1 to the style from image 2", "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": 5.0, "qwen_context_cleanup_value_adjust": 0.0, "qwen_context_fallback_dilation": 1, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "sdxl", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 10, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": False, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": False, "trellis2_max_elevation": 1.2216999530792236, "trellis2_min_elevation": -1.0471975803375244, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": False, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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}]},
- "TRELLIS.2 (MESH ONLY)": {"description": "Uses TRELLIS.2 to generate a mesh without texturing. Useful when you only need geometry or plan to texture manually.", "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": 75.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 85.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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.10000000149011612, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "trellis2_input", "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, "early_priority": False, "early_priority_strength": 0.5, "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, "qwen_guidance_map_type": "depth", "qwen_voronoi_mode": False, "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 and transfer the format of '{main_prompt}' in image 1 to the style from image 2", "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": 5.0, "qwen_context_cleanup_value_adjust": 0.0, "qwen_context_fallback_dilation": 1, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "none", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 10, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": False, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": False, "trellis2_max_elevation": 1.2216999530792236, "trellis2_min_elevation": -1.0471975803375244, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": False, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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}]},
- "CHARACTERS (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and SDXL to generate a textured mesh. Optimized settings for generating characters. Will not texture bottom facing faces.", "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": 75.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 85.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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.10000000149011612, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "trellis2_input", "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, "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_voronoi_mode": False, "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 and transfer the format of '{main_prompt}' in image 1 to the style from image 2", "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": 5.0, "qwen_context_cleanup_value_adjust": 0.0, "qwen_context_fallback_dilation": 1, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "sdxl", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 8, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": True, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": True, "trellis2_max_elevation": 0.8726646259971648, "trellis2_min_elevation": -0.8726646259971648, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": False, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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}]},
- "ARCHITECTURE (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and SDXL to generate a textured mesh. Optimized settings for architecture, and other models with flat walls and sharp angles.", "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, "discard_factor_generation_only": True, "discard_factor_after_generation": 85.0, "weight_exponent": 10.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "separate", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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": "trellis2_input", "sequential_desaturate_factor": 0.0, "sequential_contrast_factor": 0.0, "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 0.800000011920929, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "early_priority": False, "early_priority_strength": 0.5, "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, "qwen_guidance_map_type": "depth", "qwen_voronoi_mode": False, "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 and transfer the format of '{main_prompt}' in image 1 to the style from image 2", "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": 5.0, "qwen_context_cleanup_value_adjust": 0.0, "qwen_context_fallback_dilation": 1, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "sdxl", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 8, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": True, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": False, "trellis2_max_elevation": 1.2216999530792236, "trellis2_min_elevation": -1.0471975803375244, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": False, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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 PRECISE (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and Qwen Image Edit to generate a textured mesh. Precise detail when camera overlap is good. Relies on context renders plus the prompt.", "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, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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, "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_voronoi_mode": False, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "sg_silhouette_margin": 3, "sg_silhouette_depth": 0.05000000074505806, "sg_silhouette_rays": "4", "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "qwen_image_edit", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 8, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": True, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_seed": 0, "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": True, "trellis2_max_elevation": 0.8726646259971648, "trellis2_min_elevation": -0.8726646259971648, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": True, "qwen_trellis2_style_initial_only": True, "trellis2_skip_texture": True, "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': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
- "QWEN SAFE (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and Qwen Image Edit to generate a textured mesh. Safer fallback when coverage is limited. Uses TRELLIS.2 input image as sequential style reference for global coherence.", "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, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 6.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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.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_voronoi_mode": False, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "sg_silhouette_margin": 3, "sg_silhouette_depth": 0.05000000074505806, "sg_silhouette_rays": "4", "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "qwen_image_edit", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 8, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": True, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_seed": 0, "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": True, "trellis2_max_elevation": 0.8726646259971648, "trellis2_min_elevation": -0.8726646259971648, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": True, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
- "QWEN ALT (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and Qwen Image Edit to generate a textured mesh. Uses ADDITIONAL context renders with TRELLIS.2 style transfer for consistency. Good general-purpose Qwen pipeline.", "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, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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.10000000149011612, "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 1.0, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "early_priority": True, "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_voronoi_mode": False, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "sg_silhouette_margin": 3, "sg_silhouette_depth": 0.05000000074505806, "sg_silhouette_rays": "4", "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "qwen_image_edit", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 8, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": True, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_seed": 0, "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": True, "trellis2_max_elevation": 0.8726646259971648, "trellis2_min_elevation": -0.8726646259971648, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": True, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
- "QWEN VORONOI (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and Qwen Image Edit to generate a textured mesh. Voronoi projection with exponent 1000 for hard camera segmentation during generation, then resets to 15 for softer blending.", "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, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 1000.0, "weight_exponent_generation_only": True, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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, "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_voronoi_mode": True, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "sg_silhouette_margin": 3, "sg_silhouette_depth": 0.05000000074505806, "sg_silhouette_rays": "4", "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "qwen_image_edit", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 8, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": True, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_seed": 0, "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": True, "trellis2_max_elevation": 0.8726646259971648, "trellis2_min_elevation": -0.8726646259971648, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": True, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
-}
-
-# Global list of all generation parameter names to check for a preset.
-GEN_PARAMETERS = [
- "control_after_generate",
- "model_architecture",
- "steps",
- "cfg",
- "sampler",
- "scheduler",
- "fallback_color",
- "discard_factor",
- "discard_factor_generation_only",
- "discard_factor_after_generation",
- "weight_exponent",
- "weight_exponent_generation_only",
- "weight_exponent_after_generation",
- "view_blend_use_color_match",
- "view_blend_color_match_method",
- "view_blend_color_match_strength",
- "clip_skip",
- "auto_rescale",
- "auto_rescale_target_mp",
- "overwrite_material",
- "generation_method",
- "refine_images",
- "refine_steps",
- "refine_sampler",
- "refine_scheduler",
- "denoise",
- "refine_cfg",
- "refine_prompt",
- "refine_upscale_method",
- "sequential_smooth",
- "sequential_custom_camera_order",
- "sequential_factor",
- "sequential_factor_smooth",
- "sequential_factor_smooth_2",
- "sequential_ipadapter",
- "sequential_ipadapter_mode",
- "sequential_desaturate_factor",
- "sequential_contrast_factor",
- "sequential_ipadapter_regenerate",
- "ipadapter_weight_type",
- "ipadapter_strength",
- "ipadapter_start",
- "ipadapter_end",
- "early_priority",
- "early_priority_strength",
- "differential_diffusion",
- "differential_noise",
- "blur_mask",
- "blur_mask_radius",
- "blur_mask_sigma",
- "grow_mask_by",
- "canny_threshold_low",
- "canny_threshold_high",
- "qwen_guidance_map_type",
- "qwen_voronoi_mode",
- "qwen_use_external_style_image",
- "qwen_external_style_image",
- "qwen_context_render_mode",
- "qwen_external_style_initial_only",
- "qwen_use_custom_prompts",
- "qwen_custom_prompt_initial",
- "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",
- "qwen_prompt_gray_background",
- "qwen_rescale_alignment",
- "qwen_generation_method",
- "qwen_refine_use_prev_ref",
- "qwen_refine_use_depth",
- "qwen_timestep_zero_ref",
- "refine_angle_ramp_active",
- "refine_angle_ramp_pos_0",
- "refine_angle_ramp_pos_1",
- "visibility_vignette",
- "visibility_vignette_width",
- "visibility_vignette_softness",
- "visibility_vignette_blur",
- "sg_silhouette_margin",
- "sg_silhouette_depth",
- "sg_silhouette_rays",
- "refine_feather_ramp_pos_0",
- "refine_feather_ramp_pos_1",
- "refine_edge_feather_projection",
- "refine_edge_feather_width",
- "refine_edge_feather_softness",
- # --- TRELLIS.2 ---
- "trellis2_texture_mode",
- "trellis2_initial_image_arch",
- "trellis2_camera_count",
- "trellis2_placement_mode",
- "trellis2_auto_prompts",
- "trellis2_exclude_bottom",
- "trellis2_exclude_bottom_angle",
- "trellis2_auto_aspect",
- "trellis2_occlusion_mode",
- "trellis2_consider_existing",
- "trellis2_delete_cameras",
- "trellis2_coverage_target",
- "trellis2_max_auto_cameras",
- "trellis2_fan_angle",
- "trellis2_resolution",
- "trellis2_vram_mode",
- "trellis2_attn_backend",
- "trellis2_seed",
- "trellis2_ss_guidance",
- "trellis2_ss_steps",
- "trellis2_shape_guidance",
- "trellis2_shape_steps",
- "trellis2_tex_guidance",
- "trellis2_tex_steps",
- "trellis2_max_tokens",
- "trellis2_texture_size",
- "trellis2_decimation",
- "trellis2_remesh",
- "trellis2_post_processing_enabled",
-
- "trellis2_bg_removal",
- "trellis2_background_color",
- "trellis2_import_scale",
- "trellis2_clamp_elevation",
- "trellis2_max_elevation",
- "trellis2_min_elevation",
- "trellis2_auto_lighting",
-
- # --- General workflow settings (not prompt/seed/model/paths) ---
- "use_ipadapter",
- "sequential_ipadapter_regenerate_wo_controlnet",
- "allow_modify_existing_textures",
- "ask_object_prompts",
- "weight_exponent_mask",
- "mask_blocky",
- "architecture_mode",
- "use_camera_prompts",
- "sg_use_custom_camera_order",
- "generation_mode",
- "texture_objects",
- "use_flux_lora",
- "qwen_use_trellis2_style",
- "qwen_trellis2_style_initial_only",
- "trellis2_skip_texture",
- "pbr_decomposition",
- "pbr_albedo_saturation_mode",
-]
-
-# ── Preset diff preview helpers ──────────────────────────────────────────
-
-# Parameters shown first in the diff preview (most user-visible settings).
-_PRESET_DIFF_CORE = [
- 'architecture_mode', 'model_architecture', 'steps', 'cfg',
- 'sampler', 'scheduler', 'generation_method', 'denoise',
- 'generation_mode', 'pbr_decomposition',
-]
-
-# Known enum values that need specific casing in diff labels.
-_DISPLAY_NAMES = {
- 'sdxl': 'SDXL',
- 'flux1': 'FLUX.1',
- 'qwen_image_edit': 'Qwen',
- 'flux2_klein': 'FLUX.2 Klein',
- 'trellis2': 'TRELLIS.2',
- 'standard': 'Standard',
-}
-
-def _fmt_diff_val(v):
- """Format a property value for compact display in the diff preview."""
- if isinstance(v, bool):
- return 'On' if v else 'Off'
- if isinstance(v, float):
- # Strip trailing zeros: 1.50 → "1.5", 8.00 → "8"
- return f'{v:.2f}'.rstrip('0').rstrip('.')
- if isinstance(v, str):
- if v in _DISPLAY_NAMES:
- return _DISPLAY_NAMES[v]
- s = v.replace('_', ' ').title()
- return s[:20] + '…' if len(s) > 22 else s
- return str(v)
-
-
-def _preset_diff(context):
- """Return list of (param, current_formatted, preset_formatted) for
- parameters that differ between the scene and the selected preset.
- Core parameters are listed first."""
- scene = context.scene
- preset_key = scene.stablegen_preset
- if preset_key not in PRESETS:
- return []
- preset = PRESETS[preset_key]
- core_diffs = []
- other_diffs = []
- core_set = set(_PRESET_DIFF_CORE)
- for key in GEN_PARAMETERS:
- if key not in preset or not hasattr(scene, key):
- continue
- current = getattr(scene, key)
- target = preset[key]
- try:
- if isinstance(current, (int, float)) and isinstance(target, (int, float)):
- if math.isclose(float(current), float(target), rel_tol=1e-7, abs_tol=0.0):
- continue
- elif current == target:
- continue
- except Exception:
- continue
- entry = (key, _fmt_diff_val(current), _fmt_diff_val(target))
- if key in core_set:
- core_diffs.append(entry)
- else:
- other_diffs.append(entry)
- # Sort core diffs in the defined display order
- core_order = {k: i for i, k in enumerate(_PRESET_DIFF_CORE)}
- core_diffs.sort(key=lambda x: core_order.get(x[0], 999))
- return core_diffs + other_diffs
-
-
-def get_preset_items(self, context):
- # Group presets by architecture for easier navigation.
- _ARCH_GROUP_ORDER = [
- ('sdxl', 'SDXL / FLUX.1'),
- ('qwen_image_edit', 'Qwen Image Edit'),
- ('flux2_klein', 'FLUX.2 Klein'),
- ('trellis2', 'TRELLIS.2 Pipeline'),
- ]
- _known_groups = {k for k, _ in _ARCH_GROUP_ORDER}
-
- # Classify each preset into its architecture group.
- groups = {} # group_key -> list of (identifier, name, description)
- for key, preset in PRESETS.items():
- if preset.get('architecture_mode') == 'trellis2':
- grp = 'trellis2'
- else:
- grp = preset.get('model_architecture', 'sdxl')
- groups.setdefault(grp, []).append(
- (key, key.title(), preset.get('description', f'Preset {key}'))
- )
-
- items = []
- first_group = True
- for grp_key, _label in _ARCH_GROUP_ORDER:
- if grp_key not in groups:
- continue
- if not first_group:
- items.append(('', '', '')) # horizontal separator
- # Section header: empty identifier + label name renders as header
- # in Blender 4.x enum popup menus.
- items.append(('', _label, ''))
- first_group = False
- items.extend(groups[grp_key])
-
- # Any architectures not explicitly listed above (future-proofing).
- for grp_key, grp_items in groups.items():
- if grp_key not in _known_groups:
- items.append(('', '', ''))
- items.append(('', grp_key.replace('_', ' ').title(), ''))
- items.extend(grp_items)
-
- items.append(('', '', '')) # separator before Custom
- items.append(('CUSTOM', 'Custom', 'Custom configuration'))
- return items
-
-def update_parameters(self, context):
- scene = context.scene
- # Build a dictionary of current parameter values
- current = {key: getattr(scene, key) for key in GEN_PARAMETERS if hasattr(scene, key)}
-
- # Compare current values with every stock preset's stored values
- for name, preset in PRESETS.items():
- # First check regular parameters
- if all(
- (lambda v1, v2: math.isclose(v1, v2, rel_tol=1e-7, abs_tol=0.0) if isinstance(v1, (int, float)) and isinstance(v2, (int, float)) else v1 == v2)
- (current.get(key), preset.get(key))
- for key in GEN_PARAMETERS if key in preset
- ):
- units_match = True
-
- # Now check ControlNet units if present in the preset
- if "controlnet_units" in preset:
- # Get current controlnet units as comparable data
- current_units = []
- for unit in scene.controlnet_units:
- unit_data = {
- "unit_type": unit.unit_type,
- "model_name": unit.model_name,
- "strength": unit.strength,
- "start_percent": unit.start_percent,
- "end_percent": unit.end_percent,
- "is_union": unit.is_union,
- "use_union_type": unit.use_union_type
- }
-
- current_units.append(unit_data)
-
- # Compare unit count
- if len(current_units) != len(preset["controlnet_units"]):
- continue # Different number of units, not a match
-
- # Compare each unit's properties
- units_match = True
- for i, unit_data in enumerate(current_units):
- preset_unit = preset["controlnet_units"][i]
- for key, value in unit_data.items():
- if key not in preset_unit or preset_unit[key] != value:
- units_match = False
- break
- if not units_match:
- break
-
- if not units_match:
- continue # ControlNet units don't match, try next preset
-
- # Now check LoRA units if present in the preset
- if "lora_units" in preset:
- current_lora_units_data = []
- for lora_unit_obj in scene.lora_units:
- current_lora_units_data.append({
- "model_name": lora_unit_obj.model_name,
- "model_strength": round(lora_unit_obj.model_strength, 7),
- "clip_strength": round(lora_unit_obj.clip_strength, 7),
- })
-
- preset_lora_units_data = preset["lora_units"]
- if len(current_lora_units_data) != len(preset_lora_units_data):
- continue # Different number of LoRA units
-
- lora_units_match = True
- for i, current_lora_unit_data in enumerate(current_lora_units_data):
- preset_lora_unit_data = preset_lora_units_data[i]
- for key, value in current_lora_unit_data.items():
- if key not in preset_lora_unit_data or preset_lora_unit_data[key] != value:
- lora_units_match = False
- break
- if not lora_units_match:
- break
-
- if not lora_units_match:
- continue # LoRA units don't match, try next preset
-
- # All parameters and ControlNet and LoRA units match
- if scene.stablegen_preset != name:
- scene.stablegen_preset = name
- scene.active_preset = name
- return
-
- # No match found, set to custom
- scene.active_preset = "CUSTOM"
- scene.stablegen_preset = "CUSTOM"
-
-class StableGenPanel(bpy.types.Panel):
- """
- Creates the main UI panel for the StableGen addon.
- """
- bl_label = "StableGen"
- bl_idname = "OBJECT_PT_stablegen"
- bl_space_type = "VIEW_3D"
- bl_region_type = "UI"
- bl_category = "StableGen"
- bl_context = "objectmode"
- bl_ui_units_x = 600
-
- def draw_header(self, _):
- """
- Draws the header of the panel.
- :param _: Unused parameter.
- :return: None
- """
- self.layout.label(icon="WORLD_DATA")
-
- def draw(self, context):
- """
- Draws the panel with reorganized Advanced Parameters.
- :param context: Blender context.
- :return: None
- """
- layout = self.layout
- scene = context.scene # Get the scene for easier access
-
- # Detect the current width of the panel
- region = context.region
- width = region.width
- width_mode = 'narrow' if width < 420 else 'wide'
-
- # Compute properties that differ from the pending (unapplied) preset.
- # Used to highlight affected fields inline with alert coloring.
- # Dict maps param name → formatted target value for "→ X" labels.
- _diff_props = {}
- if (hasattr(scene, 'active_preset')
- and scene.stablegen_preset != 'CUSTOM'
- and getattr(scene, 'active_preset', '') != scene.stablegen_preset):
- _diff_props = {d[0]: d[2] for d in _preset_diff(context)}
-
- # --- Action Buttons & Progress ---
- cam_tools_row = layout.row()
- cam_tools_row.operator("object.add_cameras", text="Add Cameras", icon="CAMERA_DATA")
- if width_mode == 'narrow':
- cam_tools_row = layout.row()
- cam_tools_row.operator("object.collect_camera_prompts", text="Collect Camera Prompts", icon="FILE_TEXT")
-
- cam_extra_row = layout.row(align=True)
- cam_extra_row.operator("object.clone_camera", text="Clone Camera", icon="DUPLICATE")
- cam_extra_row.operator("object.mirror_camera", text="Mirror", icon="MOD_MIRROR")
- cam_extra_row.operator("object.toggle_camera_labels", text="Labels", icon="FONT_DATA")
-
-
- addon_prefs = context.preferences.addons[__package__].preferences
- config_error_message = None
-
- if not os.path.exists(addon_prefs.output_dir):
- config_error_message = "Output Path Invalid"
- elif not addon_prefs.server_address:
- config_error_message = "Server Address Missing"
- elif not addon_prefs.server_online:
- config_error_message = "Cannot reach server"
-
- # Determine if we are in TRELLIS.2 mode (used by generate button & later sections)
- _arch_mode = getattr(scene, 'architecture_mode', 'sdxl')
- _is_trellis2_mode = (_arch_mode == 'trellis2')
-
- # ── Prerequisite warnings (shown inline before the Generate button) ──
- if not _is_trellis2_mode and not config_error_message:
- has_cameras = any(obj.type == 'CAMERA' for obj in scene.objects)
- has_meshes = any(obj.type == 'MESH' for obj in context.view_layer.objects if not obj.hide_get())
- if not has_meshes:
- warn_box = layout.box()
- warn_row = warn_box.row()
- warn_row.alert = True
- warn_row.label(text="No visible mesh objects", icon='ERROR')
- warn_row.operator("stablegen.switch_to_mesh_generation",
- text="Mesh Generation", icon='MESH_DATA')
- if not has_cameras:
- warn_box = layout.box()
- warn_row = warn_box.row()
- warn_row.alert = True
- warn_row.label(text="No cameras in the scene", icon='ERROR')
- warn_row.operator("object.add_cameras", text="Add Cameras", icon='CAMERA_DATA')
-
- action_row = layout.row()
- action_row.scale_y = 2.0 # Scale the row vertically
-
- # Show a "Refreshing…" indicator while async model fetches are in-flight
- if _is_refreshing():
- refresh_row = layout.row()
- refresh_row.alignment = 'CENTER'
- refresh_row.label(text="Refreshing model lists...", icon="SORTTIME")
-
- if _is_trellis2_mode:
- # --- TRELLIS.2 Generate Button ---
- trellis2_op = next(
- (op for win in context.window_manager.windows
- for op in win.modal_operators
- if op.bl_idname == 'OBJECT_OT_trellis2_generate'),
- None
- )
- # Also look for ComfyUIGenerate running as the texturing phase
- comfy_tex_op = next(
- (op for win in context.window_manager.windows
- for op in win.modal_operators
- if op.bl_idname == 'OBJECT_OT_test_stable'),
- None
- ) if not trellis2_op else None
-
- if config_error_message:
- if config_error_message == "Cannot reach server":
- split = action_row.split(factor=0.85, align=True)
- split.operator("object.trellis2_generate", text="Cannot generate: " + config_error_message, icon="ERROR")
- split.operator("stablegen.check_server_status", text="", icon='FILE_REFRESH')
- else:
- action_row.operator("object.trellis2_generate", text="Cannot generate: " + config_error_message, icon="ERROR")
- action_row.enabled = False
-
- elif trellis2_op:
- # ── Phases 1 & 2: Trellis2Generate is alive ──
- action_row.operator("object.trellis2_generate", text="Cancel TRELLIS.2", icon="CANCEL")
- progress_col = layout.column()
-
- # Bar 1 — Overall
- overall_pct = getattr(trellis2_op, '_overall_progress', 0)
- overall_label = getattr(trellis2_op, '_overall_stage', 'Initializing')
- progress_col.progress(
- text=f"{overall_label} ({overall_pct:.0f}%)",
- factor=max(0.0, min(overall_pct / 100.0, 1.0))
- )
-
- # Bar 2 — Phase
- phase_pct = getattr(trellis2_op, '_phase_progress', 0)
- phase_label = getattr(trellis2_op, '_phase_stage', '')
- if phase_label:
- progress_col.progress(
- text=f"{phase_label} ({phase_pct:.0f}%)",
- factor=max(0.0, min(phase_pct / 100.0, 1.0))
- )
-
- # Bar 3 — Detail (only when there's actual sampler progress)
- detail_pct = getattr(trellis2_op, '_detail_progress', 0)
- detail_label = getattr(trellis2_op, '_detail_stage', '')
- if detail_label and detail_pct > 0:
- progress_col.progress(
- text=f"{detail_label} ({detail_pct:.0f}%)",
- factor=max(0.0, min(detail_pct / 100.0, 1.0))
- )
-
- elif comfy_tex_op and getattr(scene, 'trellis2_pipeline_active', False):
- # ── Phase 3: Texturing via ComfyUIGenerate ──
- action_row.operator("object.test_stable", text="Cancel Texturing", icon="CANCEL")
- progress_col = layout.column()
-
- pbr_active = getattr(comfy_tex_op, '_pbr_active', False)
- if pbr_active:
- # PBR decomposition sub-phase — 3 bars:
- # Bar 1 (top): overall pipeline progress
- # Bar 2: current PBR model (camera progress)
- # Bar 3: PBR step N/M
- pbr_step = getattr(comfy_tex_op, '_pbr_step', 0)
- pbr_total = max(getattr(comfy_tex_op, '_pbr_total_steps', 1), 1)
- pbr_cam = getattr(comfy_tex_op, '_pbr_cam', 0)
- pbr_cam_total = max(getattr(comfy_tex_op, '_pbr_cam_total', 1), 1)
- cam_frac = pbr_cam / pbr_cam_total
- stage_text = getattr(comfy_tex_op, '_stage', 'PBR Decomposition')
-
- # Overall: texturing is done, PBR owns the upper slice of Phase 3
- phase_start = getattr(scene, 'trellis2_pipeline_phase_start_pct', 65.0)
- phase_weight = 100.0 - phase_start
- tex_portion = phase_weight * 0.6 # first 60% of phase = texturing
- pbr_portion = phase_weight * 0.4 # last 40% = PBR
- pbr_frac = max(0.0, min(((pbr_step - 1) + cam_frac) / pbr_total, 1.0))
- overall_pct = phase_start + tex_portion + pbr_portion * pbr_frac
- overall_pct = max(0.0, min(overall_pct, 100.0))
- total_phases = getattr(scene, 'trellis2_pipeline_total_phases', 3)
-
- # Bar 1 — Overall pipeline
- progress_col.progress(
- text=f"Phase {total_phases}/{total_phases}: PBR Decomposition ({overall_pct:.0f}%)",
- factor=max(0.0, min(overall_pct / 100.0, 1.0))
- )
- # Bar 2 — Current model (camera progress)
- progress_col.progress(text=stage_text, factor=max(0.0, min(cam_frac, 1.0)))
- # Bar 3 — PBR step N/M
- progress_col.progress(text=f"PBR: Step {pbr_step}/{pbr_total}", factor=pbr_frac)
- else:
- # Normal texturing progress
- # Compute overall from scene pipeline props + ComfyUI progress
- phase_start = getattr(scene, 'trellis2_pipeline_phase_start_pct', 65.0)
- phase_weight = 100.0 - phase_start
- comfy_progress = getattr(comfy_tex_op, '_progress', 0) / 100.0
- total_imgs = getattr(comfy_tex_op, '_total_images', 0)
- cur_img_idx = getattr(comfy_tex_op, '_current_image', 0)
- if total_imgs > 1:
- comfy_overall = (cur_img_idx + comfy_progress) / total_imgs
- else:
- comfy_overall = comfy_progress
- overall_pct = phase_start + comfy_overall * phase_weight
- overall_pct = max(0.0, min(overall_pct, 100.0))
-
- total_phases = getattr(scene, 'trellis2_pipeline_total_phases', 3)
- # Bar 1 — Overall
- progress_col.progress(
- text=f"Phase {total_phases}/{total_phases}: Texturing ({overall_pct:.0f}%)",
- factor=max(0.0, min(overall_pct / 100.0, 1.0))
- )
-
- # Bar 2 — Per-image (same as normal ComfyUI bar)
- stage = getattr(comfy_tex_op, '_stage', 'Generating')
- img_pct = getattr(comfy_tex_op, '_progress', 0)
- progress_col.progress(
- text=f"{stage} ({img_pct:.0f}%)",
- factor=max(0.0, min(img_pct / 100.0, 1.0))
- )
-
- # Bar 3 — Image N/M (same as normal ComfyUI overall bar)
- if total_imgs > 1:
- cur_img = min(cur_img_idx + 1, total_imgs)
- img_overall = max(0.0, min(comfy_overall, 1.0))
- progress_col.progress(
- text=f"Overall: Image {cur_img}/{total_imgs}",
- factor=img_overall
- )
-
- elif comfy_tex_op and scene.generation_status == 'running':
- # Standalone ComfyUIGenerate (e.g. Reproject with PBR)
- # outside the TRELLIS.2 pipeline.
- action_row.operator("object.test_stable", text="Cancel Generation", icon="CANCEL")
- progress_col = layout.column()
- raw_progress = getattr(comfy_tex_op, '_progress', 0) / 100.0
- pbr_active = getattr(comfy_tex_op, '_pbr_active', False)
-
- if pbr_active:
- pbr_step = getattr(comfy_tex_op, '_pbr_step', 0)
- pbr_total = max(getattr(comfy_tex_op, '_pbr_total_steps', 1), 1)
- pbr_cam = getattr(comfy_tex_op, '_pbr_cam', 0)
- pbr_cam_total = max(getattr(comfy_tex_op, '_pbr_cam_total', 1), 1)
- cam_frac = pbr_cam / pbr_cam_total
- stage_text = getattr(comfy_tex_op, '_stage', 'PBR Decomposition')
- progress_col.progress(text=stage_text, factor=max(0.0, min(cam_frac, 1.0)))
- pbr_factor = max(0.0, min(((pbr_step - 1) + cam_frac) / pbr_total, 1.0))
- progress_col.progress(text=f"PBR: Step {pbr_step}/{pbr_total}", factor=pbr_factor)
- else:
- progress_text = f"{getattr(comfy_tex_op, '_stage', 'Generating')} ({getattr(comfy_tex_op, '_progress', 0):.0f}%)"
- progress_col.progress(text=progress_text, factor=max(0.0, min(raw_progress, 1.0)))
- total_images = getattr(comfy_tex_op, '_total_images', 0)
- if total_images > 1:
- current_image_idx = getattr(comfy_tex_op, '_current_image', 0)
- overall_progress = (current_image_idx + max(0.0, min(raw_progress, 1.0))) / total_images if total_images > 0 else 0
- cur_img = min(current_image_idx + 1, total_images)
- progress_col.progress(text=f"Overall: Image {cur_img}/{total_images}", factor=max(0.0, min(overall_progress, 1.0)))
-
- elif scene.trellis2_generate_from == 'image' and not scene.trellis2_input_image:
- action_row.operator("object.trellis2_generate", text="Select an image first", icon="ERROR")
- action_row.enabled = False
- elif not getattr(scene, 'trellis2_available', False):
- split = action_row.split(factor=0.8)
- err_sub = split.row()
- err_sub.operator("object.trellis2_generate", text="TRELLIS.2 nodes not found", icon="ERROR")
- err_sub.enabled = False
- split.operator("stablegen.check_server_status", text="", icon="FILE_REFRESH")
- else:
- action_row.operator("object.trellis2_generate", text="Generate 3D Mesh", icon="MESH_ICOSPHERE")
- else:
- # --- Standard Diffusion Generate Button ---
- if config_error_message:
- # Split the row to have the error message/disabled button and the refresh button
- if config_error_message == "Cannot reach server":
- split = action_row.split(factor=0.85, align=True) # Adjust factor as needed
- split.operator("object.test_stable", text="Cannot generate: " + config_error_message, icon="ERROR") # Use ERROR icon
- # Use the operator from __init__.py
- split.operator("stablegen.check_server_status", text="", icon='FILE_REFRESH')
- else:
- action_row.operator("object.test_stable", text="Cannot generate: " + config_error_message, icon="ERROR")
- action_row.enabled = False
- else:
- action_row.enabled = True
- if not bpy.app.online_access:
- action_row.operator("object.test_stable", text="Enable online access in preferences", icon="ERROR")
- action_row.enabled = False
- elif not scene.model_name or scene.model_name == "NONE_FOUND":
- action_row.operator("object.test_stable", text="Cannot generate: Model Directory Empty", icon="ERROR")
- action_row.enabled = False
- elif scene.generation_status == 'idle':
- # Check if any cameras are selected and if there is existing output
- selected_cameras = [obj for obj in context.selected_objects if obj.type == 'CAMERA']
- if not selected_cameras or scene.get("output_timestamp") == "":
- action_row.operator("object.test_stable", text="Generate", icon="PLAY")
- else:
- # Use the regenerate operator
- action_row.operator("object.stablegen_regenerate", text="Regenerate Selected Views", icon="PLAY")
- elif scene.generation_status == 'running':
- action_row.operator("object.test_stable", text="Cancel Generation", icon="CANCEL")
-
- operator_instance = next((op for win in context.window_manager.windows for op in win.modal_operators if op.bl_idname == 'OBJECT_OT_test_stable'), None)
- if operator_instance:
- progress_col = layout.column()
- raw_progress = getattr(operator_instance, '_progress', 0) / 100.0
- pbr_active = getattr(operator_instance, '_pbr_active', False)
-
- if pbr_active:
- # During PBR: top bar shows camera progress within
- # the current model step (no raw ComfyUI jitter).
- pbr_step = getattr(operator_instance, '_pbr_step', 0)
- pbr_total = max(getattr(operator_instance, '_pbr_total_steps', 1), 1)
- pbr_cam = getattr(operator_instance, '_pbr_cam', 0)
- pbr_cam_total = max(getattr(operator_instance, '_pbr_cam_total', 1), 1)
-
- # Top bar: camera X out of N within this step
- cam_frac = pbr_cam / pbr_cam_total
- stage_text = getattr(operator_instance, '_stage', 'PBR Decomposition')
- progress_col.progress(
- text=stage_text,
- factor=max(0.0, min(cam_frac, 1.0))
- )
- # Bottom bar: overall PBR progress
- pbr_factor = max(0.0, min(
- ((pbr_step - 1) + cam_frac) / pbr_total, 1.0))
- progress_col.progress(
- text=f"PBR: Step {pbr_step}/{pbr_total}",
- factor=pbr_factor
- )
- else:
- # Normal generation progress
- progress_text = f"{getattr(operator_instance, '_stage', 'Generating')} ({getattr(operator_instance, '_progress', 0):.0f}%)"
- progress_col.progress(text=progress_text, factor=max(0.0, min(raw_progress, 1.0)))
-
- total_images = getattr(operator_instance, '_total_images', 0)
- if total_images > 1:
- current_image_idx = getattr(operator_instance, '_current_image', 0)
- current_image_decimal_progress = max(0.0, min(raw_progress, 1.0))
-
- # Ensure total_images is not zero to prevent division by zero
- overall_progress_factor = (current_image_idx + current_image_decimal_progress) / total_images if total_images > 0 else 0
- overall_progress_factor_clamped = max(0.0, min(overall_progress_factor, 1.0))
-
- current_img = min(current_image_idx + 1, total_images) # Clamp to total_images
-
- progress_col.progress(
- text=f"Overall: Image {current_img}/{total_images}",
- factor=overall_progress_factor_clamped # Ensure factor is <= 1.0 (logic maintained)
- )
-
- elif context.scene.generation_status == 'waiting':
- action_row.operator("object.test_stable", text="Waiting for Cancellation", icon="TIME")
- else:
- action_row.operator("object.test_stable", text="Fix Issues to Generate", icon="ERROR")
- action_row.enabled = False
-
- bake_row = layout.row()
- if config_error_message:
- bake_row.operator("object.bake_textures", text="Cannot Bake: " + config_error_message, icon="ERROR")
- bake_row.enabled = False
- else:
- bake_row.operator("object.bake_textures", text="Bake Textures", icon="RENDER_STILL")
- bake_row.enabled = True
- bake_operator = next((op for win in context.window_manager.windows for op in win.modal_operators if op.bl_idname == 'OBJECT_OT_bake_textures'), None)
- if bake_operator:
- bake_progress_col = layout.column()
- bake_stage = getattr(bake_operator, '_stage', 'Baking')
- bake_progress = getattr(bake_operator, '_progress', 0) / 100.0
- bake_progress_col.progress(text=bake_stage, factor=bake_progress if bake_progress <=1.0 else 1.0) # Ensure factor is <= 1.0
-
- total_objects = getattr(bake_operator, '_total_objects', 0)
- if total_objects > 1:
- current_object = getattr(bake_operator, '_current_object', 0)
- # Ensure total_objects is not zero
- overall_bake_progress = ((current_object + bake_progress) / total_objects) if total_objects > 0 else 0
- bake_progress_col.progress(
- text=f"{bake_stage}: Object {current_object + 1}/{total_objects}",
- factor=overall_bake_progress if overall_bake_progress <=1.0 else 1.0 # Ensure factor is <= 1.0
- )
-
- export_row = layout.row()
- export_row.operator("object.export_game_engine",
- text="Export for Game Engine",
- icon="EXPORT")
-
- # --- Preset Management ---
- preset_box = layout.box()
- row = preset_box.row(align=True)
- row.prop(scene, "stablegen_preset", text="Preset")
-
- # Conditional button: Apply for stock presets, Save for custom preset
- if not hasattr(scene, 'active_preset'):
- scene.active_preset = scene.stablegen_preset
-
- if scene.stablegen_preset == "CUSTOM":
- row.operator("stablegen.save_preset", text="Save Preset", icon="PLUS")
- else:
- if scene.active_preset != scene.stablegen_preset:
- row.operator("stablegen.apply_preset", text="Apply Preset", icon="CHECKMARK")
-
- is_stock_preset = PRESETS.get(scene.stablegen_preset, {}).get("custom", False) is False
- if not is_stock_preset and scene.stablegen_preset != "DEFAULT":
- row.operator("stablegen.delete_preset", text="Delete", icon="TRASH")
-
- # --- Scene Queue ---
- queue_box = layout.box()
- queue_col = queue_box.column()
- wm = context.window_manager
- show_queue = getattr(wm, 'sg_show_queue', False)
- queue_header = queue_col.row()
- queue_header.prop(wm, "sg_show_queue",
- text=f"Scene Queue ({len(wm.sg_scene_queue)})",
- icon="TRIA_DOWN" if show_queue else "TRIA_RIGHT",
- emboss=False)
- if _queue_processing:
- status_text = "Exporting GIF..." if _queue_phase == 'exporting_gif' else "Processing..."
- queue_header.label(text=status_text, icon="SORTTIME")
-
- if show_queue:
- queue_content = queue_col.box()
- row = queue_content.row()
- row.template_list("SG_UL_SceneQueueList", "",
- wm, "sg_scene_queue",
- wm, "sg_scene_queue_index",
- rows=3)
- col = row.column(align=True)
- col.operator("stablegen.queue_move_up", text="", icon="TRIA_UP")
- col.operator("stablegen.queue_move_down", text="", icon="TRIA_DOWN")
- col.separator()
- col.operator("stablegen.queue_remove", text="", icon="REMOVE")
-
- btn_row = queue_content.row(align=True)
- btn_row.operator("stablegen.queue_add", text="Add Scene", icon="ADD")
- btn_row.operator("stablegen.queue_open_result", text="Open", icon="FILE_BLEND")
- btn_row2 = queue_content.row(align=True)
- btn_row2.operator("stablegen.queue_invalidate", text="Reset", icon="LOOP_BACK")
- btn_row2.operator("stablegen.queue_clear", text="Clear", icon="TRASH")
-
- process_row = queue_content.row()
- if _queue_processing:
- process_row.alert = True
- process_row.operator("stablegen.queue_process", text="Cancel Queue", icon="CANCEL")
- else:
- process_row.operator("stablegen.queue_process", text="Process Queue", icon="PLAY")
- process_row.enabled = len(wm.sg_scene_queue) > 0
-
- # ── GIF Export settings ──
- gif_box = queue_content.box()
- gif_row = gif_box.row()
- gif_row.prop(wm, "sg_queue_gif_export", text="Export Orbit GIF/MP4")
- if getattr(wm, 'sg_queue_gif_export', False):
- gif_col = gif_box.column(align=True)
- row = gif_col.row(align=True)
- row.prop(wm, "sg_queue_gif_duration")
- row.prop(wm, "sg_queue_gif_fps")
- row = gif_col.row(align=True)
- row.prop(wm, "sg_queue_gif_resolution")
- row.prop(wm, "sg_queue_gif_samples")
- gif_col.prop(wm, "sg_queue_gif_engine")
- gif_col.prop(wm, "sg_queue_gif_interpolation")
- gif_col.separator()
- gif_col.prop(wm, "sg_queue_gif_use_hdri")
- if getattr(wm, 'sg_queue_gif_use_hdri', False):
- gif_col.prop(wm, "sg_queue_gif_hdri_path")
- gif_col.prop(wm, "sg_queue_gif_hdri_strength")
- gif_col.prop(wm, "sg_queue_gif_hdri_rotation")
- gif_col.prop(wm, "sg_queue_gif_env_mode")
- gif_col.prop(wm, "sg_queue_gif_denoiser")
- if bpy.app.version >= (5, 1, 0) and getattr(wm, 'sg_queue_gif_engine', 'CYCLES') == 'CYCLES':
- gif_col.prop(wm, "sg_queue_gif_use_gpu")
- gif_col.prop(wm, "sg_queue_gif_also_no_pbr")
-
- # --- Main Parameters section ---
- if not hasattr(scene, 'show_generation_params'):
- scene.show_generation_params = True
-
- is_trellis2 = getattr(scene, 'architecture_mode', 'sdxl') == 'trellis2'
- trellis2_tex_mode = getattr(scene, 'trellis2_texture_mode', 'native')
- trellis2_diffusion_texturing = is_trellis2 and trellis2_tex_mode in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein')
-
- main_params_box = layout.box()
- main_params_col = main_params_box.column()
- main_params_col.prop(scene, "show_generation_params", text="Main Parameters", icon="TRIA_DOWN" if scene.show_generation_params else "TRIA_RIGHT", emboss=False)
- if scene.show_generation_params:
- params_container = main_params_col.box()
- # Split for prompt
- split = params_container.split(factor=0.25)
- split.label(text="Prompt:")
- prompt_row = split.row(align=True)
- prompt_row.prop(scene, "comfyui_prompt", text="")
- # Hide texture prompt toggle when texture mode is none/native (no separate texture pipeline)
- if not (is_trellis2 and trellis2_tex_mode in ('none', 'native')):
- prompt_row.prop(scene, "use_separate_texture_prompt", text="",
- icon='BRUSH_DATA',
- icon_only=True)
- if scene.use_separate_texture_prompt and not (is_trellis2 and trellis2_tex_mode in ('none', 'native')):
- split = params_container.split(factor=0.25)
- split.label(text="Texture Prompt:")
- split.prop(scene, "texture_prompt", text="")
-
- # Architecture selector (architecture_mode — includes TRELLIS.2)
- # Alert when either architecture_mode or model_architecture changes.
- _arch_diff = 'architecture_mode' in _diff_props or 'model_architecture' in _diff_props
- split = params_container.split(factor=0.5)
- if _arch_diff:
- split.alert = True
- split.label(text="Architecture:")
- split.prop(scene, "architecture_mode", text="")
- if _arch_diff:
- _has_mode = 'architecture_mode' in _diff_props
- _has_model = 'model_architecture' in _diff_props
- if _has_mode:
- # architecture_mode differs — always show model in parens.
- # Use the preset's model_architecture even if it didn't change.
- _preset_key = scene.stablegen_preset
- _preset_data = PRESETS.get(_preset_key, {})
- _model_display = _fmt_diff_val(_preset_data.get(
- 'model_architecture', scene.model_architecture))
- split.label(text="→ " + _diff_props['architecture_mode']
- + " (" + _model_display + ")")
- else:
- # Only model_architecture differs (e.g. SDXL → Qwen).
- split.label(text="→ " + _diff_props['model_architecture'])
-
- if is_trellis2:
- # --- TRELLIS.2 specific layout ---
- # Warning if TRELLIS.2 nodes not detected
- if not getattr(scene, 'trellis2_available', False):
- warn_row = params_container.row()
- warn_row.alert = True
- warn_split = warn_row.split(factor=0.9)
- warn_split.label(text="TRELLIS.2 nodes not detected on server", icon="ERROR")
- warn_split.operator("stablegen.check_server_status", text="", icon="FILE_REFRESH")
-
- # Generate From toggle (Image / Prompt)
- split = params_container.split(factor=0.5)
- split.label(text="Generate From:")
- split.prop(scene, "trellis2_generate_from", text="")
-
- # Input image picker (only when generate_from = image)
- if scene.trellis2_generate_from == 'image':
- split = params_container.split(factor=0.25)
- split.label(text="Input Image:")
- split.prop(scene, "trellis2_input_image", text="")
-
- # Preview gallery (only when generate_from = prompt)
- if scene.trellis2_generate_from == 'prompt':
- row = params_container.row(align=True)
- row.prop(scene, "trellis2_preview_gallery_enabled", text="Preview Gallery", toggle=True, icon="IMAGE_REFERENCE")
- sub = row.row(align=True)
- sub.enabled = scene.trellis2_preview_gallery_enabled
- sub.prop(scene, "trellis2_preview_gallery_count", text="Count")
-
- # Texture Generation Mode
- split = params_container.split(factor=0.5)
- split.label(text="Texture Mode:")
- split.prop(scene, "trellis2_texture_mode", text="")
-
- # Prompt + native/none: show initial-image architecture & checkpoint
- _prompt_needs_initial = (
- scene.trellis2_generate_from == 'prompt'
- and trellis2_tex_mode in ('native', 'none')
- )
- if _prompt_needs_initial:
- split = params_container.split(factor=0.5)
- split.label(text="Initial Image Arch:")
- split.prop(scene, "trellis2_initial_image_arch", text="")
-
- split = params_container.split(factor=0.25)
- split.label(text="Checkpoint:")
- row = split.row(align=True)
- row.prop(scene, "model_name", text="")
- row.operator("stablegen.refresh_checkpoint_list", text="", icon='FILE_REFRESH')
-
- # When diffusion texturing: show checkpoint, generation mode, camera count
- if trellis2_diffusion_texturing:
- split = params_container.split(factor=0.25)
- split.label(text="Checkpoint:")
- row = split.row(align=True)
- row.prop(scene, "model_name", text="")
- row.operator("stablegen.refresh_checkpoint_list", text="", icon='FILE_REFRESH')
-
- split = params_container.split(factor=0.5)
- if 'generation_method' in _diff_props or 'qwen_generation_method' in _diff_props:
- split.alert = True
- split.label(text="Generation Mode:")
- if scene.model_architecture.startswith("qwen"):
- split.prop(scene, "qwen_generation_method", text="")
- else:
- split.prop(scene, "generation_method", text="")
- _lbl = ''.join(f'→{_diff_props[k]} ' for k in ('generation_method', 'qwen_generation_method') if k in _diff_props)
- if _lbl:
- split.label(text=_lbl.strip())
- else:
- # --- Standard diffusion layout ---
- # Split for model name
- split = params_container.split(factor=0.25)
- split.label(text="Checkpoint:")
- row = split.row(align=True)
- row.prop(scene, "model_name", text="")
- row.operator("stablegen.refresh_checkpoint_list", text="", icon='FILE_REFRESH')
-
- # Split for generation method
- split = params_container.split(factor=0.5)
- if 'generation_method' in _diff_props or 'qwen_generation_method' in _diff_props:
- split.alert = True
- split.label(text="Generation Mode:")
- if scene.model_architecture.startswith("qwen"):
- split.prop(scene, "qwen_generation_method", text="")
- else:
- split.prop(scene, "generation_method", text="")
- _lbl = ''.join(f'→{_diff_props[k]} ' for k in ('generation_method', 'qwen_generation_method') if k in _diff_props)
- if _lbl:
- split.label(text=_lbl.strip())
-
- # Split for object selection
- split = params_container.split(factor=0.5)
- split.label(text="Target Objects:")
- split.prop(scene, "texture_objects", text="")
-
- # Helper to create collapsible sections
- def draw_collapsible_section(parent_layout, toggle_prop_name, title, icon="NONE"):
- if not hasattr(scene, toggle_prop_name):
- setattr(bpy.types.Scene, toggle_prop_name, bpy.props.BoolProperty(name=title, default=False))
-
- box = parent_layout.box()
- col = box.column()
- is_expanded = getattr(scene, toggle_prop_name, False)
- col.prop(scene, toggle_prop_name, text=title, icon="TRIA_DOWN" if is_expanded else "TRIA_RIGHT", emboss=False)
- if is_expanded:
- return col.box() # Return a new box for content if expanded
- return None
-
- core_settings_props = [
- "show_core_settings", "show_lora_settings", "show_camera_options",
- "show_scene_understanding_settings",
- "show_output_material_settings", "show_image_guidance_settings",
- "show_masking_inpainting_settings", "show_mode_specific_settings"
- ]
- for prop_name in core_settings_props:
- if not hasattr(scene, prop_name):
- setattr(bpy.types.Scene, prop_name, bpy.props.BoolProperty(name=prop_name.replace("_", " ").title(), default=False))
-
- # --- ADVANCED PARAMETERS ---
- advanced_params_box = layout.box()
- advanced_params_box = advanced_params_box.column()
- advanced_params_box.prop(scene, "show_advanced_params", text="Advanced Parameters", icon="TRIA_DOWN" if scene.show_advanced_params else "TRIA_RIGHT", emboss=False)
- if context.scene.show_advanced_params:
-
- # --- TRELLIS.2: Mesh Generation Settings ---
- if is_trellis2:
- content_box = draw_collapsible_section(advanced_params_box, "show_trellis2_mesh_settings", "Mesh Generation Settings", icon="MESH_DATA")
- if content_box:
- # Core mesh params
- row = content_box.row()
- row.prop(scene, "trellis2_seed", text="Seed")
-
- content_box.separator()
-
- # Post-processing (ComfyUI-side decimation + remesh)
- content_box.label(text="Post-Processing:", icon="OUTLINER_OB_MESH")
- row = content_box.row()
- row.prop(scene, "trellis2_post_processing_enabled", text="Enable Post-Processing", toggle=True, icon="MOD_DECIM")
-
- if scene.trellis2_post_processing_enabled:
- row = content_box.row()
- row.prop(scene, "trellis2_decimation", text="Decimation Target")
- row = content_box.row()
- row.prop(scene, "trellis2_remesh", text="Remesh", toggle=True, icon="MOD_REMESH")
-
- content_box.separator()
-
- # Model settings
- content_box.label(text="Model:", icon="SETTINGS")
- split = content_box.split(factor=0.5)
- split.label(text="Resolution:")
- split.prop(scene, "trellis2_resolution", text="")
-
- split = content_box.split(factor=0.5)
- split.label(text="VRAM Mode:")
- split.prop(scene, "trellis2_vram_mode", text="")
-
- split = content_box.split(factor=0.5)
- split.label(text="Attention:")
- split.prop(scene, "trellis2_attn_backend", text="")
-
- content_box.separator()
-
- # Shape generation
- content_box.label(text="Shape Generation:", icon="MESH_ICOSPHERE")
- row = content_box.row(align=True)
- row.prop(scene, "trellis2_ss_guidance", text="SS Guidance")
- row.prop(scene, "trellis2_ss_steps", text="SS Steps")
- row = content_box.row(align=True)
- row.prop(scene, "trellis2_shape_guidance", text="Shape Guidance")
- row.prop(scene, "trellis2_shape_steps", text="Shape Steps")
- row = content_box.row()
- row.prop(scene, "trellis2_max_tokens", text="Max Tokens (VRAM)")
-
- content_box.separator()
-
- # Conditioning
- content_box.label(text="Conditioning:", icon="IMAGE_DATA")
- split = content_box.split(factor=0.5)
- split.label(text="Background:")
- split.prop(scene, "trellis2_background_color", text="")
- content_box.separator()
-
- # Misc
- content_box.label(text="Misc:", icon="PREFERENCES")
- split = content_box.split(factor=0.5)
- split.label(text="BG Removal:")
- split.prop(scene, "trellis2_bg_removal", text="")
-
- split = content_box.split(factor=0.5)
- split.label(text="Shading:")
- split.prop(scene, "trellis2_shade_mode", text="")
- content_box.separator()
-
- # --- TRELLIS.2: Native Texture Settings ---
- if is_trellis2 and trellis2_tex_mode == 'native':
- content_box = draw_collapsible_section(advanced_params_box, "show_trellis2_texture_settings", "Texture Settings (TRELLIS.2 Native)", icon="TEXTURE")
- if content_box:
- row = content_box.row(align=True)
- row.prop(scene, "trellis2_tex_guidance", text="Tex Guidance")
- row.prop(scene, "trellis2_tex_steps", text="Tex Steps")
- row = content_box.row()
- row.prop(scene, "trellis2_texture_size", text="Texture Size")
-
- content_box.separator()
- row = content_box.row()
- row.prop(scene, "trellis2_auto_lighting", text="Studio Lighting", icon="LIGHT_AREA")
-
- # --- TRELLIS.2: Camera Placement Settings (diffusion texturing) ---
- if is_trellis2 and trellis2_diffusion_texturing:
- content_box = draw_collapsible_section(advanced_params_box, "show_trellis2_camera_settings", "Camera Placement (TRELLIS.2)", icon="CAMERA_DATA")
- if content_box:
- _t2_pm = getattr(scene, 'trellis2_placement_mode', 'normal_weighted')
-
- row = content_box.row()
- row.prop(scene, "trellis2_import_scale", text="Import Scale (BU)")
-
- content_box.separator()
-
- split = content_box.split(factor=0.4)
- split.label(text="Placement:")
- split.prop(scene, "trellis2_placement_mode", text="")
-
- # Camera count (not used by greedy)
- if _t2_pm != 'greedy_coverage':
- row = content_box.row()
- row.prop(scene, "trellis2_camera_count", text="Camera Count")
-
- # Greedy-specific
- if _t2_pm == 'greedy_coverage':
- row = content_box.row(align=True)
- row.prop(scene, "trellis2_coverage_target", text="Coverage Target")
- row.prop(scene, "trellis2_max_auto_cameras", text="Max Cameras")
-
- # Fan-specific
- if _t2_pm == 'fan_from_camera':
- row = content_box.row()
- row.prop(scene, "trellis2_fan_angle", text="Fan Angle")
-
- content_box.separator()
-
- row = content_box.row()
- row.prop(scene, "trellis2_auto_prompts", text="Auto View Prompts", toggle=True, icon="OUTLINER_OB_CAMERA")
-
- split = content_box.split(factor=0.4)
- split.label(text="Auto Aspect:")
- split.prop(scene, "trellis2_auto_aspect", text="")
-
- split = content_box.split(factor=0.4)
- split.label(text="Occlusion:")
- split.prop(scene, "trellis2_occlusion_mode", text="")
-
- row = content_box.row()
- row.prop(scene, "trellis2_exclude_bottom", text="Exclude Bottom Faces", toggle=True, icon="TRIA_DOWN_BAR")
- if scene.trellis2_exclude_bottom:
- row = content_box.row()
- row.prop(scene, "trellis2_exclude_bottom_angle", text="Bottom Angle")
-
- row = content_box.row()
- row.prop(scene, "trellis2_consider_existing", text="Consider Existing Cameras", toggle=True)
-
- row = content_box.row()
- row.prop(scene, "trellis2_delete_cameras", text="Delete Cameras After", toggle=True, icon="TRASH")
-
- content_box.separator()
-
- row = content_box.row()
- row.prop(scene, "trellis2_clamp_elevation", text="Clamp Elevation", toggle=True, icon="CON_ROTLIMIT")
- if scene.trellis2_clamp_elevation:
- row = content_box.row(align=True)
- row.prop(scene, "trellis2_min_elevation", text="Min")
- row.prop(scene, "trellis2_max_elevation", text="Max")
-
- # --- Diffusion-based advanced sections ---
- # Each is individually guarded: shown for standard arches or TRELLIS.2 with diffusion texturing
- _show_diffusion_sections = not is_trellis2 or trellis2_diffusion_texturing
- # Mesh-only + prompt mode still needs initial-image pipeline settings
- _show_initial_image_settings = (
- is_trellis2 and trellis2_tex_mode == 'none'
- and getattr(scene, 'trellis2_generate_from', 'image') == 'prompt'
- )
-
- # --- Core Generation Settings ---
-
- if _show_diffusion_sections or _show_initial_image_settings:
- content_box = draw_collapsible_section(advanced_params_box, "show_core_settings", "Core Generation Settings", icon="SETTINGS")
- else:
- content_box = None
- if content_box:
- row = content_box.row()
- row.prop(scene, "seed", text="Seed")
- if width_mode == 'narrow':
- row = content_box.row()
- sub = row.row()
- if 'steps' in _diff_props:
- sub.alert = True
- sub.prop(scene, "steps", text="Steps")
- if 'steps' in _diff_props:
- sub.label(text="→" + _diff_props['steps'])
- if width_mode == 'narrow':
- row = content_box.row()
- sub = row.row()
- if 'cfg' in _diff_props:
- sub.alert = True
- sub.prop(scene, "cfg", text="CFG")
- if 'cfg' in _diff_props:
- sub.label(text="→" + _diff_props['cfg'])
-
- split = content_box.split(factor=0.5)
- split.label(text="Negative Prompt:")
- split.prop(scene, "comfyui_negative_prompt", text="")
-
- split = content_box.split(factor=0.5)
- split.label(text="Control After Generate:")
- split.prop(scene, "control_after_generate", text="")
-
- split = content_box.split(factor=0.5)
- if 'sampler' in _diff_props:
- split.alert = True
- split.label(text="Sampler:")
- split.prop(scene, "sampler", text="")
- if 'sampler' in _diff_props:
- split.label(text="→ " + _diff_props['sampler'])
-
- split = content_box.split(factor=0.5)
- if 'scheduler' in _diff_props:
- split.alert = True
- split.label(text="Scheduler:")
- split.prop(scene, "scheduler", text="")
- if 'scheduler' in _diff_props:
- split.label(text="→ " + _diff_props['scheduler'])
-
- row = content_box.row()
- row.prop(scene, "clip_skip", text="Clip Skip")
-
- # --- LoRA Settings ---
- if _show_diffusion_sections or _show_initial_image_settings:
- content_box = draw_collapsible_section(advanced_params_box, "show_lora_settings", "LoRA Management", icon="MODIFIER")
- else:
- content_box = None
- if content_box:
- row = content_box.row()
- row.alignment = 'CENTER'
- row.label(text="LoRA Units", icon="BRUSHES_ALL") # Using decimate icon for LoRA
-
- if scene.lora_units:
- for i, lora_unit in enumerate(scene.lora_units):
- is_selected_lora = (scene.lora_units_index == i)
- unit_box = content_box.box()
- row = unit_box.row()
- row.prop(lora_unit, "model_name", text=f"LoRA {i+1}") # Shows selected model
-
- sub_row = unit_box.row(align=True)
- sub_row.prop(lora_unit, "model_strength", text="Model Strength")
- if not scene.model_architecture.startswith("qwen") and scene.model_architecture != 'flux2_klein': # Qwen/Klein use model only loras
- sub_row.prop(lora_unit, "clip_strength", text="CLIP Strength")
-
- # Icon to indicate selection more clearly alongside the alert state
- select_icon = 'CHECKBOX_HLT' if is_selected_lora else 'CHECKBOX_DEHLT'
-
- # Selection button (now more like a radio button)
- op_select_lora = row.operator("wm.context_set_int", text="", icon=select_icon, emboss=True) # Keep emboss for the button itself
- op_select_lora.data_path = "scene.lora_units_index"
- op_select_lora.value = i
-
- btn_row_lora = content_box.row(align=True)
-
- if not scene.lora_units:
- # Only one button if no LoRA units are present
- button_text = "Add LoRA Unit" # Default text
-
- # Draw the operator with the dynamically determined text
- btn_row_lora.operator("stablegen.add_lora_unit", text=button_text, icon="ADD")
- # The enabled state (greying out) will be handled by AddLoRAUnit.poll()
- else:
- # Multiple buttons if LoRA units exist
- btn_row_lora.operator("stablegen.add_lora_unit", text="Add Another LoRA", icon="ADD")
- btn_row_lora.operator("stablegen.remove_lora_unit", text="Remove Selected", icon="REMOVE")
-
- # --- Camera Options ---
- if _show_diffusion_sections:
- content_box = draw_collapsible_section(advanced_params_box, "show_camera_options", "Camera Settings", icon="CAMERA_DATA")
- else:
- content_box = None
- if content_box:
- row = content_box.row(align=True)
- row.prop(scene, "use_camera_prompts", text="Use Camera Prompts", toggle=True, icon="OUTLINER_OB_CAMERA")
-
- # ── Camera Generation Order ──
- row = content_box.row(align=True)
- row.prop(scene, "sg_use_custom_camera_order",
- text="Custom Generation Order", toggle=True, icon="SORTALPHA")
- if scene.sg_use_custom_camera_order:
- order_box = content_box.box()
- # Preset strategy selector + sync
- row = order_box.row(align=True)
- row.operator_menu_enum(
- "stablegen.apply_camera_order_preset", "strategy",
- text="Sort Preset", icon="PRESET")
- row.operator("stablegen.sync_camera_order",
- text="", icon="FILE_REFRESH")
-
- # UIList with move buttons
- row = order_box.row()
- row.template_list(
- "SG_UL_CameraOrderList", "",
- scene, "sg_camera_order",
- scene, "sg_camera_order_index",
- rows=4, maxrows=8)
- col = row.column(align=True)
- op = col.operator("stablegen.move_camera_order",
- text="", icon="TRIA_UP")
- op.direction = 'UP'
- op = col.operator("stablegen.move_camera_order",
- text="", icon="TRIA_DOWN")
- op.direction = 'DOWN'
-
- # --- Viewpoint Blending Settings ---
- if _show_diffusion_sections:
- content_box = draw_collapsible_section(advanced_params_box, "show_scene_understanding_settings", "Viewpoint Blending Settings", icon="ZOOM_IN")
- else:
- content_box = None
- if content_box:
- # Row 1: Discard-Over Angle | Weight Exponent
- split = content_box.split(factor=0.5, align=True)
- sub = split.row()
- if 'discard_factor' in _diff_props:
- sub.alert = True
- sub.prop(scene, "discard_factor", text="Discard Angle")
- if 'discard_factor' in _diff_props:
- sub.label(text="→" + _diff_props['discard_factor'])
- sub = split.row()
- if 'weight_exponent' in _diff_props:
- sub.alert = True
- sub.prop(scene, "weight_exponent", text="Exponent")
- if 'weight_exponent' in _diff_props:
- sub.label(text="→" + _diff_props['weight_exponent'])
-
- # Row 2 & 3: Reset toggles and values (only for sequential / qwen)
- if scene.generation_method == 'sequential' or scene.model_architecture in ('qwen_image_edit', 'flux2_klein'):
- split = content_box.split(factor=0.5, align=True)
- split.prop(scene, "discard_factor_generation_only", text="Reset Angle", toggle=True)
- split.prop(scene, "weight_exponent_generation_only", text="Reset Exponent", toggle=True)
-
- if scene.discard_factor_generation_only or scene.weight_exponent_generation_only:
- split = content_box.split(factor=0.5, align=True)
- if scene.discard_factor_generation_only:
- split.prop(scene, "discard_factor_after_generation", text="Angle After")
- else:
- split.label(text="")
- if scene.weight_exponent_generation_only:
- split.prop(scene, "weight_exponent_after_generation", text="Exp. After")
- else:
- split.label(text="")
-
- row = content_box.row()
- row.prop(scene, "early_priority", text="Prioritize Initial Views", toggle=True, icon="REW")
- if scene.early_priority:
- row = content_box.row()
- row.prop(scene, "early_priority_strength", text="Priority Strength")
-
- row = content_box.row()
- row.prop(scene, "bake_visibility_weights", text="Bake Visibility (Transform-Stable)", toggle=True, icon="MESH_DATA")
-
- row = content_box.row()
- row.prop(scene, "sg_silhouette_margin", text="Silhouette Margin (px)")
-
- row = content_box.row()
- row.prop(scene, "sg_silhouette_depth", text="Silhouette Depth Threshold")
-
- row = content_box.row()
- row.prop(scene, "sg_silhouette_rays", text="Silhouette Rays")
-
- # --- Output & Material Settings ---
- if _show_diffusion_sections:
- content_box = draw_collapsible_section(advanced_params_box, "show_output_material_settings", "Output & Material Settings", icon="MATERIAL")
- else:
- content_box = None
- if content_box:
- # ── PBR Decomposition ──
- row = content_box.row()
- if 'pbr_decomposition' in _diff_props:
- row.alert = True
- row.prop(scene, "pbr_decomposition", text="PBR Decomposition", toggle=True, icon="NODE_MATERIAL")
- if 'pbr_decomposition' in _diff_props:
- row.label(text="→ " + _diff_props['pbr_decomposition'])
- if scene.pbr_decomposition:
- # Warning if PBR decomposition nodes not detected
- if not getattr(scene, 'pbr_nodes_available', False):
- warn_row = content_box.row()
- warn_row.alert = True
- warn_split = warn_row.split(factor=0.9)
- warn_split.label(text="PBR nodes not detected on server", icon="ERROR")
- warn_split.operator("stablegen.check_server_status", text="", icon="FILE_REFRESH")
- sub = content_box.box()
- sub.label(text="PBR Decomposition", icon="NODE_MATERIAL")
- # ── Map toggles ──
- sub.label(text="Maps to Extract:", icon="IMAGE_DATA")
- grid = sub.grid_flow(row_major=True, columns=3, even_columns=True, align=True)
- grid.prop(scene, "pbr_map_albedo", toggle=True, icon="SHADING_SOLID")
- grid.prop(scene, "pbr_map_roughness", toggle=True, icon="MATFLUID")
- grid.prop(scene, "pbr_map_metallic", toggle=True, icon="META_BALL")
- grid.prop(scene, "pbr_map_normal", toggle=True, icon="NORMALS_FACE")
- grid.prop(scene, "pbr_map_height", toggle=True, icon="MOD_DISPLACE")
- grid.prop(scene, "pbr_map_ao", toggle=True, icon="SHADING_RENDERED")
- grid.prop(scene, "pbr_map_emission", toggle=True, icon="LIGHT_POINT")
- # ── Per-map adjustment controls ──
- if scene.pbr_map_normal:
- adj_row = sub.row()
- adj_row.prop(scene, "pbr_normal_strength", text="Normal Strength", slider=True)
- if scene.pbr_map_height:
- adj_row = sub.row()
- adj_row.prop(scene, "pbr_height_scale", text="Height Scale", slider=True)
- if scene.pbr_map_ao:
- ao_row = sub.row(align=True)
- ao_row.prop(scene, "pbr_ao_samples", text="AO Samples")
- ao_row.prop(scene, "pbr_ao_distance", text="AO Distance")
- if scene.pbr_map_emission:
- adj_row = sub.row()
- adj_row.prop(scene, "pbr_emission_method", text="Method")
- adj_row = sub.row()
- adj_row.prop(scene, "pbr_emission_threshold", text="Threshold", slider=True)
- adj_row = sub.row()
- adj_row.prop(scene, "pbr_emission_strength", text="Emission Strength", slider=True)
- if scene.pbr_emission_method == 'hsv':
- hsv_row = sub.row(align=True)
- hsv_row.prop(scene, "pbr_emission_saturation_min", text="Sat Min", slider=True)
- hsv_row.prop(scene, "pbr_emission_value_min", text="Val Min", slider=True)
- hsv_row = sub.row()
- hsv_row.prop(scene, "pbr_emission_bloom", text="Bloom Radius", slider=True)
- # ── Albedo source selector (only when albedo enabled) ──
- if scene.pbr_map_albedo:
- sub.separator()
- row = sub.row()
- row.prop(scene, "pbr_albedo_source", text="Albedo Source")
- if scene.pbr_albedo_source == 'delight':
- adj_row = sub.row()
- adj_row.prop(scene, "pbr_delight_strength", text="Delight Strength", slider=True)
- adj_row = sub.row(align=True)
- adj_row.prop(scene, "pbr_albedo_auto_saturation", text="Correct Albedo Saturation", toggle=True, icon="BRUSHES_ALL")
- if scene.pbr_albedo_auto_saturation:
- adj_row.prop(scene, "pbr_albedo_saturation_mode", text="")
- sub.separator()
- # ── Quality settings ──
- row = sub.row(align=True)
- row.prop(scene, "pbr_use_native_resolution", text="Native Resolution", toggle=True, icon="FULLSCREEN_ENTER")
- row.prop(scene, "pbr_tiling", text="")
- if scene.pbr_tiling != 'off':
- row = sub.row(align=True)
- row.prop(scene, "pbr_tile_grid", text="Tile Grid (N\u00d7N)")
- row.prop(scene, "pbr_tile_superres", text="Super Res", toggle=True, icon="IMAGE_PLANE")
- if scene.pbr_tiling == 'custom':
- row = sub.row(align=True)
- row.prop(scene, "pbr_tile_albedo", text="Albedo", toggle=True)
- row.prop(scene, "pbr_tile_material", text="Material", toggle=True)
- row.prop(scene, "pbr_tile_normal", text="Normal", toggle=True)
- row.prop(scene, "pbr_tile_height", text="Height", toggle=True)
- row.prop(scene, "pbr_tile_emission", text="Emission", toggle=True)
- if not scene.pbr_use_native_resolution:
- row = sub.row()
- row.prop(scene, "pbr_processing_resolution", text="Processing Resolution")
- row = sub.row(align=True)
- row.prop(scene, "pbr_denoise_steps", text="Denoise Steps")
- row.prop(scene, "pbr_ensemble_size", text="Ensemble Size")
- sub.separator()
- row = sub.row()
- row.prop(scene, "pbr_replace_color_with_albedo", text="Use Albedo as Base Color", toggle=True, icon="SHADING_SOLID")
- row = sub.row()
- row.prop(scene, "pbr_auto_lighting", text="Studio Lighting", toggle=True, icon="LIGHT_AREA")
-
- content_box.separator()
- split = content_box.split(factor=0.5)
- split.label(text="Fallback Color:")
- split.prop(scene, "fallback_color", text="")
-
- row = content_box.row()
- row.prop(scene, "auto_rescale", text="Auto Rescale Resolution", toggle=True, icon="ARROW_LEFTRIGHT")
- if scene.auto_rescale:
- sub_box_rescale = content_box.box()
- row = sub_box_rescale.row()
- row.prop(scene, "auto_rescale_target_mp", text="Target Megapixels")
- if scene.model_architecture.startswith('qwen'):
- row = sub_box_rescale.row()
- row.prop(scene, "qwen_rescale_alignment", text="Qwen VL-Aligned Rescale (112px)", toggle=True, icon="SNAP_INCREMENT")
- row = content_box.row()
- if 'overwrite_material' in _diff_props:
- row.alert = True
- row.prop(scene, "overwrite_material", text="Overwrite Material", toggle=True, icon="FILE_REFRESH")
- if 'overwrite_material' in _diff_props:
- row.label(text="→ " + _diff_props['overwrite_material'])
-
- # --- Image Guidance (IPAdapter & ControlNet) ---
- if _show_diffusion_sections:
- if scene.model_architecture in ['sdxl', 'flux1']:
- content_box = draw_collapsible_section(advanced_params_box, "show_image_guidance_settings", "Image Guidance (IPAdapter & ControlNet)", icon="MODIFIER")
- elif scene.model_architecture == 'flux2_klein':
- content_box = draw_collapsible_section(advanced_params_box, "show_image_guidance_settings", "FLUX.2 Klein Guidance", icon="MODIFIER")
- else: # Qwen Image Edit
- content_box = draw_collapsible_section(advanced_params_box, "show_image_guidance_settings", "Qwen-Image-Edit Guidance", icon="MODIFIER")
- else:
- content_box = None
- if content_box:
- if scene.model_architecture in ('qwen_image_edit', 'flux2_klein'):
- if scene.model_architecture == 'flux2_klein' or scene.qwen_generation_method == 'generate':
- split = content_box.split(factor=0.5)
- split.label(text="Guidance Map:")
- split.prop(scene, "qwen_guidance_map_type", text="")
-
- row = content_box.row()
- row.prop(scene, "qwen_use_external_style_image", text="Use External Image as Style", toggle=True, icon="FILE_IMAGE")
-
- # TRELLIS.2 input as style (only in trellis2 architecture mode)
- if getattr(scene, 'architecture_mode', '') == 'trellis2':
- row = content_box.row()
- row.prop(scene, "qwen_use_trellis2_style", text="Use TRELLIS.2 Input as Style", toggle=True, icon="MESH_MONKEY")
-
- if scene.qwen_use_external_style_image:
- style_box = content_box.box()
- row = style_box.row()
- row.prop(scene, "qwen_external_style_image", text="Style Image")
- row = style_box.row()
- 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:
- 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 getattr(scene, 'architecture_mode', '') == 'trellis2' and scene.qwen_use_trellis2_style:
- t2_style_box = content_box.box()
- row = t2_style_box.row()
- row.prop(scene, "qwen_trellis2_style_initial_only", text="TRELLIS.2 Style for Initial Only", toggle=True)
- if scene.qwen_trellis2_style_initial_only:
- subsequent_box = t2_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:
- 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)
- split.label(text="Context Render:")
- split.prop(scene, "qwen_context_render_mode", text="")
-
- row = content_box.row()
- row.prop(scene, "qwen_voronoi_mode", text="Voronoi Projection", toggle=True, icon="MESH_GRID")
-
- elif scene.qwen_generation_method in ('refine', 'local_edit'):
- row = content_box.row()
- row.prop(scene, "qwen_refine_use_prev_ref", text="Use Previous Refined View", toggle=True)
-
- row = content_box.row()
- row.prop(scene, "qwen_refine_use_depth", text="Use Depth Map", toggle=True, icon="MODIFIER")
-
- row = content_box.row()
- row.prop(scene, "qwen_use_external_style_image", text="Use External Image as Style", toggle=True, icon="FILE_IMAGE")
-
- # TRELLIS.2 input as style (only in trellis2 architecture mode)
- if getattr(scene, 'architecture_mode', '') == 'trellis2':
- row = content_box.row()
- row.prop(scene, "qwen_use_trellis2_style", text="Use TRELLIS.2 Input as Style", toggle=True, icon="MESH_MONKEY")
-
- if scene.qwen_use_external_style_image:
- style_box = content_box.box()
- row = style_box.row()
- row.prop(scene, "qwen_external_style_image", text="Style Image")
-
- # Timestep-zero reference method — shared across all Qwen modes
- row = content_box.row()
- row.prop(scene, "qwen_timestep_zero_ref", text="Timestep-Zero References (color shift fix)", toggle=True, icon="COLORSET_08_VEC")
-
- row = content_box.row()
- row.prop(scene, "qwen_use_custom_prompts", text="Custom Guidance Prompts", toggle=True, icon="TEXT")
- if scene.qwen_use_custom_prompts:
- custom_prompt_box = content_box.box()
-
- # Initial Image Prompt
- col = custom_prompt_box.column()
- col.label(text="Initial Image Prompt:")
- row = col.row(align=True)
- row.prop(scene, "qwen_custom_prompt_initial", text="")
- op = row.operator("stablegen.reset_qwen_prompt", text="", icon='FILE_REFRESH')
- op.prompt_type = 'initial'
-
- # Subsequent Images Prompt (conditional)
- if scene.generation_method == 'sequential' or (scene.qwen_generation_method in ('refine', 'local_edit') and scene.qwen_refine_mode == 'sequential'):
- col = custom_prompt_box.column()
- col.label(text="Subsequent Images Prompt:")
- row = col.row(align=True)
-
- if scene.qwen_context_render_mode == 'NONE' and scene.qwen_generation_method == 'generate':
- row.prop(scene, "qwen_custom_prompt_seq_none", text="")
- op_prop = 'seq_none'
- elif scene.qwen_context_render_mode == 'REPLACE_STYLE' and scene.qwen_generation_method == 'generate':
- row.prop(scene, "qwen_custom_prompt_seq_replace", text="")
- op_prop = 'seq_replace'
- elif scene.qwen_context_render_mode == 'ADDITIONAL' and scene.qwen_generation_method == 'generate':
- row.prop(scene, "qwen_custom_prompt_seq_additional", text="")
- op_prop = 'seq_additional'
- else: # Refine mode or other
- row.prop(scene, "qwen_custom_prompt_seq_none", text="")
- op_prop = 'seq_none'
-
- op = row.operator("stablegen.reset_qwen_prompt", text="", icon='FILE_REFRESH')
- op.prompt_type = op_prop
-
- if (scene.generation_method == 'sequential' and scene.qwen_generation_method == 'generate' and
- scene.qwen_context_render_mode in {'REPLACE_STYLE', 'ADDITIONAL'}):
- context_box = content_box.box()
- context_box.label(text="Context Render Options")
-
- row = context_box.row()
- row.prop(scene, "qwen_prompt_gray_background", text="Prompt: Gray Background", toggle=True)
-
- 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':
- # IPAdapter Parameters
- if not scene.generation_method == 'uv_inpaint':
- ipadapter_main_box = content_box.box() # Group IPAdapter settings together
- if scene.model_architecture == 'flux1':
- row = ipadapter_main_box.row()
- row.prop(scene, "use_flux_lora", text="Use Flux Depth LoRA", toggle=True, icon="MODIFIER")
- row = ipadapter_main_box.row()
- row.prop(scene, "use_ipadapter", text="Use IPAdapter (External image)", toggle=True, icon="MOD_MULTIRES")
- if scene.use_ipadapter:
- sub_ip_box = ipadapter_main_box.box()
- row = sub_ip_box.row()
- row.prop(scene, "ipadapter_image", text="Image")
- row = sub_ip_box.row()
- row.prop(scene, "ipadapter_strength", text="Strength")
- if width_mode == 'narrow':
- row = sub_ip_box.row()
- row.prop(scene, "ipadapter_start", text="Start")
- if width_mode == 'narrow':
- row = sub_ip_box.row()
- row.prop(scene, "ipadapter_end", text="End")
- split = sub_ip_box.split(factor=0.5)
- if context.scene.model_architecture == 'sdxl':
- split.label(text="Weight Type:")
- split.prop(scene, "ipadapter_weight_type", text="")
-
- content_box.separator() # Separator between IPAdapter and ControlNet if both are shown
- # ControlNet Parameters
- if not (scene.model_architecture == 'flux1' and scene.use_flux_lora):
- cn_box = content_box.box()
- row = cn_box.row()
- row.alignment = 'CENTER'
- row.label(text="ControlNet Units", icon="NODETREE")
- for i, unit in enumerate(scene.controlnet_units):
- sub_unit_box = cn_box.box() # Each unit gets its own box
- row = sub_unit_box.row()
- row.label(text=f"Unit: {unit.unit_type.replace('_', ' ').title()}", icon="DOT")
- row.alignment = 'LEFT'
-
- if width_mode == 'narrow':
- split = sub_unit_box.split(factor=0.35, align=True)
- else:
- split = sub_unit_box.split(factor=0.2, align=True)
- split.label(text="Model:")
- split.prop(unit, "model_name", text="")
-
- row = sub_unit_box.row()
- row.prop(unit, "strength", text="Strength")
- if width_mode == 'narrow':
- row = sub_unit_box.row()
- row.prop(unit, "start_percent", text="Start")
- if width_mode == 'narrow':
- row = sub_unit_box.row()
- row.prop(unit, "end_percent", text="End")
-
- if unit.unit_type == 'canny':
- row = sub_unit_box.row()
- row.prop(scene, "canny_threshold_low", text="Canny Low")
- if width_mode == 'narrow':
- row = sub_unit_box.row()
- row.prop(scene, "canny_threshold_high", text="Canny High")
- if hasattr(unit, 'is_union') and unit.is_union:
- row = sub_unit_box.row()
- row.prop(unit, "use_union_type", text="Set Union Type", toggle=True, icon="MOD_BOOLEAN")
-
- btn_row = cn_box.row(align=True)
- if width_mode == 'wide':
- btn_row.operator("stablegen.add_controlnet_unit", text="Add Unit", icon="ADD")
- btn_row.operator("stablegen.remove_controlnet_unit", text="Remove Unit", icon="REMOVE")
- else:
- cn_box.operator("stablegen.add_controlnet_unit", text="Add ControlNet Unit", icon="ADD")
- cn_box.operator("stablegen.remove_controlnet_unit", text="Remove Last ControlNet Unit", icon="REMOVE")
-
- if _show_diffusion_sections and scene.model_architecture not in ('qwen_image_edit', 'flux2_klein'):
- # --- Inpainting Options (Conditional) ---
- if scene.generation_method == 'uv_inpaint' or scene.generation_method == 'sequential':
- content_box = draw_collapsible_section(advanced_params_box, "show_masking_inpainting_settings", "Inpainting Options", icon="MOD_MASK")
- if content_box: # content_box is the container for these settings
- row = content_box.row()
- if 'differential_diffusion' in _diff_props:
- row.alert = True
- row.prop(scene, "differential_diffusion", text="Use Differential Diffusion", toggle=True, icon="SMOOTHCURVE")
- if 'differential_diffusion' in _diff_props:
- row.label(text="→ " + _diff_props['differential_diffusion'])
-
- if scene.differential_diffusion:
- row = content_box.row()
- row.prop(scene, "differential_noise", text="Add Latent Noise Mask", toggle=True, icon="MOD_NOISE")
-
- if not (scene.differential_diffusion and not scene.differential_noise):
- row = content_box.row()
- row.prop(scene, "mask_blocky", text="Use Blocky Mask", icon="MOD_MASK")
-
- if width_mode == 'narrow':
- row = content_box.row()
-
- row.prop(scene, "blur_mask", text="Blur Mask", toggle=True, icon="SURFACE_NSPHERE")
-
- if scene.blur_mask:
- row = content_box.row()
- row.prop(scene, "blur_mask_radius", text="Blur Radius")
- if width_mode == 'narrow':
- row = content_box.row()
- row.prop(scene, "blur_mask_sigma", text="Blur Sigma")
-
- row = content_box.row() # Draw directly in content_box
- row.prop(scene, "grow_mask_by", text="Grow Mask By")
-
-
- # --- Generation Mode Specifics ---
- if _show_diffusion_sections:
- mode_specific_outer_box = draw_collapsible_section(advanced_params_box, "show_mode_specific_settings", "Generation Mode Specifics", icon="OPTIONS")
- else:
- mode_specific_outer_box = None
- if mode_specific_outer_box: # This is the box where all mode-specific UIs should go
-
- # Qwen Local Edit Mode Parameters
- if scene.model_architecture.startswith('qwen') and scene.qwen_generation_method == 'local_edit':
- row = mode_specific_outer_box.row()
- row.alignment = 'CENTER'
- row.label(text="Qwen Local Edit Parameters", icon='BRUSH_DATA')
- row = mode_specific_outer_box.row()
- if 'denoise' in _diff_props:
- row.alert = True
- row.prop(scene, "denoise", text="Denoise")
- if 'denoise' in _diff_props:
- row.label(text="→ " + _diff_props['denoise'])
-
- # Angle Ramp Controls
- box = mode_specific_outer_box.box()
- row = box.row()
- row.prop(scene, "refine_angle_ramp_active", text="Use Angle-Based Blending", icon="DRIVER")
- if scene.refine_angle_ramp_active:
- row = box.row()
- row.prop(scene, "refine_angle_ramp_pos_0", text="Black Point")
- row.prop(scene, "refine_angle_ramp_pos_1", text="White Point")
-
- # Feather Ramp Controls
- box = mode_specific_outer_box.box()
- row = box.row()
- row.prop(scene, "visibility_vignette", text="Use Vignette Blending", icon="DRIVER")
- if scene.visibility_vignette:
- row = box.row()
- row.prop(scene, "refine_feather_ramp_pos_0", text="Black Point")
- row.prop(scene, "refine_feather_ramp_pos_1", text="White Point")
- row = box.row()
- row.prop(scene, "visibility_vignette_width", text="Feather Width")
- if width_mode == 'narrow':
- row = box.row()
- row.prop(scene, "visibility_vignette_softness", text="Feather Softness")
- row = box.row()
- row.prop(scene, "visibility_vignette_blur", text="Blur Mask", icon="SURFACE_NSPHERE")
-
- # Edge Feather Projection Controls
- box = mode_specific_outer_box.box()
- row = box.row()
- row.prop(scene, "refine_edge_feather_projection", text="Edge Feather (Projection)", icon="MOD_EDGESPLIT")
- if scene.refine_edge_feather_projection:
- row = box.row()
- row.prop(scene, "refine_edge_feather_width", text="Feather Width (px)")
- row = box.row()
- row.prop(scene, "refine_edge_feather_softness", text="Feather Softness")
-
- # Color Matching
- box = mode_specific_outer_box.box()
- row = box.row()
- row.prop(scene, "view_blend_use_color_match", text="Match Colors to Viewport", toggle=True, icon="COLOR")
- if scene.view_blend_use_color_match:
- row = box.row(align=True)
- row.prop(scene, "view_blend_color_match_method", text="Method")
- row = box.row()
- row.prop(scene, "view_blend_color_match_strength", text="Strength")
-
- # Qwen Refine Mode Parameters
- elif scene.model_architecture.startswith('qwen') and scene.qwen_generation_method == 'refine':
- row = mode_specific_outer_box.row()
- row.alignment = 'CENTER'
- row.label(text="Qwen Refine Parameters", icon='SHADERFX')
- row = mode_specific_outer_box.row()
- if 'denoise' in _diff_props:
- row.alert = True
- row.prop(scene, "denoise", text="Denoise")
- if 'denoise' in _diff_props:
- row.label(text="→ " + _diff_props['denoise'])
-
- # Grid Mode Parameters
- elif scene.generation_method == 'grid':
- # Draw Grid parameters directly into mode_specific_outer_box
- row = mode_specific_outer_box.row()
- row.alignment = 'CENTER'
- row.label(text="Grid Mode Parameters", icon="MESH_GRID")
-
- row = mode_specific_outer_box.row()
- row.prop(scene, "refine_images", text="Refine Images", toggle=True, icon="SHADERFX")
- if scene.refine_images:
- split = mode_specific_outer_box.split(factor=0.5)
- split.label(text="Refine Sampler:")
- split.prop(scene, "refine_sampler", text="")
-
- split = mode_specific_outer_box.split(factor=0.5)
- split.label(text="Refine Scheduler:")
- split.prop(scene, "refine_scheduler", text="")
-
- row = mode_specific_outer_box.row()
- if 'denoise' in _diff_props:
- row.alert = True
- row.prop(scene, "denoise", text="Denoise")
- if 'denoise' in _diff_props:
- row.label(text="→ " + _diff_props['denoise'])
- if width_mode == 'narrow':
- row = mode_specific_outer_box.row()
- row.prop(scene, "refine_cfg", text="Refine CFG")
- if width_mode == 'narrow':
- row = mode_specific_outer_box.row()
- row.prop(scene, "refine_steps", text="Refine Steps")
-
- row = mode_specific_outer_box.row()
- split = mode_specific_outer_box.split(factor=0.25)
- split.label(text="Refine Prompt:")
- split.prop(scene, "refine_prompt", text="")
-
- split = mode_specific_outer_box.split(factor=0.5)
- split.label(text="Refine Upscale:")
- split.prop(scene, "refine_upscale_method", text="")
-
- # Separate Mode Parameters
- elif scene.generation_method == 'separate':
- row = mode_specific_outer_box.row()
- row.alignment = 'CENTER'
- row.label(text="Separate Mode Parameters", icon='FORCE_FORCE')
-
- row = mode_specific_outer_box.row()
- row.prop(scene, "sequential_ipadapter", text="Use IPAdapter for Separate Mode", toggle=True, icon="MODIFIER")
- if scene.sequential_ipadapter:
- sub_ip_box_separate = mode_specific_outer_box.box()
-
- split = sub_ip_box_separate.split(factor=0.5)
- split.label(text="Mode:")
- split.prop(scene, "sequential_ipadapter_mode", text="")
-
- if scene.sequential_ipadapter_mode == 'recent':
- sub_ip_box_separate.prop(scene, "sequential_desaturate_factor", text="Desaturate")
- sub_ip_box_separate.prop(scene, "sequential_contrast_factor", text="Reduce Contrast")
-
- if context.scene.model_architecture not in ('qwen_image_edit', 'flux2_klein'):
- split = sub_ip_box_separate.split(factor=0.5)
- if context.scene.model_architecture == 'sdxl':
- split.label(text="Weight Type:")
- split.prop(scene, "ipadapter_weight_type", text="")
-
- row = sub_ip_box_separate.row()
- row.prop(scene, "ipadapter_strength", text="Strength")
- if width_mode == 'narrow':
- row = sub_ip_box_separate.row()
- row.prop(scene, "ipadapter_start", text="Start")
- if width_mode == 'narrow':
- row = sub_ip_box_separate.row()
- row.prop(scene, "ipadapter_end", text="End")
-
- if context.scene.sequential_ipadapter_mode == 'first':
- row = sub_ip_box_separate.row()
- row.prop(scene, "sequential_ipadapter_regenerate", text="Regenerate First Image", toggle=True, icon="FILE_REFRESH")
- if context.scene.sequential_ipadapter_regenerate:
- row = sub_ip_box_separate.row()
- row.prop(scene, "sequential_ipadapter_regenerate_wo_controlnet", text="Generate reference without ControlNet", toggle=True, icon="HIDE_OFF")
-
- # Refine Mode Parameters
- elif scene.generation_method == 'refine':
- row = mode_specific_outer_box.row()
- row.alignment = 'CENTER'
- row.label(text="Refine Mode Parameters", icon='SHADERFX')
- row = mode_specific_outer_box.row()
- if 'denoise' in _diff_props:
- row.alert = True
- row.prop(scene, "denoise", text="Denoise")
- if 'denoise' in _diff_props:
- row.label(text="→ " + _diff_props['denoise'])
- row = mode_specific_outer_box.row()
- row.prop(scene, "sequential_ipadapter", text="Use IPAdapter for Refine Mode", toggle=True, icon="MODIFIER")
- if scene.sequential_ipadapter:
- sub_ip_box_separate = mode_specific_outer_box.box()
-
- split = sub_ip_box_separate.split(factor=0.5)
- split.label(text="Mode:")
- split.prop(scene, "sequential_ipadapter_mode", text="")
-
- split = sub_ip_box_separate.split(factor=0.5)
- if context.scene.model_architecture == 'sdxl':
- split.label(text="Weight Type:")
- split.prop(scene, "ipadapter_weight_type", text="")
-
- row = sub_ip_box_separate.row()
- row.prop(scene, "ipadapter_strength", text="Strength")
- if width_mode == 'narrow':
- row = sub_ip_box_separate.row()
- row.prop(scene, "ipadapter_start", text="Start")
- if width_mode == 'narrow':
- row = sub_ip_box_separate.row()
- row.prop(scene, "ipadapter_end", text="End")
-
- if context.scene.sequential_ipadapter_mode == 'first':
- row = sub_ip_box_separate.row()
- row.prop(scene, "sequential_ipadapter_regenerate", text="Regenerate First Image", toggle=True, icon="FILE_REFRESH")
- if context.scene.sequential_ipadapter_regenerate:
- row = sub_ip_box_separate.row()
- row.prop(scene, "sequential_ipadapter_regenerate_wo_controlnet", text="Generate reference without ControlNet", toggle=True, icon="HIDE_OFF")
-
- # Local Edit Mode Parameters
- elif scene.generation_method == 'local_edit':
- row = mode_specific_outer_box.row()
- row.alignment = 'CENTER'
- row.label(text="Local Edit Parameters", icon='BRUSH_DATA')
- row = mode_specific_outer_box.row()
- if 'denoise' in _diff_props:
- row.alert = True
- row.prop(scene, "denoise", text="Denoise")
- if 'denoise' in _diff_props:
- row.label(text="→ " + _diff_props['denoise'])
-
- # Angle Ramp Controls
- box = mode_specific_outer_box.box()
- row = box.row()
- row.prop(scene, "refine_angle_ramp_active", text="Use Angle-Based Blending", icon="DRIVER")
- if scene.refine_angle_ramp_active:
- row = box.row()
- row.prop(scene, "refine_angle_ramp_pos_0", text="Black Point")
- row.prop(scene, "refine_angle_ramp_pos_1", text="White Point")
-
- # Feather Ramp Controls
- box = mode_specific_outer_box.box()
- row = box.row()
- row.prop(scene, "visibility_vignette", text="Use Vignette Blending", icon="DRIVER")
- if scene.visibility_vignette:
- row = box.row()
- row.prop(scene, "refine_feather_ramp_pos_0", text="Black Point")
- row.prop(scene, "refine_feather_ramp_pos_1", text="White Point")
- row = box.row()
- row.prop(scene, "visibility_vignette_width", text="Feather Width")
- if width_mode == 'narrow':
- row = box.row()
- row.prop(scene, "visibility_vignette_softness", text="Feather Softness")
- row = box.row()
- row.prop(scene, "visibility_vignette_blur", text="Blur Mask", icon="SURFACE_NSPHERE")
-
- # Edge Feather Projection Controls
- box = mode_specific_outer_box.box()
- row = box.row()
- row.prop(scene, "refine_edge_feather_projection", text="Edge Feather (Projection)", icon="MOD_EDGESPLIT")
- if scene.refine_edge_feather_projection:
- row = box.row()
- row.prop(scene, "refine_edge_feather_width", text="Feather Width (px)")
- row = box.row()
- row.prop(scene, "refine_edge_feather_softness", text="Feather Softness")
-
- # Color Matching
- box = mode_specific_outer_box.box()
- row = box.row()
- row.prop(scene, "view_blend_use_color_match", text="Match Colors to Viewport", toggle=True, icon="COLOR")
- if scene.view_blend_use_color_match:
- row = box.row(align=True)
- row.prop(scene, "view_blend_color_match_method", text="Method")
- row = box.row()
- row.prop(scene, "view_blend_color_match_strength", text="Strength")
-
- row = mode_specific_outer_box.row()
- row.prop(scene, "sequential_ipadapter", text="Use IPAdapter for Local Edit", toggle=True, icon="MODIFIER")
- if scene.sequential_ipadapter:
- sub_ip_box_separate = mode_specific_outer_box.box()
-
- split = sub_ip_box_separate.split(factor=0.5)
- split.label(text="Mode:")
- split.prop(scene, "sequential_ipadapter_mode", text="")
-
- split = sub_ip_box_separate.split(factor=0.5)
- if context.scene.model_architecture == 'sdxl':
- split.label(text="Weight Type:")
- split.prop(scene, "ipadapter_weight_type", text="")
-
- row = sub_ip_box_separate.row()
- row.prop(scene, "ipadapter_strength", text="Strength")
- if width_mode == 'narrow':
- row = sub_ip_box_separate.row()
- row.prop(scene, "ipadapter_start", text="Start")
- if width_mode == 'narrow':
- row = sub_ip_box_separate.row()
- row.prop(scene, "ipadapter_end", text="End")
-
- if context.scene.sequential_ipadapter_mode == 'first':
- row = sub_ip_box_separate.row()
- row.prop(scene, "sequential_ipadapter_regenerate", text="Regenerate First Image", toggle=True, icon="FILE_REFRESH")
- if context.scene.sequential_ipadapter_regenerate:
- row = sub_ip_box_separate.row()
- row.prop(scene, "sequential_ipadapter_regenerate_wo_controlnet", text="Generate reference without ControlNet", toggle=True, icon="HIDE_OFF")
-
- # UV Inpainting Parameters
- elif scene.generation_method == 'uv_inpaint':
- row = mode_specific_outer_box.row()
- row.alignment = 'CENTER'
- row.label(text="UV Inpainting Parameters", icon="IMAGE_PLANE")
- row = mode_specific_outer_box.row()
- row.prop(scene, "allow_modify_existing_textures", text="Allow Modifying Existing Textures", toggle=True, icon="TEXTURE")
- row = mode_specific_outer_box.row()
- row.prop(scene, "ask_object_prompts", text="Ask for Object Specific Prompts", toggle=True, icon="QUESTION")
-
- # Sequential Mode Parameters
- elif scene.generation_method == 'sequential':
- row = mode_specific_outer_box.row()
- row.alignment = 'CENTER'
- row.label(text="Sequential Mode Parameters", icon="SEQUENCE")
-
- if not (scene.differential_diffusion and not scene.differential_noise):
- row = mode_specific_outer_box.row()
- if 'sequential_smooth' in _diff_props:
- row.alert = True
- row.prop(scene, "sequential_smooth", text="Use Smooth Visibility Map", toggle=True, icon="MOD_SMOOTH")
- if 'sequential_smooth' in _diff_props:
- row.label(text="→ " + _diff_props['sequential_smooth'])
- if width_mode == 'narrow':
- row = mode_specific_outer_box.row()
- row.prop(scene, "weight_exponent_mask", text="Exponent for Visibility Map", toggle=True, icon="IPO_EXPO")
-
- if not scene.sequential_smooth:
- row = mode_specific_outer_box.row()
- row.prop(scene, "sequential_factor", text="Visibility Threshold")
- else:
- row = mode_specific_outer_box.row()
- row.prop(scene, "sequential_factor_smooth", text="Smooth Visibility Black Point")
- if width_mode == 'narrow':
- row = mode_specific_outer_box.row()
- row.prop(scene, "sequential_factor_smooth_2", text="Smooth Visibility White Point")
-
- row = mode_specific_outer_box.row()
- row.prop(scene, "sequential_ipadapter", text="Use IPAdapter for Sequential Mode", toggle=True, icon="MODIFIER")
- if scene.sequential_ipadapter:
- sub_ip_seq_box = mode_specific_outer_box.box()
-
- split = sub_ip_seq_box.split(factor=0.5)
- split.label(text="Mode:")
- split.prop(scene, "sequential_ipadapter_mode", text="")
-
- if scene.sequential_ipadapter_mode == 'recent':
- sub_ip_seq_box.prop(scene, "sequential_desaturate_factor", text="Desaturate")
- sub_ip_seq_box.prop(scene, "sequential_contrast_factor", text="Reduce Contrast")
-
- if context.scene.model_architecture not in ('qwen_image_edit', 'flux2_klein'):
- split = sub_ip_seq_box.split(factor=0.5)
- if context.scene.model_architecture == 'sdxl':
- split.label(text="Weight Type:")
- split.prop(scene, "ipadapter_weight_type", text="")
-
- row = sub_ip_seq_box.row()
- row.prop(scene, "ipadapter_strength", text="Strength")
- if width_mode == 'narrow':
- row = sub_ip_seq_box.row()
- row.prop(scene, "ipadapter_start", text="Start")
- if width_mode == 'narrow':
- row = sub_ip_seq_box.row()
- row.prop(scene, "ipadapter_end", text="End")
-
- if context.scene.sequential_ipadapter_mode == 'first':
- row = sub_ip_seq_box.row()
- row.prop(scene, "sequential_ipadapter_regenerate", text="Regenerate First Image", toggle=True, icon="FILE_REFRESH")
- if context.scene.sequential_ipadapter_regenerate:
- row = sub_ip_seq_box.row()
- row.prop(scene, "sequential_ipadapter_regenerate_wo_controlnet", text="Generate reference without ControlNet", toggle=True, icon="HIDE_OFF")
-
- # --- Tools ---
- layout.separator()
- tools_box = layout.box()
- row = tools_box.row()
- row.alignment = 'CENTER'
- row.label(text="Tools", icon="TOOL_SETTINGS")
-
- row = tools_box.row()
- row.operator("object.switch_material", text="Switch Material", icon="MATERIAL_DATA")
- if width_mode == 'narrow':
- row = tools_box.row()
- row.operator("object.add_hdri", text="Add HDRI Light", icon="WORLD")
-
- row = tools_box.row()
- row.operator("object.apply_all_mesh_modifiers", text="Apply All Modifiers", icon="MODIFIER_DATA")
- if width_mode == 'narrow':
- row = tools_box.row()
- row.operator("object.curves_to_mesh", text="Convert Curves to Mesh", icon="CURVE_DATA")
-
- row = tools_box.row()
- if config_error_message:
- row.enabled = False
- row.operator("object.export_orbit_gif", text=f"Cannot Export: {config_error_message}", icon="ERROR")
- else:
- row.enabled = True
- row.operator("object.export_orbit_gif", text="Export Orbit GIF/MP4", icon="RENDER_ANIMATION")
-
- if width_mode == 'narrow':
- row = tools_box.row()
- row.operator("object.stablegen_reproject", text="Reproject Textures", icon="FILE_REFRESH")
-
- row = tools_box.row()
- row.operator("object.stablegen_mirror_reproject", text="Mirror Last Projection", icon="MOD_MIRROR")
-
- # --- Debug Tools ---
- prefs = context.preferences.addons.get(__package__)
- if prefs and prefs.preferences.enable_debug:
- layout.separator()
- debug_box = layout.box()
- row = debug_box.row()
- row.alignment = 'CENTER'
- row.label(text="Debug / Diagnostics", icon="GHOST_ENABLED")
-
- row = debug_box.row()
- row.operator("stablegen.debug_solid_colors", text="Draw Solid Colors", icon="COLOR")
- if width_mode == 'narrow':
- row = debug_box.row()
- row.operator("stablegen.debug_grid_pattern", text="Grid Pattern", icon="MESH_GRID")
-
- row = debug_box.row()
- row.operator("stablegen.debug_coverage_heatmap", text="Coverage Heatmap", icon="AREA_SWAP")
- if width_mode == 'narrow':
- row = debug_box.row()
- row.operator("stablegen.debug_visibility_material", text="Visibility Material", icon="HIDE_OFF")
-
- row = debug_box.row()
- row.operator("stablegen.debug_uv_seam_viz", text="UV Seam Visualizer", icon="UV")
- if width_mode == 'narrow':
- row = debug_box.row()
- row.operator("stablegen.debug_restore_material", text="Remove Debug Mats", icon="TRASH")
-
- row = debug_box.row()
- op = row.operator("stablegen.debug_per_camera_weight", text="Per-Camera Weight", icon="CAMERA_DATA")
- if width_mode == 'narrow':
- row = debug_box.row()
- op2 = row.operator("stablegen.debug_feather_preview", text="Feather Preview", icon="MOD_SMOOTH")
-
- # --- Narrow panel hint ---
- if width_mode == 'narrow':
- hint_row = layout.row()
- hint_row.alignment = 'CENTER'
- hint_row.label(text="Widen panel for side-by-side layout", icon="INFO")
-
- layout.separator()
-
-
-class ResetQwenPrompt(bpy.types.Operator):
- """Resets a guidance prompt to its default value"""
- bl_idname = "stablegen.reset_qwen_prompt"
- bl_label = "Reset Qwen Prompt"
- bl_description = "Reset this prompt to its default value based on the current settings"
-
- prompt_type: bpy.props.StringProperty()
-
- @classmethod
- def poll(cls, context):
- return not sg_modal_active(context)
-
- def execute(self, context):
- from .workflows import WorkflowManager
- # We need an operator instance to call the helper, a bit of a workaround
- # This doesn't run generation, just gives us access to the method
- wm = WorkflowManager(self)
-
- # Choose the correct default prompt builder for the active architecture
- is_klein = getattr(context.scene, 'model_architecture', '') == 'flux2_klein'
- get_defaults = wm._get_klein_default_prompts if is_klein else wm._get_qwen_default_prompts
-
- # Determine which prompt to reset
- if self.prompt_type == 'initial':
- default_prompt = get_defaults(context, is_initial_image=True)
- context.scene.qwen_custom_prompt_initial = default_prompt
- elif self.prompt_type == 'seq_none':
- default_prompt = get_defaults(context, is_initial_image=False)
- context.scene.qwen_custom_prompt_seq_none = default_prompt
- elif self.prompt_type == 'seq_replace':
- default_prompt = get_defaults(context, is_initial_image=False)
- context.scene.qwen_custom_prompt_seq_replace = default_prompt
- elif self.prompt_type == 'seq_additional':
- default_prompt = get_defaults(context, is_initial_image=False)
- context.scene.qwen_custom_prompt_seq_additional = default_prompt
-
- self.report({'INFO'}, "Prompt reset to default.")
- return {'FINISHED'}
-
-class ApplyPreset(bpy.types.Operator):
- """Apply selected preset values to parameters"""
- bl_idname = "stablegen.apply_preset"
- bl_label = "Apply Preset"
- bl_description = "Set multiple parameters based on selected preset for easier configuration"
-
- @classmethod
- def poll(cls, context):
- return not sg_modal_active(context)
-
- def execute(self, context):
- preset = context.scene.stablegen_preset
- if preset in PRESETS:
- values = PRESETS[preset]
-
- # Apply architecture_mode first so dynamic enums that
- # depend on it (e.g. sequential_ipadapter_mode) are valid
- # when their values are set in the main loop.
- if "architecture_mode" in values and hasattr(context.scene, "architecture_mode"):
- try:
- setattr(context.scene, "architecture_mode", values["architecture_mode"])
- except (TypeError, AttributeError):
- pass
-
- # Apply regular parameters
- skipped = []
- for key, value in values.items():
- if key not in ["controlnet_units", "lora_units", "description"] and hasattr(context.scene, key):
- try:
- setattr(context.scene, key, value)
- except (TypeError, AttributeError):
- # Dynamic enum values (e.g. trellis2_input) may not
- # exist when the current architecture differs.
- skipped.append(key)
-
- # Apply ControlNet units if present in the preset
- if "controlnet_units" in values:
- # Clear existing units
- context.scene.controlnet_units.clear()
-
- # Add new units from preset
- controlnet_units = values["controlnet_units"]
- for unit_data in controlnet_units:
- new_unit = context.scene.controlnet_units.add()
- for key, value in unit_data.items():
- try:
- setattr(new_unit, key, value)
- except TypeError:
- self.report({'ERROR'}, f"Failed to set {key} for ControlNet unit: {value}. Model might be missing or might not be named correctly.")
- return {'CANCELLED'}
-
- if "lora_units" in values:
- # Clear existing LoRA units
- context.scene.lora_units.clear()
-
- # Add new LoRA units from preset
- lora_units = values["lora_units"]
- for lora_data in lora_units:
- new_lora = context.scene.lora_units.add()
- for key, value in lora_data.items():
- try:
- setattr(new_lora, key, value)
- except TypeError:
- self.report({'ERROR'}, f"Failed to set {key} for LoRA unit: {value}. Model might be missing or might not be named correctly.")
- context.scene.lora_units.remove(len(context.scene.lora_units) - 1)
- return {'CANCELLED'}
-
- # Reverse-sync: if the preset set model_architecture but didn't
- # include architecture_mode, update the visible dropdown to match.
- if "architecture_mode" not in values:
- arch = context.scene.model_architecture
- if arch in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein'):
- context.scene.architecture_mode = arch
-
- if skipped:
- self.report({'WARNING'}, f"Preset '{preset}' applied (skipped {len(skipped)} incompatible setting(s)).")
- else:
- self.report({'INFO'}, f"Preset '{preset}' applied.")
- else:
- self.report({'INFO'}, "Custom preset active.")
-
- # Force update to ensure preset detection is correct after list changes
- update_parameters(self, context)
-
- return {'FINISHED'}
-
-class SwitchToMeshGeneration(bpy.types.Operator):
- """Switch to mesh generation mode using TRELLIS.2"""
- bl_idname = "stablegen.switch_to_mesh_generation"
- bl_label = "Switch to Mesh Generation"
- bl_description = "Apply the TRELLIS.2 (Mesh Only) preset to generate 3D meshes"
-
- @classmethod
- def poll(cls, context):
- return (not sg_modal_active(context)
- and "TRELLIS.2 (MESH ONLY)" in PRESETS
- and getattr(context.scene, 'trellis2_available', False))
-
- def execute(self, context):
- context.scene.stablegen_preset = "TRELLIS.2 (MESH ONLY)"
- bpy.ops.stablegen.apply_preset()
- return {'FINISHED'}
-
-
-class SavePreset(bpy.types.Operator):
- """Save the current parameter values as a custom preset"""
- bl_idname = "stablegen.save_preset"
- bl_label = "Save Custom Preset"
-
- @classmethod
- def poll(cls, context):
- return not sg_modal_active(context)
-
- preset_name: bpy.props.StringProperty(
- name="Preset Name",
- default="MyPreset"
- ) # type: ignore
-
- preset_description: bpy.props.StringProperty(
- name="Description",
- description="A short description of what this preset is good for",
- default=""
- ) # type: ignore
-
- include_controlnet: bpy.props.BoolProperty(
- name="Include ControlNet Units",
- default=True,
- description="Include ControlNet units in the preset"
- ) # type: ignore
-
- include_loras: bpy.props.BoolProperty(
- name="Include LoRA Units",
- default=True
- ) # type: ignore
-
- def execute(self, context):
- scene = context.scene
- key = self.preset_name.upper()
-
- # Save all parameters defined in GEN_PARAMETERS
- PRESETS[key] = {param: getattr(scene, param) for param in GEN_PARAMETERS if hasattr(scene, param)}
-
- # Add description
- PRESETS[key]["description"] = self.preset_description
-
- # Add custom flag
- PRESETS[key]["custom"] = True
-
- if self.include_controlnet:
- # Save ControlNet units
- controlnet_units = []
- for unit in scene.controlnet_units:
- unit_data = {
- "unit_type": unit.unit_type,
- "model_name": unit.model_name,
- "strength": unit.strength,
- "start_percent": unit.start_percent,
- "end_percent": unit.end_percent,
- "is_union": unit.is_union,
- "use_union_type": unit.use_union_type
- }
- controlnet_units.append(unit_data)
-
- # Add controlnet units to the preset
- PRESETS[key]["controlnet_units"] = controlnet_units
-
- if self.include_loras: # Save LoRA units
- lora_units_data = []
- for lora_unit in scene.lora_units:
- lora_units_data.append({
- "model_name": lora_unit.model_name,
- "model_strength": lora_unit.model_strength,
- "clip_strength": lora_unit.clip_strength,
- })
-
- # Add LoRA units to the preset
- PRESETS[key]["lora_units"] = lora_units_data
-
- scene.stablegen_preset = key
- scene.active_preset = key
- self.report({'INFO'}, f"Preset '{self.preset_name}' saved.")
-
- # Print in the console for debugging
- print(f'"{key}": {{', end="")
- print(f'"description": "{self.preset_description}", ', end="")
- for param in GEN_PARAMETERS:
- if hasattr(scene, param):
- value = getattr(scene, param)
- if isinstance(value, str):
- print(f'"{param}": "{value}", ', end="")
- else:
- print(f'"{param}": {value}, ', end="")
-
- # Print controlnet units in a compact format if included
- 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):
- return context.window_manager.invoke_props_dialog(self)
-
- def draw(self, context):
- layout = self.layout
- layout.prop(self, "preset_name")
- layout.prop(self, "preset_description")
- layout.prop(self, "include_controlnet")
- layout.prop(self, "include_loras")
-
-class DeletePreset(bpy.types.Operator):
- """Delete a custom preset"""
- bl_idname = "stablegen.delete_preset"
- bl_label = "Delete Preset"
-
- @classmethod
- def poll(cls, context):
- return not sg_modal_active(context)
-
- def execute(self, context):
- preset = context.scene.stablegen_preset
- if preset in PRESETS:
- del PRESETS[preset]
- context.scene.stablegen_preset = "CUSTOM"
- self.report({'INFO'}, f"Preset '{preset}' deleted.")
- update_parameters(self, context)
- return {'FINISHED'}
- else:
- self.report({'WARNING'}, "Preset not found.")
- return {'CANCELLED'}
-
-
-# =====================================================================
-# Scene Queue System
-# =====================================================================
-# Allows users to queue multiple Blender scenes for unattended batch
-# processing. Each queued scene is processed in order — the manager
-# switches to the scene, invokes the appropriate generation operator
-# (TRELLIS.2 or standard texturing), waits for completion, optionally
-# saves a .blend copy, then advances to the next item.
-
-_queue_timer = None # Reference to the active timer callback
-_queue_processing = False # Global flag: queue is being processed
-_queue_current_idx = 0 # Index of the currently-processing item
-_queue_phase = 'idle' # 'idle' | 'switching' | 'running' | 'settling' | 'waiting_texturing' | 'exporting_gif'
-_queue_settle_deadline = 0.0 # monotonic time after which 'settling' may finish
-_queue_force_reload = False # Force re-open of the .blend on next idle tick (for retries)
-_queue_texturing_seen = False # Set True once generation_status='running' is observed during settling
-_queue_refresh_deadline = 0.0 # monotonic deadline while waiting for model-list refresh
-_queue_original_pbr = False # stored pbr_decomposition before no-PBR reproject
-_queue_original_gen_method = 'sequential' # stored generation_method before reproject
-_queue_original_overwrite = True # stored overwrite_material before reproject
-_QUEUE_MAX_RETRIES = 3 # how many times a failed item is retried
-
-
-class SG_UL_SceneQueueList(bpy.types.UIList):
- """UIList for the scene generation queue."""
- bl_idname = "SG_UL_SceneQueueList"
-
- def draw_item(self, context, layout, data, item, icon, active_data, active_property, index):
- if self.layout_type in {'DEFAULT', 'COMPACT'}:
- row = layout.row(align=True)
-
- # Label (or scene name as fallback)
- display = item.label if item.label else item.scene_name
- row.label(text=display, icon='SCENE_DATA')
-
- # Status icon + retry indicator
- if item.status == 'done':
- row.label(text="", icon='CHECKMARK')
- elif item.status == 'error':
- retry_txt = f"x{item.retries}" if item.retries else ""
- if item.error_reason:
- reason_short = item.error_reason[:28] + "…" if len(item.error_reason) > 30 else item.error_reason
- row.label(text=f"{reason_short} {retry_txt}".strip(), icon='ERROR')
- else:
- row.label(text=retry_txt, icon='ERROR')
- elif item.status == 'processing':
- retry_txt = f"#{item.retries + 1}" if item.retries else ""
- row.label(text=retry_txt, icon='SORTTIME')
- elif self.layout_type == 'GRID':
- layout.alignment = 'CENTER'
- display = item.label if item.label else item.scene_name
- layout.label(text=display, icon='SCENE_DATA')
-
-
-def _persist_queue():
- """Write the queue + processing state to a JSON file on disk."""
- try:
- from . import _sg_queue_save
- _sg_queue_save(
- processing=_queue_processing,
- current_idx=_queue_current_idx,
- phase=_queue_phase,
- )
- except Exception:
- pass
-
-
-def _resume_queue(idx):
- """Resume queue processing from *idx* (called after a .blend switch).
-
- This is invoked by ``_sg_queue_load()`` in ``__init__.py`` when the
- JSON file indicates that processing was active.
- """
- global _queue_processing, _queue_current_idx, _queue_phase, _queue_timer
-
- _queue_processing = True
- _queue_current_idx = idx
- _queue_phase = 'idle'
-
- # Avoid duplicate timers
- if _queue_timer is not None:
- try:
- bpy.app.timers.unregister(_queue_tick)
- except Exception:
- pass
-
- _queue_timer = bpy.app.timers.register(_queue_tick, first_interval=1.5)
- print(f"[Queue] Resumed processing from item {idx}")
- _tag_redraw()
-
-
-class SceneQueueAdd(bpy.types.Operator):
- """Add the current scene to the generation queue.
-
- Saves a *copy* of the current .blend into ``<output_dir>/queue_jobs/``.
- The queue item points at that copy so the original file is never
- modified by queue processing.
- """
- bl_idname = "stablegen.queue_add"
- bl_label = "Add to Queue"
- bl_description = "Snapshot the current .blend and add the scene to the batch queue"
- bl_options = {'REGISTER', 'UNDO'}
-
- item_label: bpy.props.StringProperty(
- name="Name",
- description="A short label for this queue entry",
- default="",
- ) # type: ignore
-
- @classmethod
- def poll(cls, context):
- return not _queue_processing and not sg_modal_active(context)
-
- def invoke(self, context, event):
- scene = context.scene
- cur_prompt = getattr(scene, 'comfyui_prompt', '')
- # Pre-fill with a truncated prompt
- self.item_label = (cur_prompt[:40]) if cur_prompt else scene.name
- return context.window_manager.invoke_props_dialog(self, width=350)
-
- def draw(self, context):
- layout = self.layout
- layout.prop(self, "item_label")
-
- def execute(self, context):
- import time
-
- wm = context.window_manager
- scene = context.scene
- scene_name = scene.name
- cur_prompt = getattr(scene, 'comfyui_prompt', '')
- cur_neg = getattr(scene, 'comfyui_negative_prompt', '')
- label = self.item_label.strip() or scene_name
-
- # Determine queue_jobs directory
- prefs = context.preferences.addons.get(__package__)
- output_dir = ''
- if prefs:
- output_dir = bpy.path.abspath(prefs.preferences.output_dir or '')
- if not output_dir or not os.path.isdir(output_dir):
- self.report({'ERROR'}, "Set an Output Directory in StableGen preferences first.")
- return {'CANCELLED'}
-
- jobs_dir = os.path.join(output_dir, "queue_jobs")
- os.makedirs(jobs_dir, exist_ok=True)
-
- # Build filename from the user-provided label
- timestamp = time.strftime("%Y%m%d_%H%M%S")
- safe_label = label.replace(" ", "_").replace("/", "_").replace("\\", "_")
- # Truncate to keep filenames reasonable
- safe_label = safe_label[:60]
- copy_name = f"{safe_label}_{timestamp}.blend"
- copy_path = os.path.join(jobs_dir, copy_name)
-
- # Save a copy — current file stays active & untouched
- # Snapshot the model_name into the plain-string backup property
- # BEFORE saving so the copy carries the correct value even if
- # the dynamic Enum resolves wrong on re-open (stale item cache).
- scene.sg_model_name_backup = scene.model_name
- try:
- bpy.ops.wm.save_as_mainfile(filepath=copy_path, copy=True)
- except Exception as e:
- self.report({'ERROR'}, f"Failed to save queue snapshot: {e}")
- return {'CANCELLED'}
-
- new_item = wm.sg_scene_queue.add()
- new_item.label = label
- new_item.scene_name = scene_name
- new_item.blend_file = copy_path
- new_item.prompt = cur_prompt
- new_item.negative_prompt = cur_neg
- new_item.status = 'pending'
- wm.sg_scene_queue_index = len(wm.sg_scene_queue) - 1
- _persist_queue()
-
- self.report({'INFO'}, f"Queued '{label}' ({copy_name})")
- return {'FINISHED'}
-
-
-class SceneQueueRemove(bpy.types.Operator):
- """Remove the selected scene from the queue"""
- bl_idname = "stablegen.queue_remove"
- bl_label = "Remove from Queue"
-
- @classmethod
- def poll(cls, context):
- wm = context.window_manager
- return not _queue_processing and len(wm.sg_scene_queue) > 0 and not sg_modal_active(context)
-
- def invoke(self, context, event):
- wm = context.window_manager
- idx = wm.sg_scene_queue_index
- if 0 <= idx < len(wm.sg_scene_queue):
- item = wm.sg_scene_queue[idx]
- display = item.label or item.scene_name
- # Show a confirmation popup
- return context.window_manager.invoke_confirm(self, event,
- message=f"Remove '{display}' from queue?",
- confirm_text="Remove")
- return {'CANCELLED'}
-
- def execute(self, context):
- wm = context.window_manager
- idx = wm.sg_scene_queue_index
- if 0 <= idx < len(wm.sg_scene_queue):
- item = wm.sg_scene_queue[idx]
- display = item.label or item.scene_name
- # Delete the snapshot .blend if the item hasn't been processed
- if item.status != 'done' and item.blend_file:
- try:
- blend_path = item.blend_file
- if os.path.isfile(blend_path):
- os.remove(blend_path)
- print(f"[Queue] Deleted snapshot: {blend_path}")
- except Exception as e:
- print(f"[Queue] Could not delete snapshot: {e}")
- wm.sg_scene_queue.remove(idx)
- wm.sg_scene_queue_index = min(idx, len(wm.sg_scene_queue) - 1)
- _persist_queue()
- self.report({'INFO'}, f"Removed '{display}' from queue.")
- return {'FINISHED'}
-
-
-class SceneQueueClear(bpy.types.Operator):
- """Clear the entire queue"""
- bl_idname = "stablegen.queue_clear"
- bl_label = "Clear Queue"
-
- @classmethod
- def poll(cls, context):
- wm = context.window_manager
- return not _queue_processing and len(wm.sg_scene_queue) > 0 and not sg_modal_active(context)
-
- def invoke(self, context, event):
- wm = context.window_manager
- count = len(wm.sg_scene_queue)
- return context.window_manager.invoke_confirm(self, event,
- message=f"Clear all {count} item(s) from queue?",
- confirm_text="Clear All")
-
- def execute(self, context):
- wm = context.window_manager
- # Delete snapshot .blends for unprocessed items
- for item in wm.sg_scene_queue:
- if item.status != 'done' and item.blend_file:
- try:
- if os.path.isfile(item.blend_file):
- os.remove(item.blend_file)
- print(f"[Queue] Deleted snapshot: {item.blend_file}")
- except Exception as e:
- print(f"[Queue] Could not delete snapshot: {e}")
- wm.sg_scene_queue.clear()
- wm.sg_scene_queue_index = 0
- _persist_queue()
- self.report({'INFO'}, "Queue cleared.")
- return {'FINISHED'}
-
-
-class SceneQueueMoveUp(bpy.types.Operator):
- """Move the selected queue item up"""
- bl_idname = "stablegen.queue_move_up"
- bl_label = "Move Up"
-
- @classmethod
- def poll(cls, context):
- wm = context.window_manager
- return not _queue_processing and wm.sg_scene_queue_index > 0 and not sg_modal_active(context)
-
- def execute(self, context):
- wm = context.window_manager
- idx = wm.sg_scene_queue_index
- wm.sg_scene_queue.move(idx, idx - 1)
- wm.sg_scene_queue_index -= 1
- return {'FINISHED'}
-
-
-class SceneQueueMoveDown(bpy.types.Operator):
- """Move the selected queue item down"""
- bl_idname = "stablegen.queue_move_down"
- bl_label = "Move Down"
-
- @classmethod
- def poll(cls, context):
- wm = context.window_manager
- return (not _queue_processing
- and wm.sg_scene_queue_index < len(wm.sg_scene_queue) - 1
- and not sg_modal_active(context))
-
- def execute(self, context):
- wm = context.window_manager
- idx = wm.sg_scene_queue_index
- wm.sg_scene_queue.move(idx, idx + 1)
- wm.sg_scene_queue_index += 1
- return {'FINISHED'}
-
-
-class SceneQueueOpenResult(bpy.types.Operator):
- """Open the .blend snapshot for the selected queue item"""
- bl_idname = "stablegen.queue_open_result"
- bl_label = "Open"
- bl_description = "Open the .blend snapshot of the selected queue item"
-
- @classmethod
- def poll(cls, context):
- if sg_modal_active(context):
- return False
- wm = context.window_manager
- idx = wm.sg_scene_queue_index
- if idx < 0 or idx >= len(wm.sg_scene_queue):
- return False
- item = wm.sg_scene_queue[idx]
- return bool(item.blend_file) and os.path.isfile(item.blend_file)
-
- def execute(self, context):
- wm = context.window_manager
- item = wm.sg_scene_queue[wm.sg_scene_queue_index]
- filepath = item.blend_file
- try:
- bpy.ops.wm.open_mainfile(filepath=filepath)
- except Exception as e:
- self.report({'ERROR'}, f"Failed to open file: {e}")
- return {'CANCELLED'}
- return {'FINISHED'}
-
-
-class SceneQueueInvalidate(bpy.types.Operator):
- """Reset a processed queue item back to pending and clear its scene"""
- bl_idname = "stablegen.queue_invalidate"
- bl_label = "Reset to Pending"
- bl_description = (
- "Reset the selected item to unprocessed, "
- "removing generated meshes and cameras from its snapshot "
- "so it can be re-processed"
- )
- bl_options = {'REGISTER', 'UNDO'}
-
- @classmethod
- def poll(cls, context):
- wm = context.window_manager
- idx = wm.sg_scene_queue_index
- if _queue_processing:
- cls.poll_message_set("Queue is currently processing")
- return False
- if sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- if idx < 0 or idx >= len(wm.sg_scene_queue):
- cls.poll_message_set("No queue item selected")
- return False
- item = wm.sg_scene_queue[idx]
- if item.status not in ('done', 'error'):
- cls.poll_message_set("Only completed or failed items can be reset")
- return False
- if not item.blend_file or not os.path.isfile(item.blend_file):
- cls.poll_message_set("Snapshot .blend file not found")
- return False
- return True
-
- def invoke(self, context, event):
- wm = context.window_manager
- idx = wm.sg_scene_queue_index
- item = wm.sg_scene_queue[idx]
- display = item.label or item.scene_name
- return context.window_manager.invoke_confirm(
- self, event,
- message=f"Reset '{display}'?\nThis will remove generated meshes and cameras from its snapshot and mark it as unprocessed.",
- confirm_text="Reset",
- )
-
- def execute(self, context):
- wm = context.window_manager
- idx = wm.sg_scene_queue_index
- item = wm.sg_scene_queue[idx]
- blend_path = item.blend_file
- display = item.label or item.scene_name
-
- # Open the processed .blend, clean it, save, then re-open current
- current_file = bpy.data.filepath
-
- try:
- bpy.ops.wm.open_mainfile(filepath=blend_path)
- except Exception as e:
- self.report({'ERROR'}, f"Failed to open snapshot: {e}")
- return {'CANCELLED'}
-
- # Remove all mesh objects and cameras from the scene
- scene = bpy.context.scene
- to_remove = [obj for obj in scene.objects if obj.type in {'MESH', 'CAMERA', 'EMPTY'}]
- for obj in to_remove:
- bpy.data.objects.remove(obj, do_unlink=True)
-
- # Purge orphaned data
- for block_attr in ('meshes', 'cameras', 'materials', 'images'):
- collection = getattr(bpy.data, block_attr, None)
- if collection is None:
- continue
- orphans = [b for b in collection if b.users == 0]
- for orphan in orphans:
- collection.remove(orphan)
-
- # Save the cleaned .blend in place
- try:
- bpy.ops.wm.save_mainfile()
- print(f"[Queue] Invalidated and saved: {blend_path}")
- except Exception as e:
- print(f"[Queue] Save warning during invalidate: {e}")
-
- # Mark the item as pending directly in the queue JSON file.
- # After open_mainfile the in-memory queue collection is empty
- # (it's restored 0.3 s later by the deferred load handler), so
- # patching the JSON is the only reliable approach.
- try:
- from . import _sg_queue_filepath
- fp = _sg_queue_filepath()
- if os.path.isfile(fp):
- import json as _json
- with open(fp, 'r', encoding='utf-8') as f:
- data = _json.load(f)
- items_list = data.get("items", data) if isinstance(data, dict) else data
- if isinstance(items_list, list) and idx < len(items_list):
- items_list[idx]["status"] = "pending"
- items_list[idx]["retries"] = 0
- items_list[idx]["error_reason"] = ""
- with open(fp, 'w', encoding='utf-8') as f:
- _json.dump(data, f)
- print(f"[Queue] Marked item {idx} as pending in queue JSON")
- except Exception as e:
- print(f"[Queue] Warning: could not update queue JSON: {e}")
-
- # Re-open the original file (if we had one)
- if current_file and os.path.isfile(current_file):
- try:
- bpy.ops.wm.open_mainfile(filepath=current_file)
- except Exception:
- pass
-
- self.report({'INFO'}, f"Reset '{display}' — ready for re-processing")
- return {'FINISHED'}
-
-
-class SceneQueueProcess(bpy.types.Operator):
- """Start or cancel batch processing of the scene queue"""
- bl_idname = "stablegen.queue_process"
- bl_label = "Process Queue"
- bl_description = "Process all queued scenes sequentially"
-
- @classmethod
- def poll(cls, context):
- wm = context.window_manager
- if _queue_processing:
- return True # Allow clicking to cancel
- if len(wm.sg_scene_queue) == 0:
- cls.poll_message_set("Queue is empty — add scenes first")
- return False
- if sg_modal_active(context):
- cls.poll_message_set("Another operation is in progress")
- return False
- return True
-
- def execute(self, context):
- global _queue_processing, _queue_timer, _queue_current_idx, _queue_phase
-
- if _queue_processing:
- # Cancel
- _queue_processing = False
- _queue_phase = 'idle'
- _persist_queue() # Save stopped state so load_post won't auto-resume
- print("[Queue] Cancelled by user.")
- self.report({'WARNING'}, "Queue processing cancelled.")
- return {'FINISHED'}
-
- # Reset statuses
- wm = context.window_manager
- for item in wm.sg_scene_queue:
- if item.status != 'done':
- item.status = 'pending'
-
- _queue_processing = True
- _queue_current_idx = 0
- _queue_phase = 'idle'
-
- # Find first pending item
- while _queue_current_idx < len(wm.sg_scene_queue):
- if wm.sg_scene_queue[_queue_current_idx].status == 'pending':
- break
- _queue_current_idx += 1
-
- if _queue_current_idx >= len(wm.sg_scene_queue):
- _queue_processing = False
- self.report({'INFO'}, "No pending items in queue.")
- return {'FINISHED'}
-
- # Start the timer-based queue driver
- _queue_timer = bpy.app.timers.register(_queue_tick, first_interval=0.5)
- self.report({'INFO'}, f"Queue processing started ({len(wm.sg_scene_queue)} items).")
- return {'FINISHED'}
-
-
-def _queue_tick():
- """Timer callback that drives queue advancement.
-
- Returns:
- float: Seconds until next tick, or None to stop.
- """
- import time as _time
- global _queue_processing, _queue_current_idx, _queue_phase, _queue_timer
- global _queue_settle_deadline, _queue_force_reload, _queue_texturing_seen
- global _queue_refresh_deadline
-
- if not _queue_processing:
- _queue_phase = 'idle'
- _queue_timer = None
- _tag_redraw()
- return None # Stop timer
-
- wm = bpy.context.window_manager
- queue = wm.sg_scene_queue
-
- if _queue_current_idx >= len(queue):
- # All done
- _queue_processing = False
- _queue_phase = 'idle'
- _queue_timer = None
- print("[Queue] All items processed.")
- _tag_redraw()
- return None
-
- item = queue[_queue_current_idx]
-
- # ── Phase: IDLE → switch to the next scene and start ──
- if _queue_phase == 'idle':
- # Verify the item belongs to the currently-open .blend
- current_blend = bpy.data.filepath or "<unsaved>"
- need_open = (item.blend_file and item.blend_file != current_blend)
-
- # On retry, always re-open the .blend to restore clean state
- is_retry_reload = False
- if _queue_force_reload and item.blend_file:
- need_open = True
- is_retry_reload = True
- _queue_force_reload = False
-
- if need_open:
- # Need to open a different .blend file
- target = item.blend_file
- if not os.path.isfile(target):
- print(f"[Queue] Blend file not found: {target} — skipping.")
- item.status = 'error'
- item.error_reason = "Blend file not found"
- _queue_current_idx += 1
- _persist_queue()
- _tag_redraw()
- return 0.5
-
- # Save current file first so nothing is lost — BUT skip save
- # on retry reloads; we want to discard dirty state and revert
- # to the original clean copy.
- if bpy.data.filepath and not is_retry_reload:
- try:
- bpy.ops.wm.save_mainfile()
- except Exception as e:
- print(f"[Queue] Warning: could not save current file: {e}")
-
- # Persist state so the load_post handler can resume after
- # the new file is opened.
- _persist_queue()
- print(f"[Queue] Opening '{os.path.basename(target)}' for item "
- f"'{item.scene_name}'...")
-
- # Open the target file. After this call the current WM
- # data (and our timer) are gone — the load_post handler
- # will restore the queue and call _resume_queue().
- try:
- bpy.ops.wm.open_mainfile(filepath=target)
- except Exception as e:
- print(f"[Queue] Failed to open '{target}': {e}")
- # Can't recover gracefully — stop processing
- _queue_processing = False
- _queue_phase = 'idle'
- _queue_timer = None
- _persist_queue()
- _tag_redraw()
- return None # Stop timer — load_post will restart it
-
- scene_ref = bpy.data.scenes.get(item.scene_name)
- if not scene_ref:
- print(f"[Queue] Scene '{item.scene_name}' not found — skipping.")
- item.status = 'error'
- item.error_reason = "Scene not found"
- _queue_current_idx += 1
- _tag_redraw()
- return 0.5 # Try next immediately
-
- # Switch to the target scene
- bpy.context.window.scene = scene_ref
- item.status = 'processing'
- _queue_phase = 'switching'
- print(f"[Queue] Switched to scene '{item.scene_name}'")
- _tag_redraw()
- return 1.0 # Give Blender a moment to digest
-
- # ── Phase: SWITCHING → invoke the generation operator ──
- if _queue_phase == 'switching':
- scene_ref = bpy.context.scene
- arch_mode = getattr(scene_ref, 'architecture_mode', 'sdxl')
-
- # Wait for any in-flight checkpoint / LoRA refreshes to finish so
- # ``model_name`` resolves to the correct value. The load_post
- # handler triggers an async refresh when the cached architecture
- # doesn't match the file that was just opened.
- from . import _pending_refreshes
- if _pending_refreshes > 0:
- if _queue_refresh_deadline == 0.0:
- _queue_refresh_deadline = _time.monotonic() + 15.0
- if _time.monotonic() < _queue_refresh_deadline:
- return 1.0 # Re-check on next tick
- else:
- print("[Queue] Model-list refresh timed out — proceeding anyway")
- _queue_refresh_deadline = 0.0 # Reset for next item
-
- # Clear the error flag before invoking
- scene_ref.sg_last_gen_error = False
-
- # Disable interactive elements for unattended batch
- if arch_mode == 'trellis2':
- scene_ref.trellis2_preview_gallery_enabled = False
-
- try:
- if arch_mode == 'trellis2':
- bpy.ops.object.trellis2_generate('INVOKE_DEFAULT')
- print(f"[Queue] Invoked trellis2_generate for '{item.scene_name}'")
- else:
- # Standard texturing — select all cameras first
- bpy.ops.object.select_all(action='DESELECT')
- for obj in scene_ref.objects:
- if obj.type == 'CAMERA':
- obj.select_set(True)
- bpy.ops.object.test_stable('INVOKE_DEFAULT')
- print(f"[Queue] Invoked test_stable for '{item.scene_name}'")
- _queue_phase = 'running'
- except Exception as e:
- print(f"[Queue] Failed to invoke operator for '{item.scene_name}': {e}")
- _queue_handle_failure(item, str(e))
- _tag_redraw()
- return 1.5
-
- # ── Phase: RUNNING → poll for completion ──
- if _queue_phase == 'running':
- from .generator import Trellis2Generate, ComfyUIGenerate
- scene_ref = bpy.context.scene
- arch_mode = getattr(scene_ref, 'architecture_mode', 'sdxl')
-
- if arch_mode == 'trellis2':
- trellis_running = Trellis2Generate._is_running
- gen_err = getattr(scene_ref, 'sg_last_gen_error', False)
- tex_mode = getattr(scene_ref, 'trellis2_texture_mode', 'native')
- pipe_active = getattr(scene_ref, 'trellis2_pipeline_active', False)
- gen_status = getattr(scene_ref, 'generation_status', 'idle')
- # TRELLIS.2: wait for the operator to finish
- if trellis_running:
- return 1.0 # Still running
-
- # Check if Trellis2 itself failed
- if gen_err:
- print(f"[Queue] TRELLIS.2 generation failed for '{item.scene_name}'")
- _queue_handle_failure(item, "TRELLIS.2 generation failed")
- return 0.5
-
- # Trellis2Generate finished — enter settling phase to wait for
- # _schedule_texture_generation / _deferred_generate to fire.
- if tex_mode in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein'):
- _queue_phase = 'settling'
- _queue_settle_deadline = _time.monotonic() + 15.0 # generous: server may need VRAM flush
- _queue_texturing_seen = False
- print(f"[Queue] TRELLIS.2 mesh done, entering settling (tex_mode='{tex_mode}'), "
- f"waiting for texturing to start (up to 15 s)...")
- else:
- # Native texture mode — mesh only, done
- print(f"[Queue] tex_mode='{tex_mode}' — no diffusion texturing, finishing item")
- _queue_finish_item(item)
- return 1.0
- else:
- # Standard texturing
- gen_status = getattr(scene_ref, 'generation_status', 'idle')
- if gen_status in ('idle', 'waiting'):
- if getattr(scene_ref, 'sg_last_gen_error', False):
- _queue_handle_failure(item, "Generation failed")
- return 0.5
- _queue_finish_item(item)
- return 0.5
- return 1.0 # Still running
-
- # ── Phase: SETTLING → wait for TRELLIS.2 chained texturing to start ──
- if _queue_phase == 'settling':
- scene_ref = bpy.context.scene
- pipeline_active = getattr(scene_ref, 'trellis2_pipeline_active', False)
- gen_status = getattr(scene_ref, 'generation_status', 'idle')
- if pipeline_active or gen_status == 'running':
- _queue_texturing_seen = True
-
- if _queue_texturing_seen:
- if gen_status == 'running' or pipeline_active:
- # Texturing is actively running — transition
- _queue_phase = 'waiting_texturing'
- print(f"[Queue] Texturing confirmed running, waiting for completion...")
- return 1.0
- else:
- # Texturing already completed (fast finish during settling)
- if getattr(scene_ref, 'sg_last_gen_error', False):
- _queue_handle_failure(item, "Texturing failed")
- else:
- _queue_finish_item(item)
- return 0.5
-
- # Texturing hasn't appeared yet — wait until deadline
- if _time.monotonic() >= _queue_settle_deadline:
- print(f"[Queue] Texturing never started for '{item.scene_name}' (timeout)")
- _queue_handle_failure(item, "Diffusion texturing failed to start")
- return 0.5
-
- return 1.0 # Still settling
-
- # ── Phase: WAITING_TEXTURING → poll chained ComfyUIGenerate ──
- if _queue_phase == 'waiting_texturing':
- scene_ref = bpy.context.scene
- gen_status = getattr(scene_ref, 'generation_status', 'idle')
- pipeline_active = getattr(scene_ref, 'trellis2_pipeline_active', False)
- gen_err = getattr(scene_ref, 'sg_last_gen_error', False)
-
- if gen_status in ('idle', 'waiting') and not pipeline_active:
- if getattr(scene_ref, 'sg_last_gen_error', False):
- _queue_handle_failure(item, "Texturing failed")
- return 0.5
- _queue_finish_item(item)
- return 0.5
- return 1.0 # Still running
-
- # ── Phase: EXPORTING_GIF → poll ExportOrbitGIF completion ──
- if _queue_phase == 'exporting_gif':
- from .render_tools import ExportOrbitGIF
- if ExportOrbitGIF._rendering:
- return 1.0 # Still rendering frames
- # First GIF export finished
- print("[Queue] GIF export completed")
-
- # Check whether a second (no-PBR) export is needed
- wm = bpy.context.window_manager
- scene_ref = bpy.context.scene
- if (getattr(wm, 'sg_queue_gif_also_no_pbr', False)
- and getattr(scene_ref, 'pbr_decomposition', False)):
- # Start the no-PBR reproject → second GIF pipeline
- if _queue_start_no_pbr_reproject():
- _queue_phase = 'reprojecting_no_pbr'
- print("[Queue] Starting no-PBR reproject for second GIF")
- _tag_redraw()
- return 1.0
- else:
- print("[Queue] No-PBR reproject failed to start — skipping second GIF")
-
- _queue_save_and_advance(item)
- return 0.5
-
- # ── Phase: REPROJECTING_NO_PBR → wait for ComfyUIGenerate to finish ──
- if _queue_phase == 'reprojecting_no_pbr':
- from .generator import ComfyUIGenerate
- if ComfyUIGenerate._is_running:
- return 1.0
- # Reproject done — start second GIF with 1 sample + suffix
- print("[Queue] No-PBR reproject finished — starting second GIF export")
- if _queue_start_gif_export(samples_override=1, suffix='_no_pbr'):
- _queue_phase = 'exporting_gif_no_pbr'
- _tag_redraw()
- return 1.0
- else:
- print("[Queue] Second GIF export failed to start — restoring PBR")
- _queue_restore_pbr()
- _queue_save_and_advance(item)
- return 0.5
-
- # ── Phase: EXPORTING_GIF_NO_PBR → poll second GIF completion ──
- if _queue_phase == 'exporting_gif_no_pbr':
- from .render_tools import ExportOrbitGIF
- if ExportOrbitGIF._rendering:
- return 1.0
- print("[Queue] Second (no-PBR) GIF export completed")
- _queue_restore_pbr()
- _queue_save_and_advance(item)
- return 0.5
-
- return 1.0
-
-
-def _queue_handle_failure(item, reason):
- """Handle a failed queue item — retry if under the limit, else mark as error."""
- global _queue_current_idx, _queue_phase, _queue_processing, _queue_timer
- global _queue_force_reload
-
- item.retries += 1
- if item.retries < _QUEUE_MAX_RETRIES:
- item.status = 'pending'
- _queue_phase = 'idle' # Will re-attempt on next tick
- _queue_force_reload = True # Force re-open to restore clean .blend state
- print(f"[Queue] Retrying '{item.label or item.scene_name}' "
- f"(attempt {item.retries + 1}/{_QUEUE_MAX_RETRIES}): {reason}")
- else:
- item.status = 'error'
- item.error_reason = reason
- print(f"[Queue] '{item.label or item.scene_name}' failed after "
- f"{_QUEUE_MAX_RETRIES} attempts: {reason}")
- _queue_current_idx += 1
- # Find next pending
- wm = bpy.context.window_manager
- while _queue_current_idx < len(wm.sg_scene_queue):
- if wm.sg_scene_queue[_queue_current_idx].status == 'pending':
- break
- _queue_current_idx += 1
- if _queue_current_idx >= len(wm.sg_scene_queue):
- _queue_processing = False
- _queue_phase = 'idle'
- _queue_timer = None
- print("[Queue] All queue items processed.")
- else:
- _queue_phase = 'idle'
- _persist_queue()
- _tag_redraw()
-
-
-def _queue_finish_item(item):
- """Mark the current queue item as done and advance to the next.
-
- If GIF export is enabled, starts the orbit GIF export first and
- defers the save+advance to the ``exporting_gif`` phase handler.
- """
- global _queue_current_idx, _queue_phase, _queue_processing, _queue_timer
-
- item.status = 'done'
- print(f"[Queue] Finished '{item.scene_name}'")
-
- # ── Optional GIF export before saving ──
- wm = bpy.context.window_manager
- if getattr(wm, 'sg_queue_gif_export', False):
- if _queue_start_gif_export():
- _queue_phase = 'exporting_gif'
- print("[Queue] GIF export started — deferring save until export finishes")
- _tag_redraw()
- return # save + advance will happen in the 'exporting_gif' handler
-
- # No GIF (or export failed to start) — save and advance immediately
- _queue_save_and_advance(item)
-
-
-def _queue_start_gif_export(samples_override=None, suffix=''):
- """Try to invoke ExportOrbitGIF with current WM settings.
-
- Args:
- samples_override: If set, use this sample count instead of the WM setting.
- suffix: Appended to orbit filenames (e.g. '_no_pbr' → orbit_no_pbr.gif).
-
- Returns True if the operator was successfully started.
- """
- from .render_tools import ExportOrbitGIF
-
- scene = bpy.context.scene
-
- # ── Always use the first camera (Camera_0 or lowest-numbered) ──
- cameras = sorted(
- [o for o in scene.objects if o.type == 'CAMERA'],
- key=lambda c: c.name,
- )
- if not cameras:
- print("[Queue] No cameras in scene — skipping GIF export")
- return False
- scene.camera = cameras[0]
- print(f"[Queue] GIF export: using camera '{cameras[0].name}'")
-
- # ── Ensure an active mesh/empty exists (poll requirement) ──
- vl = bpy.context.view_layer
- active = vl.objects.active
- if not active or active.type not in {'MESH', 'EMPTY'}:
- for obj in scene.objects:
- if obj.type == 'MESH':
- vl.objects.active = obj
- obj.select_set(True)
- print(f"[Queue] GIF export: activated mesh '{obj.name}'")
- break
- else:
- print("[Queue] No mesh in scene — skipping GIF export")
- return False
-
- wm = bpy.context.window_manager
-
- # ── Explicitly apply HDRI from the queue's file path (if set) ──
- hdri_path = getattr(wm, 'sg_queue_gif_hdri_path', '')
- hdri_strength = getattr(wm, 'sg_queue_gif_hdri_strength', 1.0)
- use_hdri = getattr(wm, 'sg_queue_gif_use_hdri', False)
-
- if use_hdri and hdri_path:
- import os as _os
- resolved = bpy.path.abspath(hdri_path)
- if _os.path.isfile(resolved):
- try:
- bpy.ops.object.add_hdri(hdri_path=resolved,
- strength=hdri_strength)
- print(f"[Queue] Applied HDRI: {resolved} "
- f"(strength={hdri_strength:.2f})")
- except Exception as e:
- print(f"[Queue] AddHDRI failed: {e} — continuing anyway")
- else:
- print(f"[Queue] HDRI file not found: {resolved} — skipping HDRI")
-
- kwargs = {
- 'duration': getattr(wm, 'sg_queue_gif_duration', 5.0),
- 'frame_rate': getattr(wm, 'sg_queue_gif_fps', 24),
- 'resolution_percentage': getattr(wm, 'sg_queue_gif_resolution', 50),
- 'samples': samples_override if samples_override is not None else getattr(wm, 'sg_queue_gif_samples', 32),
- 'engine': getattr(wm, 'sg_queue_gif_engine', 'CYCLES'),
- 'interpolation': getattr(wm, 'sg_queue_gif_interpolation', 'LINEAR'),
- 'use_hdri': use_hdri,
- 'hdri_rotation': getattr(wm, 'sg_queue_gif_hdri_rotation', 0.0),
- 'env_mode': getattr(wm, 'sg_queue_gif_env_mode', 'FIXED'),
- 'use_denoiser': getattr(wm, 'sg_queue_gif_denoiser', True),
- 'use_gpu': getattr(wm, 'sg_queue_gif_use_gpu', True),
- 'filename_suffix': suffix,
- }
-
- # Build a temp_override so the modal operator has a proper window
- window = bpy.context.window
- if not window:
- for w in wm.windows:
- window = w
- break
- if not window:
- print("[Queue] No window available — skipping GIF export")
- return False
-
- try:
- with bpy.context.temp_override(window=window):
- result = bpy.ops.object.export_orbit_gif('EXEC_DEFAULT', **kwargs)
- if 'RUNNING_MODAL' in result:
- print(f"[Queue] GIF export started successfully")
- return True
- print(f"[Queue] GIF export returned {result} — skipping")
- return False
- except Exception as e:
- print(f"[Queue] GIF export failed to start: {e}")
- import traceback; traceback.print_exc()
- return False
-
-
-def _queue_start_no_pbr_reproject():
- """Disable PBR, then invoke a project-only reproject.
-
- Saves the original PBR / generation settings so they can be restored
- by ``_queue_restore_pbr()`` after the second GIF is done.
-
- Returns True if the reproject operator was successfully started.
- """
- global _queue_original_pbr, _queue_original_gen_method, _queue_original_overwrite
-
- scene = bpy.context.scene
- _queue_original_pbr = scene.pbr_decomposition
- _queue_original_gen_method = scene.generation_method
- _queue_original_overwrite = scene.overwrite_material
-
- scene.pbr_decomposition = False
- scene.generation_mode = 'project_only'
- scene.generation_method = 'separate'
- scene.overwrite_material = True
-
- # Select all cameras (required by ComfyUIGenerate)
- bpy.ops.object.select_all(action='DESELECT')
- for obj in scene.objects:
- if obj.type == 'CAMERA':
- obj.select_set(True)
-
- window = bpy.context.window
- wm = bpy.context.window_manager
- if not window:
- for w in wm.windows:
- window = w
- break
- if not window:
- print("[Queue] No window available — cannot start reproject")
- return False
-
- try:
- with bpy.context.temp_override(window=window):
- bpy.ops.object.test_stable('INVOKE_DEFAULT')
- print("[Queue] No-PBR reproject invoked")
- return True
- except Exception as e:
- print(f"[Queue] Failed to invoke reproject: {e}")
- import traceback; traceback.print_exc()
- # Restore settings on failure
- scene.pbr_decomposition = _queue_original_pbr
- scene.generation_mode = 'standard'
- scene.generation_method = _queue_original_gen_method
- scene.overwrite_material = _queue_original_overwrite
- return False
-
-
-def _queue_restore_pbr():
- """Restore PBR and generation settings saved by ``_queue_start_no_pbr_reproject``."""
- scene = bpy.context.scene
- scene.pbr_decomposition = _queue_original_pbr
- scene.generation_mode = 'standard'
- scene.generation_method = _queue_original_gen_method
- scene.overwrite_material = _queue_original_overwrite
- print(f"[Queue] Restored pbr_decomposition={_queue_original_pbr}")
-
-
-def _queue_save_and_advance(item):
- """Save the current .blend and advance to the next queue item."""
- global _queue_current_idx, _queue_phase, _queue_processing, _queue_timer
-
- # Save the result in-place on the queue copy .blend
- if bpy.data.filepath:
- try:
- bpy.ops.wm.save_mainfile()
- print(f"[Queue] Saved result: {bpy.data.filepath}")
- except Exception as e:
- print(f"[Queue] Save warning: {e}")
-
- # Advance to next pending item
- _queue_current_idx += 1
- wm = bpy.context.window_manager
- while _queue_current_idx < len(wm.sg_scene_queue):
- if wm.sg_scene_queue[_queue_current_idx].status == 'pending':
- break
- _queue_current_idx += 1
-
- if _queue_current_idx >= len(wm.sg_scene_queue):
- _queue_processing = False
- _queue_phase = 'idle'
- _queue_timer = None
- print("[Queue] All queue items processed.")
- else:
- _queue_phase = 'idle' # Will switch to next scene on next tick
-
- _persist_queue()
- _tag_redraw()
-
-
-def _tag_redraw():
- """Redraw all 3D viewports so the queue UI updates."""
- try:
- for window in bpy.context.window_manager.windows:
- for area in window.screen.areas:
- area.tag_redraw()
- except Exception:
- pass
diff --git a/stablegen/texturing/__init__.py b/stablegen/texturing/__init__.py
new file mode 100644
index 0000000..0b7aa0a
--- /dev/null
+++ b/stablegen/texturing/__init__.py
@@ -0,0 +1,58 @@
+"""texturing – image-to-texture pipeline: exports, masks, baking, orbit GIF."""
+
+# -- rendering.py ------------------------------------------------------------
+from .rendering import ( # noqa: F401
+ purge_orphans,
+ apply_vignette_to_mask,
+ create_edge_feathered_mask,
+ render_edge_feather_mask,
+ apply_uv_inpaint_texture,
+ flatten_projection_material_for_refine,
+ export_emit_image,
+ export_render,
+ export_viewport,
+ export_canny,
+ expand_mask_to_blocks,
+ export_visibility,
+ SwitchMaterial,
+ prepare_baking,
+ unwrap,
+ bake_texture,
+ bake_pbr_channel,
+ BakeTextures,
+)
+
+# -- orbit_export.py ---------------------------------------------------------
+from .orbit_export import ExportOrbitGIF # noqa: F401
+
+# -- game_export.py ----------------------------------------------------------
+from .game_export import ExportForGameEngine # noqa: F401
+
+# -- generator.py ------------------------------------------------------------
+from .generator import ( # noqa: F401
+ ComfyUIGenerate,
+ Regenerate,
+ Reproject,
+ MirrorReproject,
+)
+
+# -- pbr.py ------------------------------------------------------------------
+from .pbr import _PBRMixin # noqa: F401
+
+# -- gallery.py --------------------------------------------------------------
+from .gallery import _PreviewGalleryOverlay # noqa: F401
+
+# -- projection.py -----------------------------------------------------------
+from .projection import ( # noqa: F401
+ _SG_BUFFER_UV_NAME,
+ _copy_uv_to_attribute,
+ project_image,
+ get_or_create_osl_text,
+ get_or_load_image,
+ reinstate_compare_nodes,
+ create_native_raycast_visibility,
+ create_native_feather,
+)
+
+# -- pbr_projection.py -------------------------------------------------------
+from .pbr_projection import project_pbr_to_bsdf # noqa: F401
diff --git a/stablegen/texturing/gallery.py b/stablegen/texturing/gallery.py
new file mode 100644
index 0000000..f92a3d9
--- /dev/null
+++ b/stablegen/texturing/gallery.py
@@ -0,0 +1,299 @@
+"""Preview gallery overlay for generated image selection."""
+
+import math
+import bpy # pylint: disable=import-error
+import blf # pylint: disable=import-error
+import gpu # pylint: disable=import-error
+from gpu_extras.batch import batch_for_shader # pylint: disable=import-error
+import numpy as np
+from PIL import Image
+import io
+
+def _draw_rect_2d(x1, y1, x2, y2, color):
+ """Draw a filled rectangle in 2D screen-space."""
+ shader = gpu.shader.from_builtin('UNIFORM_COLOR')
+ batch = batch_for_shader(
+ shader, 'TRIS',
+ {"pos": ((x1, y1), (x2, y1), (x2, y2), (x1, y2))},
+ indices=((0, 1, 2), (0, 2, 3)),
+ )
+ shader.bind()
+ shader.uniform_float("color", color)
+ gpu.state.blend_set('ALPHA')
+ batch.draw(shader)
+ gpu.state.blend_set('NONE')
+
+
+def _draw_texture_2d(texture, x1, y1, x2, y2):
+ """Draw a textured rectangle in 2D screen-space."""
+ shader = gpu.shader.from_builtin('IMAGE')
+ batch = batch_for_shader(
+ shader, 'TRIS',
+ {"pos": ((x1, y1), (x2, y1), (x2, y2), (x1, y2)),
+ "texCoord": ((0, 0), (1, 0), (1, 1), (0, 1))},
+ indices=((0, 1, 2), (0, 2, 3)),
+ )
+ shader.bind()
+ shader.uniform_sampler("image", texture)
+ gpu.state.blend_set('ALPHA')
+ batch.draw(shader)
+ gpu.state.blend_set('NONE')
+
+
+class _PreviewGalleryOverlay:
+ """Viewport overlay that shows N generated images and lets the user pick one.
+
+ Lifecycle:
+ 1. ``__init__`` — creates bpy.data.images + GPU textures, registers draw handler.
+ 2. ``handle_mouse_move`` / ``handle_click`` — called from Trellis2Generate.modal().
+ 3. ``update_images`` — called when "Generate More" produces a new batch.
+ 4. ``cleanup`` — removes draw handler and temp images.
+ """
+
+ def __init__(self, pil_images, seeds):
+ """*pil_images*: list[PIL.Image] *seeds*: list[int]"""
+ self._pil_images = list(pil_images)
+ self._seeds = list(seeds)
+ self._n = len(pil_images)
+ self._hover_idx = -1
+ self._more_hover = False
+ self._cancel_hover = False
+ self._selected_idx = -1
+ self.action = None # 'select' | 'more' | 'cancel'
+ self._cols = max(1, math.ceil(math.sqrt(self._n)))
+ self._rows = max(1, math.ceil(self._n / self._cols))
+ self._cell_rects: list[tuple] = []
+ self._more_rect: tuple | None = None
+ self._cancel_rect: tuple | None = None
+ self._bpy_images: list = []
+ self._textures: list = []
+ self._setup_textures()
+ self._draw_handle = bpy.types.SpaceView3D.draw_handler_add(
+ self._draw, (), 'WINDOW', 'POST_PIXEL')
+
+ # ── texture management ──
+
+ def _setup_textures(self):
+ self._clear_textures()
+ for i, pil_img in enumerate(self._pil_images):
+ name = f"_sg_gallery_{i}"
+ if name in bpy.data.images:
+ bpy.data.images.remove(bpy.data.images[name])
+ rgba = pil_img.convert('RGBA')
+ w, h = rgba.size
+ bpy_img = bpy.data.images.new(name, w, h, alpha=True)
+ # Mark as Non-Color so gpu.texture.from_image() stores raw
+ # sRGB values — the viewport's own output transform will
+ # apply the single correct sRGB curve on display.
+ bpy_img.colorspace_settings.name = 'Non-Color'
+ # Blender images are stored bottom-to-top; PIL is top-to-bottom
+ flipped = rgba.transpose(Image.FLIP_TOP_BOTTOM)
+ flat = np.array(flipped, dtype=np.float32).ravel() / 255.0
+ bpy_img.pixels.foreach_set(flat)
+ bpy_img.pack()
+ self._bpy_images.append(bpy_img)
+ self._textures.append(gpu.texture.from_image(bpy_img))
+
+ def _clear_textures(self):
+ self._textures.clear()
+ for img in self._bpy_images:
+ if img.name in bpy.data.images:
+ bpy.data.images.remove(img)
+ self._bpy_images.clear()
+
+ def update_images(self, pil_images, seeds):
+ """Replace the gallery with a new batch."""
+ self._pil_images = list(pil_images)
+ self._seeds = list(seeds)
+ self._n = len(pil_images)
+ self._cols = max(1, math.ceil(math.sqrt(self._n)))
+ self._rows = max(1, math.ceil(self._n / self._cols))
+ self._hover_idx = -1
+ self._more_hover = False
+ self._cancel_hover = False
+ self._selected_idx = -1
+ self.action = None
+ self._setup_textures()
+
+ def cleanup(self):
+ if self._draw_handle:
+ bpy.types.SpaceView3D.draw_handler_remove(self._draw_handle, 'WINDOW')
+ self._draw_handle = None
+ self._clear_textures()
+
+ # ── interaction ──
+
+ def handle_mouse_move(self, mx, my):
+ """Update hover state. Returns True if display should redraw."""
+ old = (self._hover_idx, self._more_hover, self._cancel_hover)
+ self._hover_idx = -1
+ self._more_hover = False
+ self._cancel_hover = False
+ for i, rect in enumerate(self._cell_rects):
+ x1, y1, x2, y2 = rect
+ if x1 <= mx <= x2 and y1 <= my <= y2:
+ self._hover_idx = i
+ break
+ if self._more_rect:
+ x1, y1, x2, y2 = self._more_rect
+ if x1 <= mx <= x2 and y1 <= my <= y2:
+ self._more_hover = True
+ if self._cancel_rect:
+ x1, y1, x2, y2 = self._cancel_rect
+ if x1 <= mx <= x2 and y1 <= my <= y2:
+ self._cancel_hover = True
+ return old != (self._hover_idx, self._more_hover, self._cancel_hover)
+
+ def handle_click(self, mx, my):
+ """Returns 'select', 'more', 'cancel', or None."""
+ if self._more_hover:
+ return 'more'
+ if self._cancel_hover:
+ return 'cancel'
+ if self._hover_idx >= 0:
+ self._selected_idx = self._hover_idx
+ return 'select'
+ return None
+
+ @property
+ def selected_seed(self):
+ if 0 <= self._selected_idx < len(self._seeds):
+ return self._seeds[self._selected_idx]
+ return None
+
+ @property
+ def selected_image_bytes(self):
+ """Return PNG bytes of the selected image (for TRELLIS workflow)."""
+ if 0 <= self._selected_idx < len(self._pil_images):
+ buf = io.BytesIO()
+ self._pil_images[self._selected_idx].save(buf, format='PNG')
+ return buf.getvalue()
+ return None
+
+ # ── drawing ──
+
+ def _draw(self):
+ """POST_PIXEL callback — draws the full-screen gallery overlay."""
+ context = bpy.context
+ region = context.region
+ if not region:
+ return
+ vw, vh = region.width, region.height
+ if vw < 100 or vh < 100:
+ return
+
+ # Detect sidebar (N-panel) width so the grid avoids being covered.
+ sidebar_w = 0
+ area = context.area
+ if area:
+ for r in area.regions:
+ if r.type == 'UI' and r.width > 1:
+ sidebar_w = r.width
+ break
+
+ # Layout constants
+ pad = 20
+ btn_h = 40
+ title_h = 30
+ bottom_area = btn_h + pad * 2
+ usable_w = vw - sidebar_w # exclude area behind the N-panel
+
+ # --- dark backdrop ---
+ _draw_rect_2d(0, 0, vw, vh, (0.08, 0.08, 0.08, 0.90))
+
+ # --- title ---
+ blf.size(0, 20)
+ title = "Select an image — or generate more"
+ tw, th = blf.dimensions(0, title)
+ blf.position(0, (usable_w - tw) / 2, vh - pad - th, 0)
+ blf.color(0, 1.0, 1.0, 1.0, 1.0)
+ blf.draw(0, title)
+
+ grid_top = vh - pad - title_h - pad
+ available_w = usable_w - pad * 2
+ available_h = grid_top - bottom_area - pad
+
+ cell_w = max(1, available_w // self._cols)
+ cell_h = max(1, available_h // self._rows)
+
+ # Maintain image aspect ratio
+ if self._pil_images:
+ img_aspect = self._pil_images[0].width / max(1, self._pil_images[0].height)
+ desired_h = cell_w / img_aspect
+ if desired_h > cell_h:
+ cell_w = int(cell_h * img_aspect)
+ else:
+ cell_h = int(desired_h)
+
+ inner = 6
+ total_grid_w = self._cols * cell_w
+ offset_x = (usable_w - total_grid_w) / 2.0
+
+ self._cell_rects.clear()
+
+ for i in range(self._n):
+ row = i // self._cols
+ col = i % self._cols
+ x1 = offset_x + col * cell_w + inner
+ y2 = grid_top - row * cell_h - inner
+ x2 = x1 + cell_w - inner * 2
+ y1 = y2 - cell_h + inner * 2
+ self._cell_rects.append((x1, y1, x2, y2))
+
+ # Hover highlight ring
+ if i == self._hover_idx:
+ _draw_rect_2d(x1 - 3, y1 - 3, x2 + 3, y2 + 3, (0.35, 0.65, 1.0, 0.85))
+
+ # Image texture
+ if i < len(self._textures):
+ _draw_texture_2d(self._textures[i], x1, y1, x2, y2)
+
+ # Number badge
+ blf.size(0, 15)
+ label = str(i + 1)
+ lw, lh = blf.dimensions(0, label)
+ _draw_rect_2d(x1, y2 - lh - 8, x1 + lw + 12, y2, (0.0, 0.0, 0.0, 0.70))
+ blf.position(0, x1 + 6, y2 - lh - 3, 0)
+ blf.color(0, 1.0, 1.0, 1.0, 1.0)
+ blf.draw(0, label)
+
+ # Seed label
+ blf.size(0, 11)
+ seed_txt = f"Seed: {self._seeds[i]}"
+ sw, sh = blf.dimensions(0, seed_txt)
+ _draw_rect_2d(x1, y1, x1 + sw + 10, y1 + sh + 6, (0, 0, 0, 0.6))
+ blf.position(0, x1 + 5, y1 + 3, 0)
+ blf.color(0, 0.7, 0.7, 0.7, 1.0)
+ blf.draw(0, seed_txt)
+
+ # --- buttons ---
+ btn_w = 160
+ btn_y1 = pad
+ btn_y2 = pad + btn_h
+
+ # "Generate More"
+ center_x = usable_w / 2.0
+ mx1 = center_x - btn_w - 10
+ mx2 = center_x - 10
+ c = (0.25, 0.55, 0.85, 0.95) if self._more_hover else (0.20, 0.35, 0.55, 0.85)
+ _draw_rect_2d(mx1, btn_y1, mx2, btn_y2, c)
+ self._more_rect = (mx1, btn_y1, mx2, btn_y2)
+ blf.size(0, 15)
+ mt = "Generate More"
+ mtw, mth = blf.dimensions(0, mt)
+ blf.position(0, (mx1 + mx2 - mtw) / 2, (btn_y1 + btn_y2 - mth) / 2, 0)
+ blf.color(0, 1.0, 1.0, 1.0, 1.0)
+ blf.draw(0, mt)
+
+ # "Cancel"
+ cx1 = center_x + 10
+ cx2 = center_x + btn_w + 10
+ c = (0.75, 0.25, 0.25, 0.95) if self._cancel_hover else (0.45, 0.20, 0.20, 0.85)
+ _draw_rect_2d(cx1, btn_y1, cx2, btn_y2, c)
+ self._cancel_rect = (cx1, btn_y1, cx2, btn_y2)
+ blf.size(0, 15)
+ ct = "Cancel"
+ ctw, cth = blf.dimensions(0, ct)
+ blf.position(0, (cx1 + cx2 - ctw) / 2, (btn_y1 + btn_y2 - cth) / 2, 0)
+ blf.color(0, 1.0, 1.0, 1.0, 1.0)
+ blf.draw(0, ct)
diff --git a/stablegen/texturing/game_export.py b/stablegen/texturing/game_export.py
new file mode 100644
index 0000000..28b0649
--- /dev/null
+++ b/stablegen/texturing/game_export.py
@@ -0,0 +1,162 @@
+"""Game engine export operator (glTF/FBX)."""
+
+import os
+import bpy # pylint: disable=import-error
+from ..utils import get_dir_path, sg_modal_active
+
+_ADDON_PKG = __package__.rsplit('.', 1)[0]
+
+class ExportForGameEngine(bpy.types.Operator):
+ """Export textured objects for game engines with PBR textures.
+
+ Wraps Blender's glTF / FBX exporters with game-engine-optimal settings
+ and auto-saves to the StableGen output directory."""
+ bl_idname = "object.export_game_engine"
+ bl_label = "Export for Game Engine"
+ bl_options = {'REGISTER'}
+
+ export_format: bpy.props.EnumProperty(
+ name="Format",
+ description="Export file format",
+ items=[
+ ('GLB', 'glTF Binary (.glb)',
+ 'Single-file binary glTF — best for web, Unity, Godot'),
+ ('GLTF_SEPARATE', 'glTF + Textures (.gltf)',
+ 'Separate .gltf, .bin and texture files'),
+ ('FBX', 'FBX (.fbx)',
+ 'Autodesk FBX with embedded textures — Unreal Engine'),
+ ],
+ default='GLB'
+ ) # type: ignore
+
+ export_scope: bpy.props.EnumProperty(
+ name="Objects",
+ description="Which objects to export",
+ items=[
+ ('SELECTED', 'Selected', 'Export selected mesh objects'),
+ ('ALL_MESH', 'All Mesh', 'Export all visible mesh objects'),
+ ],
+ default='SELECTED'
+ ) # type: ignore
+
+ apply_transforms: bpy.props.BoolProperty(
+ name="Apply Transforms",
+ description="Apply location, rotation and scale before export",
+ default=True
+ ) # type: ignore
+
+ export_animations: bpy.props.BoolProperty(
+ name="Export Animations",
+ description="Include animations in the export",
+ default=False
+ ) # type: ignore
+
+ @classmethod
+ def poll(cls, context):
+ addon_prefs = context.preferences.addons[_ADDON_PKG].preferences
+ if not os.path.exists(addon_prefs.output_dir):
+ cls.poll_message_set("Output directory not set or does not exist (check addon preferences)")
+ return False
+ if sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ return True
+
+ def draw(self, context):
+ layout = self.layout
+ layout.prop(self, "export_format")
+ layout.prop(self, "export_scope")
+ layout.prop(self, "apply_transforms")
+ layout.prop(self, "export_animations")
+
+ def invoke(self, context, event):
+ return context.window_manager.invoke_props_dialog(self)
+
+ def execute(self, context):
+ # ── Prepare selection ─────────────────────────────────────────
+ original_selection = list(context.selected_objects)
+ original_active = context.view_layer.objects.active
+
+ if self.export_scope == 'ALL_MESH':
+ bpy.ops.object.select_all(action='DESELECT')
+ meshes = [o for o in context.view_layer.objects
+ if o.type == 'MESH' and not o.hide_get()]
+ for o in meshes:
+ o.select_set(True)
+ if meshes:
+ context.view_layer.objects.active = meshes[0]
+ else:
+ meshes = [o for o in context.selected_objects
+ if o.type == 'MESH']
+ if not meshes:
+ self.report({'ERROR'}, "No mesh objects selected.")
+ return {'CANCELLED'}
+
+ # ── Output path ───────────────────────────────────────────────
+ output_base = get_dir_path(context, "baked")
+ os.makedirs(output_base, exist_ok=True)
+
+ # Build filename from first object name
+ base_name = meshes[0].name if meshes else "export"
+
+ if self.export_format == 'FBX':
+ ext = ".fbx"
+ elif self.export_format == 'GLTF_SEPARATE':
+ ext = ".gltf"
+ else:
+ ext = ".glb"
+ filepath = os.path.join(output_base, f"{base_name}{ext}")
+
+ # ── Apply transforms if requested ─────────────────────────────
+ if self.apply_transforms:
+ for o in meshes:
+ o.select_set(True)
+ bpy.ops.object.transform_apply(
+ location=True, rotation=True, scale=True)
+
+ # ── Export ────────────────────────────────────────────────────
+ try:
+ if self.export_format in ('GLB', 'GLTF_SEPARATE'):
+ bpy.ops.export_scene.gltf(
+ filepath=filepath,
+ export_format=self.export_format,
+ use_selection=True,
+ export_materials='EXPORT',
+ export_image_format='AUTO',
+ export_tangents=True,
+ export_yup=True,
+ export_apply=False,
+ export_animations=self.export_animations,
+ )
+ else: # FBX
+ bpy.ops.export_scene.fbx(
+ filepath=filepath,
+ use_selection=True,
+ apply_scale_options='FBX_SCALE_ALL',
+ path_mode='COPY',
+ embed_textures=True,
+ bake_anim=self.export_animations,
+ mesh_smooth_type='FACE',
+ )
+
+ self.report({'INFO'},
+ f"Exported to {filepath}")
+ except Exception as e:
+ self.report({'ERROR'}, f"Export failed: {e}")
+ import traceback
+ traceback.print_exc()
+
+ # ── Restore selection ─────────────────────────────────────────
+ bpy.ops.object.select_all(action='DESELECT')
+ for o in original_selection:
+ try:
+ o.select_set(True)
+ except ReferenceError:
+ pass
+ if original_active:
+ try:
+ context.view_layer.objects.active = original_active
+ except ReferenceError:
+ pass
+
+ return {'FINISHED'}
\ No newline at end of file
diff --git a/stablegen/texturing/generator.py b/stablegen/texturing/generator.py
new file mode 100644
index 0000000..c74a973
--- /dev/null
+++ b/stablegen/texturing/generator.py
@@ -0,0 +1,2071 @@
+"""ComfyUI texture generation operator (core pipeline)."""
+
+import os
+import bpy # pylint: disable=import-error
+import mathutils # pylint: disable=import-error
+import numpy as np
+import cv2
+
+import uuid
+import json
+import urllib.request
+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
+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 ..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
+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
+
+import websocket
+
+_ADDON_PKG = __package__.rsplit('.', 1)[0]
+
+class Regenerate(bpy.types.Operator):
+ """Regenerate textures for selected cameras / viewpoints
+ - Works for sequential and separate generation modes
+ - Generates new images for the selected cameras only, keeping existing images for unselected cameras
+ - This can be used with different prompts or settings to refine specific viewpoints without affecting others"""
+ bl_idname = "object.stablegen_regenerate"
+ bl_label = "Regenerate Selected Viewpoints"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ _original_method = None
+ _original_overwrite_material = None
+ _timer = None
+ _to_texture = None
+ @classmethod
+ def poll(cls, context):
+ addon_prefs = context.preferences.addons[_ADDON_PKG].preferences
+ if not os.path.exists(addon_prefs.output_dir):
+ cls.poll_message_set("Output directory not set or does not exist")
+ return False
+ if not addon_prefs.server_address or not addon_prefs.server_online:
+ cls.poll_message_set("ComfyUI server is not connected")
+ return False
+ if not (context.scene.generation_method == 'sequential' or context.scene.generation_method == 'separate'):
+ cls.poll_message_set("Regenerate is only available in Sequential or Separate mode")
+ return False
+ if context.scene.output_timestamp == "":
+ cls.poll_message_set("No previous generation to regenerate from")
+ return False
+ if context.scene.generation_status == 'waiting' or sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ return True
+
+ def execute(self, context):
+ """
+ Executes the operator.
+ :param context: Blender context.
+ :return: {'FINISHED'}
+ """
+
+ self._original_overwrite_material = context.scene.overwrite_material
+ # Set the flag to reproject
+ context.scene.generation_mode = 'regenerate_selected'
+ # Set the generation method to 'separate' to avoid generating new images
+ context.scene.overwrite_material = True
+ # Set timer to 1 seconds to give some time for the generate to start
+ context.window_manager.modal_handler_add(self)
+ self._timer = context.window_manager.event_timer_add(1.0, window=context.window)
+ # Revert to original discard angle in material nodes in case it was reset after generation
+ if context.scene.texture_objects == 'selected':
+ self._to_texture = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
+ # If empty, cancel the operation
+ if not self._to_texture:
+ self.report({'ERROR'}, "No mesh objects selected. Select mesh objects in the viewport before regenerating.")
+ context.scene.generation_status = 'idle'
+ ComfyUIGenerate._is_running = False
+ 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()]
+ # Revert discard angle
+ new_discard_angle = context.scene.discard_factor
+ 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
+ # Run the generation operator (bypass modal poll guard since we are
+ # already registered as a modal operator ourselves)
+ from .. import utils as _sg_utils
+ _sg_utils._sg_bypass_modal_check = True
+ try:
+ bpy.ops.object.test_stable('INVOKE_DEFAULT')
+ finally:
+ _sg_utils._sg_bypass_modal_check = False
+
+ # Switch to modal and wait for completion
+ print("[StableGen] Going modal")
+ return {'RUNNING_MODAL'}
+
+ def modal(self, context, event):
+ """
+ Handles modal events.
+ :param context: Blender context.
+ :param event: Blender event.
+ :return: {'PASS_THROUGH'}
+ """
+ if event.type == 'TIMER':
+ running = False
+ if ComfyUIGenerate._is_running:
+ running = True
+ if not running:
+ # Reset the generation method and overwrite material flag
+ context.scene.overwrite_material = self._original_overwrite_material
+ # Reset the project only flag
+ context.scene.generation_mode = 'standard'
+ # Remove the modal handler
+ context.window_manager.event_timer_remove(self._timer)
+ # Report completion
+ self.report({'INFO'}, "Regeneration complete.")
+ return {'FINISHED'}
+ return {'PASS_THROUGH'}
+
+class Reproject(bpy.types.Operator):
+ """Rerun projection of existing images
+ - Uses the Generate operator to reproject images, new textures will respect new Viewpoint Blending Settings
+ - Will not work with textures which used refine mode with the preserve parameter enabled"""
+ bl_idname = "object.stablegen_reproject"
+ bl_label = "Reproject Images"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ _original_method = None
+ _original_overwrite_material = None
+ _timer = None
+ @classmethod
+ def poll(cls, context):
+ if context.scene.output_timestamp == "":
+ cls.poll_message_set("No previous generation to reproject from")
+ return False
+ if context.scene.generation_status == 'waiting' or sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ return True
+
+ def execute(self, context):
+ """
+ Executes the operator.
+ :param context: Blender context.
+ :return: {'FINISHED'}
+ """
+ if context.scene.texture_objects == 'all':
+ to_texture = [obj for obj in bpy.context.view_layer.objects if obj.type == 'MESH' and not obj.hide_get()]
+ else: # selected
+ to_texture = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
+
+ # Search for largest material id
+ max_id = -1
+ for obj in to_texture:
+ mat_id = get_last_material_index(obj)
+ if mat_id > max_id:
+ max_id = mat_id
+
+ cameras = [obj for obj in bpy.context.scene.objects if obj.type == 'CAMERA']
+ for i, _ in enumerate(cameras):
+ # Check if the camera has a corresponding generated image
+ image_path = get_file_path(context, "generated", camera_id=i, material_id=max_id)
+ if not os.path.exists(image_path):
+ # Try to recover from a packed/embedded Blender image
+ # (e.g. after saving and reopening the .blend file when the
+ # original output directory no longer exists).
+ recovered = self._recover_image_from_blend(
+ image_path, to_texture, max_id)
+ if not recovered:
+ self.report(
+ {'ERROR'},
+ f"Camera {i} does not have a corresponding "
+ f"generated image.")
+ print(f"[StableGen] {image_path} does not exist")
+ return {'CANCELLED'}
+
+ self._original_method = context.scene.generation_method
+ self._original_overwrite_material = context.scene.overwrite_material
+ # Set the flag to reproject
+ context.scene.generation_mode = 'project_only'
+ # Set the generation method to 'separate' to avoid generating new images
+ context.scene.generation_method = 'separate'
+ context.scene.overwrite_material = True
+ # Set timer to 1 seconds to give some time for the generate to start
+ context.window_manager.modal_handler_add(self)
+ self._timer = context.window_manager.event_timer_add(1.0, window=context.window)
+ # Run the generation operator (bypass modal poll guard since we are
+ # already registered as a modal operator ourselves)
+ from .. import utils as _sg_utils
+ _sg_utils._sg_bypass_modal_check = True
+ try:
+ bpy.ops.object.test_stable('INVOKE_DEFAULT')
+ finally:
+ _sg_utils._sg_bypass_modal_check = False
+
+ # Switch to modal and wait for completion
+ print("[StableGen] Going modal")
+ return {'RUNNING_MODAL'}
+
+ @staticmethod
+ def _recover_image_from_blend(image_path, to_texture, material_id):
+ """Try to recover a generated image from bpy.data.images.
+
+ When a .blend file is saved after generation, the generated
+ textures are packed/embedded as Blender image data-blocks. If
+ the original output directory no longer exists (e.g. moved PC,
+ temp folder cleared), we can find the image in the material's
+ node tree and re-save it to the expected path.
+
+ Returns True if the file was successfully recovered.
+ """
+ target_name = os.path.basename(image_path)
+
+ def _try_save_image(img):
+ """Attempt to write an image data-block to *image_path*."""
+ os.makedirs(os.path.dirname(image_path), exist_ok=True)
+
+ # Case 1: packed file — write raw bytes
+ if img.packed_file:
+ try:
+ with open(image_path, 'wb') as f:
+ f.write(img.packed_file.data)
+ print(f"[StableGen] Recovered packed image: "
+ f"{target_name}")
+ return True
+ except Exception as err:
+ print(f"[StableGen] Packed write failed: {err}")
+
+ # Case 2: image has a valid filepath elsewhere — copy it
+ existing_path = bpy.path.abspath(img.filepath_raw)
+ if existing_path and os.path.isfile(existing_path):
+ try:
+ import shutil
+ shutil.copy2(existing_path, image_path)
+ print(f"[StableGen] Copied image from "
+ f"{existing_path} → {image_path}")
+ return True
+ except Exception as err:
+ print(f"[StableGen] File copy failed: {err}")
+
+ # Case 3: pixel data in memory — save via render
+ try:
+ if img.has_data or len(img.pixels) > 0:
+ img.save_render(image_path)
+ print(f"[StableGen] Recovered image via save_render: "
+ f"{target_name}")
+ return True
+ except Exception:
+ pass
+
+ return False
+
+ # Strategy 1: scan Image Texture nodes in the target materials
+ # (most reliable — images are directly referenced by the object
+ # being reprojected, so we won't accidentally pick up images
+ # from a different model/object that share the same filename).
+ for obj in to_texture:
+ if not hasattr(obj, 'data') or not obj.data.materials:
+ continue
+ for mat in obj.data.materials:
+ if not mat or not mat.use_nodes:
+ continue
+ for node in mat.node_tree.nodes:
+ if node.type == 'TEX_IMAGE' and node.image:
+ node_filename = os.path.basename(
+ node.image.filepath_raw)
+ if node_filename == target_name:
+ if _try_save_image(node.image):
+ return True
+
+ # Strategy 2: direct name lookup in bpy.data.images (fallback)
+ for img in bpy.data.images:
+ if img.name == target_name or os.path.basename(
+ img.filepath_raw) == target_name:
+ if _try_save_image(img):
+ return True
+
+ return False
+
+ def modal(self, context, event):
+ if event.type == 'TIMER':
+ running = False
+ if ComfyUIGenerate._is_running:
+ running = True
+ if not running:
+ # Reset the generation method and overwrite material flag
+ context.scene.generation_method = self._original_method
+ context.scene.overwrite_material = self._original_overwrite_material
+ # Reset the project only flag
+ context.scene.generation_mode = 'standard'
+ # Remove the modal handler
+ context.window_manager.event_timer_remove(self._timer)
+ # Report completion
+ self.report({'INFO'}, "Reprojection complete.")
+ return {'FINISHED'}
+ return {'PASS_THROUGH'}
+
+
+
+class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
+ """Generate textures using ComfyUI (to all mesh objects using all cameras in the scene)
+
+ - Multiple modes are available. Choose by setting Generation Mode in the UI.
+ - This includes texture generation and projection to the mesh objects.
+ - By default, the generated textures will only be visible in the Rendered viewport shading mode."""
+ bl_idname = "object.test_stable"
+ bl_label = "Generate using ComfyUI"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ _timer = None
+ _progress = 0
+ _error = None
+ _is_running = False
+ _active_ws = None # WebSocket reference for cancel-time close
+ _threads_left = 0
+ _cameras = None
+ _selected_camera_ids = None
+ _grid_width = 0
+ _grid_height = 0
+ _material_id = -1
+ _to_texture = None
+ _original_visibility = None
+ _generation_method_on_start = None
+ _uploaded_images_cache: dict = {}
+ workflow_manager: object = None
+
+ # Add properties to track progress
+ _progress = 0.0
+ _stage = ""
+ _current_image = 0
+ _total_images = 0
+ _wait_event = None
+ # PBR progress (model-level steps across all cameras)
+ _pbr_active = False
+ _pbr_step = 0
+ _pbr_total_steps = 0
+ _pbr_cam = 0 # current camera index within current step
+ _pbr_cam_total = 1 # total cameras in current step
+
+ # Add new properties at the top of the class
+ _object_prompts: dict = {}
+ show_prompt_dialog: bpy.props.BoolProperty(default=True)
+ current_object_name: bpy.props.StringProperty()
+ current_object_prompt: bpy.props.StringProperty(
+ name="Object Prompt",
+ description="Enter a specific prompt for this object",
+ default=""
+ ) # type: ignore
+ # New properties for prompt collection
+ _mesh_objects: list = []
+ mesh_index: int = 0
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._total_images = 0
+ self._current_image = 0
+ self._stage = ""
+ self._progress = 0
+ self._pbr_active = False
+ self._pbr_step = 0
+ self._pbr_total_steps = 0
+ self._pbr_cam = 0
+ self._pbr_cam_total = 1
+ self._wait_event = threading.Event()
+ from ..workflows import WorkflowManager
+ self.workflow_manager = WorkflowManager(self)
+
+ def _run_on_main_thread(self, func):
+ """Execute *func* on Blender's main thread via a timer callback and
+ block until it completes. Sets ``self._error`` on failure."""
+ def _callback():
+ try:
+ func()
+ except Exception as exc:
+ self._error = str(exc)
+ traceback.print_exc()
+ self._wait_event.set()
+ return None # one-shot timer
+ bpy.app.timers.register(_callback)
+ self._wait_event.wait()
+ self._wait_event.clear()
+
+ 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):
+ if cls._is_running:
+ return True # Allow cancellation
+ # Check for other modal operators
+ if context.scene.generation_status == 'waiting' or sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ addon_prefs = context.preferences.addons[_ADDON_PKG].preferences
+ if not os.path.exists(addon_prefs.output_dir):
+ cls.poll_message_set("Output directory not set or does not exist (check addon preferences)")
+ return False
+ if not addon_prefs.server_address or not addon_prefs.server_online:
+ cls.poll_message_set("ComfyUI server is not connected")
+ return False
+ if bpy.app.online_access == False:
+ cls.poll_message_set("Blender's online access is disabled (File → Preferences → System)")
+ return False
+ return True
+
+ def execute(self, context):
+ """
+ Executes the operator.
+ :param context: Blender context.
+ :return: {'RUNNING_MODAL'}
+ """
+ if ComfyUIGenerate._is_running:
+ self.cancel_generate(context)
+ return {'FINISHED'}
+
+ self._generation_method_on_start = context.scene.generation_method
+
+ # Clear the upload cache at the start of a new generation
+ self._uploaded_images_cache.clear()
+
+ # Timestamp for output directory
+ if context.scene.generation_mode == 'standard':
+ context.scene.output_timestamp = datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
+
+ # If UV inpainting and we're in prompt collection mode, collect prompts first.
+ if context.scene.generation_method == 'uv_inpaint' and self.show_prompt_dialog:
+ self._object_prompts[self.current_object_name] = self.current_object_prompt
+ if self.mesh_index < len(self._to_texture) - 1:
+ self.mesh_index += 1
+ self.current_object_name = self._to_texture[self.mesh_index]
+ self.current_object_prompt = ""
+ return context.window_manager.invoke_props_dialog(self, width=400)
+ else:
+ self.show_prompt_dialog = False
+
+
+ context.scene.generation_status = 'running'
+ context.scene.sg_last_gen_error = False
+ ComfyUIGenerate._is_running = True
+
+ print("[StableGen] Executing ComfyUI Generation")
+
+ if context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein') and not context.scene.generation_mode == 'project_only':
+ context.scene.generation_method = 'sequential' # Force sequential for edit models
+
+ render = bpy.context.scene.render
+
+ # Force 100% resolution scale so depth/guide map renders match the
+ # base resolution_x × resolution_y. The Qwen workflow derives its
+ # output size from the uploaded guidance map, so a non-100% percentage
+ # causes it to generate at the wrong resolution.
+ self._original_resolution_percentage = render.resolution_percentage
+ self._original_resolution_x = render.resolution_x
+ self._original_resolution_y = render.resolution_y
+ render.resolution_percentage = 100
+
+ resolution_x = render.resolution_x
+ resolution_y = render.resolution_y
+ total_pixels = resolution_x * resolution_y
+
+ # Qwen Image Edit benefits from 112-aligned resolution (LCM of VAE=8,
+ # ViT patch=14, spatial merge=2×14=28, ViT window=112) to avoid
+ # subtle pixel shifts between the latent, VAE and CLIP grids.
+ # FLUX.2 Klein uses 16x latent downscale, so 16-aligned is needed.
+ use_qwen_alignment = (
+ context.scene.model_architecture.startswith('qwen')
+ and getattr(context.scene, 'qwen_rescale_alignment', False)
+ )
+ if use_qwen_alignment:
+ align_step = 112
+ elif context.scene.model_architecture == 'flux2_klein':
+ align_step = 16
+ else:
+ align_step = 8
+
+ target_px = int(getattr(context.scene, 'auto_rescale_target_mp', 1.0) * 1_000_000)
+ upper_bound = int(target_px * 1.2)
+ lower_bound = int(target_px * 0.8)
+
+ if context.scene.auto_rescale and ((total_pixels > upper_bound or total_pixels < lower_bound) or (resolution_x % align_step != 0 or resolution_y % align_step != 0)):
+ scale_factor = (target_px / total_pixels) ** 0.5
+ render.resolution_x = int(resolution_x * scale_factor)
+ render.resolution_y = int(resolution_y * scale_factor)
+ # Round down to nearest multiple of align_step
+ render.resolution_x -= render.resolution_x % align_step
+ render.resolution_y -= render.resolution_y % align_step
+ self.report({'INFO'}, f"Resolution automatically rescaled to {render.resolution_x}x{render.resolution_y}.")
+
+ elif total_pixels > upper_bound:
+ target_mp = target_px / 1_000_000
+ self.report({'WARNING'}, f"High resolution detected. Resolutions above {target_mp:.1f} MP may reduce performance and quality.")
+
+ self._cameras = [obj for obj in bpy.context.scene.objects if obj.type == 'CAMERA']
+ if not self._cameras:
+ self.report({'ERROR'}, "No cameras found in the scene. Use 'Add Cameras' to create them.")
+ context.scene.generation_status = 'idle'
+ context.scene.sg_last_gen_error = True
+ ComfyUIGenerate._is_running = False
+ render.resolution_percentage = self._original_resolution_percentage
+ render.resolution_x = self._original_resolution_x
+ render.resolution_y = self._original_resolution_y
+ return {'CANCELLED'}
+ # Sort cameras by name
+ self._cameras.sort(key=lambda x: x.name)
+
+ # Apply custom generation order if enabled (non-destructive reorder)
+ if context.scene.sg_use_custom_camera_order and len(context.scene.sg_camera_order) > 0:
+ order_names = [item.name for item in context.scene.sg_camera_order]
+ cam_by_name = {cam.name: cam for cam in self._cameras}
+ ordered = []
+ for name in order_names:
+ if name in cam_by_name:
+ ordered.append(cam_by_name.pop(name))
+ # Append any cameras not in the order list (newly added cameras)
+ for cam in self._cameras:
+ if cam.name in cam_by_name:
+ ordered.append(cam)
+ self._cameras = ordered
+
+ # Hide crop and label overlays during generation
+ _sg_remove_crop_overlay()
+ _sg_hide_label_overlay()
+
+ # Auto-rescale per-camera resolutions (if any cameras have sg_res_x/y)
+ if context.scene.auto_rescale:
+ for cam in self._cameras:
+ if "sg_res_x" in cam and "sg_res_y" in cam:
+ crx, cry = int(cam["sg_res_x"]), int(cam["sg_res_y"])
+ c_total = crx * cry
+ if (c_total > upper_bound or c_total < lower_bound) or (crx % align_step != 0 or cry % align_step != 0):
+ sf = (target_px / c_total) ** 0.5
+ crx = int(crx * sf)
+ cry = int(cry * sf)
+ crx -= crx % align_step
+ cry -= cry % align_step
+ cam["sg_res_x"] = crx
+ cam["sg_res_y"] = cry
+ self._selected_camera_ids = [i for i, cam in enumerate(self._cameras) if cam in bpy.context.selected_objects] #TEST
+ if len(self._selected_camera_ids) == 0:
+ self._selected_camera_ids = list(range(len(self._cameras))) # All cameras selected if none are selected
+
+ # Check if there is at least one ControlNet unit
+ controlnet_units = getattr(context.scene, "controlnet_units", [])
+ if not controlnet_units and not (context.scene.use_flux_lora and context.scene.model_architecture == 'flux1') and context.scene.model_architecture != 'flux2_klein':
+ self.report({'ERROR'}, "At least one ControlNet unit is required. Add one in the Guidance section below.")
+ context.scene.generation_status = 'idle'
+ context.scene.sg_last_gen_error = True
+ ComfyUIGenerate._is_running = False
+ render.resolution_percentage = self._original_resolution_percentage
+ render.resolution_x = self._original_resolution_x
+ render.resolution_y = self._original_resolution_y
+ return {'CANCELLED'}
+
+ # If there are curves within the scene, warn the user
+ if any(obj.type == 'CURVE' for obj in bpy.context.view_layer.objects):
+ self.report({'WARNING'}, "Curves detected in the scene. Consider using 'Convert Curves to Mesh' in the Tools section.")
+
+ if context.scene.generation_mode == 'project_only':
+ print(f"[StableGen] Reprojecting images for {len(self._cameras)} cameras")
+ elif context.scene.generation_mode == 'standard':
+ print(f"[StableGen] Generating images for {len(self._cameras)} cameras")
+ else:
+ print(f"[StableGen] Regenerating images for {len(self._selected_camera_ids)} selected cameras")
+
+ if context.scene.texture_objects == 'selected':
+ self._to_texture = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
+ # If empty, cancel the operation
+ if not self._to_texture:
+ self.report({'ERROR'}, "No mesh objects selected for texturing.")
+ context.scene.generation_status = 'idle'
+ context.scene.sg_last_gen_error = True
+ ComfyUIGenerate._is_running = False
+ render.resolution_percentage = self._original_resolution_percentage
+ render.resolution_x = self._original_resolution_x
+ render.resolution_y = self._original_resolution_y
+ 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()]
+
+ # Find all mesh objects, check their material ids and store the highest one
+ for obj in self._to_texture:
+ for slot in obj.material_slots:
+ material_id = get_last_material_index(obj)
+ if (material_id > self._material_id):
+ self._material_id = material_id
+ # Check if there's room for the projection buffer UV map (only 1 slot needed)
+ # Projection UV data is stored as attributes (no slot limit), but we need
+ # 1 temporary buffer UV slot for the UV Project modifier
+ has_buffer = obj.data.uv_layers.get("_SG_ProjectionBuffer") is not None
+ if not has_buffer and len(obj.data.uv_layers) >= 8:
+ self.report({'ERROR'}, "Not enough UV map slots (max 8). Remove an existing UV map on the object to free a slot for the projection buffer.")
+ context.scene.generation_status = 'idle'
+ context.scene.sg_last_gen_error = True
+ ComfyUIGenerate._is_running = False
+ render.resolution_percentage = self._original_resolution_percentage
+ render.resolution_x = self._original_resolution_x
+ render.resolution_y = self._original_resolution_y
+ 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')):
+ self._material_id += 1
+
+ self._controlnet_units = list(controlnet_units)
+
+ # Prepare for generating
+ if context.scene.generation_method == 'grid':
+ self._threads_left = 1
+ if context.scene.generation_method == 'uv_inpaint':
+ self._threads_left = len(self._to_texture)
+ else:
+ self._threads_left = len(self._cameras)
+
+ self._original_visibility = {}
+ if context.scene.texture_objects == 'selected':
+ # Hide unselected objects for rendering
+ for obj in bpy.context.view_layer.objects:
+ if obj.type == 'MESH' and obj not in self._to_texture:
+ # Save original visibility
+ self._original_visibility[obj.name] = obj.hide_render
+ obj.hide_render = True
+
+
+ # UV inpainting mode preparation
+ if context.scene.generation_method == 'uv_inpaint':
+ # Check if there are baked textures for all objects
+
+ if self.show_prompt_dialog:
+ # Start the prompt collection process with the first object
+ if not self._object_prompts: # Only if prompts haven't been collected
+ self.current_object_name = self._to_texture[0].name
+ return context.window_manager.invoke_props_dialog(self, width=400)
+
+ # Continue with normal execution if all prompts are collected
+ for obj in self._to_texture:
+ # Use get_file_path to check for baked texture existence
+ baked_texture_path = get_file_path(context, "baked", object_name=obj.name)
+ if not os.path.exists(baked_texture_path):
+ # Bake the texture if it doesn't exist
+ self._stage = f"Baking UV Textures ({obj.name})"
+ prepare_baking(context)
+ unwrap(obj, method='pack', overlap_only=True)
+ bake_texture(context, obj, texture_resolution=2048, output_dir=get_dir_path(context, "baked"))
+
+ # Check if the material is compatible (uses projection shader)
+ active_material = obj.active_material
+ if not active_material or not active_material.use_nodes:
+ error = True
+ else:
+ # Check if the last node before the output is a color mix node or a bsdf shader node with a color mix node before it
+ output_node = None
+ for node in active_material.node_tree.nodes:
+ if node.type == 'OUTPUT_MATERIAL':
+ output_node = node
+ break
+ if not output_node:
+ error = True
+ else:
+ # Check if the last node before the output is a color mix node or a bsdf shader node with a color mix node before it
+ for link in output_node.inputs[0].links:
+ if link.from_node.type == 'MIX_RGB' or (link.from_node.type == 'BSDF_PRINCIPLED' and any(n.type == 'MIX_RGB' for n in link.from_node.inputs)):
+ error = False
+ break
+ else:
+ error = True
+ if error:
+ self.report({'ERROR'}, f"Cannot use UV inpainting with the material of object '{obj.name}'. The generated StableGen material must be the active material.")
+ context.scene.generation_status = 'idle'
+ ComfyUIGenerate._is_running = False
+ render.resolution_percentage = self._original_resolution_percentage
+ render.resolution_x = self._original_resolution_x
+ render.resolution_y = self._original_resolution_y
+ return {'CANCELLED'}
+
+ # Export visibility masks for each object
+ self._stage = f"Computing Visibility ({obj.name})"
+ export_visibility(context, None, obj)
+
+ if context.scene.view_blend_use_color_match and self._to_texture:
+ self._stage = "Matching Colors"
+ # Use the first target object as the reference for viewport color
+ ref_np = _get_viewport_ref_np(self._to_texture[0])
+ if ref_np is not None:
+ # Apply color match to ALL generated camera images for this material
+ for cam_idx, cam in enumerate(self._cameras):
+ image_path = get_file_path(
+ context,
+ "generated",
+ camera_id=cam_idx,
+ material_id=self._material_id,
+ )
+ _apply_color_match_to_file(
+ image_path=image_path,
+ ref_rgb=ref_np,
+ scene=context.scene,
+ )
+
+ self.prompt_text = context.scene.comfyui_prompt
+
+ self._progress = 0.0
+ if context.scene.generation_mode == 'project_only':
+ self._stage = "Reprojecting"
+ else:
+ self._stage = "Starting"
+ redraw_ui(context)
+ self._current_image = 0
+ self._total_images = len(self._cameras)
+ if context.scene.generation_method == 'grid':
+ self._total_images = 1
+ if context.scene.refine_images:
+ self._total_images += len(self._cameras) # Add refinement steps
+ elif context.scene.generation_method == 'uv_inpaint':
+ self._total_images = len(self._to_texture)
+
+ # Regenerate mode preparation
+ if context.scene.generation_mode == 'regenerate_selected':
+ if context.scene.generation_method == 'sequential':
+ # Sequential regeneration: reset all cameras from the first
+ # selected onward so the projection sequence replays correctly.
+ # Non-selected cameras reuse their existing images but still
+ # get reprojected, keeping subsequent cameras' context intact.
+ first_selected = min(self._selected_camera_ids)
+ ids = [(cid, self._material_id)
+ for cid in range(first_selected, len(self._cameras))]
+ reinstate_compare_nodes(context, self._to_texture, ids)
+ self._current_image = first_selected
+ self._threads_left = len(self._cameras) - first_selected
+ else:
+ # Non-sequential modes: only reset selected cameras
+ ids = [(cid, self._material_id)
+ for cid in self._selected_camera_ids]
+ reinstate_compare_nodes(context, self._to_texture, ids)
+
+ # Add modal timer
+ context.window_manager.modal_handler_add(self)
+ self._timer = context.window_manager.event_timer_add(0.5, window=context.window)
+ print("[StableGen] Starting thread")
+ if context.scene.generation_method == 'grid':
+ self._thread = threading.Thread(target=self.async_generate, args=(context,))
+ else:
+ _start_cam = self._current_image # 0 normally, first_selected for sequential regen
+ self._thread = threading.Thread(target=self.async_generate, args=(context, _start_cam))
+
+ self._thread.start()
+
+ return {'RUNNING_MODAL'}
+
+
+ def modal(self, context, event):
+ """
+ Handles modal events.
+ :param context: Blender context.
+ :param event: Blender event.
+ :return: {'PASS_THROUGH'}
+ """
+ if event.type == 'TIMER':
+ redraw_ui(context)
+
+ if not self._thread.is_alive():
+ context.window_manager.event_timer_remove(self._timer)
+ ComfyUIGenerate._is_running = False
+ # Restore resolution_percentage that was forced to 100 in execute()
+ if hasattr(self, '_original_resolution_percentage'):
+ bpy.context.scene.render.resolution_percentage = self._original_resolution_percentage
+ bpy.context.scene.render.resolution_x = self._original_resolution_x
+ bpy.context.scene.render.resolution_y = self._original_resolution_y
+ # Restore original visibility for non-selected objects
+ if context.scene.texture_objects == 'selected':
+ 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]
+ if self._error:
+ if self._error == "'25'" or self._error == "'111'" or self._error == "'5'":
+ # Probably canceled by user, quietly return
+ context.scene.generation_status = 'idle'
+ context.scene.sg_last_gen_error = True
+ self.report({'WARNING'}, "Generation cancelled.")
+ _sg_restore_square_display(context.scene)
+ _sg_ensure_crop_overlay()
+ _sg_restore_label_overlay()
+ remove_empty_dirs(context)
+ return {'CANCELLED'}
+ self.report({'ERROR'}, self._error)
+ _sg_restore_square_display(context.scene)
+ _sg_ensure_crop_overlay()
+ _sg_restore_label_overlay()
+ remove_empty_dirs(context)
+ context.scene.generation_status = 'idle'
+ context.scene.sg_last_gen_error = True
+ return {'CANCELLED'}
+ 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
+ for node in nodes:
+ # OSL script nodes: update 'Power' input
+ if node.type == 'SCRIPT':
+ if 'Power' in node.inputs:
+ node.inputs['Power'].default_value = new_exponent
+ # Native MATH POWER nodes (Blender 5.1+ native path)
+ elif node.type == 'MATH' and node.operation == 'POWER' and node.label == 'power_weight':
+ node.inputs[1].default_value = new_exponent
+
+ print("[StableGen] Weight exponent reset complete.")
+
+ # 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'
+ context.area.spaces.active.shading.type = 'RENDERED'
+ context.scene.display_settings.display_device = 'sRGB'
+ context.scene.view_settings.view_transform = 'Standard'
+ _sg_restore_square_display(context.scene)
+ _sg_ensure_crop_overlay()
+ _sg_restore_label_overlay()
+ context.scene.generation_status = 'idle'
+ context.scene.sg_last_gen_error = False
+ # Clear output directories which are not needed anymore
+ addon_prefs = context.preferences.addons[_ADDON_PKG].preferences
+ # Save blend file in the output directory if enabled
+ if addon_prefs.save_blend_file:
+ blend_dir = get_dir_path(context, "revision")
+ # Save the current blend file in the output directory
+ scene_name = os.path.splitext(os.path.basename(bpy.data.filepath))[0]
+ if not scene_name:
+ scene_name = context.scene.name
+ blend_file_path = os.path.join(blend_dir, f"{scene_name}_{context.scene.output_timestamp}.blend")
+ # Clean-up unused data blocks
+ bpy.ops.outliner.orphans_purge(do_recursive=True)
+ # Pack resources and save the blend file
+ bpy.ops.file.pack_all()
+ bpy.ops.wm.save_as_mainfile(filepath=blend_file_path, copy=True)
+ remove_empty_dirs(context)
+ return {'FINISHED'}
+
+ # Handle prompt collection for UV inpainting
+ if context.scene.generation_method == 'uv_inpaint' and self.show_prompt_dialog:
+ current_index = next((i for i, obj in enumerate(self._to_texture)
+ if obj.name == self.current_object_name), -1)
+
+ # Store the current prompt
+ self._object_prompts[self.current_object_name] = self.current_object_prompt
+
+ # Move to next object or finish
+ if current_index < len(self._to_texture) - 1:
+ self.current_object_name = self._to_texture[current_index + 1].name
+ self.current_object_prompt = ""
+ return context.window_manager.invoke_props_dialog(self, width=400)
+ else:
+ self.show_prompt_dialog = False
+ return self.execute(context)
+
+ return {'PASS_THROUGH'}
+
+ def cancel_generate(self, context):
+ """
+ Cancels the generation process using api.interupt().
+ :param context: Blender context.
+ :return: None
+ """
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ client_id = str(uuid.uuid4())
+ data = json.dumps({"client_id": client_id}).encode('utf-8')
+ req = urllib.request.Request("http://{}/interrupt".format(server_address), data=data)
+ context.scene.generation_status = 'waiting'
+ ComfyUIGenerate._is_running = False
+ urllib.request.urlopen(req)
+ # Close active WebSocket to unblock recv() immediately
+ ws = getattr(ComfyUIGenerate, '_active_ws', None)
+ if ws:
+ try:
+ ws.close()
+ except Exception:
+ pass
+ ComfyUIGenerate._active_ws = None
+ remove_empty_dirs(context)
+
+ # ------------------------------------------------------------------
+ # Map preparation — runs from the async thread via timer callbacks
+ # so that _stage updates are visible in real time through the modal.
+ # ------------------------------------------------------------------
+ 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
+ ``_run_on_main_thread`` so the progress bar can update between calls."""
+ controlnet_units = self._controlnet_units
+ cameras = self._cameras
+
+ if context.scene.generation_mode in ('standard', 'regenerate_selected'):
+ need_depth = (
+ any(u["unit_type"] == "depth" for u in controlnet_units)
+ or (context.scene.use_flux_lora and context.scene.model_architecture == 'flux1')
+ or (context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein')
+ and context.scene.qwen_guidance_map_type == 'depth')
+ or (context.scene.model_architecture.startswith('qwen')
+ and context.scene.qwen_generation_method in ('refine', 'local_edit')
+ and context.scene.qwen_refine_use_depth)
+ )
+ 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
+ with _SGCameraResolution(context, _cam):
+ self.export_depthmap(context, camera_id=_i)
+ self._run_on_main_thread(_render_depth)
+ if self._error:
+ return
+ if context.scene.generation_method == 'grid':
+ self._run_on_main_thread(
+ lambda: self.combine_maps(context, cameras, type="depth"))
+ if self._error:
+ return
+
+ need_canny = any(u["unit_type"] == "canny" for u in controlnet_units)
+ if need_canny and context.scene.generation_method != 'uv_inpaint':
+ for i, camera in enumerate(cameras):
+ 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
+ with _SGCameraResolution(context, _cam):
+ export_canny(context, camera_id=_i,
+ low_threshold=context.scene.canny_threshold_low,
+ high_threshold=context.scene.canny_threshold_high)
+ self._run_on_main_thread(_render_canny)
+ if self._error:
+ return
+ if context.scene.generation_method == 'grid':
+ self._run_on_main_thread(
+ lambda: self.combine_maps(context, cameras, type="canny"))
+ if self._error:
+ return
+
+ need_normal = (
+ any(u["unit_type"] == "normal" for u in controlnet_units)
+ or (context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein')
+ and context.scene.qwen_guidance_map_type == 'normal')
+ )
+ if need_normal and context.scene.generation_method != 'uv_inpaint':
+ for i, camera in enumerate(cameras):
+ 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
+ with _SGCameraResolution(context, _cam):
+ self.export_normal(context, camera_id=_i)
+ self._run_on_main_thread(_render_normal)
+ if self._error:
+ return
+ if context.scene.generation_method == 'grid':
+ self._run_on_main_thread(
+ lambda: self.combine_maps(context, cameras, type="normal"))
+ if self._error:
+ return
+
+ # Qwen guidance using Workbench
+ if (context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein')
+ and context.scene.qwen_guidance_map_type == 'workbench'
+ and context.scene.generation_method != 'uv_inpaint'):
+ workbench_dir = get_dir_path(context, "controlnet")["workbench"]
+ for i, camera in enumerate(cameras):
+ 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
+ with _SGCameraResolution(context, _cam):
+ export_render(context, camera_id=_i,
+ output_dir=workbench_dir, filename=f"render{_i}")
+ self._run_on_main_thread(_render_wb)
+ if self._error:
+ return
+ if context.scene.generation_method == 'grid':
+ self._run_on_main_thread(
+ lambda: self.combine_maps(context, cameras, type="workbench"))
+ if self._error:
+ return
+
+ # Qwen guidance using Viewport
+ elif (context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein')
+ and context.scene.qwen_guidance_map_type == 'viewport'
+ and context.scene.generation_method != 'uv_inpaint'):
+ viewport_dir = get_dir_path(context, "controlnet")["viewport"]
+ for i, camera in enumerate(cameras):
+ 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
+ with _SGCameraResolution(context, _cam):
+ export_viewport(context, camera_id=_i,
+ output_dir=viewport_dir, filename=f"viewport{_i}")
+ self._run_on_main_thread(_render_vp)
+ if self._error:
+ return
+ if context.scene.generation_method == 'grid':
+ self._run_on_main_thread(
+ lambda: self.combine_maps(context, cameras, type="viewport"))
+ if self._error:
+ return
+
+ # Refine / Local Edit mode — emit images + edge feather masks
+ is_refine = (
+ context.scene.generation_method in ('refine', 'local_edit')
+ or (context.scene.model_architecture.startswith('qwen')
+ and context.scene.qwen_generation_method in ('refine', 'local_edit'))
+ )
+ if is_refine:
+ need_feather = (
+ context.scene.refine_edge_feather_projection
+ and (context.scene.generation_method == 'local_edit'
+ or (context.scene.model_architecture.startswith('qwen')
+ and context.scene.qwen_generation_method == 'local_edit'))
+ )
+ for i, camera in enumerate(cameras):
+ 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
+ with _SGCameraResolution(context, _cam):
+ export_emit_image(context, self._to_texture, camera_id=_i)
+ if need_feather:
+ render_edge_feather_mask(
+ context, self._to_texture, _cam, _i,
+ feather_width=context.scene.refine_edge_feather_width,
+ softness=context.scene.refine_edge_feather_softness)
+ self._run_on_main_thread(_render_refine)
+ if self._error:
+ return
+
+ def async_generate(self, context, camera_id = None):
+ """
+ Asynchronously generates the image using ComfyUI.
+ :param context: Blender context.
+ :return: None
+ """
+ self._error = None
+ self._pbr_maps = {} # camera_key → {map_name: file_path}
+ try:
+ # --- Render ControlNet / refine maps with live progress ---
+ self._prepare_maps(context)
+ if self._error:
+ return
+
+ while self._threads_left > 0 and ComfyUIGenerate._is_running and not context.scene.generation_mode == 'project_only':
+ # Swap scene resolution to per-camera values if stored.
+ # Must use a timer callback so the write happens on the
+ # main thread; writing RNA from a background thread would
+ # trigger DEG_id_tag_update on a NULL depsgraph and crash.
+ if camera_id is not None and camera_id < len(self._cameras):
+ _cam = self._cameras[camera_id]
+ _rx, _ry = _get_camera_resolution(_cam, context.scene)
+ def _swap_resolution():
+ try:
+ context.scene.render.resolution_x = _rx
+ context.scene.render.resolution_y = _ry
+ except Exception as e:
+ self._error = str(e)
+ traceback.print_exc()
+ self._wait_event.set()
+ return None
+ bpy.app.timers.register(_swap_resolution)
+ self._wait_event.wait()
+ self._wait_event.clear()
+ if self._error:
+ return
+
+ # Sequential regeneration: non-selected cameras skip AI
+ # generation but still reproject their existing image so
+ # subsequent cameras see the correct incremental texture state.
+ _is_seq_reproject = (
+ context.scene.generation_mode == 'regenerate_selected'
+ and camera_id not in self._selected_camera_ids
+ and context.scene.generation_method == 'sequential'
+ )
+
+ if _is_seq_reproject:
+ self._stage = "Reprojecting Image"
+ self._progress = 0
+ # project_image patches the material tree for this camera,
+ # loading the existing generated image from disk.
+ def image_reproject_callback():
+ try:
+ redraw_ui(context)
+ project_image(context, self._to_texture, self._material_id, stop_index=self._current_image)
+ except Exception as e:
+ self._error = str(e)
+ traceback.print_exc()
+ self._wait_event.set()
+ return None
+ bpy.app.timers.register(image_reproject_callback)
+ self._wait_event.wait()
+ self._wait_event.clear()
+ if self._error:
+ return
+
+ elif context.scene.steps != 0 and not (context.scene.generation_mode == 'regenerate_selected' and camera_id not in self._selected_camera_ids):
+ # Prepare Image Info for Upload
+ controlnet_info = {}
+ mask_info = None
+ render_info = None
+ ipadapter_ref_info = None
+
+ # Get info for controlnet images for the current camera or grid
+ if context.scene.generation_method != 'uv_inpaint':
+ controlnet_info["depth"] = self._get_uploaded_image_info(context, "controlnet", subtype="depth", camera_id=camera_id)
+ controlnet_info["canny"] = self._get_uploaded_image_info(context, "controlnet", subtype="canny", camera_id=camera_id)
+ controlnet_info["normal"] = self._get_uploaded_image_info(context, "controlnet", subtype="normal", camera_id=camera_id)
+ else: # UV Inpainting
+ current_obj_name = self._to_texture[self._current_image].name
+ mask_info = self._get_uploaded_image_info(context, "uv_inpaint", subtype="visibility", object_name=current_obj_name)
+ render_info = self._get_uploaded_image_info(context, "baked", object_name=current_obj_name)
+
+ # Get info for refine/sequential render/mask inputs
+ if context.scene.generation_method in ('refine', 'local_edit'):
+ render_info = self._get_uploaded_image_info(context, "inpaint", subtype="render", camera_id=camera_id)
+ elif context.scene.generation_method == 'sequential' and self._current_image > 0:
+ render_info = self._get_uploaded_image_info(context, "inpaint", subtype="render", camera_id=self._current_image)
+ mask_info = self._get_uploaded_image_info(context, "inpaint", subtype="visibility", camera_id=self._current_image)
+
+ # Get info for IPAdapter reference image
+ if context.scene.use_ipadapter:
+ ipadapter_ref_info = self._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(context.scene.ipadapter_image))
+ elif context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_mode == 'trellis2_input':
+ # Use the TRELLIS.2 input image as IPAdapter reference
+ t2_path = getattr(context.scene, 'trellis2_last_input_image', '')
+ if t2_path and os.path.exists(bpy.path.abspath(t2_path)):
+ ipadapter_ref_info = self._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(t2_path))
+ elif context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_mode == 'original_render' and context.scene.generation_method == 'local_edit':
+ # Use the existing texture render from this camera's viewpoint as IPAdapter reference
+ ipadapter_ref_info = self._get_uploaded_image_info(context, "inpaint", subtype="render", camera_id=camera_id)
+ elif context.scene.sequential_ipadapter and self._current_image > 0:
+ cam_id = 0 if context.scene.sequential_ipadapter_mode == 'first' else self._current_image - 1
+ ipadapter_ref_info = self._get_uploaded_image_info(context, "generated", camera_id=cam_id, material_id=self._material_id)
+
+ # Filter out None values from controlnet_info
+ controlnet_info = {k: v for k, v in controlnet_info.items() if v is not None}
+ # End Prepare Image Info
+
+ # Generate image without ControlNet if needed
+ if context.scene.generation_mode == 'standard' and camera_id == 0 and (context.scene.generation_method == 'sequential' or context.scene.generation_method in ('refine', 'local_edit'))\
+ and context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_regenerate and not context.scene.use_ipadapter and context.scene.sequential_ipadapter_mode == 'first'\
+ and context.scene.sequential_ipadapter_mode != 'trellis2_input':
+ self._stage = "Generating Reference Image"
+ # Don't use ControlNet for the first image if sequential_ipadapter_regenerate_wo_controlnet is enabled
+ if context.scene.sequential_ipadapter_regenerate_wo_controlnet:
+ original_strengths = [unit.strength for unit in context.scene.controlnet_units]
+ for unit in context.scene.controlnet_units:
+ unit.strength = 0.0
+ else:
+ self._stage = "Uploading to Server"
+ self._progress = 0
+
+ # Generate the image
+ if context.scene.generation_method in ('refine', 'local_edit'):
+ if context.scene.model_architecture == 'flux1':
+ image = self.workflow_manager.refine_flux(context, controlnet_info=controlnet_info, render_info=render_info, ipadapter_ref_info=ipadapter_ref_info)
+ else:
+ image = self.workflow_manager.refine(context, controlnet_info=controlnet_info, render_info=render_info, ipadapter_ref_info=ipadapter_ref_info)
+ elif context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method in ('refine', 'local_edit'):
+ image = self.workflow_manager.generate_qwen_refine(context, camera_id=camera_id)
+ elif context.scene.generation_method == 'uv_inpaint':
+ if context.scene.model_architecture == 'flux1':
+ image = self.workflow_manager.refine_flux(context, mask_info=mask_info, render_info=render_info)
+ else:
+ image = self.workflow_manager.refine(context, mask_info=mask_info, render_info=render_info)
+ elif context.scene.generation_method == 'sequential':
+ if self._current_image == 0:
+ if context.scene.model_architecture == 'flux1':
+ image = self.workflow_manager.generate_flux(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)
+ elif context.scene.model_architecture == 'qwen_image_edit':
+ image = self.workflow_manager.generate_qwen_edit(context, camera_id=camera_id)
+ elif context.scene.model_architecture == 'flux2_klein':
+ image = self.workflow_manager.generate_flux2_klein(context, camera_id=camera_id)
+ else:
+ image = self.workflow_manager.generate(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)
+ else:
+ self._stage = "Preparing Next Camera"
+ self._progress = 0
+ def context_callback():
+ try:
+ # Export visibility mask and render for the current camera, we need to use a callback to be in the main thread
+ # Visibility is rendered from the *current* camera's viewpoint
+ # (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
+ with _SGCameraResolution(context, _cur_cam):
+ export_visibility(context, self._to_texture, camera_visibility=_vis_cam) # Export mask for current view
+ _emit_cam = _cur_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)
+ 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:
+ # Use a gray (neutral) background and fallback for other architectures
+ export_emit_image(context, self._to_texture, camera_id=self._current_image, bg_color=(0.5, 0.5, 0.5), fallback_color=(0.5, 0.5, 0.5))
+ except Exception as e:
+ self._error = str(e)
+ traceback.print_exc()
+ self._wait_event.set()
+ return None
+ bpy.app.timers.register(context_callback)
+ self._wait_event.wait()
+ self._wait_event.clear()
+ if self._error:
+ return
+ self._stage = "Uploading to Server"
+ # Get info for the previous render and mask
+ render_info = self._get_uploaded_image_info(context, "inpaint", subtype="render", camera_id=self._current_image)
+ mask_info = self._get_uploaded_image_info(context, "inpaint", subtype="visibility", camera_id=self._current_image)
+
+ if context.scene.model_architecture == 'flux1':
+ image = self.workflow_manager.refine_flux(context, controlnet_info=controlnet_info, render_info=render_info, mask_info=mask_info, ipadapter_ref_info=ipadapter_ref_info)
+ elif context.scene.model_architecture == 'qwen_image_edit':
+ image = self.workflow_manager.generate_qwen_edit(context, camera_id=camera_id)
+ elif context.scene.model_architecture == 'flux2_klein':
+ image = self.workflow_manager.generate_flux2_klein(context, camera_id=camera_id)
+ else:
+ image = self.workflow_manager.refine(context, controlnet_info=controlnet_info, render_info=render_info, mask_info=mask_info, ipadapter_ref_info=ipadapter_ref_info)
+ else: # Grid or Separate
+ if context.scene.model_architecture == 'flux1':
+ image = self.workflow_manager.generate_flux(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)
+ elif context.scene.model_architecture == 'qwen_image_edit':
+ image = self.workflow_manager.generate_qwen_edit(context, camera_id=camera_id)
+ elif context.scene.model_architecture == 'flux2_klein':
+ image = self.workflow_manager.generate_flux2_klein(context, camera_id=camera_id)
+ else:
+ image = self.workflow_manager.generate(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)
+
+ if image == {"error": "conn_failed"}:
+ if not self._error:
+ 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)
+ elif camera_id is not None:
+ image_path = get_file_path(context, "generated", camera_id=camera_id, material_id=self._material_id)
+ else: # Grid mode initial generation
+ image_path = get_file_path(context, "generated", filename="generated_image_grid") # Save grid to a specific name
+
+ with open(image_path, 'wb') as f:
+ f.write(image)
+
+
+ # Use hack to re-generate the image using IPAdapter to match IPAdapter style
+ if camera_id == 0 and (context.scene.generation_method == 'sequential' or context.scene.generation_method == 'separate' or context.scene.generation_method in ('refine', 'local_edit'))\
+ and context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_regenerate and not context.scene.use_ipadapter and context.scene.sequential_ipadapter_mode == 'first':
+
+ # Restore original strengths
+ if context.scene.sequential_ipadapter_regenerate_wo_controlnet:
+ for i, unit in enumerate(context.scene.controlnet_units):
+ unit.strength = original_strengths[i]
+ self._stage = "Generating Image"
+ context.scene.use_ipadapter = True
+ context.scene.ipadapter_image = image_path
+ ipadapter_ref_info = self._get_uploaded_image_info(context, "custom", filename=image_path)
+ if context.scene.model_architecture == "sdxl":
+ if context.scene.generation_method in ("refine", "local_edit"):
+ image = self.workflow_manager.refine(context, controlnet_info=controlnet_info, render_info=render_info, mask_info=mask_info, ipadapter_ref_info=ipadapter_ref_info)
+ else:
+ image = self.workflow_manager.generate(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)
+ elif context.scene.model_architecture == "flux1":
+ if context.scene.generation_method in ("refine", "local_edit"):
+ image = self.workflow_manager.refine_flux(context, controlnet_info=controlnet_info, render_info=render_info, mask_info=mask_info, ipadapter_ref_info=ipadapter_ref_info)
+ else:
+ image = self.workflow_manager.generate_flux(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)
+ context.scene.use_ipadapter = False
+ image_path = image_path.replace(".png", "_ipadapter.png")
+ with open(image_path, 'wb') as f:
+ f.write(image)
+
+ # ── PBR decomposition is deferred until after ALL cameras ──
+
+ # Sequential mode callback
+ if context.scene.generation_method == 'sequential':
+ self._stage = "Projecting Image"
+ def image_project_callback():
+ try:
+ redraw_ui(context)
+ project_image(context, self._to_texture, self._material_id, stop_index=self._current_image)
+ except Exception as e:
+ self._error = str(e)
+ traceback.print_exc()
+ # Set the event to signal the end of the process
+ self._wait_event.set()
+ return None
+ bpy.app.timers.register(image_project_callback)
+ # Wait for the event to be set
+ self._wait_event.wait()
+ self._wait_event.clear()
+ if self._error:
+ return
+ # Update info for the next iteration (if any)
+ if self._current_image < len(self._cameras) - 1:
+ next_camera_id = self._current_image + 1
+ # ControlNet info will be re-fetched at the start of the next loop iteration
+ else: # steps == 0, skip generation
+ pass # No image generation needed
+
+ if context.scene.generation_method in ('separate', 'refine', 'local_edit', 'sequential') or (context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method in ('refine', 'local_edit')):
+ self._current_image += 1
+ self._threads_left -= 1
+ if self._threads_left > 0:
+ self._progress = 0
+ if camera_id is not None: # Increment camera_id only if it was initially provided
+ camera_id += 1
+
+ elif context.scene.generation_method == 'uv_inpaint':
+ self._current_image += 1
+ self._threads_left -= 1
+ if self._threads_left > 0:
+ self._progress = 0
+
+ elif context.scene.generation_method == 'grid':
+ # Split the generated grid image back into multiple images
+ self.split_generated_grid(context, self._cameras)
+ if context.scene.refine_images:
+ for i, _ in enumerate(self._cameras):
+ self._stage = f"Refining Image {i+1}/{len(self._cameras)}"
+ self._current_image = i + 1
+ # Refine the split images
+ refine_cn_info = {
+ "depth": self._get_uploaded_image_info(context, "controlnet", subtype="depth", camera_id=i),
+ "canny": self._get_uploaded_image_info(context, "controlnet", subtype="canny", camera_id=i),
+ "normal": self._get_uploaded_image_info(context, "controlnet", subtype="normal", camera_id=i)
+ }
+ refine_cn_info = {k: v for k, v in refine_cn_info.items() if v is not None}
+ refine_render_info = self._get_uploaded_image_info(context, "generated", camera_id=i, material_id=self._material_id)
+
+ if context.scene.model_architecture == 'flux1':
+ image = self.workflow_manager.refine_flux(context, controlnet_info=refine_cn_info, render_info=refine_render_info)
+ else:
+ image = self.workflow_manager.refine(context, controlnet_info=refine_cn_info, render_info=refine_render_info)
+
+ if image == {"error": "conn_failed"}:
+ self._error = "Failed to connect to ComfyUI server."
+ return
+ # Overwrite the split image with the refined one
+ image_path = get_file_path(context, "generated", camera_id=i, material_id=self._material_id)
+ with open(image_path, 'wb') as f:
+ f.write(image)
+ self._threads_left = 0
+
+ except Exception as e:
+ self._error = str(e)
+ traceback.print_exc()
+ return
+
+ # ── PBR Decomposition (runs after ALL cameras are generated) ──
+ if getattr(context.scene, 'pbr_decomposition', False):
+ self._pbr_maps = {}
+ # Collect camera images that need PBR decomposition
+ camera_images = {} # cam_idx → image_path
+ per_camera_missing = {} # cam_idx → set of missing map names (reproject only)
+ num_cameras = len(self._cameras)
+ for cam_idx in range(num_cameras):
+ cam_image_path = get_file_path(
+ context, "generated", camera_id=cam_idx,
+ material_id=self._material_id,
+ )
+ if os.path.exists(cam_image_path):
+ # In project_only (reproject) mode, reuse existing PBR
+ # maps when all enabled maps are already on disk. This
+ # avoids re-running the slow ComfyUI decomposition.
+ if context.scene.generation_mode == 'project_only':
+ existing, missing = self._find_existing_pbr_maps(
+ context, cam_idx)
+ if not missing:
+ # All maps present — fully reuse
+ self._ensure_raw_copies(
+ context, cam_idx, existing)
+ self._pbr_maps[cam_idx] = existing
+ print(f"[StableGen] Reusing existing PBR maps "
+ f"for camera {cam_idx}")
+ continue
+ # Some maps exist, some are missing — seed existing
+ # maps so the batched function only generates what's
+ # missing.
+ if existing:
+ self._ensure_raw_copies(
+ context, cam_idx, existing)
+ self._pbr_maps[cam_idx] = existing
+ per_camera_missing[cam_idx] = missing
+ print(f"[StableGen] PBR maps missing for camera "
+ f"{cam_idx}: {missing}, running selective decomposition…")
+ camera_images[cam_idx] = cam_image_path
+ if camera_images:
+ self._run_pbr_decomposition_batched(
+ context, camera_images,
+ per_camera_missing=per_camera_missing if per_camera_missing else None)
+ if self._error:
+ return
+
+ # ── Batch post-processing (albedo sat/contrast, roughness scale) ──
+ # Runs after ALL cameras have raw maps saved, so auto-saturation
+ # can average across all cameras uniformly.
+ if self._pbr_maps:
+ self._apply_albedo_postprocessing_batch(context)
+
+ # ── Persist per-map settings hashes so future reprojects can
+ # detect stale maps and only regenerate what changed. ──
+ if camera_images:
+ for cam_idx in camera_images:
+ self._save_pbr_settings(context, cam_idx)
+
+ def image_project_callback():
+ if context.scene.generation_method == 'sequential':
+ # In sequential mode, projection happened per-camera inside the loop.
+ # PBR is projected onto the existing material after all cameras.
+ if getattr(context.scene, 'pbr_decomposition', False) and hasattr(self, '_pbr_maps') and self._pbr_maps:
+ from .pbr_projection import project_pbr_to_bsdf
+ project_pbr_to_bsdf(
+ context, self._to_texture, self._pbr_maps,
+ material_id=self._material_id
+ )
+ if getattr(context.scene, 'pbr_auto_lighting', False):
+ _pbr_setup_studio_lights(context, self._to_texture)
+ 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)
+ else:
+ # Apply the UV inpainted textures to each mesh
+ from .rendering import apply_uv_inpaint_texture
+ for obj in self._to_texture:
+ texture_path = get_file_path(
+ context, "generated_baked", object_name=obj.name, material_id=self._material_id
+ )
+ apply_uv_inpaint_texture(context, obj, texture_path)
+
+ # Project PBR maps onto material after color projection
+ if getattr(context.scene, 'pbr_decomposition', False) and hasattr(self, '_pbr_maps') and self._pbr_maps:
+ from .pbr_projection import project_pbr_to_bsdf
+ project_pbr_to_bsdf(
+ context, self._to_texture, self._pbr_maps,
+ material_id=self._material_id
+ )
+ if getattr(context.scene, 'pbr_auto_lighting', False):
+ _pbr_setup_studio_lights(context, self._to_texture)
+
+ return None
+
+ if context.scene.view_blend_use_color_match and self._to_texture:
+ self._stage = "Matching Colors"
+ # Use the first object in the target list as the color reference
+ ref_np = _get_viewport_ref_np(self._to_texture[0])
+ if ref_np is not None:
+ # Loop all cameras we generated for
+ for cam_idx, cam in enumerate(self._cameras):
+ image_path = get_file_path(
+ context,
+ "generated",
+ camera_id=cam_idx,
+ material_id=self._material_id,
+ )
+ _apply_color_match_to_file(
+ image_path=image_path,
+ ref_rgb=ref_np,
+ scene=context.scene,
+ )
+
+ bpy.app.timers.register(image_project_callback)
+
+ # Update seed based on control parameter
+ if context.scene.control_after_generate == 'increment':
+ context.scene.seed += 1
+ elif context.scene.control_after_generate == 'decrement':
+ context.scene.seed -= 1
+ elif context.scene.control_after_generate == 'randomize':
+ context.scene.seed = np.random.randint(0, 1000000)
+
+ def draw(self, context):
+ layout = self.layout
+ if context.scene.generation_method == 'uv_inpaint' and self.show_prompt_dialog:
+ layout.label(text=f"Enter prompt for object: {self.current_object_name}")
+ layout.prop(self, "current_object_prompt", text="")
+
+ def invoke(self, context, event):
+ if context.scene.generation_method == 'uv_inpaint':
+ # Reset object prompts on every run
+ self.show_prompt_dialog = True
+ self._object_prompts = {}
+ self._to_texture = [obj.name for obj in bpy.context.view_layer.objects if obj.type == 'MESH']
+ if context.scene.texture_objects == 'selected':
+ self._to_texture = [obj.name for obj in bpy.context.selected_objects if obj.type == 'MESH']
+ self.mesh_index = 0
+ self.current_object_name = self._to_texture[0] if self._to_texture else ""
+ # If "Ask for object prompts" is disabled, don’t prompt per object
+ if not context.scene.ask_object_prompts or self._is_running:
+ self.show_prompt_dialog = False
+ return self.execute(context)
+ 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):
+ """
+ Exports the depth map of the scene.
+ :param context: Blender context.
+ :param camera_id: ID of the camera.
+ :return: None
+ """
+ print("[StableGen] Exporting depth map")
+ # Save original settings to restore later.
+ original_engine = bpy.context.scene.render.engine
+ original_view_transform = bpy.context.scene.view_settings.view_transform
+ original_film_transparent = bpy.context.scene.render.film_transparent
+ 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"
+
+ # Ensure the directory exists
+ if not os.path.exists(output_dir):
+ os.makedirs(output_dir)
+
+ # Get the active view layer
+ view_layer = bpy.context.view_layer
+
+ # Switch to WORKBENCH render engine
+ bpy.context.scene.render.engine = 'BLENDER_WORKBENCH'
+
+ bpy.context.scene.display_settings.display_device = 'sRGB'
+ bpy.context.scene.view_settings.view_transform = 'Raw'
+
+ original_pass_z = view_layer.use_pass_z
+
+ # Enable depth pass in the render settings
+ view_layer.use_pass_z = True
+
+ # Enable compositor pipeline (may have been disabled by prior GIF export)
+ bpy.context.scene.render.use_compositing = True
+ bpy.context.scene.use_nodes = True
+ node_tree = get_compositor_node_tree(bpy.context.scene)
+ nodes = node_tree.nodes
+ links = node_tree.links
+
+ # 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)
+
+ # Add a normalize node (to scale depth values between 0 and 1)
+ normalize_node = nodes.new(type="CompositorNodeNormalize")
+ normalize_node.location = (200, 0)
+ links.new(render_layers_node.outputs["Depth"], normalize_node.inputs[0])
+
+ # Add an invert node to flip the depth map values
+ invert_node = nodes.new(type="CompositorNodeInvert")
+ invert_node.location = (400, 0)
+ # Blender 5.x uses named "Color" input, 4.x uses index 1
+ color_input = invert_node.inputs["Color"] if "Color" in invert_node.inputs else invert_node.inputs[1]
+ links.new(normalize_node.outputs[0], color_input)
+
+ # Add an output file node
+ output_node = nodes.new(type="CompositorNodeOutputFile")
+ output_node.location = (600, 0)
+ configure_output_node_paths(output_node, output_dir, output_file)
+ links.new(invert_node.outputs[0], output_node.inputs[0])
+
+ # Render the scene
+ bpy.ops.render.render(write_still=True)
+
+ bpy.context.scene.view_settings.view_transform = 'Standard'
+
+ print(f"[StableGen] Depth map saved to: {os.path.join(output_dir, output_file)}.png")
+
+ # Restore original settings
+ bpy.context.scene.render.engine = original_engine
+ bpy.context.scene.view_settings.view_transform = original_view_transform
+ bpy.context.scene.render.film_transparent = original_film_transparent
+ bpy.context.scene.render.use_compositing = original_use_compositing
+ bpy.context.scene.render.filepath = original_filepath
+ view_layer.use_pass_z = original_pass_z
+
+ def export_normal(self, context, camera_id=None):
+ """
+ Exports the normal map of the scene.
+ Areas without geometry will show the neutral color (0.5, 0.5, 1.0).
+ :param context: Blender context.
+ :param camera_id: ID of the camera.
+ :return: None
+ """
+ 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"
+
+ if not os.path.exists(output_dir):
+ os.makedirs(output_dir)
+
+ view_layer = bpy.context.view_layer
+ original_pass_normal = view_layer.use_pass_normal
+ view_layer.use_pass_normal = True
+
+ # Store original settings to restore later.
+ original_engine = bpy.context.scene.render.engine
+ original_view_transform = bpy.context.scene.view_settings.view_transform
+ original_film_transparent = bpy.context.scene.render.film_transparent
+ original_use_compositing = bpy.context.scene.render.use_compositing
+ original_filepath = bpy.context.scene.render.filepath
+
+ bpy.context.scene.render.engine = get_eevee_engine_id()
+ bpy.context.scene.view_settings.view_transform = 'Raw'
+ bpy.context.scene.render.film_transparent = True
+ bpy.context.scene.render.use_compositing = True
+ bpy.context.scene.use_nodes = True
+
+ # Clear existing nodes.
+ 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")
+ render_layers_node.location = (0, 0)
+
+ # Create an RGB node set to the neutral normal color (0.5, 0.5, 1.0, 1.0).
+ bg_node = nodes.new(type="CompositorNodeRGB")
+ bg_node.outputs[0].default_value = (0.5, 0.5, 1.0, 1.0)
+ bg_node.location = (0, -200)
+
+ alpha_over_node = nodes.new(type="CompositorNodeAlphaOver")
+ alpha_over_node.location = (200, 0)
+ # Link the normal pass to the top input.
+ links.new(render_layers_node.outputs["Normal"], alpha_over_node.inputs[2])
+ # Link the neutral background to the bottom input.
+ links.new(bg_node.outputs[0], alpha_over_node.inputs[1])
+
+ # Create the Output File node.
+ output_node = nodes.new(type="CompositorNodeOutputFile")
+ output_node.location = (400, 0)
+ configure_output_node_paths(output_node, output_dir, output_file)
+ links.new(alpha_over_node.outputs[0], output_node.inputs[0])
+ links.new(render_layers_node.outputs["Alpha"], alpha_over_node.inputs[0])
+
+ bpy.ops.render.render(write_still=True)
+
+ # Restore original settings.
+ bpy.context.scene.render.engine = original_engine
+ bpy.context.scene.view_settings.view_transform = original_view_transform
+ bpy.context.scene.render.film_transparent = original_film_transparent
+ bpy.context.scene.render.use_compositing = original_use_compositing
+ bpy.context.scene.render.filepath = original_filepath
+
+ view_layer.use_pass_normal = original_pass_normal
+
+ print(f"[StableGen] Normal map saved to: {os.path.join(output_dir, output_file)}.png")
+
+ def combine_maps(self, context, cameras, type):
+ """Combines depth maps into a grid."""
+ if type == 'depth':
+ grid_image_path = get_file_path(context, "controlnet", subtype="depth", camera_id=None, material_id=self._material_id)
+ elif type == 'canny':
+ grid_image_path = get_file_path(context, "controlnet", subtype="canny", camera_id=None, material_id=self._material_id)
+ elif type == 'normal':
+ grid_image_path = get_file_path(context, "controlnet", subtype="normal", camera_id=None, material_id=self._material_id)
+ elif type == 'workbench':
+ grid_image_path = get_file_path(context, "controlnet", subtype="workbench", camera_id=None, material_id=self._material_id)
+ elif type == 'viewport':
+ grid_image_path = get_file_path(context, "controlnet", subtype="viewport", camera_id=None, material_id=self._material_id)
+
+ # Render depth maps for each camera and combine them into a grid
+ depth_maps = []
+ for i, camera in enumerate(cameras):
+ bpy.context.scene.camera = camera
+ if type == 'depth':
+ depth_map_path = get_file_path(context, "controlnet", subtype="depth", camera_id=i, material_id=self._material_id)
+ elif type == 'canny':
+ depth_map_path = get_file_path(context, "controlnet", subtype="canny", camera_id=i, material_id=self._material_id)
+ elif type == 'normal':
+ depth_map_path = get_file_path(context, "controlnet", subtype="normal", camera_id=i, material_id=self._material_id)
+ elif type == 'workbench':
+ depth_map_path = get_file_path(context, "controlnet", subtype="workbench", camera_id=i, material_id=self._material_id)
+ elif type == 'viewport':
+ depth_map_path = get_file_path(context, "controlnet", subtype="viewport", camera_id=i, material_id=self._material_id)
+ depth_maps.append(depth_map_path)
+
+ # Combine depth maps into a grid
+ grid_image = self.create_grid_image(depth_maps)
+ grid_image = self.rescale_to_1mp(grid_image)
+ grid_image.save(grid_image_path)
+ print(f"[StableGen] Combined depth map grid saved to: {grid_image_path}")
+
+ def create_grid_image(self, image_paths):
+ """Creates a grid image from a list of image paths."""
+ images = [Image.open(path) for path in image_paths]
+ widths, heights = zip(*(i.size for i in images))
+
+ # Calculate grid dimensions to make it as square as possible
+ num_images = len(images)
+ grid_width = math.ceil(math.sqrt(num_images))
+ grid_height = math.ceil(num_images / grid_width)
+
+ max_width = max(widths)
+ max_height = max(heights)
+
+ total_width = grid_width * max_width
+ total_height = grid_height * max_height
+
+ grid_image = Image.new('RGB', (total_width, total_height))
+
+ x_offset = 0
+ y_offset = 0
+ for i, img in enumerate(images):
+ grid_image.paste(img, (x_offset, y_offset))
+ x_offset += max_width
+ if (i + 1) % grid_width == 0:
+ x_offset = 0
+ y_offset += max_height
+
+ return grid_image
+
+ def rescale_to_1mp(self, image):
+ """Rescales the image to approximately 1MP."""
+
+ width, height = image.size
+ total_pixels = width * height
+ scale_factor = (1_000_000 / total_pixels) ** 0.5
+
+ new_width = int(width * scale_factor)
+ new_height = int(height * scale_factor)
+
+ # Ensure the new dimensions are divisible by 8 (ComfyUI requirement)
+ new_width -= new_width % 8
+ new_height -= new_height % 8
+
+ self._grid_height = new_height
+ self._grid_width = new_width
+
+ return image.resize((new_width, new_height), Image.LANCZOS)
+
+ def split_generated_grid(self, context, cameras):
+ """Splits the generated grid image back into multiple images."""
+ grid_image_path = get_file_path(context, "generated", camera_id=None, material_id=self._material_id)
+
+ # Load the generated grid image
+ grid_image = Image.open(grid_image_path)
+
+ # Calculate grid dimensions to make it as square as possible
+ num_images = len(cameras)
+ grid_width = math.ceil(math.sqrt(num_images))
+ grid_height = math.ceil(num_images / grid_width)
+
+ max_width = grid_image.width // grid_width
+ max_height = grid_image.height // grid_height
+
+ x_offset = 0
+ y_offset = 0
+ for i in range(num_images):
+ bbox = (x_offset, y_offset, x_offset + max_width, y_offset + max_height)
+ individual_image = grid_image.crop(bbox)
+ individual_image_path = get_file_path(context, "generated", camera_id=i, material_id=self._material_id)
+ individual_image.save(individual_image_path)
+ print(f"[StableGen] Generated image for camera {i+1} saved to: {individual_image_path}")
+ x_offset += max_width
+ if (i + 1) % grid_width == 0:
+ x_offset = 0
+ y_offset += max_height
+
+ def _get_uploaded_image_info(self, context, file_type, subtype=None, filename=None, camera_id=None, object_name=None, material_id=None):
+ """
+ Gets local path, uploads if needed, caches, and returns ComfyUI upload info.
+ Intended to be called within the ComfyUIGenerate operator instance.
+
+ Args:
+ self: The instance of the ComfyUIGenerate operator.
+ context: Blender context.
+ file_type: Type of file (e.g., "controlnet", "generated", "baked").
+ subtype: Subtype (e.g., "depth", "render").
+ filename: Specific filename if overriding default naming.
+ camera_id: Camera index.
+ object_name: Object name.
+ material_id: Material index.
+
+ Returns:
+ dict: Upload info from ComfyUI (containing 'name', etc.) or None if failed/not found.
+ """
+ effective_material_id = material_id
+
+ # Use the existing get_file_path to determine the canonical local path
+ if not file_type == "custom": # Custom files use provided filename directly
+ local_path = get_file_path(context, file_type, subtype, filename, camera_id, object_name, effective_material_id)
+ else:
+ local_path = filename
+
+ # --- Image Modification for 'recent' sequential mode ---
+ # Check if we need to modify the image before uploading
+ is_recent_mode_ref = (
+ file_type == "generated" and
+ context.scene.sequential_ipadapter_mode == 'recent' and
+ (context.scene.sequential_ipadapter or context.scene.model_architecture in ('qwen_image_edit', 'flux2_klein'))
+ )
+
+ temp_image_path = None
+ upload_path = local_path
+
+ if is_recent_mode_ref:
+ desaturate = context.scene.sequential_desaturate_factor
+ contrast = context.scene.sequential_contrast_factor
+
+ if desaturate > 0.0 or contrast > 0.0:
+ try:
+ with Image.open(local_path) as img:
+ if desaturate > 0.0:
+ enhancer = ImageEnhance.Color(img)
+ img = enhancer.enhance(1.0 - desaturate)
+
+ if contrast > 0.0:
+ enhancer = ImageEnhance.Contrast(img)
+ img = enhancer.enhance(1.0 - contrast)
+
+ # Save to a temporary file for upload
+ temp_dir = get_dir_path(context, "temp")
+ os.makedirs(temp_dir, exist_ok=True)
+ temp_image_path = os.path.join(temp_dir, f"temp_{os.path.basename(local_path)}")
+ img.save(temp_image_path)
+ upload_path = temp_image_path
+ except Exception as e:
+ print(f"[StableGen] Error modifying image {local_path}: {e}. Uploading original.")
+ upload_path = local_path # Fallback to original on error
+ # --- End Image Modification ---
+
+ # Use the operator's instance cache variable (self._uploaded_images_cache)
+ if not hasattr(self, '_uploaded_images_cache') or self._uploaded_images_cache is None:
+ # Initialize cache if it doesn't exist (e.g., first call in execute)
+ # Although clearing in execute() is preferred
+ self._uploaded_images_cache = {}
+ print("[StableGen] Warning: _uploaded_images_cache not found, initializing. Should be cleared in execute().")
+
+
+ # Check cache first using the absolute local path as the key
+ absolute_local_path = os.path.abspath(upload_path)
+ cached_info = self._uploaded_images_cache.get(absolute_local_path)
+ if cached_info is not None: # Can be None if previous upload failed
+ # print(f"Debug: Using cached upload info for: {absolute_local_path}")
+ return cached_info # Return cached info (could be None if failed before)
+
+ # File exists locally? If not, we can't upload. Return None. Cache this result.
+ if not os.path.exists(absolute_local_path) or not os.path.isfile(absolute_local_path):
+ # print(f"Debug: Local file not found or not a file, cannot upload: {absolute_local_path}")
+ self._uploaded_images_cache[absolute_local_path] = None # Cache the fact that it's missing/invalid
+ return None
+
+ # Not cached and file exists, try to upload it
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ uploaded_info = upload_image_to_comfyui(server_address, absolute_local_path)
+
+ # Store result (the info dict or None if upload failed) in cache
+ self._uploaded_images_cache[absolute_local_path] = uploaded_info
+
+ # Clean up the temporary file after upload
+ if temp_image_path and os.path.exists(temp_image_path):
+ os.remove(temp_image_path)
+
+ if uploaded_info:
+ return uploaded_info
+ else:
+ # Upload failed, error message was printed by upload_image_to_comfyui
+ # Returning None allows optional inputs to be skipped gracefully.
+ # If a *required* image fails to upload, the workflow submission
+ # will likely fail later when ComfyUI can't find the input.
+ return None
+
+
+# ── Preview Gallery helpers ───────────────────────────────────────────
diff --git a/stablegen/texturing/orbit_export.py b/stablegen/texturing/orbit_export.py
new file mode 100644
index 0000000..beac761
--- /dev/null
+++ b/stablegen/texturing/orbit_export.py
@@ -0,0 +1,974 @@
+"""Orbit GIF/MP4 export operator."""
+
+import os
+import math
+import time
+import tempfile
+import shutil
+import bpy # pylint: disable=import-error
+import numpy as np
+import mathutils
+from ..utils import get_dir_path, get_eevee_engine_id, sg_modal_active
+import imageio
+import imageio_ffmpeg
+
+class ExportOrbitGIF(bpy.types.Operator):
+ """Exports a GIF and MP4 animation orbiting the active object"""
+ bl_idname = "object.export_orbit_gif"
+ bl_label = "Export Orbit GIF/MP4"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ duration: bpy.props.FloatProperty(
+ name="Duration (seconds)",
+ description="Duration of the 360-degree orbit",
+ default=5.0,
+ min=0.1,
+ max=60.0
+ ) # type: ignore
+
+ frame_rate: bpy.props.IntProperty(
+ name="Frame Rate (fps)",
+ description="Frames per second for the animation",
+ default=24,
+ min=1,
+ max=60
+ ) # type: ignore
+
+ resolution_percentage: bpy.props.IntProperty(
+ name="Resolution %",
+ description="Percentage of the scene's render resolution to use",
+ default=50,
+ min=10,
+ max=100,
+ subtype='PERCENTAGE'
+ ) # type: ignore
+
+ samples: bpy.props.IntProperty(
+ name="Samples",
+ description="Number of render samples per frame",
+ default=32,
+ min=1,
+ max=4096
+ ) # type: ignore
+
+ engine: bpy.props.EnumProperty(
+ name="Render Engine",
+ description="Render engine to use",
+ items=[
+ ('BLENDER_WORKBENCH', "Workbench", "Use Workbench render engine"),
+ ('EEVEE', "Eevee", "Use Eevee render engine"),
+ ('CYCLES', "Cycles", "Use Cycles render engine")
+ ],
+ default='CYCLES'
+ ) # type: ignore
+
+ interpolation: bpy.props.EnumProperty(
+ name="Rotation Curve",
+ description="Keyframe interpolation for the orbit rotation",
+ items=[
+ ('LINEAR', "Linear", "Constant speed throughout the orbit"),
+ ('BEZIER', "Ease In/Out", "Smooth acceleration and deceleration"),
+ ],
+ default='LINEAR'
+ ) # type: ignore
+
+ # ── PBR showcase settings ─────────────────────────────────────────
+ use_hdri: bpy.props.BoolProperty(
+ name="HDRI Environment",
+ description="Use the scene's existing HDRI world (set up via the "
+ "Add HDRI operator) for realistic environment lighting",
+ default=False
+ ) # type: ignore
+
+ hdri_rotation: bpy.props.FloatProperty(
+ name="Environment Rotation",
+ description="Initial Z-rotation offset for the HDRI (degrees)",
+ default=0.0,
+ min=0.0,
+ max=360.0,
+ subtype='ANGLE'
+ ) # type: ignore
+
+ env_mode: bpy.props.EnumProperty(
+ name="Environment Mode",
+ description="How the environment lighting interacts with the orbit",
+ items=[
+ ('FIXED', "Fixed",
+ "Camera orbits, HDRI stays fixed — reflections shift naturally"),
+ ('LOCKED', "Locked",
+ "HDRI co-rotates with the camera — lighting stays identical "
+ "from every angle (consistent showcase)"),
+ ('COUNTER', "Counter-Rotate",
+ "HDRI rotates opposite to camera — 2× apparent light change"),
+ ('ENV_ONLY', "Environment Only",
+ "Camera stays fixed, only the HDRI rotates around the model"),
+ ],
+ default='FIXED'
+ ) # type: ignore
+
+ use_denoiser: bpy.props.BoolProperty(
+ name="Denoise",
+ description="Enable Cycles denoising for cleaner specular "
+ "highlights with fewer samples",
+ default=True
+ ) # type: ignore
+
+ use_gpu: bpy.props.BoolProperty(
+ name="GPU Compute",
+ description="Use GPU for Cycles rendering (Blender 5.1+ only). "
+ "Disable to force CPU rendering",
+ default=True
+ ) # type: ignore
+
+ filename_suffix: bpy.props.StringProperty(
+ name="Filename Suffix",
+ description="Internal: appended to the output filename (e.g. '_no_pbr')",
+ default="",
+ options={'HIDDEN'}
+ ) # type: ignore
+
+ _timer = None
+ _rendering = False
+ _cancelled = False # Added flag to track cancellation
+ _handle_complete = None
+ _handle_cancel = None
+ _initial_settings = {}
+ _temp_empty = None # Added for the pivot empty
+ _env_mapping_node = None # For HDRI rotation animation
+ _original_world_data = None # Store entire world setup for restore
+ _output_path = "" # Internal variable to store the final GIF path
+ _output_path_mp4 = "" # Internal variable to store the final MP4 path
+ _temp_dir = "" # Temporary directory for frames
+ _frame_paths = [] # List to store paths of rendered frames
+ _original_camera_parent = None # Store original camera parent
+ _original_camera_matrix = None # Store original camera matrix
+ _original_compute_device_type = None # For GPU/CPU toggle restore
+
+ @classmethod
+ def poll(cls, context):
+ try:
+ import imageio
+ except ImportError:
+ cls.poll_message_set("Python module 'imageio' not installed")
+ return False
+ if context.active_object is None or context.active_object.type not in {'MESH', 'EMPTY'}:
+ cls.poll_message_set("Select a mesh or empty object first")
+ return False
+ if context.scene.camera is None:
+ cls.poll_message_set("No active camera in the scene")
+ return False
+ if sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ return True
+
+ def invoke(self, context, event):
+ # Check dependency again in invoke to provide feedback
+ try:
+ import imageio
+ # Check if ffmpeg is likely available for MP4 export
+ if not imageio.plugins.ffmpeg.is_available():
+ self.report({'WARNING'}, "FFmpeg plugin for imageio not found. MP4 export might fail. Install 'imageio-ffmpeg'.")
+ except ImportError:
+ self.report({'ERROR'}, "Python module 'imageio' not found. Please install it (e.g., 'pip install imageio imageio-ffmpeg').")
+ return {'CANCELLED'}
+ except Exception as e:
+ self.report({'WARNING'}, f"Could not check imageio ffmpeg availability: {e}. MP4 export might fail.")
+
+
+ # Check for active camera
+ if not context.scene.camera:
+ self.report({'ERROR'}, "No active camera found in the scene.")
+ return {'CANCELLED'}
+
+ # Determine output paths using get_dir_path
+ try:
+ revision_dir = get_dir_path(context, "revision")
+ os.makedirs(revision_dir, exist_ok=True)
+ suffix = self.filename_suffix or ""
+ self._output_path = os.path.join(revision_dir, f"orbit{suffix}.gif")
+ self._output_path_mp4 = os.path.join(revision_dir, f"orbit{suffix}.mp4") # MP4 path
+ except Exception as e:
+ self.report({'ERROR'}, f"Could not determine output directory: {e}")
+ return {'CANCELLED'}
+
+ # Create temporary directory for frames
+ try:
+ self._temp_dir = tempfile.mkdtemp(prefix="blender_gif_")
+ except Exception as e:
+ self.report({'ERROR'}, f"Could not create temporary directory: {e}")
+ self.cleanup(context) # Clean up if temp dir fails
+ return {'CANCELLED'}
+
+ return context.window_manager.invoke_props_dialog(self)
+
+ @staticmethod
+ def _force_fcurve_interpolation(obj, interpolation_type):
+ """Set interpolation on all keyframe points of *obj*'s fcurves.
+
+ Works on Blender 4.x (action.fcurves) and 5.x+ (layered actions
+ with slots / channelbags).
+ """
+ anim = obj.animation_data
+ if not anim or not anim.action:
+ return
+
+ action = anim.action
+ fcurves = []
+
+ # Blender 5.x layered-action API (slots → channelbags → fcurves)
+ action_slot = getattr(anim, "action_slot", None)
+ if action_slot is not None:
+ # Try the helper shipped in bpy_extras first
+ try:
+ from bpy_extras.anim_utils import action_get_channelbag_for_slot
+ bag = action_get_channelbag_for_slot(action, action_slot)
+ if bag is not None:
+ fcurves = list(bag.fcurves)
+ except Exception:
+ pass
+ # Manual fallback: iterate channelbags on the *action*
+ if not fcurves:
+ for attr in ("channelbags", "channel_bags"):
+ bags = getattr(action, attr, None)
+ if bags is not None:
+ for bag in bags:
+ fcurves.extend(bag.fcurves)
+ break
+
+ # Blender 4.x legacy path
+ if not fcurves and hasattr(action, "fcurves"):
+ fcurves = list(action.fcurves)
+
+ for fc in fcurves:
+ for kf in fc.keyframe_points:
+ kf.interpolation = interpolation_type
+
+ # ── PBR Showcase helpers ──────────────────────────────────────────
+
+ def _setup_hdri_environment(self, context):
+ """Prepare the scene's existing HDRI world for orbit rendering.
+
+ Assumes the world was already set up by the AddHDRI operator
+ (or any setup with an Environment Texture node). This method:
+
+ 1. Stores the original world state for ``cleanup()``.
+ 2. Injects a Mapping node before the Environment Texture to
+ allow rotation animation (counter-rotate / env-only modes).
+ 3. Applies the user's initial rotation offset.
+
+ If no Environment Texture node is found, falls back to creating
+ a procedural Nishita sky with a Mapping node.
+ """
+ world = context.scene.world
+ if not world:
+ world = bpy.data.worlds.new("SG_OrbitWorld")
+ context.scene.world = world
+
+ # Store original state for restore
+ self._original_world_data = {
+ 'world_ref': world,
+ 'use_nodes': world.use_nodes,
+ 'color': world.color.copy(),
+ }
+
+ world.use_nodes = True
+ tree = world.node_tree
+
+ # Find the existing Environment Texture node
+ env_tex = None
+ for node in tree.nodes:
+ if node.type == 'TEX_ENVIRONMENT':
+ env_tex = node
+ break
+
+ if env_tex is None:
+ # No HDRI world set up — create a *separate* procedural sky world
+ # so the original world node tree is never destroyed.
+ print("[StableGen] Orbit GIF: No HDRI found, creating sky fallback "
+ "(original world preserved)")
+ fallback_world = bpy.data.worlds.new("SG_OrbitSkyFallback")
+ fallback_world.use_nodes = True
+ fb_tree = fallback_world.node_tree
+ fb_tree.nodes.clear()
+
+ tex_coord = fb_tree.nodes.new("ShaderNodeTexCoord")
+ tex_coord.location = (-800, 300)
+
+ mapping = fb_tree.nodes.new("ShaderNodeMapping")
+ mapping.location = (-600, 300)
+ mapping.inputs["Rotation"].default_value[2] = self.hdri_rotation
+ self._env_mapping_node = mapping
+
+ sky_tex = fb_tree.nodes.new("ShaderNodeTexSky")
+ sky_tex.location = (-300, 300)
+ # Set sky type — Blender 5.x replaced 'NISHITA' with new names
+ for sky_type in ('NISHITA', 'HOSEK_WILKIE', 'PREETHAM'):
+ try:
+ sky_tex.sky_type = sky_type
+ break
+ except TypeError:
+ continue
+ # Sun attributes vary by sky type / version
+ try:
+ sky_tex.sun_elevation = math.radians(30)
+ sky_tex.sun_rotation = math.radians(45)
+ except AttributeError:
+ pass
+
+ bg_node = fb_tree.nodes.new("ShaderNodeBackground")
+ bg_node.location = (0, 300)
+
+ output_node = fb_tree.nodes.new("ShaderNodeOutputWorld")
+ output_node.location = (200, 300)
+
+ fb_tree.links.new(tex_coord.outputs["Generated"],
+ mapping.inputs["Vector"])
+ fb_tree.links.new(mapping.outputs["Vector"],
+ sky_tex.inputs["Vector"])
+ fb_tree.links.new(sky_tex.outputs["Color"],
+ bg_node.inputs["Color"])
+ fb_tree.links.new(bg_node.outputs["Background"],
+ output_node.inputs["Surface"])
+
+ # Swap the scene world to the fallback; original is stored for restore
+ context.scene.world = fallback_world
+ self._original_world_data['created_fallback'] = True
+ self._original_world_data['fallback_world'] = fallback_world
+ return
+
+ # ── Inject Mapping node before the Environment Texture ────────
+ # Store original Vector input link/socket for restore
+ existing_vec_link = None
+ if env_tex.inputs["Vector"].links:
+ existing_vec_link = env_tex.inputs["Vector"].links[0]
+ self._original_world_data['env_vec_from_socket'] = (
+ existing_vec_link.from_socket)
+
+ # Create TexCoord + Mapping chain
+ tex_coord = tree.nodes.new("ShaderNodeTexCoord")
+ tex_coord.name = "SG_OrbitTexCoord"
+ tex_coord.location = (env_tex.location[0] - 500,
+ env_tex.location[1])
+
+ mapping = tree.nodes.new("ShaderNodeMapping")
+ mapping.name = "SG_OrbitMapping"
+ mapping.location = (env_tex.location[0] - 250,
+ env_tex.location[1])
+ mapping.inputs["Rotation"].default_value[2] = self.hdri_rotation
+ self._env_mapping_node = mapping
+
+ tree.links.new(tex_coord.outputs["Generated"],
+ mapping.inputs["Vector"])
+
+ # Remove existing vector link and insert mapping before env tex
+ if existing_vec_link:
+ # Rewire: old_source → Mapping.Vector, Mapping.out → EnvTex.Vector
+ old_source = existing_vec_link.from_socket
+ tree.links.remove(existing_vec_link)
+ tree.links.new(old_source, mapping.inputs["Vector"])
+ # else: TexCoord → Mapping is already connected above
+
+ tree.links.new(mapping.outputs["Vector"],
+ env_tex.inputs["Vector"])
+
+ print("[StableGen] Orbit GIF: using existing HDRI world"
+ f" (rotation offset={math.degrees(self.hdri_rotation):.0f}°)")
+
+ def _animate_hdri_rotation(self, context, total_frames):
+ """Animate the HDRI Mapping node rotation for counter-rotate
+ or environment-only modes."""
+ if not self._env_mapping_node:
+ return
+
+ mapping = self._env_mapping_node
+ prefs = bpy.context.preferences.edit
+ prev_interp = prefs.keyframe_new_interpolation_type
+ prefs.keyframe_new_interpolation_type = self.interpolation
+ try:
+ # Start rotation = current value (may include user offset)
+ start_z = mapping.inputs["Rotation"].default_value[2]
+ mapping.inputs["Rotation"].default_value[2] = start_z
+ mapping.inputs["Rotation"].keyframe_insert(
+ data_path="default_value", index=2, frame=1)
+
+ # For counter-rotate: rotate in opposite direction to camera
+ # For env-only: rotate in positive direction
+ if self.env_mode == 'COUNTER':
+ end_z = start_z - math.radians(360)
+ elif self.env_mode == 'LOCKED':
+ # Co-rotate: same direction as camera orbit (+Z)
+ end_z = start_z + math.radians(360)
+ else:
+ end_z = start_z + math.radians(360)
+
+ mapping.inputs["Rotation"].default_value[2] = end_z
+ mapping.inputs["Rotation"].keyframe_insert(
+ data_path="default_value", index=2, frame=total_frames + 1)
+ finally:
+ prefs.keyframe_new_interpolation_type = prev_interp
+
+ # Force interpolation on the world node tree's fcurves
+ world = context.scene.world
+ if world and world.node_tree:
+ self._force_fcurve_interpolation(
+ world.node_tree, self.interpolation)
+
+ def setup_animation(self, context):
+ obj = context.active_object
+ scene = context.scene
+ active_camera = scene.camera # Use the scene's active camera
+
+ if not active_camera: # Should be caught by poll/invoke, but double-check
+ raise RuntimeError("No active camera found during setup.")
+
+ # Store original camera state
+ self._original_camera_parent = active_camera.parent
+ self._original_camera_matrix = active_camera.matrix_world.copy()
+
+ # Calculate Center (Center of Mass or Bounds)
+ cursor_location = scene.cursor.location.copy()
+ # Use object bounds center for better visual centering
+ bpy.ops.object.select_all(action='DESELECT')
+ context.view_layer.objects.active = obj
+ obj.select_set(True)
+ # Use geometry center for pivot, less prone to being skewed by outliers than bounds
+ bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS')
+ center_location = obj.matrix_world.translation.copy()
+ # Restore cursor location (origin setting might change cursor)
+ scene.cursor.location = cursor_location
+
+ # Create Temporary Empty at Center
+ self._temp_empty = bpy.data.objects.new("OrbitPivot", None)
+ self._temp_empty.location = center_location
+ scene.collection.objects.link(self._temp_empty)
+ # Update matrices after linking and setting location
+ context.view_layer.update()
+
+ # Calculate camera's local transform relative to the empty *before* parenting
+ cam_original_world_matrix = self._original_camera_matrix.copy()
+ empty_world_matrix_inv = self._temp_empty.matrix_world.inverted()
+ cam_local_matrix = empty_world_matrix_inv @ cam_original_world_matrix
+
+ # Parent Active Camera to Empty
+ active_camera.parent = self._temp_empty
+ # Set the camera's local transform (matrix_basis)
+ active_camera.matrix_basis = cam_local_matrix
+
+ # Set Up Animation Timing
+ total_frames = int(self.duration * self.frame_rate)
+ scene.frame_start = 1
+ scene.frame_end = total_frames
+ scene.render.fps = self.frame_rate
+
+ # Animate Empty's Rotation (skip if ENV_ONLY — camera stays fixed)
+ if self.env_mode != 'ENV_ONLY':
+ # Set keyframe interpolation type before inserting so keyframes
+ # are created with the correct curve.
+ prefs = bpy.context.preferences.edit
+ prev_interp = prefs.keyframe_new_interpolation_type
+ prefs.keyframe_new_interpolation_type = self.interpolation
+ try:
+ self._temp_empty.rotation_euler = (0, 0, 0)
+ self._temp_empty.keyframe_insert(data_path="rotation_euler", index=2, frame=1)
+
+ self._temp_empty.rotation_euler = (0, 0, math.radians(360))
+ self._temp_empty.keyframe_insert(data_path="rotation_euler", index=2, frame=total_frames + 1)
+ finally:
+ prefs.keyframe_new_interpolation_type = prev_interp
+
+ # Post-insert: force the chosen interpolation on all keyframe points.
+ # The preference alone doesn't always take effect on Blender 5.x.
+ self._force_fcurve_interpolation(self._temp_empty, self.interpolation)
+
+ # Animate HDRI environment rotation (locked, counter-rotate, or env-only)
+ if self.use_hdri and self.env_mode in ('LOCKED', 'COUNTER', 'ENV_ONLY'):
+ self._animate_hdri_rotation(context, total_frames)
+
+
+ def execute(self, context):
+ scene = context.scene
+ render = scene.render
+ cycles = scene.cycles # Get cycles settings
+
+ # When called via EXEC_DEFAULT (e.g. from the queue) invoke()
+ # is skipped, so initialise paths / temp dir here if needed.
+ if not self._temp_dir:
+ try:
+ revision_dir = get_dir_path(context, "revision")
+ os.makedirs(revision_dir, exist_ok=True)
+ suffix = self.filename_suffix or ""
+ self._output_path = os.path.join(revision_dir, f"orbit{suffix}.gif")
+ self._output_path_mp4 = os.path.join(revision_dir, f"orbit{suffix}.mp4")
+ except Exception as e:
+ self.report({'ERROR'}, f"Could not determine output directory: {e}")
+ return {'CANCELLED'}
+ try:
+ self._temp_dir = tempfile.mkdtemp(prefix="blender_gif_")
+ except Exception as e:
+ self.report({'ERROR'}, f"Could not create temporary directory: {e}")
+ return {'CANCELLED'}
+
+ # Store Initial Settings
+ self._initial_settings = {
+ 'frame_start': scene.frame_start,
+ 'frame_end': scene.frame_end,
+ 'fps': render.fps,
+ 'camera': scene.camera, # Store the camera object itself
+ 'filepath': render.filepath,
+ 'file_format': render.image_settings.file_format,
+ 'color_mode': render.image_settings.color_mode,
+ 'resolution_x': render.resolution_x,
+ 'resolution_y': render.resolution_y,
+ 'resolution_percentage': render.resolution_percentage,
+ 'use_overwrite': render.use_overwrite,
+ 'use_placeholder': render.use_placeholder,
+ 'samples': cycles.samples, # Store original samples
+ 'engine': scene.render.engine, # Store original engine
+ 'film_transparent': render.film_transparent, # Store original film transparency
+ 'light': scene.display.shading.light, # Store original shading light
+ 'color_type': scene.display.shading.color_type, # Store original shading color type
+ 'use_compositing': render.use_compositing, # Store compositor pipeline state
+ }
+
+ scene.render.engine = get_eevee_engine_id() if self.engine == 'EEVEE' else self.engine
+
+ if scene.render.engine == 'BLENDER_WORKBENCH':
+ context.scene.display.shading.light = 'STUDIO'
+ context.scene.display.shading.color_type = 'SINGLE'
+
+ # Disable compositing in the render pipeline so Blender writes
+ # the raw ViewLayer result to render.filepath. StableGen's
+ # compositor uses File Output nodes without a Composite node,
+ # which causes the Composite output (and thus saved frames) to be
+ # blank/transparent.
+ render.use_compositing = False
+
+ # Apply Render Settings for PNG sequence
+ # Use a fixed 1024×1024 base so GIF resolution is independent of
+ # whatever the generation pipeline left in resolution_x/y.
+ render.resolution_x = 1024
+ render.resolution_y = 1024
+ render.filepath = os.path.join(self._temp_dir, "frame_") # Base path for frames
+ render.image_settings.file_format = 'PNG' # Render as PNG sequence
+ render.image_settings.color_mode = 'RGBA' # Use RGBA (needed for potential alpha, even if we make background opaque)
+ render.resolution_percentage = self.resolution_percentage
+ render.use_overwrite = True
+ render.use_placeholder = False
+ render.film_transparent = True
+ cycles.samples = self.samples # Set samples for rendering
+
+ # ── GPU / CPU Compute (Blender 5.1+) ───────────────────────────
+ if bpy.app.version >= (5, 1, 0) and scene.render.engine == 'CYCLES':
+ try:
+ cycles_prefs = bpy.context.preferences.addons['cycles'].preferences
+ self._original_compute_device_type = cycles_prefs.compute_device_type
+ self._initial_settings['cycles_device'] = scene.cycles.device
+ if self.use_gpu:
+ # Probe GPU backends, pick the first that has devices
+ for backend in ('CUDA', 'OPTIX', 'HIP', 'METAL', 'ONEAPI'):
+ try:
+ cycles_prefs.compute_device_type = backend
+ cycles_prefs.get_devices()
+ gpu_devs = [d for d in cycles_prefs.devices if d.type == backend]
+ if gpu_devs:
+ for d in gpu_devs:
+ d.use = True
+ scene.cycles.device = 'GPU'
+ print(f"[GIF Export] Using {backend} GPU: "
+ f"{', '.join(d.name for d in gpu_devs)}")
+ break
+ except Exception:
+ continue
+ else:
+ # No GPU found, fall back to CPU
+ cycles_prefs.compute_device_type = 'NONE'
+ scene.cycles.device = 'CPU'
+ print("[GIF Export] No GPU backend found, falling back to CPU")
+ else:
+ cycles_prefs.compute_device_type = 'NONE'
+ scene.cycles.device = 'CPU'
+ except Exception as e:
+ print(f"[GIF Export] Could not configure compute device: {e}")
+ self._original_compute_device_type = None
+
+ # ── PBR Showcase: HDRI Environment ────────────────────────────
+ if self.use_hdri:
+ self._setup_hdri_environment(context)
+
+ # ── PBR Showcase: Cycles Denoiser ─────────────────────────────
+ if self.use_denoiser and scene.render.engine == 'CYCLES':
+ self._initial_settings['use_denoising'] = cycles.use_denoising
+ self._initial_settings['denoiser'] = cycles.denoiser
+ cycles.use_denoising = True
+ # Prefer OpenImageDenoise (CPU-based, always available)
+ try:
+ cycles.denoiser = 'OPENIMAGEDENOISE'
+ except TypeError:
+ pass # Fallback to whatever is default
+
+ # Setup Animation
+ try:
+ self.setup_animation(context)
+ except Exception as e:
+ # Clean up partially created objects if setup fails
+ self.cleanup(context)
+ self.report({'ERROR'}, f"Animation setup failed: {e}")
+ # Print traceback for debugging
+ import traceback
+ traceback.print_exc()
+ return {'CANCELLED'}
+
+ # Start Rendering
+ self._rendering = True
+ ExportOrbitGIF._rendering = True # class-level flag for external polling
+ self._cancelled = False # Reset cancellation flag
+ self._frame_paths.clear() # Clear list for new render
+ self._handle_complete = bpy.app.handlers.render_complete.append(self.render_complete_handler)
+ self._handle_cancel = bpy.app.handlers.render_cancel.append(self.render_cancel_handler)
+ # Add handler for each rendered frame to collect paths
+ bpy.app.handlers.render_post.append(self.render_post_handler)
+
+
+ # Use timer to check render status without blocking UI completely
+ self._timer = context.window_manager.event_timer_add(0.5, window=context.window)
+ context.window_manager.modal_handler_add(self)
+
+ # Start the render process
+ bpy.ops.render.render('INVOKE_DEFAULT', animation=True)
+
+ self.report({'INFO'}, f"Rendering frames to temporary directory...")
+ return {'RUNNING_MODAL'}
+
+ def modal(self, context, event):
+ if event.type == 'TIMER':
+ if not self._rendering:
+ # Render finished or cancelled
+ context.window_manager.event_timer_remove(self._timer)
+
+ files_created = False
+ # Check if frames exist AND it wasn't cancelled before trying to create files
+ if self._frame_paths and not self._cancelled:
+ files_created = self.create_output_files() # Call create_output_files
+
+ # Perform cleanup regardless
+ self.cleanup(context)
+
+ # Check for error AFTER cleanup, using the cancellation flag
+ if not files_created and not self._cancelled: # Use self._cancelled flag
+ self.report({'ERROR'}, "No frames rendered or collected, or output file creation failed.")
+ elif files_created: # Report success only if files were made
+ self.report({'INFO'}, f"GIF saved to {self._output_path}, MP4 saved to {self._output_path_mp4}")
+
+
+ return {'FINISHED'}
+ # Allow render window events to pass through
+ return {'PASS_THROUGH'}
+
+
+ def cleanup(self, context):
+ # Remove handlers first to prevent them running during cleanup
+ if self._handle_complete in bpy.app.handlers.render_complete:
+ bpy.app.handlers.render_complete.remove(self._handle_complete)
+ if self._handle_cancel in bpy.app.handlers.render_cancel:
+ bpy.app.handlers.render_cancel.remove(self._handle_cancel)
+ if self.render_post_handler in bpy.app.handlers.render_post:
+ bpy.app.handlers.render_post.remove(self.render_post_handler)
+ self._handle_complete = None
+ self._handle_cancel = None
+
+ # Restore initial settings
+ scene = context.scene
+ render = scene.render
+ cycles = scene.cycles # Get cycles settings
+ original_camera = self._initial_settings.get('camera')
+
+ # Restore camera parent and transform BEFORE restoring scene.camera setting
+ if original_camera and original_camera.name in bpy.data.objects:
+ try:
+ # Check if it's still parented to the temp empty before unparenting
+ if original_camera.parent == self._temp_empty:
+ original_camera.parent = self._original_camera_parent
+ # Restore world matrix after unparenting
+ original_camera.matrix_world = self._original_camera_matrix
+ else:
+ # If parent changed unexpectedly, just restore matrix
+ print("[StableGen] Warning: Camera parent changed during render, restoring world matrix only.")
+ original_camera.matrix_world = self._original_camera_matrix
+ # Clear potentially stale parent inverse matrix
+ original_camera.matrix_parent_inverse.identity()
+
+ except Exception as e:
+ print(f"[StableGen] Warning: Could not restore camera state for '{original_camera.name}': {e}")
+
+ # Restore other settings
+ for key, value in self._initial_settings.items():
+ if key == 'camera':
+ # Restore the scene's active camera object if it exists
+ if value and value.name in bpy.data.objects:
+ scene.camera = value
+ continue # Already handled transform/parent above
+ try:
+ # Handle nested properties correctly
+ if key == 'file_format':
+ setattr(render.image_settings, 'file_format', value)
+ elif key == 'color_mode':
+ setattr(render.image_settings, 'color_mode', value)
+ elif key in ('samples', 'use_denoising', 'denoiser'):
+ setattr(cycles, key, value)
+ elif key == 'cycles_device':
+ cycles.device = value
+ elif key == 'engine': # Restore engine
+ setattr(render, key, value)
+ elif key == 'film_transparent': # Restore film transparency
+ setattr(render, key, value)
+ elif key == 'use_compositing': # Restore compositor pipeline state
+ render.use_compositing = value
+ elif key in ('resolution_x', 'resolution_y'):
+ setattr(render, key, value)
+ elif hasattr(render, key):
+ setattr(render, key, value)
+ elif hasattr(scene, key):
+ setattr(scene, key, value)
+ except Exception as e:
+ print(f"[StableGen] Warning: Could not restore setting '{key}': {e}")
+
+ self._initial_settings = {} # Clear stored settings
+ self._original_camera_parent = None
+ self._original_camera_matrix = None
+
+ # Restore GPU/CPU compute device type (Blender 5.1+)
+ if self._original_compute_device_type is not None:
+ try:
+ cycles_prefs = bpy.context.preferences.addons['cycles'].preferences
+ cycles_prefs.compute_device_type = self._original_compute_device_type
+ except Exception as e:
+ print(f"[StableGen] Warning: Could not restore compute device type: {e}")
+ self._original_compute_device_type = None
+
+ # Remove temporary empty
+ if self._temp_empty:
+ # Remove animation data
+ if self._temp_empty.animation_data and self._temp_empty.animation_data.action:
+ action = self._temp_empty.animation_data.action
+ # Check if action exists before removing
+ if action and action.name in bpy.data.actions:
+ bpy.data.actions.remove(action)
+ # Unlink and remove object
+ if self._temp_empty.name in context.scene.collection.objects:
+ context.scene.collection.objects.unlink(self._temp_empty)
+ if self._temp_empty.name in bpy.data.objects:
+ bpy.data.objects.remove(self._temp_empty, do_unlink=True)
+ self._temp_empty = None
+
+ # Restore the world environment (remove injected mapping nodes)
+ if self._original_world_data:
+ try:
+ world = self._original_world_data['world_ref']
+
+ if self._original_world_data.get('created_fallback'):
+ # We swapped in a separate fallback world — restore original
+ fallback_world = self._original_world_data.get(
+ 'fallback_world')
+
+ # Clean up animation data on the fallback world
+ if (fallback_world and fallback_world.node_tree
+ and fallback_world.node_tree.animation_data):
+ action = fallback_world.node_tree.animation_data.action
+ if action and action.name in bpy.data.actions:
+ bpy.data.actions.remove(action)
+ fallback_world.node_tree.animation_data_clear()
+
+ # Restore the original world
+ if world and world.name in bpy.data.worlds:
+ bpy.context.scene.world = world
+ # Remove the temporary fallback world datablock
+ if (fallback_world
+ and fallback_world.name in bpy.data.worlds):
+ bpy.data.worlds.remove(fallback_world)
+ elif world and world.name in bpy.data.worlds:
+ # Remove HDRI animation keyframes on the original world
+ if world.node_tree and world.node_tree.animation_data:
+ action = world.node_tree.animation_data.action
+ if action and action.name in bpy.data.actions:
+ bpy.data.actions.remove(action)
+ world.node_tree.animation_data_clear()
+
+ # We injected SG_OrbitMapping + SG_OrbitTexCoord
+ # into the existing tree. Remove them and restore
+ # the original vector link.
+ tree = world.node_tree
+ if tree:
+ # Find the env tex and restore its vector input
+ env_tex = None
+ for node in tree.nodes:
+ if node.type == 'TEX_ENVIRONMENT':
+ env_tex = node
+ break
+
+ orig_socket = self._original_world_data.get(
+ 'env_vec_from_socket')
+ if env_tex and orig_socket:
+ tree.links.new(orig_socket,
+ env_tex.inputs["Vector"])
+ elif env_tex:
+ # Had no vector link originally — just remove
+ for link in list(env_tex.inputs["Vector"].links):
+ tree.links.remove(link)
+
+ # Remove injected nodes
+ for name in ("SG_OrbitMapping", "SG_OrbitTexCoord"):
+ if name in tree.nodes:
+ tree.nodes.remove(tree.nodes[name])
+ except Exception as e:
+ print(f"[StableGen] Warning: Could not restore world environment: {e}")
+ self._original_world_data = None
+ self._env_mapping_node = None
+
+
+ self._rendering = False # Ensure rendering flag is reset
+ ExportOrbitGIF._rendering = False # class-level flag for external polling
+ self._cancelled = False # Reset cancellation flag
+
+ # Clean up temporary directory if it exists
+ if self._temp_dir and os.path.exists(self._temp_dir):
+ try:
+ shutil.rmtree(self._temp_dir)
+ print(f"[StableGen] Removed temporary directory: {self._temp_dir}")
+ except Exception as e:
+ print(f"[StableGen] Warning: Could not remove temporary directory '{self._temp_dir}': {e}")
+ self._temp_dir = ""
+ self._frame_paths.clear() # Clear frame paths
+
+
+ def render_post_handler(self, scene, _):
+ """Called after each frame is rendered."""
+ if self._rendering and self._temp_dir: # Check temp_dir exists
+ frame_num = scene.frame_current
+ # Construct the expected filename based on Blender's padding
+ filename = f"frame_{frame_num:04d}.png"
+ filepath = os.path.join(self._temp_dir, filename)
+ if os.path.exists(filepath):
+ self._frame_paths.append(filepath)
+ else:
+ # Check if render path uses frame number suffix differently
+ # Use scene.render.frame_path() which respects output settings
+ alt_filepath = scene.render.frame_path(frame=frame_num)
+ if os.path.exists(alt_filepath):
+ self._frame_paths.append(alt_filepath)
+ # print(f"Used alternative frame path: {alt_filepath}") # Less verbose
+ else:
+ print(f"[StableGen] Warning: Frame file not found after render: {filepath} or {alt_filepath}")
+
+
+ def render_complete_handler(self, scene, _):
+ try:
+ # Only respond if we're still in the rendering phase
+ if not getattr(self, "_rendering", False):
+ return
+
+ print("[StableGen] Frame rendering complete.")
+ # Signal that rendering is done so modal can wrap up
+ self._rendering = False
+
+ # Unregister this handler so it won't fire after operator finishes
+ # Check if handler exists before removing
+ if hasattr(self, 'render_complete_handler') and self.render_complete_handler in bpy.app.handlers.render_complete:
+ try:
+ bpy.app.handlers.render_complete.remove(self.render_complete_handler)
+ except ValueError:
+ pass # Ignore if already removed elsewhere
+ except ReferenceError:
+ # RNA is gone ignore
+ pass
+ except Exception as e:
+ print(f"[StableGen] Error in render_complete_handler: {e}") # Log other potential errors
+
+
+ def render_cancel_handler(self, scene, _):
+ if self._rendering: # Check if it was our render job that was cancelled
+ self.report({'WARNING'}, "Render cancelled by user.")
+ self._cancelled = True # Set the cancellation flag
+ self._rendering = False # Signal modal loop to finish and cleanup
+ # Unregister this handler
+ if hasattr(self, 'render_cancel_handler') and self.render_cancel_handler in bpy.app.handlers.render_cancel:
+ try:
+ bpy.app.handlers.render_cancel.remove(self.render_cancel_handler)
+ except ValueError:
+ pass # Ignore if already removed
+
+
+ def create_output_files(self):
+ """Creates the GIF and MP4 from the rendered frames using imageio. Returns True on success, False otherwise."""
+ if not self._frame_paths:
+ # Error is reported in modal loop if needed
+ return False
+
+ print(f"[StableGen] Assembling output files from {len(self._frame_paths)} frames...")
+ gif_success = False
+ mp4_success = False
+
+ try:
+ # Sort frames numerically just in case paths weren't added perfectly in order
+ self._frame_paths.sort()
+
+ images = []
+ for filename in self._frame_paths:
+ try:
+ images.append(imageio.imread(filename))
+ except FileNotFoundError:
+ print(f"[StableGen] Warning: Frame file disappeared before processing: {filename}")
+ continue # Skip missing frame
+
+ if not images: # Check if any images were actually loaded
+ self.report({'ERROR'}, "No valid frame images found to create output files.")
+ return False
+
+ # Calculate duration per frame for imageio (in seconds)
+ frame_duration = 1.0 / self.frame_rate
+
+ # --- Create GIF ---
+ print(f"[StableGen] Creating GIF: {self._output_path}")
+ try:
+ # imageio v3+ uses 'duration' in seconds per frame
+ imageio.mimsave(self._output_path, images, fps=self.frame_rate, loop=0, disposal=2) # loop=0 means infinite loop
+ print(f"[StableGen] GIF saved successfully.")
+ gif_success = True
+ except TypeError:
+ # Fallback if 'duration' is not accepted
+ imageio.mimsave(self._output_path, images, duration=frame_duration, loop=0)
+ print(f"[StableGen] GIF saved successfully (fallback duration).")
+ gif_success = True
+ except Exception as e:
+ self.report({'ERROR'}, f"Failed to create GIF: {e}")
+ # Print traceback for debugging GIF errors
+ import traceback
+ traceback.print_exc()
+
+ # --- Create MP4 ---
+ print(f"[StableGen] Creating MP4: {self._output_path_mp4}")
+ try:
+ imageio.mimsave(self._output_path_mp4, images, format='mp4', fps=self.frame_rate, quality=8)
+ print(f"[StableGen] MP4 saved successfully.")
+ mp4_success = True
+ except ImportError:
+ self.report({'ERROR'}, "Python module 'imageio' or its 'ffmpeg' plugin not found/configured correctly. Cannot create MP4.")
+ except Exception as e:
+ self.report({'ERROR'}, f"Failed to create MP4: {e}")
+ # Print traceback for debugging MP4 errors
+ import traceback
+ traceback.print_exc()
+
+
+ return gif_success and mp4_success # Return True only if both succeed
+
+ except ImportError:
+ # This top-level catch handles if imageio itself wasn't imported initially
+ self.report({'ERROR'}, "Python module 'imageio' not found. Cannot create output files.")
+ return False
+ except Exception as e:
+ # Catch other potential errors during image loading or sorting
+ self.report({'ERROR'}, f"Failed during output file creation process: {e}")
+ import traceback
+ traceback.print_exc()
+ return False
diff --git a/stablegen/texturing/pbr.py b/stablegen/texturing/pbr.py
new file mode 100644
index 0000000..0ee424c
--- /dev/null
+++ b/stablegen/texturing/pbr.py
@@ -0,0 +1,1469 @@
+"""PBR decomposition pipeline — extracted as a mixin for ComfyUIGenerate.
+
+The `_PBRMixin` class provides all PBR-related instance methods.
+`ComfyUIGenerate` inherits from this mixin so existing `self.xxx()`
+call sites remain unchanged.
+"""
+
+import os
+import json
+import io
+import math
+import colorsys
+import hashlib
+import traceback
+from statistics import median
+import numpy as np
+import cv2
+from PIL import Image, ImageEnhance
+import bpy # pylint: disable=import-error
+from ..utils import get_file_path, get_dir_path, get_generation_dirs
+from ..timeout_config import get_timeout
+
+_ADDON_PKG = __package__.rsplit('.', 1)[0]
+
+
+class _PBRMixin:
+ """Mixin providing PBR decomposition methods to ComfyUIGenerate."""
+
+ @staticmethod
+ def _pbr_settings_hash(settings_dict):
+ """Return a short hex hash for a dict of PBR settings."""
+ import hashlib
+ raw = json.dumps(settings_dict, sort_keys=True, default=str)
+ return hashlib.md5(raw.encode()).hexdigest()[:12]
+
+ def _pbr_per_map_settings(self, context):
+ """Compute per-map settings dicts that affect each PBR output.
+
+ Returns ``{map_name: {setting_key: value, ...}}`` for every
+ map type. Two dicts compare equal iff the same generation would
+ produce identical output.
+
+ Tiling modes are resolved to effective booleans so that e.g.
+ ``selective`` (albedo-only) and ``custom`` with only albedo
+ enabled produce identical hashes for every map.
+ """
+ scene = context.scene
+ # ── Common model settings (affect all server-side outputs) ────
+ common = {
+ 'resolution': getattr(scene, 'pbr_processing_resolution', 768),
+ 'native_res': getattr(scene, 'pbr_use_native_resolution', True),
+ 'denoise_steps': getattr(scene, 'pbr_denoise_steps', 4),
+ 'ensemble_size': getattr(scene, 'pbr_ensemble_size', 1),
+ }
+
+ albedo_source = getattr(scene, 'pbr_albedo_source', 'delight')
+
+ # ── Resolve effective per-map tiling booleans ─────────────────
+ tile_mode = getattr(scene, 'pbr_tiling', 'off')
+ tile_grid = getattr(scene, 'pbr_tile_grid', 2)
+ tile_superres = getattr(scene, 'pbr_tile_superres', False)
+
+ tile_albedo = getattr(scene, 'pbr_tile_albedo', True) if tile_mode == 'custom' else (tile_mode in ('selective', 'all'))
+ tile_material = getattr(scene, 'pbr_tile_material', False) if tile_mode == 'custom' else (tile_mode == 'all')
+ tile_normal = getattr(scene, 'pbr_tile_normal', False) if tile_mode == 'custom' else (tile_mode == 'all')
+ tile_height = getattr(scene, 'pbr_tile_height', False) if tile_mode == 'custom' else (tile_mode == 'all')
+ tile_emission = getattr(scene, 'pbr_tile_emission', False) if tile_mode == 'custom' else False
+
+ def tiling_dict(is_tiled):
+ """Return tiling sub-dict; grid/superres only matter when tiled."""
+ if is_tiled:
+ return {'tiled': True, 'tile_grid': tile_grid,
+ 'tile_superres': tile_superres}
+ return {'tiled': False}
+
+ settings = {}
+
+ settings['albedo'] = {
+ **common,
+ **tiling_dict(tile_albedo),
+ 'source': albedo_source,
+ 'delight_strength': getattr(scene, 'pbr_delight_strength', 1.0) if albedo_source == 'delight' else None,
+ 'auto_saturation': getattr(scene, 'pbr_albedo_auto_saturation', False),
+ 'saturation_mode': getattr(scene, 'pbr_albedo_saturation_mode', 'MEDIAN') if getattr(scene, 'pbr_albedo_auto_saturation', False) else None,
+ }
+ settings['roughness'] = {
+ **common,
+ **tiling_dict(tile_material),
+ }
+ settings['metallic'] = {
+ **common,
+ **tiling_dict(tile_material),
+ }
+ settings['normal'] = {
+ **common,
+ **tiling_dict(tile_normal),
+ }
+ settings['height'] = {
+ **common,
+ **tiling_dict(tile_height),
+ }
+
+ emission_method = getattr(scene, 'pbr_emission_method', 'residual')
+ emission_base = {
+ 'method': emission_method,
+ 'threshold': getattr(scene, 'pbr_emission_threshold', 0.2),
+ }
+ if emission_method == 'hsv':
+ emission_base.update({
+ 'sat_min': getattr(scene, 'pbr_emission_saturation_min', 0.5),
+ 'val_min': getattr(scene, 'pbr_emission_value_min', 0.85),
+ 'bloom': getattr(scene, 'pbr_emission_bloom', 5.0),
+ })
+ else:
+ # Residual method uses a model pass
+ emission_base.update({
+ **common,
+ **tiling_dict(tile_emission),
+ })
+ settings['emission'] = emission_base
+
+ return settings
+
+ def _pbr_sidecar_path(self, context):
+ """Return the path for the single PBR settings sidecar JSON file."""
+ dirs = get_generation_dirs(context)
+ base_dir = dirs["pbr"]
+ material_suffix = f"-{self._material_id}" if self._material_id is not None else ""
+ return os.path.join(base_dir, f"pbr_settings{material_suffix}.json")
+
+ def _save_pbr_settings(self, context, camera_id):
+ """Write current per-map settings hashes for *camera_id* into
+ the shared sidecar JSON file (merges with existing data)."""
+ settings = self._pbr_per_map_settings(context)
+ hashes = {k: self._pbr_settings_hash(v) for k, v in settings.items()}
+ path = self._pbr_sidecar_path(context)
+ # Load existing data to merge
+ data = {}
+ if os.path.exists(path):
+ try:
+ with open(path, 'r') as f:
+ data = json.load(f)
+ except Exception:
+ pass
+ data[str(camera_id)] = hashes
+ try:
+ os.makedirs(os.path.dirname(path), exist_ok=True)
+ with open(path, 'w') as f:
+ json.dump(data, f, indent=1)
+ except Exception as e:
+ print(f"[StableGen] Warning: could not write PBR settings "
+ f"sidecar {path}: {e}")
+
+ def _load_pbr_settings(self, context, camera_id):
+ """Load stored per-map settings hashes for *camera_id* from the
+ shared sidecar JSON file.
+
+ Returns a dict ``{map_name: hash_string}`` or ``None`` if no
+ sidecar or camera entry exists.
+ """
+ path = self._pbr_sidecar_path(context)
+ if not os.path.exists(path):
+ return None
+ try:
+ with open(path, 'r') as f:
+ data = json.load(f)
+ return data.get(str(camera_id))
+ except Exception:
+ return None
+
+ def _find_existing_pbr_maps(self, context, camera_id):
+ """Check which enabled PBR maps already exist on disk for a camera.
+
+ Returns ``(existing, missing)`` where *existing* is a dict
+ ``{map_name: file_path}`` of maps already on disk **whose settings
+ hash matches the current configuration**, and *missing* is a set of
+ map names that are enabled but either not found or stale.
+ """
+ scene = context.scene
+ existing = {}
+ missing = set()
+
+ map_checks = []
+ if getattr(scene, 'pbr_map_albedo', True):
+ map_checks.append('albedo')
+ if getattr(scene, 'pbr_map_roughness', True):
+ map_checks.append('roughness')
+ if getattr(scene, 'pbr_map_metallic', True):
+ map_checks.append('metallic')
+ if getattr(scene, 'pbr_map_normal', True):
+ map_checks.append('normal')
+ if getattr(scene, 'pbr_map_height', False):
+ map_checks.append('height')
+ if getattr(scene, 'pbr_map_emission', False):
+ map_checks.append('emission')
+
+ if not map_checks:
+ return existing, missing
+
+ # Load stored hashes and compute current ones for comparison
+ stored_hashes = self._load_pbr_settings(context, camera_id)
+ current_settings = self._pbr_per_map_settings(context)
+ current_hashes = {k: self._pbr_settings_hash(v)
+ for k, v in current_settings.items()}
+
+ for map_name in map_checks:
+ path = get_file_path(
+ context, "pbr", subtype=map_name,
+ camera_id=camera_id, material_id=self._material_id
+ )
+ if os.path.exists(path):
+ # Check if settings have changed since last generation
+ if (stored_hashes is not None
+ and stored_hashes.get(map_name) == current_hashes.get(map_name)):
+ existing[map_name] = path
+ else:
+ # File exists but settings changed → stale, regenerate
+ missing.add(map_name)
+ else:
+ missing.add(map_name)
+
+ return existing, missing
+
+ # ── Tiled PBR processing ──────────────────────────────────────────
+
+ @staticmethod
+ def _create_tile_blend_mask(h, w, overlap_x, overlap_y,
+ fade_left, fade_right,
+ fade_top, fade_bottom):
+ """Return a float32 [H, W, 1] weight mask with cosine fade on inner edges.
+
+ Only the edges that border a neighbouring tile (indicated by the
+ ``fade_*`` booleans) get a cosine-weighted ramp.
+ ``overlap_x`` controls left/right ramp length,
+ ``overlap_y`` controls top/bottom ramp length.
+ Image-boundary edges stay at full weight (1.0).
+ """
+ mask = np.ones((h, w, 1), dtype=np.float32)
+
+ if overlap_x > 0:
+ ramp_x = np.linspace(0.0, np.pi, overlap_x, dtype=np.float32)
+ ramp_x = (1.0 - np.cos(ramp_x)) * 0.5 # 0 → 1 cosine ease
+ if fade_left and overlap_x <= w:
+ mask[:, :overlap_x, 0] *= ramp_x[np.newaxis, :]
+ if fade_right and overlap_x <= w:
+ mask[:, -overlap_x:, 0] *= ramp_x[np.newaxis, ::-1]
+
+ if overlap_y > 0:
+ ramp_y = np.linspace(0.0, np.pi, overlap_y, dtype=np.float32)
+ ramp_y = (1.0 - np.cos(ramp_y)) * 0.5
+ if fade_top and overlap_y <= h:
+ mask[:overlap_y, :, 0] *= ramp_y[:, np.newaxis]
+ if fade_bottom and overlap_y <= h:
+ mask[-overlap_y:, :, 0] *= ramp_y[::-1, np.newaxis]
+
+ return mask
+
+ def _process_model_tiled(self, context, image_path, model_name=None,
+ process_fn=None):
+ """Run a model on an N×N grid of overlapping tiles.
+
+ Each tile is **upscaled to the full image's longest edge** before
+ processing, so the model spends its full resolution budget on
+ only 1/N² of the spatial area — effectively N²× the detail.
+ The stitched output is at the **upscaled** resolution (~N× the
+ original), producing super-resolution PBR maps.
+
+ Args:
+ model_name: Passed to ``generate_pbr_maps()``.
+ process_fn: Optional callable ``(context, tile_path) → result``.
+ If given, called instead of ``generate_pbr_maps``.
+ May return ``bytes`` (single image) or ``list[bytes]``.
+
+ Returns the same ``list[bytes]`` as ``generate_pbr_maps()``.
+ """
+ import tempfile
+
+ OVERLAP = 64 # pixels each tile extends beyond its boundary
+ MIN_DIM = 256 # skip tiling if either dimension is too small
+
+ scene = context.scene
+ N = getattr(scene, 'pbr_tile_grid', 2)
+
+ src = Image.open(image_path)
+ W, H = src.size
+ longest = max(W, H)
+
+ if W < MIN_DIM or H < MIN_DIM:
+ src.close()
+ print(f"[StableGen] Image {W}×{H} too small for tiling, "
+ f"processing normally")
+ if process_fn is not None:
+ fb = process_fn(context, image_path)
+ return [fb] if isinstance(fb, bytes) else fb
+ return self.workflow_manager.generate_pbr_maps(
+ context, image_path, model_name=model_name)
+
+ # Compute tile boundaries in original-image coordinates
+ x_bounds = [W * c // N for c in range(N + 1)] # [0, W/N, 2W/N, …, W]
+ y_bounds = [H * r // N for r in range(N + 1)]
+
+ tile_info = [] # list of (l, t, r, b, fade_left, fade_right, fade_top, fade_bottom)
+ for row in range(N):
+ for col in range(N):
+ l = x_bounds[col] - (OVERLAP if col > 0 else 0)
+ r = x_bounds[col+1] + (OVERLAP if col < N - 1 else 0)
+ t = y_bounds[row] - (OVERLAP if row > 0 else 0)
+ b = y_bounds[row+1] + (OVERLAP if row < N - 1 else 0)
+ # Clamp to image bounds
+ l = max(l, 0); r = min(r, W)
+ t = max(t, 0); b = min(b, H)
+ tile_info.append((
+ l, t, r, b,
+ col > 0, # fade_left
+ col < N - 1, # fade_right
+ row > 0, # fade_top
+ row < N - 1, # fade_bottom
+ ))
+
+ total_tiles = len(tile_info)
+
+ # Uniform scale factor — upscale tiles so longest edge ≈ full image
+ sample_tw = tile_info[0][2] - tile_info[0][0]
+ sample_th = tile_info[0][3] - tile_info[0][1]
+ sample_longest = max(sample_tw, sample_th)
+ scale = longest / sample_longest if sample_longest < longest else 1.0
+
+ # ── Process each tile ─────────────────────────────────
+ all_tile_results = [] # list of list[bytes]
+ tile_up_sizes = [] # (up_w, up_h) per tile
+ tmp_dir = tempfile.gettempdir()
+
+ for i, (l, t, r, b, fl, fr, ft, fb) in enumerate(tile_info):
+ tw, th = r - l, b - t
+ tile_img = src.crop((l, t, r, b))
+
+ tile_longest = max(tw, th)
+ if tile_longest < longest:
+ up_w = int(round(tw * scale))
+ up_h = int(round(th * scale))
+ tile_img = tile_img.resize(
+ (up_w, up_h), Image.LANCZOS)
+ print(f"[StableGen] Tile {i+1}/{total_tiles}: "
+ f"{tw}×{th} → {up_w}×{up_h} "
+ f"region ({l},{t})→({r},{b})")
+ else:
+ up_w, up_h = tw, th
+ print(f"[StableGen] Tile {i+1}/{total_tiles}: "
+ f"{tw}×{th} region ({l},{t})→({r},{b})")
+ tile_up_sizes.append((up_w, up_h))
+
+ tile_path = os.path.join(tmp_dir, f"sg_tile_{i}.png")
+ tile_img.save(tile_path)
+
+ if process_fn is not None:
+ result = process_fn(context, tile_path)
+ # Normalise to list[bytes] (StableDelight returns bytes)
+ if isinstance(result, bytes):
+ result = [result]
+ else:
+ result = self.workflow_manager.generate_pbr_maps(
+ context, tile_path, model_name=model_name,
+ force_native_resolution=True)
+
+ try:
+ os.remove(tile_path)
+ except OSError:
+ pass
+
+ if isinstance(result, dict):
+ print(f"[StableGen] Tile {i+1} failed, falling back "
+ f"to full-image processing")
+ src.close()
+ if process_fn is not None:
+ fb = process_fn(context, image_path)
+ return [fb] if isinstance(fb, bytes) else fb
+ return self.workflow_manager.generate_pbr_maps(
+ context, image_path, model_name=model_name)
+
+ # Resize model output to expected upscaled tile dims
+ resized = []
+ for map_bytes in result:
+ map_img = Image.open(io.BytesIO(map_bytes))
+ if map_img.size != (up_w, up_h):
+ map_img = map_img.resize((up_w, up_h), Image.LANCZOS)
+ buf = io.BytesIO()
+ map_img.save(buf, format='PNG')
+ resized.append(buf.getvalue())
+ all_tile_results.append(resized)
+
+ src.close()
+
+ # ── Stitch tiles ──────────────────────────────────────
+ superres = getattr(scene, 'pbr_tile_superres', False)
+
+ if superres:
+ # Super-resolution: stitch at the upscaled tile size (~N× original)
+ out_W = int(round(W * scale))
+ out_H = int(round(H * scale))
+ scaled_overlap = int(round(OVERLAP * scale))
+ else:
+ # Original resolution: downscale tiles back before stitching
+ out_W, out_H = W, H
+ scaled_overlap = OVERLAP # overlap in original coords
+
+ num_maps = len(all_tile_results[0])
+ stitched = []
+
+ for map_idx in range(num_maps):
+ canvas = np.zeros((out_H, out_W, 3), dtype=np.float32)
+ weight = np.zeros((out_H, out_W, 1), dtype=np.float32)
+
+ for i, (l, t, r, b, fl, fr, ft, fb) in enumerate(tile_info):
+ tile_bytes = all_tile_results[i][map_idx]
+ tile_img = Image.open(io.BytesIO(tile_bytes)).convert('RGB')
+
+ if superres:
+ up_w, up_h = tile_up_sizes[i]
+ sl = int(round(l * scale))
+ st = int(round(t * scale))
+ sr = min(sl + up_w, out_W)
+ sb = min(st + up_h, out_H)
+ else:
+ # Downscale tile back to original crop dimensions
+ orig_w, orig_h = r - l, b - t
+ if tile_img.size != (orig_w, orig_h):
+ tile_img = tile_img.resize(
+ (orig_w, orig_h), Image.LANCZOS)
+ sl, st, sr, sb = l, t, r, b
+
+ tile_arr = np.asarray(tile_img, dtype=np.float32)
+ tile_arr = tile_arr[:sb - st, :sr - sl]
+
+ overlap_px = 2 * scaled_overlap
+ mask = self._create_tile_blend_mask(
+ sb - st, sr - sl,
+ overlap_x=overlap_px,
+ overlap_y=overlap_px,
+ fade_left=fl, fade_right=fr,
+ fade_top=ft, fade_bottom=fb)
+
+ canvas[st:sb, sl:sr] += tile_arr * mask
+ weight[st:sb, sl:sr] += mask
+
+ canvas /= np.maximum(weight, 1e-6)
+ canvas = np.clip(canvas, 0, 255).astype(np.uint8)
+
+ out_img = Image.fromarray(canvas, mode='RGB')
+ buf = io.BytesIO()
+ out_img.save(buf, format='PNG')
+ stitched.append(buf.getvalue())
+
+ sr_label = " (super-res)" if superres else ""
+ print(f"[StableGen] Stitched {N}×{N} output: {out_W}×{out_H}"
+ f"{sr_label}")
+
+ return stitched
+
+ def _convert_normals_cam_to_world(self, map_bytes, camera_id):
+ """Convert a Marigold camera-space normal map to world space.
+
+ Marigold normals are in camera space (X = right, Y = up,
+ Z = toward the camera). The PNG encodes them as
+ ``(N + 1) / 2``, mapping [-1, 1] to [0, 1].
+
+ This method:
+ 1. Decodes the PNG into an RGB float array.
+ 2. Converts [0, 1] → [-1, 1].
+ 3. Rotates each normal by the camera's world rotation
+ (Marigold OpenGL convention and Blender camera-local
+ convention agree: X-right, Y-up, Z-toward-viewer).
+ 4. Re-normalises and re-encodes to [0, 1] PNG bytes.
+
+ Returns PNG bytes, or ``None`` on failure.
+ """
+ try:
+ img = Image.open(io.BytesIO(map_bytes)).convert('RGB')
+ arr = np.asarray(img, dtype=np.float32) / 255.0 # [H, W, 3] in [0,1]
+
+ # Decode to [-1, 1]
+ normals = arr * 2.0 - 1.0 # [H, W, 3] (X, Y, Z) camera-space
+
+ # Marigold (OpenGL) convention:
+ # X = right, Y = up, Z = toward camera (out of screen)
+ # Blender camera-local convention:
+ # X = right, Y = up, Z = behind camera (camera looks -Z)
+ #
+ # These AGREE: a surface facing the camera has Z = +1 in
+ # both systems (+Z_local points toward the viewer).
+ # No axis flip is needed.
+
+ # Get camera rotation (camera-local → world)
+ cam = self._cameras[camera_id]
+ cam_rot = np.array(cam.matrix_world.to_3x3(), dtype=np.float32) # 3×3
+
+ # Rotate all normals: N_world = cam_rot @ N_local
+ # Reshape to [H*W, 3] for batch matrix multiply
+ H, W, _ = normals.shape
+ flat = normals.reshape(-1, 3) # [H*W, 3]
+ flat_world = (cam_rot @ flat.T).T # [H*W, 3]
+
+ # Re-normalise (avoid div-by-zero)
+ norms = np.linalg.norm(flat_world, axis=1, keepdims=True)
+ norms = np.maximum(norms, 1e-6)
+ flat_world /= norms
+
+ # Encode back to [0, 1] PNG
+ world_normals = flat_world.reshape(H, W, 3)
+ encoded = np.clip((world_normals + 1.0) / 2.0, 0.0, 1.0)
+ encoded_u8 = (encoded * 255.0).astype(np.uint8)
+
+ out_img = Image.fromarray(encoded_u8, mode='RGB')
+ buf = io.BytesIO()
+ out_img.save(buf, format='PNG')
+ print(f"[StableGen] Converted normal map to world space for camera {camera_id}")
+ return buf.getvalue()
+ except Exception as err:
+ print(f"[StableGen] Warning: camera→world normal conversion failed: {err}")
+ return None
+
+ # ── PBR post-processing helpers ───────────────────────────────────
+
+ @staticmethod
+ def _compute_image_mean_saturation(image_path_or_bytes, sample_limit=100_000):
+ """Compute the mean HSV saturation for a single image.
+
+ *image_path_or_bytes* can be a file path (str) or raw PNG bytes.
+ Skips very dark pixels whose saturation is unreliable.
+ Returns a float in [0, 1], or 0.0 on error.
+ """
+ import colorsys
+ from PIL import Image
+
+ try:
+ if isinstance(image_path_or_bytes, (str, bytes)) and isinstance(image_path_or_bytes, str):
+ img = Image.open(image_path_or_bytes).convert('RGB')
+ else:
+ img = Image.open(io.BytesIO(image_path_or_bytes)).convert('RGB')
+
+ pixels = list(img.getdata())
+ step = max(1, len(pixels) // sample_limit)
+ total_s, n = 0.0, 0
+ for i in range(0, len(pixels), step):
+ r, g, b = pixels[i]
+ _, s, v = colorsys.rgb_to_hsv(r / 255, g / 255, b / 255)
+ if v > 0.05:
+ total_s += s
+ n += 1
+ return total_s / n if n > 0 else 0.0
+ except Exception as err:
+ print(f"[StableGen] Warning: saturation computation failed: {err}")
+ return 0.0
+
+ def _compute_uniform_saturation(self, context, use_median=True):
+ """Compute a uniform auto-saturation ratio across ALL cameras.
+
+ Compares each camera's original rendered image against its raw
+ PBR albedo, computes per-camera ratios, then aggregates them
+ using the **median** (robust to outlier cameras) or arithmetic
+ **mean**, depending on *use_median*.
+
+ Returns a float multiplier (1.0 = no correction needed).
+ """
+ import statistics
+
+ ratios = []
+ num_cameras = len(self._cameras) if hasattr(self, '_cameras') else 0
+
+ for cam_idx in range(num_cameras):
+ original_path = get_file_path(
+ context, "generated", camera_id=cam_idx,
+ material_id=self._material_id)
+ raw_path = get_file_path(
+ context, "pbr", subtype="raw_albedo",
+ camera_id=cam_idx,
+ material_id=self._material_id)
+
+ if not os.path.exists(original_path) or not os.path.exists(raw_path):
+ continue
+
+ s_orig = self._compute_image_mean_saturation(original_path)
+ s_albedo = self._compute_image_mean_saturation(raw_path)
+
+ if s_albedo < 0.01:
+ continue # skip grayscale albedos
+
+ ratio = s_orig / s_albedo
+ ratio = max(0.5, min(ratio, 3.0))
+ ratios.append(ratio)
+
+ if not ratios:
+ return 1.0
+
+ if use_median:
+ agg = statistics.median(ratios)
+ label = "median"
+ else:
+ agg = sum(ratios) / len(ratios)
+ label = "mean"
+
+ print(f"[StableGen] Auto saturation: per-camera ratios = "
+ f"{[f'{r:.2f}' for r in ratios]}, {label} = {agg:.2f}")
+ return agg
+
+ def _apply_albedo_postprocessing_batch(self, context):
+ """Apply auto-saturation correction to all camera albedos.
+
+ Must be called AFTER decomposition saves raw files for all cameras.
+ By default computes the averaged saturation ratio across all cameras
+ for uniform correction. With per-camera mode, each camera gets its
+ own individual ratio.
+
+ When correction is disabled, restores raw (unprocessed) albedos
+ so that a previously corrected active file doesn't persist.
+ """
+ auto_on = getattr(context.scene, 'pbr_albedo_auto_saturation', True)
+
+ if not auto_on:
+ self._restore_raw_albedos(context)
+ return
+
+ sat_mode = getattr(context.scene, 'pbr_albedo_saturation_mode', 'MEDIAN')
+
+ if sat_mode == 'PER_CAMERA':
+ # Per-camera mode: individual ratio per camera
+ self._apply_per_camera_saturation(context)
+ else:
+ # Uniform mode (MEDIAN or MEAN)
+ effective_sat = self._compute_uniform_saturation(
+ context, use_median=(sat_mode == 'MEDIAN'))
+
+ if effective_sat == 1.0:
+ print("[StableGen] Albedo saturation correction: no-op (ratio ≈ 1.0)")
+ self._restore_raw_albedos(context)
+ return
+
+ for cam_idx in sorted(self._pbr_maps.keys()):
+ cam_maps = self._pbr_maps[cam_idx]
+ if 'albedo' not in cam_maps:
+ continue
+
+ raw_path = get_file_path(
+ context, "pbr", subtype="raw_albedo",
+ camera_id=cam_idx,
+ material_id=self._material_id)
+ if not os.path.exists(raw_path):
+ continue
+
+ with open(raw_path, 'rb') as f:
+ raw_bytes = f.read()
+ processed = self._postprocess_albedo(raw_bytes, effective_sat)
+ with open(cam_maps['albedo'], 'wb') as f:
+ f.write(processed)
+ print(f"[StableGen] Corrected albedo saturation cam {cam_idx} "
+ f"(uniform ×{effective_sat:.2f})")
+
+ def _apply_per_camera_saturation(self, context):
+ """Apply individual saturation correction for each camera."""
+ for cam_idx in sorted(self._pbr_maps.keys()):
+ cam_maps = self._pbr_maps[cam_idx]
+ if 'albedo' not in cam_maps:
+ continue
+
+ raw_path = get_file_path(
+ context, "pbr", subtype="raw_albedo",
+ camera_id=cam_idx,
+ material_id=self._material_id)
+ original_path = get_file_path(
+ context, "generated", camera_id=cam_idx,
+ material_id=self._material_id)
+
+ if not os.path.exists(raw_path) or not os.path.exists(original_path):
+ continue
+
+ s_orig = self._compute_image_mean_saturation(original_path)
+ s_albedo = self._compute_image_mean_saturation(raw_path)
+
+ if s_albedo < 0.01:
+ continue
+
+ ratio = max(0.5, min(s_orig / s_albedo, 3.0))
+
+ with open(raw_path, 'rb') as f:
+ raw_bytes = f.read()
+ processed = self._postprocess_albedo(raw_bytes, ratio)
+ with open(cam_maps['albedo'], 'wb') as f:
+ f.write(processed)
+ print(f"[StableGen] Corrected albedo saturation cam {cam_idx} "
+ f"(per-camera ×{ratio:.2f})")
+
+ def _restore_raw_albedos(self, context):
+ """Copy raw (unprocessed) albedos back to active paths.
+
+ Ensures that disabling saturation correction and reprojecting
+ uses the original decomposition output, not a previously corrected file.
+ """
+ import shutil
+ for cam_idx in sorted(self._pbr_maps.keys()):
+ cam_maps = self._pbr_maps[cam_idx]
+ if 'albedo' not in cam_maps:
+ continue
+ raw_path = get_file_path(
+ context, "pbr", subtype="raw_albedo",
+ camera_id=cam_idx,
+ material_id=self._material_id)
+ if os.path.exists(raw_path) and os.path.exists(cam_maps['albedo']):
+ shutil.copy2(raw_path, cam_maps['albedo'])
+ print(f"[StableGen] Restored raw albedo for camera {cam_idx}")
+
+ @staticmethod
+ def _postprocess_albedo(image_bytes, saturation=1.0):
+ """Apply saturation adjustment to albedo PNG bytes.
+
+ Returns post-processed PNG bytes, or the original bytes unchanged
+ if saturation is 1.0 (no-op).
+ """
+ if saturation == 1.0:
+ return image_bytes
+ from PIL import Image, ImageEnhance
+ img = Image.open(io.BytesIO(image_bytes)).convert('RGB')
+ img = ImageEnhance.Color(img).enhance(saturation)
+ buf = io.BytesIO()
+ img.save(buf, format='PNG')
+ return buf.getvalue()
+
+ def _save_raw_copy(self, context, subtype, camera_id, data_bytes):
+ """Save an unprocessed (raw) copy of a PBR map for later re-processing."""
+ raw_path = get_file_path(
+ context, "pbr", subtype=f"raw_{subtype}",
+ camera_id=camera_id,
+ material_id=self._material_id)
+ try:
+ with open(raw_path, 'wb') as f:
+ f.write(data_bytes)
+ except Exception as err:
+ print(f"[StableGen] Warning: failed to save raw {subtype}: {err}")
+
+ def _ensure_raw_copies(self, context, camera_id, existing_maps):
+ """Ensure a raw albedo copy exists for backward compatibility.
+
+ For files generated before the saturation correction feature, the
+ active albedo IS the unprocessed original. Copy it to the raw slot
+ so that the batch saturation correction can read from it.
+ """
+ if 'albedo' not in existing_maps:
+ return
+ raw_path = get_file_path(
+ context, "pbr", subtype="raw_albedo",
+ camera_id=camera_id,
+ material_id=self._material_id)
+ if not os.path.exists(raw_path) and os.path.exists(existing_maps['albedo']):
+ try:
+ import shutil
+ shutil.copy2(existing_maps['albedo'], raw_path)
+ print(f"[StableGen] Created missing raw albedo from active file "
+ f"(camera {camera_id})")
+ except Exception as err:
+ print(f"[StableGen] Warning: could not create raw albedo copy: {err}")
+
+ def _run_pbr_decomposition_batched(self, context, camera_images,
+ per_camera_missing=None):
+ """Run PBR decomposition **model-first** across all cameras.
+
+ Instead of iterating cameras → models (which forces repeated model
+ loading/unloading), this iterates models → cameras so each model's
+ weights are loaded once and reused for every camera image.
+
+ Args:
+ context: Blender context.
+ camera_images: ``{cam_idx: image_path}`` for cameras that need
+ processing.
+ per_camera_missing: Optional ``{cam_idx: set_of_missing_map_names}``.
+ When provided (reproject mode), only the models needed to
+ produce the missing maps are executed, and cameras that
+ already have a model's outputs are skipped.
+ """
+ scene = context.scene
+ self._stage = "PBR Decomposition"
+ self._progress = 0
+ num_cams = len(camera_images)
+ cam_ids = sorted(camera_images.keys())
+
+ # When per_camera_missing is provided, compute the union of all
+ # missing maps to determine which models we actually need.
+ if per_camera_missing:
+ all_missing = set()
+ for missing_set in per_camera_missing.values():
+ all_missing |= missing_set
+ print(f"[StableGen] Running selective PBR decomposition on "
+ f"{num_cams} camera(s), missing maps: {all_missing}")
+ else:
+ all_missing = None
+ print(f"[StableGen] Running batched PBR decomposition on "
+ f"{num_cams} camera(s)…")
+
+ # Free VRAM before loading PBR models — the previous generation
+ # model may still be cached, and Marigold/StableDelight need
+ # GPU memory.
+ try:
+ server_address = context.preferences.addons[
+ _ADDON_PKG].preferences.server_address
+ self.workflow_manager._flush_comfyui_vram(
+ server_address, retries=1, label="PBR pre-load")
+ except Exception as e:
+ print(f"[StableGen] VRAM flush before PBR failed (non-fatal): {e}")
+
+ want_albedo = getattr(scene, 'pbr_map_albedo', True)
+ want_roughness = getattr(scene, 'pbr_map_roughness', True)
+ want_metallic = getattr(scene, 'pbr_map_metallic', True)
+ want_normal = getattr(scene, 'pbr_map_normal', True)
+ want_height = getattr(scene, 'pbr_map_height', False)
+ want_emission = getattr(scene, 'pbr_map_emission', False)
+ emission_method = getattr(scene, 'pbr_emission_method', 'residual')
+ albedo_source = getattr(scene, 'pbr_albedo_source', 'delight')
+
+ use_delight = (want_albedo and albedo_source == 'delight')
+ use_lighting_albedo = (want_albedo and albedo_source == 'lighting')
+
+ # ── Determine which Marigold models to run ────────────────────
+ # When all_missing is set (selective rerun), only models whose
+ # outputs intersect with the missing set are scheduled.
+ need_appearance = (
+ (want_albedo and albedo_source == 'marigold')
+ or want_roughness
+ or want_metallic
+ )
+ need_normals = want_normal
+ need_height = want_height
+ # IID-Lighting is needed for 'residual' emission OR 'lighting' albedo
+ need_iid_lighting = (
+ (want_emission and emission_method == 'residual')
+ or use_lighting_albedo
+ )
+
+ if all_missing is not None:
+ # Selective mode: narrow down to only models producing missing maps
+ need_appearance = need_appearance and bool(
+ all_missing & {'albedo', 'roughness', 'metallic'})
+ need_normals = need_normals and 'normal' in all_missing
+ need_height = need_height and 'height' in all_missing
+ need_iid_lighting = need_iid_lighting and bool(
+ all_missing & {'emission', 'albedo'})
+ use_delight = use_delight and 'albedo' in all_missing
+
+ models_to_run = []
+ if need_appearance:
+ models_to_run.append((
+ 'prs-eth/marigold-iid-appearance-v1-1',
+ ['albedo', 'material'],
+ ))
+ if need_normals:
+ models_to_run.append((
+ 'prs-eth/marigold-normals-lcm-v0-1',
+ ['normal'],
+ ))
+ if need_height:
+ models_to_run.append((
+ 'prs-eth/marigold-depth-lcm-v1-0',
+ ['height'],
+ ))
+ if need_iid_lighting:
+ models_to_run.append((
+ 'prs-eth/marigold-iid-lighting-v1-1',
+ ['_lighting_albedo', '_lighting_shading', '_lighting_residual'],
+ ))
+
+ # Count extra post-processing steps (HSV emission)
+ extra_emission_steps = 0
+ if want_emission and emission_method == 'hsv':
+ extra_emission_steps = 1
+
+ total_model_steps = (len(models_to_run)
+ + (1 if use_delight else 0)
+ + extra_emission_steps)
+ if total_model_steps == 0:
+ print("[StableGen] No PBR maps enabled, skipping decomposition")
+ return
+
+ # Activate PBR progress tracking for the UI
+ self._pbr_active = True
+ self._pbr_step = 0
+ self._pbr_total_steps = total_model_steps
+ self._pbr_cam_total = num_cams
+
+ # Ensure every camera has an entry in _pbr_maps
+ for cam_idx in cam_ids:
+ if cam_idx not in self._pbr_maps:
+ self._pbr_maps[cam_idx] = {}
+
+ # ── Tiling settings ───────────────────────────────────────────
+ tiling_mode = getattr(scene, 'pbr_tiling', 'off')
+ tile_grid = getattr(scene, 'pbr_tile_grid', 2)
+
+ tile_model_keys = set()
+
+ # Custom mode per-map toggles (read once, used later)
+ custom_tile_albedo = False
+ custom_tile_material = False
+ custom_tile_normal = False
+ custom_tile_height = False
+ custom_tile_emission = False
+ # Whether IID-Appearance is the albedo provider
+ appearance_provides_albedo = (not use_delight
+ and not use_lighting_albedo)
+
+ if tiling_mode == 'selective':
+ # When using lighting albedo, don't tile appearance
+ # (its albedo will be discarded — only material channels needed).
+ if not use_lighting_albedo:
+ tile_model_keys.add('appearance')
+ if use_lighting_albedo:
+ tile_model_keys.add('lighting')
+ elif tiling_mode == 'all':
+ tile_model_keys = {'appearance', 'normals', 'depth', 'lighting'}
+ elif tiling_mode == 'custom':
+ custom_tile_albedo = getattr(scene, 'pbr_tile_albedo', True)
+ custom_tile_material = getattr(scene, 'pbr_tile_material', False)
+ custom_tile_normal = getattr(scene, 'pbr_tile_normal', False)
+ custom_tile_height = getattr(scene, 'pbr_tile_height', False)
+ custom_tile_emission = (
+ getattr(scene, 'pbr_tile_emission', False)
+ and want_emission
+ and emission_method == 'residual'
+ )
+
+ # IID-Appearance: tile if ANY of its outputs are tiled
+ need_tile_appearance = (
+ custom_tile_material
+ or (custom_tile_albedo and appearance_provides_albedo)
+ )
+ if need_tile_appearance:
+ tile_model_keys.add('appearance')
+
+ # IID-Lighting: tile if albedo or residual/emission output
+ # needs tiling
+ need_tile_lighting = (
+ (use_lighting_albedo and custom_tile_albedo)
+ or custom_tile_emission
+ )
+ if need_tile_lighting:
+ tile_model_keys.add('lighting')
+
+ # Single-output models
+ if custom_tile_normal:
+ tile_model_keys.add('normals')
+ if custom_tile_height:
+ tile_model_keys.add('depth')
+
+ model_step = 0
+
+ # Helper: map model short names to the final PBR map names they
+ # produce, used for per-camera skipping in selective mode.
+ _MODEL_OUTPUT_MAPS = {
+ 'appearance': {'albedo', 'roughness', 'metallic'},
+ 'normals': {'normal'},
+ 'depth': {'height'},
+ 'lighting': {'emission', 'albedo'}, # residual → emission, lighting → albedo
+ }
+
+ def _camera_needs_model(cam_idx, model_key):
+ """Return True if this camera still needs outputs from *model_key*."""
+ if per_camera_missing is None or cam_idx not in per_camera_missing:
+ return True # full run, always needed
+ return bool(per_camera_missing[cam_idx] & _MODEL_OUTPUT_MAPS.get(model_key, set()))
+
+ # ── StableDelight pass (all cameras) ──────────────────────────
+ if use_delight:
+ model_step += 1
+ self._pbr_step = model_step
+ tile_delight = (tiling_mode in ('selective', 'all')
+ or (tiling_mode == 'custom' and custom_tile_albedo))
+ tile_label = f" (tiled {tile_grid}×{tile_grid})" if tile_delight else ""
+ print(f"[StableGen] Model {model_step}/{total_model_steps}: "
+ f"StableDelight{tile_label}")
+
+ for ci, cam_idx in enumerate(cam_ids):
+ # Skip camera if albedo already exists in selective mode
+ if per_camera_missing and cam_idx in per_camera_missing:
+ if 'albedo' not in per_camera_missing[cam_idx]:
+ print(f"[StableGen] Camera {cam_idx}: albedo exists, skipping StableDelight")
+ continue
+ image_path = camera_images[cam_idx]
+ self._pbr_cam = ci
+ self._stage = (f"PBR: StableDelight "
+ f"(cam {ci+1}/{num_cams})")
+ self._progress = 0
+ print(f"[StableGen] Camera {cam_idx} ({ci+1}/{num_cams})…")
+
+ if tile_delight:
+ result = self._process_model_tiled(
+ context, image_path,
+ process_fn=self.workflow_manager.generate_delight_map)
+ if isinstance(result, list) and result:
+ result = result[0]
+ else:
+ result = self.workflow_manager.generate_delight_map(
+ context, image_path
+ )
+
+ if isinstance(result, dict) and "error" in result:
+ print(f"[StableGen] StableDelight error: {result['error']}")
+ self._error = f"StableDelight failed: {result['error']}"
+ return
+ elif isinstance(result, bytes):
+ pbr_path = get_file_path(
+ context, "pbr", subtype="albedo",
+ camera_id=cam_idx,
+ material_id=self._material_id)
+ try:
+ self._save_raw_copy(context, "albedo", cam_idx, result)
+ with open(pbr_path, 'wb') as f:
+ f.write(result)
+ self._pbr_maps[cam_idx]['albedo'] = pbr_path
+ print(f"[StableGen] Saved albedo (delight) → "
+ f"{os.path.basename(pbr_path)}")
+ except Exception as err:
+ print(f"[StableGen] Failed to save delight albedo: {err}")
+
+ # ── Marigold model passes (all cameras per model) ─────────────
+ for model_name, map_names in models_to_run:
+ model_step += 1
+ self._pbr_step = model_step
+ model_short = model_name.split('/')[-1]
+ should_tile = any(k in model_short for k in tile_model_keys)
+ is_iid = 'appearance' in model_short
+ is_iid_lighting = 'lighting' in model_short
+
+ tile_label = f" (tiled {tile_grid}×{tile_grid})" if should_tile else ""
+ print(f"[StableGen] Model {model_step}/{total_model_steps}: "
+ f"{model_name}{tile_label}")
+
+ for ci, cam_idx in enumerate(cam_ids):
+ # Selective mode: skip camera if it doesn't need this model
+ model_key = ('appearance' if is_iid
+ else 'lighting' if is_iid_lighting
+ else 'normals' if 'normals' in model_short
+ else 'depth')
+ if not _camera_needs_model(cam_idx, model_key):
+ print(f"[StableGen] Camera {cam_idx}: already has outputs for "
+ f"{model_key}, skipping")
+ continue
+ image_path = camera_images[cam_idx]
+ self._pbr_cam = ci
+ self._stage = (f"PBR: {model_short} "
+ f"(cam {ci+1}/{num_cams})")
+ self._progress = 0
+
+ # ── Dual-run logic ─────────────────────────────────
+ # Dual-run = run tiled + untiled, then stitch outputs.
+ # Needed when some outputs of a multi-output model
+ # should be tiled and others should not.
+ dual_run_iid = False
+ dual_run_iid_reverse = False # tile material, untile albedo
+ dual_run_lighting = False
+
+ if tiling_mode == 'selective':
+ dual_run_iid = (
+ should_tile and is_iid and not use_delight
+ )
+ dual_run_lighting = (
+ should_tile and is_iid_lighting
+ and use_lighting_albedo
+ )
+ elif tiling_mode == 'custom' and should_tile:
+ if is_iid:
+ # IID-Appearance: [albedo, material]
+ want_tiled_albedo = (custom_tile_albedo
+ and appearance_provides_albedo)
+ want_tiled_material = custom_tile_material
+ # Dual-run only when there's a mismatch
+ if want_tiled_albedo and not want_tiled_material:
+ dual_run_iid = True
+ elif want_tiled_material and not want_tiled_albedo:
+ dual_run_iid = True
+ dual_run_iid_reverse = True
+ # If both tiled → full tile (no dual-run)
+ # If neither → shouldn't reach here (should_tile=False)
+ elif is_iid_lighting:
+ # IID-Lighting: [albedo, shading, residual]
+ # Determine per-output tiling needs
+ want_tiled_l_albedo = (use_lighting_albedo
+ and custom_tile_albedo)
+ want_tiled_l_residual = custom_tile_emission
+ # Shading is always untiled (no benefit).
+ # Dual-run when at least one output is tiled
+ # but not all (shading is never tiled, so dual-run
+ # is needed whenever ANY lighting output is tiled).
+ any_tiled = want_tiled_l_albedo or want_tiled_l_residual
+ if any_tiled:
+ dual_run_lighting = True
+
+ extra = ""
+ if dual_run_iid and not dual_run_iid_reverse:
+ extra = " + untiled material"
+ elif dual_run_iid and dual_run_iid_reverse:
+ extra = " + untiled albedo"
+ elif dual_run_lighting:
+ extra = " + untiled shading"
+ print(f"[StableGen] Camera {cam_idx} ({ci+1}/{num_cams}){extra}…")
+
+ if dual_run_iid:
+ result_tiled = self._process_model_tiled(
+ context, image_path, model_name)
+ result_untiled = self.workflow_manager.generate_pbr_maps(
+ context, image_path, model_name=model_name)
+ if (isinstance(result_tiled, list)
+ and len(result_tiled) >= 2
+ and isinstance(result_untiled, list)
+ and len(result_untiled) >= 2):
+ if dual_run_iid_reverse:
+ # Tile material, keep albedo untiled
+ result = [result_untiled[0], result_tiled[1]]
+ else:
+ # Tile albedo, keep material untiled
+ result = [result_tiled[0], result_untiled[1]]
+ elif isinstance(result_tiled, dict):
+ result = result_tiled
+ elif isinstance(result_untiled, dict):
+ result = result_untiled
+ else:
+ result = (result_tiled
+ if isinstance(result_tiled, list)
+ else result_untiled)
+ elif dual_run_lighting:
+ # IID-Lighting: stitch per-output from tiled/untiled
+ result_tiled = self._process_model_tiled(
+ context, image_path, model_name)
+ result_untiled = self.workflow_manager.generate_pbr_maps(
+ context, image_path, model_name=model_name)
+ if (isinstance(result_tiled, list)
+ and len(result_tiled) >= 3
+ and isinstance(result_untiled, list)
+ and len(result_untiled) >= 3):
+ # Per-output: pick tiled or untiled based on toggles
+ if tiling_mode == 'custom':
+ tile_l_albedo = (use_lighting_albedo
+ and custom_tile_albedo)
+ tile_l_residual = custom_tile_emission
+ else:
+ # selective: tile albedo only
+ tile_l_albedo = True
+ tile_l_residual = False
+ r_albedo = (result_tiled[0] if tile_l_albedo
+ else result_untiled[0])
+ r_shading = result_untiled[1] # always untiled
+ r_residual = (result_tiled[2] if tile_l_residual
+ else result_untiled[2])
+ result = [r_albedo, r_shading, r_residual]
+ elif isinstance(result_tiled, dict):
+ result = result_tiled
+ elif isinstance(result_untiled, dict):
+ result = result_untiled
+ else:
+ result = (result_tiled
+ if isinstance(result_tiled, list)
+ else result_untiled)
+ elif should_tile:
+ # Tile the model even when StableDelight handles albedo —
+ # the IID-Appearance albedo output is discarded by
+ # _save_pbr_map_outputs, but roughness/metallic still
+ # benefit from tiling.
+ result = self._process_model_tiled(
+ context, image_path, model_name)
+ else:
+ result = self.workflow_manager.generate_pbr_maps(
+ context, image_path, model_name=model_name)
+
+ if isinstance(result, dict) and "error" in result:
+ print(f"[StableGen] PBR model error: {result['error']}")
+ self._error = (f"PBR decomposition failed: "
+ f"{result['error']}")
+ return
+
+ # ── Save each output component ────────────────────
+ self._save_pbr_map_outputs(
+ context, result, map_names, cam_idx,
+ want_roughness=want_roughness,
+ want_metallic=want_metallic,
+ use_delight=use_delight,
+ use_lighting_albedo=use_lighting_albedo)
+
+ # ── Post-Marigold emission passes ─────────────────────────────
+ if want_emission:
+ if emission_method == 'residual':
+ # IID-Lighting residual was already saved by the model
+ # loop above – now gate it with roughness/metallic.
+ model_step += 1
+ self._pbr_step = model_step
+ self._progress = 0
+ print(f"[StableGen] Post-processing emission (residual "
+ f"gating) – step {model_step}/{total_model_steps}")
+ for ci, cam_idx in enumerate(cam_ids):
+ if per_camera_missing and cam_idx in per_camera_missing:
+ if 'emission' not in per_camera_missing[cam_idx]:
+ continue
+ self._pbr_cam = ci
+ self._stage = (f"PBR: Emission gating "
+ f"(cam {ci+1}/{num_cams})")
+ self._progress = (ci / num_cams) * 100
+ self._gate_emission_residual(context, cam_idx)
+ self._progress = 100
+
+ elif emission_method == 'hsv':
+ model_step += 1
+ self._pbr_step = model_step
+ self._progress = 0
+ print(f"[StableGen] Emission via HSV threshold "
+ f"– step {model_step}/{total_model_steps}")
+ for ci, cam_idx in enumerate(cam_ids):
+ if per_camera_missing and cam_idx in per_camera_missing:
+ if 'emission' not in per_camera_missing[cam_idx]:
+ continue
+ self._pbr_cam = ci
+ self._stage = (f"PBR: HSV emission "
+ f"(cam {ci+1}/{num_cams})")
+ self._progress = (ci / num_cams) * 100
+ image_path = camera_images[cam_idx]
+ self._generate_emission_hsv(context, cam_idx, image_path)
+ self._progress = 100
+
+ # Deactivate PBR progress tracking
+ self._pbr_active = False
+ self._pbr_step = 0
+ self._pbr_total_steps = 0
+
+ print(f"[StableGen] PBR decomposition complete for "
+ f"{num_cams} camera(s)")
+
+ def _save_pbr_map_outputs(self, context, result, map_names, camera_id,
+ want_roughness=True, want_metallic=True,
+ use_delight=False, use_lighting_albedo=False):
+ """Save the output images from a single model run to disk.
+
+ Handles the IID material channel split (R=roughness, G=metallic)
+ and the camera→world normal conversion.
+
+ Args:
+ result: list of bytes (one per output map).
+ map_names: list of names corresponding to each output.
+ camera_id: Camera index.
+ want_roughness / want_metallic: Whether to save those channels.
+ use_delight: Whether StableDelight handles albedo (skip IID albedo).
+ """
+ scene = context.scene
+ for i, map_bytes in enumerate(result):
+ map_name = map_names[i] if i < len(map_names) else f"component_{i}"
+
+ # ── IID-Appearance "material" channel split ───────────
+ if map_name == 'material':
+ try:
+ mat_img = Image.open(io.BytesIO(map_bytes)).convert('RGB')
+ except Exception as err:
+ print(f"[StableGen] Failed to decode material image: {err}")
+ continue
+
+ if want_roughness:
+ rough_img = mat_img.getchannel('R').convert('L')
+ rough_path = get_file_path(
+ context, "pbr", subtype="roughness",
+ camera_id=camera_id,
+ material_id=self._material_id)
+ try:
+ rough_img.save(rough_path)
+ self._pbr_maps[camera_id]['roughness'] = rough_path
+ print(f"[StableGen] Saved roughness (material R) → "
+ f"{os.path.basename(rough_path)}")
+ except Exception as err:
+ print(f"[StableGen] Failed to save roughness: {err}")
+
+ if want_metallic:
+ metal_img = mat_img.getchannel('G').convert('L')
+ metal_path = get_file_path(
+ context, "pbr", subtype="metallic",
+ camera_id=camera_id,
+ material_id=self._material_id)
+ try:
+ metal_img.save(metal_path)
+ self._pbr_maps[camera_id]['metallic'] = metal_path
+ print(f"[StableGen] Saved metallic (material G) → "
+ f"{os.path.basename(metal_path)}")
+ except Exception as err:
+ print(f"[StableGen] Failed to save metallic: {err}")
+ continue
+
+ # ── IID-Lighting intermediate outputs ─────────────────
+ # These are prefixed with '_lighting_' to avoid toggle
+ # checks. The residual is saved for later gating; albedo
+ # and shading are discarded unless using lighting albedo.
+ if map_name.startswith('_lighting_'):
+ lighting_key = map_name # e.g. '_lighting_residual'
+ lighting_path = get_file_path(
+ context, "pbr", subtype=lighting_key.lstrip('_'),
+ camera_id=camera_id,
+ material_id=self._material_id)
+ try:
+ with open(lighting_path, 'wb') as f:
+ f.write(map_bytes)
+ self._pbr_maps[camera_id][lighting_key] = lighting_path
+ print(f"[StableGen] Saved {lighting_key} → "
+ f"{os.path.basename(lighting_path)}")
+
+ # When using IID-Lighting as albedo source, copy the
+ # lighting albedo to the main albedo slot as well.
+ if map_name == '_lighting_albedo' and use_lighting_albedo:
+ albedo_path = get_file_path(
+ context, "pbr", subtype="albedo",
+ camera_id=camera_id,
+ material_id=self._material_id)
+ self._save_raw_copy(context, "albedo", camera_id, map_bytes)
+ with open(albedo_path, 'wb') as f:
+ f.write(map_bytes)
+ self._pbr_maps[camera_id]['albedo'] = albedo_path
+ print(f"[StableGen] Saved albedo (IID-Lighting) → "
+ f"{os.path.basename(albedo_path)}")
+ except Exception as err:
+ print(f"[StableGen] Failed to save {lighting_key}: {err}")
+ continue
+
+ # Skip maps the user didn't enable
+ toggle_attr = f"pbr_map_{map_name}"
+ if hasattr(scene, toggle_attr) and not getattr(scene, toggle_attr):
+ continue
+
+ # Skip Marigold IID-Appearance albedo when another source handles it
+ if map_name == 'albedo' and (use_delight or use_lighting_albedo):
+ continue
+
+ pbr_path = get_file_path(
+ context, "pbr", subtype=map_name,
+ camera_id=camera_id,
+ material_id=self._material_id)
+ try:
+ # ── Camera→world-space conversion for normals ─────
+ if map_name == 'normal':
+ converted = self._convert_normals_cam_to_world(
+ map_bytes, camera_id)
+ if converted is not None:
+ map_bytes = converted
+
+ # ── Raw save for albedo (post-processing applied in batch) ─
+ if map_name == 'albedo':
+ self._save_raw_copy(context, "albedo", camera_id, map_bytes)
+
+ with open(pbr_path, 'wb') as f:
+ f.write(map_bytes)
+ self._pbr_maps[camera_id][map_name] = pbr_path
+ print(f"[StableGen] Saved {map_name} → "
+ f"{os.path.basename(pbr_path)}")
+ except Exception as err:
+ print(f"[StableGen] Failed to save PBR map {map_name}: {err}")
+
+ # ── Emission extraction methods ───────────────────────────────────
+
+ def _gate_emission_residual(self, context, camera_id):
+ """Gate the IID-Lighting residual with roughness/metallic to
+ produce a cleaner emission map.
+
+ emission = residual × (1 − metallic) × roughness_mask, then
+ threshold. High-metallic + low-roughness regions are likely
+ specular reflections, not true emission.
+ """
+ scene = context.scene
+ cam_maps = self._pbr_maps.get(camera_id, {})
+ residual_path = cam_maps.get('_lighting_residual')
+ if not residual_path or not os.path.exists(residual_path):
+ print(f"[StableGen] Emission residual not found for camera {camera_id}")
+ return
+
+ threshold = getattr(scene, 'pbr_emission_threshold', 0.2)
+
+ try:
+ residual = np.array(Image.open(residual_path).convert('RGB')).astype(np.float32) / 255.0
+
+ # Optionally gate with metallic if available — high metallic
+ # surfaces produce strong specular that isn't true emission.
+ metal_path = cam_maps.get('metallic')
+ if metal_path and os.path.exists(metal_path):
+ metal_img = Image.open(metal_path).convert('L')
+ # Resize metallic to match residual dims (they may differ
+ # when tiling/super-res settings aren't identical).
+ res_h, res_w = residual.shape[:2]
+ if metal_img.size != (res_w, res_h):
+ metal_img = metal_img.resize(
+ (res_w, res_h), Image.Resampling.BILINEAR)
+ metallic = np.array(metal_img).astype(np.float32) / 255.0
+ gate = (1.0 - metallic)[:, :, np.newaxis]
+ residual = residual * gate
+
+ # Apply soft threshold — pixels below the threshold are
+ # smoothly faded out rather than hard-clipped.
+ luminance = np.mean(residual, axis=2)
+ # Smooth fade: rescale [0, threshold] → 0, [threshold, 1] → preserved
+ fade = np.clip((luminance - threshold * 0.5) / max(threshold * 0.5, 1e-6),
+ 0.0, 1.0)[:, :, np.newaxis]
+ emission = residual * fade
+
+ emission_img = Image.fromarray(
+ np.clip(emission * 255, 0, 255).astype(np.uint8), mode='RGB')
+ emission_path = get_file_path(
+ context, "pbr", subtype="emission",
+ camera_id=camera_id,
+ material_id=self._material_id)
+ emission_img.save(emission_path)
+ self._pbr_maps[camera_id]['emission'] = emission_path
+ print(f"[StableGen] Saved emission (residual gated) → "
+ f"{os.path.basename(emission_path)}")
+ except Exception as err:
+ print(f"[StableGen] Failed to generate emission (residual): {err}")
+ import traceback; traceback.print_exc()
+
+ def _generate_emission_hsv(self, context, camera_id, image_path):
+ """Extract emission via high-saturation + high-value thresholding
+ in HSV space. Glowing objects retain colour intensity while
+ normally-lit surfaces desaturate under bright light.
+ """
+ scene = context.scene
+ sat_min = getattr(scene, 'pbr_emission_saturation_min', 0.5)
+ val_min = getattr(scene, 'pbr_emission_value_min', 0.85)
+ bloom_radius = getattr(scene, 'pbr_emission_bloom', 5.0)
+ threshold = getattr(scene, 'pbr_emission_threshold', 0.3)
+
+ try:
+ original = np.array(Image.open(image_path).convert('RGB'))
+ rgb_f = original.astype(np.float32) / 255.0
+
+ # RGB → HSV
+ maxc = rgb_f.max(axis=2)
+ minc = rgb_f.min(axis=2)
+ delta = maxc - minc
+ saturation = np.where(maxc > 1e-6, delta / maxc, 0.0)
+ value = maxc
+
+ # Build binary mask: high saturation AND high value
+ mask = ((saturation >= sat_min) & (value >= val_min)).astype(np.float32)
+
+ # Apply bloom (Gaussian blur) to soften edges
+ if bloom_radius > 0:
+ try:
+ import cv2
+ ksize = int(bloom_radius * 4) | 1 # must be odd
+ mask = cv2.GaussianBlur(mask, (ksize, ksize), bloom_radius)
+ except ImportError:
+ # Fallback: simple box blur via PIL
+ from PIL import ImageFilter
+ mask_pil = Image.fromarray(
+ (mask * 255).astype(np.uint8), mode='L')
+ mask_pil = mask_pil.filter(
+ ImageFilter.GaussianBlur(radius=bloom_radius))
+ mask = np.array(mask_pil).astype(np.float32) / 255.0
+
+ # Apply threshold on the blurred mask
+ mask = np.where(mask > threshold, mask, 0.0)
+
+ # Multiply mask by original RGB to get coloured emission
+ emission = rgb_f * mask[:, :, np.newaxis]
+
+ emission_img = Image.fromarray(
+ np.clip(emission * 255, 0, 255).astype(np.uint8), mode='RGB')
+ emission_path = get_file_path(
+ context, "pbr", subtype="emission",
+ camera_id=camera_id,
+ material_id=self._material_id)
+ emission_img.save(emission_path)
+ self._pbr_maps[camera_id]['emission'] = emission_path
+ print(f"[StableGen] Saved emission (HSV threshold) → "
+ f"{os.path.basename(emission_path)}")
+ except Exception as err:
+ print(f"[StableGen] Failed to generate emission (HSV): {err}")
+ import traceback; traceback.print_exc()
diff --git a/stablegen/texturing/pbr_projection.py b/stablegen/texturing/pbr_projection.py
new file mode 100644
index 0000000..b67e7de
--- /dev/null
+++ b/stablegen/texturing/pbr_projection.py
@@ -0,0 +1,502 @@
+import bpy
+import os
+
+from .projection import get_or_load_image
+
+
+def _clone_color_tree_for_pbr(nodes, links, source_node, cam_tex_to_pbr,
+ channel, y_offset):
+ """Recursively clone the colour mix-tree, sharing *Fac* connections and
+ swapping Image Texture leaves for their PBR counterparts.
+
+ Args:
+ nodes / links: ``mat.node_tree.nodes / links``.
+ source_node: current node being cloned (walk from BSDF backwards).
+ cam_tex_to_pbr: ``{color_tex_node: pbr_tex_node}``.
+ channel: PBR channel name (for default values).
+ y_offset: vertical pixel shift for cloned nodes.
+
+ Returns:
+ Output socket of the cloned sub-tree, or *None*.
+ """
+ # ── Leaf: Image Texture → swap for PBR variant ────────────────────
+ if source_node.type == 'TEX_IMAGE':
+ pbr_node = cam_tex_to_pbr.get(source_node)
+ return pbr_node.outputs[0] if pbr_node else None
+
+ # ── Resolve input names: ShaderNodeMixRGB vs ShaderNodeMix ────────
+ # Blender 4.0+ may use "A"/"B"/"Factor" instead of legacy names.
+ fac_name, c1_name, c2_name = "Fac", "Color1", "Color2"
+ if "Factor" in source_node.inputs and "A" in source_node.inputs:
+ fac_name, c1_name, c2_name = "Factor", "A", "B"
+
+ # ── MixRGB / Mix node → clone with same Fac ──────────────────────
+ is_mix = (source_node.type in ('MIX_RGB', 'MIX')
+ or (hasattr(source_node, 'bl_idname')
+ and source_node.bl_idname in (
+ 'ShaderNodeMixRGB', 'ShaderNodeMix'))
+ or (c1_name in source_node.inputs
+ and c2_name in source_node.inputs
+ and fac_name in source_node.inputs))
+ if is_mix:
+ c1_link = source_node.inputs[c1_name].links
+ c2_link = source_node.inputs[c2_name].links
+ c1_src = c1_link[0].from_node if c1_link else None
+ c2_src = c2_link[0].from_node if c2_link else None
+
+ c1_pbr = (_clone_color_tree_for_pbr(
+ nodes, links, c1_src, cam_tex_to_pbr, channel, y_offset)
+ if c1_src else None)
+ c2_pbr = (_clone_color_tree_for_pbr(
+ nodes, links, c2_src, cam_tex_to_pbr, channel, y_offset)
+ if c2_src else None)
+
+ if c1_pbr is None and c2_pbr is None:
+ return None
+
+ new_mix = nodes.new("ShaderNodeMixRGB")
+ new_mix.location = (source_node.location[0],
+ source_node.location[1] + y_offset)
+ new_mix.label = f"PBR_{channel}"
+
+ # Share the Fac (visibility weight) connection
+ fac_links = source_node.inputs[fac_name].links
+ if fac_links:
+ links.new(fac_links[0].from_socket, new_mix.inputs["Fac"])
+ else:
+ new_mix.inputs["Fac"].default_value = (
+ source_node.inputs[fac_name].default_value)
+
+ # Connect children
+ if c1_pbr:
+ links.new(c1_pbr, new_mix.inputs["Color1"])
+ else:
+ # Sensible defaults for uncovered areas
+ defaults = {
+ 'roughness': (0.5, 0.5, 0.5, 1.0),
+ 'metallic': (0.0, 0.0, 0.0, 1.0),
+ 'albedo': (0.5, 0.5, 0.5, 1.0),
+ 'residual': (0.0, 0.0, 0.0, 1.0),
+ 'shading': (0.5, 0.5, 0.5, 1.0),
+ }
+ new_mix.inputs["Color1"].default_value = defaults.get(
+ channel, (0.0, 0.0, 0.0, 1.0))
+
+ if c2_pbr:
+ links.new(c2_pbr, new_mix.inputs["Color2"])
+ else:
+ # For Color2 (the "fallback" side of the mix) we inherit the
+ # original node's default so that areas not covered by any
+ # camera projection keep the scene's fallback colour. Only
+ # data channels (roughness, metallic, …) use a hardcoded
+ # neutral, because the original tree's fallback colour would
+ # be nonsensical for those channels.
+ defaults = {
+ 'roughness': (0.5, 0.5, 0.5, 1.0),
+ 'metallic': (0.0, 0.0, 0.0, 1.0),
+ 'residual': (0.0, 0.0, 0.0, 1.0),
+ 'shading': (0.5, 0.5, 0.5, 1.0),
+ }
+ new_mix.inputs["Color2"].default_value = defaults.get(
+ channel, source_node.inputs[c2_name].default_value)
+
+ return new_mix.outputs[0]
+
+ # ── Unknown node → follow first connected input ───────────────────
+ for inp in source_node.inputs:
+ if inp.links:
+ result = _clone_color_tree_for_pbr(
+ nodes, links, inp.links[0].from_node,
+ cam_tex_to_pbr, channel, y_offset)
+ if result:
+ return result
+ return None
+
+
+def project_pbr_to_bsdf(context, to_texture, pbr_maps, material_id=None):
+ """Project PBR decomposition maps through the same camera UV projections
+ as the colour texture and wire them to the Principled BSDF.
+
+ This function is called **after** ``project_image()`` has finished
+ building the full colour projection material (including UV Project
+ attributes and the visibility-weighted mix tree).
+
+ For each PBR channel it:
+
+ 1. Creates Image Texture nodes that re-use the per-camera UV attributes
+ already baked by ``project_image()``.
+ 2. Clones the colour MixRGB binary tree, sharing the **Fac** (weight)
+ connections so visibility blending is identical.
+ 3. Wires the cloned tree's output to the corresponding Principled BSDF
+ input (Roughness, Metallic, Base Color, etc.).
+
+ Args:
+ context: Blender context.
+ to_texture: List of mesh objects that were textured.
+ pbr_maps: ``{camera_idx: {map_name: file_path}, …}``.
+ material_id: Material slot index used during generation.
+ """
+ scene = context.scene
+
+ if not pbr_maps:
+ return
+
+ # Build channel → BSDF input map from enabled per-map toggles
+ channel_wiring = {} # map_name → BSDF input name
+ special_wiring = {} # map_name → special handler ('normal' | 'displacement')
+
+ if getattr(scene, 'pbr_replace_color_with_albedo', True) and getattr(scene, 'pbr_map_albedo', True):
+ channel_wiring['albedo'] = 'Base Color'
+ if getattr(scene, 'pbr_map_roughness', True):
+ channel_wiring['roughness'] = 'Roughness'
+ if getattr(scene, 'pbr_map_metallic', True):
+ channel_wiring['metallic'] = 'Metallic'
+ if getattr(scene, 'pbr_map_normal', True):
+ special_wiring['normal'] = 'normal'
+ if getattr(scene, 'pbr_map_height', False):
+ special_wiring['height'] = 'displacement'
+ if getattr(scene, 'pbr_map_emission', False):
+ special_wiring['emission'] = 'emission'
+ processed_materials = set()
+
+ for obj in to_texture:
+ if not hasattr(obj, 'active_material') or not obj.active_material:
+ continue
+ mat = obj.active_material
+ if mat.name in processed_materials:
+ continue
+ if not mat.use_nodes:
+ continue
+
+ nodes = mat.node_tree.nodes
+ links = mat.node_tree.links
+
+ # ── Find or create the Principled BSDF ────────────────────────────
+ principled = None
+ for node in nodes:
+ if node.type == 'BSDF_PRINCIPLED':
+ principled = node
+ break
+
+ if principled is None:
+ # No BSDF exists yet. Create one and rewire the existing
+ # colour output through it so that PBR channels have
+ # somewhere to connect.
+ output_node = None
+ for node in nodes:
+ if node.type == 'OUTPUT_MATERIAL':
+ output_node = node
+ break
+ if output_node is None:
+ continue
+
+ principled = nodes.new("ShaderNodeBsdfPrincipled")
+ principled.inputs["Roughness"].default_value = 1.0
+
+ # Rewire: existing surface source → BSDF Base Color → Output
+ surface_links = output_node.inputs["Surface"].links
+ if surface_links:
+ old_source = surface_links[0].from_socket
+ # Position BSDF between old source and output
+ principled.location = (
+ output_node.location[0] - 300,
+ output_node.location[1])
+ links.new(old_source, principled.inputs["Base Color"])
+ else:
+ principled.location = (
+ output_node.location[0] - 300,
+ output_node.location[1])
+
+ links.new(principled.outputs[0],
+ output_node.inputs["Surface"])
+
+ # ── Discover existing camera Image Texture nodes ──────────────
+ # Labels follow the pattern "{cam_idx}-{mat_id}" set by
+ # project_image().
+ camera_tex_nodes = {} # cam_idx → tex_node
+ for node in nodes:
+ if node.type == 'TEX_IMAGE' and node.label and '-' in node.label:
+ parts = node.label.split('-')
+ try:
+ cam_idx = int(parts[0])
+ camera_tex_nodes[cam_idx] = node
+ except ValueError:
+ continue
+
+ if not camera_tex_nodes:
+ continue
+
+ # ── Find the root of the colour mix tree ─────────────────────
+ color_root = None
+ if principled.inputs["Base Color"].links:
+ color_root = principled.inputs["Base Color"].links[0].from_node
+
+ # ── For each PBR channel, create textures and clone the tree ──
+ channel_idx = 0
+ for channel, bsdf_input in channel_wiring.items():
+ if bsdf_input not in principled.inputs:
+ continue
+
+ # Build mapping: colour_tex_node → PBR_tex_node
+ cam_tex_to_pbr = {}
+ for cam_idx, color_tex in camera_tex_nodes.items():
+ if cam_idx not in pbr_maps:
+ continue
+ cam_pbr = pbr_maps[cam_idx]
+ if channel not in cam_pbr:
+ continue
+
+ pbr_path = cam_pbr[channel]
+ if not os.path.exists(pbr_path):
+ continue
+
+ pbr_img = get_or_load_image(pbr_path, force_reload=True)
+ if not pbr_img:
+ continue
+
+ # Colorspace: roughness/metallic are always linear data.
+ # Albedo from IID-Lighting is also linear; IID-Appearance
+ # albedo is sRGB (Blender's default for PNG), so leave it.
+ if channel in ('roughness', 'metallic'):
+ pbr_img.colorspace_settings.name = 'Non-Color'
+ elif (channel == 'albedo'
+ and getattr(scene, 'pbr_albedo_source', 'marigold') == 'lighting'):
+ pbr_img.colorspace_settings.name = 'Non-Color'
+
+ pbr_tex = nodes.new("ShaderNodeTexImage")
+ pbr_tex.image = pbr_img
+ pbr_tex.extension = 'CLIP'
+ pbr_tex.label = (
+ f"PBR_{channel}_{cam_idx}_{material_id}")
+ pbr_tex.location = (
+ color_tex.location[0],
+ color_tex.location[1]
+ - 200 * (1 + channel_idx) * len(camera_tex_nodes))
+
+ # Re-use the same camera UV attribute
+ if color_tex.inputs["Vector"].links:
+ uv_socket = (
+ color_tex.inputs["Vector"].links[0].from_socket)
+ links.new(uv_socket, pbr_tex.inputs["Vector"])
+
+ cam_tex_to_pbr[color_tex] = pbr_tex
+
+ if not cam_tex_to_pbr:
+ continue
+
+ # Clone the colour mix tree (or fall back to direct connection)
+ pbr_final_output = None
+ if color_root:
+ y_off = -500 * (1 + channel_idx)
+ pbr_output = _clone_color_tree_for_pbr(
+ nodes, links, color_root, cam_tex_to_pbr,
+ channel, y_off)
+ if pbr_output:
+ pbr_final_output = pbr_output
+ else:
+ # No colour tree exists; connect first PBR texture directly
+ first_pbr = next(iter(cam_tex_to_pbr.values()))
+ pbr_final_output = first_pbr.outputs["Color"]
+
+ if pbr_final_output:
+ links.new(pbr_final_output,
+ principled.inputs[bsdf_input])
+
+ channel_idx += 1
+
+ # ── Special wiring: Normal Map + Displacement ─────────────────
+ for sp_channel, sp_type in special_wiring.items():
+ # Build PBR texture nodes for this special channel
+ sp_tex_map = {} # color_tex_node → pbr_tex_node
+ for cam_idx, color_tex in camera_tex_nodes.items():
+ if cam_idx not in pbr_maps:
+ continue
+ cam_pbr = pbr_maps[cam_idx]
+ if sp_channel not in cam_pbr:
+ continue
+ sp_path = cam_pbr[sp_channel]
+ if not os.path.exists(sp_path):
+ continue
+ sp_img = get_or_load_image(sp_path, force_reload=True)
+ if not sp_img:
+ continue
+ sp_img.colorspace_settings.name = 'Non-Color'
+
+ sp_tex = nodes.new("ShaderNodeTexImage")
+ sp_tex.image = sp_img
+ sp_tex.extension = 'CLIP'
+ sp_tex.label = f"PBR_{sp_channel}_{cam_idx}_{material_id}"
+ sp_tex.location = (
+ color_tex.location[0],
+ color_tex.location[1]
+ - 200 * (2 + channel_idx) * len(camera_tex_nodes))
+ if color_tex.inputs["Vector"].links:
+ uv_socket = color_tex.inputs["Vector"].links[0].from_socket
+ links.new(uv_socket, sp_tex.inputs["Vector"])
+ sp_tex_map[color_tex] = sp_tex
+
+ if not sp_tex_map:
+ continue
+
+ # Clone the colour tree for this special channel
+ sp_output = None
+ if color_root:
+ y_off = -500 * (2 + channel_idx)
+ sp_output = _clone_color_tree_for_pbr(
+ nodes, links, color_root, sp_tex_map,
+ sp_channel, y_off)
+ else:
+ first_sp = next(iter(sp_tex_map.values()))
+ sp_output = first_sp.outputs["Color"]
+
+ if sp_output is None:
+ continue
+
+ if sp_type == 'normal':
+ normal_strength = getattr(scene, 'pbr_normal_strength', 0.5)
+
+ # World-space Normal Map — camera-space normals were
+ # already converted in _convert_normals_cam_to_world().
+ # Using WORLD space avoids tangent-frame dependency,
+ # so it works on voxel-remeshed geometry.
+ normal_map_node = nodes.new("ShaderNodeNormalMap")
+ normal_map_node.space = 'WORLD'
+ normal_map_node.location = (
+ principled.location[0] - 300,
+ principled.location[1] - 400)
+ normal_map_node.label = "PBR Normal Map (World)"
+ normal_map_node.inputs["Strength"].default_value = (
+ normal_strength)
+ links.new(sp_output, normal_map_node.inputs["Color"])
+ if "Normal" in principled.inputs:
+ links.new(normal_map_node.outputs["Normal"],
+ principled.inputs["Normal"])
+
+ elif sp_type == 'displacement':
+ # ── Displacement / Height Map ─────────────────────
+ height_scale = getattr(scene, 'pbr_height_scale', 0.1)
+ output_node = None
+ for node in nodes:
+ if node.type == 'OUTPUT_MATERIAL':
+ output_node = node
+ break
+ if output_node and "Displacement" in output_node.inputs:
+ disp_node = nodes.new("ShaderNodeDisplacement")
+ disp_node.location = (
+ output_node.location[0] - 300,
+ output_node.location[1] - 200)
+ disp_node.label = "PBR Displacement"
+ disp_node.inputs["Scale"].default_value = height_scale
+ disp_node.inputs["Midlevel"].default_value = 0.5
+ links.new(sp_output, disp_node.inputs["Height"])
+ links.new(disp_node.outputs["Displacement"],
+ output_node.inputs["Displacement"])
+
+ elif sp_type == 'emission':
+ # ── Emission Map ──────────────────────────────────
+ emission_strength = getattr(
+ scene, 'pbr_emission_strength', 5.0)
+ if "Emission Color" in principled.inputs:
+ links.new(sp_output,
+ principled.inputs["Emission Color"])
+ principled.inputs[
+ "Emission Strength"].default_value = emission_strength
+ elif "Emission" in principled.inputs:
+ # Older Blender versions use "Emission" not "Emission Color"
+ links.new(sp_output,
+ principled.inputs["Emission"])
+ if "Emission Strength" in principled.inputs:
+ principled.inputs[
+ "Emission Strength"].default_value = emission_strength
+
+ channel_idx += 1
+
+ # ── Set BSDF default values ───────────────────────────────────
+ if 'roughness' in channel_wiring:
+ principled.inputs["Roughness"].default_value = 1.0
+ if 'metallic' in channel_wiring:
+ principled.inputs["Metallic"].default_value = 0.0
+
+ processed_materials.add(mat.name)
+
+ # ── AO Bake (geometry-based, per-object) ──────────────────────────
+ if getattr(scene, 'pbr_map_ao', False):
+ _bake_ao_for_objects(context, to_texture, scene)
+
+
+def _bake_ao_for_objects(context, to_texture, scene):
+ """Add shader-based Ambient Occlusion to materials.
+
+ Inserts a Blender ``Ambient Occlusion`` shader node into each
+ material and multiplies it with the Base Color input of the
+ Principled BSDF. This is evaluated at render time — zero bake
+ time and works with any poly count.
+ """
+ ao_samples = getattr(scene, 'pbr_ao_samples', 16)
+ ao_distance = getattr(scene, 'pbr_ao_distance', 0.0)
+
+ print(f"[StableGen] Adding shader-based AO "
+ f"({ao_samples} samples, distance={ao_distance})…")
+
+ processed = set()
+ for oi, obj in enumerate(to_texture):
+ if not hasattr(obj, 'active_material') or not obj.active_material:
+ continue
+ mat = obj.active_material
+ if mat.name in processed:
+ continue
+ if not mat.use_nodes:
+ continue
+
+ nodes = mat.node_tree.nodes
+ links = mat.node_tree.links
+
+ # Find Principled BSDF
+ principled = None
+ for node in nodes:
+ if node.type == 'BSDF_PRINCIPLED':
+ principled = node
+ break
+ if principled is None:
+ continue
+
+ print(f"[StableGen] Adding AO node to {mat.name} "
+ f"({oi+1}/{len(to_texture)})…")
+
+ # Create Ambient Occlusion shader node
+ ao_node = nodes.new("ShaderNodeAmbientOcclusion")
+ ao_node.samples = ao_samples
+ ao_node.label = "PBR AO"
+ ao_node.location = (
+ principled.location[0] - 500,
+ principled.location[1] + 350)
+ if ao_distance > 0:
+ ao_node.inputs["Distance"].default_value = ao_distance
+
+ # Wire AO into the material:
+ # Intercept Base Color → Principled BSDF with a Multiply mix.
+ base_input = principled.inputs.get("Base Color")
+ if base_input and base_input.links:
+ old_source = base_input.links[0].from_socket
+
+ mix_node = nodes.new("ShaderNodeMix")
+ mix_node.data_type = 'RGBA'
+ mix_node.blend_type = 'MULTIPLY'
+ mix_node.inputs["Factor"].default_value = 1.0
+ mix_node.location = (
+ principled.location[0] - 200,
+ principled.location[1] + 200)
+ mix_node.label = "AO Multiply"
+
+ links.new(old_source, mix_node.inputs[6]) # A (base colour)
+ links.new(ao_node.outputs["AO"], mix_node.inputs[7]) # B (AO)
+ links.new(mix_node.outputs[2], base_input) # Result → Base Color
+ else:
+ # No existing link — just plug AO colour directly
+ links.new(ao_node.outputs["Color"], base_input)
+
+ processed.add(mat.name)
+
+ print(f"[StableGen] AO shader nodes added "
+ f"({len(processed)} material(s))")
+
diff --git a/stablegen/project.py b/stablegen/texturing/projection.py
similarity index 79%
rename from stablegen/project.py
rename to stablegen/texturing/projection.py
index 37bc47b..e221072 100644
--- a/stablegen/project.py
+++ b/stablegen/texturing/projection.py
@@ -1,2458 +1,1962 @@
-import bpy
-import os
-from math import atan, tan, pi
-
-from .utils import get_last_material_index, get_file_path, get_dir_path
-from .render_tools import prepare_baking, bake_texture, unwrap, _get_camera_resolution
-from mathutils import Vector
-
-_SG_BUFFER_UV_NAME = "_SG_ProjectionBuffer"
-
-
-def _copy_uv_to_attribute(obj, uv_layer_name, attr_name):
- """Copy UV data from a UV layer to a named FLOAT_VECTOR corner-domain attribute.
- Uses FLOAT_VECTOR (3-component) instead of FLOAT2 so the attribute does NOT
- appear in the UV Maps list (FLOAT2 + CORNER = UV map in Blender 3.5+).
- """
- uv_layer = obj.data.uv_layers.get(uv_layer_name)
- if not uv_layer:
- return
- n = len(obj.data.loops)
- uv_buf = [0.0] * (n * 2)
- uv_layer.data.foreach_get("uv", uv_buf)
- # Convert (u,v) pairs to (u,v,0) triples for FLOAT_VECTOR storage
- vec_buf = [0.0] * (n * 3)
- for i in range(n):
- vec_buf[i * 3] = uv_buf[i * 2]
- vec_buf[i * 3 + 1] = uv_buf[i * 2 + 1]
- # vec_buf[i * 3 + 2] already 0.0
- # Remove existing attribute if present
- existing = obj.data.attributes.get(attr_name)
- if existing:
- obj.data.attributes.remove(existing)
- attr = obj.data.attributes.new(name=attr_name, type='FLOAT_VECTOR', domain='CORNER')
- attr.data.foreach_set("vector", vec_buf)
- obj.data.update()
-
-
-def _bake_visibility_weights(context, objects, cameras, mat_id, stop_index=1000000):
- """Pre-compute per-vertex, per-camera visibility weights and store them
- as ``FLOAT`` ``POINT``-domain mesh attributes.
-
- Attribute naming: ``_SG_VisWeight_{cam_idx}_{mat_id}``
-
- The computation replicates the same logic as the runtime
- ``create_native_raycast_visibility`` + ``create_native_feather`` shader
- nodes but runs once in Python so the resulting material has no world-space
- dependencies — projected textures survive object transforms.
-
- Uses NumPy for bulk frustum/angle/feather computation and only falls back
- to per-vertex BVH raycasting for the subset that passes initial checks.
- """
- import bmesh
- import numpy as np
- from mathutils.bvhtree import BVHTree
- from math import pow as fpow
-
- scene = context.scene
- angle_threshold = scene.discard_factor # degrees
- power_exponent = scene.weight_exponent
- feather_active = scene.visibility_vignette
- feather_width = scene.visibility_vignette_width if feather_active else 0.0
- feather_gamma = scene.visibility_vignette_softness if feather_active else 1.0
- feather_width = max(0.0, min(feather_width, 0.49))
-
- # Pre-compute camera data once -----------------------------------------------
- cam_data = []
- for camera in cameras:
- cam_loc = camera.location.copy()
- quat = camera.matrix_world.to_quaternion()
- cam_dir = quat @ Vector((0, 0, -1))
- cam_up = quat @ Vector((0, 1, 0))
- cam_right = cam_dir.cross(cam_up).normalized()
- cam_up_vec = cam_right.cross(cam_dir).normalized()
-
- cam_res_x, cam_res_y = _get_camera_resolution(camera, scene)
- fov = camera.data.angle_x
- if cam_res_y > cam_res_x:
- fov = 2 * atan(tan(fov / 2) * cam_res_x / cam_res_y)
- filmw = tan(fov / 2)
- aspect = cam_res_x / cam_res_y
- filmh = filmw / 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,
- cam_loc, # keep mathutils Vector for BVH calls
- ))
-
- # Build scene-wide BVH for occlusion (matches ShaderNodeRaycast behaviour,
- # which checks ALL visible scene geometry, not just the current object).
- 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():
- continue
- tmp = bmesh.new()
- tmp.from_mesh(scene_obj.data)
- tmp.transform(scene_obj.matrix_world)
- for v in tmp.verts:
- scene_bm.verts.new(v.co)
- scene_bm.verts.ensure_lookup_table()
- for f in tmp.faces:
- try:
- scene_bm.faces.new(
- [scene_bm.verts[v.index + vert_offset] for v in f.verts])
- except ValueError:
- pass # skip degenerate / duplicate faces
- vert_offset += len(tmp.verts)
- tmp.free()
- scene_bm.normal_update()
- scene_tree = BVHTree.FromBMesh(scene_bm)
- scene_bm.free()
- print(f"[StableGen] Scene BVH built ({vert_offset} verts across visible meshes)")
-
- # Per-object bake -------------------------------------------------------------
- n_cameras = len(cameras)
- for obj_idx, obj in enumerate(objects):
- bm = bmesh.new()
- bm.from_mesh(obj.data)
- bm.transform(obj.matrix_world)
- bm.normal_update()
-
- n_verts = len(bm.verts)
- # Build numpy arrays (Nx3) for positions and normals
- positions_np = np.array([v.co[:] for v in bm.verts], dtype=np.float64)
- normals_np = np.array([v.normal[:] for v in bm.verts], dtype=np.float64)
- bm.free()
-
- for cam_idx, camera in enumerate(cameras):
- print(f"[StableGen] Baking visibility: object {obj_idx+1}/{len(objects)}, "
- f"camera {cam_idx+1}/{n_cameras} ({n_verts} verts)")
-
- weights = np.zeros(n_verts, dtype=np.float64)
-
- if cam_idx > stop_index:
- # Store zeros (camera disabled) and continue
- attr_name = f"_SG_VisWeight_{cam_idx}_{mat_id}"
- existing = obj.data.attributes.get(attr_name)
- if existing:
- obj.data.attributes.remove(existing)
- attr = obj.data.attributes.new(
- name=attr_name, type='FLOAT', domain='POINT')
- 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]
-
- # ── 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)
-
- # Angle threshold
- dot_dn = np.abs(np.einsum('ij,ij->i', direction, normals_np))
- dot_dn = np.clip(dot_dn, 0.0, 1.0)
- angle_deg = np.degrees(np.arccos(dot_dn))
- valid &= angle_deg < angle_threshold
-
- # ── BVH raycast only for candidates that passed filters ───
- candidate_indices = np.nonzero(valid)[0]
- occluded = np.zeros(n_verts, dtype=bool)
- for vi in candidate_indices:
- d_mu = Vector(direction[vi])
- hit_loc, _, _, hit_dist = scene_tree.ray_cast(
- cam_loc_mu, d_mu, float(dists[vi]) + 0.01)
- if hit_loc is not None and dists[vi] > hit_dist + 0.001:
- occluded[vi] = True
-
- valid &= ~occluded
-
- # ── Silhouette erosion ─────────────────────────────────────
- # Cast offset rays around each visible vertex. If any offset
- # ray hits an occluder significantly closer, this vertex is
- # near a depth discontinuity (e.g. hand edge on body) and its
- # weight should be zeroed so another camera fills that area.
- silhouette_margin_px = getattr(scene, 'sg_silhouette_margin', 3)
- if silhouette_margin_px > 0:
- # Angular offset per pixel (radians)
- cam_res_x_n, cam_res_y_n = _get_camera_resolution(camera, scene)
- rad_per_px = fov / max(cam_res_x_n, 1)
- offset_angle = rad_per_px * silhouette_margin_px
-
- visible_indices = np.nonzero(valid)[0]
- near_silhouette = np.zeros(n_verts, dtype=bool)
-
- # Offset directions in camera space
- offsets_right = cam_right_np * offset_angle
- offsets_up = cam_up_np * offset_angle
-
- use_8_rays = getattr(scene, 'sg_silhouette_rays', '4') == '8'
- if use_8_rays:
- diag = offset_angle * 0.7071067811865476 # 1/sqrt(2)
- offsets_diag_ru = cam_right_np * diag + cam_up_np * diag
- offsets_diag_rd = cam_right_np * diag - cam_up_np * diag
- all_offsets = (offsets_right, -offsets_right,
- offsets_up, -offsets_up,
- offsets_diag_ru, -offsets_diag_ru,
- offsets_diag_rd, -offsets_diag_rd)
- else:
- all_offsets = (offsets_right, -offsets_right,
- offsets_up, -offsets_up)
-
- for vi in visible_indices:
- d_base = direction[vi]
- dist_v = float(dists[vi])
- is_edge = False
- for off in all_offsets:
- d_off = d_base + off
- # 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)
- h_loc, _, _, h_dist = scene_tree.ray_cast(
- cam_loc_mu, d_off_mu, dist_v + 0.01)
- depth_thr = getattr(scene, 'sg_silhouette_depth', 0.05)
- if h_loc is not None and dist_v > h_dist + depth_thr:
- is_edge = True
- break
- if is_edge:
- near_silhouette[vi] = True
-
- valid &= ~near_silhouette
-
- # ── Vectorised weight + feather ────────────────────────────
- w = np.power(np.maximum(dot_dn, 1e-12), power_exponent)
-
- if feather_width > 0:
- nx = np.abs(x_proj) / filmw
- ny = np.abs(y_proj) / filmh
- edge = np.maximum(nx, ny)
- low = 1.0 - feather_width
- need_feather = edge > low
- t = np.clip((edge - low) / feather_width, 0.0, 1.0)
- ss = t * t * (3.0 - 2.0 * t) # smoothstep
- ff = np.power(np.maximum(1.0 - ss, 0.0),
- max(feather_gamma, 0.01))
- w = np.where(need_feather, w * ff, w)
-
- weights[valid] = w[valid]
-
- # Store as FLOAT POINT-domain attribute on the *base* mesh
- attr_name = f"_SG_VisWeight_{cam_idx}_{mat_id}"
- existing = obj.data.attributes.get(attr_name)
- if existing:
- obj.data.attributes.remove(existing)
- attr = obj.data.attributes.new(
- name=attr_name, type='FLOAT', domain='POINT')
- attr.data.foreach_set("value", weights.tolist())
-
- # Force UI update between cameras to keep Blender responsive
- if context.window:
- context.window.cursor_set('WAIT')
- for area in context.screen.areas:
- area.tag_redraw()
-
- obj.data.update()
-
-
-def create_native_raycast_visibility(nodes, links, camera, geometry, context, i, mat_id, stop_index):
- """
- Creates a visibility weight node group using the native Raycast shader node (Blender 5.1+).
- Replicates the OSL raycast.osl shader logic:
- 1. Frustum check (is the surface within the camera FOV)
- 2. Occlusion check (Raycast from camera, compare Hit Distance with expected distance)
- 3. Angle check (surface normal vs ray direction)
- 4. Weight = pow(orthogonality, Power)
-
- Returns: (weight_output_node, subtract_node, normalize_node, length_node,
- camera_loc_node, camera_fov_node, camera_aspect_node, camera_dir_node, camera_up_node)
- """
- x_base = -400
- y_base = (-800) * i
-
- # --- Camera location (CombineXYZ) ---
- add_camera_loc = nodes.new("ShaderNodeCombineXYZ")
- add_camera_loc.location = (-600, 200 + 300 * i)
- add_camera_loc.inputs[0].default_value = camera.location.x
- add_camera_loc.inputs[1].default_value = camera.location.y
- add_camera_loc.inputs[2].default_value = camera.location.z
-
- # --- Direction: Geometry.Position - CameraLoc ---
- subtract = nodes.new("ShaderNodeVectorMath")
- subtract.operation = 'SUBTRACT'
- subtract.location = (x_base, -300 + y_base)
- subtract.inputs[1].default_value = camera.location
-
- normalize_node = nodes.new("ShaderNodeVectorMath")
- normalize_node.operation = 'NORMALIZE'
- normalize_node.location = (x_base, -500 + y_base)
-
- links.new(geometry.outputs["Position"], subtract.inputs[0])
- links.new(subtract.outputs["Vector"], normalize_node.inputs[0])
-
- # --- Length (camera-to-surface distance) ---
- length_node = nodes.new("ShaderNodeVectorMath")
- length_node.operation = 'LENGTH'
- length_node.location = (x_base, 200 * (i + 1))
- links.new(subtract.outputs["Vector"], length_node.inputs[0])
-
- # --- Camera FOV ---
- camera_fov_node = nodes.new("ShaderNodeValue")
- camera_fov_node.location = (-800, 200 + 300 * i)
- cam_res_x, cam_res_y = _get_camera_resolution(camera, context.scene)
- fov = camera.data.angle_x
- if cam_res_y > cam_res_x:
- fov = 2 * atan(tan(fov / 2) * cam_res_x / cam_res_y)
- camera_fov_node.outputs[0].default_value = fov
-
- # --- Camera aspect ratio ---
- camera_aspect_node = nodes.new("ShaderNodeValue")
- camera_aspect_node.location = (-800, 100 + 300 * i)
- camera_aspect_node.outputs[0].default_value = cam_res_x / cam_res_y
-
- # --- Camera direction (CombineXYZ) ---
- cam_dir_vec = camera.matrix_world.to_quaternion() @ Vector((0, 0, -1))
- camera_dir_node = nodes.new("ShaderNodeCombineXYZ")
- camera_dir_node.location = (-800, 0 + 300 * i)
- camera_dir_node.inputs[0].default_value = cam_dir_vec.x
- camera_dir_node.inputs[1].default_value = cam_dir_vec.y
- camera_dir_node.inputs[2].default_value = cam_dir_vec.z
-
- # --- Camera up (CombineXYZ) ---
- cam_up_vec = camera.matrix_world.to_quaternion() @ Vector((0, 1, 0))
- camera_up_node = nodes.new("ShaderNodeCombineXYZ")
- camera_up_node.location = (-800, -100 + 300 * i)
- camera_up_node.inputs[0].default_value = cam_up_vec.x
- camera_up_node.inputs[1].default_value = cam_up_vec.y
- camera_up_node.inputs[2].default_value = cam_up_vec.z
-
- # ========================================
- # FRUSTUM CHECK (using vector math nodes)
- # ========================================
- norm_forward = nodes.new("ShaderNodeVectorMath")
- norm_forward.operation = 'NORMALIZE'
- norm_forward.location = (-700, y_base - 100)
- links.new(camera_dir_node.outputs["Vector"], norm_forward.inputs[0])
-
- cross_fwd_up = nodes.new("ShaderNodeVectorMath")
- cross_fwd_up.operation = 'CROSS_PRODUCT'
- cross_fwd_up.location = (-700, y_base - 200)
- links.new(norm_forward.outputs["Vector"], cross_fwd_up.inputs[0])
- links.new(camera_up_node.outputs["Vector"], cross_fwd_up.inputs[1])
-
- norm_right = nodes.new("ShaderNodeVectorMath")
- norm_right.operation = 'NORMALIZE'
- norm_right.location = (-600, y_base - 200)
- links.new(cross_fwd_up.outputs["Vector"], norm_right.inputs[0])
-
- cross_right_fwd = nodes.new("ShaderNodeVectorMath")
- cross_right_fwd.operation = 'CROSS_PRODUCT'
- cross_right_fwd.location = (-700, y_base - 300)
- links.new(norm_right.outputs["Vector"], cross_right_fwd.inputs[0])
- links.new(norm_forward.outputs["Vector"], cross_right_fwd.inputs[1])
-
- norm_up_vec = nodes.new("ShaderNodeVectorMath")
- norm_up_vec.operation = 'NORMALIZE'
- norm_up_vec.location = (-600, y_base - 300)
- links.new(cross_right_fwd.outputs["Vector"], norm_up_vec.inputs[0])
-
- neg_forward = nodes.new("ShaderNodeVectorMath")
- neg_forward.operation = 'SCALE'
- neg_forward.location = (-600, y_base - 400)
- neg_forward.inputs["Scale"].default_value = -1.0
- links.new(norm_forward.outputs["Vector"], neg_forward.inputs[0])
-
- dot_d_right = nodes.new("ShaderNodeVectorMath")
- dot_d_right.operation = 'DOT_PRODUCT'
- dot_d_right.location = (-400, y_base - 200)
- links.new(normalize_node.outputs["Vector"], dot_d_right.inputs[0])
- links.new(norm_right.outputs["Vector"], dot_d_right.inputs[1])
-
- dot_d_up = nodes.new("ShaderNodeVectorMath")
- dot_d_up.operation = 'DOT_PRODUCT'
- dot_d_up.location = (-400, y_base - 300)
- links.new(normalize_node.outputs["Vector"], dot_d_up.inputs[0])
- links.new(norm_up_vec.outputs["Vector"], dot_d_up.inputs[1])
-
- dot_d_neg_fwd = nodes.new("ShaderNodeVectorMath")
- dot_d_neg_fwd.operation = 'DOT_PRODUCT'
- dot_d_neg_fwd.location = (-400, y_base - 400)
- links.new(normalize_node.outputs["Vector"], dot_d_neg_fwd.inputs[0])
- links.new(neg_forward.outputs["Vector"], dot_d_neg_fwd.inputs[1])
-
- cam_z_check = nodes.new("ShaderNodeMath")
- cam_z_check.operation = 'LESS_THAN'
- cam_z_check.location = (-200, y_base - 400)
- cam_z_check.inputs[1].default_value = 0.0
- links.new(dot_d_neg_fwd.outputs["Value"], cam_z_check.inputs[0])
-
- fov_half = nodes.new("ShaderNodeMath")
- fov_half.operation = 'MULTIPLY'
- fov_half.location = (-400, y_base - 500)
- fov_half.inputs[1].default_value = 0.5
- links.new(camera_fov_node.outputs[0], fov_half.inputs[0])
-
- filmw = nodes.new("ShaderNodeMath")
- filmw.operation = 'TANGENT'
- filmw.location = (-300, y_base - 500)
- links.new(fov_half.outputs[0], filmw.inputs[0])
-
- filmh = nodes.new("ShaderNodeMath")
- filmh.operation = 'DIVIDE'
- filmh.location = (-200, y_base - 500)
- links.new(filmw.outputs[0], filmh.inputs[0])
- links.new(camera_aspect_node.outputs[0], filmh.inputs[1])
-
- neg_cam_z = nodes.new("ShaderNodeMath")
- neg_cam_z.operation = 'MULTIPLY'
- neg_cam_z.location = (-200, y_base - 600)
- neg_cam_z.inputs[1].default_value = -1.0
- links.new(dot_d_neg_fwd.outputs["Value"], neg_cam_z.inputs[0])
-
- x_proj = nodes.new("ShaderNodeMath")
- x_proj.operation = 'DIVIDE'
- x_proj.location = (-100, y_base - 200)
- links.new(dot_d_right.outputs["Value"], x_proj.inputs[0])
- links.new(neg_cam_z.outputs[0], x_proj.inputs[1])
-
- y_proj = nodes.new("ShaderNodeMath")
- y_proj.operation = 'DIVIDE'
- y_proj.location = (-100, y_base - 300)
- links.new(dot_d_up.outputs["Value"], y_proj.inputs[0])
- links.new(neg_cam_z.outputs[0], y_proj.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])
-
- x_in_bounds = nodes.new("ShaderNodeMath")
- x_in_bounds.operation = 'LESS_THAN'
- x_in_bounds.location = (100, y_base - 200)
- links.new(abs_x.outputs[0], x_in_bounds.inputs[0])
- links.new(filmw.outputs[0], x_in_bounds.inputs[1])
-
- 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])
-
- y_in_bounds = nodes.new("ShaderNodeMath")
- y_in_bounds.operation = 'LESS_THAN'
- y_in_bounds.location = (100, y_base - 300)
- links.new(abs_y.outputs[0], y_in_bounds.inputs[0])
- links.new(filmh.outputs[0], y_in_bounds.inputs[1])
-
- frustum_xy = nodes.new("ShaderNodeMath")
- frustum_xy.operation = 'MINIMUM'
- frustum_xy.location = (200, y_base - 250)
- links.new(x_in_bounds.outputs[0], frustum_xy.inputs[0])
- links.new(y_in_bounds.outputs[0], frustum_xy.inputs[1])
-
- frustum_ok = nodes.new("ShaderNodeMath")
- frustum_ok.operation = 'MINIMUM'
- frustum_ok.location = (300, y_base - 300)
- links.new(cam_z_check.outputs[0], frustum_ok.inputs[0])
- links.new(frustum_xy.outputs[0], frustum_ok.inputs[1])
-
- # ========================================
- # RAYCAST NODE (occlusion check)
- # ========================================
- raycast = nodes.new("ShaderNodeRaycast")
- raycast.location = (x_base + 200, y_base)
- links.new(add_camera_loc.outputs["Vector"], raycast.inputs["Position"])
- links.new(normalize_node.outputs["Vector"], raycast.inputs["Direction"])
- threshold_plus = nodes.new("ShaderNodeMath")
- threshold_plus.operation = 'ADD'
- threshold_plus.location = (x_base + 100, y_base + 100)
- threshold_plus.inputs[1].default_value = 0.01
- links.new(length_node.outputs["Value"], threshold_plus.inputs[0])
- links.new(threshold_plus.outputs[0], raycast.inputs["Length"])
-
- hit_dist_plus = nodes.new("ShaderNodeMath")
- hit_dist_plus.operation = 'ADD'
- hit_dist_plus.location = (x_base + 400, y_base + 50)
- hit_dist_plus.inputs[1].default_value = 0.001
- links.new(raycast.outputs["Hit Distance"], hit_dist_plus.inputs[0])
-
- dist_check = nodes.new("ShaderNodeMath")
- dist_check.operation = 'LESS_THAN'
- dist_check.location = (x_base + 500, y_base + 50)
- links.new(length_node.outputs["Value"], dist_check.inputs[0])
- links.new(hit_dist_plus.outputs[0], dist_check.inputs[1])
-
- occlusion_ok = nodes.new("ShaderNodeMath")
- occlusion_ok.operation = 'MINIMUM'
- occlusion_ok.location = (x_base + 600, y_base + 50)
- links.new(raycast.outputs["Is Hit"], occlusion_ok.inputs[0])
- links.new(dist_check.outputs[0], occlusion_ok.inputs[1])
-
- # ========================================
- # ANGLE CHECK + WEIGHT
- # ========================================
- dot_d_normal = nodes.new("ShaderNodeVectorMath")
- dot_d_normal.operation = 'DOT_PRODUCT'
- dot_d_normal.location = (x_base + 300, y_base - 150)
- links.new(normalize_node.outputs["Vector"], dot_d_normal.inputs[0])
- links.new(geometry.outputs["Normal"], dot_d_normal.inputs[1])
-
- clamp_dot = nodes.new("ShaderNodeMath")
- clamp_dot.operation = 'MAXIMUM'
- clamp_dot.location = (x_base + 400, y_base - 150)
- clamp_dot.inputs[1].default_value = -1.0
- links.new(dot_d_normal.outputs["Value"], clamp_dot.inputs[0])
-
- clamp_dot2 = nodes.new("ShaderNodeMath")
- clamp_dot2.operation = 'MINIMUM'
- clamp_dot2.location = (x_base + 500, y_base - 150)
- clamp_dot2.inputs[1].default_value = 1.0
- links.new(clamp_dot.outputs[0], clamp_dot2.inputs[0])
-
- abs_dot = nodes.new("ShaderNodeMath")
- abs_dot.operation = 'ABSOLUTE'
- abs_dot.location = (x_base + 600, y_base - 150)
- links.new(clamp_dot2.outputs[0], abs_dot.inputs[0])
-
- acos_node = nodes.new("ShaderNodeMath")
- acos_node.operation = 'ARCCOSINE'
- acos_node.location = (x_base + 700, y_base - 150)
- links.new(abs_dot.outputs[0], acos_node.inputs[0])
-
- to_degrees = nodes.new("ShaderNodeMath")
- to_degrees.operation = 'MULTIPLY'
- to_degrees.location = (x_base + 800, y_base - 150)
- to_degrees.inputs[1].default_value = 180.0 / pi
- links.new(acos_node.outputs[0], to_degrees.inputs[0])
-
- angle_check = nodes.new("ShaderNodeMath")
- angle_check.operation = 'LESS_THAN'
- angle_check.location = (x_base + 900, y_base - 150)
- angle_check.inputs[1].default_value = context.scene.discard_factor
- angle_check.label = f"AngleThreshold-{i}-{mat_id}"
- links.new(to_degrees.outputs[0], angle_check.inputs[0])
-
- power_node = nodes.new("ShaderNodeMath")
- power_node.operation = 'POWER'
- power_node.location = (x_base + 700, y_base - 250)
- power_node.inputs[1].default_value = context.scene.weight_exponent
- power_node.label = 'power_weight'
- links.new(abs_dot.outputs[0], power_node.inputs[0])
-
- # ========================================
- # COMBINE ALL: frustum_ok * occlusion_ok * angle_ok * weight
- # ========================================
- combine1 = nodes.new("ShaderNodeMath")
- combine1.operation = 'MULTIPLY'
- combine1.location = (x_base + 800, y_base)
- links.new(frustum_ok.outputs[0], combine1.inputs[0])
- links.new(occlusion_ok.outputs[0], combine1.inputs[1])
-
- combine2 = nodes.new("ShaderNodeMath")
- combine2.operation = 'MULTIPLY'
- combine2.location = (x_base + 900, y_base)
- links.new(combine1.outputs[0], combine2.inputs[0])
- links.new(angle_check.outputs[0], combine2.inputs[1])
-
- final_weight = nodes.new("ShaderNodeMath")
- final_weight.operation = 'MULTIPLY'
- final_weight.location = (x_base + 1000, y_base)
- final_weight.label = f"Angle-{i}-{mat_id}"
- links.new(combine2.outputs[0], final_weight.inputs[0])
- links.new(power_node.outputs[0], final_weight.inputs[1])
-
- return (final_weight, subtract, normalize_node, length_node,
- add_camera_loc, camera_fov_node, camera_aspect_node,
- camera_dir_node, camera_up_node)
-
-
-def create_native_feather(nodes, links, normalize_node, camera_fov_node, camera_aspect_node,
- camera_dir_node, camera_up_node, 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.
-
- Returns the feather output node (ShaderNodeMath).
- """
- x_base = -400
- y_base = (-800) * i - 400 # Offset below the raycast nodes
-
- # Build camera basis (reuse from raycast, but we need the forward/right/up)
- norm_forward = nodes.new("ShaderNodeVectorMath")
- norm_forward.operation = 'NORMALIZE'
- norm_forward.location = (x_base - 300, y_base)
- links.new(camera_dir_node.outputs["Vector"], norm_forward.inputs[0])
-
- cross_fwd_up = nodes.new("ShaderNodeVectorMath")
- cross_fwd_up.operation = 'CROSS_PRODUCT'
- cross_fwd_up.location = (x_base - 300, y_base - 100)
- links.new(norm_forward.outputs["Vector"], cross_fwd_up.inputs[0])
- links.new(camera_up_node.outputs["Vector"], cross_fwd_up.inputs[1])
-
- norm_right = nodes.new("ShaderNodeVectorMath")
- norm_right.operation = 'NORMALIZE'
- norm_right.location = (x_base - 200, y_base - 100)
- links.new(cross_fwd_up.outputs["Vector"], norm_right.inputs[0])
-
- cross_right_fwd = nodes.new("ShaderNodeVectorMath")
- cross_right_fwd.operation = 'CROSS_PRODUCT'
- cross_right_fwd.location = (x_base - 300, y_base - 200)
- links.new(norm_right.outputs["Vector"], cross_right_fwd.inputs[0])
- links.new(norm_forward.outputs["Vector"], cross_right_fwd.inputs[1])
-
- norm_up_vec = nodes.new("ShaderNodeVectorMath")
- norm_up_vec.operation = 'NORMALIZE'
- norm_up_vec.location = (x_base - 200, y_base - 200)
- links.new(cross_right_fwd.outputs["Vector"], norm_up_vec.inputs[0])
-
- neg_forward = nodes.new("ShaderNodeVectorMath")
- neg_forward.operation = 'SCALE'
- neg_forward.location = (x_base - 200, y_base - 300)
- neg_forward.inputs["Scale"].default_value = -1.0
- links.new(norm_forward.outputs["Vector"], neg_forward.inputs[0])
-
- # cam_x = dot(d, right)
- dot_d_right = nodes.new("ShaderNodeVectorMath")
- dot_d_right.operation = 'DOT_PRODUCT'
- dot_d_right.location = (x_base, y_base - 100)
- links.new(normalize_node.outputs["Vector"], dot_d_right.inputs[0])
- links.new(norm_right.outputs["Vector"], dot_d_right.inputs[1])
-
- # cam_y = dot(d, upVec)
- dot_d_up = nodes.new("ShaderNodeVectorMath")
- dot_d_up.operation = 'DOT_PRODUCT'
- dot_d_up.location = (x_base, y_base - 200)
- links.new(normalize_node.outputs["Vector"], dot_d_up.inputs[0])
- links.new(norm_up_vec.outputs["Vector"], dot_d_up.inputs[1])
-
- # cam_z = dot(d, -forward)
- dot_d_neg_fwd = nodes.new("ShaderNodeVectorMath")
- dot_d_neg_fwd.operation = 'DOT_PRODUCT'
- dot_d_neg_fwd.location = (x_base, y_base - 300)
- links.new(normalize_node.outputs["Vector"], dot_d_neg_fwd.inputs[0])
- links.new(neg_forward.outputs["Vector"], dot_d_neg_fwd.inputs[1])
-
- # cam_z >= 0 check (behind camera -> result = 0)
- cam_z_check = nodes.new("ShaderNodeMath")
- cam_z_check.operation = 'LESS_THAN'
- cam_z_check.location = (x_base + 100, y_base - 300)
- cam_z_check.inputs[1].default_value = 0.0
- links.new(dot_d_neg_fwd.outputs["Value"], cam_z_check.inputs[0])
-
- # filmw = tan(FOV * 0.5)
- fov_half = nodes.new("ShaderNodeMath")
- fov_half.operation = 'MULTIPLY'
- fov_half.location = (x_base, y_base - 400)
- fov_half.inputs[1].default_value = 0.5
- links.new(camera_fov_node.outputs[0], fov_half.inputs[0])
-
- filmw = nodes.new("ShaderNodeMath")
- filmw.operation = 'TANGENT'
- filmw.location = (x_base + 100, y_base - 400)
- links.new(fov_half.outputs[0], filmw.inputs[0])
-
- # filmh = filmw / CameraAspect
- filmh = nodes.new("ShaderNodeMath")
- filmh.operation = 'DIVIDE'
- filmh.location = (x_base + 200, y_base - 400)
- links.new(filmw.outputs[0], filmh.inputs[0])
- links.new(camera_aspect_node.outputs[0], filmh.inputs[1])
-
- # neg_cam_z = -cam_z
- neg_cam_z = nodes.new("ShaderNodeMath")
- neg_cam_z.operation = 'MULTIPLY'
- neg_cam_z.location = (x_base + 100, y_base - 500)
- neg_cam_z.inputs[1].default_value = -1.0
- links.new(dot_d_neg_fwd.outputs["Value"], neg_cam_z.inputs[0])
-
- # x_proj = cam_x / -cam_z
- x_proj = nodes.new("ShaderNodeMath")
- x_proj.operation = 'DIVIDE'
- x_proj.location = (x_base + 200, y_base - 100)
- links.new(dot_d_right.outputs["Value"], x_proj.inputs[0])
- links.new(neg_cam_z.outputs[0], x_proj.inputs[1])
-
- # y_proj = cam_y / -cam_z
- y_proj = nodes.new("ShaderNodeMath")
- y_proj.operation = 'DIVIDE'
- y_proj.location = (x_base + 200, y_base - 200)
- links.new(dot_d_up.outputs["Value"], y_proj.inputs[0])
- links.new(neg_cam_z.outputs[0], y_proj.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])
-
- nx = nodes.new("ShaderNodeMath")
- nx.operation = 'DIVIDE'
- nx.location = (x_base + 400, y_base - 100)
- links.new(abs_x.outputs[0], nx.inputs[0])
- links.new(filmw.outputs[0], nx.inputs[1])
-
- # ny = abs(y_proj) / filmh
- 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])
-
- ny = nodes.new("ShaderNodeMath")
- ny.operation = 'DIVIDE'
- ny.location = (x_base + 400, y_base - 200)
- links.new(abs_y.outputs[0], ny.inputs[0])
- links.new(filmh.outputs[0], ny.inputs[1])
-
- # edge = max(nx, ny)
- edge = nodes.new("ShaderNodeMath")
- edge.operation = 'MAXIMUM'
- edge.location = (x_base + 500, y_base - 150)
- links.new(nx.outputs[0], edge.inputs[0])
- links.new(ny.outputs[0], edge.inputs[1])
-
- # Get feather parameters
- if context.scene.visibility_vignette:
- feather_val = context.scene.visibility_vignette_width
- gamma_val = context.scene.visibility_vignette_softness
- else:
- feather_val = 0.0
- gamma_val = 1.0
-
- feather_val = max(0.0, min(feather_val, 0.49))
-
- if feather_val > 0.0:
- # edge_factor = 1.0 - smoothstep(1.0 - feather, 1.0, edge)
- # Approximate smoothstep using a Map Range node with SMOOTHSTEP interpolation
- map_range = nodes.new("ShaderNodeMapRange")
- map_range.location = (x_base + 600, y_base - 150)
- map_range.interpolation_type = 'SMOOTHSTEP'
- map_range.inputs["From Min"].default_value = 1.0 - feather_val
- map_range.inputs["From Max"].default_value = 1.0
- map_range.inputs["To Min"].default_value = 1.0 # Invert: edge=low -> factor=1
- map_range.inputs["To Max"].default_value = 0.0 # edge=high -> factor=0
- links.new(edge.outputs[0], map_range.inputs["Value"])
-
- # edge_factor = pow(smoothstep_result, gamma)
- pow_node = nodes.new("ShaderNodeMath")
- pow_node.operation = 'POWER'
- pow_node.location = (x_base + 700, y_base - 150)
- pow_node.inputs[1].default_value = max(0.01, gamma_val)
- links.new(map_range.outputs[0], pow_node.inputs[0])
-
- # Multiply by cam_z_check (0 if behind camera)
- final_feather = nodes.new("ShaderNodeMath")
- final_feather.operation = 'MULTIPLY'
- final_feather.location = (x_base + 800, y_base - 150)
- final_feather.label = f"Feather-{i}-{mat_id}"
- links.new(cam_z_check.outputs[0], final_feather.inputs[0])
- links.new(pow_node.outputs[0], final_feather.inputs[1])
- else:
- # No feathering: result = cam_z_check (1 if in front, 0 behind)
- final_feather = cam_z_check
- final_feather.label = f"Feather-{i}-{mat_id}"
-
- return final_feather
-
-
-_SG_VORONOI_PLACEHOLDER_NAME = "_SG_Voronoi_Placeholder"
-
-def _get_voronoi_placeholder(color=(1.0, 0.0, 1.0)):
- """Get or create a 1x1 solid-colour image used as placeholder for
- non-generated cameras in Voronoi projection mode. The colour is
- updated every call so it always matches the current setting."""
- img = bpy.data.images.get(_SG_VORONOI_PLACEHOLDER_NAME)
- if img is None:
- img = bpy.data.images.new(_SG_VORONOI_PLACEHOLDER_NAME, width=1, height=1)
- img.colorspace_settings.name = 'Non-Color'
- img.pack()
- img.pixels[:] = [color[0], color[1], color[2], 1.0]
- return img
-
-
-def project_image(context, to_project, mat_id, stop_index=1000000):
- """
- Projects an image onto all mesh objects using UV Project Modifier.
- """
- def build_mix_tree(shaders, weight_nodes, nodes_collection, links, last_node=None, level=0, stop_index=1000000):
- """
- Recursively builds a binary tree of mix shader nodes.
- The mix factor between two shader nodes is computed dynamically using
- the outputs of weight_nodes assumed to come from OSL/maths nodes.
-
- For each pair, the total weight is created using an ADD node.
- Then a DIVIDE node computes:
- mix_factor = weight_A / (weight_A + weight_B)
- which is connected to the Mix Shader node's Fac input.
-
- Params:
- shaders: list of shader nodes (e.g., Principled BSDF nodes).
- weight_nodes: list of nodes whose outputs provide the dynamic weight values.
- (e.g. the less_than nodes outputs)
- nodes_collection: the node tree's nodes collection.
- links: the node tree's links collection.
- Returns:
- A tuple (final_shader, final_weight_node).
- """
-
- def _combine_angle_feather(nodes_col, lnk, cr_angle_out, cr_feather_out, loc):
- """Combine angle-ramp and feather-ramp outputs into a single
- visibility weight: ``angle_vis × feather_vis``.
-
- Returns the output node of the final MULTIPLY.
- """
- x, y = loc
- mult = nodes_col.new("ShaderNodeMath")
- mult.operation = 'MULTIPLY'
- mult.location = (x, y)
- lnk.new(cr_angle_out, mult.inputs[0])
- lnk.new(cr_feather_out, mult.inputs[1])
- return mult
-
- # Compute offsets based on recursion level
- is_local_edit = (context.scene.generation_method == 'local_edit' or (context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method == 'local_edit'))
- if is_local_edit:
- last_node_x = last_node.location[0] if last_node else 0
- x_offset = 1000 + level * 800 + last_node_x + 200 if context.scene.early_priority else 1000 + level * 600 + last_node_x + 200
- else:
- x_offset = 1000 + level * 800 if context.scene.early_priority else 1000 + level * 600
- y_offset = 0
- if len(shaders) == 1:
- # Base-case: Mix single color with fallback magenta
- final_mix = nodes_collection.new("ShaderNodeMixRGB")
- final_mix.location = (x_offset - 200, y_offset)
- # Convert fallback_color from 3 to 4 components by appending alpha 1.0
- final_mix.inputs["Color2"].default_value = (*context.scene.fallback_color, 1.0)
- # Connect the single color output to Color1
- links.new(shaders[0].outputs[0], final_mix.inputs["Color1"])
- if last_node:
- # Connect the final mix node to the last node
- links.new(last_node.outputs[0], final_mix.inputs["Color2"])
- # Create a compare node to drive the mix factor
-
- if is_local_edit:
- w_node = weight_nodes[0]
- if isinstance(w_node, tuple):
- angle_node = w_node[0]
- feather_node = w_node[1]
- ef_node = w_node[2] if len(w_node) >= 3 else None
-
- # Create Ramps (Output Visibility: 0=Invis, 1=Vis)
- # Angle Ramp
- cr_angle = nodes_collection.new("ShaderNodeValToRGB")
- cr_angle.location = (x_offset - 500, y_offset)
- cr_angle.color_ramp.elements[0].position = context.scene.refine_angle_ramp_pos_0 if context.scene.refine_angle_ramp_active else 0.0
- cr_angle.color_ramp.elements[0].color = (0, 0, 0, 1) # Black (Invis)
- cr_angle.color_ramp.elements[1].position = context.scene.refine_angle_ramp_pos_1 if context.scene.refine_angle_ramp_active else 0.0
- cr_angle.color_ramp.elements[1].color = (1, 1, 1, 1) # White (Vis)
- cr_angle.color_ramp.interpolation = 'LINEAR'
- links.new(angle_node.outputs[0], cr_angle.inputs[0])
-
- # Feather Ramp
- cr_feather = nodes_collection.new("ShaderNodeValToRGB")
- cr_feather.location = (x_offset - 500, y_offset - 200)
- cr_feather.color_ramp.elements[0].position = context.scene.refine_feather_ramp_pos_0 if context.scene.visibility_vignette else 0.0
- cr_feather.color_ramp.elements[0].color = (0, 0, 0, 1) # Black (Invis)
- cr_feather.color_ramp.elements[1].position = context.scene.refine_feather_ramp_pos_1 if context.scene.visibility_vignette else 0.0
- cr_feather.color_ramp.elements[1].color = (1, 1, 1, 1) # White (Vis)
- cr_feather.color_ramp.interpolation = 'LINEAR'
- links.new(feather_node.outputs[0], cr_feather.inputs[0])
-
- # Combine angle and feather visibility
- mult = _combine_angle_feather(
- nodes_collection, links,
- cr_angle.outputs[0], cr_feather.outputs[0],
- loc=(x_offset - 300, y_offset)
- )
-
- # Optionally multiply by edge-feather mask
- if ef_node is not None:
- ef_mult = nodes_collection.new("ShaderNodeMath")
- ef_mult.operation = 'MULTIPLY'
- ef_mult.location = (x_offset - 200, y_offset - 80)
- links.new(mult.outputs[0], ef_mult.inputs[0])
- links.new(ef_node.outputs[0], ef_mult.inputs[1])
- weight_out = ef_mult
- else:
- weight_out = mult
-
- # Invert (Convert Visibility to Mix Factor: 1=Vis->0=Proj, 0=Invis->1=Orig)
- invert = nodes_collection.new("ShaderNodeMath")
- invert.operation = 'SUBTRACT'
- invert.location = (x_offset - 150, y_offset)
- invert.inputs[0].default_value = 1.0
- links.new(weight_out.outputs[0], invert.inputs[1])
-
- compare_node = invert # For return
- links.new(invert.outputs[0], final_mix.inputs["Fac"])
- else:
- # Fallback if not tuple (e.g. stop_index logic used LessThan node)
- # If it's a sum of processed weights (from multi-camera recursion), we need to invert it
- # to get the Mix Factor (1 - TotalVis)
- if is_local_edit:
- invert = nodes_collection.new("ShaderNodeMath")
- invert.operation = 'SUBTRACT'
- invert.location = (x_offset - 150, y_offset)
- invert.inputs[0].default_value = 1.0
- links.new(w_node.outputs[0], invert.inputs[1])
- links.new(invert.outputs[0], final_mix.inputs["Fac"])
- compare_node = invert
- else:
- links.new(w_node.outputs[0], final_mix.inputs["Fac"])
- compare_node = w_node
- else:
- compare_node = nodes_collection.new("ShaderNodeMath")
- compare_node.operation = 'COMPARE'
- # Value to compare against (Value 2)
- compare_node.inputs[1].default_value = 0.0
- # Epsilon for comparison
- compare_node.inputs[2].default_value = 0.0
- compare_node.location = (x_offset - 500, y_offset)
- # Connect final add node to compare node
- links.new(weight_nodes[0].outputs[0], compare_node.inputs[0])
- links.new(compare_node.outputs[0], final_mix.inputs["Fac"])
-
- return final_mix, compare_node
-
- # ── Helper: extract final weight output from a weight_node item ────
- def _get_weight_output(w_item, v_offset):
- """Process a weight_node item (tuple or plain node) through ramps
- and return the output socket. Used by the normalization pre-pass."""
- if isinstance(w_item, tuple):
- angle_node = w_item[0]
- feather_node = w_item[1]
- ef_node = w_item[2] if len(w_item) >= 3 else None
-
- # Angle Ramp
- cr_angle = nodes_collection.new("ShaderNodeValToRGB")
- cr_angle.location = (x_offset - 1100, y_offset + v_offset)
- cr_angle.color_ramp.elements[0].position = context.scene.refine_angle_ramp_pos_0 if context.scene.refine_angle_ramp_active else 0.0
- cr_angle.color_ramp.elements[0].color = (0, 0, 0, 1)
- cr_angle.color_ramp.elements[1].position = context.scene.refine_angle_ramp_pos_1 if context.scene.refine_angle_ramp_active else 0.0
- cr_angle.color_ramp.elements[1].color = (1, 1, 1, 1)
- cr_angle.color_ramp.interpolation = 'LINEAR'
- links.new(angle_node.outputs[0], cr_angle.inputs[0])
-
- # Feather Ramp
- cr_feather = nodes_collection.new("ShaderNodeValToRGB")
- cr_feather.location = (x_offset - 1100, y_offset + v_offset - 200)
- cr_feather.color_ramp.elements[0].position = context.scene.refine_feather_ramp_pos_0 if context.scene.visibility_vignette else 0.0
- cr_feather.color_ramp.elements[0].color = (0, 0, 0, 1)
- cr_feather.color_ramp.elements[1].position = context.scene.refine_feather_ramp_pos_1 if context.scene.visibility_vignette else 0.0
- cr_feather.color_ramp.elements[1].color = (1, 1, 1, 1)
- cr_feather.color_ramp.interpolation = 'LINEAR'
- links.new(feather_node.outputs[0], cr_feather.inputs[0])
-
- # Combine angle × feather
- m = _combine_angle_feather(
- nodes_collection, links,
- cr_angle.outputs[0], cr_feather.outputs[0],
- loc=(x_offset - 900, y_offset + v_offset)
- )
-
- # Optionally multiply by edge-feather mask
- if ef_node is not None:
- ef_mult = nodes_collection.new("ShaderNodeMath")
- ef_mult.operation = 'MULTIPLY'
- ef_mult.location = (x_offset - 800, y_offset + v_offset - 80)
- links.new(m.outputs[0], ef_mult.inputs[0])
- links.new(ef_node.outputs[0], ef_mult.inputs[1])
- return ef_mult.outputs[0]
-
- return m.outputs[0]
- else:
- return w_item.outputs[0]
-
- # ── Max-relative weight normalization (level 0 only) ──────────────
- # At high weight exponents, pow(cos(θ), exp) can underflow to 0 in
- # float32 for every camera, making the w_A/(w_A+w_B) ratio become
- # 0/0 → black. To fix this robustly we:
- # 1. Temporarily set each per-camera Power to 1.0 so the weight
- # outputs become the *base* values (binary_gates × |cos(θ)|)
- # that never underflow.
- # 2. Normalize those base weights by the per-pixel maximum so the
- # best camera gets 1.0.
- # 3. Re-apply the user exponent to the normalized [0,1] values
- # where underflow is impossible.
- # Mathematically: pow(cos/max_cos, exp) == pow(cos, exp)/pow(max_cos, exp),
- # so the blending ratios are identical to the original formula.
- if level == 0 and len(shaders) > 1:
- # ── Step 1: Neutralize per-camera Power (set to 1.0) ──────
- user_exponent = None
- for idx in range(len(weight_nodes)):
- w = weight_nodes[idx]
- node = w[0] if isinstance(w, tuple) else w
-
- # Skip disabled cameras (stop_index → LESS_THAN always-0)
- if node.type == 'MATH' and node.operation == 'LESS_THAN':
- continue
-
- # Native raycast path:
- # final_weight (MULTIPLY, label Angle-*) →
- # inputs[1] → power_node (POWER, label power_weight)
- if (node.type == 'MATH' and node.operation == 'MULTIPLY'
- and node.label.startswith('Angle-')):
- if node.inputs[1].links:
- pn = node.inputs[1].links[0].from_node
- if (pn.type == 'MATH' and pn.operation == 'POWER'
- and pn.label == 'power_weight'):
- if user_exponent is None:
- user_exponent = pn.inputs[1].default_value
- pn.inputs[1].default_value = 1.0
-
- # 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
-
- if user_exponent is None:
- user_exponent = 1.0
-
- # ── Step 2: Get base weight outputs (now power = 1) ───────
- all_w_outputs = []
- for idx in range(len(weight_nodes)):
- v_off = -200 * idx
- all_w_outputs.append(_get_weight_output(weight_nodes[idx], v_off))
-
- # ── Step 3: Find per-pixel maximum ────────────────────────
- max_node = nodes_collection.new("ShaderNodeMath")
- max_node.operation = 'MAXIMUM'
- max_node.location = (x_offset - 1500, y_offset - 600)
- max_node.label = "WeightMax"
- links.new(all_w_outputs[0], max_node.inputs[0])
- if len(all_w_outputs) > 1:
- links.new(all_w_outputs[1], max_node.inputs[1])
- else:
- max_node.inputs[1].default_value = 0.0
-
- for idx in range(2, len(all_w_outputs)):
- next_max = nodes_collection.new("ShaderNodeMath")
- next_max.operation = 'MAXIMUM'
- next_max.location = (x_offset - 1500 + 150 * (idx - 1),
- y_offset - 600)
- next_max.label = "WeightMax"
- links.new(max_node.outputs[0], next_max.inputs[0])
- links.new(all_w_outputs[idx], next_max.inputs[1])
- max_node = next_max
-
- # Clamp max to epsilon to prevent 0/0 division
- safe_max = nodes_collection.new("ShaderNodeMath")
- safe_max.operation = 'MAXIMUM'
- safe_max.location = (max_node.location[0] + 150, y_offset - 600)
- safe_max.inputs[1].default_value = 1e-7
- safe_max.label = "SafeMax"
- links.new(max_node.outputs[0], safe_max.inputs[0])
-
- # ── Step 4: Normalize + re-apply exponent ─────────────────
- powered_nodes = []
- for idx, w_out in enumerate(all_w_outputs):
- # norm_w = base_w / max(base_w)
- div = nodes_collection.new("ShaderNodeMath")
- div.operation = 'DIVIDE'
- div.location = (safe_max.location[0] + 200,
- y_offset - 120 * idx)
- div.label = f"NormW-{idx}"
- links.new(w_out, div.inputs[0])
- links.new(safe_max.outputs[0], div.inputs[1])
-
- # sharp_w = pow(norm_w, user_exponent)
- # (inputs are in [0, 1] → no underflow possible)
- if user_exponent != 1.0:
- pw = nodes_collection.new("ShaderNodeMath")
- pw.operation = 'POWER'
- pw.location = (div.location[0] + 200,
- div.location[1])
- pw.label = f"SharpW-{idx}"
- pw.inputs[1].default_value = user_exponent
- links.new(div.outputs[0], pw.inputs[0])
- powered_nodes.append(pw)
- else:
- powered_nodes.append(div)
-
- # Replace weight_nodes with sharpened-normalised nodes
- weight_nodes = powered_nodes
-
- new_shaders = []
- new_weight_nodes = []
- i = 0
- while i < len(shaders):
- if i + 1 < len(shaders):
- # Sum the weights
- vert_offset = -200 * (i // 2)
- sum_node = nodes_collection.new("ShaderNodeMath")
- sum_node.operation = 'ADD'
- sum_node.location = (x_offset - 800, y_offset + vert_offset) if context.scene.early_priority else (x_offset - 600, y_offset + vert_offset)
-
- def get_weight_output(w_item):
- if isinstance(w_item, tuple):
- angle_node = w_item[0]
- feather_node = w_item[1]
- ef_node = w_item[2] if len(w_item) >= 3 else None
-
- # Create Ramps (Output Visibility: 0=Invis, 1=Vis)
- # Angle Ramp
- cr_angle = nodes_collection.new("ShaderNodeValToRGB")
- cr_angle.location = (x_offset - 1100, y_offset + vert_offset)
- cr_angle.color_ramp.elements[0].position = context.scene.refine_angle_ramp_pos_0 if context.scene.refine_angle_ramp_active else 0.0
- cr_angle.color_ramp.elements[0].color = (0, 0, 0, 1) # Black (Invis)
- cr_angle.color_ramp.elements[1].position = context.scene.refine_angle_ramp_pos_1 if context.scene.refine_angle_ramp_active else 0.0
- cr_angle.color_ramp.elements[1].color = (1, 1, 1, 1) # White (Vis)
- cr_angle.color_ramp.interpolation = 'LINEAR'
- links.new(angle_node.outputs[0], cr_angle.inputs[0])
-
- # Feather Ramp
- cr_feather = nodes_collection.new("ShaderNodeValToRGB")
- cr_feather.location = (x_offset - 1100, y_offset + vert_offset - 200)
- cr_feather.color_ramp.elements[0].position = context.scene.refine_feather_ramp_pos_0 if context.scene.visibility_vignette else 0.0
- cr_feather.color_ramp.elements[0].color = (0, 0, 0, 1) # Black (Invis)
- cr_feather.color_ramp.elements[1].position = context.scene.refine_feather_ramp_pos_1 if context.scene.visibility_vignette else 0.0
- cr_feather.color_ramp.elements[1].color = (1, 1, 1, 1) # White (Vis)
- cr_feather.color_ramp.interpolation = 'LINEAR'
- links.new(feather_node.outputs[0], cr_feather.inputs[0])
-
- # Combine angle and feather visibility
- m = _combine_angle_feather(
- nodes_collection, links,
- cr_angle.outputs[0], cr_feather.outputs[0],
- loc=(x_offset - 900, y_offset + vert_offset)
- )
-
- # Optionally multiply by edge-feather mask
- if ef_node is not None:
- ef_mult = nodes_collection.new("ShaderNodeMath")
- ef_mult.operation = 'MULTIPLY'
- ef_mult.location = (x_offset - 800, y_offset + vert_offset - 80)
- links.new(m.outputs[0], ef_mult.inputs[0])
- links.new(ef_node.outputs[0], ef_mult.inputs[1])
- return ef_mult.outputs[0]
-
- return m.outputs[0]
- else:
- return w_item.outputs[0]
-
- # Cache outputs to avoid creating duplicate nodes
- w_out_1 = get_weight_output(weight_nodes[i])
- w_out_2 = get_weight_output(weight_nodes[i+1])
-
- links.new(w_out_1, sum_node.inputs[0])
- links.new(w_out_2, sum_node.inputs[1])
-
- # Compute mix factor: weight_A / (weight_A+weight_B)
- div_node = nodes_collection.new("ShaderNodeMath")
- div_node.operation = 'DIVIDE'
- div_node.location = (x_offset - 600, y_offset + vert_offset) if context.scene.early_priority else (x_offset - 400, y_offset + vert_offset)
- links.new(w_out_2, div_node.inputs[0])
- links.new(sum_node.outputs[0], div_node.inputs[1])
-
- if context.scene.early_priority:
- # Add map range node
- map_range_node = nodes_collection.new("ShaderNodeMapRange")
- map_range_node.location = (x_offset - 400, y_offset + vert_offset)
- # to_min, to_max, from_max are set to 0.0 and 1.0 by default
- # set to prioritize earlier images
- map_range_node.inputs[1].default_value = context.scene.early_priority_strength
- links.new(div_node.outputs[0], map_range_node.inputs[0])
-
- # Create a MixRGB node for color blending of the two inputs
- mix_node = nodes_collection.new("ShaderNodeMixRGB")
- mix_node.location = (x_offset - 200, y_offset + vert_offset)
- mix_node.use_clamp = True
- # Connect first color to Color1 and second color to Color2
- links.new(shaders[i].outputs[0], mix_node.inputs["Color1"])
- links.new(shaders[i+1].outputs[0], mix_node.inputs["Color2"])
- # Use the computed mix factor
- to_connect = div_node if not context.scene.early_priority else map_range_node
- links.new(to_connect.outputs[0], mix_node.inputs["Fac"])
-
- new_shaders.append(mix_node)
- # The new effective weight is the sum (stored in sum_node)
- new_weight_nodes.append(sum_node)
- i += 2
- else:
- new_shaders.append(shaders[i])
- new_weight_nodes.append(weight_nodes[i])
- i += 1
- return build_mix_tree(new_shaders, new_weight_nodes, nodes_collection, links, last_node, level+1)
-
- cameras = [obj for obj in context.scene.objects if obj.type == 'CAMERA']
- cameras.sort(key=lambda x: x.name)
-
- # Force refresh of the UI
- for area in context.screen.areas:
- area.tag_redraw()
-
- # Apply projection to all mesh objects for each camera
- for i, camera in enumerate(cameras):
- for x, obj in enumerate(to_project):
- # We can skip the UV Projection step in sequential mode for i > 0
- if context.scene.generation_method != 'sequential' or stop_index == 0:
- # Deselect all objects
- bpy.ops.object.select_all(action='DESELECT')
- # Select object as active (needed for applying the modifier)
- context.view_layer.objects.active = obj
- obj.select_set(True)
-
- # Make object data single-user before applying modifier
- bpy.ops.object.make_single_user(object=True, obdata=True)
-
- # Check if the data is now single user
- if obj.data.users > 1:
- # If not, we need to make it single user again
- print("Warning: Cannot make object data single user. Making a copy.")
- obj.data = obj.data.copy()
- obj.data.name = f"{obj.name}_data"
- obj.data.update()
- # Check again
- if obj.data.users > 1:
- print("Error: Cannot make object data single user. Exiting.")
- return Exception("Cannot make object data single user. Exiting.")
-
- # Create or reuse a buffer UV map for the UV Project modifier
- buffer_uv = obj.data.uv_layers.get(_SG_BUFFER_UV_NAME)
- if not buffer_uv:
- buffer_uv = obj.data.uv_layers.new(name=_SG_BUFFER_UV_NAME)
-
- # Add the UV Project Modifier
- uv_project_mod = obj.modifiers.new(name="UVProject", type='UV_PROJECT')
-
- # Assign the active camera to the UV Project modifier
- if not camera:
- return False
-
- uv_project_mod.projectors[0].object = camera
-
- # Set the buffer UV map for the modifier
- uv_project_mod.uv_layer = buffer_uv.name
-
- # Calculate and set the aspect ratio (per-camera if available)
- cam_res_x, cam_res_y = _get_camera_resolution(camera, context.scene)
- aspect_ratio = cam_res_x / cam_res_y
- uv_project_mod.aspect_x = aspect_ratio if aspect_ratio > 1 else 1
- uv_project_mod.aspect_y = 1 / aspect_ratio if aspect_ratio < 1 else 1
-
- # Set the buffer UV as active so the modifier writes to it
- # (Blender 5.0+ ignores uv_layer and writes to the active UV map)
- obj.data.uv_layers.active = buffer_uv
-
- # Apply the modifier
- bpy.ops.object.modifier_apply(modifier=uv_project_mod.name)
-
- # Copy buffer UV data to a named attribute (no UV slot limit)
- attr_name = f"ProjectionUV_{i}_{mat_id}"
- _copy_uv_to_attribute(obj, _SG_BUFFER_UV_NAME, attr_name)
-
- # Restore active UV to the first non-buffer UV layer
- original_uv_map = obj.data.uv_layers[0]
- if original_uv_map.name == _SG_BUFFER_UV_NAME and len(obj.data.uv_layers) > 1:
- original_uv_map = obj.data.uv_layers[1]
- obj.data.uv_layers.active = original_uv_map
-
- # Clean up buffer UV map from all objects
- for obj in to_project:
- buffer_uv = obj.data.uv_layers.get(_SG_BUFFER_UV_NAME)
- if buffer_uv:
- obj.data.uv_layers.remove(buffer_uv)
-
- # ── Optional: bake per-vertex visibility weights ──────────────────
- use_baked_weights = getattr(context.scene, 'bake_visibility_weights', False)
- 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.
- is_sequential = context.scene.generation_method == 'sequential'
- if not is_sequential or stop_index == 0:
- _bake_visibility_weights(context, to_project, cameras, mat_id)
-
- # Switch to Cycles (needed for Raycast shader node on all versions)
- context.scene.render.engine = 'CYCLES'
- # Force CPU + OSL only for Blender < 5.1 (native Raycast nodes don't need it)
- if bpy.app.version < (5, 1, 0):
- context.scene.cycles.device = 'CPU'
- if hasattr(context.scene.cycles, 'shading_system'):
- context.scene.cycles.shading_system = True
- else:
- context.scene.cycles.use_osl = True
-
- processed_materials = set()
-
- for obj in to_project:
-
- # Deselect all objects
- bpy.ops.object.select_all(action='DESELECT')
- context.view_layer.objects.active = obj
- obj.select_set(True)
-
- # Create the material
- to_switch = False
- is_local_edit = (context.scene.generation_method == 'local_edit' or (context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method == 'local_edit'))
- if is_local_edit and not context.scene.overwrite_material:
- # Copy active material
- mat = obj.active_material.copy()
- obj.data.materials.append(mat)
- to_switch = True
- elif obj.active_material and (context.scene.overwrite_material or is_local_edit \
- or (context.scene.generation_method == 'sequential' and stop_index > 0) or context.scene.generation_mode == 'regenerate_selected'):
- # Use active material
- mat = obj.active_material
- else:
- mat = bpy.data.materials.new(name="ProjectionMaterial")
- obj.data.materials.append(mat)
- # Mark as active material
- obj.active_material_index = obj.material_slots.find(mat.name)
- to_switch = True
-
- if to_switch:
- original_materials = obj.data.materials[:]
- # Clear existing materials
- obj.data.materials.clear()
- # Add the new material
- obj.data.materials.append(mat)
- # Add the rest of the original materials
- for original_mat in original_materials:
- if original_mat != mat:
- obj.data.materials.append(original_mat)
-
- # Enable use of nodes
- mat.use_nodes = True
- nodes = mat.node_tree.nodes
- links = mat.node_tree.links
-
- previous_node = None
- output = None
-
- # Get the original UV map (needed for bake path).
- # If the mesh has no UV maps at all, create one so baking / node
- # setup doesn't crash.
- if len(obj.data.uv_layers) == 0:
- obj.data.uv_layers.new(name="UVMap")
- original_uv_map = obj.data.uv_layers[0]
- if original_uv_map.name == _SG_BUFFER_UV_NAME and len(obj.data.uv_layers) > 1:
- 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:
- # Clear existing nodes
- for node in nodes:
- nodes.remove(node)
- else:
- # Find the node connected to the output node
- for node in nodes:
- if node.type == 'OUTPUT_MATERIAL':
- output = node
- break
- if output:
- # Save the node connected to the output node into a variable
- if output.inputs[0].links[0].from_node.type == 'BSDF_PRINCIPLED':
- previous_node = output.inputs[0].links[0].from_node.inputs[0].links[0].from_node
- else:
- previous_node = output.inputs[0].links[0].from_node
-
- if not is_local_edit:
- output = nodes.new("ShaderNodeOutputMaterial")
- output.location = (3000, 0)
-
- geometry = nodes.new("ShaderNodeNewGeometry")
- geometry.location = (-600, 0)
-
- use_native_raycast = bpy.app.version >= (5, 1, 0)
-
- tex_image_nodes = []
- uv_map_nodes = []
- subtract_nodes = []
- normalize_nodes = []
- script_nodes = []
- script_nodes_outputs = []
- add_camera_loc_nodes = []
- length_nodes = []
- camera_fov_nodes = []
- camera_aspect_ratio_nodes = []
- camera_direction_nodes = []
- camera_up_nodes = []
-
- def _create_edge_feather_weight(cam_index, mat_id_inner):
- """Load the edge-feathered visibility mask for camera *cam_index*
- and return a shader Math node whose output[0] gives a 0..1 float
- weight (0 = edge/invisible, 1 = interior/full), or *None* on failure.
-
- The mask is UV-projected through the same camera UV attribute
- that projects the generated image.
- """
- vis_dir = get_dir_path(context, "inpaint")["visibility"]
- ef_path = os.path.join(vis_dir, f"render{cam_index}_edgefeather.png")
- if not os.path.exists(ef_path):
- print(f"[StableGen] Edge-feather mask not found: {ef_path}")
- return None
- ef_image = get_or_load_image(ef_path, force_reload=context.scene.overwrite_material)
- if ef_image is None:
- return None
-
- # Image Texture node to sample the mask via camera UV
- mask_tex = nodes.new("ShaderNodeTexImage")
- mask_tex.image = ef_image
- mask_tex.extension = 'CLIP'
- mask_tex.label = f"EdgeFeather-{cam_index}-{mat_id_inner}"
- mask_tex.location = (-500, -200 * cam_index - 150)
-
- # Connect to the same camera UV attribute
- uv_node = uv_map_nodes[cam_index]
- uv_out = "Vector" if uv_node.type == 'ATTRIBUTE' else "UV"
- links.new(uv_node.outputs[uv_out], mask_tex.inputs["Vector"])
-
- # Color→Float conversion via a pass-through Math node
- to_float = nodes.new("ShaderNodeMath")
- to_float.operation = 'MULTIPLY'
- to_float.inputs[1].default_value = 1.0
- to_float.location = (-300, -200 * cam_index - 150)
- to_float.label = f"EF_Weight-{cam_index}"
- links.new(mask_tex.outputs[0], to_float.inputs[0])
- return to_float
-
- # 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)
- and context.scene.generation_method == 'sequential')
-
- for i, camera in enumerate(cameras):
- # Add image texture node
- tex_image = nodes.new("ShaderNodeTexImage")
- if i <= stop_index:
- image_path = get_file_path(context, "generated", camera_id=i, 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 i == 0 and context.scene.sequential_ipadapter_mode == 'first':
- image_path = get_file_path(context, "generated", camera_id=i, material_id=mat_id).replace(".png", "_ipadapter.png")
-
- image = get_or_load_image(image_path, force_reload=context.scene.overwrite_material)
- if image:
- tex_image.image = image
- elif voronoi_active:
- # Voronoi mode: assign a solid placeholder matching the Qwen
- # guidance fallback colour so non-generated cameras contribute
- # that colour instead of black.
- voronoi_color = tuple(context.scene.qwen_guidance_fallback_color)
- tex_image.image = _get_voronoi_placeholder(voronoi_color)
- tex_image.interpolation = 'Closest'
-
- tex_image.location = (0, -200 * i)
- tex_image.extension = 'CLIP'
- tex_image.label = f"{i}-{mat_id}"
- tex_image_nodes.append(tex_image)
-
- # Add UV map / attribute node
- # Use ShaderNodeAttribute to read from the corner-domain attribute
- uv_map_node = nodes.new("ShaderNodeAttribute")
- uv_map_node.attribute_name = f"ProjectionUV_{i}_{mat_id}"
- uv_map_node.attribute_type = 'GEOMETRY'
- uv_map_node.location = (-200, -200 * (i+1))
- uv_map_nodes.append(uv_map_node)
-
- # Compute the dot product of the direction vector and geometry normal
-
- if use_baked_weights:
- # ── Baked-weights path (transform-stable) ──
- # Read pre-computed per-vertex weight from mesh attribute.
- # A pass-through Math node is used so outputs[0] provides the
- # scalar float (ShaderNodeAttribute outputs[0] is Color).
- vis_attr = nodes.new("ShaderNodeAttribute")
- vis_attr.attribute_name = f"_SG_VisWeight_{i}_{mat_id}"
- vis_attr.attribute_type = 'GEOMETRY'
- vis_attr.location = (-400, (-800) * i)
- vis_attr.label = f"BakedVis-{i}-{mat_id}"
-
- passthrough = nodes.new("ShaderNodeMath")
- passthrough.operation = 'MULTIPLY'
- passthrough.inputs[1].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])
-
- scripts_to_connect = []
- final_weight_node = passthrough
-
- script_nodes.append(scripts_to_connect)
-
- if i > stop_index and not voronoi_active:
- less_than = nodes.new("ShaderNodeMath")
- less_than.operation = 'LESS_THAN'
- less_than.location = (-100, (-800) * i)
- less_than.inputs[1].default_value = -1
- links.new(passthrough.outputs[0], less_than.inputs[0])
- script_nodes_outputs.append(less_than)
- else:
- script_nodes_outputs.append(final_weight_node)
-
- 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)
-
- elif use_native_raycast:
- # ── Native Raycast path (Blender 5.1+) ──
- result = create_native_raycast_visibility(
- nodes, links, camera, geometry, context, i, mat_id, stop_index
- )
- angle_weight = result[0]
- # Destructure result for create_native_feather
- _, _, normalize_node, length_node, camera_loc_node, camera_fov_node, camera_aspect_node, camera_dir_node, camera_up_node = result
-
- scripts_to_connect = [] # No OSL scripts to connect
- final_weight_node = None
-
- 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'))
- 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
- )
- # Optionally add edge-feather mask as third multiplier
- if 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 = (angle_weight, feather_weight, ef_node)
- else:
- final_weight_node = (angle_weight, feather_weight)
- else:
- final_weight_node = (angle_weight, feather_weight)
- else:
- final_weight_node = angle_weight
-
- script_nodes.append(scripts_to_connect)
-
- if i > stop_index and not voronoi_active:
- less_than = nodes.new("ShaderNodeMath")
- less_than.operation = 'LESS_THAN'
- less_than.location = (-200, (-800) * i)
- less_than.inputs[1].default_value = -1
- source_node = final_weight_node[0] if isinstance(final_weight_node, tuple) else final_weight_node
- links.new(source_node.outputs[0], less_than.inputs[0])
- script_nodes_outputs.append(less_than)
- else:
- script_nodes_outputs.append(final_weight_node)
-
- # Append None to node lists (connections handled inside create functions)
- 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)
- else:
- # ── OSL path (Blender < 5.1) ──
- subtract = nodes.new("ShaderNodeVectorMath")
- subtract.operation = 'SUBTRACT'
- subtract.location = (-400, -300 + (-800) * i)
- subtract.inputs[1].default_value = camera.location
- subtract_nodes.append(subtract)
-
- normalize = nodes.new("ShaderNodeVectorMath")
- normalize.operation = 'NORMALIZE'
- normalize.location = (-400, -500 + (-800) * (i))
- normalize_nodes.append(normalize)
-
- # Add script nodes (Angle)
- script_angle = nodes.new("ShaderNodeScript")
- script_angle.location = (-400, (-800) * i)
-
- # Load OSL script into internal text block for portability
- raycast_osl_text = get_or_create_osl_text("raycast.osl")
- if raycast_osl_text:
- script_angle.mode = 'INTERNAL'
- script_angle.script = raycast_osl_text
- else:
- script_angle.mode = 'EXTERNAL'
- script_angle.filepath = os.path.join(os.path.dirname(__file__), "raycast.osl")
-
- script_angle.inputs["AngleThreshold"].default_value = context.scene.discard_factor
- script_angle.inputs["Power"].default_value = context.scene.weight_exponent
- 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')):
- # Add Feather script
- script_feather = nodes.new("ShaderNodeScript")
- script_feather.location = (-400, (-800) * i - 200) # Offset slightly
-
- # Load OSL script into internal text block for portability
- feather_osl_text = get_or_create_osl_text("feather.osl")
- if feather_osl_text:
- script_feather.mode = 'INTERNAL'
- script_feather.script = feather_osl_text
- else:
- script_feather.mode = 'EXTERNAL'
- script_feather.filepath = os.path.join(os.path.dirname(__file__), "feather.osl")
-
- if context.scene.visibility_vignette: # Only set if feathering is active
- script_feather.inputs["EdgeFeather"].default_value = context.scene.visibility_vignette_width
- script_feather.inputs["EdgeGamma"].default_value = context.scene.visibility_vignette_softness
- else:
- script_feather.inputs["EdgeFeather"].default_value = 0.0
- script_feather.inputs["EdgeGamma"].default_value = 1.0
- script_feather.label = f"Feather-{i}-{mat_id}"
- 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:
- 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:
- final_weight_node = (script_angle, script_feather)
- else:
- # Just use Angle script output directly
- final_weight_node = script_angle
-
- script_nodes.append(scripts_to_connect)
-
- if i > stop_index and not voronoi_active:
- # Connect a temporary less than node to the script node
- less_than = nodes.new("ShaderNodeMath")
- less_than.operation = 'LESS_THAN'
- less_than.location = (-200, (-800) * i)
- less_than.inputs[1].default_value = -1
-
- # For stop_index check, we use the Angle script output
- # (or the first element if it's a tuple)
- source_node = final_weight_node[0] if isinstance(final_weight_node, tuple) else final_weight_node
- links.new(source_node.outputs[0], less_than.inputs[0])
-
- script_nodes_outputs.append(less_than)
- else:
- script_nodes_outputs.append(final_weight_node)
-
- # Add additional add node, which will contain camera's FOV in first default value, and camera's aspect ratio in second default value
- camera_fov = nodes.new("ShaderNodeValue")
- camera_fov.location = (-600, 200 + 300 * i)
- cam_res_x, cam_res_y = _get_camera_resolution(camera, context.scene)
- fov = camera.data.angle_x
- # Correct the FOV for vertical aspect ratio
- if cam_res_y > cam_res_x:
- fov = 2 * atan(tan(fov / 2) * cam_res_x / cam_res_y)
- camera_fov.outputs[0].default_value = fov
- camera_fov_nodes.append(camera_fov)
- camera_aspect_ratio = nodes.new("ShaderNodeValue")
- camera_aspect_ratio.location = (-600, 200 + 300 * i)
-
- # Add camera direction and up nodes (combine XYZ)
- camera_direction = nodes.new("ShaderNodeCombineXYZ")
- camera_direction.location = (-600, 200 + 300 * i)
- # Get the camera direction vector
- camera_direction.inputs[0].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 0, -1))).x
- camera_direction.inputs[1].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 0, -1))).y
- camera_direction.inputs[2].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 0, -1))).z
- camera_direction_nodes.append(camera_direction)
- camera_up = nodes.new("ShaderNodeCombineXYZ")
- camera_up.location = (-600, 200 + 300 * i)
- # Get the camera up vector
- camera_up.inputs[0].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 1, 0))).x
- camera_up.inputs[1].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 1, 0))).y
- camera_up.inputs[2].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 1, 0))).z
- camera_up_nodes.append(camera_up)
-
- camera_aspect_ratio.outputs[0].default_value = cam_res_x / cam_res_y
- camera_aspect_ratio_nodes.append(camera_aspect_ratio)
-
- # Add combine XYZ node
- add_camera_loc = nodes.new("ShaderNodeCombineXYZ")
- add_camera_loc.location = (-600, 200 + 300 * i)
- add_camera_loc.inputs[0].default_value = camera.location.x
- add_camera_loc.inputs[1].default_value = camera.location.y
- add_camera_loc.inputs[2].default_value = camera.location.z
- add_camera_loc_nodes.append(add_camera_loc)
-
- # Add length node
- length = nodes.new("ShaderNodeVectorMath")
- length.operation = 'LENGTH'
- length.location = (-400, 200 * (i+1))
- length_nodes.append(length)
-
- # 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
- # But we still need to connect the nodes for the current object (done in the loop below)
- # We need to find the existing mix node to position the output node
- # The mix node is connected to the output node
- mix_node = output.inputs["Surface"].links[0].from_node
- else:
- mix_node, _ = build_mix_tree(tex_image_nodes, script_nodes_outputs, nodes, links, previous_node, stop_index=stop_index)
- links.new(mix_node.outputs[0], output.inputs["Surface"])
- processed_materials.add(mat.name)
-
- # Move output node right to the mix_node
- output.location = (mix_node.location[0] + 400, mix_node.location[1])
-
- for i, camera in enumerate(cameras):
- tex_image = tex_image_nodes[i]
- uv_map_node = uv_map_nodes[i]
-
- # 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"])
-
- if use_baked_weights or use_native_raycast:
- # Baked-weights path: all links already made during creation.
- # Native raycast path: all links already made inside
- # create_native_raycast_visibility / create_native_feather.
- pass
- else:
- # OSL path: connect subtract, normalize, script inputs, length
- subtract = subtract_nodes[i]
- normalize = normalize_nodes[i]
- scripts = script_nodes[i]
- add_camera_loc = add_camera_loc_nodes[i]
- length = length_nodes[i]
- camera_fov = camera_fov_nodes[i]
- camera_aspect_ratio = camera_aspect_ratio_nodes[i]
- camera_direction = camera_direction_nodes[i]
- camera_up = camera_up_nodes[i]
-
- links.new(geometry.outputs["Position"], subtract.inputs[0])
- links.new(subtract.outputs["Vector"], normalize.inputs[0])
-
- for script in scripts:
- links.new(normalize.outputs["Vector"], script.inputs["Direction"])
-
- if "Origin" in script.inputs:
- links.new(add_camera_loc.outputs["Vector"], script.inputs["Origin"])
-
- if "threshold" in script.inputs:
- links.new(length.outputs["Value"], script.inputs["threshold"])
-
- if "SurfaceNormal" in script.inputs:
- links.new(geometry.outputs["Normal"], script.inputs["SurfaceNormal"])
- links.new(camera_fov.outputs[0], script.inputs["CameraFOV"])
- links.new(camera_aspect_ratio.outputs[0], script.inputs["CameraAspect"])
- links.new(camera_direction.outputs[0], script.inputs["CameraDir"])
- links.new(camera_up.outputs[0], script.inputs["CameraUp"])
-
- links.new(subtract.outputs["Vector"], length.inputs[0])
-
- # Add material index node (subtract node)
- subtract_node = nodes.new("ShaderNodeMath")
- subtract_node.operation = 'SUBTRACT'
- subtract_node.inputs[0].default_value = mat_id
- subtract_node.location = (-1000, 0)
- return True
-
-
-def get_or_create_osl_text(filename):
- """
- Loads an OSL script into a Blender internal text block.
- This ensures the script is embedded in the .blend file (portable).
- """
- filepath = os.path.join(os.path.dirname(__file__), filename)
- if not os.path.exists(filepath):
- print(f"Error: OSL script not found at {filepath}")
- return None
-
- # Check if text block exists
- text = bpy.data.texts.get(filename)
-
- try:
- with open(filepath, 'r') as f:
- content = f.read()
-
- if text:
- # Update content to match the current file on disk
- text.clear()
- text.write(content)
- else:
- text = bpy.data.texts.new(name=filename)
- text.write(content)
- except Exception as e:
- print(f"Error reading OSL file {filepath}: {e}")
- return None
-
- return text
-
-
-def get_or_load_image(filepath, force_reload=False):
- """
- Prevents duplicate image datablocks by default.
- If force_reload is True, it finds the existing datablock
- and reloads it from the specified filepath.
- """
- if not filepath:
- print("Error: No filepath provided to get_or_load_image.")
- return None
-
- filename = os.path.basename(filepath)
- image = bpy.data.images.get(filename)
-
- # Verify if the found image actually points to the requested file
- if image:
- # Normalize paths for comparison (handle // prefix and OS separators)
- try:
- # bpy.path.abspath resolves // relative paths to absolute paths
- img_path = os.path.normpath(bpy.path.abspath(image.filepath))
- req_path = os.path.normpath(bpy.path.abspath(filepath))
-
- if img_path != req_path:
- # Name collision: found an image with the same name but different path.
- # This is NOT the image we want.
- image = None
-
- # Try to find if the correct image is already loaded under a different name
- for img in bpy.data.images:
- if img.filepath:
- try:
- if os.path.normpath(bpy.path.abspath(img.filepath)) == req_path:
- image = img
- break
- except:
- continue
- except Exception as e:
- print(f"Warning: Error comparing image paths: {e}")
- image = None
-
- if image and force_reload:
- # Image exists, but we are forced to reload (overwrite).
- try:
- # IMPORTANT: Update the filepath property of the existing
- # datablock to the new file path.
- image.filepath = filepath
-
- # Reload the image data from that path.
- image.reload()
- except RuntimeError as e:
- # Reload can fail if the file isn't found, etc.
- print(f"Reload failed for {filename}. Removing old datablock. Error: {e}")
- # Remove the bad datablock so we can try loading it fresh.
- bpy.data.images.remove(image)
- image = None # Set to None to trigger the load block below
-
- if image is None:
- # Image does not exist in .data, or it failed to reload.
- try:
- image = bpy.data.images.load(filepath)
- # Only set the name if it's a new datablock to avoid renaming existing ones
- # Blender will handle naming collisions (e.g. .001) automatically
- if not bpy.data.images.get(filename):
- image.name = filename
- except RuntimeError as e:
- # Load can fail if the file isn't found.
- print(f"Warning: Could not load image file: {filepath}. Error: {e}")
- return None
-
- return image
-
-
-def reinstate_compare_nodes(context, to_project, stop_id_mat_id_pairs):
- """
- Reinstates the 'LESS_THAN' compare nodes that were removed for sequential generation.
- This will esentially revert given views to not-generated state for viewpoint regeneration.
- """
-
- for obj in to_project:
- if not obj.active_material:
- continue
-
- mat = obj.active_material
- if not mat.use_nodes:
- continue
-
- nodes = mat.node_tree.nodes
- links = mat.node_tree.links
-
- for stop_id, mat_id in stop_id_mat_id_pairs:
- script_node = None
- # Find the script node with the specific label
- # 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"{stop_id}-{mat_id}":
- script_node = node
- break
- # Also check Angle-prefixed labels
- if (node.type == 'SCRIPT' or (node.type == 'MATH' and node.operation == 'MULTIPLY')) and node.label == f"Angle-{stop_id}-{mat_id}":
- script_node = node
- break
-
- if not script_node:
- continue
-
- # Store links to disconnect and reconnect later
- links_to_recreate = []
- for link in list(script_node.outputs[0].links):
- links_to_recreate.append((link.from_socket, link.to_socket))
- links.remove(link)
-
- # For each original connection, insert a 'LESS_THAN' node
- for from_socket, to_socket in links_to_recreate:
- # Create a new 'LESS_THAN' math node
- less_than_node = nodes.new(type='ShaderNodeMath')
- less_than_node.operation = 'LESS_THAN'
- less_than_node.inputs[1].default_value = -1
- # Position it between the script node and its original destination
- less_than_node.location = (script_node.location.x + 200, script_node.location.y)
-
- # Connect script_node -> less_than_node -> original destination
- links.new(from_socket, less_than_node.inputs[0])
- links.new(less_than_node.outputs[0], to_socket)
-
-
-# ── PBR Projection ────────────────────────────────────────────────────
-
-
-def _clone_color_tree_for_pbr(nodes, links, source_node, cam_tex_to_pbr,
- channel, y_offset):
- """Recursively clone the colour mix-tree, sharing *Fac* connections and
- swapping Image Texture leaves for their PBR counterparts.
-
- Args:
- nodes / links: ``mat.node_tree.nodes / links``.
- source_node: current node being cloned (walk from BSDF backwards).
- cam_tex_to_pbr: ``{color_tex_node: pbr_tex_node}``.
- channel: PBR channel name (for default values).
- y_offset: vertical pixel shift for cloned nodes.
-
- Returns:
- Output socket of the cloned sub-tree, or *None*.
- """
- # ── Leaf: Image Texture → swap for PBR variant ────────────────────
- if source_node.type == 'TEX_IMAGE':
- pbr_node = cam_tex_to_pbr.get(source_node)
- return pbr_node.outputs[0] if pbr_node else None
-
- # ── Resolve input names: ShaderNodeMixRGB vs ShaderNodeMix ────────
- # Blender 4.0+ may use "A"/"B"/"Factor" instead of legacy names.
- fac_name, c1_name, c2_name = "Fac", "Color1", "Color2"
- if "Factor" in source_node.inputs and "A" in source_node.inputs:
- fac_name, c1_name, c2_name = "Factor", "A", "B"
-
- # ── MixRGB / Mix node → clone with same Fac ──────────────────────
- is_mix = (source_node.type in ('MIX_RGB', 'MIX')
- or (hasattr(source_node, 'bl_idname')
- and source_node.bl_idname in (
- 'ShaderNodeMixRGB', 'ShaderNodeMix'))
- or (c1_name in source_node.inputs
- and c2_name in source_node.inputs
- and fac_name in source_node.inputs))
- if is_mix:
- c1_link = source_node.inputs[c1_name].links
- c2_link = source_node.inputs[c2_name].links
- c1_src = c1_link[0].from_node if c1_link else None
- c2_src = c2_link[0].from_node if c2_link else None
-
- c1_pbr = (_clone_color_tree_for_pbr(
- nodes, links, c1_src, cam_tex_to_pbr, channel, y_offset)
- if c1_src else None)
- c2_pbr = (_clone_color_tree_for_pbr(
- nodes, links, c2_src, cam_tex_to_pbr, channel, y_offset)
- if c2_src else None)
-
- if c1_pbr is None and c2_pbr is None:
- return None
-
- new_mix = nodes.new("ShaderNodeMixRGB")
- new_mix.location = (source_node.location[0],
- source_node.location[1] + y_offset)
- new_mix.label = f"PBR_{channel}"
-
- # Share the Fac (visibility weight) connection
- fac_links = source_node.inputs[fac_name].links
- if fac_links:
- links.new(fac_links[0].from_socket, new_mix.inputs["Fac"])
- else:
- new_mix.inputs["Fac"].default_value = (
- source_node.inputs[fac_name].default_value)
-
- # Connect children
- if c1_pbr:
- links.new(c1_pbr, new_mix.inputs["Color1"])
- else:
- # Sensible defaults for uncovered areas
- defaults = {
- 'roughness': (0.5, 0.5, 0.5, 1.0),
- 'metallic': (0.0, 0.0, 0.0, 1.0),
- 'albedo': (0.5, 0.5, 0.5, 1.0),
- 'residual': (0.0, 0.0, 0.0, 1.0),
- 'shading': (0.5, 0.5, 0.5, 1.0),
- }
- new_mix.inputs["Color1"].default_value = defaults.get(
- channel, (0.0, 0.0, 0.0, 1.0))
-
- if c2_pbr:
- links.new(c2_pbr, new_mix.inputs["Color2"])
- else:
- # For Color2 (the "fallback" side of the mix) we inherit the
- # original node's default so that areas not covered by any
- # camera projection keep the scene's fallback colour. Only
- # data channels (roughness, metallic, …) use a hardcoded
- # neutral, because the original tree's fallback colour would
- # be nonsensical for those channels.
- defaults = {
- 'roughness': (0.5, 0.5, 0.5, 1.0),
- 'metallic': (0.0, 0.0, 0.0, 1.0),
- 'residual': (0.0, 0.0, 0.0, 1.0),
- 'shading': (0.5, 0.5, 0.5, 1.0),
- }
- new_mix.inputs["Color2"].default_value = defaults.get(
- channel, source_node.inputs[c2_name].default_value)
-
- return new_mix.outputs[0]
-
- # ── Unknown node → follow first connected input ───────────────────
- for inp in source_node.inputs:
- if inp.links:
- result = _clone_color_tree_for_pbr(
- nodes, links, inp.links[0].from_node,
- cam_tex_to_pbr, channel, y_offset)
- if result:
- return result
- return None
-
-
-def project_pbr_to_bsdf(context, to_texture, pbr_maps, material_id=None):
- """Project PBR decomposition maps through the same camera UV projections
- as the colour texture and wire them to the Principled BSDF.
-
- This function is called **after** ``project_image()`` has finished
- building the full colour projection material (including UV Project
- attributes and the visibility-weighted mix tree).
-
- For each PBR channel it:
-
- 1. Creates Image Texture nodes that re-use the per-camera UV attributes
- already baked by ``project_image()``.
- 2. Clones the colour MixRGB binary tree, sharing the **Fac** (weight)
- connections so visibility blending is identical.
- 3. Wires the cloned tree's output to the corresponding Principled BSDF
- input (Roughness, Metallic, Base Color, etc.).
-
- Args:
- context: Blender context.
- to_texture: List of mesh objects that were textured.
- pbr_maps: ``{camera_idx: {map_name: file_path}, …}``.
- material_id: Material slot index used during generation.
- """
- scene = context.scene
-
- if not pbr_maps:
- return
-
- # Build channel → BSDF input map from enabled per-map toggles
- channel_wiring = {} # map_name → BSDF input name
- special_wiring = {} # map_name → special handler ('normal' | 'displacement')
-
- if getattr(scene, 'pbr_replace_color_with_albedo', True) and getattr(scene, 'pbr_map_albedo', True):
- channel_wiring['albedo'] = 'Base Color'
- if getattr(scene, 'pbr_map_roughness', True):
- channel_wiring['roughness'] = 'Roughness'
- if getattr(scene, 'pbr_map_metallic', True):
- channel_wiring['metallic'] = 'Metallic'
- if getattr(scene, 'pbr_map_normal', True):
- special_wiring['normal'] = 'normal'
- if getattr(scene, 'pbr_map_height', False):
- special_wiring['height'] = 'displacement'
- if getattr(scene, 'pbr_map_emission', False):
- special_wiring['emission'] = 'emission'
- processed_materials = set()
-
- for obj in to_texture:
- if not hasattr(obj, 'active_material') or not obj.active_material:
- continue
- mat = obj.active_material
- if mat.name in processed_materials:
- continue
- if not mat.use_nodes:
- continue
-
- nodes = mat.node_tree.nodes
- links = mat.node_tree.links
-
- # ── Find or create the Principled BSDF ────────────────────────────
- principled = None
- for node in nodes:
- if node.type == 'BSDF_PRINCIPLED':
- principled = node
- break
-
- if principled is None:
- # No BSDF exists yet. Create one and rewire the existing
- # colour output through it so that PBR channels have
- # somewhere to connect.
- output_node = None
- for node in nodes:
- if node.type == 'OUTPUT_MATERIAL':
- output_node = node
- break
- if output_node is None:
- continue
-
- principled = nodes.new("ShaderNodeBsdfPrincipled")
- principled.inputs["Roughness"].default_value = 1.0
-
- # Rewire: existing surface source → BSDF Base Color → Output
- surface_links = output_node.inputs["Surface"].links
- if surface_links:
- old_source = surface_links[0].from_socket
- # Position BSDF between old source and output
- principled.location = (
- output_node.location[0] - 300,
- output_node.location[1])
- links.new(old_source, principled.inputs["Base Color"])
- else:
- principled.location = (
- output_node.location[0] - 300,
- output_node.location[1])
-
- links.new(principled.outputs[0],
- output_node.inputs["Surface"])
-
- # ── Discover existing camera Image Texture nodes ──────────────
- # Labels follow the pattern "{cam_idx}-{mat_id}" set by
- # project_image().
- camera_tex_nodes = {} # cam_idx → tex_node
- for node in nodes:
- if node.type == 'TEX_IMAGE' and node.label and '-' in node.label:
- parts = node.label.split('-')
- try:
- cam_idx = int(parts[0])
- camera_tex_nodes[cam_idx] = node
- except ValueError:
- continue
-
- if not camera_tex_nodes:
- continue
-
- # ── Find the root of the colour mix tree ─────────────────────
- color_root = None
- if principled.inputs["Base Color"].links:
- color_root = principled.inputs["Base Color"].links[0].from_node
-
- # ── For each PBR channel, create textures and clone the tree ──
- channel_idx = 0
- for channel, bsdf_input in channel_wiring.items():
- if bsdf_input not in principled.inputs:
- continue
-
- # Build mapping: colour_tex_node → PBR_tex_node
- cam_tex_to_pbr = {}
- for cam_idx, color_tex in camera_tex_nodes.items():
- if cam_idx not in pbr_maps:
- continue
- cam_pbr = pbr_maps[cam_idx]
- if channel not in cam_pbr:
- continue
-
- pbr_path = cam_pbr[channel]
- if not os.path.exists(pbr_path):
- continue
-
- pbr_img = get_or_load_image(pbr_path, force_reload=True)
- if not pbr_img:
- continue
-
- # Colorspace: roughness/metallic are always linear data.
- # Albedo from IID-Lighting is also linear; IID-Appearance
- # albedo is sRGB (Blender's default for PNG), so leave it.
- if channel in ('roughness', 'metallic'):
- pbr_img.colorspace_settings.name = 'Non-Color'
- elif (channel == 'albedo'
- and getattr(scene, 'pbr_albedo_source', 'marigold') == 'lighting'):
- pbr_img.colorspace_settings.name = 'Non-Color'
-
- pbr_tex = nodes.new("ShaderNodeTexImage")
- pbr_tex.image = pbr_img
- pbr_tex.extension = 'CLIP'
- pbr_tex.label = (
- f"PBR_{channel}_{cam_idx}_{material_id}")
- pbr_tex.location = (
- color_tex.location[0],
- color_tex.location[1]
- - 200 * (1 + channel_idx) * len(camera_tex_nodes))
-
- # Re-use the same camera UV attribute
- if color_tex.inputs["Vector"].links:
- uv_socket = (
- color_tex.inputs["Vector"].links[0].from_socket)
- links.new(uv_socket, pbr_tex.inputs["Vector"])
-
- cam_tex_to_pbr[color_tex] = pbr_tex
-
- if not cam_tex_to_pbr:
- continue
-
- # Clone the colour mix tree (or fall back to direct connection)
- pbr_final_output = None
- if color_root:
- y_off = -500 * (1 + channel_idx)
- pbr_output = _clone_color_tree_for_pbr(
- nodes, links, color_root, cam_tex_to_pbr,
- channel, y_off)
- if pbr_output:
- pbr_final_output = pbr_output
- else:
- # No colour tree exists; connect first PBR texture directly
- first_pbr = next(iter(cam_tex_to_pbr.values()))
- pbr_final_output = first_pbr.outputs["Color"]
-
- if pbr_final_output:
- links.new(pbr_final_output,
- principled.inputs[bsdf_input])
-
- channel_idx += 1
-
- # ── Special wiring: Normal Map + Displacement ─────────────────
- for sp_channel, sp_type in special_wiring.items():
- # Build PBR texture nodes for this special channel
- sp_tex_map = {} # color_tex_node → pbr_tex_node
- for cam_idx, color_tex in camera_tex_nodes.items():
- if cam_idx not in pbr_maps:
- continue
- cam_pbr = pbr_maps[cam_idx]
- if sp_channel not in cam_pbr:
- continue
- sp_path = cam_pbr[sp_channel]
- if not os.path.exists(sp_path):
- continue
- sp_img = get_or_load_image(sp_path, force_reload=True)
- if not sp_img:
- continue
- sp_img.colorspace_settings.name = 'Non-Color'
-
- sp_tex = nodes.new("ShaderNodeTexImage")
- sp_tex.image = sp_img
- sp_tex.extension = 'CLIP'
- sp_tex.label = f"PBR_{sp_channel}_{cam_idx}_{material_id}"
- sp_tex.location = (
- color_tex.location[0],
- color_tex.location[1]
- - 200 * (2 + channel_idx) * len(camera_tex_nodes))
- if color_tex.inputs["Vector"].links:
- uv_socket = color_tex.inputs["Vector"].links[0].from_socket
- links.new(uv_socket, sp_tex.inputs["Vector"])
- sp_tex_map[color_tex] = sp_tex
-
- if not sp_tex_map:
- continue
-
- # Clone the colour tree for this special channel
- sp_output = None
- if color_root:
- y_off = -500 * (2 + channel_idx)
- sp_output = _clone_color_tree_for_pbr(
- nodes, links, color_root, sp_tex_map,
- sp_channel, y_off)
- else:
- first_sp = next(iter(sp_tex_map.values()))
- sp_output = first_sp.outputs["Color"]
-
- if sp_output is None:
- continue
-
- if sp_type == 'normal':
- normal_strength = getattr(scene, 'pbr_normal_strength', 0.5)
-
- # World-space Normal Map — camera-space normals were
- # already converted in _convert_normals_cam_to_world().
- # Using WORLD space avoids tangent-frame dependency,
- # so it works on voxel-remeshed geometry.
- normal_map_node = nodes.new("ShaderNodeNormalMap")
- normal_map_node.space = 'WORLD'
- normal_map_node.location = (
- principled.location[0] - 300,
- principled.location[1] - 400)
- normal_map_node.label = "PBR Normal Map (World)"
- normal_map_node.inputs["Strength"].default_value = (
- normal_strength)
- links.new(sp_output, normal_map_node.inputs["Color"])
- if "Normal" in principled.inputs:
- links.new(normal_map_node.outputs["Normal"],
- principled.inputs["Normal"])
-
- elif sp_type == 'displacement':
- # ── Displacement / Height Map ─────────────────────
- height_scale = getattr(scene, 'pbr_height_scale', 0.1)
- output_node = None
- for node in nodes:
- if node.type == 'OUTPUT_MATERIAL':
- output_node = node
- break
- if output_node and "Displacement" in output_node.inputs:
- disp_node = nodes.new("ShaderNodeDisplacement")
- disp_node.location = (
- output_node.location[0] - 300,
- output_node.location[1] - 200)
- disp_node.label = "PBR Displacement"
- disp_node.inputs["Scale"].default_value = height_scale
- disp_node.inputs["Midlevel"].default_value = 0.5
- links.new(sp_output, disp_node.inputs["Height"])
- links.new(disp_node.outputs["Displacement"],
- output_node.inputs["Displacement"])
-
- elif sp_type == 'emission':
- # ── Emission Map ──────────────────────────────────
- emission_strength = getattr(
- scene, 'pbr_emission_strength', 5.0)
- if "Emission Color" in principled.inputs:
- links.new(sp_output,
- principled.inputs["Emission Color"])
- principled.inputs[
- "Emission Strength"].default_value = emission_strength
- elif "Emission" in principled.inputs:
- # Older Blender versions use "Emission" not "Emission Color"
- links.new(sp_output,
- principled.inputs["Emission"])
- if "Emission Strength" in principled.inputs:
- principled.inputs[
- "Emission Strength"].default_value = emission_strength
-
- channel_idx += 1
-
- # ── Set BSDF default values ───────────────────────────────────
- if 'roughness' in channel_wiring:
- principled.inputs["Roughness"].default_value = 1.0
- if 'metallic' in channel_wiring:
- principled.inputs["Metallic"].default_value = 0.0
-
- processed_materials.add(mat.name)
-
- # ── AO Bake (geometry-based, per-object) ──────────────────────────
- if getattr(scene, 'pbr_map_ao', False):
- _bake_ao_for_objects(context, to_texture, scene)
-
-
-def _bake_ao_for_objects(context, to_texture, scene):
- """Add shader-based Ambient Occlusion to materials.
-
- Inserts a Blender ``Ambient Occlusion`` shader node into each
- material and multiplies it with the Base Color input of the
- Principled BSDF. This is evaluated at render time — zero bake
- time and works with any poly count.
- """
- ao_samples = getattr(scene, 'pbr_ao_samples', 16)
- ao_distance = getattr(scene, 'pbr_ao_distance', 0.0)
-
- print(f"[StableGen] Adding shader-based AO "
- f"({ao_samples} samples, distance={ao_distance})…")
-
- processed = set()
- for oi, obj in enumerate(to_texture):
- if not hasattr(obj, 'active_material') or not obj.active_material:
- continue
- mat = obj.active_material
- if mat.name in processed:
- continue
- if not mat.use_nodes:
- continue
-
- nodes = mat.node_tree.nodes
- links = mat.node_tree.links
-
- # Find Principled BSDF
- principled = None
- for node in nodes:
- if node.type == 'BSDF_PRINCIPLED':
- principled = node
- break
- if principled is None:
- continue
-
- print(f" Adding AO node to {mat.name} "
- f"({oi+1}/{len(to_texture)})…")
-
- # Create Ambient Occlusion shader node
- ao_node = nodes.new("ShaderNodeAmbientOcclusion")
- ao_node.samples = ao_samples
- ao_node.label = "PBR AO"
- ao_node.location = (
- principled.location[0] - 500,
- principled.location[1] + 350)
- if ao_distance > 0:
- ao_node.inputs["Distance"].default_value = ao_distance
-
- # Wire AO into the material:
- # Intercept Base Color → Principled BSDF with a Multiply mix.
- base_input = principled.inputs.get("Base Color")
- if base_input and base_input.links:
- old_source = base_input.links[0].from_socket
-
- mix_node = nodes.new("ShaderNodeMix")
- mix_node.data_type = 'RGBA'
- mix_node.blend_type = 'MULTIPLY'
- mix_node.inputs["Factor"].default_value = 1.0
- mix_node.location = (
- principled.location[0] - 200,
- principled.location[1] + 200)
- mix_node.label = "AO Multiply"
-
- links.new(old_source, mix_node.inputs[6]) # A (base colour)
- links.new(ao_node.outputs["AO"], mix_node.inputs[7]) # B (AO)
- links.new(mix_node.outputs[2], base_input) # Result → Base Color
- else:
- # No existing link — just plug AO colour directly
- links.new(ao_node.outputs["Color"], base_input)
-
- processed.add(mat.name)
-
- print(f"[StableGen] AO shader nodes added "
- f"({len(processed)} material(s))")
-
+import bpy
+import os
+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
+
+
+_SG_BUFFER_UV_NAME = "_SG_ProjectionBuffer"
+
+
+def _copy_uv_to_attribute(obj, uv_layer_name, attr_name):
+ """Copy UV data from a UV layer to a named FLOAT_VECTOR corner-domain attribute.
+ Uses FLOAT_VECTOR (3-component) instead of FLOAT2 so the attribute does NOT
+ appear in the UV Maps list (FLOAT2 + CORNER = UV map in Blender 3.5+).
+ """
+ uv_layer = obj.data.uv_layers.get(uv_layer_name)
+ if not uv_layer:
+ return
+ n = len(obj.data.loops)
+ uv_buf = [0.0] * (n * 2)
+ uv_layer.data.foreach_get("uv", uv_buf)
+ # Convert (u,v) pairs to (u,v,0) triples for FLOAT_VECTOR storage
+ vec_buf = [0.0] * (n * 3)
+ for i in range(n):
+ vec_buf[i * 3] = uv_buf[i * 2]
+ vec_buf[i * 3 + 1] = uv_buf[i * 2 + 1]
+ # vec_buf[i * 3 + 2] already 0.0
+ # Remove existing attribute if present
+ existing = obj.data.attributes.get(attr_name)
+ if existing:
+ obj.data.attributes.remove(existing)
+ attr = obj.data.attributes.new(name=attr_name, type='FLOAT_VECTOR', domain='CORNER')
+ attr.data.foreach_set("vector", vec_buf)
+ obj.data.update()
+
+
+def _bake_visibility_weights(context, objects, cameras, mat_id, stop_index=1000000):
+ """Pre-compute per-vertex, per-camera visibility weights and store them
+ as ``FLOAT`` ``POINT``-domain mesh attributes.
+
+ Attribute naming: ``_SG_VisWeight_{cam_idx}_{mat_id}``
+
+ The computation replicates the same logic as the runtime
+ ``create_native_raycast_visibility`` + ``create_native_feather`` shader
+ nodes but runs once in Python so the resulting material has no world-space
+ dependencies — projected textures survive object transforms.
+
+ Uses NumPy for bulk frustum/angle/feather computation and only falls back
+ to per-vertex BVH raycasting for the subset that passes initial checks.
+ """
+ import bmesh
+ import numpy as np
+ from mathutils.bvhtree import BVHTree
+ from math import pow as fpow
+
+ scene = context.scene
+ angle_threshold = scene.discard_factor # degrees
+ power_exponent = scene.weight_exponent
+ feather_active = scene.visibility_vignette
+ feather_width = scene.visibility_vignette_width if feather_active else 0.0
+ feather_gamma = scene.visibility_vignette_softness if feather_active else 1.0
+ feather_width = max(0.0, min(feather_width, 0.49))
+
+ # Pre-compute camera data once -----------------------------------------------
+ cam_data = []
+ for camera in cameras:
+ cam_loc = camera.location.copy()
+ quat = camera.matrix_world.to_quaternion()
+ cam_dir = quat @ Vector((0, 0, -1))
+ cam_up = quat @ Vector((0, 1, 0))
+ cam_right = cam_dir.cross(cam_up).normalized()
+ cam_up_vec = cam_right.cross(cam_dir).normalized()
+
+ cam_res_x, cam_res_y = _get_camera_resolution(camera, scene)
+ fov = camera.data.angle_x
+ if cam_res_y > cam_res_x:
+ fov = 2 * atan(tan(fov / 2) * cam_res_x / cam_res_y)
+ filmw = tan(fov / 2)
+ aspect = cam_res_x / cam_res_y
+ filmh = filmw / 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,
+ cam_loc, # keep mathutils Vector for BVH calls
+ ))
+
+ # Build scene-wide BVH for occlusion (matches ShaderNodeRaycast behaviour,
+ # which checks ALL visible scene geometry, not just the current object).
+ 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():
+ continue
+ tmp = bmesh.new()
+ tmp.from_mesh(scene_obj.data)
+ tmp.transform(scene_obj.matrix_world)
+ for v in tmp.verts:
+ scene_bm.verts.new(v.co)
+ scene_bm.verts.ensure_lookup_table()
+ for f in tmp.faces:
+ try:
+ scene_bm.faces.new(
+ [scene_bm.verts[v.index + vert_offset] for v in f.verts])
+ except ValueError:
+ pass # skip degenerate / duplicate faces
+ vert_offset += len(tmp.verts)
+ tmp.free()
+ scene_bm.normal_update()
+ scene_tree = BVHTree.FromBMesh(scene_bm)
+ scene_bm.free()
+ print(f"[StableGen] Scene BVH built ({vert_offset} verts across visible meshes)")
+
+ # Per-object bake -------------------------------------------------------------
+ n_cameras = len(cameras)
+ for obj_idx, obj in enumerate(objects):
+ bm = bmesh.new()
+ bm.from_mesh(obj.data)
+ bm.transform(obj.matrix_world)
+ bm.normal_update()
+
+ n_verts = len(bm.verts)
+ # Build numpy arrays (Nx3) for positions and normals
+ positions_np = np.array([v.co[:] for v in bm.verts], dtype=np.float64)
+ normals_np = np.array([v.normal[:] for v in bm.verts], dtype=np.float64)
+ bm.free()
+
+ for cam_idx, camera in enumerate(cameras):
+ print(f"[StableGen] Baking visibility: object {obj_idx+1}/{len(objects)}, "
+ f"camera {cam_idx+1}/{n_cameras} ({n_verts} verts)")
+
+ weights = np.zeros(n_verts, dtype=np.float64)
+
+ if cam_idx > stop_index:
+ # Store zeros (camera disabled) and continue
+ attr_name = f"_SG_VisWeight_{cam_idx}_{mat_id}"
+ existing = obj.data.attributes.get(attr_name)
+ if existing:
+ obj.data.attributes.remove(existing)
+ attr = obj.data.attributes.new(
+ name=attr_name, type='FLOAT', domain='POINT')
+ 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]
+
+ # ── 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)
+
+ # Angle threshold
+ dot_dn = np.abs(np.einsum('ij,ij->i', direction, normals_np))
+ dot_dn = np.clip(dot_dn, 0.0, 1.0)
+ angle_deg = np.degrees(np.arccos(dot_dn))
+ valid &= angle_deg < angle_threshold
+
+ # ── BVH raycast only for candidates that passed filters ───
+ candidate_indices = np.nonzero(valid)[0]
+ occluded = np.zeros(n_verts, dtype=bool)
+ for vi in candidate_indices:
+ d_mu = Vector(direction[vi])
+ hit_loc, _, _, hit_dist = scene_tree.ray_cast(
+ cam_loc_mu, d_mu, float(dists[vi]) + 0.01)
+ if hit_loc is not None and dists[vi] > hit_dist + 0.001:
+ occluded[vi] = True
+
+ valid &= ~occluded
+
+ # ── Silhouette erosion ─────────────────────────────────────
+ # Cast offset rays around each visible vertex. If any offset
+ # ray hits an occluder significantly closer, this vertex is
+ # near a depth discontinuity (e.g. hand edge on body) and its
+ # weight should be zeroed so another camera fills that area.
+ silhouette_margin_px = getattr(scene, 'sg_silhouette_margin', 3)
+ if silhouette_margin_px > 0:
+ # Angular offset per pixel (radians)
+ cam_res_x_n, cam_res_y_n = _get_camera_resolution(camera, scene)
+ rad_per_px = fov / max(cam_res_x_n, 1)
+ offset_angle = rad_per_px * silhouette_margin_px
+
+ visible_indices = np.nonzero(valid)[0]
+ near_silhouette = np.zeros(n_verts, dtype=bool)
+
+ # Offset directions in camera space
+ offsets_right = cam_right_np * offset_angle
+ offsets_up = cam_up_np * offset_angle
+
+ use_8_rays = getattr(scene, 'sg_silhouette_rays', '4') == '8'
+ if use_8_rays:
+ diag = offset_angle * 0.7071067811865476 # 1/sqrt(2)
+ offsets_diag_ru = cam_right_np * diag + cam_up_np * diag
+ offsets_diag_rd = cam_right_np * diag - cam_up_np * diag
+ all_offsets = (offsets_right, -offsets_right,
+ offsets_up, -offsets_up,
+ offsets_diag_ru, -offsets_diag_ru,
+ offsets_diag_rd, -offsets_diag_rd)
+ else:
+ all_offsets = (offsets_right, -offsets_right,
+ offsets_up, -offsets_up)
+
+ for vi in visible_indices:
+ d_base = direction[vi]
+ dist_v = float(dists[vi])
+ is_edge = False
+ for off in all_offsets:
+ d_off = d_base + off
+ # 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)
+ h_loc, _, _, h_dist = scene_tree.ray_cast(
+ cam_loc_mu, d_off_mu, dist_v + 0.01)
+ depth_thr = getattr(scene, 'sg_silhouette_depth', 0.05)
+ if h_loc is not None and dist_v > h_dist + depth_thr:
+ is_edge = True
+ break
+ if is_edge:
+ near_silhouette[vi] = True
+
+ valid &= ~near_silhouette
+
+ # ── Vectorised weight + feather ────────────────────────────
+ w = np.power(np.maximum(dot_dn, 1e-12), power_exponent)
+
+ if feather_width > 0:
+ nx = np.abs(x_proj) / filmw
+ ny = np.abs(y_proj) / filmh
+ edge = np.maximum(nx, ny)
+ low = 1.0 - feather_width
+ need_feather = edge > low
+ t = np.clip((edge - low) / feather_width, 0.0, 1.0)
+ ss = t * t * (3.0 - 2.0 * t) # smoothstep
+ ff = np.power(np.maximum(1.0 - ss, 0.0),
+ max(feather_gamma, 0.01))
+ w = np.where(need_feather, w * ff, w)
+
+ weights[valid] = w[valid]
+
+ # Store as FLOAT POINT-domain attribute on the *base* mesh
+ attr_name = f"_SG_VisWeight_{cam_idx}_{mat_id}"
+ existing = obj.data.attributes.get(attr_name)
+ if existing:
+ obj.data.attributes.remove(existing)
+ attr = obj.data.attributes.new(
+ name=attr_name, type='FLOAT', domain='POINT')
+ attr.data.foreach_set("value", weights.tolist())
+
+ # Force UI update between cameras to keep Blender responsive
+ if context.window:
+ context.window.cursor_set('WAIT')
+ for area in context.screen.areas:
+ area.tag_redraw()
+
+ obj.data.update()
+
+
+def create_native_raycast_visibility(nodes, links, camera, geometry, context, i, mat_id, stop_index):
+ """
+ Creates a visibility weight node group using the native Raycast shader node (Blender 5.1+).
+ Replicates the OSL raycast.osl shader logic:
+ 1. Frustum check (is the surface within the camera FOV)
+ 2. Occlusion check (Raycast from camera, compare Hit Distance with expected distance)
+ 3. Angle check (surface normal vs ray direction)
+ 4. Weight = pow(orthogonality, Power)
+
+ Returns: (weight_output_node, subtract_node, normalize_node, length_node,
+ camera_loc_node, camera_fov_node, camera_aspect_node, camera_dir_node, camera_up_node)
+ """
+ x_base = -400
+ y_base = (-800) * i
+
+ # --- Camera location (CombineXYZ) ---
+ add_camera_loc = nodes.new("ShaderNodeCombineXYZ")
+ add_camera_loc.location = (-600, 200 + 300 * i)
+ add_camera_loc.inputs[0].default_value = camera.location.x
+ add_camera_loc.inputs[1].default_value = camera.location.y
+ add_camera_loc.inputs[2].default_value = camera.location.z
+
+ # --- Direction: Geometry.Position - CameraLoc ---
+ subtract = nodes.new("ShaderNodeVectorMath")
+ subtract.operation = 'SUBTRACT'
+ subtract.location = (x_base, -300 + y_base)
+ subtract.inputs[1].default_value = camera.location
+
+ normalize_node = nodes.new("ShaderNodeVectorMath")
+ normalize_node.operation = 'NORMALIZE'
+ normalize_node.location = (x_base, -500 + y_base)
+
+ links.new(geometry.outputs["Position"], subtract.inputs[0])
+ links.new(subtract.outputs["Vector"], normalize_node.inputs[0])
+
+ # --- Length (camera-to-surface distance) ---
+ length_node = nodes.new("ShaderNodeVectorMath")
+ length_node.operation = 'LENGTH'
+ length_node.location = (x_base, 200 * (i + 1))
+ links.new(subtract.outputs["Vector"], length_node.inputs[0])
+
+ # --- Camera FOV ---
+ camera_fov_node = nodes.new("ShaderNodeValue")
+ camera_fov_node.location = (-800, 200 + 300 * i)
+ cam_res_x, cam_res_y = _get_camera_resolution(camera, context.scene)
+ fov = camera.data.angle_x
+ if cam_res_y > cam_res_x:
+ fov = 2 * atan(tan(fov / 2) * cam_res_x / cam_res_y)
+ camera_fov_node.outputs[0].default_value = fov
+
+ # --- Camera aspect ratio ---
+ camera_aspect_node = nodes.new("ShaderNodeValue")
+ camera_aspect_node.location = (-800, 100 + 300 * i)
+ camera_aspect_node.outputs[0].default_value = cam_res_x / cam_res_y
+
+ # --- Camera direction (CombineXYZ) ---
+ cam_dir_vec = camera.matrix_world.to_quaternion() @ Vector((0, 0, -1))
+ camera_dir_node = nodes.new("ShaderNodeCombineXYZ")
+ camera_dir_node.location = (-800, 0 + 300 * i)
+ camera_dir_node.inputs[0].default_value = cam_dir_vec.x
+ camera_dir_node.inputs[1].default_value = cam_dir_vec.y
+ camera_dir_node.inputs[2].default_value = cam_dir_vec.z
+
+ # --- Camera up (CombineXYZ) ---
+ cam_up_vec = camera.matrix_world.to_quaternion() @ Vector((0, 1, 0))
+ camera_up_node = nodes.new("ShaderNodeCombineXYZ")
+ camera_up_node.location = (-800, -100 + 300 * i)
+ camera_up_node.inputs[0].default_value = cam_up_vec.x
+ camera_up_node.inputs[1].default_value = cam_up_vec.y
+ camera_up_node.inputs[2].default_value = cam_up_vec.z
+
+ # ========================================
+ # FRUSTUM CHECK (using vector math nodes)
+ # ========================================
+ norm_forward = nodes.new("ShaderNodeVectorMath")
+ norm_forward.operation = 'NORMALIZE'
+ norm_forward.location = (-700, y_base - 100)
+ links.new(camera_dir_node.outputs["Vector"], norm_forward.inputs[0])
+
+ cross_fwd_up = nodes.new("ShaderNodeVectorMath")
+ cross_fwd_up.operation = 'CROSS_PRODUCT'
+ cross_fwd_up.location = (-700, y_base - 200)
+ links.new(norm_forward.outputs["Vector"], cross_fwd_up.inputs[0])
+ links.new(camera_up_node.outputs["Vector"], cross_fwd_up.inputs[1])
+
+ norm_right = nodes.new("ShaderNodeVectorMath")
+ norm_right.operation = 'NORMALIZE'
+ norm_right.location = (-600, y_base - 200)
+ links.new(cross_fwd_up.outputs["Vector"], norm_right.inputs[0])
+
+ cross_right_fwd = nodes.new("ShaderNodeVectorMath")
+ cross_right_fwd.operation = 'CROSS_PRODUCT'
+ cross_right_fwd.location = (-700, y_base - 300)
+ links.new(norm_right.outputs["Vector"], cross_right_fwd.inputs[0])
+ links.new(norm_forward.outputs["Vector"], cross_right_fwd.inputs[1])
+
+ norm_up_vec = nodes.new("ShaderNodeVectorMath")
+ norm_up_vec.operation = 'NORMALIZE'
+ norm_up_vec.location = (-600, y_base - 300)
+ links.new(cross_right_fwd.outputs["Vector"], norm_up_vec.inputs[0])
+
+ neg_forward = nodes.new("ShaderNodeVectorMath")
+ neg_forward.operation = 'SCALE'
+ neg_forward.location = (-600, y_base - 400)
+ neg_forward.inputs["Scale"].default_value = -1.0
+ links.new(norm_forward.outputs["Vector"], neg_forward.inputs[0])
+
+ dot_d_right = nodes.new("ShaderNodeVectorMath")
+ dot_d_right.operation = 'DOT_PRODUCT'
+ dot_d_right.location = (-400, y_base - 200)
+ links.new(normalize_node.outputs["Vector"], dot_d_right.inputs[0])
+ links.new(norm_right.outputs["Vector"], dot_d_right.inputs[1])
+
+ dot_d_up = nodes.new("ShaderNodeVectorMath")
+ dot_d_up.operation = 'DOT_PRODUCT'
+ dot_d_up.location = (-400, y_base - 300)
+ links.new(normalize_node.outputs["Vector"], dot_d_up.inputs[0])
+ links.new(norm_up_vec.outputs["Vector"], dot_d_up.inputs[1])
+
+ dot_d_neg_fwd = nodes.new("ShaderNodeVectorMath")
+ dot_d_neg_fwd.operation = 'DOT_PRODUCT'
+ dot_d_neg_fwd.location = (-400, y_base - 400)
+ links.new(normalize_node.outputs["Vector"], dot_d_neg_fwd.inputs[0])
+ links.new(neg_forward.outputs["Vector"], dot_d_neg_fwd.inputs[1])
+
+ cam_z_check = nodes.new("ShaderNodeMath")
+ cam_z_check.operation = 'LESS_THAN'
+ cam_z_check.location = (-200, y_base - 400)
+ cam_z_check.inputs[1].default_value = 0.0
+ links.new(dot_d_neg_fwd.outputs["Value"], cam_z_check.inputs[0])
+
+ fov_half = nodes.new("ShaderNodeMath")
+ fov_half.operation = 'MULTIPLY'
+ fov_half.location = (-400, y_base - 500)
+ fov_half.inputs[1].default_value = 0.5
+ links.new(camera_fov_node.outputs[0], fov_half.inputs[0])
+
+ filmw = nodes.new("ShaderNodeMath")
+ filmw.operation = 'TANGENT'
+ filmw.location = (-300, y_base - 500)
+ links.new(fov_half.outputs[0], filmw.inputs[0])
+
+ filmh = nodes.new("ShaderNodeMath")
+ filmh.operation = 'DIVIDE'
+ filmh.location = (-200, y_base - 500)
+ links.new(filmw.outputs[0], filmh.inputs[0])
+ links.new(camera_aspect_node.outputs[0], filmh.inputs[1])
+
+ neg_cam_z = nodes.new("ShaderNodeMath")
+ neg_cam_z.operation = 'MULTIPLY'
+ neg_cam_z.location = (-200, y_base - 600)
+ neg_cam_z.inputs[1].default_value = -1.0
+ links.new(dot_d_neg_fwd.outputs["Value"], neg_cam_z.inputs[0])
+
+ x_proj = nodes.new("ShaderNodeMath")
+ x_proj.operation = 'DIVIDE'
+ x_proj.location = (-100, y_base - 200)
+ links.new(dot_d_right.outputs["Value"], x_proj.inputs[0])
+ links.new(neg_cam_z.outputs[0], x_proj.inputs[1])
+
+ y_proj = nodes.new("ShaderNodeMath")
+ y_proj.operation = 'DIVIDE'
+ y_proj.location = (-100, y_base - 300)
+ links.new(dot_d_up.outputs["Value"], y_proj.inputs[0])
+ links.new(neg_cam_z.outputs[0], y_proj.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])
+
+ x_in_bounds = nodes.new("ShaderNodeMath")
+ x_in_bounds.operation = 'LESS_THAN'
+ x_in_bounds.location = (100, y_base - 200)
+ links.new(abs_x.outputs[0], x_in_bounds.inputs[0])
+ links.new(filmw.outputs[0], x_in_bounds.inputs[1])
+
+ 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])
+
+ y_in_bounds = nodes.new("ShaderNodeMath")
+ y_in_bounds.operation = 'LESS_THAN'
+ y_in_bounds.location = (100, y_base - 300)
+ links.new(abs_y.outputs[0], y_in_bounds.inputs[0])
+ links.new(filmh.outputs[0], y_in_bounds.inputs[1])
+
+ frustum_xy = nodes.new("ShaderNodeMath")
+ frustum_xy.operation = 'MINIMUM'
+ frustum_xy.location = (200, y_base - 250)
+ links.new(x_in_bounds.outputs[0], frustum_xy.inputs[0])
+ links.new(y_in_bounds.outputs[0], frustum_xy.inputs[1])
+
+ frustum_ok = nodes.new("ShaderNodeMath")
+ frustum_ok.operation = 'MINIMUM'
+ frustum_ok.location = (300, y_base - 300)
+ links.new(cam_z_check.outputs[0], frustum_ok.inputs[0])
+ links.new(frustum_xy.outputs[0], frustum_ok.inputs[1])
+
+ # ========================================
+ # RAYCAST NODE (occlusion check)
+ # ========================================
+ raycast = nodes.new("ShaderNodeRaycast")
+ raycast.location = (x_base + 200, y_base)
+ links.new(add_camera_loc.outputs["Vector"], raycast.inputs["Position"])
+ links.new(normalize_node.outputs["Vector"], raycast.inputs["Direction"])
+ threshold_plus = nodes.new("ShaderNodeMath")
+ threshold_plus.operation = 'ADD'
+ threshold_plus.location = (x_base + 100, y_base + 100)
+ threshold_plus.inputs[1].default_value = 0.01
+ links.new(length_node.outputs["Value"], threshold_plus.inputs[0])
+ links.new(threshold_plus.outputs[0], raycast.inputs["Length"])
+
+ hit_dist_plus = nodes.new("ShaderNodeMath")
+ hit_dist_plus.operation = 'ADD'
+ hit_dist_plus.location = (x_base + 400, y_base + 50)
+ hit_dist_plus.inputs[1].default_value = 0.001
+ links.new(raycast.outputs["Hit Distance"], hit_dist_plus.inputs[0])
+
+ dist_check = nodes.new("ShaderNodeMath")
+ dist_check.operation = 'LESS_THAN'
+ dist_check.location = (x_base + 500, y_base + 50)
+ links.new(length_node.outputs["Value"], dist_check.inputs[0])
+ links.new(hit_dist_plus.outputs[0], dist_check.inputs[1])
+
+ occlusion_ok = nodes.new("ShaderNodeMath")
+ occlusion_ok.operation = 'MINIMUM'
+ occlusion_ok.location = (x_base + 600, y_base + 50)
+ links.new(raycast.outputs["Is Hit"], occlusion_ok.inputs[0])
+ links.new(dist_check.outputs[0], occlusion_ok.inputs[1])
+
+ # ========================================
+ # ANGLE CHECK + WEIGHT
+ # ========================================
+ dot_d_normal = nodes.new("ShaderNodeVectorMath")
+ dot_d_normal.operation = 'DOT_PRODUCT'
+ dot_d_normal.location = (x_base + 300, y_base - 150)
+ links.new(normalize_node.outputs["Vector"], dot_d_normal.inputs[0])
+ links.new(geometry.outputs["Normal"], dot_d_normal.inputs[1])
+
+ clamp_dot = nodes.new("ShaderNodeMath")
+ clamp_dot.operation = 'MAXIMUM'
+ clamp_dot.location = (x_base + 400, y_base - 150)
+ clamp_dot.inputs[1].default_value = -1.0
+ links.new(dot_d_normal.outputs["Value"], clamp_dot.inputs[0])
+
+ clamp_dot2 = nodes.new("ShaderNodeMath")
+ clamp_dot2.operation = 'MINIMUM'
+ clamp_dot2.location = (x_base + 500, y_base - 150)
+ clamp_dot2.inputs[1].default_value = 1.0
+ links.new(clamp_dot.outputs[0], clamp_dot2.inputs[0])
+
+ abs_dot = nodes.new("ShaderNodeMath")
+ abs_dot.operation = 'ABSOLUTE'
+ abs_dot.location = (x_base + 600, y_base - 150)
+ links.new(clamp_dot2.outputs[0], abs_dot.inputs[0])
+
+ acos_node = nodes.new("ShaderNodeMath")
+ acos_node.operation = 'ARCCOSINE'
+ acos_node.location = (x_base + 700, y_base - 150)
+ links.new(abs_dot.outputs[0], acos_node.inputs[0])
+
+ to_degrees = nodes.new("ShaderNodeMath")
+ to_degrees.operation = 'MULTIPLY'
+ to_degrees.location = (x_base + 800, y_base - 150)
+ to_degrees.inputs[1].default_value = 180.0 / pi
+ links.new(acos_node.outputs[0], to_degrees.inputs[0])
+
+ angle_check = nodes.new("ShaderNodeMath")
+ angle_check.operation = 'LESS_THAN'
+ angle_check.location = (x_base + 900, y_base - 150)
+ angle_check.inputs[1].default_value = context.scene.discard_factor
+ angle_check.label = f"AngleThreshold-{i}-{mat_id}"
+ links.new(to_degrees.outputs[0], angle_check.inputs[0])
+
+ power_node = nodes.new("ShaderNodeMath")
+ power_node.operation = 'POWER'
+ power_node.location = (x_base + 700, y_base - 250)
+ power_node.inputs[1].default_value = context.scene.weight_exponent
+ power_node.label = 'power_weight'
+ links.new(abs_dot.outputs[0], power_node.inputs[0])
+
+ # ========================================
+ # COMBINE ALL: frustum_ok * occlusion_ok * angle_ok * weight
+ # ========================================
+ combine1 = nodes.new("ShaderNodeMath")
+ combine1.operation = 'MULTIPLY'
+ combine1.location = (x_base + 800, y_base)
+ links.new(frustum_ok.outputs[0], combine1.inputs[0])
+ links.new(occlusion_ok.outputs[0], combine1.inputs[1])
+
+ combine2 = nodes.new("ShaderNodeMath")
+ combine2.operation = 'MULTIPLY'
+ combine2.location = (x_base + 900, y_base)
+ links.new(combine1.outputs[0], combine2.inputs[0])
+ links.new(angle_check.outputs[0], combine2.inputs[1])
+
+ final_weight = nodes.new("ShaderNodeMath")
+ final_weight.operation = 'MULTIPLY'
+ final_weight.location = (x_base + 1000, y_base)
+ final_weight.label = f"Angle-{i}-{mat_id}"
+ links.new(combine2.outputs[0], final_weight.inputs[0])
+ links.new(power_node.outputs[0], final_weight.inputs[1])
+
+ return (final_weight, subtract, normalize_node, length_node,
+ add_camera_loc, camera_fov_node, camera_aspect_node,
+ camera_dir_node, camera_up_node)
+
+
+def create_native_feather(nodes, links, normalize_node, camera_fov_node, camera_aspect_node,
+ camera_dir_node, camera_up_node, 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.
+
+ Returns the feather output node (ShaderNodeMath).
+ """
+ x_base = -400
+ y_base = (-800) * i - 400 # Offset below the raycast nodes
+
+ # Build camera basis (reuse from raycast, but we need the forward/right/up)
+ norm_forward = nodes.new("ShaderNodeVectorMath")
+ norm_forward.operation = 'NORMALIZE'
+ norm_forward.location = (x_base - 300, y_base)
+ links.new(camera_dir_node.outputs["Vector"], norm_forward.inputs[0])
+
+ cross_fwd_up = nodes.new("ShaderNodeVectorMath")
+ cross_fwd_up.operation = 'CROSS_PRODUCT'
+ cross_fwd_up.location = (x_base - 300, y_base - 100)
+ links.new(norm_forward.outputs["Vector"], cross_fwd_up.inputs[0])
+ links.new(camera_up_node.outputs["Vector"], cross_fwd_up.inputs[1])
+
+ norm_right = nodes.new("ShaderNodeVectorMath")
+ norm_right.operation = 'NORMALIZE'
+ norm_right.location = (x_base - 200, y_base - 100)
+ links.new(cross_fwd_up.outputs["Vector"], norm_right.inputs[0])
+
+ cross_right_fwd = nodes.new("ShaderNodeVectorMath")
+ cross_right_fwd.operation = 'CROSS_PRODUCT'
+ cross_right_fwd.location = (x_base - 300, y_base - 200)
+ links.new(norm_right.outputs["Vector"], cross_right_fwd.inputs[0])
+ links.new(norm_forward.outputs["Vector"], cross_right_fwd.inputs[1])
+
+ norm_up_vec = nodes.new("ShaderNodeVectorMath")
+ norm_up_vec.operation = 'NORMALIZE'
+ norm_up_vec.location = (x_base - 200, y_base - 200)
+ links.new(cross_right_fwd.outputs["Vector"], norm_up_vec.inputs[0])
+
+ neg_forward = nodes.new("ShaderNodeVectorMath")
+ neg_forward.operation = 'SCALE'
+ neg_forward.location = (x_base - 200, y_base - 300)
+ neg_forward.inputs["Scale"].default_value = -1.0
+ links.new(norm_forward.outputs["Vector"], neg_forward.inputs[0])
+
+ # cam_x = dot(d, right)
+ dot_d_right = nodes.new("ShaderNodeVectorMath")
+ dot_d_right.operation = 'DOT_PRODUCT'
+ dot_d_right.location = (x_base, y_base - 100)
+ links.new(normalize_node.outputs["Vector"], dot_d_right.inputs[0])
+ links.new(norm_right.outputs["Vector"], dot_d_right.inputs[1])
+
+ # cam_y = dot(d, upVec)
+ dot_d_up = nodes.new("ShaderNodeVectorMath")
+ dot_d_up.operation = 'DOT_PRODUCT'
+ dot_d_up.location = (x_base, y_base - 200)
+ links.new(normalize_node.outputs["Vector"], dot_d_up.inputs[0])
+ links.new(norm_up_vec.outputs["Vector"], dot_d_up.inputs[1])
+
+ # cam_z = dot(d, -forward)
+ dot_d_neg_fwd = nodes.new("ShaderNodeVectorMath")
+ dot_d_neg_fwd.operation = 'DOT_PRODUCT'
+ dot_d_neg_fwd.location = (x_base, y_base - 300)
+ links.new(normalize_node.outputs["Vector"], dot_d_neg_fwd.inputs[0])
+ links.new(neg_forward.outputs["Vector"], dot_d_neg_fwd.inputs[1])
+
+ # cam_z >= 0 check (behind camera -> result = 0)
+ cam_z_check = nodes.new("ShaderNodeMath")
+ cam_z_check.operation = 'LESS_THAN'
+ cam_z_check.location = (x_base + 100, y_base - 300)
+ cam_z_check.inputs[1].default_value = 0.0
+ links.new(dot_d_neg_fwd.outputs["Value"], cam_z_check.inputs[0])
+
+ # filmw = tan(FOV * 0.5)
+ fov_half = nodes.new("ShaderNodeMath")
+ fov_half.operation = 'MULTIPLY'
+ fov_half.location = (x_base, y_base - 400)
+ fov_half.inputs[1].default_value = 0.5
+ links.new(camera_fov_node.outputs[0], fov_half.inputs[0])
+
+ filmw = nodes.new("ShaderNodeMath")
+ filmw.operation = 'TANGENT'
+ filmw.location = (x_base + 100, y_base - 400)
+ links.new(fov_half.outputs[0], filmw.inputs[0])
+
+ # filmh = filmw / CameraAspect
+ filmh = nodes.new("ShaderNodeMath")
+ filmh.operation = 'DIVIDE'
+ filmh.location = (x_base + 200, y_base - 400)
+ links.new(filmw.outputs[0], filmh.inputs[0])
+ links.new(camera_aspect_node.outputs[0], filmh.inputs[1])
+
+ # neg_cam_z = -cam_z
+ neg_cam_z = nodes.new("ShaderNodeMath")
+ neg_cam_z.operation = 'MULTIPLY'
+ neg_cam_z.location = (x_base + 100, y_base - 500)
+ neg_cam_z.inputs[1].default_value = -1.0
+ links.new(dot_d_neg_fwd.outputs["Value"], neg_cam_z.inputs[0])
+
+ # x_proj = cam_x / -cam_z
+ x_proj = nodes.new("ShaderNodeMath")
+ x_proj.operation = 'DIVIDE'
+ x_proj.location = (x_base + 200, y_base - 100)
+ links.new(dot_d_right.outputs["Value"], x_proj.inputs[0])
+ links.new(neg_cam_z.outputs[0], x_proj.inputs[1])
+
+ # y_proj = cam_y / -cam_z
+ y_proj = nodes.new("ShaderNodeMath")
+ y_proj.operation = 'DIVIDE'
+ y_proj.location = (x_base + 200, y_base - 200)
+ links.new(dot_d_up.outputs["Value"], y_proj.inputs[0])
+ links.new(neg_cam_z.outputs[0], y_proj.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])
+
+ nx = nodes.new("ShaderNodeMath")
+ nx.operation = 'DIVIDE'
+ nx.location = (x_base + 400, y_base - 100)
+ links.new(abs_x.outputs[0], nx.inputs[0])
+ links.new(filmw.outputs[0], nx.inputs[1])
+
+ # ny = abs(y_proj) / filmh
+ 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])
+
+ ny = nodes.new("ShaderNodeMath")
+ ny.operation = 'DIVIDE'
+ ny.location = (x_base + 400, y_base - 200)
+ links.new(abs_y.outputs[0], ny.inputs[0])
+ links.new(filmh.outputs[0], ny.inputs[1])
+
+ # edge = max(nx, ny)
+ edge = nodes.new("ShaderNodeMath")
+ edge.operation = 'MAXIMUM'
+ edge.location = (x_base + 500, y_base - 150)
+ links.new(nx.outputs[0], edge.inputs[0])
+ links.new(ny.outputs[0], edge.inputs[1])
+
+ # Get feather parameters
+ if context.scene.visibility_vignette:
+ feather_val = context.scene.visibility_vignette_width
+ gamma_val = context.scene.visibility_vignette_softness
+ else:
+ feather_val = 0.0
+ gamma_val = 1.0
+
+ feather_val = max(0.0, min(feather_val, 0.49))
+
+ if feather_val > 0.0:
+ # edge_factor = 1.0 - smoothstep(1.0 - feather, 1.0, edge)
+ # Approximate smoothstep using a Map Range node with SMOOTHSTEP interpolation
+ map_range = nodes.new("ShaderNodeMapRange")
+ map_range.location = (x_base + 600, y_base - 150)
+ map_range.interpolation_type = 'SMOOTHSTEP'
+ map_range.inputs["From Min"].default_value = 1.0 - feather_val
+ map_range.inputs["From Max"].default_value = 1.0
+ map_range.inputs["To Min"].default_value = 1.0 # Invert: edge=low -> factor=1
+ map_range.inputs["To Max"].default_value = 0.0 # edge=high -> factor=0
+ links.new(edge.outputs[0], map_range.inputs["Value"])
+
+ # edge_factor = pow(smoothstep_result, gamma)
+ pow_node = nodes.new("ShaderNodeMath")
+ pow_node.operation = 'POWER'
+ pow_node.location = (x_base + 700, y_base - 150)
+ pow_node.inputs[1].default_value = max(0.01, gamma_val)
+ links.new(map_range.outputs[0], pow_node.inputs[0])
+
+ # Multiply by cam_z_check (0 if behind camera)
+ final_feather = nodes.new("ShaderNodeMath")
+ final_feather.operation = 'MULTIPLY'
+ final_feather.location = (x_base + 800, y_base - 150)
+ final_feather.label = f"Feather-{i}-{mat_id}"
+ links.new(cam_z_check.outputs[0], final_feather.inputs[0])
+ links.new(pow_node.outputs[0], final_feather.inputs[1])
+ else:
+ # No feathering: result = cam_z_check (1 if in front, 0 behind)
+ final_feather = cam_z_check
+ final_feather.label = f"Feather-{i}-{mat_id}"
+
+ return final_feather
+
+
+_SG_VORONOI_PLACEHOLDER_NAME = "_SG_Voronoi_Placeholder"
+
+def _get_voronoi_placeholder(color=(1.0, 0.0, 1.0)):
+ """Get or create a 1x1 solid-colour image used as placeholder for
+ non-generated cameras in Voronoi projection mode. The colour is
+ updated every call so it always matches the current setting."""
+ img = bpy.data.images.get(_SG_VORONOI_PLACEHOLDER_NAME)
+ if img is None:
+ img = bpy.data.images.new(_SG_VORONOI_PLACEHOLDER_NAME, width=1, height=1)
+ img.colorspace_settings.name = 'Non-Color'
+ img.pack()
+ img.pixels[:] = [color[0], color[1], color[2], 1.0]
+ return img
+
+
+def project_image(context, to_project, mat_id, stop_index=1000000):
+ """
+ Projects an image onto all mesh objects using UV Project Modifier.
+ """
+ def build_mix_tree(shaders, weight_nodes, nodes_collection, links, last_node=None, level=0, stop_index=1000000):
+ """
+ Recursively builds a binary tree of mix shader nodes.
+ The mix factor between two shader nodes is computed dynamically using
+ the outputs of weight_nodes assumed to come from OSL/maths nodes.
+
+ For each pair, the total weight is created using an ADD node.
+ Then a DIVIDE node computes:
+ mix_factor = weight_A / (weight_A + weight_B)
+ which is connected to the Mix Shader node's Fac input.
+
+ Params:
+ shaders: list of shader nodes (e.g., Principled BSDF nodes).
+ weight_nodes: list of nodes whose outputs provide the dynamic weight values.
+ (e.g. the less_than nodes outputs)
+ nodes_collection: the node tree's nodes collection.
+ links: the node tree's links collection.
+ Returns:
+ A tuple (final_shader, final_weight_node).
+ """
+
+ def _combine_angle_feather(nodes_col, lnk, cr_angle_out, cr_feather_out, loc):
+ """Combine angle-ramp and feather-ramp outputs into a single
+ visibility weight: ``angle_vis × feather_vis``.
+
+ Returns the output node of the final MULTIPLY.
+ """
+ x, y = loc
+ mult = nodes_col.new("ShaderNodeMath")
+ mult.operation = 'MULTIPLY'
+ mult.location = (x, y)
+ lnk.new(cr_angle_out, mult.inputs[0])
+ lnk.new(cr_feather_out, mult.inputs[1])
+ return mult
+
+ # Compute offsets based on recursion level
+ is_local_edit = (context.scene.generation_method == 'local_edit' or (context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method == 'local_edit'))
+ if is_local_edit:
+ last_node_x = last_node.location[0] if last_node else 0
+ x_offset = 1000 + level * 800 + last_node_x + 200 if context.scene.early_priority else 1000 + level * 600 + last_node_x + 200
+ else:
+ x_offset = 1000 + level * 800 if context.scene.early_priority else 1000 + level * 600
+ y_offset = 0
+ if len(shaders) == 1:
+ # Base-case: Mix single color with fallback magenta
+ final_mix = nodes_collection.new("ShaderNodeMixRGB")
+ final_mix.location = (x_offset - 200, y_offset)
+ # Convert fallback_color from 3 to 4 components by appending alpha 1.0
+ final_mix.inputs["Color2"].default_value = (*context.scene.fallback_color, 1.0)
+ # Connect the single color output to Color1
+ links.new(shaders[0].outputs[0], final_mix.inputs["Color1"])
+ if last_node:
+ # Connect the final mix node to the last node
+ links.new(last_node.outputs[0], final_mix.inputs["Color2"])
+ # Create a compare node to drive the mix factor
+
+ if is_local_edit:
+ w_node = weight_nodes[0]
+ if isinstance(w_node, tuple):
+ angle_node = w_node[0]
+ feather_node = w_node[1]
+ ef_node = w_node[2] if len(w_node) >= 3 else None
+
+ # Create Ramps (Output Visibility: 0=Invis, 1=Vis)
+ # Angle Ramp
+ cr_angle = nodes_collection.new("ShaderNodeValToRGB")
+ cr_angle.location = (x_offset - 500, y_offset)
+ cr_angle.color_ramp.elements[0].position = context.scene.refine_angle_ramp_pos_0 if context.scene.refine_angle_ramp_active else 0.0
+ cr_angle.color_ramp.elements[0].color = (0, 0, 0, 1) # Black (Invis)
+ cr_angle.color_ramp.elements[1].position = context.scene.refine_angle_ramp_pos_1 if context.scene.refine_angle_ramp_active else 0.0
+ cr_angle.color_ramp.elements[1].color = (1, 1, 1, 1) # White (Vis)
+ cr_angle.color_ramp.interpolation = 'LINEAR'
+ links.new(angle_node.outputs[0], cr_angle.inputs[0])
+
+ # Feather Ramp
+ cr_feather = nodes_collection.new("ShaderNodeValToRGB")
+ cr_feather.location = (x_offset - 500, y_offset - 200)
+ cr_feather.color_ramp.elements[0].position = context.scene.refine_feather_ramp_pos_0 if context.scene.visibility_vignette else 0.0
+ cr_feather.color_ramp.elements[0].color = (0, 0, 0, 1) # Black (Invis)
+ cr_feather.color_ramp.elements[1].position = context.scene.refine_feather_ramp_pos_1 if context.scene.visibility_vignette else 0.0
+ cr_feather.color_ramp.elements[1].color = (1, 1, 1, 1) # White (Vis)
+ cr_feather.color_ramp.interpolation = 'LINEAR'
+ links.new(feather_node.outputs[0], cr_feather.inputs[0])
+
+ # Combine angle and feather visibility
+ mult = _combine_angle_feather(
+ nodes_collection, links,
+ cr_angle.outputs[0], cr_feather.outputs[0],
+ loc=(x_offset - 300, y_offset)
+ )
+
+ # Optionally multiply by edge-feather mask
+ if ef_node is not None:
+ ef_mult = nodes_collection.new("ShaderNodeMath")
+ ef_mult.operation = 'MULTIPLY'
+ ef_mult.location = (x_offset - 200, y_offset - 80)
+ links.new(mult.outputs[0], ef_mult.inputs[0])
+ links.new(ef_node.outputs[0], ef_mult.inputs[1])
+ weight_out = ef_mult
+ else:
+ weight_out = mult
+
+ # Invert (Convert Visibility to Mix Factor: 1=Vis->0=Proj, 0=Invis->1=Orig)
+ invert = nodes_collection.new("ShaderNodeMath")
+ invert.operation = 'SUBTRACT'
+ invert.location = (x_offset - 150, y_offset)
+ invert.inputs[0].default_value = 1.0
+ links.new(weight_out.outputs[0], invert.inputs[1])
+
+ compare_node = invert # For return
+ links.new(invert.outputs[0], final_mix.inputs["Fac"])
+ else:
+ # Fallback if not tuple (e.g. stop_index logic used LessThan node)
+ # If it's a sum of processed weights (from multi-camera recursion), we need to invert it
+ # to get the Mix Factor (1 - TotalVis)
+ if is_local_edit:
+ invert = nodes_collection.new("ShaderNodeMath")
+ invert.operation = 'SUBTRACT'
+ invert.location = (x_offset - 150, y_offset)
+ invert.inputs[0].default_value = 1.0
+ links.new(w_node.outputs[0], invert.inputs[1])
+ links.new(invert.outputs[0], final_mix.inputs["Fac"])
+ compare_node = invert
+ else:
+ links.new(w_node.outputs[0], final_mix.inputs["Fac"])
+ compare_node = w_node
+ else:
+ compare_node = nodes_collection.new("ShaderNodeMath")
+ compare_node.operation = 'COMPARE'
+ # Value to compare against (Value 2)
+ compare_node.inputs[1].default_value = 0.0
+ # Epsilon for comparison
+ compare_node.inputs[2].default_value = 0.0
+ compare_node.location = (x_offset - 500, y_offset)
+ # Connect final add node to compare node
+ links.new(weight_nodes[0].outputs[0], compare_node.inputs[0])
+ links.new(compare_node.outputs[0], final_mix.inputs["Fac"])
+
+ return final_mix, compare_node
+
+ # ── Helper: extract final weight output from a weight_node item ────
+ def _get_weight_output(w_item, v_offset):
+ """Process a weight_node item (tuple or plain node) through ramps
+ and return the output socket. Used by the normalization pre-pass."""
+ if isinstance(w_item, tuple):
+ angle_node = w_item[0]
+ feather_node = w_item[1]
+ ef_node = w_item[2] if len(w_item) >= 3 else None
+
+ # Angle Ramp
+ cr_angle = nodes_collection.new("ShaderNodeValToRGB")
+ cr_angle.location = (x_offset - 1100, y_offset + v_offset)
+ cr_angle.color_ramp.elements[0].position = context.scene.refine_angle_ramp_pos_0 if context.scene.refine_angle_ramp_active else 0.0
+ cr_angle.color_ramp.elements[0].color = (0, 0, 0, 1)
+ cr_angle.color_ramp.elements[1].position = context.scene.refine_angle_ramp_pos_1 if context.scene.refine_angle_ramp_active else 0.0
+ cr_angle.color_ramp.elements[1].color = (1, 1, 1, 1)
+ cr_angle.color_ramp.interpolation = 'LINEAR'
+ links.new(angle_node.outputs[0], cr_angle.inputs[0])
+
+ # Feather Ramp
+ cr_feather = nodes_collection.new("ShaderNodeValToRGB")
+ cr_feather.location = (x_offset - 1100, y_offset + v_offset - 200)
+ cr_feather.color_ramp.elements[0].position = context.scene.refine_feather_ramp_pos_0 if context.scene.visibility_vignette else 0.0
+ cr_feather.color_ramp.elements[0].color = (0, 0, 0, 1)
+ cr_feather.color_ramp.elements[1].position = context.scene.refine_feather_ramp_pos_1 if context.scene.visibility_vignette else 0.0
+ cr_feather.color_ramp.elements[1].color = (1, 1, 1, 1)
+ cr_feather.color_ramp.interpolation = 'LINEAR'
+ links.new(feather_node.outputs[0], cr_feather.inputs[0])
+
+ # Combine angle × feather
+ m = _combine_angle_feather(
+ nodes_collection, links,
+ cr_angle.outputs[0], cr_feather.outputs[0],
+ loc=(x_offset - 900, y_offset + v_offset)
+ )
+
+ # Optionally multiply by edge-feather mask
+ if ef_node is not None:
+ ef_mult = nodes_collection.new("ShaderNodeMath")
+ ef_mult.operation = 'MULTIPLY'
+ ef_mult.location = (x_offset - 800, y_offset + v_offset - 80)
+ links.new(m.outputs[0], ef_mult.inputs[0])
+ links.new(ef_node.outputs[0], ef_mult.inputs[1])
+ return ef_mult.outputs[0]
+
+ return m.outputs[0]
+ else:
+ return w_item.outputs[0]
+
+ # ── Max-relative weight normalization (level 0 only) ──────────────
+ # At high weight exponents, pow(cos(θ), exp) can underflow to 0 in
+ # float32 for every camera, making the w_A/(w_A+w_B) ratio become
+ # 0/0 → black. To fix this robustly we:
+ # 1. Temporarily set each per-camera Power to 1.0 so the weight
+ # outputs become the *base* values (binary_gates × |cos(θ)|)
+ # that never underflow.
+ # 2. Normalize those base weights by the per-pixel maximum so the
+ # best camera gets 1.0.
+ # 3. Re-apply the user exponent to the normalized [0,1] values
+ # where underflow is impossible.
+ # Mathematically: pow(cos/max_cos, exp) == pow(cos, exp)/pow(max_cos, exp),
+ # so the blending ratios are identical to the original formula.
+ if level == 0 and len(shaders) > 1:
+ # ── Step 1: Neutralize per-camera Power (set to 1.0) ──────
+ user_exponent = None
+ for idx in range(len(weight_nodes)):
+ w = weight_nodes[idx]
+ node = w[0] if isinstance(w, tuple) else w
+
+ # Skip disabled cameras (stop_index → LESS_THAN always-0)
+ if node.type == 'MATH' and node.operation == 'LESS_THAN':
+ continue
+
+ # Native raycast path:
+ # final_weight (MULTIPLY, label Angle-*) →
+ # inputs[1] → power_node (POWER, label power_weight)
+ if (node.type == 'MATH' and node.operation == 'MULTIPLY'
+ and node.label.startswith('Angle-')):
+ if node.inputs[1].links:
+ pn = node.inputs[1].links[0].from_node
+ if (pn.type == 'MATH' and pn.operation == 'POWER'
+ and pn.label == 'power_weight'):
+ if user_exponent is None:
+ user_exponent = pn.inputs[1].default_value
+ pn.inputs[1].default_value = 1.0
+
+ # 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
+
+ if user_exponent is None:
+ user_exponent = 1.0
+
+ # ── Step 2: Get base weight outputs (now power = 1) ───────
+ all_w_outputs = []
+ for idx in range(len(weight_nodes)):
+ v_off = -200 * idx
+ all_w_outputs.append(_get_weight_output(weight_nodes[idx], v_off))
+
+ # ── Step 3: Find per-pixel maximum ────────────────────────
+ max_node = nodes_collection.new("ShaderNodeMath")
+ max_node.operation = 'MAXIMUM'
+ max_node.location = (x_offset - 1500, y_offset - 600)
+ max_node.label = "WeightMax"
+ links.new(all_w_outputs[0], max_node.inputs[0])
+ if len(all_w_outputs) > 1:
+ links.new(all_w_outputs[1], max_node.inputs[1])
+ else:
+ max_node.inputs[1].default_value = 0.0
+
+ for idx in range(2, len(all_w_outputs)):
+ next_max = nodes_collection.new("ShaderNodeMath")
+ next_max.operation = 'MAXIMUM'
+ next_max.location = (x_offset - 1500 + 150 * (idx - 1),
+ y_offset - 600)
+ next_max.label = "WeightMax"
+ links.new(max_node.outputs[0], next_max.inputs[0])
+ links.new(all_w_outputs[idx], next_max.inputs[1])
+ max_node = next_max
+
+ # Clamp max to epsilon to prevent 0/0 division
+ safe_max = nodes_collection.new("ShaderNodeMath")
+ safe_max.operation = 'MAXIMUM'
+ safe_max.location = (max_node.location[0] + 150, y_offset - 600)
+ safe_max.inputs[1].default_value = 1e-7
+ safe_max.label = "SafeMax"
+ links.new(max_node.outputs[0], safe_max.inputs[0])
+
+ # ── Step 4: Normalize + re-apply exponent ─────────────────
+ powered_nodes = []
+ for idx, w_out in enumerate(all_w_outputs):
+ # norm_w = base_w / max(base_w)
+ div = nodes_collection.new("ShaderNodeMath")
+ div.operation = 'DIVIDE'
+ div.location = (safe_max.location[0] + 200,
+ y_offset - 120 * idx)
+ div.label = f"NormW-{idx}"
+ links.new(w_out, div.inputs[0])
+ links.new(safe_max.outputs[0], div.inputs[1])
+
+ # sharp_w = pow(norm_w, user_exponent)
+ # (inputs are in [0, 1] → no underflow possible)
+ if user_exponent != 1.0:
+ pw = nodes_collection.new("ShaderNodeMath")
+ pw.operation = 'POWER'
+ pw.location = (div.location[0] + 200,
+ div.location[1])
+ pw.label = f"SharpW-{idx}"
+ pw.inputs[1].default_value = user_exponent
+ links.new(div.outputs[0], pw.inputs[0])
+ powered_nodes.append(pw)
+ else:
+ powered_nodes.append(div)
+
+ # Replace weight_nodes with sharpened-normalised nodes
+ weight_nodes = powered_nodes
+
+ new_shaders = []
+ new_weight_nodes = []
+ i = 0
+ while i < len(shaders):
+ if i + 1 < len(shaders):
+ # Sum the weights
+ vert_offset = -200 * (i // 2)
+ sum_node = nodes_collection.new("ShaderNodeMath")
+ sum_node.operation = 'ADD'
+ sum_node.location = (x_offset - 800, y_offset + vert_offset) if context.scene.early_priority else (x_offset - 600, y_offset + vert_offset)
+
+ def get_weight_output(w_item):
+ if isinstance(w_item, tuple):
+ angle_node = w_item[0]
+ feather_node = w_item[1]
+ ef_node = w_item[2] if len(w_item) >= 3 else None
+
+ # Create Ramps (Output Visibility: 0=Invis, 1=Vis)
+ # Angle Ramp
+ cr_angle = nodes_collection.new("ShaderNodeValToRGB")
+ cr_angle.location = (x_offset - 1100, y_offset + vert_offset)
+ cr_angle.color_ramp.elements[0].position = context.scene.refine_angle_ramp_pos_0 if context.scene.refine_angle_ramp_active else 0.0
+ cr_angle.color_ramp.elements[0].color = (0, 0, 0, 1) # Black (Invis)
+ cr_angle.color_ramp.elements[1].position = context.scene.refine_angle_ramp_pos_1 if context.scene.refine_angle_ramp_active else 0.0
+ cr_angle.color_ramp.elements[1].color = (1, 1, 1, 1) # White (Vis)
+ cr_angle.color_ramp.interpolation = 'LINEAR'
+ links.new(angle_node.outputs[0], cr_angle.inputs[0])
+
+ # Feather Ramp
+ cr_feather = nodes_collection.new("ShaderNodeValToRGB")
+ cr_feather.location = (x_offset - 1100, y_offset + vert_offset - 200)
+ cr_feather.color_ramp.elements[0].position = context.scene.refine_feather_ramp_pos_0 if context.scene.visibility_vignette else 0.0
+ cr_feather.color_ramp.elements[0].color = (0, 0, 0, 1) # Black (Invis)
+ cr_feather.color_ramp.elements[1].position = context.scene.refine_feather_ramp_pos_1 if context.scene.visibility_vignette else 0.0
+ cr_feather.color_ramp.elements[1].color = (1, 1, 1, 1) # White (Vis)
+ cr_feather.color_ramp.interpolation = 'LINEAR'
+ links.new(feather_node.outputs[0], cr_feather.inputs[0])
+
+ # Combine angle and feather visibility
+ m = _combine_angle_feather(
+ nodes_collection, links,
+ cr_angle.outputs[0], cr_feather.outputs[0],
+ loc=(x_offset - 900, y_offset + vert_offset)
+ )
+
+ # Optionally multiply by edge-feather mask
+ if ef_node is not None:
+ ef_mult = nodes_collection.new("ShaderNodeMath")
+ ef_mult.operation = 'MULTIPLY'
+ ef_mult.location = (x_offset - 800, y_offset + vert_offset - 80)
+ links.new(m.outputs[0], ef_mult.inputs[0])
+ links.new(ef_node.outputs[0], ef_mult.inputs[1])
+ return ef_mult.outputs[0]
+
+ return m.outputs[0]
+ else:
+ return w_item.outputs[0]
+
+ # Cache outputs to avoid creating duplicate nodes
+ w_out_1 = get_weight_output(weight_nodes[i])
+ w_out_2 = get_weight_output(weight_nodes[i+1])
+
+ links.new(w_out_1, sum_node.inputs[0])
+ links.new(w_out_2, sum_node.inputs[1])
+
+ # Compute mix factor: weight_A / (weight_A+weight_B)
+ div_node = nodes_collection.new("ShaderNodeMath")
+ div_node.operation = 'DIVIDE'
+ div_node.location = (x_offset - 600, y_offset + vert_offset) if context.scene.early_priority else (x_offset - 400, y_offset + vert_offset)
+ links.new(w_out_2, div_node.inputs[0])
+ links.new(sum_node.outputs[0], div_node.inputs[1])
+
+ if context.scene.early_priority:
+ # Add map range node
+ map_range_node = nodes_collection.new("ShaderNodeMapRange")
+ map_range_node.location = (x_offset - 400, y_offset + vert_offset)
+ # to_min, to_max, from_max are set to 0.0 and 1.0 by default
+ # set to prioritize earlier images
+ map_range_node.inputs[1].default_value = context.scene.early_priority_strength
+ links.new(div_node.outputs[0], map_range_node.inputs[0])
+
+ # Create a MixRGB node for color blending of the two inputs
+ mix_node = nodes_collection.new("ShaderNodeMixRGB")
+ mix_node.location = (x_offset - 200, y_offset + vert_offset)
+ mix_node.use_clamp = True
+ # Connect first color to Color1 and second color to Color2
+ links.new(shaders[i].outputs[0], mix_node.inputs["Color1"])
+ links.new(shaders[i+1].outputs[0], mix_node.inputs["Color2"])
+ # Use the computed mix factor
+ to_connect = div_node if not context.scene.early_priority else map_range_node
+ links.new(to_connect.outputs[0], mix_node.inputs["Fac"])
+
+ new_shaders.append(mix_node)
+ # The new effective weight is the sum (stored in sum_node)
+ new_weight_nodes.append(sum_node)
+ i += 2
+ else:
+ new_shaders.append(shaders[i])
+ new_weight_nodes.append(weight_nodes[i])
+ i += 1
+ return build_mix_tree(new_shaders, new_weight_nodes, nodes_collection, links, last_node, level+1)
+
+ cameras = [obj for obj in context.scene.objects if obj.type == 'CAMERA']
+ cameras.sort(key=lambda x: x.name)
+
+ # Force refresh of the UI
+ for area in context.screen.areas:
+ area.tag_redraw()
+
+ # Apply projection to all mesh objects for each camera
+ for i, camera in enumerate(cameras):
+ for x, obj in enumerate(to_project):
+ # We can skip the UV Projection step in sequential mode for i > 0
+ if context.scene.generation_method != 'sequential' or stop_index == 0:
+ # Deselect all objects
+ bpy.ops.object.select_all(action='DESELECT')
+ # Select object as active (needed for applying the modifier)
+ context.view_layer.objects.active = obj
+ obj.select_set(True)
+
+ # Make object data single-user before applying modifier
+ bpy.ops.object.make_single_user(object=True, obdata=True)
+
+ # Check if the data is now single user
+ if obj.data.users > 1:
+ # If not, we need to make it single user again
+ print("[StableGen] Warning: Cannot make object data single user. Making a copy.")
+ obj.data = obj.data.copy()
+ obj.data.name = f"{obj.name}_data"
+ obj.data.update()
+ # Check again
+ if obj.data.users > 1:
+ print("[StableGen] Error: Cannot make object data single user. Exiting.")
+ return Exception("Cannot make object data single user. Exiting.")
+
+ # Create or reuse a buffer UV map for the UV Project modifier
+ buffer_uv = obj.data.uv_layers.get(_SG_BUFFER_UV_NAME)
+ if not buffer_uv:
+ buffer_uv = obj.data.uv_layers.new(name=_SG_BUFFER_UV_NAME)
+
+ # Add the UV Project Modifier
+ uv_project_mod = obj.modifiers.new(name="UVProject", type='UV_PROJECT')
+
+ # Assign the active camera to the UV Project modifier
+ if not camera:
+ return False
+
+ uv_project_mod.projectors[0].object = camera
+
+ # Set the buffer UV map for the modifier
+ uv_project_mod.uv_layer = buffer_uv.name
+
+ # Calculate and set the aspect ratio (per-camera if available)
+ cam_res_x, cam_res_y = _get_camera_resolution(camera, context.scene)
+ aspect_ratio = cam_res_x / cam_res_y
+ uv_project_mod.aspect_x = aspect_ratio if aspect_ratio > 1 else 1
+ uv_project_mod.aspect_y = 1 / aspect_ratio if aspect_ratio < 1 else 1
+
+ # Set the buffer UV as active so the modifier writes to it
+ # (Blender 5.0+ ignores uv_layer and writes to the active UV map)
+ obj.data.uv_layers.active = buffer_uv
+
+ # Apply the modifier
+ bpy.ops.object.modifier_apply(modifier=uv_project_mod.name)
+
+ # Copy buffer UV data to a named attribute (no UV slot limit)
+ attr_name = f"ProjectionUV_{i}_{mat_id}"
+ _copy_uv_to_attribute(obj, _SG_BUFFER_UV_NAME, attr_name)
+
+ # Restore active UV to the first non-buffer UV layer
+ original_uv_map = obj.data.uv_layers[0]
+ if original_uv_map.name == _SG_BUFFER_UV_NAME and len(obj.data.uv_layers) > 1:
+ original_uv_map = obj.data.uv_layers[1]
+ obj.data.uv_layers.active = original_uv_map
+
+ # Clean up buffer UV map from all objects
+ for obj in to_project:
+ buffer_uv = obj.data.uv_layers.get(_SG_BUFFER_UV_NAME)
+ if buffer_uv:
+ obj.data.uv_layers.remove(buffer_uv)
+
+ # ── Optional: bake per-vertex visibility weights ──────────────────
+ use_baked_weights = getattr(context.scene, 'bake_visibility_weights', False)
+ 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.
+ is_sequential = context.scene.generation_method == 'sequential'
+ if not is_sequential or stop_index == 0:
+ _bake_visibility_weights(context, to_project, cameras, mat_id)
+
+ # Switch to Cycles (needed for Raycast shader node on all versions)
+ context.scene.render.engine = 'CYCLES'
+ # Force CPU + OSL only for Blender < 5.1 (native Raycast nodes don't need it)
+ if bpy.app.version < (5, 1, 0):
+ context.scene.cycles.device = 'CPU'
+ if hasattr(context.scene.cycles, 'shading_system'):
+ context.scene.cycles.shading_system = True
+ else:
+ context.scene.cycles.use_osl = True
+
+ processed_materials = set()
+
+ for obj in to_project:
+
+ # Deselect all objects
+ bpy.ops.object.select_all(action='DESELECT')
+ context.view_layer.objects.active = obj
+ obj.select_set(True)
+
+ # Create the material
+ to_switch = False
+ is_local_edit = (context.scene.generation_method == 'local_edit' or (context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method == 'local_edit'))
+ if is_local_edit and not context.scene.overwrite_material:
+ # Copy active material
+ mat = obj.active_material.copy()
+ obj.data.materials.append(mat)
+ to_switch = True
+ elif obj.active_material and (context.scene.overwrite_material or is_local_edit \
+ or (context.scene.generation_method == 'sequential' and stop_index > 0) or context.scene.generation_mode == 'regenerate_selected'):
+ # Use active material
+ mat = obj.active_material
+ else:
+ mat = bpy.data.materials.new(name="ProjectionMaterial")
+ obj.data.materials.append(mat)
+ # Mark as active material
+ obj.active_material_index = obj.material_slots.find(mat.name)
+ to_switch = True
+
+ if to_switch:
+ original_materials = obj.data.materials[:]
+ # Clear existing materials
+ obj.data.materials.clear()
+ # Add the new material
+ obj.data.materials.append(mat)
+ # Add the rest of the original materials
+ for original_mat in original_materials:
+ if original_mat != mat:
+ obj.data.materials.append(original_mat)
+
+ # Enable use of nodes
+ mat.use_nodes = True
+ nodes = mat.node_tree.nodes
+ links = mat.node_tree.links
+
+ previous_node = None
+ output = None
+
+ # Get the original UV map (needed for bake path).
+ # If the mesh has no UV maps at all, create one so baking / node
+ # setup doesn't crash.
+ if len(obj.data.uv_layers) == 0:
+ obj.data.uv_layers.new(name="UVMap")
+ original_uv_map = obj.data.uv_layers[0]
+ if original_uv_map.name == _SG_BUFFER_UV_NAME and len(obj.data.uv_layers) > 1:
+ 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:
+ # Clear existing nodes
+ for node in nodes:
+ nodes.remove(node)
+ else:
+ # Find the node connected to the output node
+ for node in nodes:
+ if node.type == 'OUTPUT_MATERIAL':
+ output = node
+ break
+ if output:
+ # Save the node connected to the output node into a variable
+ if output.inputs[0].links[0].from_node.type == 'BSDF_PRINCIPLED':
+ previous_node = output.inputs[0].links[0].from_node.inputs[0].links[0].from_node
+ else:
+ previous_node = output.inputs[0].links[0].from_node
+
+ if not is_local_edit:
+ output = nodes.new("ShaderNodeOutputMaterial")
+ output.location = (3000, 0)
+
+ geometry = nodes.new("ShaderNodeNewGeometry")
+ geometry.location = (-600, 0)
+
+ use_native_raycast = bpy.app.version >= (5, 1, 0)
+
+ tex_image_nodes = []
+ uv_map_nodes = []
+ subtract_nodes = []
+ normalize_nodes = []
+ script_nodes = []
+ script_nodes_outputs = []
+ add_camera_loc_nodes = []
+ length_nodes = []
+ camera_fov_nodes = []
+ camera_aspect_ratio_nodes = []
+ camera_direction_nodes = []
+ camera_up_nodes = []
+
+ def _create_edge_feather_weight(cam_index, mat_id_inner):
+ """Load the edge-feathered visibility mask for camera *cam_index*
+ and return a shader Math node whose output[0] gives a 0..1 float
+ weight (0 = edge/invisible, 1 = interior/full), or *None* on failure.
+
+ The mask is UV-projected through the same camera UV attribute
+ that projects the generated image.
+ """
+ vis_dir = get_dir_path(context, "inpaint")["visibility"]
+ ef_path = os.path.join(vis_dir, f"render{cam_index}_edgefeather.png")
+ if not os.path.exists(ef_path):
+ print(f"[StableGen] Edge-feather mask not found: {ef_path}")
+ return None
+ ef_image = get_or_load_image(ef_path, force_reload=context.scene.overwrite_material)
+ if ef_image is None:
+ return None
+
+ # Image Texture node to sample the mask via camera UV
+ mask_tex = nodes.new("ShaderNodeTexImage")
+ mask_tex.image = ef_image
+ mask_tex.extension = 'CLIP'
+ mask_tex.label = f"EdgeFeather-{cam_index}-{mat_id_inner}"
+ mask_tex.location = (-500, -200 * cam_index - 150)
+
+ # Connect to the same camera UV attribute
+ uv_node = uv_map_nodes[cam_index]
+ uv_out = "Vector" if uv_node.type == 'ATTRIBUTE' else "UV"
+ links.new(uv_node.outputs[uv_out], mask_tex.inputs["Vector"])
+
+ # Color→Float conversion via a pass-through Math node
+ to_float = nodes.new("ShaderNodeMath")
+ to_float.operation = 'MULTIPLY'
+ to_float.inputs[1].default_value = 1.0
+ to_float.location = (-300, -200 * cam_index - 150)
+ to_float.label = f"EF_Weight-{cam_index}"
+ links.new(mask_tex.outputs[0], to_float.inputs[0])
+ return to_float
+
+ # 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)
+ and context.scene.generation_method == 'sequential')
+
+ for i, camera in enumerate(cameras):
+ # Add image texture node
+ tex_image = nodes.new("ShaderNodeTexImage")
+ if i <= stop_index:
+ image_path = get_file_path(context, "generated", camera_id=i, 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 i == 0 and context.scene.sequential_ipadapter_mode == 'first':
+ image_path = get_file_path(context, "generated", camera_id=i, material_id=mat_id).replace(".png", "_ipadapter.png")
+
+ image = get_or_load_image(image_path, force_reload=context.scene.overwrite_material)
+ if image:
+ tex_image.image = image
+ elif voronoi_active:
+ # Voronoi mode: assign a solid placeholder matching the Qwen
+ # guidance fallback colour so non-generated cameras contribute
+ # that colour instead of black.
+ voronoi_color = tuple(context.scene.qwen_guidance_fallback_color)
+ tex_image.image = _get_voronoi_placeholder(voronoi_color)
+ tex_image.interpolation = 'Closest'
+
+ tex_image.location = (0, -200 * i)
+ tex_image.extension = 'CLIP'
+ tex_image.label = f"{i}-{mat_id}"
+ tex_image_nodes.append(tex_image)
+
+ # Add UV map / attribute node
+ # Use ShaderNodeAttribute to read from the corner-domain attribute
+ uv_map_node = nodes.new("ShaderNodeAttribute")
+ uv_map_node.attribute_name = f"ProjectionUV_{i}_{mat_id}"
+ uv_map_node.attribute_type = 'GEOMETRY'
+ uv_map_node.location = (-200, -200 * (i+1))
+ uv_map_nodes.append(uv_map_node)
+
+ # Compute the dot product of the direction vector and geometry normal
+
+ if use_baked_weights:
+ # ── Baked-weights path (transform-stable) ──
+ # Read pre-computed per-vertex weight from mesh attribute.
+ # A pass-through Math node is used so outputs[0] provides the
+ # scalar float (ShaderNodeAttribute outputs[0] is Color).
+ vis_attr = nodes.new("ShaderNodeAttribute")
+ vis_attr.attribute_name = f"_SG_VisWeight_{i}_{mat_id}"
+ vis_attr.attribute_type = 'GEOMETRY'
+ vis_attr.location = (-400, (-800) * i)
+ vis_attr.label = f"BakedVis-{i}-{mat_id}"
+
+ passthrough = nodes.new("ShaderNodeMath")
+ passthrough.operation = 'MULTIPLY'
+ passthrough.inputs[1].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])
+
+ scripts_to_connect = []
+ final_weight_node = passthrough
+
+ script_nodes.append(scripts_to_connect)
+
+ if i > stop_index and not voronoi_active:
+ less_than = nodes.new("ShaderNodeMath")
+ less_than.operation = 'LESS_THAN'
+ less_than.location = (-100, (-800) * i)
+ less_than.inputs[1].default_value = -1
+ links.new(passthrough.outputs[0], less_than.inputs[0])
+ script_nodes_outputs.append(less_than)
+ else:
+ script_nodes_outputs.append(final_weight_node)
+
+ 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)
+
+ elif use_native_raycast:
+ # ── Native Raycast path (Blender 5.1+) ──
+ result = create_native_raycast_visibility(
+ nodes, links, camera, geometry, context, i, mat_id, stop_index
+ )
+ angle_weight = result[0]
+ # Destructure result for create_native_feather
+ _, _, normalize_node, length_node, camera_loc_node, camera_fov_node, camera_aspect_node, camera_dir_node, camera_up_node = result
+
+ scripts_to_connect = [] # No OSL scripts to connect
+ final_weight_node = None
+
+ 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'))
+ 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
+ )
+ # Optionally add edge-feather mask as third multiplier
+ if 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 = (angle_weight, feather_weight, ef_node)
+ else:
+ final_weight_node = (angle_weight, feather_weight)
+ else:
+ final_weight_node = (angle_weight, feather_weight)
+ else:
+ final_weight_node = angle_weight
+
+ script_nodes.append(scripts_to_connect)
+
+ if i > stop_index and not voronoi_active:
+ less_than = nodes.new("ShaderNodeMath")
+ less_than.operation = 'LESS_THAN'
+ less_than.location = (-200, (-800) * i)
+ less_than.inputs[1].default_value = -1
+ source_node = final_weight_node[0] if isinstance(final_weight_node, tuple) else final_weight_node
+ links.new(source_node.outputs[0], less_than.inputs[0])
+ script_nodes_outputs.append(less_than)
+ else:
+ script_nodes_outputs.append(final_weight_node)
+
+ # Append None to node lists (connections handled inside create functions)
+ 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)
+ else:
+ # ── OSL path (Blender < 5.1) ──
+ subtract = nodes.new("ShaderNodeVectorMath")
+ subtract.operation = 'SUBTRACT'
+ subtract.location = (-400, -300 + (-800) * i)
+ subtract.inputs[1].default_value = camera.location
+ subtract_nodes.append(subtract)
+
+ normalize = nodes.new("ShaderNodeVectorMath")
+ normalize.operation = 'NORMALIZE'
+ normalize.location = (-400, -500 + (-800) * (i))
+ normalize_nodes.append(normalize)
+
+ # Add script nodes (Angle)
+ script_angle = nodes.new("ShaderNodeScript")
+ script_angle.location = (-400, (-800) * i)
+
+ # Load OSL script into internal text block for portability
+ raycast_osl_text = get_or_create_osl_text("raycast.osl")
+ if raycast_osl_text:
+ script_angle.mode = 'INTERNAL'
+ script_angle.script = raycast_osl_text
+ else:
+ script_angle.mode = 'EXTERNAL'
+ script_angle.filepath = os.path.join(os.path.dirname(__file__), "raycast.osl")
+
+ script_angle.inputs["AngleThreshold"].default_value = context.scene.discard_factor
+ script_angle.inputs["Power"].default_value = context.scene.weight_exponent
+ 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')):
+ # Add Feather script
+ script_feather = nodes.new("ShaderNodeScript")
+ script_feather.location = (-400, (-800) * i - 200) # Offset slightly
+
+ # Load OSL script into internal text block for portability
+ feather_osl_text = get_or_create_osl_text("feather.osl")
+ if feather_osl_text:
+ script_feather.mode = 'INTERNAL'
+ script_feather.script = feather_osl_text
+ else:
+ script_feather.mode = 'EXTERNAL'
+ script_feather.filepath = os.path.join(os.path.dirname(__file__), "feather.osl")
+
+ if context.scene.visibility_vignette: # Only set if feathering is active
+ script_feather.inputs["EdgeFeather"].default_value = context.scene.visibility_vignette_width
+ script_feather.inputs["EdgeGamma"].default_value = context.scene.visibility_vignette_softness
+ else:
+ script_feather.inputs["EdgeFeather"].default_value = 0.0
+ script_feather.inputs["EdgeGamma"].default_value = 1.0
+ script_feather.label = f"Feather-{i}-{mat_id}"
+ 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:
+ 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:
+ final_weight_node = (script_angle, script_feather)
+ else:
+ # Just use Angle script output directly
+ final_weight_node = script_angle
+
+ script_nodes.append(scripts_to_connect)
+
+ if i > stop_index and not voronoi_active:
+ # Connect a temporary less than node to the script node
+ less_than = nodes.new("ShaderNodeMath")
+ less_than.operation = 'LESS_THAN'
+ less_than.location = (-200, (-800) * i)
+ less_than.inputs[1].default_value = -1
+
+ # For stop_index check, we use the Angle script output
+ # (or the first element if it's a tuple)
+ source_node = final_weight_node[0] if isinstance(final_weight_node, tuple) else final_weight_node
+ links.new(source_node.outputs[0], less_than.inputs[0])
+
+ script_nodes_outputs.append(less_than)
+ else:
+ script_nodes_outputs.append(final_weight_node)
+
+ # Add additional add node, which will contain camera's FOV in first default value, and camera's aspect ratio in second default value
+ camera_fov = nodes.new("ShaderNodeValue")
+ camera_fov.location = (-600, 200 + 300 * i)
+ cam_res_x, cam_res_y = _get_camera_resolution(camera, context.scene)
+ fov = camera.data.angle_x
+ # Correct the FOV for vertical aspect ratio
+ if cam_res_y > cam_res_x:
+ fov = 2 * atan(tan(fov / 2) * cam_res_x / cam_res_y)
+ camera_fov.outputs[0].default_value = fov
+ camera_fov_nodes.append(camera_fov)
+ camera_aspect_ratio = nodes.new("ShaderNodeValue")
+ camera_aspect_ratio.location = (-600, 200 + 300 * i)
+
+ # Add camera direction and up nodes (combine XYZ)
+ camera_direction = nodes.new("ShaderNodeCombineXYZ")
+ camera_direction.location = (-600, 200 + 300 * i)
+ # Get the camera direction vector
+ camera_direction.inputs[0].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 0, -1))).x
+ camera_direction.inputs[1].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 0, -1))).y
+ camera_direction.inputs[2].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 0, -1))).z
+ camera_direction_nodes.append(camera_direction)
+ camera_up = nodes.new("ShaderNodeCombineXYZ")
+ camera_up.location = (-600, 200 + 300 * i)
+ # Get the camera up vector
+ camera_up.inputs[0].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 1, 0))).x
+ camera_up.inputs[1].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 1, 0))).y
+ camera_up.inputs[2].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 1, 0))).z
+ camera_up_nodes.append(camera_up)
+
+ camera_aspect_ratio.outputs[0].default_value = cam_res_x / cam_res_y
+ camera_aspect_ratio_nodes.append(camera_aspect_ratio)
+
+ # Add combine XYZ node
+ add_camera_loc = nodes.new("ShaderNodeCombineXYZ")
+ add_camera_loc.location = (-600, 200 + 300 * i)
+ add_camera_loc.inputs[0].default_value = camera.location.x
+ add_camera_loc.inputs[1].default_value = camera.location.y
+ add_camera_loc.inputs[2].default_value = camera.location.z
+ add_camera_loc_nodes.append(add_camera_loc)
+
+ # Add length node
+ length = nodes.new("ShaderNodeVectorMath")
+ length.operation = 'LENGTH'
+ length.location = (-400, 200 * (i+1))
+ length_nodes.append(length)
+
+ # 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
+ # But we still need to connect the nodes for the current object (done in the loop below)
+ # We need to find the existing mix node to position the output node
+ # The mix node is connected to the output node
+ mix_node = output.inputs["Surface"].links[0].from_node
+ else:
+ mix_node, _ = build_mix_tree(tex_image_nodes, script_nodes_outputs, nodes, links, previous_node, stop_index=stop_index)
+ links.new(mix_node.outputs[0], output.inputs["Surface"])
+ processed_materials.add(mat.name)
+
+ # Move output node right to the mix_node
+ output.location = (mix_node.location[0] + 400, mix_node.location[1])
+
+ for i, camera in enumerate(cameras):
+ tex_image = tex_image_nodes[i]
+ uv_map_node = uv_map_nodes[i]
+
+ # 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"])
+
+ if use_baked_weights or use_native_raycast:
+ # Baked-weights path: all links already made during creation.
+ # Native raycast path: all links already made inside
+ # create_native_raycast_visibility / create_native_feather.
+ pass
+ else:
+ # OSL path: connect subtract, normalize, script inputs, length
+ subtract = subtract_nodes[i]
+ normalize = normalize_nodes[i]
+ scripts = script_nodes[i]
+ add_camera_loc = add_camera_loc_nodes[i]
+ length = length_nodes[i]
+ camera_fov = camera_fov_nodes[i]
+ camera_aspect_ratio = camera_aspect_ratio_nodes[i]
+ camera_direction = camera_direction_nodes[i]
+ camera_up = camera_up_nodes[i]
+
+ links.new(geometry.outputs["Position"], subtract.inputs[0])
+ links.new(subtract.outputs["Vector"], normalize.inputs[0])
+
+ for script in scripts:
+ links.new(normalize.outputs["Vector"], script.inputs["Direction"])
+
+ if "Origin" in script.inputs:
+ links.new(add_camera_loc.outputs["Vector"], script.inputs["Origin"])
+
+ if "threshold" in script.inputs:
+ links.new(length.outputs["Value"], script.inputs["threshold"])
+
+ if "SurfaceNormal" in script.inputs:
+ links.new(geometry.outputs["Normal"], script.inputs["SurfaceNormal"])
+ links.new(camera_fov.outputs[0], script.inputs["CameraFOV"])
+ links.new(camera_aspect_ratio.outputs[0], script.inputs["CameraAspect"])
+ links.new(camera_direction.outputs[0], script.inputs["CameraDir"])
+ links.new(camera_up.outputs[0], script.inputs["CameraUp"])
+
+ links.new(subtract.outputs["Vector"], length.inputs[0])
+
+ # Add material index node (subtract node)
+ subtract_node = nodes.new("ShaderNodeMath")
+ subtract_node.operation = 'SUBTRACT'
+ subtract_node.inputs[0].default_value = mat_id
+ subtract_node.location = (-1000, 0)
+ return True
+
+
+def get_or_create_osl_text(filename):
+ """
+ Loads an OSL script into a Blender internal text block.
+ This ensures the script is embedded in the .blend file (portable).
+ """
+ filepath = os.path.join(os.path.dirname(__file__), filename)
+ if not os.path.exists(filepath):
+ print(f"[StableGen] Error: OSL script not found at {filepath}")
+ return None
+
+ # Check if text block exists
+ text = bpy.data.texts.get(filename)
+
+ try:
+ with open(filepath, 'r') as f:
+ content = f.read()
+
+ if text:
+ # Update content to match the current file on disk
+ text.clear()
+ text.write(content)
+ else:
+ text = bpy.data.texts.new(name=filename)
+ text.write(content)
+ except Exception as e:
+ print(f"[StableGen] Error reading OSL file {filepath}: {e}")
+ return None
+
+ return text
+
+
+def get_or_load_image(filepath, force_reload=False):
+ """
+ Prevents duplicate image datablocks by default.
+ If force_reload is True, it finds the existing datablock
+ and reloads it from the specified filepath.
+ """
+ if not filepath:
+ print("[StableGen] Error: No filepath provided to get_or_load_image.")
+ return None
+
+ filename = os.path.basename(filepath)
+ image = bpy.data.images.get(filename)
+
+ # Verify if the found image actually points to the requested file
+ if image:
+ # Normalize paths for comparison (handle // prefix and OS separators)
+ try:
+ # bpy.path.abspath resolves // relative paths to absolute paths
+ img_path = os.path.normpath(bpy.path.abspath(image.filepath))
+ req_path = os.path.normpath(bpy.path.abspath(filepath))
+
+ if img_path != req_path:
+ # Name collision: found an image with the same name but different path.
+ # This is NOT the image we want.
+ image = None
+
+ # Try to find if the correct image is already loaded under a different name
+ for img in bpy.data.images:
+ if img.filepath:
+ try:
+ if os.path.normpath(bpy.path.abspath(img.filepath)) == req_path:
+ image = img
+ break
+ except:
+ continue
+ except Exception as e:
+ print(f"[StableGen] Warning: Error comparing image paths: {e}")
+ image = None
+
+ if image and force_reload:
+ # Image exists, but we are forced to reload (overwrite).
+ try:
+ # IMPORTANT: Update the filepath property of the existing
+ # datablock to the new file path.
+ image.filepath = filepath
+
+ # Reload the image data from that path.
+ image.reload()
+ except RuntimeError as e:
+ # Reload can fail if the file isn't found, etc.
+ print(f"[StableGen] Reload failed for {filename}. Removing old datablock. Error: {e}")
+ # Remove the bad datablock so we can try loading it fresh.
+ bpy.data.images.remove(image)
+ image = None # Set to None to trigger the load block below
+
+ if image is None:
+ # Image does not exist in .data, or it failed to reload.
+ try:
+ image = bpy.data.images.load(filepath)
+ # Only set the name if it's a new datablock to avoid renaming existing ones
+ # Blender will handle naming collisions (e.g. .001) automatically
+ if not bpy.data.images.get(filename):
+ image.name = filename
+ except RuntimeError as e:
+ # Load can fail if the file isn't found.
+ print(f"[StableGen] Warning: Could not load image file: {filepath}. Error: {e}")
+ return None
+
+ return image
+
+
+def reinstate_compare_nodes(context, to_project, stop_id_mat_id_pairs):
+ """
+ Reinstates the 'LESS_THAN' compare nodes that were removed for sequential generation.
+ This will esentially revert given views to not-generated state for viewpoint regeneration.
+ """
+
+ for obj in to_project:
+ if not obj.active_material:
+ continue
+
+ mat = obj.active_material
+ if not mat.use_nodes:
+ continue
+
+ nodes = mat.node_tree.nodes
+ links = mat.node_tree.links
+
+ for stop_id, mat_id in stop_id_mat_id_pairs:
+ script_node = None
+ # Find the script node with the specific label
+ # 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"{stop_id}-{mat_id}":
+ script_node = node
+ break
+ # Also check Angle-prefixed labels
+ if (node.type == 'SCRIPT' or (node.type == 'MATH' and node.operation == 'MULTIPLY')) and node.label == f"Angle-{stop_id}-{mat_id}":
+ script_node = node
+ break
+
+ if not script_node:
+ continue
+
+ # Store links to disconnect and reconnect later
+ links_to_recreate = []
+ for link in list(script_node.outputs[0].links):
+ links_to_recreate.append((link.from_socket, link.to_socket))
+ links.remove(link)
+
+ # For each original connection, insert a 'LESS_THAN' node
+ for from_socket, to_socket in links_to_recreate:
+ # Create a new 'LESS_THAN' math node
+ less_than_node = nodes.new(type='ShaderNodeMath')
+ less_than_node.operation = 'LESS_THAN'
+ less_than_node.inputs[1].default_value = -1
+ # Position it between the script node and its original destination
+ less_than_node.location = (script_node.location.x + 200, script_node.location.y)
+
+ # Connect script_node -> less_than_node -> original destination
+ links.new(from_socket, less_than_node.inputs[0])
+ links.new(less_than_node.outputs[0], to_socket)
+
+
+# ── PBR Projection ────────────────────────────────────────────────────
diff --git a/stablegen/texturing/rendering.py b/stablegen/texturing/rendering.py
new file mode 100644
index 0000000..82e7f52
--- /dev/null
+++ b/stablegen/texturing/rendering.py
@@ -0,0 +1,2647 @@
+"""Mask processing, render exports, visibility, texture baking, and material helpers."""
+
+import os
+import math
+import bpy, bmesh # pylint: disable=import-error
+import numpy as np
+import mathutils
+from ..utils import get_file_path, get_dir_path, get_compositor_node_tree, configure_output_node_paths, get_eevee_engine_id, sg_modal_active, remove_empty_dirs
+from PIL import Image
+import cv2
+
+_ADDON_PKG = __package__.rsplit('.', 1)[0]
+
+
+def purge_orphans():
+ """
+ Purge unused datablocks (images, materials, node groups, etc).
+ Uses Blender's Outliner orphan purge.
+ """
+ try:
+ # Blender 3.x/4.x: call recursively a few times to fully purge
+ for _ in range(5):
+ result = bpy.ops.outliner.orphans_purge(do_recursive=True)
+ # If it reports "CANCELLED" or does nothing, we're done
+ if 'CANCELLED' in result:
+ break
+ except Exception as e:
+ print(f"[StableGen] Orphan purge failed: {e}")
+
+def apply_vignette_to_mask(mask_file_path, feather_width=0.15, gamma=1.0, blur=True):
+ """
+ Soften hard edges in a grayscale visibility mask.
+
+ Instead of only darkening a thin border at the image edges, this applies
+ a Gaussian blur whose radius is proportional to the image size. That means
+ *any* 0→1 transition in the mask (occlusion edges, camera frustum edges,
+ etc.) becomes a smooth ramp, which the shader can use for a soft blend.
+
+ feather_width: fraction of min(image_w, image_h) used as blur radius.
+ 0.0 = no blur, 0.5 = very soft edges.
+ gamma: optional gamma applied to the blurred mask (1.0 = none).
+ blur: whether to apply Gaussian blur.
+ """
+ log_prefix = "[StableGen] Vignette:"
+
+ # -------------------------------------------------------------------------
+ # Basic guards
+ # -------------------------------------------------------------------------
+ if feather_width <= 0.0:
+ return
+
+ if not isinstance(mask_file_path, str):
+ print(f"[StableGen] {log_prefix} mask_file_path must be a string, got {type(mask_file_path)}")
+ return
+
+ if not os.path.exists(mask_file_path):
+ print(f"[StableGen] {log_prefix} mask file not found: {mask_file_path}")
+ return
+
+ img = cv2.imread(mask_file_path, cv2.IMREAD_GRAYSCALE)
+ if img is None:
+ print(f"[StableGen] {log_prefix} failed to read mask: {mask_file_path}")
+ return
+
+ # -------------------------------------------------------------------------
+ # Blur-based feathering
+ # -------------------------------------------------------------------------
+ h, w = img.shape[:2]
+ base = img.astype(np.float32) / 255.0
+
+ # Radius as a fraction of the smallest dimension
+ min_dim = float(min(h, w))
+ radius = max(1.0, feather_width * min_dim)
+
+ # Kernel size must be odd and at least 3
+ ksize = int(max(3, int(radius) | 1))
+
+ if blur:
+ blurred = cv2.GaussianBlur(base, (ksize, ksize), 0)
+ else:
+ blurred = base
+
+ if gamma != 1.0:
+ blurred = np.power(blurred, gamma)
+
+ result = np.clip(blurred, 0.0, 1.0)
+ result_u8 = (result * 255.0).astype(np.uint8)
+ cv2.imwrite(mask_file_path, result_u8)
+
+ print(
+ f"{log_prefix} soft-edge blur applied to mask: {mask_file_path} "
+ f"(ksize={ksize}, fw={feather_width}, gamma={gamma}, blur={blur})"
+ )
+
+
+def create_edge_feathered_mask(mask_path, feather_width=30):
+ """Create an edge-feathered version of a visibility mask for projection blending.
+
+ Uses a distance transform on the binary mask so that:
+ - Interior pixels (far from any edge) → 1.0 (full new texture)
+ - Edge pixels (near visibility boundary) → ramp 0→1 over *feather_width* px
+ - Invisible pixels → 0.0 (keep original texture)
+
+ The result is saved next to the original with an ``_edgefeather`` suffix.
+ Returns the output path, or *None* on failure.
+ """
+ if not isinstance(mask_path, str) or not os.path.exists(mask_path):
+ print(f"[StableGen] Edge-feather: mask not found: {mask_path}")
+ return None
+
+ mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
+ if mask is None:
+ print(f"[StableGen] Edge-feather: failed to read mask: {mask_path}")
+ return None
+
+ # Threshold to hard binary (visible = white, invisible = black)
+ _, binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
+
+ # Distance transform: each white pixel → distance to nearest black pixel
+ dist = cv2.distanceTransform(binary, cv2.DIST_L2, 5)
+
+ # Normalise into [0, 1] ramp over feather_width pixels
+ feathered = np.clip(dist / max(feather_width, 1), 0.0, 1.0)
+
+ feathered_u8 = (feathered * 255).astype(np.uint8)
+
+ output_path = mask_path.replace('.png', '_edgefeather.png')
+ cv2.imwrite(output_path, feathered_u8)
+ print(f"[StableGen] Edge-feather mask saved: {output_path} (width={feather_width}px)")
+ return output_path
+
+
+def render_edge_feather_mask(context, to_export, camera, camera_index, feather_width=30, softness=1.0):
+ """Render a geometry silhouette from *camera* and apply distance-transform
+ edge feathering.
+
+ All target objects render as white (Emission), non-target mesh objects are
+ hidden, and the world is set to black. The resulting binary silhouette is
+ distance-transformed so that interior pixels = 1.0, boundary pixels ramp
+ 0→1 over *feather_width* pixels, and background = 0.0.
+
+ The final mask is saved to ``inpaint/visibility/render{camera_index}_edgefeather.png``.
+ Returns the output path, or *None* on failure.
+ """
+ output_dir = get_dir_path(context, "inpaint")["visibility"]
+ os.makedirs(output_dir, exist_ok=True)
+ raw_file = f"render{camera_index}_geomask"
+
+ # ── Save original state ─────────────────────────────────────────────────
+ original_camera = context.scene.camera
+ original_engine = context.scene.render.engine
+ original_transparent = context.scene.render.film_transparent
+ original_samples = context.scene.cycles.samples
+ original_view_transform = bpy.context.scene.view_settings.view_transform
+ original_use_compositing = context.scene.render.use_compositing
+ original_filepath = context.scene.render.filepath
+
+ world = context.scene.world
+ if not world:
+ world = bpy.data.worlds.new("World")
+ context.scene.world = world
+ original_use_nodes = world.use_nodes
+ original_color = world.color.copy()
+ original_bg_node_color = None
+ original_bg_node_strength = None
+ if world.use_nodes and world.node_tree:
+ for wn in world.node_tree.nodes:
+ if wn.type == 'BACKGROUND':
+ original_bg_node_color = tuple(wn.inputs["Color"].default_value)
+ original_bg_node_strength = wn.inputs["Strength"].default_value
+ break
+
+ # ── Camera ──────────────────────────────────────────────────────────────
+ 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)
+
+ # ── Hide non-target mesh objects ────────────────────────────────────────
+ hidden_restore = {}
+ for obj in context.scene.objects:
+ if obj.type == 'MESH' and obj not in to_export:
+ hidden_restore[obj] = obj.hide_render
+ obj.hide_render = True
+
+ # ── World → black background ────────────────────────────────────────────
+ if bpy.app.version >= (5, 0, 0):
+ world.use_nodes = True
+ if world.node_tree:
+ for wn in world.node_tree.nodes:
+ if wn.type == 'BACKGROUND':
+ wn.inputs["Color"].default_value = (0.0, 0.0, 0.0, 1.0)
+ wn.inputs["Strength"].default_value = 1.0
+ break
+ else:
+ world.color = (0, 0, 0)
+ world.use_nodes = False
+
+ # ── Render settings ─────────────────────────────────────────────────────
+ context.scene.render.engine = 'CYCLES'
+ context.scene.render.film_transparent = False
+ context.scene.cycles.samples = 1
+ bpy.context.scene.display_settings.display_device = 'sRGB'
+ bpy.context.scene.view_settings.view_transform = 'Raw'
+
+ view_layer = context.view_layer
+ view_layer.use_pass_emit = True
+ view_layer.use_pass_environment = True
+
+ # ── Compositor ──────────────────────────────────────────────────────────
+ context.scene.render.use_compositing = True
+ context.scene.use_nodes = True
+ 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')
+ except Exception:
+ mix_node = comp_nodes.new('ShaderNodeMixRGB')
+ mix_node.blend_type = 'ADD'
+ mix_node.inputs[0].default_value = 1
+ output_node = comp_nodes.new('CompositorNodeOutputFile')
+ configure_output_node_paths(output_node, output_dir, raw_file)
+
+ if bpy.app.version < (5, 0, 0):
+ comp_links.new(render_layers.outputs['Emit'], mix_node.inputs[1])
+ comp_links.new(render_layers.outputs['Env'], mix_node.inputs[2])
+ else:
+ comp_links.new(render_layers.outputs['Emission'], mix_node.inputs[1])
+ comp_links.new(render_layers.outputs['Environment'], mix_node.inputs[2])
+ comp_links.new(mix_node.outputs[0], output_node.inputs[0])
+
+ # ── Render ──────────────────────────────────────────────────────────────
+ bpy.ops.render.render(write_still=True)
+
+ # ── Determine actual output path ────────────────────────────────────────
+ 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
+
+ # ── Restore render / world settings ─────────────────────────────────────
+ context.scene.camera = original_camera
+ context.scene.render.engine = original_engine
+ context.scene.render.film_transparent = original_transparent
+ context.scene.cycles.samples = original_samples
+ bpy.context.scene.view_settings.view_transform = original_view_transform
+ context.scene.render.use_compositing = original_use_compositing
+ context.scene.render.filepath = original_filepath
+ world.use_nodes = original_use_nodes
+ world.color = original_color
+ if original_bg_node_color is not None and world.node_tree:
+ for wn in world.node_tree.nodes:
+ if wn.type == 'BACKGROUND':
+ wn.inputs["Color"].default_value = original_bg_node_color
+ wn.inputs["Strength"].default_value = original_bg_node_strength
+ break
+
+ # ── Distance-transform edge feathering ──────────────────────────────────
+ if not os.path.exists(raw_path):
+ print(f"[StableGen] Edge-feather: raw mask not found after render: {raw_path}")
+ return None
+
+ mask_img = cv2.imread(raw_path, cv2.IMREAD_GRAYSCALE)
+ if mask_img is None:
+ print(f"[StableGen] Edge-feather: failed to read raw mask: {raw_path}")
+ return None
+
+ _, binary = cv2.threshold(mask_img, 127, 255, cv2.THRESH_BINARY)
+ dist = cv2.distanceTransform(binary, cv2.DIST_L2, 5)
+ linear = np.clip(dist / max(feather_width, 1), 0.0, 1.0).astype(np.float32)
+
+ # Gaussian-blur the linear ramp to round off the kinks at both ends.
+ # The linear ramp has sharp slope discontinuities at dist=0 (edge) and
+ # dist=feather_width (interior plateau). A small Gaussian blur smooths
+ # these transitions without shifting the ramp position or compressing the
+ # transition zone the way smoothstep does.
+ # softness == 0 → raw linear ramp (kinks preserved)
+ # softness == 1 → moderate rounding (sigma ≈ 25% of feather width)
+ # softness > 1 → stronger rounding / wider smooth zone
+ if softness > 0.01:
+ blur_sigma = softness * feather_width * 0.25
+ feathered = cv2.GaussianBlur(linear, (0, 0), sigmaX=blur_sigma)
+ feathered = np.clip(feathered, 0.0, 1.0)
+ else:
+ feathered = linear
+
+ feathered_u8 = (np.clip(feathered, 0.0, 1.0) * 255).astype(np.uint8)
+
+ ef_path = os.path.join(output_dir, f"render{camera_index}_edgefeather.png")
+ cv2.imwrite(ef_path, feathered_u8)
+
+ # Clean up raw mask
+ try:
+ os.remove(raw_path)
+ except OSError:
+ pass
+
+ print(f"[StableGen] Edge-feather mask saved: {ef_path} (width={feather_width}px, softness={softness:.2f})")
+ return ef_path
+
+
+def apply_uv_inpaint_texture(context, obj, baked_image_path):
+ """
+ Apply a UV inpainted/baked texture to the active material.
+
+ Priority:
+ 1) StableGen projection chain: replace LAST MixRGB with "Projection" in name (Color2).
+ 2) Fallback: traverse from Material Output and find a suitable MIX_RGB (Color2 unlinked
+ or linked from TEX_IMAGE), then replace that (Color2).
+
+ In both cases:
+ - Insert TexImage + UVMap (first non-ProjectionUV layer) feeding Color2.
+ - Remove existing Color2 links.
+ - If replacing a projection input, also remove the old TexImage node and orphaned image.
+ """
+
+ mat = obj.active_material
+ if not mat or not mat.use_nodes or not mat.node_tree:
+ print("[StableGen] No active material or no node tree.")
+ return
+
+ nodes = mat.node_tree.nodes
+ links = mat.node_tree.links
+
+ # -----------------------------
+ # Load baked image
+ # -----------------------------
+ try:
+ img = bpy.data.images.load(baked_image_path)
+ except Exception as e:
+ print("[StableGen] Failed to load baked image", baked_image_path, e)
+ return
+
+ # -----------------------------
+ # Helpers
+ # -----------------------------
+ def pick_uv_name():
+ uv_layers = getattr(obj.data, "uv_layers", None)
+ if not uv_layers or len(uv_layers) == 0:
+ return None
+ for uv_layer in uv_layers:
+ if not uv_layer.name.startswith("ProjectionUV") and uv_layer.name != "_SG_ProjectionBuffer":
+ return uv_layer.name
+ return uv_layers.active.name
+
+ def clear_color2_links(mix_node, cleanup_teximage=False):
+ """Remove links to Color2. Optionally remove upstream TexImage nodes & orphaned images."""
+ for link in list(mix_node.inputs["Color2"].links):
+ src = link.from_node
+ links.remove(link)
+
+ if cleanup_teximage and src and src.type == "TEX_IMAGE":
+ old_img = src.image
+ try:
+ nodes.remove(src)
+ except Exception:
+ pass
+ if old_img and old_img.users == 0:
+ try:
+ bpy.data.images.remove(old_img)
+ except Exception:
+ pass
+
+ def inject_tex_uv_into_color2(mix_node):
+ """Create TexImage+UVMap and connect into mix_node Color2."""
+ tex = nodes.new("ShaderNodeTexImage")
+ tex.image = img
+ tex.label = "BakedProjection"
+ tex.location = (mix_node.location[0] - 300, mix_node.location[1])
+
+ uv = nodes.new("ShaderNodeUVMap")
+ uv_name = pick_uv_name()
+ if uv_name:
+ uv.uv_map = uv_name
+ uv.location = (tex.location[0] - 300, tex.location[1] - 200)
+
+ links.new(uv.outputs["UV"], tex.inputs["Vector"])
+ links.new(tex.outputs["Color"], mix_node.inputs["Color2"])
+
+ # ============================================================
+ # 1) StableGen projection chain path
+ # ============================================================
+ proj_mix_nodes = [n for n in nodes if n.type == "MIX_RGB" and "Projection" in n.name]
+ if proj_mix_nodes:
+ mix_node = sorted(proj_mix_nodes, key=lambda n: n.name)[-1]
+
+ # Remove previous projection input, clean up old TexImage nodes
+ clear_color2_links(mix_node, cleanup_teximage=True)
+
+ # Inject baked texture
+ inject_tex_uv_into_color2(mix_node)
+
+ print(f"[StableGen] Injected baked projection into projection chain ({mix_node.name}) on {obj.name}")
+ return
+
+ # ============================================================
+ # 2) Fallback traversal path (your original logic)
+ # ============================================================
+ output_node = next((n for n in nodes if n.type == "OUTPUT_MATERIAL"), None)
+ if not output_node or not output_node.inputs["Surface"].links:
+ print("[StableGen] No Material Output surface link found for fallback.")
+ return
+
+ before_output = output_node.inputs["Surface"].links[0].from_node
+
+ if before_output.type == "BSDF_PRINCIPLED":
+ # Follow: Output.Surface -> Principled -> Base Color -> upstream node
+ base_color = before_output.inputs.get("Base Color")
+ if base_color and base_color.links:
+ current_node = base_color.links[0].from_node
+ else:
+ current_node = None
+ else:
+ current_node = before_output
+
+ mix_node = None
+ visited = set()
+
+ while current_node and current_node.as_pointer() not in visited:
+ visited.add(current_node.as_pointer())
+
+ if current_node.type == "MIX_RGB":
+ c2 = current_node.inputs.get("Color2")
+ if c2:
+ if (not c2.is_linked) or (c2.is_linked and c2.links and c2.links[0].from_node.type == "TEX_IMAGE"):
+ mix_node = current_node
+ break
+
+ c2 = current_node.inputs.get("Color2")
+ if c2 and c2.links:
+ current_node = c2.links[0].from_node
+ else:
+ current_node = None
+
+ if not mix_node:
+ print("[StableGen] No suitable fallback MixRGB node found.")
+ return
+
+ # Remove any existing links on Color2 (fallback does NOT aggressively delete nodes/images)
+ clear_color2_links(mix_node, cleanup_teximage=False)
+
+ # Insert baked texture + UVMap
+ inject_tex_uv_into_color2(mix_node)
+
+ print(f"[StableGen] Injected baked projection into fallback chain ({mix_node.name}) on {obj.name}")
+
+
+def flatten_projection_material_for_refine(context, obj, baked_image_path):
+ """
+ Replace the StableGen ProjectionMaterial on this object with a minimal
+ baked-base material that still matches the expectations of:
+ - export_emit_image / _setup_emit_material
+ - project_image local_edit logic
+
+ Final graph:
+ UV Map -> Baked Image -> MixRGB -> Principled BSDF -> Output
+ """
+ import os
+ try:
+ img = bpy.data.images.load(baked_image_path)
+ except Exception as e:
+ print(f"[StableGen] Failed to load baked image for {obj.name}: {baked_image_path} ({e})")
+ return
+
+ # -------------------------------------------------------------------------
+ # 1) Find or create a suitable material
+ # -------------------------------------------------------------------------
+ target_mat = None
+ for slot in obj.material_slots:
+ mat = slot.material
+ if mat and mat.name.startswith("ProjectionMaterial"):
+ target_mat = mat
+ break
+
+ if target_mat is None:
+ target_mat = obj.active_material
+
+ if target_mat is None:
+ target_mat = bpy.data.materials.new(name="ProjectionMaterial")
+ obj.data.materials.append(target_mat)
+
+ obj.active_material = target_mat
+ if obj.active_material_index < 0:
+ if target_mat not in obj.data.materials:
+ obj.data.materials.append(target_mat)
+ obj.active_material_index = obj.material_slots.find(target_mat.name)
+
+ # -------------------------------------------------------------------------
+ # 2) Build: UV -> Tex -> MixRGB -> Principled -> Output
+ # -------------------------------------------------------------------------
+ target_mat.use_nodes = True
+ nodes = target_mat.node_tree.nodes
+ links = target_mat.node_tree.links
+
+ # Clear old nodes
+ for node in list(nodes):
+ nodes.remove(node)
+
+ # Output
+ output_node = nodes.new("ShaderNodeOutputMaterial")
+ output_node.location = (800, 0)
+
+ # Principled
+ principled_node = nodes.new("ShaderNodeBsdfPrincipled")
+ principled_node.location = (500, 0)
+ principled_node.inputs["Roughness"].default_value = 1.0
+
+ # MixRGB that _setup_emit_material expects to sit before the Principled
+ mix_node = nodes.new("ShaderNodeMixRGB")
+ mix_node.location = (200, 0)
+ mix_node.use_clamp = True
+ # Fac=0 -> output = Color1 (baked tex)
+ mix_node.inputs["Fac"].default_value = 0.0
+
+ # Baked texture
+ tex_node = nodes.new("ShaderNodeTexImage")
+ tex_node.location = (-100, 0)
+ tex_node.image = img
+
+ # UV map
+ uv_node = nodes.new("ShaderNodeUVMap")
+ uv_node.location = (-400, -150)
+
+ # -------------------------------------------------------------------------
+ # 3) Choose a stable UV map (avoid ProjectionUV)
+ # -------------------------------------------------------------------------
+ uv_name = None
+ for uv in obj.data.uv_layers:
+ if not uv.name.startswith("ProjectionUV") and uv.name != "_SG_ProjectionBuffer":
+ uv_name = uv.name
+ break
+
+ if not uv_name:
+ uv_names = [uv.name for uv in obj.data.uv_layers]
+ if "BakeUV" in uv_names:
+ uv_name = "BakeUV"
+ else:
+ uv_layer = obj.data.uv_layers.new(name="BakeUV")
+ uv_name = uv_layer.name
+
+ uv_node.uv_map = uv_name
+
+ # -------------------------------------------------------------------------
+ # 4) Wire up the graph
+ # -------------------------------------------------------------------------
+ links.new(uv_node.outputs["UV"], tex_node.inputs["Vector"])
+ links.new(tex_node.outputs["Color"], mix_node.inputs["Color1"])
+
+ links.new(mix_node.outputs["Color"], principled_node.inputs["Base Color"])
+
+ # Drive emission too (Blender 4.x uses "Emission Color")
+ if "Emission Color" in principled_node.inputs:
+ links.new(mix_node.outputs["Color"], principled_node.inputs["Emission Color"])
+ elif "Emission" in principled_node.inputs:
+ links.new(mix_node.outputs["Color"], principled_node.inputs["Emission"])
+
+ if "Emission Strength" in principled_node.inputs:
+ principled_node.inputs["Emission Strength"].default_value = 1.0
+
+ links.new(principled_node.outputs["BSDF"], output_node.inputs["Surface"])
+
+
+ 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)):
+ """
+ Exports a emit-only render of the scene from a camera's perspective.
+ :param context: Blender context.
+ :param camera_id: ID of the camera.
+ :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 = {}
+ original_active_material = {}
+ temporary_materials = {}
+
+ # Check if there is BSDF applied
+
+ # We need to temporarily disconnect BDSF nodes and connect their inputs directly to the output
+
+ for obj in to_export:
+ # Store original materials
+ original_materials[obj] = list(obj.data.materials)
+ original_active_material[obj] = obj.active_material
+
+ # Copy active material and switch to it
+ mat = obj.active_material
+ if not mat:
+ continue
+ mat_copy = mat.copy()
+
+ # Clear materials and assign temp material
+ obj.data.materials.clear()
+ obj.data.materials.append(mat_copy)
+
+ # Store the temporary material for later deletion
+ temporary_materials[obj] = mat_copy
+
+ # Enable use of nodes
+ mat_copy.use_nodes = True
+ nodes = mat_copy.node_tree.nodes
+ links = mat_copy.node_tree.links
+
+ # Find the output node
+ output = None
+ for node in nodes:
+ if node.type == 'OUTPUT_MATERIAL':
+ output = node
+ break
+
+ if not output or not output.inputs[0].links:
+ continue
+
+ # Check the type of the node which connects to output
+ before_output = output.inputs[0].links[0].from_node
+ if before_output.type == 'BSDF_PRINCIPLED':
+ # Find the last color mix node
+ color_mix = output.inputs[0].links[0].from_node.inputs[0].links[0].from_node
+ # Set color 2 to fallback color
+ if not "visibility" in str(camera_id):
+ color_mix.inputs["Color2"].default_value = (fallback_color[0], fallback_color[1], fallback_color[2], 1.0)
+ else:
+ # Already a color mix node
+ color_mix = before_output
+ # Set color 2 to fallback color
+ if not "visibility" in str(camera_id):
+ color_mix.inputs["Color2"].default_value = (fallback_color[0], fallback_color[1], fallback_color[2], 1.0)
+
+ # Blender 5.0+: Wrap color output with Emission shader so the Emit pass picks it up
+ if bpy.app.version >= (5, 0, 0):
+ emission_node = nodes.new("ShaderNodeEmission")
+ emission_node.location = (output.location.x - 200, output.location.y)
+ links.new(color_mix.outputs[0], emission_node.inputs["Color"])
+ links.new(emission_node.outputs[0], output.inputs["Surface"])
+ continue
+
+ # Find the last color mix node
+ color_mix = output.inputs[0].links[0].from_node.inputs[0].links[0].from_node
+ # Connect the color mix node directly to the output
+ if bpy.app.version >= (5, 0, 0):
+ # Blender 5.0+: Wrap in Emission shader for Emit pass
+ emission_node = nodes.new("ShaderNodeEmission")
+ emission_node.location = (output.location.x - 200, output.location.y)
+ links.new(color_mix.outputs[0], emission_node.inputs["Color"])
+ links.new(emission_node.outputs[0], output.inputs["Surface"])
+ else:
+ links.new(color_mix.outputs[0], output.inputs[0])
+
+ output_dir = get_dir_path(context, "inpaint")["visibility"] if "visibility" in str(camera_id) else get_dir_path(context, "inpaint")["render"]
+ output_file = f"ctx_render{camera_id}" if camera_id is not None else "ctx_render"
+
+ # Store and set world settings
+ world = context.scene.world
+ if not world:
+ world = bpy.data.worlds.new("World")
+ context.scene.world = world
+
+ # Store original settings
+ original_engine = context.scene.render.engine
+ original_film_transparent = context.scene.render.film_transparent
+ original_use_compositing = context.scene.render.use_compositing
+ original_filepath = context.scene.render.filepath
+ original_use_nodes = world.use_nodes
+ original_color = world.color.copy()
+ original_bg_node_color = None
+ original_bg_node_strength = None
+ if world.use_nodes and world.node_tree:
+ for wn in world.node_tree.nodes:
+ if wn.type == 'BACKGROUND':
+ original_bg_node_color = tuple(wn.inputs["Color"].default_value)
+ original_bg_node_strength = wn.inputs["Strength"].default_value
+ break
+
+ # Set world background color
+ if bpy.app.version >= (5, 0, 0):
+ # Blender 5.0+: Use shader nodes for world background (required for Environment pass)
+ world.use_nodes = True
+ if world.node_tree:
+ for wn in world.node_tree.nodes:
+ if wn.type == 'BACKGROUND':
+ wn.inputs["Color"].default_value = (*bg_color[:3], 1.0)
+ wn.inputs["Strength"].default_value = 1.0
+ break
+ else:
+ world.color = bg_color
+ world.use_nodes = False
+
+ # Switch to CYCLES render engine (needed for emission pass rendering)
+ context.scene.render.engine = 'CYCLES'
+ # Force CPU + OSL only for Blender < 5.1 (native Raycast nodes don't need it)
+ if bpy.app.version < (5, 1, 0):
+ if hasattr(context.scene.cycles, 'shading_system'):
+ context.scene.cycles.shading_system = True
+ else:
+ context.scene.cycles.use_osl = True
+ context.scene.cycles.device = 'CPU'
+ context.scene.render.film_transparent = False
+ # Change color management to standard
+ bpy.context.scene.display_settings.display_device = 'sRGB'
+ bpy.context.scene.view_settings.view_transform = view_transform
+ context.scene.cycles.samples = 1 # Minimum samples for speed
+ # Configure view layer settings for diffuse-only
+ view_layer = context.view_layer
+ view_layer.use_pass_diffuse_color = False
+ view_layer.use_pass_diffuse_direct = False
+ view_layer.use_pass_diffuse_indirect = False
+
+ # Disable all other passes
+ view_layer.use_pass_ambient_occlusion = False
+ view_layer.use_pass_shadow = False
+ view_layer.use_pass_emit = True
+ view_layer.use_pass_environment = True
+
+
+ # Set up compositor nodes
+ context.scene.render.use_compositing = True
+ context.scene.use_nodes = True
+ node_tree = get_compositor_node_tree(context.scene)
+ nodes = node_tree.nodes
+ links = node_tree.links
+
+ # Clear existing nodes
+ nodes.clear()
+
+ # Create nodes
+ render_layers = nodes.new('CompositorNodeRLayers')
+ try:
+ mix_node = nodes.new('CompositorNodeMixRGB')
+ except:
+ mix_node = nodes.new('ShaderNodeMixRGB')
+ mix_node.blend_type = 'ADD'
+ mix_node.inputs[0].default_value = 1
+ output_node = nodes.new('CompositorNodeOutputFile')
+ configure_output_node_paths(output_node, output_dir, output_file)
+
+ # Connect emission to output
+ # Blender 5.0+ renamed pass names: Emit -> Emission, Env -> Environment
+ if bpy.app.version < (5, 0, 0):
+ links.new(render_layers.outputs['Emit'], mix_node.inputs[1])
+ links.new(render_layers.outputs['Env'], mix_node.inputs[2])
+ else:
+ links.new(render_layers.outputs['Emission'], mix_node.inputs[1])
+ links.new(render_layers.outputs['Environment'], mix_node.inputs[2])
+ links.new(mix_node.outputs[0], output_node.inputs[0])
+
+ # Render
+ bpy.ops.render.render(write_still=True)
+
+ # Post-processing for visibility masks
+ if "visibility" in str(camera_id):
+ # Determine the actual file path (Blender 4.x appends 0001, 5.x does not)
+ if bpy.app.version >= (5, 0, 0):
+ final_path = os.path.join(output_dir, f"{output_file}.png")
+ else:
+ final_path = os.path.join(output_dir, f"{output_file}0001.png")
+
+ if context.scene.visibility_vignette and (context.scene.generation_method == 'local_edit' or (context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method == 'local_edit')):
+ # Smooth edge feathering, no blocky mask
+ apply_vignette_to_mask(
+ final_path,
+ feather_width=context.scene.visibility_vignette_width,
+ gamma=1.0,
+ blur=context.scene.visibility_vignette_blur
+ )
+ elif context.scene.mask_blocky:
+ # Only do blocky mask if vignette is OFF
+ expanded_mask = expand_mask_to_blocks(final_path, block_size=8)
+ if expanded_mask is not None:
+ expanded_mask_u8 = (expanded_mask * 255).astype(np.uint8)
+ cv2.imwrite(final_path, expanded_mask_u8)
+
+ # Restore original settings
+ context.scene.render.engine = original_engine
+ context.scene.render.film_transparent = original_film_transparent
+ context.scene.render.use_compositing = original_use_compositing
+ context.scene.render.filepath = original_filepath
+ world.use_nodes = original_use_nodes
+ world.color = original_color
+ if original_bg_node_color is not None and world.node_tree:
+ for wn in world.node_tree.nodes:
+ if wn.type == 'BACKGROUND':
+ wn.inputs["Color"].default_value = original_bg_node_color
+ wn.inputs["Strength"].default_value = original_bg_node_strength
+ break
+ bpy.context.scene.view_settings.view_transform = 'Standard'
+
+ # Restore original materials
+ for obj, materials in original_materials.items():
+ obj.data.materials.clear()
+ # First append the original active material
+ if original_active_material[obj]:
+ obj.data.materials.append(original_active_material[obj])
+ for mat in materials:
+ if mat != original_active_material[obj]:
+ obj.data.materials.append(mat)
+
+ # Clean up temporary materials
+ for _, temp_mat in temporary_materials.items():
+ if temp_mat and temp_mat.name in bpy.data.materials:
+ bpy.data.materials.remove(temp_mat)
+
+ 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):
+ """
+ Renders the scene from a camera's perspective using Workbench.
+ Creates temporary materials for consistent rendering.
+ :param context: Blender context.
+ :param camera_id: ID of the camera for the output filename.
+ :param output_dir: Optional output directory.
+ :param filename: Optional filename (without component/frame suffix).
+ :return: None
+ """
+ print("[StableGen] Exporting render using Workbench")
+
+ # Store original materials and create temporary ones
+ original_materials = {}
+ original_active_material = {}
+ for obj in context.view_layer.objects:
+ if obj.type == 'MESH':
+ # Store original materials
+ original_materials[obj] = list(obj.data.materials)
+ original_active_material[obj] = obj.active_material
+
+ # Create temporary material
+ temp_mat = bpy.data.materials.new(name="TempRenderMaterial")
+ 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")
+
+ if filename is None:
+ output_file = f"render{camera_id}" if camera_id is not None else "render"
+ else:
+ output_file = filename
+
+ # Store original render settings
+ original_engine = context.scene.render.engine
+ original_workbench_settings = {
+ 'lighting': context.scene.display.shading.light,
+ 'color_type': context.scene.display.shading.color_type
+ }
+ original_render_filepath = context.scene.render.filepath
+ original_image_settings = context.scene.render.image_settings.file_format
+ original_use_compositing = context.scene.render.use_compositing
+
+ # Switch to WORKBENCH render engine and configure settings
+ context.scene.render.engine = 'BLENDER_WORKBENCH'
+ # Configure Workbench for a flat, consistent look if needed
+ context.scene.display.shading.light = 'STUDIO'
+ context.scene.display.shading.color_type = 'SINGLE'
+
+ render_layer = context.view_layer
+ original_combined = render_layer.use_pass_combined
+
+ # Enable combined pass for Workbench
+ render_layer.use_pass_combined = True
+
+ # Set up output nodes (Compositor setup remains the same)
+ context.scene.render.use_compositing = True
+ context.scene.use_nodes = True
+ 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)
+ links.new(render_layers.outputs['Image'], output_node.inputs[0])
+
+ # Render
+ bpy.ops.render.render(write_still=True)
+
+ # Restore original materials
+ for obj, materials in original_materials.items():
+ obj.data.materials.clear()
+ # First append the original active material
+ if original_active_material[obj]:
+ obj.data.materials.append(original_active_material[obj])
+ for mat in materials:
+ if mat != original_active_material[obj]:
+ obj.data.materials.append(mat)
+
+ # Restore original render settings
+ render_layer.use_pass_combined = original_combined
+
+ # Clean up temporary materials
+ # Use a while loop to safely remove materials while iterating
+ temp_mats = [m for m in bpy.data.materials if m.name.startswith("TempRenderMaterial")]
+ for mat in temp_mats:
+ bpy.data.materials.remove(mat)
+
+
+ # Restore original render settings
+ context.scene.render.engine = original_engine
+ context.scene.display.shading.light = original_workbench_settings['lighting']
+ context.scene.display.shading.color_type = original_workbench_settings['color_type']
+ context.scene.render.filepath = original_render_filepath
+ context.scene.render.image_settings.file_format = original_image_settings
+ 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_viewport(context, camera_id=None, output_dir=None, filename=None):
+ """
+ Renders the scene using viewport OpenGL render to include overlays.
+ :param context: Blender context.
+ :param camera_id: ID of the camera for the output filename.
+ :param output_dir: Optional output directory.
+ :param filename: Optional filename (without component/frame suffix).
+ :return: None
+ """
+ print("[StableGen] Exporting render using Viewport (OpenGL)")
+
+ # Setup output path
+ if output_dir is None:
+ output_dir = get_dir_path(context, "misc")
+
+ if filename is None:
+ output_file = f"render{camera_id}" if camera_id is not None else "render"
+ else:
+ output_file = filename
+
+ # Store original render settings
+ original_engine = context.scene.render.engine
+ original_render_filepath = context.scene.render.filepath
+ original_image_settings = context.scene.render.image_settings.file_format
+
+ # Switch to WORKBENCH render engine for consistent viewport shading
+ context.scene.render.engine = 'BLENDER_WORKBENCH'
+
+ # Find a viewport
+ viewport_area = None
+ viewport_region = None
+ viewport_space = None
+ viewport_region_3d = None
+ viewport_window = None
+
+ for window in context.window_manager.windows:
+ for area in window.screen.areas:
+ if area.type == 'VIEW_3D':
+ viewport_area = area
+ viewport_window = window
+ for region in area.regions:
+ if region.type == 'WINDOW':
+ viewport_region = region
+ break
+ viewport_space = area.spaces.active
+ viewport_region_3d = viewport_space.region_3d if viewport_space else None
+ break
+ if viewport_area:
+ break
+
+ if not (viewport_area and viewport_region and viewport_space and viewport_region_3d and viewport_window):
+ print("[StableGen] Viewport render failed: no VIEW_3D area found.")
+ context.scene.render.engine = original_engine
+ return
+
+ # Store viewport settings
+ original_view_perspective = viewport_region_3d.view_perspective
+ original_shading_type = viewport_space.shading.type
+ original_overlay_show = viewport_space.overlay.show_overlays
+
+ # Configure viewport for camera render with overlays
+ viewport_region_3d.view_perspective = 'CAMERA'
+ viewport_space.shading.type = 'RENDERED'
+ viewport_space.overlay.show_overlays = True
+
+ # Configure output filepath
+ context.scene.render.filepath = os.path.join(output_dir, f"{output_file}.png")
+ context.scene.render.image_settings.file_format = 'PNG'
+
+ override = {
+ 'window': viewport_window,
+ 'screen': viewport_window.screen,
+ 'area': viewport_area,
+ 'region': viewport_region,
+ 'scene': context.scene,
+ 'space_data': viewport_space,
+ 'region_data': viewport_region_3d,
+ }
+ with bpy.context.temp_override(**override):
+ bpy.ops.render.opengl(write_still=True, view_context=True)
+
+ # Restore viewport settings
+ viewport_region_3d.view_perspective = original_view_perspective
+ viewport_space.shading.type = original_shading_type
+ viewport_space.overlay.show_overlays = original_overlay_show
+
+ # Restore original render settings
+ context.scene.render.engine = original_engine
+ context.scene.render.filepath = original_render_filepath
+ context.scene.render.image_settings.file_format = original_image_settings
+
+ print(f"[StableGen] Viewport render saved to: {os.path.join(output_dir, output_file)}.png")
+
+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.
+ :param context: Blender context.
+ :param camera_id: ID of the camera for the output filename.
+ :param low_threshold: Low threshold for edge detection.
+ :param high_threshold: High threshold for edge detection.
+ :return: None
+ """
+ # Render the scene
+ export_render(context, camera_id)
+
+ # Load the rendered image
+ output_dir_render = get_dir_path(context, "misc")
+ output_dir_canny = get_dir_path(context, "controlnet")["canny"]
+ # Blender 4.x appends 0001 frame suffix, 5.x does not
+ frame_suffix = "0001" if bpy.app.version < (5, 0, 0) else ""
+ output_file = f"render{camera_id}{frame_suffix}" if camera_id is not None else "render"
+ image_path = os.path.join(output_dir_render, f"{output_file}.png")
+ image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
+
+ # Apply Canny edge detection
+ edges = cv2.Canny(image, low_threshold, high_threshold)
+
+ # Save the edge detection image
+ output_file = f"canny{camera_id}{frame_suffix}" if camera_id is not None else "canny"
+ cv2.imwrite(os.path.join(output_dir_canny, f"{output_file}.png"), edges)
+
+ print(f"[StableGen] Canny edge detection saved to: {os.path.join(output_dir_canny, output_file)}.png")
+
+
+def expand_mask_to_blocks(mask_file_path, block_size=8):
+ """
+ Loads a mask image from a file path and processes it so that any
+ block_size x block_size grid cell containing any non-black pixel (value > 0)
+ becomes fully white (1.0).
+
+ Args:
+ mask_file_path (str): The path to the mask image file.
+ block_size (int): The size of the grid blocks (default: 8).
+
+ Returns:
+ np.ndarray | None: The processed mask as a NumPy array (float32, normalized [0, 1]).
+ Returns None if the file cannot be loaded or processed.
+ """
+ if not isinstance(mask_file_path, str):
+ print(f"[StableGen] Error: mask_file_path must be a string. Got: {type(mask_file_path)}")
+ return None
+ if not os.path.exists(mask_file_path):
+ print(f"[StableGen] Error: Mask file not found at {mask_file_path}")
+ return None
+ if not os.path.isfile(mask_file_path):
+ print(f"[StableGen] Error: Path provided is not a file: {mask_file_path}")
+ return None
+
+ try:
+ # Load the image using Pillow
+ with Image.open(mask_file_path) as img:
+ # Convert to grayscale ('L') which typically represents intensity
+ mask_pil = img.convert("L")
+ # Convert PIL Image to numpy array
+ mask_array = np.array(mask_pil)
+
+ # --- Normalize mask to float32 [0, 1] ---
+ # Ensures comparison with 0.0 is reliable regardless of original bit depth
+ if mask_array.dtype == np.uint8:
+ mask_array = mask_array.astype(np.float32) / 255.0
+ elif np.issubdtype(mask_array.dtype, np.integer):
+ max_val = np.iinfo(mask_array.dtype).max
+ mask_array = mask_array.astype(np.float32) / max_val if max_val > 0 else mask_array.astype(np.float32)
+ elif not np.issubdtype(mask_array.dtype, np.floating):
+ print(f"[StableGen] Warning: Unsupported mask dtype {mask_array.dtype} after loading. Trying to convert.")
+ mask_array = mask_array.astype(np.float32) # Attempt conversion
+ # Ensure it's precisely within [0, 1] after potential float conversion
+ mask_array = np.clip(mask_array, 0.0, 1.0)
+ # --- End normalization ---
+
+ max_value = 1.0 # Output will be normalized float [0, 1]
+
+ height, width = mask_array.shape
+ # Create output mask initialized to zeros (black), ensure float32 type
+ output_mask = np.zeros_like(mask_array, dtype=np.float32)
+
+ # --- Core block processing logic ---
+ for y in range(0, height, block_size):
+ for x in range(0, width, block_size):
+ # Define block boundaries, handle image edges
+ y_start = y
+ y_end = min(y + block_size, height)
+ x_start = x
+ x_end = min(x + block_size, width)
+
+ # Extract the current block
+ block = mask_array[y_start:y_end, x_start:x_end]
+
+ # Check if *any* pixel value in the block is greater than 0.0
+ if np.any(block > 0.0):
+ # If yes, set the corresponding block in the output mask to 1.0 (white)
+ output_mask[y_start:y_end, x_start:x_end] = max_value
+ # --- End of core logic ---
+
+ return output_mask
+
+ except FileNotFoundError:
+ print(f"[StableGen] Error: Mask file not found at {mask_file_path}")
+ return None
+ except Exception as e:
+ print(f"[StableGen] Error loading or processing mask file {mask_file_path}: {e}")
+ return None
+
+
+def export_visibility(context, to_export, obj=None, camera_visibility=None, prepare_only=False):
+ """
+ Exports the visibility of the mesh by temporarily altering the shading nodes.
+ :param context: Blender context.
+ :param filepath: Path to the output file.
+ :param obj: Blender object.
+ :param camera_visibility: Camera object for visibility calculation.
+ :param prepare_only: If True, only prepare the visibility material without
+ baking/rendering and without restoring the original materials.
+ Used by debug tools to inspect the material in the viewport.
+ :return: None
+ """
+ # Store original materials and create temporary ones
+ original_materials = {}
+ original_active_material = {}
+ temporary_materials = {}
+
+ def prepare_material(obj):
+ mat = obj.active_material
+ if not mat:
+ return False
+
+ # Store original materials
+ original_materials[obj] = list(obj.data.materials)
+ original_active_material[obj] = obj.active_material # Store original active material
+
+ # Store original active mat
+
+ # Copy active material and switch to it
+ mat = obj.active_material
+ if not mat:
+ return False
+ mat_copy = mat.copy()
+
+ # Clear materials and assign temp material
+ obj.data.materials.clear()
+ obj.data.materials.append(mat_copy)
+
+ # Store temporary material for later deletion
+ temporary_materials[obj] = mat_copy
+
+ # Enable use of nodes
+ mat_copy.use_nodes = True
+ nodes = mat_copy.node_tree.nodes
+ links = mat_copy.node_tree.links
+
+ # Find the output node
+ output = None
+ for node in nodes:
+ if node.type == 'OUTPUT_MATERIAL':
+ output = node
+ break
+
+ if not output:
+ return False
+
+ if not output.inputs[0].links:
+ return False
+
+ # 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':
+ principled = output.inputs[0].links[0].from_node
+ if not principled.inputs[0].links:
+ # Principled BSDF exists but nothing is connected to its
+ # Base Color – this is not a projection material.
+ return False
+ color_mix = principled.inputs[0].links[0].from_node
+ input = principled.inputs[0]
+ else:
+ color_mix = output.inputs[0].links[0].from_node
+ input = output.inputs[0]
+ # Add equal node between color mix and bsdf
+ equal = nodes.new("ShaderNodeMath")
+
+ # Use compare operation to filter to only 1 or 0
+
+ if context.scene.generation_method == 'sequential' and context.scene.sequential_smooth:
+ # Add color ramp node
+ compare = nodes.new("ShaderNodeValToRGB")
+ compare.color_ramp.interpolation = 'LINEAR'
+ compare.color_ramp.elements[0].color = (0.0, 0.0, 0.0, 1.0)
+ compare.color_ramp.elements[0].position = context.scene.sequential_factor_smooth if context.scene.generation_method == 'sequential' else 0.5
+ compare.color_ramp.elements[1].color = (1.0, 1.0, 1.0, 1.0)
+ compare.color_ramp.elements[1].position = context.scene.sequential_factor_smooth_2 if context.scene.generation_method == 'sequential' else 1.0
+ links.new(color_mix.outputs[0], compare.inputs[0])
+ links.new(compare.outputs[0], input)
+ elif not context.scene.allow_modify_existing_textures or context.scene.generation_method == 'sequential':
+ equal.operation = 'COMPARE'
+ equal.inputs[1].default_value = 1
+ equal.inputs[2].default_value = context.scene.sequential_factor if context.scene.generation_method == 'sequential' else (1e-5 if bpy.app.version >= (5, 1, 0) else 0.0) # Small epsilon needed for blender 5.1+
+ equal.location = (color_mix.location[0], color_mix.location[1])
+ links.new(color_mix.outputs[0], equal.inputs[0])
+ links.new(equal.outputs[0], input)
+ else:
+ links.new(color_mix.outputs[0], input)
+
+ while True:
+ # Remove color ramp connected to fac, connect directly to color ramp's fac
+ if not color_mix.inputs[0].links:
+ break
+ color_ramp = color_mix.inputs[0].links[0].from_node
+ if not color_ramp.inputs[0].links:
+ # Color ramp Fac is unlinked (constant value) — remove it and
+ # fall through so the rest of the chain is wired correctly.
+ nodes.remove(color_ramp)
+ break
+ fac_node = color_ramp.inputs[0].links[0].from_node
+ # Add subtract node
+ subtract = nodes.new("ShaderNodeMath")
+ subtract.operation = 'SUBTRACT'
+ subtract.location = (color_ramp.location[0], color_ramp.location[1])
+ subtract.inputs[0].default_value = 1
+ links.new(fac_node.outputs[0], subtract.inputs[1])
+ links.new(subtract.outputs[0], color_mix.inputs["Fac"])
+ nodes.remove(color_ramp)
+ # Disconnect color1
+ links.remove(color_mix.inputs[1].links[0])
+ color_mix.inputs["Color1"].default_value = (0, 0, 0, 1)
+ if not (color_mix.inputs["Color2"].links and (color_mix.inputs[2].links[0].from_node.type == 'MIX_RGB')):
+ color_mix.inputs["Color2"].default_value = (1, 1, 1, 1)
+ break
+ else:
+ color_mix = color_mix.inputs["Color2"].links[0].from_node
+ # If there is previous tex_image node, remove it (for cases when this function is called multiple times)
+ if color_mix.inputs["Color2"].is_linked:
+ nodes.remove(color_mix.inputs["Color2"].links[0].from_node)
+
+ for node in nodes:
+ if node.type == 'SCRIPT' and "Power" in node.inputs:
+ try:
+ # Set according to weight_exponent_mask
+ node.inputs["Power"].default_value = context.scene.weight_exponent if context.scene.weight_exponent_mask else 1.0
+ except Exception as e:
+ print(f"[StableGen] - Warning: Failed to set Power for node '{node.name}'. Error: {e}")
+ # Also handle native Raycast path (Blender 5.1+) where power is a MATH POWER node
+ elif node.type == 'MATH' and node.operation == 'POWER' and node.label == 'power_weight':
+ try:
+ node.inputs[1].default_value = context.scene.weight_exponent if context.scene.weight_exponent_mask else 1.0
+ except Exception as e:
+ print(f"[StableGen] - Warning: Failed to set Power for native node '{node.name}'. Error: {e}")
+
+ # When the normalization chain exists (multi-camera), the node
+ # chain is: power_weight(exp=1) → base_weight → NormW(÷max) →
+ # SharpW(pow exp) → mix tree.
+ # For the visibility map we need the *original* un-normalized
+ # weight: pow(cos(θ), target_exp) × binary_gates.
+ # We achieve this by:
+ # 1. Setting power_weight to target_exp (restores original weight)
+ # 2. Making SharpW a passthrough (exp=1) and rerouting its input
+ # from NormW's source (the base weight) so the DIVIDE-by-max
+ # is bypassed entirely.
+ has_norm_nodes = any(
+ n.type == 'MATH' and n.operation == 'DIVIDE'
+ and n.label.startswith('NormW-')
+ for n in nodes
+ )
+ if has_norm_nodes:
+ target_exp = context.scene.weight_exponent if context.scene.weight_exponent_mask else 1.0
+ for node in nodes:
+ # Restore per-camera power to the desired exponent
+ if (node.type == 'MATH' and node.operation == 'POWER'
+ and node.label == 'power_weight'):
+ try:
+ node.inputs[1].default_value = target_exp
+ except Exception as e:
+ print(f"[StableGen] - Warning: Failed to set power_weight '{node.name}'. Error: {e}")
+ elif node.type == 'SCRIPT' and "Power" in node.inputs:
+ try:
+ node.inputs["Power"].default_value = target_exp
+ except Exception as e:
+ print(f"[StableGen] - Warning: Failed to set OSL Power '{node.name}'. Error: {e}")
+
+ # Bypass NormW+SharpW: reroute SharpW to read from
+ # NormW's source (the base weight), set exponent to 1.0
+ elif (node.type == 'MATH' and node.operation == 'POWER'
+ and node.label.startswith('SharpW-')):
+ try:
+ # SharpW.inputs[0] ← NormW.outputs[0]
+ # NormW.inputs[0] ← base_weight_output
+ # Reroute: SharpW.inputs[0] ← base_weight_output
+ if node.inputs[0].links:
+ norm_node = node.inputs[0].links[0].from_node
+ if (norm_node.label.startswith('NormW-')
+ and norm_node.inputs[0].links):
+ base_out = norm_node.inputs[0].links[0].from_socket
+ links.remove(node.inputs[0].links[0])
+ links.new(base_out, node.inputs[0])
+ node.inputs[1].default_value = 1.0
+ except Exception as e:
+ print(f"[StableGen] - Warning: Failed to bypass SharpW '{node.name}'. Error: {e}")
+
+ # Also bypass standalone NormW nodes (when user_exponent
+ # was 1.0 at build time, there's no SharpW — the NormW
+ # DIVIDE node is used directly in the mix tree).
+ elif (node.type == 'MATH' and node.operation == 'DIVIDE'
+ and node.label.startswith('NormW-')):
+ try:
+ # Check if this NormW feeds directly into the
+ # mix tree (no SharpW after it).
+ feeds_sharp = False
+ for link in node.outputs[0].links:
+ if (link.to_node.type == 'MATH'
+ and link.to_node.operation == 'POWER'
+ and link.to_node.label.startswith('SharpW-')):
+ feeds_sharp = True
+ break
+ if not feeds_sharp:
+ # This NormW feeds the mix tree directly.
+ # Reroute its downstream links to its source.
+ if node.inputs[0].links:
+ base_out = node.inputs[0].links[0].from_socket
+ for link in list(node.outputs[0].links):
+ to_socket = link.to_socket
+ links.remove(link)
+ links.new(base_out, to_socket)
+ except Exception as e:
+ print(f"[StableGen] - Warning: Failed to bypass NormW '{node.name}'. Error: {e}")
+
+ return True
+
+ # Prepare the material
+
+ if not obj:
+ # Prepare for all objects
+ for obj in to_export:
+ if not prepare_material(obj):
+ return False
+ else:
+ if not prepare_material(obj):
+ return False
+
+ if prepare_only:
+ # Debug mode: leave the visibility material applied for viewport inspection
+ # Rename the temp materials so they are identifiable as debug materials
+ for obj_key, temp_mat in temporary_materials.items():
+ temp_mat.name = f"SG_Debug_Visibility_{obj_key.name}"
+ return True
+
+ # Bake or render the texture
+ if not camera_visibility:
+ output_dir = get_dir_path(context, "uv_inpaint")["visibility"]
+ output_file = f"{obj.name}_baked_visibility"
+ prepare_baking(context)
+ bake_texture(context, obj, suffix="_visibility", texture_resolution=1024, view_transform='Raw', output_dir=output_dir)
+ # Use openCV to normalize the image
+ image_path = os.path.join(output_dir, f"{output_file}.png")
+ image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
+ # Normalize the image
+ image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX)
+ # Save the image
+ cv2.imwrite(image_path, image)
+
+ if context.scene.visibility_vignette and (context.scene.generation_method == 'local_edit' or (context.scene.model_architecture.startswith('qwen') and context.scene.qwen_generation_method == 'local_edit')):
+ apply_vignette_to_mask(
+ image_path,
+ feather_width=context.scene.visibility_vignette_width,
+ gamma=1.0,
+ blur=context.scene.visibility_vignette_blur
+ )
+ else:
+ # Make sure the camera is active and set to render
+ cameras = [obj for obj in context.scene.objects if obj.type == 'CAMERA']
+ cameras.sort(key=lambda x: x.name)
+ camera_visibility_index = [i for i, camera in enumerate(cameras) if camera == camera_visibility][0]
+ camera_render_index = (camera_visibility_index + 1) % len(cameras)
+ camera_render = cameras[camera_render_index]
+ 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))
+
+ # Restore original materials
+ for obj, materials in original_materials.items():
+ obj.data.materials.clear()
+ # First append the original active material
+ if original_active_material[obj]:
+ obj.data.materials.append(original_active_material[obj])
+ for mat in materials:
+ if mat != original_active_material[obj]:
+ obj.data.materials.append(mat)
+
+ # Clean up temporary materials
+ for _, temp_mat in temporary_materials.items():
+ if temp_mat and temp_mat.name in bpy.data.materials:
+ bpy.data.materials.remove(temp_mat)
+
+ return True
+
+
+# =========================================================
+# Camera Placement Helper Functions
+# =========================================================
+
+
+
+# ---- Baking ---------------------------------------------------------------
+
+
+class SwitchMaterial(bpy.types.Operator):
+ """Switches the material of all objects to a desired index."""
+
+ bl_idname = "object.switch_material"
+ bl_label = "Switch Material"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ material_index: bpy.props.IntProperty(
+ name="Material Index",
+ description="Index of the material to switch to",
+ default=0,
+ min=0
+ ) # type: ignore
+
+ @classmethod
+ def poll(cls, context):
+ if sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ return True
+
+ def invoke(self, context, event):
+ return context.window_manager.invoke_props_dialog(self)
+
+ def execute(self, context):
+ """
+ Executes the operator.
+ :param context: Blender context.
+ :return: {'FINISHED'}
+ """
+ # Select all objects
+ bpy.ops.object.select_all(action='SELECT')
+
+
+ for obj in context.selected_objects:
+ if obj.type != 'MESH':
+ continue
+ if self.material_index < len(obj.data.materials):
+ obj.active_material_index = self.material_index
+ # Store original materials
+ original_materials = list(obj.data.materials)
+ # Store the material to be set as active
+ to_be_active_material = obj.active_material
+ # Clear materials and assign the material at the specified index
+ obj.data.materials.clear()
+ obj.data.materials.append(to_be_active_material)
+ # Restore original materials
+ for mat in original_materials:
+ if mat != to_be_active_material:
+ obj.data.materials.append(mat)
+
+ # Deselct all objects
+ bpy.ops.object.select_all(action='DESELECT')
+ return {'FINISHED'}
+
+def prepare_baking(context):
+ bpy.context.scene.render.engine = 'CYCLES'
+ # Force CPU + OSL only for Blender < 5.1 (native Raycast nodes don't need it)
+ if bpy.app.version < (5, 1, 0):
+ if hasattr(bpy.context.scene.cycles, 'shading_system'):
+ bpy.context.scene.cycles.shading_system = True
+ else:
+ bpy.context.scene.cycles.use_osl = True
+ bpy.context.scene.cycles.device = 'CPU'
+ else:
+ # Blender 5.1+: prefer GPU baking for massive speedup on high-poly.
+ # Setting cycles.device = 'GPU' alone is not enough — we must also
+ # set compute_device_type on Cycles preferences and ensure the GPU
+ # device is enabled, otherwise Blender silently falls back to CPU.
+ try:
+ cycles_prefs = bpy.context.preferences.addons['cycles'].preferences
+ # Probe all available backends in order of preference
+ for backend in ('CUDA', 'OPTIX', 'HIP', 'ONEAPI', 'METAL'):
+ try:
+ cycles_prefs.compute_device_type = backend
+ cycles_prefs.get_devices()
+ gpu_devs = [d for d in cycles_prefs.devices
+ if d.type == backend]
+ if gpu_devs:
+ for d in gpu_devs:
+ d.use = True
+ bpy.context.scene.cycles.device = 'GPU'
+ print(f"[StableGen] Baking with {backend} GPU: "
+ f"{', '.join(d.name for d in gpu_devs)}")
+ break
+ except TypeError:
+ # This backend is not available on this platform
+ continue
+ else:
+ print("[StableGen] No GPU backend found, baking on CPU")
+ except Exception:
+ pass # Fall back to whatever the user had
+
+ # Set bake type to diffuse and contributions to color only
+ bpy.context.scene.cycles.bake_type = 'DIFFUSE'
+ bpy.context.scene.render.bake.use_pass_direct = False
+ bpy.context.scene.render.bake.use_pass_indirect = False
+ bpy.context.scene.render.bake.use_pass_color = True
+ bpy.context.scene.render.bake.view_from = 'ABOVE_SURFACE'
+
+ # Set steps to 1 for faster baking
+ bpy.context.scene.cycles.samples = 1
+
+ # Minimize light bounces — not needed for EMIT or color-only DIFFUSE bakes
+ bpy.context.scene.cycles.max_bounces = 0
+
+ # Keep the BVH in memory between consecutive bakes (huge win for PBR
+ # where we bake 5-7 channels of the same mesh back-to-back)
+ bpy.context.scene.render.use_persistent_data = True
+
+def unwrap(obj, method, overlap_only):
+ """
+ Unwraps the UVs of the given object using the selected method.
+ :param obj: Blender object.
+ :return: None
+ """
+ if method == 'none':
+ return
+
+ # Deselect all objects
+ bpy.ops.object.select_all(action='DESELECT')
+ # Set object as active and select it
+ bpy.context.view_layer.objects.active = obj
+ obj.select_set(True)
+
+ is_new = False
+
+ # If all UV maps are ProjectionUV or buffer, add new one
+ if all(["ProjectionUV" in uv.name or uv.name == "_SG_ProjectionBuffer" for uv in obj.data.uv_layers]):
+ # Add a new UV map
+ obj.data.uv_layers.new(name=f"BakeUV")
+ is_new = True
+ obj.data.uv_layers.active_index = len(obj.data.uv_layers) - 1
+ # Set it for rendering
+ obj.data.uv_layers.active = obj.data.uv_layers[-1]
+ else:
+ # Ensure the active UV is a non-ProjectionUV/buffer map
+ for uv_layer in obj.data.uv_layers:
+ if "ProjectionUV" not in uv_layer.name and uv_layer.name != "_SG_ProjectionBuffer":
+ obj.data.uv_layers.active = uv_layer
+ break
+
+ bpy.ops.object.mode_set(mode='EDIT')
+
+ # Ensure UV selection sync is OFF
+ bpy.context.scene.tool_settings.use_uv_select_sync = False
+
+ if overlap_only:
+ # Deselect
+ bpy.ops.uv.select_all(action='DESELECT')
+ # Check if the object has overlapping UVs, if not, skip unwrapping
+ bpy.ops.uv.select_overlap()
+ # Get a BMesh representation of the mesh
+ bm = bmesh.from_edit_mesh(obj.data)
+ bm.faces.ensure_lookup_table()
+ # Use the active UV layer (this is a BMUVLayer)
+ uv_layer = bm.loops.layers.uv.active
+ uv_layer = bm.loops.layers.uv.active
+ if not uv_layer:
+ bpy.ops.object.mode_set(mode='OBJECT')
+ return
+
+ # Check for ANY selected UV elements
+ if bpy.app.version >= (5, 0, 0):
+ # Blender 5.0+ uses loop.uv_select_vert
+ has_overlap = any(
+ loop.uv_select_vert
+ for face in bm.faces
+ for loop in face.loops
+ )
+ else:
+ has_overlap = any(
+ loop[uv_layer].select
+ for face in bm.faces
+ for loop in face.loops
+ )
+
+ if not has_overlap:
+ bpy.ops.object.mode_set(mode='OBJECT')
+ return
+
+ bpy.context.scene.tool_settings.use_uv_select_sync = True
+ # Select all faces
+ bpy.ops.mesh.select_all(action='SELECT')
+
+ if method == 'basic':
+ bpy.ops.uv.unwrap(method='ANGLE_BASED', margin=0.001)
+ elif method == 'smart':
+ bpy.ops.uv.smart_project()
+ elif method == 'cube':
+ bpy.ops.uv.cube_project(cube_size=1.0)
+ elif method == 'lightmap':
+ bpy.ops.uv.lightmap_pack()
+ elif method == 'pack':
+ bpy.ops.uv.pack_islands()
+
+ bpy.ops.uv.select_all(action='DESELECT')
+ bpy.ops.object.mode_set(mode='OBJECT')
+
+def bake_texture(context, obj, texture_resolution, suffix = "", view_transform = 'Standard', output_dir = None):
+ """
+ Bakes the texture of the given object.
+ :param context: Blender context.
+ :param obj: Blender object.
+ :return: True if successful, False otherwise.
+ """
+ bpy.context.scene.display_settings.display_device = 'sRGB'
+ # Backup original view transform
+ original_view_transform = bpy.context.scene.view_settings.view_transform
+ # Backup whether the object was enabled for rendering
+ original_render = obj.hide_render
+
+ bpy.context.scene.view_settings.view_transform = view_transform
+ # Set the object to be rendered
+ obj.hide_render = False
+
+ # Create a new image for baking
+ image_name = f"{obj.name}_baked{suffix}"
+ image = bpy.data.images.new(name=image_name, width=texture_resolution, height=texture_resolution)
+
+ # Create a new texture node in the object's material
+ if not obj.data.materials:
+ # Cancel
+ print("[StableGen] No materials found")
+ return False
+ else:
+ # Temporarily remove all original materials
+ mat = obj.active_material.copy()
+ original_materials = list(obj.data.materials)
+ original_active_material = obj.active_material
+ obj.data.materials.clear()
+ obj.data.materials.append(mat)
+
+ mat.use_nodes = True
+ nodes = mat.node_tree.nodes
+ links = mat.node_tree.links
+
+ tex_image = nodes.new("ShaderNodeTexImage")
+ tex_image.image = image
+ tex_image.location = (0, 0)
+
+ # Deselect all objects
+ bpy.ops.object.select_all(action='DESELECT')
+ # Set object as active and select it
+ bpy.context.view_layer.objects.active = obj
+ obj.select_set(True)
+ # Set the image node as the active bake target
+ nodes.active = tex_image
+
+ # Ensure the active UV is a non-ProjectionUV/buffer map for correct baking
+ for uv_layer in obj.data.uv_layers:
+ if "ProjectionUV" not in uv_layer.name and uv_layer.name != "_SG_ProjectionBuffer":
+ obj.data.uv_layers.active = uv_layer
+ break
+
+ # Check if there is a BSDF node before the output node
+ output = None
+ had_bsdf = False
+ for node in nodes:
+ if node.type == 'OUTPUT_MATERIAL':
+ output = node
+ break
+
+ before_output = output.inputs[0].links[0].from_node if output and output.inputs[0].is_linked else None
+ if before_output and before_output.type != 'BSDF_PRINCIPLED':
+ bsdf_node = nodes.new("ShaderNodeBsdfPrincipled")
+ bsdf_node.location = (before_output.location[0] + 200, before_output.location[1])
+ # Connect the before output node to the BSDF node
+ links.new(before_output.outputs[0], bsdf_node.inputs[0])
+ # Connect the BSDF node to the output node
+ links.new(bsdf_node.outputs[0], output.inputs[0])
+
+ # Disconnect Metallic so the DIFFUSE color-only pass returns the
+ # true Base Color. Principled BSDF multiplies diffuse by
+ # (1 - Metallic), which would dim metallic areas incorrectly.
+ for node in nodes:
+ if node.type == 'BSDF_PRINCIPLED':
+ met = node.inputs.get("Metallic")
+ if met:
+ for link in list(met.links):
+ links.remove(link)
+ met.default_value = 0.0
+ break
+
+ # Bake the texture
+ # Ensure OBJECT mode — bake fails silently in EDIT mode
+ if bpy.context.mode != 'OBJECT':
+ bpy.ops.object.mode_set(mode='OBJECT')
+ bpy.ops.object.bake(type='DIFFUSE', width=texture_resolution, height=texture_resolution)
+
+ # Save the image if required
+ if not os.path.exists(output_dir):
+ os.makedirs(output_dir)
+ if suffix == "":
+ image.filepath_raw = os.path.join(output_dir, f"{obj.name}.png")
+ else:
+ image.filepath_raw = os.path.join(output_dir, f"{obj.name}_baked{suffix}.png")
+ image.file_format = 'PNG'
+ image.save()
+
+ # Remove tex_image node
+ nodes.remove(tex_image)
+
+ # Restore original view transform
+ bpy.context.scene.view_settings.view_transform = original_view_transform
+ # Restore original render settings
+ obj.hide_render = original_render
+
+ # Restore original materials
+ obj.data.materials.clear()
+ # First append the original active material
+ if original_active_material:
+ obj.data.materials.append(original_active_material)
+ for mat_item in original_materials:
+ if mat_item != original_active_material:
+ obj.data.materials.append(mat_item)
+
+ # Remove the temporary material copy (prevent leak)
+ bpy.data.materials.remove(mat)
+
+ return True
+
+def _has_non_projection_uv(obj):
+ """Return True if the object has at least one UV map that can be used
+ for baking (not a StableGen projection UV or buffer)."""
+ if not obj or obj.type != 'MESH':
+ return False
+ if not obj.data.uv_layers:
+ return False
+ for uv in obj.data.uv_layers:
+ if (not uv.name.startswith("ProjectionUV")
+ and uv.name != "_SG_ProjectionBuffer"):
+ return True
+ return False
+
+def _uvs_likely_overlap(obj, uv_name=None):
+ me = obj.data
+ if not me.uv_layers:
+ return False
+ uv_layer = me.uv_layers.get(uv_name) if uv_name else me.uv_layers.active
+ if not uv_layer:
+ return False
+
+ seen = set()
+ for poly in me.polygons:
+ for li in poly.loop_indices:
+ uv = uv_layer.data[li].uv
+ key = (round(uv.x, 5), round(uv.y, 5))
+ if key in seen:
+ return True
+ seen.add(key)
+ return False
+
+
+# ── PBR Channel Baking Helpers ─────────────────────────────────────────
+
+# Standard export suffixes per PBR channel
+_PBR_CHANNEL_SUFFIXES = {
+ 'base_color': 'BaseColor',
+ 'roughness': 'Roughness',
+ 'metallic': 'Metallic',
+ 'normal': 'Normal',
+ 'emission': 'Emission',
+ 'height': 'Height',
+ 'ao': 'AO',
+}
+
+
+def _find_pbr_bake_sources(mat):
+ """Inspect a Principled BSDF material and return bakeable PBR channel sources.
+
+ Returns:
+ dict: ``{channel_name: output_socket}`` for each connected PBR
+ channel on the material's Principled BSDF.
+ """
+ if not mat or not mat.use_nodes:
+ return {}
+
+ nodes = mat.node_tree.nodes
+ sources = {}
+
+ # Find Principled BSDF
+ principled = None
+ for node in nodes:
+ if node.type == 'BSDF_PRINCIPLED':
+ principled = node
+ break
+ if not principled:
+ return {}
+
+ # ── Base Color + AO ──────────────────────────────────────────────
+ base_input = principled.inputs.get("Base Color")
+ if base_input and base_input.links:
+ src_node = base_input.links[0].from_node
+ is_ao_mult = (
+ getattr(src_node, 'label', '') == 'AO Multiply'
+ or (src_node.type == 'MIX'
+ and getattr(src_node, 'data_type', '') == 'RGBA'
+ and getattr(src_node, 'blend_type', '') == 'MULTIPLY'))
+ if is_ao_mult:
+ # A input (index 6) = base colour, B input (index 7) = AO
+ a_in = src_node.inputs[6]
+ if a_in.links:
+ sources['base_color'] = a_in.links[0].from_socket
+ b_in = src_node.inputs[7]
+ if b_in.links:
+ sources['ao'] = b_in.links[0].from_socket
+ else:
+ sources['base_color'] = base_input.links[0].from_socket
+
+ # ── Roughness ────────────────────────────────────────────────────
+ ri = principled.inputs.get("Roughness")
+ if ri and ri.links:
+ sources['roughness'] = ri.links[0].from_socket
+
+ # ── Metallic ─────────────────────────────────────────────────────
+ mi = principled.inputs.get("Metallic")
+ if mi and mi.links:
+ sources['metallic'] = mi.links[0].from_socket
+
+ # ── Normal (raw colours before Normal Map node) ──────────────────
+ ni = principled.inputs.get("Normal")
+ if ni and ni.links:
+ n_node = ni.links[0].from_node
+ if n_node.type == 'NORMAL_MAP':
+ ci = n_node.inputs.get("Color")
+ if ci and ci.links:
+ sources['normal'] = ci.links[0].from_socket
+
+ # ── Emission ─────────────────────────────────────────────────────
+ ei = principled.inputs.get("Emission Color") or principled.inputs.get("Emission")
+ if ei and ei.links:
+ sources['emission'] = ei.links[0].from_socket
+
+ # ── Height (from Displacement node → Material Output) ────────────
+ for node in nodes:
+ if node.type == 'OUTPUT_MATERIAL':
+ di = node.inputs.get("Displacement")
+ if di and di.links:
+ d_node = di.links[0].from_node
+ if d_node.type == 'DISPLACEMENT':
+ hi = d_node.inputs.get("Height")
+ if hi and hi.links:
+ sources['height'] = hi.links[0].from_socket
+ break
+
+ return sources
+
+
+def _restore_materials(obj, original_materials, original_active):
+ """Restore an object's material slots back to their original state."""
+ obj.data.materials.clear()
+ if original_active:
+ obj.data.materials.append(original_active)
+ for m in original_materials:
+ if m != original_active:
+ obj.data.materials.append(m)
+
+
+def bake_pbr_channel(context, obj, channel_name, texture_resolution, output_dir):
+ """Bake one PBR channel via the Emit technique.
+
+ Copies the material, finds the source socket for *channel_name*,
+ rewires it through an Emission shader to the Material Output, bakes
+ ``EMIT``, and saves the result to ``output_dir/{obj.name}_{Suffix}.png``.
+
+ Returns:
+ File path of the baked image, or ``None`` on failure.
+ """
+ if not obj.data.materials or not obj.active_material:
+ return None
+
+ # ── Save state ─────────────────────────────────────────────────
+ orig_vt = context.scene.view_settings.view_transform
+ orig_render = obj.hide_render
+ context.scene.view_settings.view_transform = 'Standard'
+ obj.hide_render = False
+
+ # ── Copy material onto the object ──────────────────────────────
+ original_materials = list(obj.data.materials)
+ original_active = obj.active_material
+ mat_copy = original_active.copy()
+ obj.data.materials.clear()
+ obj.data.materials.append(mat_copy)
+ mat_copy.use_nodes = True
+ nodes = mat_copy.node_tree.nodes
+ links = mat_copy.node_tree.links
+
+ # ── Find source socket in the copy ─────────────────────────────
+ sources = _find_pbr_bake_sources(mat_copy)
+ source_socket = sources.get(channel_name)
+ if source_socket is None:
+ _restore_materials(obj, original_materials, original_active)
+ bpy.data.materials.remove(mat_copy)
+ context.scene.view_settings.view_transform = orig_vt
+ obj.hide_render = orig_render
+ return None
+
+ # ── Find Output node ───────────────────────────────────────────
+ output_node = None
+ for n in nodes:
+ if n.type == 'OUTPUT_MATERIAL':
+ output_node = n
+ break
+ if not output_node:
+ _restore_materials(obj, original_materials, original_active)
+ bpy.data.materials.remove(mat_copy)
+ context.scene.view_settings.view_transform = orig_vt
+ obj.hide_render = orig_render
+ return None
+
+ # ── Rewire material for baking ───────────────────────────────
+ # Normal channel: use Cycles' built-in NORMAL bake which automatically
+ # converts to tangent space — game-engine / glTF compatible regardless
+ # of the original normal mode (world, bump, tangent).
+ # All other channels: rewire source → Emission → Output for EMIT bake.
+ bake_type = 'EMIT'
+ if channel_name == 'normal':
+ # Keep the Principled BSDF wired to the Output with the Normal
+ # input connected. Cycles NORMAL bake evaluates the shader's
+ # final shading normal and converts it to tangent space for us.
+ bake_type = 'NORMAL'
+ # Ensure the normal influence is at full strength for baking
+ principled = None
+ for n in nodes:
+ if n.type == 'BSDF_PRINCIPLED':
+ principled = n
+ break
+ if principled and principled.inputs.get("Normal") and principled.inputs["Normal"].links:
+ nmap_node = principled.inputs["Normal"].links[0].from_node
+ if nmap_node.type == 'NORMAL_MAP':
+ nmap_node.inputs["Strength"].default_value = 1.0
+ else:
+ for link in list(output_node.inputs["Surface"].links):
+ links.remove(link)
+
+ emit_node = nodes.new("ShaderNodeEmission")
+ emit_node.location = (output_node.location[0] - 200,
+ output_node.location[1])
+ links.new(source_socket, emit_node.inputs["Color"])
+ links.new(emit_node.outputs["Emission"],
+ output_node.inputs["Surface"])
+
+ # ── Bake target image ──────────────────────────────────────────
+ suffix = _PBR_CHANNEL_SUFFIXES.get(channel_name, channel_name)
+ img_name = f"{obj.name}_{suffix}"
+ bake_img = bpy.data.images.new(name=img_name,
+ width=texture_resolution,
+ height=texture_resolution)
+
+ # Non-color channels (roughness, metallic, normal, height, AO) must be
+ # stored in linear / Non-Color so the round-trip is lossless:
+ # Cycles linear → raw 8-bit → PNG → load as Non-Color → linear.
+ # Without this, the default sRGB image applies gamma encoding on write,
+ # but add_baked_material() loads them as Non-Color (no decode) →
+ # values are gamma-shifted (e.g. roughness 0.22 → 0.50).
+ if channel_name not in ('base_color', 'emission'):
+ bake_img.colorspace_settings.name = 'Non-Color'
+
+ tex_node = nodes.new("ShaderNodeTexImage")
+ tex_node.image = bake_img
+ nodes.active = tex_node
+
+ # ── Select object + non-projection UV ──────────────────────────
+ bpy.ops.object.select_all(action='DESELECT')
+ context.view_layer.objects.active = obj
+ obj.select_set(True)
+ for uv in obj.data.uv_layers:
+ if "ProjectionUV" not in uv.name and uv.name != "_SG_ProjectionBuffer":
+ obj.data.uv_layers.active = uv
+ break
+
+ # ── Bake ─────────────────────────────────────────────────────
+ # Ensure OBJECT mode — bake fails silently in EDIT mode
+ if bpy.context.mode != 'OBJECT':
+ bpy.ops.object.mode_set(mode='OBJECT')
+ if bake_type == 'NORMAL':
+ bpy.ops.object.bake(type='NORMAL',
+ normal_space='TANGENT',
+ width=texture_resolution,
+ height=texture_resolution)
+ else:
+ bpy.ops.object.bake(type='EMIT',
+ width=texture_resolution,
+ height=texture_resolution)
+
+ # ── Save ───────────────────────────────────────────────────────
+ os.makedirs(output_dir, exist_ok=True)
+ out_path = os.path.join(output_dir, f"{obj.name}_{suffix}.png")
+ bake_img.filepath_raw = out_path
+ bake_img.file_format = 'PNG'
+ bake_img.save()
+ print(f"[StableGen] Saved PBR channel: {out_path}")
+
+ # ── Cleanup ────────────────────────────────────────────────────
+ _restore_materials(obj, original_materials, original_active)
+ bpy.data.materials.remove(mat_copy)
+ context.scene.view_settings.view_transform = orig_vt
+ obj.hide_render = orig_render
+ return out_path
+
+
+def _flip_normal_green(filepath):
+ """Flip the green channel of a normal map (OpenGL ↔ DirectX)."""
+ if not os.path.exists(filepath):
+ return
+ try:
+ import numpy as np
+ img = bpy.data.images.load(filepath, check_existing=False)
+ w, h = img.size
+ px = np.array(img.pixels[:]).reshape(h, w, 4)
+ px[:, :, 1] = 1.0 - px[:, :, 1]
+ img.pixels = px.flatten().tolist()
+ img.save()
+ bpy.data.images.remove(img)
+ print(f"[StableGen] Flipped normal green channel (DirectX): {filepath}")
+ except Exception as e:
+ print(f"[StableGen] Failed to flip normal green channel: {e}")
+
+
+def _pack_orm_texture(output_dir, obj_name):
+ """Pack AO, Roughness, Metallic into an ORM texture.
+
+ Channel layout: R = AO, G = Roughness, B = Metallic (Unreal / glTF).
+ Requires at least Roughness and Metallic to exist.
+ """
+ import numpy as np
+
+ rough_path = os.path.join(output_dir, f"{obj_name}_Roughness.png")
+ metal_path = os.path.join(output_dir, f"{obj_name}_Metallic.png")
+ if not os.path.exists(rough_path) or not os.path.exists(metal_path):
+ return None
+
+ rough_img = bpy.data.images.load(rough_path, check_existing=True)
+ metal_img = bpy.data.images.load(metal_path, check_existing=True)
+ w, h = rough_img.size
+
+ rough_px = np.array(rough_img.pixels[:]).reshape(h, w, 4)
+ metal_px = np.array(metal_img.pixels[:]).reshape(h, w, 4)
+
+ ao_path = os.path.join(output_dir, f"{obj_name}_AO.png")
+ if os.path.exists(ao_path):
+ ao_img = bpy.data.images.load(ao_path, check_existing=True)
+ ao_px = np.array(ao_img.pixels[:]).reshape(h, w, 4)
+ ao_chan = ao_px[:, :, 0]
+ else:
+ ao_chan = np.ones((h, w), dtype=np.float32)
+
+ orm = np.ones((h, w, 4), dtype=np.float32)
+ orm[:, :, 0] = ao_chan
+ orm[:, :, 1] = rough_px[:, :, 0]
+ orm[:, :, 2] = metal_px[:, :, 0]
+
+ orm_img = bpy.data.images.new(name=f"{obj_name}_ORM", width=w, height=h)
+ orm_img.pixels = orm.flatten().tolist()
+
+ orm_path = os.path.join(output_dir, f"{obj_name}_ORM.png")
+ orm_img.filepath_raw = orm_path
+ orm_img.file_format = 'PNG'
+ orm_img.save()
+ print(f"[StableGen] Packed ORM texture: {orm_path}")
+ return orm_path
+
+
+class BakeTextures(bpy.types.Operator):
+ """Bakes textures using the cycles render engine.
+
+ - This will convert the textures to use a UV map. The first non-projection UV map will be used. If there are none, a new one will be created.
+ - Textures will be output to the "baked" directory in the output path.
+ - This will make the generated textures available in the Material Preview viewport shading mode."""
+ bl_idname = "object.bake_textures"
+ bl_label = "Bake Textures"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ texture_resolution: bpy.props.IntProperty(
+ name="Texture Resolution",
+ description="Resolution of the baked textures",
+ default=2048,
+ min=128,
+ max=8192
+ ) # type: ignore
+
+ try_unwrap: bpy.props.EnumProperty(
+ name="Unwrap Method",
+ description="Method to unwrap UVs before baking",
+ items=[
+ ('none', 'None', 'Skip UV unwrapping'),
+ ('cube', 'Cube Project', 'Fast cube projection — great for high-poly / TRELLIS meshes (handles 1M+ faces instantly)'),
+ ('smart', 'Smart UV Project', 'Smart UV Project — good balance of quality and speed'),
+ ('basic', 'Basic Unwrap', 'Angle-based unwrap — best quality but very slow on high-poly meshes'),
+ ('lightmap', 'Lightmap Pack', 'Use Lightmap Pack with default parameters'),
+ ('pack', 'Pack Islands', 'Use Pack Islands with default parameters')
+ ],
+ default='smart'
+ ) # type: ignore
+
+ add_material: bpy.props.BoolProperty(
+ name="Add Material",
+ description="Add the baked texture as a material to the objects",
+ default=True
+ ) # type: ignore
+
+ flatten_for_refine: bpy.props.BoolProperty(
+ name="Bake & Continue Refining",
+ description="After baking, apply the baked texture to the StableGen projection material and clean up previous projection images",
+ default=False
+ ) # type: ignore
+
+ overlap_only: bpy.props.BoolProperty(
+ name="Overlap Only",
+ description="Only unwrap objects with overlapping UVs",
+ default=False
+ ) # type: ignore
+
+ bake_pbr: bpy.props.BoolProperty(
+ name="Bake PBR Maps",
+ description="Bake individual PBR channel maps (BaseColor, Roughness, Metallic, Normal, Emission, Height, AO) "
+ "with standard naming for game-engine import",
+ default=True
+ ) # type: ignore
+
+ export_orm: bpy.props.BoolProperty(
+ name="Pack ORM Texture",
+ description="Create a packed ORM texture (R=AO, G=Roughness, B=Metallic) for Unreal Engine / glTF workflows",
+ default=False
+ ) # type: ignore
+
+ normal_convention: bpy.props.EnumProperty(
+ name="Normal Convention",
+ description="Normal map Y-axis convention",
+ items=[
+ ('opengl', 'OpenGL (Y+)', 'Standard OpenGL / glTF / Unity / Blender convention'),
+ ('directx', 'DirectX (Y-)', 'DirectX / Unreal Engine convention (flips green channel)'),
+ ],
+ default='opengl'
+ ) # type: ignore
+
+ _timer = None
+ _objects = []
+ _current_index = 0
+ _phase = 'unwrap'
+
+ # Add properties to track progress
+ _progress = 0.0
+ _stage = ""
+ _current_object = 0
+ _total_objects = 0
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._progress = 0
+ self._stage = ""
+ self._current_object = 0
+ self._total_objects = 0
+
+ @classmethod
+ def poll(self, context):
+ if sg_modal_active(context):
+ self.poll_message_set("Another operation is in progress")
+ return False
+ addon_prefs = context.preferences.addons[_ADDON_PKG].preferences
+ if not os.path.exists(addon_prefs.output_dir):
+ self.poll_message_set("Output directory not set or does not exist (check addon preferences)")
+ return False
+ return True
+
+ def draw(self, context):
+ layout = self.layout
+ layout.prop(self, "texture_resolution")
+ layout.prop(self, "try_unwrap")
+ layout.prop(self, "overlap_only")
+ layout.prop(self, "add_material")
+ layout.prop(self, "flatten_for_refine")
+ layout.separator()
+ layout.label(text="PBR Export:")
+ layout.prop(self, "bake_pbr")
+ pbr_col = layout.column()
+ pbr_col.enabled = self.bake_pbr
+ pbr_col.prop(self, "export_orm")
+ pbr_col.prop(self, "normal_convention")
+
+ def invoke(self, context, event):
+ """
+ Invokes the operator.
+ :param context: Blender context.
+ :param event: Blender event.
+ :return: {'RUNNING_MODAL'}
+ """
+ if context.scene.texture_objects == 'all':
+ self._objects = [obj for obj in context.view_layer.objects if obj.type == 'MESH' and not obj.hide_get()]
+ else: # 'selected'
+ self._objects = [obj for obj in context.selected_objects if obj.type == 'MESH']
+ self._current_index = 0
+ self._phase = 'unwrap'
+ self._total_objects = len(self._objects)
+ self._stage = "Preparing"
+ return context.window_manager.invoke_props_dialog(self)
+
+ def execute(self, context):
+ """
+ Executes the operator.
+ :param context: Blender context.
+ :return: {'RUNNING_MODAL'}
+ """
+
+ self.original_engine = bpy.context.scene.render.engine
+ self.original_shading = bpy.context.space_data.shading.type
+ self.original_device = bpy.context.scene.cycles.device
+ self.original_samples = bpy.context.scene.cycles.samples
+ self.original_max_bounces = bpy.context.scene.cycles.max_bounces
+ self.original_persistent = bpy.context.scene.render.use_persistent_data
+ # Save Cycles compute_device_type so we can restore after GPU bake
+ try:
+ self._original_compute_device_type = (
+ bpy.context.preferences.addons['cycles'].preferences.compute_device_type
+ )
+ except Exception:
+ self._original_compute_device_type = None
+ # Set render engine to CYCLES (required for baking)
+ bpy.context.scene.render.engine = 'CYCLES'
+ # Switch to Solid shading to avoid conflicts with Rendered mode
+ # (Rendered viewport + bake = crash). Solid is engine-independent
+ # and won't cause pink textures like Material Preview (EEVEE) does.
+ for area in context.screen.areas:
+ if area.type == 'VIEW_3D':
+ for space in area.spaces:
+ if space.type == 'VIEW_3D':
+ space.shading.type = 'SOLID'
+ break
+ prepare_baking(context)
+
+ # Start modal operation
+ context.window_manager.modal_handler_add(self)
+ self._timer = context.window_manager.event_timer_add(0.1, window=context.window)
+ return {'RUNNING_MODAL'}
+
+ def modal(self, context, event):
+ """
+ Handles modal events.
+ :param context: Blender context.
+ :param event: Blender event.
+ :return: {'PASS_THROUGH'}
+ """
+ if event.type == 'TIMER':
+ def redraw():
+ for area in context.screen.areas:
+ area.tag_redraw()
+ redraw()
+
+ # ── PBR channel bake (flat queue, one item per tick) ──────
+ if self._phase == 'bake_pbr':
+ if self._pbr_queue_idx < len(self._pbr_queue):
+ obj, channel = self._pbr_queue[self._pbr_queue_idx]
+ nice = _PBR_CHANNEL_SUFFIXES.get(channel, channel)
+ self._stage = f"PBR {nice} — {obj.name}"
+ self._progress = (self._pbr_queue_idx / len(self._pbr_queue)) * 100
+ redraw()
+ bake_pbr_channel(
+ context, obj, channel,
+ self.texture_resolution,
+ get_dir_path(context, "baked"))
+ self._pbr_queue_idx += 1
+ else:
+ # DirectX normal flip
+ if self.normal_convention == 'directx':
+ out_dir = get_dir_path(context, "baked")
+ for obj in self._objects:
+ _flip_normal_green(
+ os.path.join(out_dir,
+ f"{obj.name}_Normal.png"))
+ # Pack ORM
+ if self.export_orm:
+ self.report({'INFO'}, "Packing ORM textures...")
+ self._phase = 'pack_orm'
+ self._current_index = 0
+ else:
+ self.report({'INFO'}, "Applying materials...")
+ self._phase = 'apply_material'
+ self._current_index = 0
+ return {'PASS_THROUGH'}
+
+ if self._current_index < len(self._objects):
+ obj = self._objects[self._current_index]
+ if self._phase == 'unwrap':
+ self._stage = f"Unwrapping {obj.name}"
+ self._progress = 0
+ redraw()
+
+ if self.try_unwrap != 'none':
+ if not _has_non_projection_uv(obj):
+ # No usable UV exists — must unwrap
+ unwrap(obj, self.try_unwrap, self.overlap_only)
+ elif self.overlap_only:
+ # Only re-unwrap if existing UVs overlap
+ if _uvs_likely_overlap(obj):
+ unwrap(obj, self.try_unwrap, self.overlap_only)
+ else:
+ # Always re-unwrap when overlap_only is disabled
+ unwrap(obj, self.try_unwrap, self.overlap_only)
+
+ self._progress = 100
+ elif self._phase == 'bake':
+ if self.bake_pbr:
+ # PBR mode bakes BaseColor via EMIT — skip the
+ # redundant DIFFUSE bake entirely.
+ self._progress = 100
+ else:
+ self._stage = f"Baking {obj.name}"
+ self._progress = 0
+ redraw()
+ if not bake_texture(context, obj, self.texture_resolution, output_dir=get_dir_path(context, "baked")):
+ self.report({'ERROR'}, f"Failed to bake texture for {obj.name}. No materials found.")
+ context.window_manager.event_timer_remove(self._timer)
+ return {'CANCELLED'}
+
+ # NEW: optionally flatten into the projection material so you can keep refining
+ if self.flatten_for_refine:
+ try:
+ baked_path = get_file_path(context, "baked", object_name=obj.name)
+ flatten_projection_material_for_refine(context, obj, baked_path)
+ purge_orphans()
+ except Exception as e:
+ print(f"[StableGen] Failed to flatten projection material for {obj.name}: {e}")
+
+ self._progress = 100
+ elif self._phase == 'pack_orm':
+ self._stage = f"Packing ORM — {obj.name}"
+ self._progress = 0
+ redraw()
+ _pack_orm_texture(get_dir_path(context, "baked"), obj.name)
+ self._progress = 100
+ elif self._phase == 'apply_material' and self.add_material:
+ self._stage = f"Applying Material to {obj.name}"
+ self._progress = 0
+ redraw()
+ self.add_baked_material(context, obj)
+ self._progress = 100
+ self._current_index += 1
+ if self._current_index < len(self._objects):
+ self._current_object = self._current_index
+ else:
+ if self._phase == 'unwrap':
+ self.report({'INFO'}, "Baking textures...")
+ self._phase = 'bake'
+ self._current_index = 0
+ elif self._phase == 'bake':
+ if self.bake_pbr:
+ self.report({'INFO'}, "Baking PBR channels...")
+ self._phase = 'bake_pbr'
+ self._pbr_queue = []
+ for obj in self._objects:
+ if obj.active_material:
+ for ch in _find_pbr_bake_sources(obj.active_material):
+ self._pbr_queue.append((obj, ch))
+ self._pbr_queue_idx = 0
+ if not self._pbr_queue:
+ # No PBR sources — skip straight to apply
+ self.report({'INFO'}, "No PBR channels found, skipping PBR bake...")
+ self._phase = 'apply_material'
+ self._current_index = 0
+ else:
+ self.report({'INFO'}, "Applying materials...")
+ self._phase = 'apply_material'
+ self._current_index = 0
+ elif self._phase == 'pack_orm':
+ self.report({'INFO'}, "Applying materials...")
+ self._phase = 'apply_material'
+ self._current_index = 0
+ else:
+ context.window_manager.event_timer_remove(self._timer)
+ bpy.context.scene.render.engine = self.original_engine
+ bpy.context.scene.cycles.device = self.original_device
+ bpy.context.scene.cycles.samples = self.original_samples
+ bpy.context.scene.cycles.max_bounces = self.original_max_bounces
+ bpy.context.scene.render.use_persistent_data = self.original_persistent
+ # Restore Cycles compute_device_type
+ if self._original_compute_device_type is not None:
+ try:
+ bpy.context.preferences.addons['cycles'].preferences.compute_device_type = (
+ self._original_compute_device_type
+ )
+ except Exception:
+ pass
+ # Restore original viewport shading
+ for area in context.screen.areas:
+ if area.type == 'VIEW_3D':
+ for space in area.spaces:
+ if space.type == 'VIEW_3D':
+ space.shading.type = self.original_shading
+ break
+ self.report({'INFO'}, "Textures baked successfully.")
+ remove_empty_dirs(context)
+ return {'FINISHED'}
+ return {'PASS_THROUGH'}
+
+ def add_baked_material(self, context, obj):
+ """Create a baked material with standard PBR textures (game-engine ready).
+
+ When ``bake_pbr`` is enabled, looks for ``{obj.name}_{Channel}.png``
+ files in the baked output directory and wires them to a Principled
+ BSDF. Falls back to the single-texture path when PBR maps are absent.
+ """
+ output_dir = get_dir_path(context, "baked")
+
+ mat = bpy.data.materials.new(name=f"{obj.name}_baked")
+ obj.data.materials.append(mat)
+ mat.use_nodes = True
+ nodes = mat.node_tree.nodes
+ links = mat.node_tree.links
+ for node in nodes:
+ nodes.remove(node)
+
+ # Assign the new material to all faces
+ obj.active_material_index = len(obj.material_slots) - 1
+ bpy.context.view_layer.objects.active = obj
+ bpy.ops.object.mode_set(mode='EDIT')
+ bpy.ops.mesh.select_all(action='SELECT')
+ bpy.ops.object.material_slot_assign()
+ bpy.ops.object.mode_set(mode='OBJECT')
+
+ # Output node
+ output_node = nodes.new("ShaderNodeOutputMaterial")
+ output_node.location = (800, 0)
+
+ # Principled BSDF
+ principled = nodes.new("ShaderNodeBsdfPrincipled")
+ principled.location = (400, 0)
+ # Fallback values when PBR textures are absent:
+ # max roughness + zero metallic → matte, non-reflective surface.
+ principled.inputs["Roughness"].default_value = 1.0
+ principled.inputs["Metallic"].default_value = 0.0
+
+ # UV Map
+ uv_node = nodes.new("ShaderNodeUVMap")
+ if "BakeUV" in [uv.name for uv in obj.data.uv_layers]:
+ uv_node.uv_map = "BakeUV"
+ else:
+ uv_node.uv_map = obj.data.uv_layers[0].name
+ uv_node.location = (-600, 0)
+
+ # ── Base Color ────────────────────────────────────────────────
+ base_path = os.path.join(output_dir, f"{obj.name}_BaseColor.png")
+ if not os.path.exists(base_path):
+ # Fallback to standard diffuse bake
+ base_path = get_file_path(context, "baked", object_name=obj.name)
+
+ has_base = os.path.exists(base_path)
+ if has_base:
+ tex_base = nodes.new("ShaderNodeTexImage")
+ tex_base.image = bpy.data.images.load(base_path)
+ tex_base.location = (-200, 200)
+ tex_base.label = "BaseColor"
+ links.new(uv_node.outputs["UV"], tex_base.inputs["Vector"])
+
+ links.new(tex_base.outputs["Color"],
+ principled.inputs["Base Color"])
+ links.new(principled.outputs[0],
+ output_node.inputs["Surface"])
+ else:
+ links.new(principled.outputs[0],
+ output_node.inputs["Surface"])
+
+ if not self.bake_pbr:
+ return
+
+ # ── PBR channel textures ──────────────────────────────────────
+ y_pos = -100
+
+ # Roughness
+ rough_path = os.path.join(output_dir, f"{obj.name}_Roughness.png")
+ if os.path.exists(rough_path):
+ y_pos -= 250
+ tex = nodes.new("ShaderNodeTexImage")
+ tex.image = bpy.data.images.load(rough_path)
+ tex.image.colorspace_settings.name = 'Non-Color'
+ tex.location = (-200, y_pos)
+ tex.label = "Roughness"
+ links.new(uv_node.outputs["UV"], tex.inputs["Vector"])
+ links.new(tex.outputs["Color"],
+ principled.inputs["Roughness"])
+
+ # Metallic
+ metal_path = os.path.join(output_dir, f"{obj.name}_Metallic.png")
+ if os.path.exists(metal_path):
+ y_pos -= 250
+ tex = nodes.new("ShaderNodeTexImage")
+ tex.image = bpy.data.images.load(metal_path)
+ tex.image.colorspace_settings.name = 'Non-Color'
+ tex.location = (-200, y_pos)
+ tex.label = "Metallic"
+ links.new(uv_node.outputs["UV"], tex.inputs["Vector"])
+ links.new(tex.outputs["Color"],
+ principled.inputs["Metallic"])
+
+ # Normal
+ normal_path = os.path.join(output_dir, f"{obj.name}_Normal.png")
+ if os.path.exists(normal_path):
+ y_pos -= 250
+ tex = nodes.new("ShaderNodeTexImage")
+ tex.image = bpy.data.images.load(normal_path)
+ tex.image.colorspace_settings.name = 'Non-Color'
+ tex.location = (-200, y_pos)
+ tex.label = "Normal"
+ links.new(uv_node.outputs["UV"], tex.inputs["Vector"])
+ nmap = nodes.new("ShaderNodeNormalMap")
+ nmap.location = (100, y_pos)
+ nmap.label = "Normal Map"
+ # The normal bake uses Cycles NORMAL pass with tangent space,
+ # so the texture is always tangent-space regardless of the
+ # original projection material's normal mode (world/bump/tangent).
+ nmap.space = 'TANGENT'
+
+ links.new(tex.outputs["Color"], nmap.inputs["Color"])
+ if "Normal" in principled.inputs:
+ links.new(nmap.outputs["Normal"],
+ principled.inputs["Normal"])
+
+ # Emission
+ emission_path = os.path.join(output_dir,
+ f"{obj.name}_Emission.png")
+ if os.path.exists(emission_path):
+ y_pos -= 250
+ tex = nodes.new("ShaderNodeTexImage")
+ tex.image = bpy.data.images.load(emission_path)
+ tex.location = (-200, y_pos)
+ tex.label = "Emission"
+ links.new(uv_node.outputs["UV"], tex.inputs["Vector"])
+ em_in = (principled.inputs.get("Emission Color")
+ or principled.inputs.get("Emission"))
+ if em_in:
+ links.new(tex.outputs["Color"], em_in)
+ if "Emission Strength" in principled.inputs:
+ principled.inputs[
+ "Emission Strength"].default_value = 1.0
+
+ # Height → Displacement
+ height_path = os.path.join(output_dir,
+ f"{obj.name}_Height.png")
+ if os.path.exists(height_path):
+ y_pos -= 250
+ tex = nodes.new("ShaderNodeTexImage")
+ tex.image = bpy.data.images.load(height_path)
+ tex.image.colorspace_settings.name = 'Non-Color'
+ tex.location = (-200, y_pos)
+ tex.label = "Height"
+ links.new(uv_node.outputs["UV"], tex.inputs["Vector"])
+ disp = nodes.new("ShaderNodeDisplacement")
+ disp.location = (600, -300)
+ disp.label = "Displacement"
+ disp.inputs["Scale"].default_value = 0.1
+ disp.inputs["Midlevel"].default_value = 0.5
+ links.new(tex.outputs["Color"], disp.inputs["Height"])
+ links.new(disp.outputs["Displacement"],
+ output_node.inputs["Displacement"])
+
+ # AO → multiply with Base Color
+ ao_path = os.path.join(output_dir, f"{obj.name}_AO.png")
+ if (os.path.exists(ao_path)
+ and principled.inputs["Base Color"].links):
+ y_pos -= 250
+ tex_ao = nodes.new("ShaderNodeTexImage")
+ tex_ao.image = bpy.data.images.load(ao_path)
+ tex_ao.image.colorspace_settings.name = 'Non-Color'
+ tex_ao.location = (-200, y_pos)
+ tex_ao.label = "AO"
+ links.new(uv_node.outputs["UV"], tex_ao.inputs["Vector"])
+ # Intercept Base Color → Principled with a Multiply
+ old_src = principled.inputs[
+ "Base Color"].links[0].from_socket
+ mix = nodes.new("ShaderNodeMix")
+ mix.data_type = 'RGBA'
+ mix.blend_type = 'MULTIPLY'
+ mix.inputs["Factor"].default_value = 1.0
+ mix.location = (200, 150)
+ mix.label = "AO Multiply"
+ links.new(old_src, mix.inputs[6]) # A
+ links.new(tex_ao.outputs["Color"], mix.inputs[7]) # B
+ links.new(mix.outputs[2],
+ principled.inputs["Base Color"])
diff --git a/stablegen/texturing/workflows.py b/stablegen/texturing/workflows.py
new file mode 100644
index 0000000..3e2e015
--- /dev/null
+++ b/stablegen/texturing/workflows.py
@@ -0,0 +1,3015 @@
+import bpy
+import os
+import json
+import uuid
+import random
+import urllib.request
+import urllib.parse
+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 io import BytesIO
+import numpy as np
+from PIL import Image
+
+_ADDON_PKG = __package__.rsplit('.', 1)[0]
+
+
+def _texturing_prompt(scene):
+ """Return the texturing portion of the prompt.
+
+ Priority order:
+ 1. Dedicated ``texture_prompt`` field (if the separate-prompt toggle is on
+ and the field is non-empty).
+ 2. Legacy ``||`` separator in the main prompt (text after ``||``).
+ 3. The full main prompt as-is.
+ """
+ if getattr(scene, 'use_separate_texture_prompt', False):
+ tex = getattr(scene, 'texture_prompt', '').strip()
+ if tex:
+ return tex
+ raw = scene.comfyui_prompt
+ if '||' in raw:
+ return raw.split('||', 1)[1].strip()
+ return raw
+
+
+def _generation_prompt(scene):
+ """Return the generation / mesh-creation portion of the prompt.
+
+ When the separate-texture-prompt toggle is on the entire main prompt is
+ treated as the generation prompt (no ``||`` splitting). Otherwise the
+ legacy ``||`` separator is respected.
+ """
+ raw = scene.comfyui_prompt
+ if getattr(scene, 'use_separate_texture_prompt', False):
+ return raw.strip()
+ if '||' in raw:
+ return raw.split('||', 1)[0].strip()
+ return raw
+
+
+class _TexturingWorkflowMixin:
+ """Texturing workflow methods for WorkflowManager."""
+
+ def _crop_and_vignette(
+ self,
+ img_bytes,
+ border_px: int = 8,
+ 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.
+
+ 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.
+ feather : fraction of min(width, height) that is used as the feather band.
+ gamma : exponent applied to the feather mask (1.0 = linear).
+ """
+
+ if img_bytes is None:
+ return None
+
+ # --- Load image as RGBA without touching color ---
+ buf = BytesIO(img_bytes)
+ try:
+ img = Image.open(buf).convert("RGBA")
+ except Exception as e:
+ print(f"[StableGen] _crop_and_vignette: cannot decode image "
+ 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))
+ w, h = img.size
+
+ # Convert to numpy for mask math
+ arr = np.asarray(img, dtype=np.float32) / 255.0 # [H, W, 4]
+ rgb = arr[..., :3]
+ alpha_orig = arr[..., 3]
+
+ # If there was no alpha, assume fully opaque as starting point
+ if np.all(alpha_orig == 0):
+ alpha_orig = np.ones_like(alpha_orig, dtype=np.float32)
+
+ # --- Build rectangular feather mask based on distance to nearest edge ---
+
+ # Normalized distance (in pixels) to each edge
+ yy, xx = np.mgrid[0:h, 0:w]
+ dist_to_left = xx
+ dist_to_right = (w - 1) - xx
+ dist_to_top = yy
+ dist_to_bottom = (h - 1) - yy
+
+ dist_to_edge = np.minimum(
+ np.minimum(dist_to_left, dist_to_right),
+ np.minimum(dist_to_top, dist_to_bottom),
+ ).astype(np.float32)
+
+ # Feather band thickness in pixels
+ # (0.0–0.5 of the smaller dimension is reasonable)
+ min_dim = float(min(w, h))
+ feather_px = max(1.0, feather * 0.5 * min_dim)
+
+ # 0 at the border, 1 in the interior beyond the feather band
+ mask = dist_to_edge / feather_px
+ mask = np.clip(mask, 0.0, 1.0)
+
+ # Shape the transition with gamma (only on the mask, not on RGB!)
+ if gamma != 1.0:
+ # stronger bias toward 1.0 in mid-range
+ mask = np.clip(mask, 1e-6, 1.0)
+ mask = np.power(mask, gamma)
+
+ # Compose final alpha: original alpha * vignette mask
+ alpha_new = alpha_orig * mask
+
+ # Reassemble RGBA, leaving RGB completely unchanged
+ out = np.empty_like(arr)
+ out[..., :3] = rgb
+ out[..., 3] = alpha_new
+ out = np.clip(out * 255.0, 0, 255).astype(np.uint8)
+
+ img_out = Image.fromarray(out, mode="RGBA")
+
+ out_buf = BytesIO()
+ img_out.save(out_buf, format="PNG")
+ return out_buf.getvalue()
+
+ def _build_qwen_reference_latent_chain(self, prompt, NODES, start_node_id=700):
+ """
+ Build VAEEncode + ReferenceLatent chain for the Qwen unzoom fix.
+
+ When the VAE input is disconnected from TextEncodeQwenImageEditPlus,
+ the node no longer creates reference latents (and no longer forces
+ a 1 MP rescale). We replicate the reference-latent injection
+ manually so the latents match the actual output resolution.
+
+ For each active image (image1 / image2 / image3) still present in
+ the positive text-encode node, a VAEEncode node is created and a
+ ReferenceLatent node is chained onto both positive and negative
+ conditioning. The KSampler inputs are then updated to point at
+ the tail of the chain.
+
+ When ``qwen_timestep_zero_ref`` is enabled on the scene, a
+ FluxKontextMultiReferenceLatentMethod node is appended with
+ ``index_timestep_zero`` so the model treats reference latents as
+ fully-denoised tokens, reducing color shift / over-saturation.
+ """
+ pos_node = NODES['pos_prompt']
+ neg_node = NODES['neg_prompt']
+ sampler_node = NODES['sampler']
+ vae_ref = ["4", 0] # VAELoader node
+
+ # Collect active image references from the pos text-encode node.
+ # image1 is always present; image2/image3 may have been deleted.
+ active_refs = []
+ pos_inputs = prompt[pos_node]['inputs']
+ for key in ('image1', 'image2', 'image3'):
+ if key in pos_inputs:
+ active_refs.append(pos_inputs[key])
+
+ if not active_refs:
+ return
+
+ node_id = start_node_id
+ # Avoid collisions with existing dynamic nodes
+ while str(node_id) in prompt:
+ node_id += 1
+
+ pos_cond_ref = [pos_node, 0]
+ neg_cond_ref = [neg_node, 0]
+
+ for i, img_ref in enumerate(active_refs):
+ # --- VAEEncode for this image ---
+ # For image1 we can reuse the existing node 8 which already
+ # encodes the same image (also used as KSampler latent_image).
+ if i == 0:
+ vae_enc_ref = ["8", 0]
+ else:
+ vae_enc_id = str(node_id)
+ node_id += 1
+ prompt[vae_enc_id] = {
+ "inputs": {
+ "pixels": img_ref,
+ "vae": vae_ref
+ },
+ "class_type": "VAEEncode",
+ "_meta": {"title": f"VAE Encode (Ref {i + 1})"}
+ }
+ vae_enc_ref = [vae_enc_id, 0]
+
+ # --- ReferenceLatent for positive ---
+ ref_pos_id = str(node_id)
+ node_id += 1
+ prompt[ref_pos_id] = {
+ "inputs": {
+ "conditioning": pos_cond_ref,
+ "latent": vae_enc_ref
+ },
+ "class_type": "ReferenceLatent",
+ "_meta": {"title": f"Reference Latent Pos {i + 1}"}
+ }
+ pos_cond_ref = [ref_pos_id, 0]
+
+ # --- ReferenceLatent for negative ---
+ ref_neg_id = str(node_id)
+ node_id += 1
+ prompt[ref_neg_id] = {
+ "inputs": {
+ "conditioning": neg_cond_ref,
+ "latent": vae_enc_ref
+ },
+ "class_type": "ReferenceLatent",
+ "_meta": {"title": f"Reference Latent Neg {i + 1}"}
+ }
+ neg_cond_ref = [ref_neg_id, 0]
+
+ # Point KSampler at the final chained conditioning
+ prompt[sampler_node]['inputs']['positive'] = pos_cond_ref
+ prompt[sampler_node]['inputs']['negative'] = neg_cond_ref
+
+ # ── Timestep-zero reference method (colour-shift reduction) ─────
+ context = bpy.context
+ if getattr(context.scene, 'qwen_timestep_zero_ref', False):
+ # Positive conditioning
+ tz_pos_id = str(node_id)
+ node_id += 1
+ prompt[tz_pos_id] = {
+ "inputs": {
+ "conditioning": pos_cond_ref,
+ "reference_latents_method": "index_timestep_zero"
+ },
+ "class_type": "FluxKontextMultiReferenceLatentMethod",
+ "_meta": {"title": "Ref Method (Pos)"}
+ }
+ prompt[sampler_node]['inputs']['positive'] = [tz_pos_id, 0]
+
+ # Negative conditioning
+ tz_neg_id = str(node_id)
+ node_id += 1
+ prompt[tz_neg_id] = {
+ "inputs": {
+ "conditioning": neg_cond_ref,
+ "reference_latents_method": "index_timestep_zero"
+ },
+ "class_type": "FluxKontextMultiReferenceLatentMethod",
+ "_meta": {"title": "Ref Method (Neg)"}
+ }
+ prompt[sampler_node]['inputs']['negative'] = [tz_neg_id, 0]
+
+ # ------------------------------------------------------------------
+ # FLUX.2 Klein – build ReferenceLatent chain for multi-reference editing
+ # ------------------------------------------------------------------
+ def _build_flux2_reference_chain(self, prompt, NODES, ref_image_infos, start_node_id=700):
+ """Build a ReferenceLatent chain for FLUX.2 Klein (based on official workflow).
+
+ For each reference image, creates:
+ LoadImage (already present) → VAEEncode
+ → ReferenceLatent (positive conditioning)
+ → ReferenceLatent (negative conditioning)
+
+ The ReferenceLatent nodes are chained so each successive reference
+ appends to the previous one. The final modified positive and negative
+ conditioning outputs are wired into CFGGuider.
+
+ Args:
+ prompt: The ComfyUI prompt dict (modified in-place).
+ NODES: Node-ID mapping dict.
+ ref_image_infos: List of dicts, each with at least a ``loader_node``
+ key pointing to the LoadImage node ID.
+ start_node_id: First dynamic node ID to use.
+ """
+ if not ref_image_infos:
+ return
+
+ pos_node = NODES['pos_prompt']
+ neg_node = NODES['neg_prompt']
+ guider_node = NODES['guider']
+ vae_ref = [NODES['vae_loader'], 0]
+
+ node_id = start_node_id
+ while str(node_id) in prompt:
+ node_id += 1
+
+ pos_cond_ref = [pos_node, 0]
+ neg_cond_ref = [neg_node, 0]
+
+ for i, ref_info in enumerate(ref_image_infos):
+ loader_node = ref_info['loader_node']
+
+ # --- VAEEncode (reference image → latent) ---
+ vae_enc_id = str(node_id); node_id += 1
+ prompt[vae_enc_id] = {
+ "inputs": {
+ "pixels": [loader_node, 0],
+ "vae": vae_ref
+ },
+ "class_type": "VAEEncode",
+ "_meta": {"title": f"VAE Encode Ref {i + 1}"}
+ }
+
+ # --- ReferenceLatent (positive) ---
+ ref_pos_id = str(node_id); node_id += 1
+ prompt[ref_pos_id] = {
+ "inputs": {
+ "conditioning": pos_cond_ref,
+ "latent": [vae_enc_id, 0]
+ },
+ "class_type": "ReferenceLatent",
+ "_meta": {"title": f"Reference Latent Pos {i + 1}"}
+ }
+ pos_cond_ref = [ref_pos_id, 0]
+
+ # --- ReferenceLatent (negative) ---
+ ref_neg_id = str(node_id); node_id += 1
+ prompt[ref_neg_id] = {
+ "inputs": {
+ "conditioning": neg_cond_ref,
+ "latent": [vae_enc_id, 0]
+ },
+ "class_type": "ReferenceLatent",
+ "_meta": {"title": f"Reference Latent Neg {i + 1}"}
+ }
+ neg_cond_ref = [ref_neg_id, 0]
+
+ # Wire final pos/neg conditioning into CFGGuider
+ prompt[guider_node]['inputs']['positive'] = pos_cond_ref
+ prompt[guider_node]['inputs']['negative'] = neg_cond_ref
+
+ def _get_klein_default_prompts(self, context, is_initial_image):
+ """Gets the default Klein prompts based on the current context.
+
+ Follows BFL's official Klein prompting guide: write like a novelist,
+ front-load the subject, reference image numbers explicitly, and
+ describe the desired transformation rather than the image contents.
+ Camera prompts are placed after the main prompt via {camera_suffix}
+ to reduce their weighting (Klein pays more attention to what comes
+ first).
+ """
+ is_generating = hasattr(self.operator, '_current_image')
+ is_subsequent_in_sequence = is_generating and self.operator._current_image > 0
+
+ if is_initial_image:
+ style_image_provided = context.scene.qwen_use_external_style_image
+ else:
+ 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:
+ if not style_image_provided:
+ return "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour."
+ else:
+ return "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour, adopting the visual style from image 2."
+ else:
+ if context_mode == 'ADDITIONAL':
+ return "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray. Image 3 represents the overall style."
+ elif context_mode == 'REPLACE_STYLE':
+ return "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray."
+ else:
+ if not style_image_provided:
+ return "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour."
+ else:
+ return "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour, adopting the visual style from image 2."
+
+ def _get_qwen_default_prompts(self, context, is_initial_image):
+ """Gets the default Qwen prompts based on the current context."""
+ # Check if we are in a real generation context vs. just resetting a prompt
+ is_generating = hasattr(self.operator, '_current_image')
+ is_subsequent_in_sequence = is_generating and self.operator._current_image > 0
+
+ # TRELLIS.2 style reference counts as a style image for prompt selection.
+ _t2_style = (
+ getattr(context.scene, 'architecture_mode', '') == 'trellis2'
+ and getattr(context.scene, 'qwen_use_trellis2_style', False)
+ )
+
+ if is_initial_image:
+ # First frame has a style reference from an external image or the TRELLIS.2 input.
+ style_image_provided = (
+ context.scene.qwen_use_external_style_image
+ or _t2_style
+ )
+ else:
+ # Later frames can draw style from previous renders, external sources,
+ # context renders, or the TRELLIS.2 input (when not initial-only).
+ 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'} or
+ (_t2_style and not getattr(context.scene, 'qwen_trellis2_style_initial_only', False))
+ )
+ context_mode = context.scene.qwen_context_render_mode
+
+ # Optional sentence injected into context-render prompts.
+ gray_bg = " Replace the background with solid gray." if getattr(context.scene, 'qwen_prompt_gray_background', True) else ""
+
+ if is_initial_image:
+ if not style_image_provided:
+ return "Change the format of image 1 to '{main_prompt}'"
+ else:
+ return "Change and transfer the format of '{main_prompt}' in image 1 to the style from image 2"
+ else: # Subsequent image
+ if context_mode == 'ADDITIONAL':
+ return f"Change and transfer the format of image 1 to '{{main_prompt}}'. Replace all solid magenta areas in image 2.{gray_bg} The style from image 2 should smoothly continue into the previously magenta areas. Image 3 represents the overall style of the object."
+ elif context_mode == 'REPLACE_STYLE':
+ return f"Change and transfer the format of image 1 to '{{main_prompt}}'. Replace all solid magenta areas in image 2.{gray_bg} The style from image 2 should smoothly continue into the previously magenta areas."
+ else: # NONE or other cases
+ if not style_image_provided:
+ return "Change the format of image 1 to '{main_prompt}'"
+ else:
+ return "Change and transfer the format of '{main_prompt}' in image 1 to the style from image 2"
+
+
+ def generate_qwen_refine(self, context, camera_id=None):
+ """Generates an image using the Qwen-Image-Edit workflow for refinement."""
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ client_id = str(uuid.uuid4())
+ revision_dir = get_generation_dirs(context)["revision"]
+
+ prompt = json.loads(prompt_text_qwen_image_edit)
+
+ NODES = {
+ 'sampler': "1",
+ 'save_image': "5",
+ 'model_sampler': "6", # ModelSamplingAuraFlow
+ 'cfg_norm': "7", # CFGNorm
+ 'vae_encode': "8",
+ 'pos_prompt': "12",
+ 'neg_prompt': "11",
+ 'unet_loader': "13",
+ 'guidance_map_loader': "14", # Image 1 (structure)
+ 'style_map_loader': "15", # Image 2 (style)
+ 'context_render_loader': "16", # Image 3 (context render)
+ }
+
+ # --- Build LoRA chain ---
+ initial_model_input = [NODES['unet_loader'], 0]
+ dummy_clip_input = [NODES['unet_loader'], 0]
+
+ is_nunchaku = context.scene.model_name.lower().endswith('.safetensors')
+ lora_class = "NunchakuQwenImageLoraLoader" if is_nunchaku else "LoraLoaderModelOnly"
+
+ prompt, final_lora_model_out, _ = self._build_lora_chain(
+ prompt, context,
+ initial_model_input, dummy_clip_input,
+ start_node_id=500,
+ lora_class_type=lora_class
+ )
+
+ prompt[NODES['model_sampler']]['inputs']['model'] = final_lora_model_out
+
+ # --- Configure Inputs ---
+ # Image 1: The current render (structure)
+ render_info = self.operator._get_uploaded_image_info(context, "inpaint", subtype="render", camera_id=camera_id)
+ if not render_info:
+ self.operator._error = f"Could not find or upload render for camera {camera_id}."
+ return {"error": "conn_failed"}
+ prompt[NODES['guidance_map_loader']]['inputs']['image'] = render_info['name']
+
+ # --- Configure Style Image (Image 2) ---
+ use_prev_ref = context.scene.qwen_refine_use_prev_ref
+ style_image_info = None
+
+ if use_prev_ref and camera_id > 0:
+ # Use previous generated image
+ style_image_info = self.operator._get_uploaded_image_info(context, "generated", camera_id=camera_id - 1, material_id=self.operator._material_id)
+
+ if style_image_info:
+ prompt[NODES['style_map_loader']]['inputs']['image'] = style_image_info['name']
+ else:
+ # Fallback to external style image if configured
+ if context.scene.qwen_use_external_style_image:
+ style_image_info = self.operator._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(context.scene.qwen_external_style_image))
+ if style_image_info:
+ prompt[NODES['style_map_loader']]['inputs']['image'] = style_image_info['name']
+
+ # Fallback to TRELLIS.2 input image as style
+ if not style_image_info and getattr(context.scene, 'architecture_mode', '') == 'trellis2' and getattr(context.scene, 'qwen_use_trellis2_style', False):
+ t2_path = getattr(context.scene, 'trellis2_last_input_image', '')
+ if t2_path and os.path.exists(bpy.path.abspath(t2_path)):
+ style_image_info = self.operator._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(t2_path))
+ if style_image_info:
+ prompt[NODES['style_map_loader']]['inputs']['image'] = style_image_info['name']
+
+ # If still no style image, remove Image 2 inputs
+ if not style_image_info:
+ del prompt[NODES['style_map_loader']]
+ del prompt[NODES['pos_prompt']]['inputs']['image2']
+ del prompt[NODES['neg_prompt']]['inputs']['image2']
+
+ # --- Configure Depth Map (Image 3) ---
+ use_depth = context.scene.qwen_refine_use_depth
+ depth_info = None
+ if use_depth:
+ depth_info = self.operator._get_uploaded_image_info(context, "controlnet", subtype="depth", camera_id=camera_id)
+
+ if depth_info:
+ prompt[NODES['context_render_loader']]['inputs']['image'] = depth_info['name']
+ else:
+ # Remove Context Render (Image 3) if not used
+ del prompt[NODES['context_render_loader']]
+ del prompt[NODES['pos_prompt']]['inputs']['image3']
+ del prompt[NODES['neg_prompt']]['inputs']['image3']
+
+ # --- Prompt ---
+ user_prompt = _texturing_prompt(context.scene)
+ final_prompt = f"Modify image1 to {user_prompt}"
+ if depth_info:
+ final_prompt += ". Use image3 as depth map reference."
+
+ prompt[NODES['pos_prompt']]['inputs']['prompt'] = final_prompt
+
+ # --- Reference Latent chain (Qwen unzoom fix) ---
+ self._build_qwen_reference_latent_chain(prompt, NODES)
+
+ # --- Save and Execute ---
+ self._save_prompt_to_file(prompt, revision_dir)
+
+ ws = self._connect_to_websocket(server_address, client_id)
+ if ws is None:
+ return {"error": "conn_failed"}
+
+ images = None
+ try:
+ images = self._execute_prompt_and_get_images(ws, prompt, client_id, server_address, NODES)
+ finally:
+ if ws:
+ ws.close()
+
+ if images is None or isinstance(images, dict) and "error" in images:
+ return {"error": "conn_failed"}
+
+ return images[NODES['save_image']][0]
+
+ def generate_qwen_edit(self, context, camera_id=None):
+ """Generates an image using the Qwen-Image-Edit workflow."""
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ client_id = str(uuid.uuid4())
+ revision_dir = get_generation_dirs(context)["revision"]
+
+ prompt = json.loads(prompt_text_qwen_image_edit)
+
+ NODES = {
+ 'sampler': "1",
+ 'save_image': "5",
+ 'model_sampler': "6", # ModelSamplingAuraFlow
+ 'cfg_norm': "7", # CFGNorm
+ 'vae_encode': "8",
+ 'pos_prompt': "12",
+ 'neg_prompt': "11",
+ 'unet_loader': "13",
+ 'guidance_map_loader': "14", # Image 1 (structure)
+ 'style_map_loader': "15", # Image 2 (style)
+ 'context_render_loader': "16", # Image 3 (context render)
+ }
+
+ # --- Build LoRA chain ---
+ # The Qwen workflow uses model-only LoRAs. We can reuse the existing
+ # chain builder by providing a dummy CLIP input that won't be used.
+ initial_model_input = [NODES['unet_loader'], 0]
+ dummy_clip_input = [NODES['unet_loader'], 0] # Dummy, not used by LoraLoaderModelOnly
+
+ is_nunchaku = context.scene.model_name.lower().endswith('.safetensors')
+ lora_class = "NunchakuQwenImageLoraLoader" if is_nunchaku else "LoraLoaderModelOnly"
+
+ prompt, final_lora_model_out, _ = self._build_lora_chain(
+ prompt, context,
+ initial_model_input, dummy_clip_input,
+ start_node_id=500, # Use a high starting ID to avoid conflicts
+ lora_class_type=lora_class # Specify model-only loader
+ )
+
+ # Connect the output of the LoRA chain to the next node in the model path
+ prompt[NODES['model_sampler']]['inputs']['model'] = final_lora_model_out
+
+
+ # --- Configure Inputs ---
+ guidance_map_type = context.scene.qwen_guidance_map_type
+ guidance_map_info = self.operator._get_uploaded_image_info(context, "controlnet", subtype=guidance_map_type, camera_id=camera_id)
+ if not guidance_map_info:
+ self.operator._error = f"Could not find or upload {guidance_map_type} map for camera {camera_id}."
+ return {"error": "conn_failed"}
+ prompt[NODES['guidance_map_loader']]['inputs']['image'] = guidance_map_info['name']
+
+ # --- Configure Style Image (Image 2) and Prompts ---
+ user_prompt = _texturing_prompt(context.scene)
+ style_image_info = None
+ context_render_info = None
+ context_mode = context.scene.qwen_context_render_mode
+ 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
+ # Prepend the view description
+ user_prompt = f"{view_desc}, {user_prompt}"
+
+ # --- Handle Context Render (Image 3) ---
+ # This is only active in sequential mode after the first image
+ is_initial_image = not self.operator._current_image > 0
+ if not is_initial_image and context_mode != 'NONE':
+ context_render_info = self.operator._get_uploaded_image_info(context, "inpaint", subtype="render", camera_id=camera_id)
+ if not context_render_info:
+ self.operator._error = f"Qwen context render enabled, but could not find context render for camera {camera_id}."
+ return {"error": "conn_failed"}
+
+ if context_mode == 'ADDITIONAL':
+ # Switch context loader and style loader ids
+ NODES['context_render_loader'], NODES['style_map_loader'] = NODES['style_map_loader'], NODES['context_render_loader']
+ prompt[NODES['context_render_loader']]['inputs']['image'] = context_render_info['name']
+ # The prompt needs to reference image 3
+ if context.scene.qwen_use_custom_prompts:
+ pos_prompt_text = context.scene.qwen_custom_prompt_seq_additional.format(main_prompt=user_prompt)
+ else:
+ pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
+ prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
+ # If mode is REPLACE_STYLE, we will handle it in the style image section below.
+ else:
+ remove_context = True
+ else:
+ remove_context = True
+
+ if remove_context:
+ del prompt[NODES['context_render_loader']]
+ del prompt[NODES['pos_prompt']]['inputs']['image3']
+ del prompt[NODES['neg_prompt']]['inputs']['image3']
+
+
+ # --- Handle Style Image (Image 2) ---
+ # Determine if we should use the external style image for this specific frame
+ use_external_this_frame = context.scene.qwen_use_external_style_image
+ if use_external_this_frame and not is_initial_image and context.scene.qwen_external_style_initial_only:
+ # If it's a subsequent image AND the "initial only" flag is set, DON'T use the external image.
+ use_external_this_frame = False
+ context.scene.sequential_ipadapter = True # Force using previous image as style
+
+ # Case 1: External Style Image for this frame
+ if use_external_this_frame:
+ style_image_info = self.operator._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(context.scene.qwen_external_style_image))
+ 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"}
+ 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)
+ else:
+ pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
+ prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
+ else: # Additional mode
+ if context.scene.qwen_use_custom_prompts:
+ pos_prompt_text = (context.scene.qwen_custom_prompt_seq_additional if not is_initial_image else context.scene.qwen_custom_prompt_initial).format(main_prompt=user_prompt)
+ else:
+ pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
+ prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
+
+ # Case 1b: TRELLIS.2 input image as style (when no external style configured)
+ elif getattr(context.scene, 'architecture_mode', '') == 'trellis2' and getattr(context.scene, 'qwen_use_trellis2_style', False):
+ use_t2_this_frame = True
+ if not is_initial_image and getattr(context.scene, 'qwen_trellis2_style_initial_only', False):
+ # Initial-only mode: fall back to sequential style for subsequent frames
+ use_t2_this_frame = False
+ context.scene.sequential_ipadapter = True
+ if use_t2_this_frame:
+ t2_path = getattr(context.scene, 'trellis2_last_input_image', '')
+ if t2_path and os.path.exists(bpy.path.abspath(t2_path)):
+ style_image_info = self.operator._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(t2_path))
+ if style_image_info:
+ if context.scene.qwen_use_custom_prompts:
+ pos_prompt_text = (context.scene.qwen_custom_prompt_initial).format(main_prompt=user_prompt)
+ else:
+ pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
+ prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
+
+ # Case 2: Sequential generation (after first image)
+ elif not is_initial_image:
+ if context_mode == 'REPLACE_STYLE':
+ # The context render becomes the style image
+ style_image_info = context_render_info
+ if context.scene.qwen_use_custom_prompts:
+ pos_prompt_text = context.scene.qwen_custom_prompt_seq_replace.format(main_prompt=user_prompt)
+ else:
+ pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
+ prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
+ elif context.scene.sequential_ipadapter: # Use previous generated image
+ if context.scene.sequential_ipadapter_mode == 'original_render':
+ # For original_render in Qwen context, fall back to first mode since
+ # the render is already used as the guidance map (Image 1).
+ ref_cam_id = 0
+ else:
+ ref_cam_id = 0 if context.scene.sequential_ipadapter_mode == 'first' else self.operator._current_image - 1
+ style_image_info = self.operator._get_uploaded_image_info(context, "generated", camera_id=ref_cam_id, material_id=self.operator._material_id)
+ if not style_image_info:
+ self.operator._error = f"Sequential mode error: Could not find previous image for camera {ref_cam_id} to use as style."
+ return {"error": "conn_failed"}
+ if context_mode != 'ADDITIONAL':
+ if context.scene.qwen_use_custom_prompts:
+ pos_prompt_text = context.scene.qwen_custom_prompt_seq_none.format(main_prompt=user_prompt)
+ else:
+ pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
+ prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
+ # If neither of the above, style_image_info remains None, handled below
+
+ # Case 3: First image of a sequence, or separate generation, or no style source in sequential
+ if style_image_info is None:
+ # No style image is provided — remove the style loader node and
+ # its reference from the prompt encoders.
+ del prompt[NODES['style_map_loader']]
+ if context_mode == 'ADDITIONAL' and not is_initial_image:
+ # In ADDITIONAL mode the context/style loader IDs were swapped
+ # above, so the style loader now sits on the node that image3
+ # originally pointed to. Remove image3 (dangling after the
+ # delete) but keep image2 (the context render).
+ for nid in (NODES['pos_prompt'], NODES['neg_prompt']):
+ prompt[nid]['inputs'].pop('image3', None)
+ else:
+ del prompt[NODES['pos_prompt']]['inputs']['image2']
+ del prompt[NODES['neg_prompt']]['inputs']['image2']
+ 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)
+ else:
+ pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
+ prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
+ else:
+ # A style image is provided, so set the loader input.
+ prompt[NODES['style_map_loader']]['inputs']['image'] = style_image_info['name']
+ # External user-selected images can be arbitrarily large (10+ MP).
+ # Scale them down to ~1 MP so the VAEEncode in the reference
+ # latent chain doesn't blow up VRAM. StableGen's own renders
+ # are already roughly 1 MP and don't need this.
+ if use_external_this_frame:
+ 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,
+ "resolution_steps": 1,
+ "image": [NODES['style_map_loader'], 0]
+ },
+ "class_type": "ImageScaleToTotalPixels",
+ "_meta": {
+ "title": "Scale External Style to 1MP"
+ }
+ }
+ # 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]
+
+ # --- Reference Latent chain (Qwen unzoom fix) ---
+ self._build_qwen_reference_latent_chain(prompt, NODES)
+
+ # --- Configure Sampler ---
+ 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 # Typically 1.0 for this kind of edit
+
+ # --- Set UNET model ---
+ 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
+
+ # --- Execute ---
+ self._save_prompt_to_file(prompt, revision_dir)
+ ws = self._connect_to_websocket(server_address, client_id)
+ if ws is None:
+ return {"error": "conn_failed"}
+
+ images = None
+ try:
+ images = self._execute_prompt_and_get_images(ws, prompt, client_id, server_address, NODES)
+ finally:
+ if ws:
+ ws.close()
+
+ if images is None or (isinstance(images, dict) and "error" in images):
+ return {"error": "conn_failed"}
+
+ print(f"[StableGen] Qwen image generated with prompt: {prompt[NODES['pos_prompt']]['inputs']['prompt']}")
+ return images[NODES['save_image']][0]
+
+ # ------------------------------------------------------------------
+ # FLUX.2 Klein – multi-reference edit generation
+ # ------------------------------------------------------------------
+ def generate_flux2_klein(self, context, camera_id=None):
+ """Generates an image using the FLUX.2 Klein multi-reference edit workflow.
+
+ Klein injects reference images via ReferenceLatent chains and
+ understands image references ("image 1", "image 2") in prompts.
+ Following BFL's official prompting guide, prompts are written as
+ natural prose describing the desired transformation, front-loading
+ the subject and referencing images by number.
+
+ Reference layout:
+ Ref 1 → structure / guidance map (depth, normal, workbench …)
+ Ref 2 → style reference (external image or prev gen)
+ Ref 3 → context render (sequential mode only)
+ """
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ client_id = str(uuid.uuid4())
+ revision_dir = get_generation_dirs(context)["revision"]
+
+ prompt = json.loads(prompt_text_flux2_klein)
+
+ NODES = {
+ 'unet_loader': "1",
+ 'clip_loader': "2",
+ 'vae_loader': "3",
+ 'pos_prompt': "4", # CLIPTextEncode
+ 'neg_prompt': "5", # CLIPTextEncode (empty)
+ 'empty_latent': "6", # EmptyFlux2LatentImage
+ 'scheduler': "7", # Flux2Scheduler
+ 'ksampler': "8", # KSamplerSelect
+ 'noise': "9", # RandomNoise
+ 'guider': "10", # CFGGuider
+ 'sampler': "11", # SamplerCustomAdvanced
+ 'vae_decode': "12", # VAEDecode
+ 'save_image': "13", # SaveImageWebsocket
+ 'ref1_loader': "14", # LoadImage – structure (guidance map)
+ 'ref2_loader': "15", # LoadImage – style
+ 'ref3_loader': "16", # LoadImage – context render
+ }
+
+ # --- Resolution (must be multiple of 16 for FLUX.2 latent) ---
+ if context.scene.generation_method == 'grid':
+ width = self.operator._grid_width
+ height = self.operator._grid_height
+ else:
+ width = context.scene.render.resolution_x
+ height = context.scene.render.resolution_y
+ width = (width // 16) * 16
+ height = (height // 16) * 16
+ prompt[NODES['empty_latent']]['inputs']['width'] = width
+ prompt[NODES['empty_latent']]['inputs']['height'] = height
+ prompt[NODES['scheduler']]['inputs']['width'] = width
+ prompt[NODES['scheduler']]['inputs']['height'] = height
+
+ # --- Sampler settings ---
+ prompt[NODES['noise']]['inputs']['noise_seed'] = context.scene.seed
+ prompt[NODES['scheduler']]['inputs']['steps'] = context.scene.steps
+ prompt[NODES['ksampler']]['inputs']['sampler_name'] = context.scene.sampler
+ prompt[NODES['guider']]['inputs']['cfg'] = context.scene.cfg
+
+ # --- Model ---
+ prompt[NODES['unet_loader']]['inputs']['unet_name'] = context.scene.model_name
+
+ # --- CLIP: Detect 9B vs 4B from model name ---
+ # 9B uses a larger Qwen 3 8B text encoder instead of 4B.
+ # Default to fp8mixed (8.66 GB) for practical VRAM usage.
+ # Full-precision qwen_3_8b.safetensors (16.4 GB) is also compatible.
+ # Download from: Comfy-Org/vae-text-encorder-for-flux-klein-9b
+ model_lower = context.scene.model_name.lower()
+ if '9b' in model_lower:
+ prompt[NODES['clip_loader']]['inputs']['clip_name'] = "qwen_3_8b_fp8mixed.safetensors"
+
+ # --- 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}"
+
+ # ─── Collect reference images ────────────────────────────────
+ ref_images = []
+ remove_ref1 = True
+ remove_ref2 = True
+ remove_ref3 = True
+
+ is_initial_image = not self.operator._current_image > 0
+ context_mode = context.scene.qwen_context_render_mode
+ context_render_info = None
+ style_image_info = None
+
+ # --- Ref 1: Structure / guidance map (always attempt) ---
+ guidance_map_type = context.scene.qwen_guidance_map_type
+ guidance_map_info = self.operator._get_uploaded_image_info(
+ context, "controlnet", subtype=guidance_map_type, camera_id=camera_id)
+ if guidance_map_info:
+ prompt[NODES['ref1_loader']]['inputs']['image'] = guidance_map_info['name']
+ ref_images.append({'loader_node': NODES['ref1_loader']})
+ remove_ref1 = False
+
+ # --- Ref 3: Context render (sequential subsequent only) ---
+ # Handled BEFORE Ref 2 so ADDITIONAL mode can swap slot ids (mirrors Qwen).
+ remove_context = True
+ if not is_initial_image and context_mode != 'NONE':
+ context_render_info = self.operator._get_uploaded_image_info(
+ context, "inpaint", subtype="render", camera_id=camera_id)
+ if not context_render_info:
+ self.operator._error = f"Klein context render enabled, but could not find context render for camera {camera_id}."
+ return {"error": "conn_failed"}
+ if context_render_info and context_mode == 'ADDITIONAL':
+ # Swap loaders so context render is Ref 2 and style is Ref 3
+ NODES['ref3_loader'], NODES['ref2_loader'] = NODES['ref2_loader'], NODES['ref3_loader']
+ prompt[NODES['ref3_loader']]['inputs']['image'] = context_render_info['name']
+ ref_images.append({'loader_node': NODES['ref3_loader']})
+ remove_context = False
+ if context.scene.qwen_use_custom_prompts:
+ pos_prompt_text = context.scene.qwen_custom_prompt_seq_additional.format(main_prompt=user_prompt, camera_suffix=camera_suffix)
+ else:
+ pos_prompt_text = self._get_klein_default_prompts(context, is_initial_image).format(main_prompt=user_prompt, camera_suffix=camera_suffix)
+ prompt[NODES['pos_prompt']]['inputs']['text'] = pos_prompt_text
+
+ if remove_context and not remove_ref3:
+ pass # already assigned
+ elif remove_context:
+ pass # will be deleted below
+
+ # --- Ref 2: Style reference (optional) ---
+ # Determine if we should use the external style image for this frame
+ use_external = context.scene.qwen_use_external_style_image
+ if use_external and not is_initial_image and context.scene.qwen_external_style_initial_only:
+ use_external = False
+ context.scene.sequential_ipadapter = True
+
+ # Case 1: External Style Image
+ if use_external:
+ style_image_info = self.operator._get_uploaded_image_info(
+ context, "custom",
+ filename=bpy.path.abspath(context.scene.qwen_external_style_image))
+ if style_image_info and 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, camera_suffix=camera_suffix)
+ else:
+ pos_prompt_text = self._get_klein_default_prompts(context, is_initial_image).format(main_prompt=user_prompt, camera_suffix=camera_suffix)
+ prompt[NODES['pos_prompt']]['inputs']['text'] = pos_prompt_text
+
+ # Case 1b: TRELLIS.2 input image as style
+ if not style_image_info and getattr(context.scene, 'architecture_mode', '') == 'trellis2' and getattr(context.scene, 'qwen_use_trellis2_style', False):
+ use_t2 = True
+ if not is_initial_image and getattr(context.scene, 'qwen_trellis2_style_initial_only', False):
+ use_t2 = False
+ context.scene.sequential_ipadapter = True
+ if use_t2:
+ t2_path = getattr(context.scene, 'trellis2_last_input_image', '')
+ if t2_path and os.path.exists(bpy.path.abspath(t2_path)):
+ style_image_info = self.operator._get_uploaded_image_info(
+ context, "custom", filename=bpy.path.abspath(t2_path))
+ if style_image_info:
+ if context.scene.qwen_use_custom_prompts:
+ pos_prompt_text = context.scene.qwen_custom_prompt_initial.format(main_prompt=user_prompt, camera_suffix=camera_suffix)
+ else:
+ pos_prompt_text = self._get_klein_default_prompts(context, is_initial_image).format(main_prompt=user_prompt, camera_suffix=camera_suffix)
+ prompt[NODES['pos_prompt']]['inputs']['text'] = pos_prompt_text
+
+ # Case 2: Sequential (after first image)
+ if not style_image_info and not is_initial_image:
+ if context_mode == 'REPLACE_STYLE':
+ style_image_info = context_render_info
+ if context.scene.qwen_use_custom_prompts:
+ pos_prompt_text = context.scene.qwen_custom_prompt_seq_replace.format(main_prompt=user_prompt, camera_suffix=camera_suffix)
+ else:
+ pos_prompt_text = self._get_klein_default_prompts(context, is_initial_image).format(main_prompt=user_prompt, camera_suffix=camera_suffix)
+ prompt[NODES['pos_prompt']]['inputs']['text'] = pos_prompt_text
+ elif context.scene.sequential_ipadapter:
+ if context.scene.sequential_ipadapter_mode == 'original_render':
+ # For original_render in Klein context, fall back to first mode since
+ # the render is already used as the guidance map (Ref 1).
+ ref_cam = 0
+ else:
+ ref_cam = (0 if context.scene.sequential_ipadapter_mode == 'first'
+ else self.operator._current_image - 1)
+ style_image_info = self.operator._get_uploaded_image_info(
+ context, "generated", camera_id=ref_cam,
+ material_id=self.operator._material_id)
+ if context_mode != 'ADDITIONAL':
+ if context.scene.qwen_use_custom_prompts:
+ pos_prompt_text = context.scene.qwen_custom_prompt_seq_none.format(main_prompt=user_prompt, camera_suffix=camera_suffix)
+ else:
+ pos_prompt_text = self._get_klein_default_prompts(context, is_initial_image).format(main_prompt=user_prompt, camera_suffix=camera_suffix)
+ prompt[NODES['pos_prompt']]['inputs']['text'] = pos_prompt_text
+
+ # Case 3: No style image – first image or no style source
+ if style_image_info is None:
+ 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, camera_suffix=camera_suffix)
+ else:
+ pos_prompt_text = self._get_klein_default_prompts(context, is_initial_image).format(main_prompt=user_prompt, camera_suffix=camera_suffix)
+ prompt[NODES['pos_prompt']]['inputs']['text'] = pos_prompt_text
+ else:
+ prompt[NODES['ref2_loader']]['inputs']['image'] = style_image_info['name']
+ ref_images.append({'loader_node': NODES['ref2_loader']})
+ remove_ref2 = False
+
+ # Remove unused LoadImage nodes
+ if remove_ref1:
+ del prompt[NODES['ref1_loader']]
+ if remove_ref2:
+ del prompt[NODES['ref2_loader']]
+ if remove_context:
+ del prompt[NODES['ref3_loader']]
+
+ # --- Build ReferenceLatent chain ---
+ self._build_flux2_reference_chain(prompt, NODES, ref_images)
+
+ # --- Execute ---
+ self._save_prompt_to_file(prompt, revision_dir)
+ ws = self._connect_to_websocket(server_address, client_id)
+ if ws is None:
+ return {"error": "conn_failed"}
+
+ images = None
+ try:
+ images = self._execute_prompt_and_get_images(
+ ws, prompt, client_id, server_address, NODES)
+ finally:
+ if ws:
+ ws.close()
+
+ if images is None or (isinstance(images, dict) and "error" in images):
+ return {"error": "conn_failed"}
+
+ print(f"[StableGen] FLUX.2 Klein image generated with prompt: {prompt[NODES['pos_prompt']]['inputs']['text']}")
+ return images[NODES['save_image']][0]
+
+
+ def generate(self, context, controlnet_info=None, ipadapter_ref_info=None):
+ """
+ Generates the image using ComfyUI.
+ :param context: Blender context.
+ :param controlnet_info: Dict of uploaded controlnet image info.
+ :param ipadapter_ref_info: Uploaded IPAdapter reference image info.
+ :return: Generated image binary data.
+ """
+
+ # Setup connection parameters
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ client_id = str(uuid.uuid4())
+ # Get revision dir for debug file
+ revision_dir = get_generation_dirs(context)["revision"]
+
+ # Initialize the prompt template and get node mappings
+ prompt, NODES = self._create_base_prompt(context)
+
+ # Set model resolution
+ self._configure_resolution(prompt, context, NODES)
+
+ if ipadapter_ref_info:
+ # Configure IPAdapter settings
+ self._configure_ipadapter(prompt, context, ipadapter_ref_info, NODES)
+ else:
+ # Remove IPAdapter nodes if not used
+ for node_id in ['235', '236', '237']:
+ if node_id in prompt:
+ del prompt[node_id]
+
+ # Build controlnet chain
+ prompt = self._build_controlnet_chain(prompt, context, controlnet_info, NODES)
+
+ # Save prompt for debugging (in revision dir)
+ self._save_prompt_to_file(prompt, revision_dir)
+
+ # Execute generation and get results
+ ws = self._connect_to_websocket(server_address, client_id)
+
+ if ws is None:
+ return {"error": "conn_failed"} # Connection error
+
+ images = None
+ try:
+ images = self._execute_prompt_and_get_images(ws, prompt, client_id, server_address, NODES)
+ finally:
+ if ws:
+ ws.close()
+
+ if images is None or isinstance(images, dict) and "error" in images:
+ return {"error": "conn_failed"}
+
+ print(f"[StableGen] Image generated with prompt: {context.scene.comfyui_prompt}")
+
+ # Return the generated image from the save_image node
+ return images[NODES['save_image']][0]
+
+ def generate_txt2img(self, context, seed_override=None):
+ """Generate a single image from the scene prompt and checkpoint.
+
+ Builds a minimal txt2img ComfyUI workflow (with LoRA support) and
+ returns the generated image as raw PNG bytes delivered via the
+ ``SaveImageWebsocket`` node.
+
+ Args:
+ seed_override: If not None, overrides ``scene.seed`` in the
+ built workflow. Useful for preview-gallery generation from
+ a background thread where ``scene.seed`` may not propagate.
+
+ Returns:
+ bytes: Raw image data on success.
+ dict: ``{"error": "..."}`` on failure.
+ """
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ client_id = str(uuid.uuid4())
+ scene = context.scene
+ architecture = scene.model_architecture # synced from texture_mode
+
+ # When using TRELLIS.2 with native/none texturing the backbone is
+ # synced from trellis2_initial_image_arch instead of texture_mode.
+ # Double-check here in case the property wasn't synced yet.
+ if getattr(scene, 'architecture_mode', '') == 'trellis2':
+ tex_mode = getattr(scene, 'trellis2_texture_mode', 'native')
+ if tex_mode not in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein'):
+ architecture = getattr(scene, 'trellis2_initial_image_arch', 'sdxl')
+
+ if architecture == 'qwen_image_edit':
+ prompt, save_node = self._build_qwen_txt2img(context)
+ elif architecture == 'flux1':
+ prompt, save_node = self._build_flux_txt2img(context)
+ elif architecture == 'flux2_klein':
+ prompt, save_node = self._build_klein_txt2img(context)
+ else:
+ prompt, save_node = self._build_sdxl_txt2img(context)
+
+ # Patch seed if caller supplies an override (thread-safe)
+ if seed_override is not None:
+ for _node in prompt.values():
+ _inp = _node.get('inputs', {})
+ if 'seed' in _inp:
+ _inp['seed'] = seed_override
+ if 'noise_seed' in _inp:
+ _inp['noise_seed'] = seed_override
+
+ ws = self._connect_to_websocket(server_address, client_id)
+ if ws is None:
+ return {"error": "WebSocket connection failed"}
+
+ # Let the operator close this WS on cancel
+ if hasattr(self.operator, '_active_ws'):
+ self.operator._active_ws = ws
+ # Also set on class level so cancel from a new instance can find it
+ type(self.operator)._active_ws = ws
+
+ try:
+ images = self._execute_prompt_and_get_images(
+ ws, prompt, client_id, server_address, {"save_image": save_node}
+ )
+ finally:
+ if hasattr(self.operator, '_active_ws'):
+ self.operator._active_ws = None
+ type(self.operator)._active_ws = None
+ try:
+ ws.close()
+ except Exception:
+ pass
+
+ if images is None or not isinstance(images, dict) or not images:
+ return {"error": "txt2img generation failed"}
+
+ if save_node not in images or not images[save_node]:
+ return {"error": "No image received from txt2img"}
+
+ return images[save_node][0]
+
+ def _build_sdxl_txt2img(self, context):
+ """Return (prompt_dict, save_node_id) for a minimal SDXL txt2img with LoRA support."""
+ scene = context.scene
+ prompt = {
+ "1": {
+ "class_type": "CheckpointLoaderSimple",
+ "inputs": {"ckpt_name": scene.model_name}
+ },
+ "2": {
+ "class_type": "CLIPTextEncode",
+ "inputs": {"text": _generation_prompt(scene), "clip": ["1", 1]}
+ },
+ "3": {
+ "class_type": "CLIPTextEncode",
+ "inputs": {"text": scene.comfyui_negative_prompt, "clip": ["1", 1]}
+ },
+ "4": {
+ "class_type": "EmptyLatentImage",
+ "inputs": {"width": 1024, "height": 1024, "batch_size": 1}
+ },
+ "5": {
+ "class_type": "KSampler",
+ "inputs": {
+ "model": ["1", 0],
+ "positive": ["2", 0],
+ "negative": ["3", 0],
+ "latent_image": ["4", 0],
+ "seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31),
+ "steps": scene.steps,
+ "cfg": scene.cfg,
+ "sampler_name": scene.sampler,
+ "scheduler": scene.scheduler,
+ "denoise": 1.0
+ }
+ },
+ "6": {
+ "class_type": "VAEDecode",
+ "inputs": {"samples": ["5", 0], "vae": ["1", 2]}
+ },
+ "7": {
+ "class_type": "SaveImageWebsocket",
+ "inputs": {"images": ["6", 0]}
+ }
+ }
+
+ # LoRA chain (LoraLoader): model + clip
+ prompt, final_model, final_clip = self._build_lora_chain(
+ prompt, context,
+ initial_model_input=["1", 0],
+ initial_clip_input=["1", 1],
+ start_node_id=300,
+ lora_class_type="LoraLoader"
+ )
+ prompt["5"]["inputs"]["model"] = final_model
+ prompt["2"]["inputs"]["clip"] = final_clip
+ prompt["3"]["inputs"]["clip"] = final_clip
+
+ return prompt, "7"
+
+ # ── PBR Decomposition (Marigold IID) ───────────────────────────────
+ def generate_pbr_maps(self, context, input_image_path, model_name=None,
+ force_native_resolution=False):
+ """Run a Marigold model on a generated texture and collect the output.
+
+ Uploads *input_image_path* to the ComfyUI server, runs it through the
+ ``MarigoldModelLoader`` + ``MarigoldDepthEstimation_v2`` pipeline,
+ and collects the resulting map(s) via ``SaveImageWebsocket``.
+
+ Different models produce different outputs:
+
+ * **IID-Appearance** → 3 images: [Albedo, Roughness, Metallicity]
+ * **IID-Lighting** → 3 images: [Albedo, Shading, Residual]
+ * **Normals** → 1 image: [Normal map]
+ * **Depth** → 1 image: [Depth map]
+
+ Args:
+ input_image_path: Absolute path to the generated texture PNG.
+ model_name: HuggingFace model repo name to use, e.g.
+ ``'prs-eth/marigold-iid-appearance-v1-1'``. If *None*,
+ defaults to the IID-Appearance model.
+ force_native_resolution: If *True*, always process at the
+ image's native longest edge (rounded to 64) regardless
+ of the user-configured resolution settings. Used by
+ the tiling path so each tile gets full-detail processing.
+
+ Returns:
+ list[bytes] | dict: A list of raw-PNG byte buffers on success,
+ or ``{"error": "..."}`` on failure.
+ """
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ client_id = str(uuid.uuid4())
+ scene = context.scene
+
+ if model_name is None:
+ model_name = 'prs-eth/marigold-iid-appearance-v1-1'
+
+ # Pre-flight: verify that ComfyUI-Marigold nodes are installed
+ required_nodes = ['MarigoldModelLoader', 'MarigoldDepthEstimation_v2']
+ for node_class in required_nodes:
+ try:
+ resp = urllib.request.urlopen(
+ f"http://{server_address}/object_info/{node_class}",
+ timeout=get_timeout('api')
+ )
+ data = json.loads(resp.read())
+ if node_class not in data:
+ return {"error": f"ComfyUI-Marigold node '{node_class}' not found. "
+ f"Install ComfyUI-Marigold (run installer option 9 "
+ f"or git clone https://github.com/kijai/ComfyUI-Marigold "
+ f"into ComfyUI/custom_nodes/) and ensure 'diffusers>=0.28' "
+ f"is installed in ComfyUI's Python, then restart ComfyUI."}
+ except urllib.error.HTTPError as e:
+ if e.code == 404:
+ return {"error": f"ComfyUI-Marigold node '{node_class}' not found. "
+ f"Install ComfyUI-Marigold (run installer option 9 "
+ f"or git clone https://github.com/kijai/ComfyUI-Marigold "
+ f"into ComfyUI/custom_nodes/) and ensure 'diffusers>=0.28' "
+ f"is installed in ComfyUI's Python, then restart ComfyUI."}
+ return {"error": f"PBR model error: failed to query ComfyUI for node "
+ f"'{node_class}' (HTTP {e.code}). Is ComfyUI running?"}
+ except Exception as e:
+ return {"error": f"PBR model error: could not reach ComfyUI at "
+ f"'{server_address}' while checking for node '{node_class}' "
+ f"({type(e).__name__}: {e}). Verify ComfyUI is running and "
+ f"the server address is correct."}
+
+ # Upload the source image to ComfyUI's input folder
+ from .._generator_utils import upload_image_to_comfyui
+ image_info = upload_image_to_comfyui(server_address, input_image_path)
+ if image_info is None:
+ return {"error": f"Failed to upload image for PBR decomposition: {input_image_path}"}
+
+ uploaded_name = image_info.get("name", os.path.basename(input_image_path))
+
+ # Determine the processing resolution.
+ # When 'Native Resolution' is enabled (or forced by tiling), use
+ # the image's own longest edge (rounded to 64) so Marigold
+ # processes at full detail.
+ # Otherwise fall back to the user-specified fixed resolution.
+ use_native = force_native_resolution or getattr(
+ scene, 'pbr_use_native_resolution', False)
+ if use_native:
+ try:
+ from PIL import Image as _PILImage
+ with _PILImage.open(input_image_path) as _img:
+ _w, _h = _img.size
+ proc_res = max(_w, _h)
+ proc_res = ((proc_res + 63) // 64) * 64
+ print(f"[StableGen] Marigold: native resolution {proc_res}px "
+ f"(input {_w}\u00d7{_h})")
+ except Exception:
+ proc_res = scene.pbr_processing_resolution
+ else:
+ proc_res = scene.pbr_processing_resolution
+
+ # Build the minimal ComfyUI workflow
+ prompt = {
+ "1": {
+ "class_type": "LoadImage",
+ "inputs": {"image": uploaded_name}
+ },
+ "2": {
+ "class_type": "MarigoldModelLoader",
+ "inputs": {
+ "model": model_name,
+ }
+ },
+ "3": {
+ "class_type": "MarigoldDepthEstimation_v2",
+ "inputs": {
+ "marigold_model": ["2", 0],
+ "image": ["1", 0],
+ "seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31),
+ "denoise_steps": scene.pbr_denoise_steps,
+ "ensemble_size": scene.pbr_ensemble_size,
+ "processing_resolution": proc_res,
+ "scheduler": "LCMScheduler",
+ "use_taesd_vae": False,
+ # Keep loaded to avoid offloading to CPU — newer diffusers
+ # raises ValueError when moving a float16 pipeline to CPU.
+ "keep_model_loaded": True,
+ }
+ },
+ "4": {
+ "class_type": "SaveImageWebsocket",
+ "inputs": {"images": ["3", 0]}
+ }
+ }
+
+ NODES = {"save_image": "4"}
+
+ ws = self._connect_to_websocket(server_address, client_id)
+ if ws is None:
+ return {"error": "WebSocket connection failed for PBR decomposition"}
+
+ if hasattr(self.operator, '_active_ws'):
+ self.operator._active_ws = ws
+ type(self.operator)._active_ws = ws
+
+ try:
+ images = self._execute_prompt_and_get_images(
+ ws, prompt, client_id, server_address, NODES
+ )
+ finally:
+ if hasattr(self.operator, '_active_ws'):
+ self.operator._active_ws = None
+ type(self.operator)._active_ws = None
+ try:
+ ws.close()
+ except Exception:
+ pass
+
+ if images is None or not isinstance(images, dict) or not images:
+ return {"error": "PBR decomposition failed — no output received"}
+
+ save_node = NODES['save_image']
+ if save_node not in images or not images[save_node]:
+ return {"error": "PBR decomposition failed — no images from save node"}
+
+ pbr_maps = images[save_node] # list of raw PNG bytes
+ print(f"[StableGen] Marigold '{model_name}' returned {len(pbr_maps)} image(s)")
+
+ return pbr_maps
+
+ def generate_delight_map(self, context, input_image_path):
+ """Run StableDelight on an image to produce a specular-free (delighted) version.
+
+ Uses the ``LoadStableDelightModel`` + ``ApplyStableDelight`` nodes
+ from the *ComfyUI_StableDelight_ll* custom-node package.
+
+ The delighted image preserves diffuse shading and texture detail
+ while removing specular highlights — making it a high-quality
+ alternative to Marigold IID's flat albedo for the Base Color slot.
+
+ Args:
+ input_image_path: Absolute path to the generated texture PNG.
+
+ Returns:
+ bytes | dict: Raw PNG bytes of the delighted image on success,
+ or ``{"error": "..."}`` on failure.
+ """
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ client_id = str(uuid.uuid4())
+ scene = context.scene
+
+ # Pre-flight: verify that StableDelight nodes are installed
+ required_nodes = ['LoadStableDelightModel', 'ApplyStableDelight']
+ for node_class in required_nodes:
+ try:
+ resp = urllib.request.urlopen(
+ f"http://{server_address}/object_info/{node_class}",
+ timeout=get_timeout('api')
+ )
+ data = json.loads(resp.read())
+ if node_class not in data:
+ return {"error": f"StableDelight node '{node_class}' not found. "
+ f"Install ComfyUI_StableDelight_ll (run installer "
+ f"option 10 or git clone "
+ f"https://github.com/lldacing/ComfyUI_StableDelight_ll "
+ f"into ComfyUI/custom_nodes/) and download the model "
+ f"'Stable-X/yoso-delight-v0-4-base' into "
+ f"ComfyUI/models/diffusers/, then restart ComfyUI."}
+ except urllib.error.HTTPError as e:
+ if e.code == 404:
+ return {"error": f"StableDelight node '{node_class}' not found. "
+ f"Install ComfyUI_StableDelight_ll (run installer "
+ f"option 10 or git clone "
+ f"https://github.com/lldacing/ComfyUI_StableDelight_ll "
+ f"into ComfyUI/custom_nodes/) and download the model "
+ f"'Stable-X/yoso-delight-v0-4-base' into "
+ f"ComfyUI/models/diffusers/, then restart ComfyUI."}
+ return {"error": f"Normal estimation error: failed to query ComfyUI for node "
+ f"'{node_class}' (HTTP {e.code}). Is ComfyUI running?"}
+ except Exception as e:
+ return {"error": f"Normal estimation error: could not reach ComfyUI at "
+ f"'{server_address}' while checking for node '{node_class}' "
+ f"({type(e).__name__}: {e}). Verify ComfyUI is running and "
+ f"the server address is correct."}
+
+ # Resolve model path — the node expects a relative path under
+ # ComfyUI/models/diffusers/ containing model_index.json.
+ delight_model_path = "Stable-X--yoso-delight-v0-4-base"
+
+ # Upload the source image to ComfyUI's input folder
+ from .._generator_utils import upload_image_to_comfyui
+ image_info = upload_image_to_comfyui(server_address, input_image_path)
+ if image_info is None:
+ return {"error": f"Failed to upload image for StableDelight: {input_image_path}"}
+
+ uploaded_name = image_info.get("name", os.path.basename(input_image_path))
+
+ # Determine the processing resolution.
+ #
+ # The official Stable-X Predictor uses processing_resolution=2048,
+ # while the ComfyUI node defaults to 1024. Both use
+ # MarigoldImageProcessor.preprocess() → resize_to_max_edge() which
+ # scales the image so its *longest edge* equals processing_resolution,
+ # then pads to a multiple of vae_scale_factor (8).
+ #
+ # Problems with a fixed resolution:
+ # - Too small (e.g. 1024): images larger than 1024px get downscaled
+ # then bilinearly upscaled back → blurry output.
+ # - Too large: small images get massively upscaled → wastes VRAM
+ # and can exceed available memory.
+ # - resolution=0: triggers skip_preprocess=True in the node, which
+ # has a 3D/4D tensor bug in the pipeline's VAE encoder.
+ #
+ # Solution: use max(native_longest_edge, 1024). This ensures:
+ # - Large images (>1024) are processed at native res (no downscale).
+ # - Small images (<1024) are upscaled to 1024 — giving the SD1.5-
+ # based model enough spatial tokens to produce clean output.
+ # The value is rounded up to the nearest multiple of 64 (not just 8)
+ # to match the official Stable-X resize_image() convention, which
+ # aligns to 64 for clean integer dimensions through every UNet level
+ # (VAE /8, then 3 downsampling stages at /2 each = 64× total).
+ try:
+ from PIL import Image as _PILImage
+ with _PILImage.open(input_image_path) as _img:
+ _w, _h = _img.size
+ # Floor: at least 1024 (the model's ComfyUI default)
+ # Ceiling: native resolution (avoid downscaling)
+ proc_res = max(max(_w, _h), 1024)
+ # Round up to next multiple of 64 (official alignment)
+ proc_res = ((proc_res + 63) // 64) * 64
+ print(f"[StableGen] StableDelight: processing at {proc_res}px "
+ f"(input {_w}×{_h})")
+ except Exception:
+ proc_res = 1024 # safe fallback
+
+ # Build the ComfyUI workflow
+ prompt = {
+ "1": {
+ "class_type": "LoadImage",
+ "inputs": {"image": uploaded_name}
+ },
+ "2": {
+ "class_type": "LoadStableDelightModel",
+ "inputs": {
+ "model": delight_model_path,
+ "device": "AUTO",
+ }
+ },
+ "3": {
+ "class_type": "ApplyStableDelight",
+ "inputs": {
+ "model": ["2", 0],
+ "images": ["1", 0],
+ "strength": getattr(scene, 'pbr_delight_strength', 1.0),
+ "resolution": proc_res,
+ # Official Stable-X Predictor uses LANCZOS for resize.
+ # F.interpolate (used by MarigoldImageProcessor) doesn't
+ # support lanczos, but "bicubic" is the closest PyTorch
+ # equivalent and much sharper than "bilinear" for both
+ # input downscale and output upscale.
+ "upscale_method": "bicubic",
+ }
+ },
+ "4": {
+ "class_type": "SaveImageWebsocket",
+ "inputs": {"images": ["3", 0]}
+ }
+ }
+
+ NODES = {"save_image": "4"}
+
+ ws = self._connect_to_websocket(server_address, client_id)
+ if ws is None:
+ return {"error": "WebSocket connection failed for StableDelight"}
+
+ if hasattr(self.operator, '_active_ws'):
+ self.operator._active_ws = ws
+ type(self.operator)._active_ws = ws
+
+ try:
+ images = self._execute_prompt_and_get_images(
+ ws, prompt, client_id, server_address, NODES
+ )
+ finally:
+ if hasattr(self.operator, '_active_ws'):
+ self.operator._active_ws = None
+ type(self.operator)._active_ws = None
+ try:
+ ws.close()
+ except Exception:
+ pass
+
+ if images is None or not isinstance(images, dict) or not images:
+ return {"error": "StableDelight failed — no output received"}
+
+ save_node = NODES['save_image']
+ if save_node not in images or not images[save_node]:
+ return {"error": "StableDelight failed — no images from save node"}
+
+ delight_images = images[save_node]
+ print(f"[StableGen] StableDelight returned {len(delight_images)} image(s)")
+
+ # StableDelight returns a single image
+ return delight_images[0] if delight_images else {"error": "StableDelight returned empty"}
+
+ def _build_klein_txt2img(self, context):
+ """Return (prompt_dict, save_node_id) for a minimal FLUX.2 Klein txt2img.
+
+ Uses the same node types as the full Klein multi-reference workflow
+ (UNETLoader, CLIPLoader with type flux2, VAELoader, Flux2Scheduler,
+ CFGGuider, SamplerCustomAdvanced) but without any reference images.
+ """
+ scene = context.scene
+ unet_name = scene.model_name
+
+ # Detect 9B vs 4B CLIP from model name
+ model_lower = unet_name.lower()
+ clip_name = "qwen_3_8b_fp8mixed.safetensors" if '9b' in model_lower else "qwen_3_4b.safetensors"
+
+ prompt = {
+ "1": {
+ "class_type": "UNETLoader",
+ "inputs": {"unet_name": unet_name, "weight_dtype": "default"}
+ },
+ "2": {
+ "class_type": "CLIPLoader",
+ "inputs": {"clip_name": clip_name, "type": "flux2"}
+ },
+ "3": {
+ "class_type": "VAELoader",
+ "inputs": {"vae_name": "flux2-vae.safetensors"}
+ },
+ "4": {
+ "class_type": "CLIPTextEncode",
+ "inputs": {"text": _generation_prompt(scene), "clip": ["2", 0]}
+ },
+ "5": {
+ "class_type": "CLIPTextEncode",
+ "inputs": {"text": "", "clip": ["2", 0]}
+ },
+ "6": {
+ "class_type": "EmptyFlux2LatentImage",
+ "inputs": {"width": 1024, "height": 1024, "batch_size": 1}
+ },
+ "7": {
+ "class_type": "Flux2Scheduler",
+ "inputs": {"steps": scene.steps, "width": 1024, "height": 1024}
+ },
+ "8": {
+ "class_type": "KSamplerSelect",
+ "inputs": {"sampler_name": scene.sampler}
+ },
+ "9": {
+ "class_type": "RandomNoise",
+ "inputs": {"noise_seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31)}
+ },
+ "10": {
+ "class_type": "CFGGuider",
+ "inputs": {
+ "model": ["1", 0],
+ "positive": ["4", 0],
+ "negative": ["5", 0],
+ "cfg": scene.cfg
+ }
+ },
+ "11": {
+ "class_type": "SamplerCustomAdvanced",
+ "inputs": {
+ "noise": ["9", 0],
+ "guider": ["10", 0],
+ "sampler": ["8", 0],
+ "sigmas": ["7", 0],
+ "latent_image": ["6", 0]
+ }
+ },
+ "12": {
+ "class_type": "VAEDecode",
+ "inputs": {"samples": ["11", 0], "vae": ["3", 0]}
+ },
+ "13": {
+ "class_type": "SaveImageWebsocket",
+ "inputs": {"images": ["12", 0]}
+ }
+ }
+
+ return prompt, "13"
+
+ def _build_flux_txt2img(self, context):
+ """Return (prompt_dict, save_node_id) for a minimal Flux txt2img with LoRA support."""
+ scene = context.scene
+ unet_name = scene.model_name
+ is_gguf = ".gguf" in unet_name.lower()
+ unet_loader_class = "UnetLoaderGGUF" if is_gguf else "UNETLoader"
+
+ prompt = {
+ "10": {
+ "class_type": "VAELoader",
+ "inputs": {"vae_name": "ae.sft"}
+ },
+ "11": {
+ "class_type": "DualCLIPLoader",
+ "inputs": {
+ "clip_name1": "t5xxl_fp8_e4m3fn.safetensors",
+ "clip_name2": "clip_l.safetensors",
+ "type": "flux",
+ "device": "default"
+ }
+ },
+ "12": {
+ "class_type": unet_loader_class,
+ "inputs": {"unet_name": unet_name}
+ },
+ "6": {
+ "class_type": "CLIPTextEncode",
+ "inputs": {"text": _generation_prompt(scene), "clip": ["11", 0]}
+ },
+ "26": {
+ "class_type": "FluxGuidance",
+ "inputs": {"guidance": scene.cfg, "conditioning": ["6", 0]}
+ },
+ "25": {
+ "class_type": "RandomNoise",
+ "inputs": {"noise_seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31)}
+ },
+ "16": {
+ "class_type": "KSamplerSelect",
+ "inputs": {"sampler_name": scene.sampler}
+ },
+ "17": {
+ "class_type": "BasicScheduler",
+ "inputs": {
+ "scheduler": scene.scheduler,
+ "steps": scene.steps,
+ "denoise": 1.0,
+ "model": ["12", 0]
+ }
+ },
+ "22": {
+ "class_type": "BasicGuider",
+ "inputs": {"model": ["12", 0], "conditioning": ["26", 0]}
+ },
+ "30": {
+ "class_type": "EmptyLatentImage",
+ "inputs": {"width": 1024, "height": 1024, "batch_size": 1}
+ },
+ "13": {
+ "class_type": "SamplerCustomAdvanced",
+ "inputs": {
+ "noise": ["25", 0],
+ "guider": ["22", 0],
+ "sampler": ["16", 0],
+ "sigmas": ["17", 0],
+ "latent_image": ["30", 0]
+ }
+ },
+ "8": {
+ "class_type": "VAEDecode",
+ "inputs": {"samples": ["13", 0], "vae": ["10", 0]}
+ },
+ "7": {
+ "class_type": "SaveImageWebsocket",
+ "inputs": {"images": ["8", 0]}
+ }
+ }
+
+ # LoRA chain (LoraLoaderModelOnly): model only, no clip
+ prompt, final_model, _ = self._build_lora_chain(
+ prompt, context,
+ initial_model_input=["12", 0],
+ initial_clip_input=["12", 0], # dummy, not used by LoraLoaderModelOnly
+ start_node_id=300,
+ lora_class_type="LoraLoaderModelOnly"
+ )
+ prompt["17"]["inputs"]["model"] = final_model
+ prompt["22"]["inputs"]["model"] = final_model
+
+ return prompt, "7"
+
+ def _build_qwen_txt2img(self, context):
+ """Return (prompt_dict, save_node_id) for a minimal Qwen txt2img.
+
+ Qwen-Image-Edit can generate images from text alone when no input
+ images are provided to the ``TextEncodeQwenImageEditPlus`` nodes.
+ """
+ scene = context.scene
+ unet_name = scene.model_name
+ is_gguf = ".gguf" in unet_name.lower()
+ unet_loader_class = "UnetLoaderGGUF" if is_gguf else "UNETLoader"
+
+ prompt = {
+ "3": {
+ "class_type": "CLIPLoader",
+ "inputs": {
+ "clip_name": "qwen_2.5_vl_7b_fp8_scaled.safetensors",
+ "type": "qwen_image",
+ "device": "default"
+ }
+ },
+ "4": {
+ "class_type": "VAELoader",
+ "inputs": {"vae_name": "qwen_image_vae.safetensors"}
+ },
+ "13": {
+ "class_type": unet_loader_class,
+ "inputs": {"unet_name": unet_name}
+ },
+ "6": {
+ "class_type": "ModelSamplingAuraFlow",
+ "inputs": {"shift": 3, "model": ["13", 0]}
+ },
+ "9": {
+ "class_type": "CFGNorm",
+ "inputs": {"strength": 1, "model": ["6", 0]}
+ },
+ "12": {
+ "class_type": "TextEncodeQwenImageEditPlus",
+ "inputs": {
+ "prompt": _generation_prompt(scene),
+ "clip": ["3", 0]
+ }
+ },
+ "11": {
+ "class_type": "TextEncodeQwenImageEditPlus",
+ "inputs": {
+ "prompt": "",
+ "clip": ["3", 0]
+ }
+ },
+ "30": {
+ "class_type": "EmptyLatentImage",
+ "inputs": {"width": 1024, "height": 1024, "batch_size": 1}
+ },
+ "1": {
+ "class_type": "KSampler",
+ "inputs": {
+ "model": ["9", 0],
+ "positive": ["12", 0],
+ "negative": ["11", 0],
+ "latent_image": ["30", 0],
+ "seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31),
+ "steps": scene.steps,
+ "cfg": scene.cfg,
+ "sampler_name": scene.sampler,
+ "scheduler": scene.scheduler,
+ "denoise": 1.0
+ }
+ },
+ "2": {
+ "class_type": "VAEDecode",
+ "inputs": {"samples": ["1", 0], "vae": ["4", 0]}
+ },
+ "7": {
+ "class_type": "SaveImageWebsocket",
+ "inputs": {"images": ["2", 0]}
+ }
+ }
+
+ # LoRA chain: Qwen uses NunchakuQwenImageLoraLoader for nunchaku
+ # models, LoraLoaderModelOnly otherwise
+ is_nunchaku = unet_name.lower().endswith('.safetensors')
+ lora_class = "NunchakuQwenImageLoraLoader" if is_nunchaku else "LoraLoaderModelOnly"
+
+ prompt, final_model, _ = self._build_lora_chain(
+ prompt, context,
+ initial_model_input=["13", 0],
+ initial_clip_input=["13", 0], # dummy
+ start_node_id=500,
+ lora_class_type=lora_class
+ )
+ # ModelSamplingAuraFlow takes the final LoRA output
+ prompt["6"]["inputs"]["model"] = final_model
+
+ return prompt, "7"
+
+ def _create_base_prompt(self, context):
+ """Creates and configures the base prompt with user settings."""
+ from ..util.workflow_templates import prompt_text
+
+ # Load the base prompt template
+ prompt = json.loads(prompt_text)
+
+ # Node IDs organized by functional category
+ NODES = {
+ # Text Prompting
+ 'pos_prompt': "9",
+ 'neg_prompt': "10",
+ 'clip_skip': "247",
+
+ # Sampling Control
+ 'sampler': "15",
+ 'seed_control': "15", # Same as sampler node but for seed parameter
+
+ # Model Loading
+ 'checkpoint': "6",
+
+ # Latent Space
+ 'latent': "16",
+
+ # Image Output
+ 'save_image': "25",
+
+ # IPAdapter
+ 'ipadapter_loader': "235",
+ 'ipadapter': "236",
+ 'ipadapter_image': "237",
+ }
+
+ 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
+ # Prepend the view description
+ base_prompt_text = f"{view_desc}, {base_prompt_text}"
+
+ # Set text prompts
+ prompt[NODES['pos_prompt']]["inputs"]["text"] = base_prompt_text
+ prompt[NODES['neg_prompt']]["inputs"]["text"] = context.scene.comfyui_negative_prompt
+
+ # Set sampling parameters
+ 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
+
+ # Set clip skip
+ prompt[NODES['clip_skip']]["inputs"]["stop_at_clip_layer"] = -context.scene.clip_skip
+
+ # Set the model name
+ prompt[NODES['checkpoint']]["inputs"]["ckpt_name"] = context.scene.model_name
+
+ # Build LoRA chain
+ initial_model_input_lora = [NODES['checkpoint'], 0]
+ initial_clip_input_lora = [NODES['checkpoint'], 1]
+
+ prompt, final_lora_model_out, final_lora_clip_out = self._build_lora_chain(
+ prompt, context,
+ initial_model_input_lora, initial_clip_input_lora,
+ start_node_id=400 # Starting node ID for LoRA chain
+ )
+
+ current_model_out = final_lora_model_out
+
+ # Set the input for the clip skip node
+ prompt[NODES['clip_skip']]["inputs"]["clip"] = final_lora_clip_out
+
+ # If using IPAdapter, set the model input
+ _is_trellis2_input_ipadapter = (
+ context.scene.sequential_ipadapter
+ and context.scene.sequential_ipadapter_mode == 'trellis2_input'
+ )
+ if (context.scene.use_ipadapter
+ or _is_trellis2_input_ipadapter
+ or (context.scene.generation_method == 'separate'
+ and context.scene.sequential_ipadapter
+ and self.operator._current_image > 0)):
+ # Set the model input for IPAdapter
+ prompt[NODES['ipadapter_loader']]["inputs"]["model"] = current_model_out
+ current_model_out = [NODES['ipadapter'], 0]
+
+ # Set the model for sampler node
+ prompt[NODES['sampler']]["inputs"]["model"] = current_model_out
+
+ return prompt, NODES
+
+ def _configure_resolution(self, prompt, context, NODES):
+ """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.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
+ prompt[NODES['latent']]["inputs"]["height"] = context.scene.render.resolution_y
+
+ def _configure_ipadapter(self, prompt, context, ipadapter_ref_info, NODES):
+ # Configure IPAdapter if enabled
+
+ # Connect IPAdapter output to the appropriate node
+ prompt[NODES['sampler']]["inputs"]["model"] = [NODES['ipadapter'], 0]
+
+ # Set IPAdapter image source
+ prompt[NODES['ipadapter_image']]["inputs"]["image"] = ipadapter_ref_info['name']
+
+ # Connect ipadapter image to the input
+ prompt[NODES['ipadapter']]["inputs"]["image"] = [NODES['ipadapter_image'], 0]
+
+ # Configure IPAdapter settings
+ prompt[NODES['ipadapter']]["inputs"]["weight"] = context.scene.ipadapter_strength
+ prompt[NODES['ipadapter']]["inputs"]["start_at"] = context.scene.ipadapter_start
+ prompt[NODES['ipadapter']]["inputs"]["end_at"] = context.scene.ipadapter_end
+
+ # Set weight type
+ weight_type_mapping = {
+ 'standard': "standard",
+ 'prompt': "prompt is more important",
+ 'style': "style transfer"
+ }
+ prompt[NODES['ipadapter']]["inputs"]["weight_type"] = weight_type_mapping.get(context.scene.ipadapter_weight_type, "standard")
+
+ def _build_controlnet_chain_extended(self, context, base_prompt, pos_input, neg_input, vae_input, controlnet_info_dict):
+ """
+ Builds a chain of ControlNet units dynamically based on scene settings.
+
+ Args:
+ context: Blender context, used to access addon preferences and scene data.
+ base_prompt (dict): The ComfyUI prompt dictionary to be modified.
+ pos_input (list): The [node_id, output_idx] for the initial positive conditioning.
+ neg_input (list): The [node_id, output_idx] for the initial negative conditioning.
+ vae_input (list): The [node_id, output_idx] for the VAE, used by ControlNetApplyAdvanced.
+ Typically, this is [checkpoint_node_id, 2] for SDXL or
+ [checkpoint_node_id, 0] for some VAE loaders.
+ controlnet_info_dict (dict): A dictionary mapping ControlNet types (e.g., "depth",
+ "canny") to their corresponding uploaded image info.
+
+ Returns:
+ tuple: (modified_prompt, final_positive_conditioning, final_negative_conditioning)
+ """
+ addon_prefs = context.preferences.addons[_ADDON_PKG].preferences
+ try:
+ mapping = json.loads(addon_prefs.controlnet_mapping)
+ except Exception:
+ mapping = {}
+
+ # Get the dynamic collection of ControlNet units
+ controlnet_units = getattr(context.scene, "controlnet_units", [])
+ current_pos = pos_input
+ current_neg = neg_input
+ has_union = False
+ for idx, unit in enumerate(controlnet_units):
+ # Get uploaded info for this unit's type
+ uploaded_info = controlnet_info_dict.get(unit.unit_type)
+ if not uploaded_info:
+ print(f"[StableGen] Warning: Uploaded info for ControlNet type '{unit.unit_type}' not found. Skipping unit.")
+ continue # Skip this unit
+
+ # Generate unique keys for nodes in this chain unit.
+ load_key = str(200 + idx * 3) # LoadImage node
+ loader_key = str(200 + idx * 3 + 1) # ControlNetLoader node
+ apply_key = str(200 + idx * 3 + 2) # ControlNetApplyAdvanced node
+
+ # Create the LoadImage node.
+ base_prompt[load_key] = {
+ "inputs": {
+ "image": uploaded_info['name'],
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": f"Load Image ({unit.unit_type})"
+ }
+ }
+ # Create the ControlNetLoader node.
+ base_prompt[loader_key] = {
+ "inputs": {
+ "control_net_name": unit.model_name # updated to use selected property
+ },
+ "class_type": "ControlNetLoader",
+ "_meta": {
+ "title": f"Load ControlNet ({unit.unit_type})"
+ }
+ }
+ # Create the ControlNetApplyAdvanced node.
+ base_prompt[apply_key] = {
+ "inputs": {
+ "strength": unit.strength,
+ "start_percent": unit.start_percent,
+ "end_percent": unit.end_percent,
+ "positive": [current_pos, 0],
+ "negative": [current_neg, 1] if (idx > 0 or current_neg == "228" or current_neg == "51") else [current_neg, 0],
+ "control_net": [loader_key, 0],
+ "image": [load_key, 0],
+ "vae": [vae_input, 2] if context.scene.model_architecture == "sdxl" else [vae_input, 0],
+ },
+ "class_type": "ControlNetApplyAdvanced",
+ "_meta": {
+ "title": f"Apply ControlNet ({unit.unit_type})"
+ }
+ }
+ # Update chain inputs: the output of this apply node becomes the new input.
+ current_pos = apply_key
+ current_neg = apply_key
+ # If the controlnet is of the union type, connect the ControlNetApplyAdvanced input into the SetUnionControlNetType node (239)
+ if unit.is_union and unit.use_union_type:
+ base_prompt[apply_key]["inputs"]["control_net"] = ["239", 0]
+ base_prompt["239"]["inputs"]["control_net"] = [loader_key, 0]
+ if unit.unit_type == "depth":
+ base_prompt["239"]["inputs"]["type"] = "depth"
+ elif unit.unit_type == "canny":
+ base_prompt["239"]["inputs"]["type"] = "canny/lineart/anime_lineart/mlsd"
+ elif unit.unit_type == "normal":
+ base_prompt["239"]["inputs"]["type"] = "normal"
+ has_union = True
+ if not has_union:
+ # Remove the node
+ if "239" in base_prompt:
+ del base_prompt["239"]
+
+ return base_prompt, current_pos
+
+ def _build_lora_chain(self, prompt, context, initial_model_input, initial_clip_input, start_node_id=300, lora_class_type="LoraLoader"):
+ """
+ Builds a chain of LoRA loaders dynamically.
+
+ Args:
+ prompt (dict): The ComfyUI prompt dictionary to modify.
+ context: Blender context.
+ initial_model_input (list): The [node_id, output_idx] for the initial model.
+ initial_clip_input (list): The [node_id, output_idx] for the initial CLIP.
+ start_node_id (int): The starting integer for generating unique LoRA node IDs.
+ lora_class_type (str): The class type of the LoRA loader node to use.
+
+ Returns:
+ tuple: (modified_prompt, final_model_output, final_clip_output)
+
+ The final model and CLIP outputs are [node_id, output_idx] lists.
+ """
+ scene = context.scene
+
+ current_model_out = initial_model_input
+ current_clip_out = initial_clip_input
+
+ if not scene.lora_units:
+ return prompt, current_model_out, current_clip_out
+
+ for i, lora_unit in enumerate(scene.lora_units):
+ if not lora_unit.model_name or lora_unit.model_name == "NONE":
+ continue # Skip if no LoRA model is selected for this unit
+
+ lora_node_id_str = str(start_node_id + i)
+
+ lora_inputs = {
+ "lora_name": lora_unit.model_name,
+ "strength_model": lora_unit.model_strength,
+ "model": current_model_out,
+ }
+
+ if lora_class_type == "LoraLoader":
+ lora_inputs["strength_clip"] = lora_unit.clip_strength
+ lora_inputs["clip"] = current_clip_out
+ elif lora_class_type == "NunchakuQwenImageLoraLoader":
+ lora_inputs = {
+ "lora_name": lora_unit.model_name,
+ "lora_strength": lora_unit.model_strength,
+ "cpu_offload": "disable",
+ "model": current_model_out
+ }
+
+ prompt[lora_node_id_str] = {
+ "inputs": lora_inputs,
+ "class_type": lora_class_type,
+ "_meta": {
+ "title": f"Load LoRA {i+1} ({lora_unit.model_name[:20]})"
+ }
+ }
+ # Update outputs for the next LoRA in the chain
+ current_model_out = [lora_node_id_str, 0]
+ if lora_class_type == "LoraLoader":
+ current_clip_out = [lora_node_id_str, 1]
+
+ return prompt, current_model_out, current_clip_out
+
+ def _build_controlnet_chain(self, prompt, context, controlnet_info, NODES):
+ """Builds the ControlNet processing chain."""
+ # Build controlnet chain with guidance images
+ prompt, final_node = self._build_controlnet_chain_extended(
+ context, prompt, NODES['pos_prompt'], NODES['neg_prompt'], NODES['checkpoint'],
+ controlnet_info
+ )
+
+ # Connect final node outputs to the KSampler
+ prompt[NODES['sampler']]["inputs"]["positive"] = [final_node, 0]
+ prompt[NODES['sampler']]["inputs"]["negative"] = [final_node, 1]
+
+ return prompt
+
+ def refine(self, context, controlnet_info=None, mask_info=None, render_info=None, ipadapter_ref_info=None):
+ """
+ Refines the image using ComfyUI.
+ :param context: Blender context.
+ :param controlnet_info: Dict of uploaded controlnet image info.
+ :param mask_info: Uploaded mask image info.
+ :param render_info: Uploaded render image info.
+ :param ipadapter_ref_info: Uploaded IPAdapter reference image info.
+ :return: Refined image.
+ """
+ # Setup connection parameters
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ client_id = str(uuid.uuid4())
+ output_dir = context.preferences.addons[_ADDON_PKG].preferences.output_dir
+
+ revision_dir = get_generation_dirs(context)["revision"]
+
+ # Initialize the img2img prompt template and configure base settings
+ prompt, NODES = self._create_img2img_base_prompt(context)
+
+ # Configure based on generation method
+ self._configure_refinement_mode(prompt, context, render_info, mask_info, NODES)
+
+ if ipadapter_ref_info and context.scene.generation_method != 'uv_inpaint':
+ # Configure IPAdapter settings
+ self._configure_ipadapter_refine(prompt, context, ipadapter_ref_info, NODES)
+ else:
+ # Remove IPAdapter nodes if not used
+ for node_id in ["235", "236", "237"]:
+ if node_id in prompt:
+ del prompt[node_id]
+
+ # Set up image inputs for different controlnet types
+ self._refine_configure_images(prompt, render_info, NODES)
+
+ # Build controlnet chain for refinement if needed
+ if not context.scene.generation_method == 'uv_inpaint':
+ prompt = self._refine_build_controlnet_chain(prompt, context, controlnet_info, NODES)
+ else:
+ if context.scene.differential_diffusion:
+ prompt[NODES['sampler']]["inputs"]["positive"] = [NODES['inpaint_conditioning'], 0]
+ prompt[NODES['sampler']]["inputs"]["negative"] = [NODES['inpaint_conditioning'], 1]
+ else:
+ prompt[NODES['sampler']]["inputs"]["positive"] = [NODES['pos_prompt'], 0]
+ prompt[NODES['sampler']]["inputs"]["negative"] = [NODES['neg_prompt'], 0]
+
+ # Save prompt for debugging
+ with open(os.path.join(output_dir, "prompt.json"), 'w') as f:
+ json.dump(prompt, f)
+
+ # Execute generation and get results
+ ws = self._connect_to_websocket(server_address, client_id)
+
+ if ws is None:
+ return {"error": "conn_failed"} # Connection error
+
+ images = None
+ try:
+ images = self._execute_prompt_and_get_images(ws, prompt, client_id, server_address, NODES)
+ finally:
+ if ws:
+ ws.close()
+
+ if images is None or isinstance(images, dict) and "error" in images:
+ return {"error": "conn_failed"}
+
+ print(f"[StableGen] Image refined with prompt: ...")
+
+ img_bytes = images[NODES['save_image']][0]
+
+ # Remove Comfy’s hard border and add a soft alpha vignette
+ img_bytes = self._crop_and_vignette(
+ img_bytes,
+ border_px=8, # tweak this if your Comfy border is wider/narrower
+ feather=0.08, # thicker feather band
+ gamma=0.5, # stronger fade near edge
+ )
+
+ return img_bytes
+
+ def _create_img2img_base_prompt(self, context):
+ """Creates and configures the base prompt for img2img refinement."""
+ from ..util.workflow_templates import prompt_text_img2img
+
+ prompt = json.loads(prompt_text_img2img)
+
+ # Node IDs organized by functional category
+ NODES = {
+ # Text Prompting
+ 'pos_prompt': "102",
+ 'neg_prompt': "103",
+ 'clip_skip': "247",
+
+ # Sampling Control
+ 'sampler': "105",
+
+ # Model Loading
+ 'checkpoint': "38",
+
+ # Image Processing
+ 'upscale_grid': "118",
+ 'upscale_uv': "23",
+ 'vae_encode': "116",
+ 'vae_encode_inpaint': "13",
+ 'inpaint_conditioning': "228",
+
+ # Input Images
+ 'input_image': "1",
+ 'mask_image': "12",
+ 'render_image': "117",
+
+ # Mask Processing
+ 'grow_mask': "224",
+ 'blur': "226",
+ 'image_to_mask': "227",
+
+ # Advanced Features
+ 'differential_diffusion': "229",
+ 'ipadapter_loader': "235",
+ 'ipadapter': "236",
+ 'ipadapter_image': "237",
+
+ # Output
+ 'save_image': "111"
+ }
+
+ 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
+ # Prepend the view description
+ base_prompt_text = f"{view_desc}, {base_prompt_text}"
+
+ # Set positive prompt based on generation method
+ if context.scene.generation_method in ['refine', 'local_edit', 'uv_inpaint', 'sequential']:
+ prompt[NODES['pos_prompt']]["inputs"]["text"] = base_prompt_text
+ else:
+ prompt[NODES['pos_prompt']]["inputs"]["text"] = context.scene.refine_prompt if context.scene.refine_prompt != "" else _texturing_prompt(context.scene)
+
+ # Set negative prompt
+ prompt[NODES['neg_prompt']]["inputs"]["text"] = context.scene.comfyui_negative_prompt
+
+ # Set sampling parameters
+ prompt[NODES['sampler']]["inputs"]["seed"] = context.scene.seed
+ prompt[NODES['sampler']]["inputs"]["steps"] = context.scene.refine_steps if context.scene.generation_method == 'grid' else context.scene.steps
+ prompt[NODES['sampler']]["inputs"]["cfg"] = context.scene.refine_cfg if context.scene.generation_method == 'grid' else context.scene.cfg
+ prompt[NODES['sampler']]["inputs"]["sampler_name"] = context.scene.refine_sampler if context.scene.generation_method == 'grid' else context.scene.sampler
+ prompt[NODES['sampler']]["inputs"]["scheduler"] = context.scene.refine_scheduler if context.scene.generation_method == 'grid' else context.scene.scheduler
+ if context.scene.generation_method in ('grid', 'refine', 'local_edit'):
+ prompt[NODES['sampler']]["inputs"]["denoise"] = context.scene.denoise
+ else:
+ prompt[NODES['sampler']]["inputs"]["denoise"] = 1.0
+
+ # Set clip skip
+ prompt[NODES['clip_skip']]["inputs"]["stop_at_clip_layer"] = -context.scene.clip_skip
+
+ # Set upscale method and dimensions
+ prompt[NODES['upscale_grid']]["inputs"]["upscale_method"] = context.scene.refine_upscale_method
+ prompt[NODES['upscale_grid']]["inputs"]["width"] = context.scene.render.resolution_x
+ prompt[NODES['upscale_grid']]["inputs"]["height"] = context.scene.render.resolution_y
+ prompt[NODES['upscale_uv']]["inputs"]["upscale_method"] = "nearest-exact"
+ prompt[NODES['upscale_uv']]["inputs"]["width"] = 1024
+ prompt[NODES['upscale_uv']]["inputs"]["height"] = 1024
+
+ # Set the model name
+ prompt[NODES['checkpoint']]["inputs"]["ckpt_name"] = context.scene.model_name
+
+ # Build LoRA chain
+ initial_model_input_lora = [NODES['checkpoint'], 0]
+ initial_clip_input_lora = [NODES['checkpoint'], 1]
+
+ prompt, final_lora_model_out, final_lora_clip_out = self._build_lora_chain(
+ prompt, context,
+ initial_model_input_lora, initial_clip_input_lora,
+ start_node_id=400 # Starting node ID for LoRA chain
+ )
+
+ current_model_out = final_lora_model_out
+
+ # Set the input for the clip skip node
+ prompt[NODES['clip_skip']]["inputs"]["clip"] = final_lora_clip_out
+
+ # If using IPAdapter, set the model input
+ _is_original_render_ipadapter = (
+ context.scene.sequential_ipadapter
+ and context.scene.sequential_ipadapter_mode == 'original_render'
+ and context.scene.generation_method == 'local_edit'
+ )
+ _is_trellis2_input_ipadapter = (
+ context.scene.sequential_ipadapter
+ and context.scene.sequential_ipadapter_mode == 'trellis2_input'
+ )
+ if (context.scene.use_ipadapter or _is_original_render_ipadapter or _is_trellis2_input_ipadapter or (context.scene.sequential_ipadapter and self.operator._current_image > 0)) and context.scene.generation_method != 'uv_inpaint':
+ # Set the model input for IPAdapter
+ prompt[NODES['ipadapter_loader']]["inputs"]["model"] = current_model_out
+ current_model_out = [NODES['ipadapter'], 0]
+
+ if context.scene.differential_diffusion and NODES['differential_diffusion'] in prompt and context.scene.generation_method not in ('refine', 'local_edit'):
+ # Set model input for differential diffusion
+ prompt[NODES['differential_diffusion']]["inputs"]["model"] = current_model_out
+ current_model_out = [NODES['differential_diffusion'], 0]
+
+ # Set the model for sampler node
+ prompt[NODES['sampler']]["inputs"]["model"] = current_model_out
+
+ return prompt, NODES
+
+ def _configure_refinement_mode(self, prompt, context, render_info, mask_info, NODES):
+ """Configures the prompt based on the specific refinement mode."""
+ # Configure based on generation method
+ if context.scene.generation_method in ('refine', 'local_edit'):
+ prompt[NODES['vae_encode']]["inputs"]["pixels"] = [NODES['render_image'], 0] # Use render directly
+
+ elif context.scene.generation_method == 'uv_inpaint' or context.scene.generation_method == 'sequential':
+ # Connect latent to KSampler
+ prompt[NODES['sampler']]["inputs"]["latent_image"] = [NODES['vae_encode_inpaint'], 0] if not context.scene.differential_diffusion else [NODES['inpaint_conditioning'], 2]
+
+ # Configure differential diffusion if enabled
+ if context.scene.differential_diffusion:
+ prompt[NODES['sampler']]["inputs"]["model"] = [NODES['differential_diffusion'], 0]
+
+ # Configure mask settings
+ 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
+
+ # Set blur parameters
+ prompt[NODES['blur']]["inputs"]["sigma"] = context.scene.blur_mask_sigma
+ prompt[NODES['blur']]["inputs"]["blur_radius"] = context.scene.blur_mask_radius
+
+ # Set grow mask parameter
+ prompt[NODES['grow_mask']]["inputs"]["expand"] = context.scene.grow_mask_by
+
+ if context.scene.generation_method == 'uv_inpaint':
+ # Configure UV inpainting specific prompts
+ self._configure_uv_inpainting_mode(prompt, context, render_info, NODES)
+ else: # Sequential mode
+ # Configure sequential mode settings
+ self._configure_sequential_mode(prompt, context, NODES)
+
+ def _configure_uv_inpainting_mode(self, prompt, context, render_info, NODES):
+ """Configures the prompts for UV inpainting mode."""
+ # Connect upscale to VAE / InpaintConditioning
+ if not context.scene.differential_diffusion:
+ prompt[NODES['vae_encode_inpaint']]["inputs"]["pixels"] = [NODES['upscale_uv'], 0]
+ else:
+ prompt[NODES['inpaint_conditioning']]["inputs"]["pixels"] = [NODES['upscale_uv'], 0]
+ # Set the noise_mask flag according to context.scene.differential_noise
+ prompt[NODES['inpaint_conditioning']]["inputs"]["noise_mask"] = context.scene.differential_noise
+
+ # Create base UV prompt
+ uv_prompt = f"seamless (UV-unwrapped texture) of {_texturing_prompt(context.scene)}, consistent material continuity, no visible seams or stretching, PBR material properties"
+ uv_prompt_neg = f"seam, stitch, visible edge, texture stretching, repeating pattern, {context.scene.comfyui_negative_prompt}"
+
+ prompt[NODES['pos_prompt']]["inputs"]["text"] = uv_prompt
+ prompt[NODES['neg_prompt']]["inputs"]["text"] = uv_prompt_neg
+
+ # Get the current object name from the file path
+ current_object_name = None
+ if render_info and 'name' in render_info:
+ current_object_name = os.path.basename(render_info['name']).split('.')[0]
+
+ # Use the object-specific prompt if available
+ object_prompt = self.operator._object_prompts.get(current_object_name, _texturing_prompt(context.scene)) if current_object_name else _texturing_prompt(context.scene)
+ if object_prompt:
+ uv_prompt = f"(UV-unwrapped texture) of {object_prompt}, consistent material continuity, no visible seams or stretching, PBR material properties"
+ uv_prompt_neg = f"seam, stitch, visible edge, texture stretching, repeating pattern, {context.scene.comfyui_negative_prompt}"
+ prompt[NODES['pos_prompt']]["inputs"]["text"] = uv_prompt
+ prompt[NODES['neg_prompt']]["inputs"]["text"] = uv_prompt_neg
+
+ def _configure_ipadapter_refine(self, prompt, context, ipadapter_ref_info, NODES):
+ """Configures IPAdapter settings for refinement mode."""
+ # Connect IPAdapter output to the appropriate node
+ if context.scene.differential_diffusion and context.scene.generation_method not in ('refine', 'local_edit'):
+ prompt[NODES['differential_diffusion']]["inputs"]["model"] = [NODES['ipadapter'], 0]
+ else:
+ prompt[NODES['sampler']]["inputs"]["model"] = [NODES['ipadapter'], 0]
+
+ # Set IPAdapter image source
+ prompt[NODES['ipadapter_image']]["inputs"]["image"] = ipadapter_ref_info['name']
+
+
+ # Connect ipadapter image to the input
+ prompt[NODES['ipadapter']]["inputs"]["image"] = [NODES['ipadapter_image'], 0]
+
+ # Configure IPAdapter settings
+ prompt[NODES['ipadapter']]["inputs"]["weight"] = context.scene.ipadapter_strength
+ prompt[NODES['ipadapter']]["inputs"]["start_at"] = context.scene.ipadapter_start
+ prompt[NODES['ipadapter']]["inputs"]["end_at"] = context.scene.ipadapter_end
+
+ # Set weight type
+ weight_type_mapping = {
+ 'standard': "standard",
+ 'prompt': "prompt is more important",
+ 'style': "style transfer"
+ }
+ prompt[NODES['ipadapter']]["inputs"]["weight_type"] = weight_type_mapping.get(context.scene.ipadapter_weight_type, "standard")
+
+ def _configure_sequential_mode(self, prompt, context, NODES):
+ """Configures the prompt for sequential generation mode."""
+ # Connect image directly to VAE
+ prompt[NODES['vae_encode_inpaint']]["inputs"]["pixels"] = [NODES['input_image'], 0]
+ if context.scene.differential_diffusion:
+ # Set the noise_mask flag according to context.scene.differential_noise
+ prompt[NODES['inpaint_conditioning']]["inputs"]["noise_mask"] = context.scene.differential_noise
+
+ def _refine_configure_images(self, prompt, render_info, NODES):
+ """Configures the input images for the refinement process."""
+ # Set render image
+ if render_info:
+ prompt[NODES['render_image']]["inputs"]["image"] = render_info['name']
+
+ def _refine_build_controlnet_chain(self, prompt, context, controlnet_info, NODES):
+ """Builds the ControlNet chain for refinement process."""
+ # Determine inputs for ControlNet chain
+ pos_input = NODES['pos_prompt'] if (not context.scene.differential_diffusion or
+ context.scene.generation_method in ["grid", "refine", "local_edit"]) else NODES['inpaint_conditioning']
+ neg_input = NODES['neg_prompt'] if (not context.scene.differential_diffusion or
+ context.scene.generation_method in ["grid", "refine", "local_edit"]) else NODES['inpaint_conditioning']
+ vae_input = NODES['checkpoint']
+
+ # Build the ControlNet chain
+ prompt, final = self._build_controlnet_chain_extended(
+ context, prompt, pos_input, neg_input, vae_input,
+ controlnet_info
+ )
+
+ # Connect final outputs to KSampler
+ prompt[NODES['sampler']]["inputs"]["positive"] = [final, 0]
+ prompt[NODES['sampler']]["inputs"]["negative"] = [final, 1]
+
+ return prompt
+
+ def create_base_prompt_flux(self, context):
+ """Creates and configures the base Flux prompt.
+ Uses prompt_text_flux and does not include negative prompt or LoRA configuration.
+ """
+ from ..util.workflow_templates import prompt_text_flux
+ prompt = json.loads(prompt_text_flux)
+ # Define node IDs for Flux
+ NODES = {
+ 'pos_prompt': "6", # CLIPTextEncode for positive prompt
+ 'vae_loader': "10", # VAELoader
+ 'dual_clip': "11", # DualCLIPLoader
+ 'unet_loader': "12", # UNETLoader
+ 'sampler': "13", # SamplerCustomAdvanced
+ 'ksampler': "16", # KSamplerSelect
+ 'scheduler': "17", # BasicScheduler
+ 'guider': "22", # BasicGuider
+ 'noise': "25", # RandomNoise
+ 'flux_guidance': "26", # FluxGuidance
+ 'latent': "30", # EmptyLatentImage
+ 'save_image': "32" # SaveImageWebsocket
+ }
+
+ 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
+ # Prepend the view description
+ base_prompt_text = f"{view_desc}, {base_prompt_text}"
+
+ # Set positive prompt only (Flux doesn't use negative prompt)
+ prompt[NODES['pos_prompt']]["inputs"]["text"] = base_prompt_text
+
+ # Configure sampler parameters
+ prompt[NODES['noise']]["inputs"]["noise_seed"] = context.scene.seed
+ prompt[NODES['scheduler']]["inputs"]["steps"] = context.scene.steps
+ prompt[NODES['scheduler']]["inputs"]["scheduler"] = context.scene.scheduler
+ prompt[NODES['flux_guidance']]["inputs"]["guidance"] = context.scene.cfg
+ prompt[NODES['ksampler']]["inputs"]["sampler_name"] = context.scene.sampler
+
+ # Replace unet_loader with UNETLoaderGGUF if using GGUF model
+ if ".gguf" in context.scene.model_name:
+ del prompt[NODES['unet_loader']]
+ from ..util.workflow_templates import gguf_unet_loader
+ unet_loader_dict = json.loads(gguf_unet_loader)
+ prompt.update(unet_loader_dict)
+
+ # Set the model name
+ prompt[NODES['unet_loader']]["inputs"]["unet_name"] = context.scene.model_name
+
+ # Flux does not use negative prompt or LoRA.
+ return prompt, NODES
+
+ def configure_ipadapter_flux(self, prompt, context, ipadapter_ref_info, NODES):
+ # Configure IPAdapter if enabled
+ from ..util.workflow_templates import ipadapter_flux
+ ipadapter_dict = json.loads(ipadapter_flux)
+ prompt.update(ipadapter_dict)
+
+ # Label nodes
+ NODES['ipadapter_loader'] = "242" # IPAdapterFluxLoader
+ NODES['ipadapter'] = "243" # ApplyIPAdapterFlux
+ NODES['ipadapter_image'] = "244" # LoadImage for IPAdapter input
+
+ # Connect IPAdapter output to guider and scheduler
+ prompt[NODES['guider']]["inputs"]["model"] = [NODES['ipadapter'], 0]
+ prompt[NODES['scheduler']]["inputs"]["model"] = [NODES['ipadapter'], 0]
+
+ # Set IPAdapter image source
+ prompt[NODES['ipadapter_image']]["inputs"]["image"] = ipadapter_ref_info['name']
+
+ # Connect ipadapter image to the input
+ prompt[NODES['ipadapter']]["inputs"]["image"] = [NODES['ipadapter_image'], 0]
+
+ # Configure IPAdapter settings
+ prompt[NODES['ipadapter']]["inputs"]["weight"] = context.scene.ipadapter_strength
+ prompt[NODES['ipadapter']]["inputs"]["start_percent"] = context.scene.ipadapter_start
+ prompt[NODES['ipadapter']]["inputs"]["end_percent"] = context.scene.ipadapter_end
+
+ # There is no weight type for Flux IPAdapter
+
+ def generate_flux(self, context, controlnet_info=None, ipadapter_ref_info=None):
+ """Generates an image using Flux 1.
+ Similar in structure to generate() but uses Flux nodes, skips negative prompt and LoRA.
+ """
+ from ..util.workflow_templates import prompt_text_flux
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ client_id = str(uuid.uuid4())
+ output_dir = context.preferences.addons[_ADDON_PKG].preferences.output_dir
+
+ revision_dir = get_generation_dirs(context)["revision"]
+
+ # Build Flux base prompt and node mapping.
+ prompt, NODES = self.create_base_prompt_flux(context)
+
+ self._configure_resolution(prompt, context, NODES)
+
+ # Configure IPAdapter for Flux if enabled
+ if ipadapter_ref_info:
+ self.configure_ipadapter_flux(prompt, context, ipadapter_ref_info, NODES)
+
+ # Build ControlNet chain if not using Depth LoRA
+ if not context.scene.use_flux_lora:
+ prompt, final_node = self._build_controlnet_chain_extended(
+ context, prompt, NODES['pos_prompt'], NODES['pos_prompt'], NODES['vae_loader'],
+ controlnet_info
+ )
+ else: # If using Depth LoRA instead of ControlNet, we do not build a ControlNet chain
+ final_node = NODES['pos_prompt'] # Use positive prompt directly if not using ControlNet
+ # Add Required nodes for the FLUX.1-Depth-dev LoRA
+ from ..util.workflow_templates import depth_lora_flux
+ depth_lora_dict = json.loads(depth_lora_flux)
+ prompt.update(depth_lora_dict)
+
+ # Label nodes
+ NODES['flux_lora_image'] = "245" # LoadImage
+ NODES['instruct_pix'] = "246" # InstructPixToPixConditioning
+ NODES['flux_lora'] = "247" # LoraLoaderModelOnly
+
+ # Connect nodes
+ final_node = NODES['instruct_pix'] # To be connected to flux_guidance
+ prompt[NODES['sampler']]["inputs"]["latent_image"] = [NODES['instruct_pix'], 2]
+
+ # If using ipadapter, set the apply_ipadapter_flux node to use the flux_lora_image
+ _is_original_render_ipadapter = (
+ context.scene.sequential_ipadapter
+ and context.scene.sequential_ipadapter_mode == 'original_render'
+ and context.scene.generation_method == 'local_edit'
+ )
+ if context.scene.use_ipadapter or _is_original_render_ipadapter or (context.scene.generation_method == 'separate' and context.scene.sequential_ipadapter and self.operator._current_image > 0):
+ prompt[NODES['ipadapter']]["inputs"]["model"] = [NODES['flux_lora'], 0]
+ else:
+ prompt[NODES['guider']]["inputs"]["model"] = [NODES['flux_lora'], 0]
+ prompt[NODES['scheduler']]["inputs"]["model"] = [NODES['flux_lora'], 0]
+
+ # Delete unnecessary nodes
+ if "239" in prompt:
+ del prompt["239"] # SetUnionControlNetType
+ if "30" in prompt:
+ del prompt["30"] # EmptyLatentImage
+
+ if controlnet_info and "depth" in controlnet_info:
+ prompt[NODES['flux_lora_image']]["inputs"]["image"] = controlnet_info["depth"]['name']
+
+ # Connect final node to FluxGuidance
+ prompt[NODES['flux_guidance']]["inputs"]["conditioning"] = [final_node, 0]
+ # Note: No negative prompt is connected.
+
+ # Save prompt for debugging.
+ self._save_prompt_to_file(prompt, revision_dir)
+
+ # Execute generation via websocket.
+ # Execute generation and get results
+ ws = self._connect_to_websocket(server_address, client_id)
+
+ if ws is None:
+ return {"error": "conn_failed"} # Connection error
+
+ images = None
+ try:
+ images = self._execute_prompt_and_get_images(ws, prompt, client_id, server_address, NODES)
+ finally:
+ if ws:
+ ws.close()
+
+ if images is None or isinstance(images, dict) and "error" in images:
+ return {"error": "conn_failed"}
+
+ print(f"[StableGen] Flux image generated with prompt: {context.scene.comfyui_prompt}")
+
+ return images[NODES['save_image']][0]
+
+ def _create_img2img_base_prompt_flux(self, context):
+ """Creates and configures the base Flux prompt for img2img refinement."""
+ from ..util.workflow_templates import prompt_text_img2img_flux
+
+ prompt = json.loads(prompt_text_img2img_flux)
+
+ # Node IDs organized by functional category for Flux
+ NODES = {
+ # Text Prompting
+ 'pos_prompt': "6", # CLIPTextEncode for positive prompt
+
+ # Model Components
+ 'vae_loader': "10", # VAELoader
+ 'dual_clip': "11", # DualCLIPLoader
+ 'unet_loader': "12", # UNETLoader
+
+ # Sampling Control
+ 'sampler': "13", # SamplerCustomAdvanced
+ 'ksampler': "16", # KSamplerSelect
+ 'scheduler': "17", # BasicScheduler
+ 'guider': "22", # BasicGuider
+ 'noise': "25", # RandomNoise
+ 'flux_guidance': "26", # FluxGuidance
+
+ # Image Processing
+ 'vae_decode': "8", # VAEDecode
+ 'vae_encode': "116", # VAEEncode
+ 'vae_encode_inpaint': "44", # VAEEncodeForInpaint
+ 'upscale': "118", # ImageScale for upscaling
+ 'upscale_uv': "43", # ImageScale for UV maps
+
+ # Input Images
+ 'input_image': "1", # LoadImage for input
+ 'mask_image': "42", # LoadImage for mask
+ 'render_image': "117", # LoadImage for render
+
+ # Mask Processing
+ 'grow_mask': "224", # GrowMask
+ 'blur': "226", # ImageBlur
+ 'image_to_mask': "227", # ImageToMask
+ 'mask_to_image': "225", # MaskToImage
+
+ # Advanced Features
+ 'differential_diffusion': "50", # DifferentialDiffusion for Flux
+ 'inpaint_conditioning': "51", # InpaintModelConditioning for Flux
+
+ # Latent Space
+ 'latent': "30", # EmptyLatentImage
+
+ # Output
+ 'save_image': "32" # SaveImageWebsocket
+ }
+
+ 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
+ # Prepend the view description
+ base_prompt_text = f"{view_desc}, {base_prompt_text}"
+
+ # Set positive prompt (Flux doesn't use negative prompt)
+ prompt[NODES['pos_prompt']]["inputs"]["text"] = base_prompt_text
+
+ # Configure sampler parameters
+ prompt[NODES['noise']]["inputs"]["noise_seed"] = context.scene.seed
+ prompt[NODES['scheduler']]["inputs"]["steps"] = context.scene.refine_steps if context.scene.generation_method == 'grid' else context.scene.steps
+ prompt[NODES['scheduler']]["inputs"]["denoise"] = context.scene.denoise if context.scene.generation_method in ['grid', 'refine', 'local_edit'] else 1.0
+ prompt[NODES['flux_guidance']]["inputs"]["guidance"] = context.scene.refine_cfg if context.scene.generation_method == 'grid' else context.scene.cfg
+ prompt[NODES['ksampler']]["inputs"]["sampler_name"] = context.scene.refine_sampler if context.scene.generation_method == 'grid' else context.scene.sampler
+ prompt[NODES['scheduler']]["inputs"]["scheduler"] = context.scene.refine_scheduler if context.scene.generation_method == 'grid' else context.scene.scheduler
+
+ # Replace unet_loader with UNETLoaderGGUF if using GGUF model
+ if ".gguf" in context.scene.model_name:
+ del prompt[NODES['unet_loader']]
+ from ..util.workflow_templates import gguf_unet_loader
+ unet_loader_dict = json.loads(gguf_unet_loader)
+ prompt.update(unet_loader_dict)
+
+ # Set the model name
+ prompt[NODES['unet_loader']]["inputs"]["unet_name"] = context.scene.model_name
+
+ # Configure upscale settings
+ prompt[NODES['upscale']]["inputs"]["upscale_method"] = context.scene.refine_upscale_method
+ prompt[NODES['upscale']]["inputs"]["width"] = context.scene.render.resolution_x
+ prompt[NODES['upscale']]["inputs"]["height"] = context.scene.render.resolution_y
+
+ # Configure UV upscale settings
+ prompt[NODES['upscale_uv']]["inputs"]["upscale_method"] = "nearest-exact"
+ prompt[NODES['upscale_uv']]["inputs"]["width"] = 1024
+ prompt[NODES['upscale_uv']]["inputs"]["height"] = 1024
+
+ # Configure mask settings
+ prompt[NODES['grow_mask']]["inputs"]["expand"] = context.scene.grow_mask_by
+ prompt[NODES['blur']]["inputs"]["blur_radius"] = context.scene.blur_mask_radius
+ prompt[NODES['blur']]["inputs"]["sigma"] = context.scene.blur_mask_sigma
+
+ return prompt, NODES
+
+ def refine_flux(self, context, controlnet_info=None, mask_info=None, render_info=None, ipadapter_ref_info=None):
+ """
+ Refines the image using Flux 1 in ComfyUI.
+ :param context: Blender context.
+ :param controlnet_info: Dict of uploaded controlnet image info.
+ :param mask_info: Uploaded mask image info.
+ :param render_info: Uploaded render image info.
+ :param ipadapter_ref_info: Uploaded IPAdapter reference image info.
+ :return: Refined image.
+ """
+ # Setup connection parameters
+ server_address = context.preferences.addons[_ADDON_PKG].preferences.server_address
+ client_id = str(uuid.uuid4())
+ output_dir = context.preferences.addons[_ADDON_PKG].preferences.output_dir
+
+ revision_dir = get_generation_dirs(context)["revision"]
+
+ # Initialize the img2img prompt template for Flux
+ prompt, NODES = self._create_img2img_base_prompt_flux(context)
+
+ # Configure IPAdapter for Flux if enabled
+ if ipadapter_ref_info and context.scene.generation_method != 'uv_inpaint':
+ self.configure_ipadapter_flux(prompt, context, ipadapter_ref_info, NODES)
+
+ # Configure based on generation method
+ self._configure_refinement_mode_flux(prompt, context, render_info, mask_info, ipadapter_ref_info, NODES)
+
+ # Set up image inputs for different controlnet types
+ self._refine_configure_images_flux(prompt, render_info, NODES)
+
+ # Build ControlNet chain if not using Depth LoRA
+ if not context.scene.generation_method == 'uv_inpaint':
+ if not context.scene.use_flux_lora:
+ prompt = self._refine_build_controlnet_chain_flux(
+ context, prompt, controlnet_info, NODES
+ )
+ else: # If using Depth LoRA instead of ControlNet, we do not build a ControlNet chain
+ final_node = NODES['pos_prompt'] # Use positive prompt directly if not using ControlNet
+ # Add Required nodes for the FLUX.1-Depth-dev LoRA
+ from ..util.workflow_templates import depth_lora_flux
+ depth_lora_dict = json.loads(depth_lora_flux)
+ prompt.update(depth_lora_dict)
+
+ # Label nodes
+ NODES['flux_lora_image'] = "245" # LoadImage
+ NODES['instruct_pix'] = "246" # InstructPixToPixConditioning
+ NODES['flux_lora'] = "247" # LoraLoaderModelOnly
+
+ # Configure InstructPixToPixConditioning inputs to InpaintModelConditioning if using differential diffusion
+ if context.scene.differential_diffusion:
+ prompt[NODES['instruct_pix']]["inputs"]["positive"] = [NODES['inpaint_conditioning'], 0]
+ prompt[NODES['instruct_pix']]["inputs"]["negative"] = [NODES['inpaint_conditioning'], 1]
+
+ # Connect nodes
+ prompt[NODES['flux_guidance']]["inputs"]["conditioning"] = [NODES['instruct_pix'], 0]
+ # prompt[NODES['sampler']]["inputs"]["latent_image"] = [NODES['instruct_pix'], 2] # Not doing since we need to respect the mask
+
+ # If using ipadapter, set the apply_ipadapter_flux node to use the flux_lora_image
+ if ipadapter_ref_info and context.scene.generation_method != 'uv_inpaint':
+ prompt[NODES['ipadapter']]["inputs"]["model"] = [NODES['flux_lora'], 0]
+ prompt[NODES['differential_diffusion']]["inputs"]["model"] = [NODES['ipadapter'], 0]
+ else:
+ prompt[NODES['guider']]["inputs"]["model"] = [NODES['flux_lora'], 0]
+ prompt[NODES['scheduler']]["inputs"]["model"] = [NODES['flux_lora'], 0]
+ prompt[NODES['differential_diffusion']]["inputs"]["model"] = [NODES['flux_lora'], 0]
+
+ # Delete unnecessary nodes
+ if "239" in prompt:
+ del prompt["239"] # SetUnionControlNetType
+ if "30" in prompt:
+ del prompt["30"] # EmptyLatentImage
+
+ # Set the image for the Flux LoRA
+ if controlnet_info and "depth" in controlnet_info:
+ prompt[NODES['flux_lora_image']]["inputs"]["image"] = controlnet_info["depth"]['name']
+
+ # Save prompt for debugging
+ self._save_prompt_to_file(prompt, revision_dir)
+
+ # Execute generation and get results
+ ws = self._connect_to_websocket(server_address, client_id)
+
+ if ws is None:
+ return {"error": "conn_failed"} # Connection error
+
+ images = None
+ try:
+ images = self._execute_prompt_and_get_images(ws, prompt, client_id, server_address, NODES)
+ finally:
+ if ws:
+ ws.close()
+
+ if images is None or isinstance(images, dict) and "error" in images:
+ return {"error": "conn_failed"}
+
+ print(f"[StableGen] Image refined with Flux using prompt: {context.scene.comfyui_prompt}")
+
+ # Return the refined image
+ return images[NODES['save_image']][0]
+
+ def _configure_refinement_mode_flux(self, prompt, context, render_info, mask_info, ipadapter_ref_info, NODES):
+ """Configures the prompt based on the specific refinement mode for Flux."""
+ # Configure based on generation method
+ if context.scene.generation_method in ('refine', 'local_edit'):
+ # Configure for refine mode - load render directly
+ if render_info:
+ prompt[NODES['render_image']]["inputs"]["image"] = render_info['name']
+ prompt[NODES['vae_encode']]["inputs"]["pixels"] = [NODES['render_image'], 0]
+ # Connect latent to sampler
+ prompt[NODES['sampler']]["inputs"]["latent_image"] = [NODES['vae_encode'], 0]
+
+ elif context.scene.generation_method in ['uv_inpaint', 'sequential']:
+ # Configure for inpainting modes
+ if mask_info:
+ prompt[NODES['mask_image']]["inputs"]["image"] = mask_info['name']
+ if render_info:
+ prompt[NODES['input_image']]["inputs"]["image"] = render_info['name']
+
+ # Configure mask processing
+ if not context.scene.blur_mask:
+ prompt[NODES['vae_encode_inpaint']]["inputs"]["mask"] = [NODES['grow_mask'], 0]
+ if context.scene.differential_diffusion:
+ prompt[NODES['inpaint_conditioning']]["inputs"]["mask"] = [NODES['grow_mask'], 0]
+ else:
+ # Configure blur chain
+ prompt[NODES['image_to_mask']]["inputs"]["image"] = [NODES['blur'], 0]
+ prompt[NODES['vae_encode_inpaint']]["inputs"]["mask"] = [NODES['image_to_mask'], 0]
+
+ # Different setups based on differential diffusion
+ if context.scene.differential_diffusion:
+ # Connect differential diffusion between model loader and other components
+ prompt[NODES['guider']]["inputs"]["model"] = [NODES['differential_diffusion'], 0]
+ prompt[NODES['scheduler']]["inputs"]["model"] = [NODES['differential_diffusion'], 0]
+
+ # Connect inpaint conditioning to differential diffusion
+ if ipadapter_ref_info:
+ prompt[NODES['differential_diffusion']]["inputs"]["model"] = [NODES['ipadapter'], 0]
+ else:
+ prompt[NODES['differential_diffusion']]["inputs"]["model"] = [NODES['unet_loader'], 0]
+
+ # Configure inpaint conditioning with proper input image and mask
+ prompt[NODES['inpaint_conditioning']]["inputs"]["pixels"] = [
+ NODES['upscale_uv'], 0
+ ] if context.scene.generation_method == 'uv_inpaint' else [NODES['input_image'], 0]
+
+ # Connect latent to sampler from inpaint conditioning
+ prompt[NODES['sampler']]["inputs"]["latent_image"] = [NODES['inpaint_conditioning'], 2]
+
+ # Connect conditioning to flux_guidance
+ prompt[NODES['flux_guidance']]["inputs"]["conditioning"] = [NODES['inpaint_conditioning'], 0]
+ else:
+ # Standard setup without differential diffusion
+ prompt[NODES['sampler']]["inputs"]["latent_image"] = [NODES['vae_encode_inpaint'], 0]
+
+ if context.scene.generation_method == 'uv_inpaint':
+ self._configure_uv_inpainting_mode_flux(prompt, context, render_info, NODES)
+ else: # Sequential mode
+ self._configure_sequential_mode_flux(prompt, context, NODES)
+
+ def _configure_uv_inpainting_mode_flux(self, prompt, context, render_info, NODES):
+ """Configures the prompts for UV inpainting mode in Flux."""
+ # UV inpainting specific configuration
+ prompt[NODES['upscale_uv']]["inputs"]["image"] = [NODES['input_image'], 0]
+
+ if not context.scene.differential_diffusion:
+ prompt[NODES['vae_encode_inpaint']]["inputs"]["pixels"] = [NODES['upscale_uv'], 0]
+ else:
+ # Set the noise_mask flag according to context.scene.differential_noise
+ prompt[NODES['inpaint_conditioning']]["inputs"]["noise_mask"] = context.scene.differential_noise
+
+ # Create UV-specific prompt
+ uv_prompt = f"seamless (UV-unwrapped texture) of {_texturing_prompt(context.scene)}, consistent material continuity, no visible seams or stretching"
+ prompt[NODES['pos_prompt']]["inputs"]["text"] = uv_prompt
+
+ # Object-specific prompt if available
+ if render_info and 'name' in render_info:
+ current_object_name = os.path.basename(render_info['name']).split('.')[0]
+ object_prompt = self.operator._object_prompts.get(current_object_name, _texturing_prompt(context.scene))
+ if object_prompt:
+ uv_prompt = f"(UV-unwrapped texture) of {object_prompt}, consistent material continuity, no visible seams or stretching"
+ prompt[NODES['pos_prompt']]["inputs"]["text"] = uv_prompt
+
+ def _configure_sequential_mode_flux(self, prompt, context, NODES):
+ """Configures the prompt for sequential generation mode in Flux."""
+ # Direct connection for sequential mode
+ if not context.scene.differential_diffusion:
+ prompt[NODES['vae_encode_inpaint']]["inputs"]["pixels"] = [NODES['input_image'], 0]
+ else:
+ # Set the noise_mask flag according to context.scene.differential_noise
+ prompt[NODES['inpaint_conditioning']]["inputs"]["noise_mask"] = context.scene.differential_noise
+
+ def _refine_configure_images_flux(self, prompt, render_info, NODES):
+ """Configures the input images for the refinement process in Flux."""
+ # Set render image if provided
+ if render_info:
+ prompt[NODES['render_image']]["inputs"]["image"] = render_info['name']
+
+ # Control images are handled by the controlnet chain builder
+
+ def _refine_build_controlnet_chain_flux(self, context, prompt, controlnet_info, NODES):
+ """Builds the ControlNet chain for refinement process with Flux."""
+ input = NODES['pos_prompt'] if not context.scene.differential_diffusion else NODES['inpaint_conditioning']
+ # For Flux, the controlnet chain connects to the guidance node
+ prompt, final_node = self._build_controlnet_chain_extended(
+ context, prompt, input, input, NODES['vae_loader'],
+ controlnet_info
+ )
+ # Connect final node to FluxGuidance conditioning input
+ prompt[NODES['flux_guidance']]["inputs"]["conditioning"] = [final_node, 0]
+ return prompt
diff --git a/stablegen/ui/__init__.py b/stablegen/ui/__init__.py
new file mode 100644
index 0000000..6abb9fd
--- /dev/null
+++ b/stablegen/ui/__init__.py
@@ -0,0 +1,38 @@
+"""UI components for the StableGen addon."""
+
+# -- presets.py ---------------------------------------------------------------
+from .presets import ( # noqa: F401
+ PRESETS,
+ GEN_PARAMETERS,
+ get_preset_items,
+ update_parameters,
+ ResetQwenPrompt,
+ ApplyPreset,
+ SwitchToMeshGeneration,
+ SavePreset,
+ DeletePreset,
+)
+
+# -- panel.py -----------------------------------------------------------------
+from .panel import StableGenPanel # noqa: F401
+
+# -- queue.py -----------------------------------------------------------------
+from .queue import ( # noqa: F401
+ SceneQueueItem,
+ _sg_queue_load_handler,
+ _sg_queue_save,
+ _sg_queue_load,
+ SG_UL_SceneQueueList,
+ SceneQueueAdd,
+ SceneQueueRemove,
+ SceneQueueClear,
+ SceneQueueMoveUp,
+ SceneQueueMoveDown,
+ SceneQueueOpenResult,
+ SceneQueueInvalidate,
+ SceneQueueProcess,
+ _resume_queue,
+ _queue_tick,
+ _persist_queue,
+ _tag_redraw,
+)
diff --git a/stablegen/ui/model_units.py b/stablegen/ui/model_units.py
new file mode 100644
index 0000000..a44d6a5
--- /dev/null
+++ b/stablegen/ui/model_units.py
@@ -0,0 +1,527 @@
+"""ControlNet / LoRA PropertyGroups and their CRUD + refresh operators.
+
+Contains:
+- ``ControlNetUnit``, ``LoRAUnit`` — CollectionProperty item types
+- ``get_controlnet_models``, ``get_lora_models`` — EnumProperty callbacks
+- ``RefreshCheckpointList``, ``RefreshLoRAList`` — async refresh operators
+- ``AddControlNetUnit``, ``RemoveControlNetUnit``
+- ``AddLoRAUnit``, ``RemoveLoRAUnit``
+- ``update_model_list``, ``update_union``, ``update_controlnet``
+"""
+
+import bpy # pylint: disable=import-error
+
+from .presets import update_parameters
+from ..utils import sg_modal_active
+from ..core import ADDON_PKG
+from ..core.server_api import _fetch_api_list
+from ..core.state import (
+ _cached_checkpoint_list,
+ _cached_checkpoint_architecture,
+ _cached_lora_list,
+ _dec_pending_refreshes,
+ _inc_pending_refreshes,
+ _run_async,
+)
+
+
+# ── Enum callbacks for model dropdowns ─────────────────────────────────────
+
+def update_model_list(self, context):
+ """Returns the cached list of checkpoint/unet models."""
+ from ..core import state as _state
+ if not _state._cached_checkpoint_list:
+ return [("NONE_AVAILABLE", "None available", "Fetch models from server")]
+ return _state._cached_checkpoint_list
+
+
+def update_union(self, context):
+ if "union" in self.model_name.lower() or "promax" in self.model_name.lower():
+ self.is_union = True
+ else:
+ self.is_union = False
+
+
+def update_controlnet(self, context):
+ update_parameters(self, context)
+ update_union(self, context)
+ return None
+
+
+def get_controlnet_models(context, unit_type):
+ """Get available ControlNet models suitable for *unit_type*."""
+ items = []
+ prefs = context.preferences.addons.get(ADDON_PKG)
+ if not prefs:
+ return [("NO_PREFS", "Addon Error", "Could not access preferences")]
+
+ mappings = prefs.preferences.controlnet_model_mappings
+
+ if not mappings:
+ return [("REFRESH", "Refresh List in Prefs", "Fetch models via Preferences")]
+
+ prop_name = f"supports_{unit_type}"
+
+ found_count = 0
+ for item in mappings:
+ if hasattr(item, prop_name):
+ if getattr(item, prop_name):
+ items.append((item.name, item.name, f"ControlNet: {item.name}"))
+ found_count += 1
+
+ if found_count == 0:
+ return [("NO_ASSIGNED", f"No models assigned to '{unit_type}'",
+ "Assign types in Addon Preferences or Refresh")]
+
+ items.sort(key=lambda x: x[1])
+ return items
+
+
+def get_lora_models(self, context):
+ """Returns the cached list of LoRA models."""
+ from ..core import state as _state
+ if not _state._cached_lora_list:
+ return [("NONE_AVAILABLE", "None available", "Fetch models from server")]
+ return _state._cached_lora_list
+
+
+# ── PropertyGroups ─────────────────────────────────────────────────────────
+
+class ControlNetUnit(bpy.types.PropertyGroup):
+ unit_type: bpy.props.StringProperty(
+ name="Type",
+ description="ControlNet type (e.g. 'depth', 'canny')",
+ default="",
+ update=update_parameters
+ ) # type: ignore
+ model_name: bpy.props.EnumProperty(
+ name="Model",
+ description="Select the ControlNet model",
+ items=lambda self, context: get_controlnet_models(context, self.unit_type),
+ update=update_controlnet
+ ) # type: ignore
+ strength: bpy.props.FloatProperty(
+ name="Strength",
+ description="Strength of the ControlNet effect",
+ default=0.5, min=0.0, max=3.0,
+ update=update_parameters
+ ) # type: ignore
+ start_percent: bpy.props.FloatProperty(
+ name="Start",
+ description="Start percentage (/100)",
+ default=0.0, min=0.0, max=1.0,
+ update=update_parameters
+ ) # type: ignore
+ end_percent: bpy.props.FloatProperty(
+ name="End",
+ description="End percentage (/100)",
+ default=1.0, min=0.0, max=1.0,
+ update=update_parameters
+ ) # type: ignore
+ is_union: bpy.props.BoolProperty(
+ name="Is Union Type",
+ description="Is this a union ControlNet?",
+ default=False,
+ update=update_parameters
+ ) # type: ignore
+ use_union_type: bpy.props.BoolProperty(
+ name="Use Union Type",
+ description="Use union type for ControlNet",
+ default=True,
+ update=update_parameters
+ ) # type: ignore
+
+
+class LoRAUnit(bpy.types.PropertyGroup):
+ model_name: bpy.props.EnumProperty(
+ name="LoRA Model",
+ description="Select the LoRA model file",
+ items=lambda self, context: get_lora_models(self, context),
+ update=update_parameters
+ ) # type: ignore
+ model_strength: bpy.props.FloatProperty(
+ name="Model Strength",
+ description="Strength of the LoRA's effect on the model's weights",
+ default=1.0, min=0.0, max=100.0,
+ update=update_parameters
+ ) # type: ignore
+ clip_strength: bpy.props.FloatProperty(
+ name="CLIP Strength",
+ description="Strength of the LoRA's effect on the CLIP/text conditioning",
+ default=1.0, min=0.0, max=100.0,
+ update=update_parameters
+ ) # type: ignore
+
+
+# ── Refresh operators ──────────────────────────────────────────────────────
+
+class RefreshCheckpointList(bpy.types.Operator):
+ """Fetches Checkpoint/UNET models from ComfyUI API and updates the cache."""
+ bl_idname = "stablegen.refresh_checkpoint_list"
+ bl_label = "Refresh Checkpoint/UNET List"
+ bl_description = "Connect to ComfyUI server to get available Checkpoint/UNET models"
+
+ @classmethod
+ def poll(cls, context):
+ prefs = context.preferences.addons.get(ADDON_PKG)
+ if not prefs or not prefs.preferences.server_address:
+ cls.poll_message_set("Server address not configured (check addon preferences)")
+ return False
+ if sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ return True
+
+ def execute(self, context):
+ from ..core import state as _state
+
+ prefs = context.preferences.addons.get(ADDON_PKG)
+ if not prefs:
+ self.report({'ERROR'}, "Cannot access addon preferences.")
+ return {'CANCELLED'}
+
+ server_address = prefs.preferences.server_address
+ architecture = getattr(context.scene, "model_architecture", "sdxl")
+
+ def _bg_work():
+ model_list = None
+ if architecture == 'sdxl':
+ model_list = _fetch_api_list(server_address, "/models/checkpoints")
+ model_type_desc = "Checkpoint"
+ elif architecture in ('flux1', 'qwen_image_edit'):
+ model_list = _fetch_api_list(server_address, "/models/unet_gguf")
+ if model_list is not None:
+ extra = _fetch_api_list(server_address, "/models/diffusion_models")
+ if extra:
+ model_list.extend(extra)
+ model_type_desc = "UNET" if architecture == 'flux1' else "UNET (GGUF/Safetensors)"
+ elif architecture == 'flux2_klein':
+ model_list = _fetch_api_list(server_address, "/models/diffusion_models")
+ model_type_desc = "Diffusion Model"
+ else:
+ model_type_desc = "Model"
+ return {'model_list': model_list, 'architecture': architecture,
+ 'model_type_desc': model_type_desc}
+
+ def _on_done(result):
+ _state._dec_pending_refreshes()
+ if result is None:
+ return
+
+ model_list = result.get('model_list')
+ arch = result.get('architecture')
+ desc = result.get('model_type_desc', 'Model')
+
+ if model_list is None:
+ _state._cached_checkpoint_list = [("NO_SERVER", "Set Server Address", "Cannot fetch")]
+ _state._cached_checkpoint_architecture = None
+ print("[StableGen] Checkpoint refresh: cannot reach server.")
+ elif not model_list:
+ _state._cached_checkpoint_list = [("NONE_FOUND", f"No {desc}s Found", "Server list is empty")]
+ _state._cached_checkpoint_architecture = arch
+ print(f"[StableGen] Checkpoint refresh: no {desc} models found.")
+ else:
+ items = []
+ for name in sorted(model_list):
+ items.append((name, name, f"{desc}: {name}"))
+ _state._cached_checkpoint_list = items
+ _state._cached_checkpoint_architecture = arch
+ print(f"[StableGen] Checkpoint refresh: {len(items)} {desc}(s) found.")
+
+ scene = bpy.context.scene if hasattr(bpy.context, 'scene') else None
+ if scene:
+ valid_ids = {it[0] for it in _state._cached_checkpoint_list}
+ backup = getattr(scene, 'sg_model_name_backup', '')
+ if backup and backup in valid_ids:
+ if scene.model_name != backup:
+ scene.model_name = backup
+ elif scene.model_name not in valid_ids:
+ placeholder = next((it[0] for it in _state._cached_checkpoint_list
+ if it[0].startswith("NO_") or it[0] == "NONE_FOUND"), None)
+ if placeholder:
+ scene.model_name = placeholder
+ elif _state._cached_checkpoint_list:
+ scene.model_name = _state._cached_checkpoint_list[0][0]
+
+ for window in bpy.context.window_manager.windows:
+ for area in window.screen.areas:
+ area.tag_redraw()
+
+ _run_async(_bg_work, _on_done)
+ _inc_pending_refreshes()
+ self.report({'INFO'}, "Fetching checkpoint list...")
+ return {'FINISHED'}
+
+
+class RefreshLoRAList(bpy.types.Operator):
+ """Fetches LoRA models from ComfyUI API and updates the cache."""
+ bl_idname = "stablegen.refresh_lora_list"
+ bl_label = "Refresh LoRA List"
+ bl_description = "Connect to ComfyUI server to get available LoRA models"
+
+ @classmethod
+ def poll(cls, context):
+ prefs = context.preferences.addons.get(ADDON_PKG)
+ if not prefs or not prefs.preferences.server_address:
+ cls.poll_message_set("Server address not configured (check addon preferences)")
+ return False
+ if sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ return True
+
+ def execute(self, context):
+ from ..core import state as _state
+
+ prefs = context.preferences.addons.get(ADDON_PKG)
+ if not prefs:
+ self.report({'ERROR'}, "Cannot access addon preferences.")
+ return {'CANCELLED'}
+
+ server_address = prefs.preferences.server_address
+
+ def _bg_work():
+ return {'lora_list': _fetch_api_list(server_address, "/models/loras")}
+
+ def _on_done(result):
+ _state._dec_pending_refreshes()
+ if result is None:
+ return
+
+ lora_list = result.get('lora_list')
+
+ if lora_list is None:
+ _state._cached_lora_list = [("NO_SERVER", "Set Server Address", "Cannot fetch")]
+ print("[StableGen] LoRA refresh: cannot reach server.")
+ elif not lora_list:
+ _state._cached_lora_list = [("NONE_FOUND", "No LoRAs Found", "Server list is empty")]
+ print("[StableGen] LoRA refresh: no models found.")
+ else:
+ items = []
+ for name in sorted(lora_list):
+ items.append((name, name, f"LoRA: {name}"))
+ _state._cached_lora_list = items
+ print(f"[StableGen] LoRA refresh: {len(items)} model(s) found.")
+
+ scene = bpy.context.scene if hasattr(bpy.context, 'scene') else None
+ if scene and hasattr(scene, 'lora_units'):
+ valid_ids = {it[0] for it in _state._cached_lora_list}
+ placeholder = next((it[0] for it in _state._cached_lora_list
+ if it[0].startswith("NO_") or it[0] == "NONE_FOUND"), None)
+ indices_to_remove = []
+ for i, unit in enumerate(scene.lora_units):
+ if unit.model_name not in valid_ids or unit.model_name == placeholder:
+ indices_to_remove.append(i)
+ for i in sorted(indices_to_remove, reverse=True):
+ scene.lora_units.remove(i)
+
+ num_loras = len(scene.lora_units)
+ if scene.lora_units_index >= num_loras:
+ scene.lora_units_index = max(0, num_loras - 1)
+ elif num_loras == 0:
+ scene.lora_units_index = 0
+
+ for window in bpy.context.window_manager.windows:
+ for area in window.screen.areas:
+ area.tag_redraw()
+
+ _run_async(_bg_work, _on_done)
+ _inc_pending_refreshes()
+ self.report({'INFO'}, "Fetching LoRA list...")
+ return {'FINISHED'}
+
+
+# ── Add / Remove operators ─────────────────────────────────────────────────
+
+class AddControlNetUnit(bpy.types.Operator):
+ bl_idname = "stablegen.add_controlnet_unit"
+ bl_label = "Add ControlNet Unit"
+ bl_description = "Add a ControlNet Unit. Only one unit per type is allowed."
+
+ unit_type: bpy.props.EnumProperty(
+ name="Type",
+ items=[('depth', 'Depth', ''), ('canny', 'Canny', ''), ('normal', 'Normal', '')],
+ default='depth',
+ update=update_parameters
+ ) # type: ignore
+
+ model_name: bpy.props.EnumProperty(
+ name="Model",
+ description="Select the ControlNet model",
+ items=lambda self, context: get_controlnet_models(context, self.unit_type),
+ update=update_parameters
+ ) # type: ignore
+
+ @classmethod
+ def poll(cls, context):
+ return not sg_modal_active(context)
+
+ def invoke(self, context, event):
+ return context.window_manager.invoke_props_dialog(self)
+
+ def draw(self, context):
+ layout = self.layout
+ layout.prop(self, "unit_type")
+ models = get_controlnet_models(context, self.unit_type)
+ if len(models) > 1:
+ layout.prop(self, "model_name")
+
+ def execute(self, context):
+ units = context.scene.controlnet_units
+ for unit in units:
+ if unit.unit_type == self.unit_type:
+ self.report({'WARNING'}, f"Unit '{self.unit_type}' already exists.")
+ return {'CANCELLED'}
+ new_unit = units.add()
+ new_unit.unit_type = self.unit_type
+ new_unit.model_name = self.model_name
+ new_unit.strength = 0.5
+ new_unit.start_percent = 0.0
+ new_unit.end_percent = 1.0
+ if "union" in new_unit.model_name.lower() or "promax" in new_unit.model_name.lower():
+ new_unit.is_union = True
+ context.scene.controlnet_units_index = len(units) - 1
+ for area in context.screen.areas:
+ area.tag_redraw()
+ return {'FINISHED'}
+
+
+class RemoveControlNetUnit(bpy.types.Operator):
+ bl_idname = "stablegen.remove_controlnet_unit"
+ bl_label = "Remove ControlNet Unit"
+ bl_description = "Remove the selected ControlNet Unit"
+
+ unit_type: bpy.props.EnumProperty(
+ name="Type",
+ items=[('depth', 'Depth', ''), ('canny', 'Canny', ''), ('normal', 'Normal', '')],
+ default='depth',
+ update=update_parameters
+ ) # type: ignore
+
+ @classmethod
+ def poll(cls, context):
+ return not sg_modal_active(context)
+
+ def invoke(self, context, event):
+ units = context.scene.controlnet_units
+ if len(units) == 1:
+ self.unit_type = units[0].unit_type
+ return self.execute(context)
+ return context.window_manager.invoke_props_dialog(self)
+
+ def draw(self, context):
+ layout = self.layout
+ layout.prop(self, "unit_type")
+
+ def execute(self, context):
+ units = context.scene.controlnet_units
+ for index, unit in enumerate(units):
+ if unit.unit_type == self.unit_type:
+ units.remove(index)
+ context.scene.controlnet_units_index = min(max(0, index - 1), len(units) - 1)
+ update_parameters(self, context)
+ for area in context.screen.areas:
+ area.tag_redraw()
+ return {'FINISHED'}
+ self.report({'WARNING'}, f"No unit of type '{self.unit_type}' found.")
+ return {'CANCELLED'}
+
+
+class AddLoRAUnit(bpy.types.Operator):
+ bl_idname = "stablegen.add_lora_unit"
+ bl_label = "Add LoRA Unit"
+ bl_description = "Add a LoRA to the chain. Disabled if no LoRAs are available or all available LoRAs have been added."
+
+ @classmethod
+ def poll(cls, context):
+ scene = context.scene
+ addon_prefs = context.preferences.addons.get(ADDON_PKG)
+
+ if not addon_prefs:
+ return False
+ addon_prefs = addon_prefs.preferences
+
+ lora_enum_items = get_lora_models(scene, context)
+
+ placeholder_ids = {"NONE_AVAILABLE", "NO_COMFYUI_DIR_LORA", "NO_LORAS_SUBDIR",
+ "PERM_ERROR", "SCAN_ERROR", "NONE_FOUND"}
+
+ available_lora_files_count = sum(1 for item in lora_enum_items if item[0] not in placeholder_ids)
+
+ if available_lora_files_count == 0:
+ cls.poll_message_set("No LoRA model files found in any specified directory (including subdirectories).")
+ return False
+
+ num_current_lora_units = len(scene.lora_units)
+ if num_current_lora_units >= available_lora_files_count:
+ cls.poll_message_set("All available distinct LoRA models appear to have corresponding units.")
+ return False
+
+ return not sg_modal_active(context)
+
+ def execute(self, context):
+ loras = context.scene.lora_units
+ new_lora = loras.add()
+
+ all_lora_enum_items = get_lora_models(context.scene, context)
+
+ placeholder_ids = {"NONE_AVAILABLE", "NO_COMFYUI_DIR_LORA", "NO_LORAS_SUBDIR",
+ "PERM_ERROR", "SCAN_ERROR", "NONE_FOUND"}
+ available_lora_identifiers = [item[0] for item in all_lora_enum_items if item[0] not in placeholder_ids]
+
+ if available_lora_identifiers:
+ current_lora_model_identifiers_in_use = {unit.model_name for unit in loras
+ if unit.model_name and unit.model_name not in placeholder_ids}
+
+ assigned_model = None
+ for lora_id in available_lora_identifiers:
+ if lora_id not in current_lora_model_identifiers_in_use:
+ assigned_model = lora_id
+ break
+
+ if not assigned_model:
+ assigned_model = available_lora_identifiers[0]
+
+ if assigned_model:
+ try:
+ new_lora.model_name = assigned_model
+ except TypeError:
+ print(f"[StableGen] AddLoRAUnit Execute: TypeError assigning model '{assigned_model}'. Enum might not be ready.")
+
+ new_lora.model_strength = 1.0
+ new_lora.clip_strength = 1.0
+ context.scene.lora_units_index = len(loras) - 1
+
+ update_parameters(self, context)
+
+ for area in context.screen.areas:
+ if area.type in ('VIEW_3D', 'PROPERTIES'):
+ area.tag_redraw()
+
+ return {'FINISHED'}
+
+
+class RemoveLoRAUnit(bpy.types.Operator):
+ bl_idname = "stablegen.remove_lora_unit"
+ bl_label = "Remove Selected LoRA Unit"
+ bl_description = "Remove the selected LoRA from the chain"
+
+ @classmethod
+ def poll(cls, context):
+ scene = context.scene
+ return (len(scene.lora_units) > 0 and
+ 0 <= scene.lora_units_index < len(scene.lora_units)
+ and not sg_modal_active(context))
+
+ def execute(self, context):
+ loras = context.scene.lora_units
+ index = context.scene.lora_units_index
+ if 0 <= index < len(loras):
+ loras.remove(index)
+ context.scene.lora_units_index = min(max(0, index - 1), len(loras) - 1)
+ update_parameters(self, context)
+ for area in context.screen.areas:
+ area.tag_redraw()
+ return {'FINISHED'}
+ self.report({'WARNING'}, "No LoRA unit selected or list is empty.")
+ return {'CANCELLED'}
diff --git a/stablegen/ui/panel.py b/stablegen/ui/panel.py
new file mode 100644
index 0000000..b6513d7
--- /dev/null
+++ b/stablegen/ui/panel.py
@@ -0,0 +1,1918 @@
+"""Main StableGen UI panel and preset diff helpers."""
+
+import os
+import bpy # pylint: disable=import-error
+import mathutils # pylint: disable=import-error
+import math
+from ..utils import sg_modal_active
+from .presets import PRESETS, GEN_PARAMETERS
+from . import queue as _queue_mod
+
+_ADDON_PKG = __package__.rsplit('.', 1)[0]
+
+def _is_refreshing():
+ """Return True while async model-list refreshes are in-flight.
+
+ If the counter has been stuck for longer than ``_REFRESH_TIMEOUT``
+ seconds (e.g. due to a lost timer), force-reset it to 0 so the UI
+ is not permanently blocked.
+ """
+ import time as _time
+ from ..core import state as _state
+ count = _state._pending_refreshes
+ if count <= 0:
+ return False
+ started = _state._refresh_started_at
+ if started > 0 and (_time.monotonic() - started) > _state._REFRESH_TIMEOUT:
+ # Safety net: force-clear a stuck counter
+ print(f"[StableGen] Refreshing model lists stuck for >{_state._REFRESH_TIMEOUT:.0f}s – resetting.")
+ _state._pending_refreshes = 0
+ _state._refresh_started_at = 0.0
+ return False
+ return True
+
+# Stock presets
+
+_PRESET_DIFF_CORE = [
+ 'architecture_mode', 'model_architecture', 'steps', 'cfg',
+ 'sampler', 'scheduler', 'generation_method', 'denoise',
+ 'generation_mode', 'pbr_decomposition',
+]
+
+# Known enum values that need specific casing in diff labels.
+_DISPLAY_NAMES = {
+ 'sdxl': 'SDXL',
+ 'flux1': 'FLUX.1',
+ 'qwen_image_edit': 'Qwen',
+ 'flux2_klein': 'FLUX.2 Klein',
+ 'trellis2': 'TRELLIS.2',
+ 'standard': 'Standard',
+}
+
+def _fmt_diff_val(v):
+ """Format a property value for compact display in the diff preview."""
+ if isinstance(v, bool):
+ return 'On' if v else 'Off'
+ if isinstance(v, float):
+ # Strip trailing zeros: 1.50 → "1.5", 8.00 → "8"
+ return f'{v:.2f}'.rstrip('0').rstrip('.')
+ if isinstance(v, str):
+ if v in _DISPLAY_NAMES:
+ return _DISPLAY_NAMES[v]
+ s = v.replace('_', ' ').title()
+ return s[:20] + '…' if len(s) > 22 else s
+ return str(v)
+
+
+def _preset_diff(context):
+ """Return list of (param, current_formatted, preset_formatted) for
+ parameters that differ between the scene and the selected preset.
+ Core parameters are listed first."""
+ scene = context.scene
+ preset_key = scene.stablegen_preset
+ if preset_key not in PRESETS:
+ return []
+ preset = PRESETS[preset_key]
+ core_diffs = []
+ other_diffs = []
+ core_set = set(_PRESET_DIFF_CORE)
+ for key in GEN_PARAMETERS:
+ if key not in preset or not hasattr(scene, key):
+ continue
+ current = getattr(scene, key)
+ target = preset[key]
+ try:
+ if isinstance(current, (int, float)) and isinstance(target, (int, float)):
+ if math.isclose(float(current), float(target), rel_tol=1e-7, abs_tol=0.0):
+ continue
+ elif current == target:
+ continue
+ except Exception:
+ continue
+ entry = (key, _fmt_diff_val(current), _fmt_diff_val(target))
+ if key in core_set:
+ core_diffs.append(entry)
+ else:
+ other_diffs.append(entry)
+ # Sort core diffs in the defined display order
+ core_order = {k: i for i, k in enumerate(_PRESET_DIFF_CORE)}
+ core_diffs.sort(key=lambda x: core_order.get(x[0], 999))
+ return core_diffs + other_diffs
+
+
+
+class StableGenPanel(bpy.types.Panel):
+ """
+ Creates the main UI panel for the StableGen addon.
+ """
+ bl_label = "StableGen"
+ bl_idname = "OBJECT_PT_stablegen"
+ bl_space_type = "VIEW_3D"
+ bl_region_type = "UI"
+ bl_category = "StableGen"
+ bl_context = "objectmode"
+ bl_ui_units_x = 600
+
+ def draw_header(self, _):
+ """
+ Draws the header of the panel.
+ :param _: Unused parameter.
+ :return: None
+ """
+ self.layout.label(icon="WORLD_DATA")
+
+ def draw(self, context):
+ """
+ Draws the panel with reorganized Advanced Parameters.
+ :param context: Blender context.
+ :return: None
+ """
+ layout = self.layout
+ scene = context.scene # Get the scene for easier access
+
+ # Detect the current width of the panel
+ region = context.region
+ width = region.width
+ width_mode = 'narrow' if width < 420 else 'wide'
+
+ # Compute properties that differ from the pending (unapplied) preset.
+ # Used to highlight affected fields inline with alert coloring.
+ # Dict maps param name → formatted target value for "→ X" labels.
+ _diff_props = {}
+ if (hasattr(scene, 'active_preset')
+ and scene.stablegen_preset != 'CUSTOM'
+ and getattr(scene, 'active_preset', '') != scene.stablegen_preset):
+ _diff_props = {d[0]: d[2] for d in _preset_diff(context)}
+
+ # --- Action Buttons & Progress ---
+ cam_tools_row = layout.row()
+ cam_tools_row.operator("object.add_cameras", text="Add Cameras", icon="CAMERA_DATA")
+ if width_mode == 'narrow':
+ cam_tools_row = layout.row()
+ cam_tools_row.operator("object.collect_camera_prompts", text="Collect Camera Prompts", icon="FILE_TEXT")
+
+ cam_extra_row = layout.row(align=True)
+ cam_extra_row.operator("object.clone_camera", text="Clone Camera", icon="DUPLICATE")
+ cam_extra_row.operator("object.mirror_camera", text="Mirror", icon="MOD_MIRROR")
+ cam_extra_row.operator("object.toggle_camera_labels", text="Labels", icon="FONT_DATA")
+
+
+ addon_prefs = context.preferences.addons[_ADDON_PKG].preferences
+ config_error_message = None
+
+ if not os.path.exists(addon_prefs.output_dir):
+ config_error_message = "Output Path Invalid"
+ elif not addon_prefs.server_address:
+ config_error_message = "Server Address Missing"
+ elif not addon_prefs.server_online:
+ config_error_message = "Cannot reach server"
+
+ # Determine if we are in TRELLIS.2 mode (used by generate button & later sections)
+ _arch_mode = getattr(scene, 'architecture_mode', 'sdxl')
+ _is_trellis2_mode = (_arch_mode == 'trellis2')
+
+ # ── Prerequisite warnings (shown inline before the Generate button) ──
+ if not _is_trellis2_mode and not config_error_message:
+ has_cameras = any(obj.type == 'CAMERA' for obj in scene.objects)
+ has_meshes = any(obj.type == 'MESH' for obj in context.view_layer.objects if not obj.hide_get())
+ if not has_meshes:
+ warn_box = layout.box()
+ warn_row = warn_box.row()
+ warn_row.alert = True
+ warn_row.label(text="No visible mesh objects", icon='ERROR')
+ warn_row.operator("stablegen.switch_to_mesh_generation",
+ text="Mesh Generation", icon='MESH_DATA')
+ if not has_cameras:
+ warn_box = layout.box()
+ warn_row = warn_box.row()
+ warn_row.alert = True
+ warn_row.label(text="No cameras in the scene", icon='ERROR')
+ warn_row.operator("object.add_cameras", text="Add Cameras", icon='CAMERA_DATA')
+
+ action_row = layout.row()
+ action_row.scale_y = 2.0 # Scale the row vertically
+
+ # Show a "Refreshing…" indicator while async model fetches are in-flight
+ if _is_refreshing():
+ refresh_row = layout.row()
+ refresh_row.alignment = 'CENTER'
+ refresh_row.label(text="Refreshing model lists...", icon="SORTTIME")
+
+ if _is_trellis2_mode:
+ # --- TRELLIS.2 Generate Button ---
+ trellis2_op = next(
+ (op for win in context.window_manager.windows
+ for op in win.modal_operators
+ if op.bl_idname == 'OBJECT_OT_trellis2_generate'),
+ None
+ )
+ # Also look for ComfyUIGenerate running as the texturing phase
+ comfy_tex_op = next(
+ (op for win in context.window_manager.windows
+ for op in win.modal_operators
+ if op.bl_idname == 'OBJECT_OT_test_stable'),
+ None
+ ) if not trellis2_op else None
+
+ if config_error_message:
+ if config_error_message == "Cannot reach server":
+ split = action_row.split(factor=0.85, align=True)
+ split.operator("object.trellis2_generate", text="Cannot generate: " + config_error_message, icon="ERROR")
+ split.operator("stablegen.check_server_status", text="", icon='FILE_REFRESH')
+ else:
+ action_row.operator("object.trellis2_generate", text="Cannot generate: " + config_error_message, icon="ERROR")
+ action_row.enabled = False
+
+ elif trellis2_op:
+ # ── Phases 1 & 2: Trellis2Generate is alive ──
+ action_row.operator("object.trellis2_generate", text="Cancel TRELLIS.2", icon="CANCEL")
+ progress_col = layout.column()
+
+ # Bar 1 — Overall
+ overall_pct = getattr(trellis2_op, '_overall_progress', 0)
+ overall_label = getattr(trellis2_op, '_overall_stage', 'Initializing')
+ progress_col.progress(
+ text=f"{overall_label} ({overall_pct:.0f}%)",
+ factor=max(0.0, min(overall_pct / 100.0, 1.0))
+ )
+
+ # Bar 2 — Phase
+ phase_pct = getattr(trellis2_op, '_phase_progress', 0)
+ phase_label = getattr(trellis2_op, '_phase_stage', '')
+ if phase_label:
+ progress_col.progress(
+ text=f"{phase_label} ({phase_pct:.0f}%)",
+ factor=max(0.0, min(phase_pct / 100.0, 1.0))
+ )
+
+ # Bar 3 — Detail (only when there's actual sampler progress)
+ detail_pct = getattr(trellis2_op, '_detail_progress', 0)
+ detail_label = getattr(trellis2_op, '_detail_stage', '')
+ if detail_label and detail_pct > 0:
+ progress_col.progress(
+ text=f"{detail_label} ({detail_pct:.0f}%)",
+ factor=max(0.0, min(detail_pct / 100.0, 1.0))
+ )
+
+ elif comfy_tex_op and getattr(scene, 'trellis2_pipeline_active', False):
+ # ── Phase 3: Texturing via ComfyUIGenerate ──
+ action_row.operator("object.test_stable", text="Cancel Texturing", icon="CANCEL")
+ progress_col = layout.column()
+
+ pbr_active = getattr(comfy_tex_op, '_pbr_active', False)
+ if pbr_active:
+ # PBR decomposition sub-phase — 3 bars:
+ # Bar 1 (top): overall pipeline progress
+ # Bar 2: current PBR model (camera progress)
+ # Bar 3: PBR step N/M
+ pbr_step = getattr(comfy_tex_op, '_pbr_step', 0)
+ pbr_total = max(getattr(comfy_tex_op, '_pbr_total_steps', 1), 1)
+ pbr_cam = getattr(comfy_tex_op, '_pbr_cam', 0)
+ pbr_cam_total = max(getattr(comfy_tex_op, '_pbr_cam_total', 1), 1)
+ cam_frac = pbr_cam / pbr_cam_total
+ stage_text = getattr(comfy_tex_op, '_stage', 'PBR Decomposition')
+
+ # Overall: texturing is done, PBR owns the upper slice of Phase 3
+ phase_start = getattr(scene, 'trellis2_pipeline_phase_start_pct', 65.0)
+ phase_weight = 100.0 - phase_start
+ tex_portion = phase_weight * 0.6 # first 60% of phase = texturing
+ pbr_portion = phase_weight * 0.4 # last 40% = PBR
+ pbr_frac = max(0.0, min(((pbr_step - 1) + cam_frac) / pbr_total, 1.0))
+ overall_pct = phase_start + tex_portion + pbr_portion * pbr_frac
+ overall_pct = max(0.0, min(overall_pct, 100.0))
+ total_phases = getattr(scene, 'trellis2_pipeline_total_phases', 3)
+
+ # Bar 1 — Overall pipeline
+ progress_col.progress(
+ text=f"Phase {total_phases}/{total_phases}: PBR Decomposition ({overall_pct:.0f}%)",
+ factor=max(0.0, min(overall_pct / 100.0, 1.0))
+ )
+ # Bar 2 — Current model (camera progress)
+ progress_col.progress(text=stage_text, factor=max(0.0, min(cam_frac, 1.0)))
+ # Bar 3 — PBR step N/M
+ progress_col.progress(text=f"PBR: Step {pbr_step}/{pbr_total}", factor=pbr_frac)
+ else:
+ # Normal texturing progress
+ # Compute overall from scene pipeline props + ComfyUI progress
+ phase_start = getattr(scene, 'trellis2_pipeline_phase_start_pct', 65.0)
+ phase_weight = 100.0 - phase_start
+ comfy_progress = getattr(comfy_tex_op, '_progress', 0) / 100.0
+ total_imgs = getattr(comfy_tex_op, '_total_images', 0)
+ cur_img_idx = getattr(comfy_tex_op, '_current_image', 0)
+ if total_imgs > 1:
+ comfy_overall = (cur_img_idx + comfy_progress) / total_imgs
+ else:
+ comfy_overall = comfy_progress
+ overall_pct = phase_start + comfy_overall * phase_weight
+ overall_pct = max(0.0, min(overall_pct, 100.0))
+
+ total_phases = getattr(scene, 'trellis2_pipeline_total_phases', 3)
+ # Bar 1 — Overall
+ progress_col.progress(
+ text=f"Phase {total_phases}/{total_phases}: Texturing ({overall_pct:.0f}%)",
+ factor=max(0.0, min(overall_pct / 100.0, 1.0))
+ )
+
+ # Bar 2 — Per-image (same as normal ComfyUI bar)
+ stage = getattr(comfy_tex_op, '_stage', 'Generating')
+ img_pct = getattr(comfy_tex_op, '_progress', 0)
+ progress_col.progress(
+ text=f"{stage} ({img_pct:.0f}%)",
+ factor=max(0.0, min(img_pct / 100.0, 1.0))
+ )
+
+ # Bar 3 — Image N/M (same as normal ComfyUI overall bar)
+ if total_imgs > 1:
+ cur_img = min(cur_img_idx + 1, total_imgs)
+ img_overall = max(0.0, min(comfy_overall, 1.0))
+ progress_col.progress(
+ text=f"Overall: Image {cur_img}/{total_imgs}",
+ factor=img_overall
+ )
+
+ elif comfy_tex_op and scene.generation_status == 'running':
+ # Standalone ComfyUIGenerate (e.g. Reproject with PBR)
+ # outside the TRELLIS.2 pipeline.
+ action_row.operator("object.test_stable", text="Cancel Generation", icon="CANCEL")
+ progress_col = layout.column()
+ raw_progress = getattr(comfy_tex_op, '_progress', 0) / 100.0
+ pbr_active = getattr(comfy_tex_op, '_pbr_active', False)
+
+ if pbr_active:
+ pbr_step = getattr(comfy_tex_op, '_pbr_step', 0)
+ pbr_total = max(getattr(comfy_tex_op, '_pbr_total_steps', 1), 1)
+ pbr_cam = getattr(comfy_tex_op, '_pbr_cam', 0)
+ pbr_cam_total = max(getattr(comfy_tex_op, '_pbr_cam_total', 1), 1)
+ cam_frac = pbr_cam / pbr_cam_total
+ stage_text = getattr(comfy_tex_op, '_stage', 'PBR Decomposition')
+ progress_col.progress(text=stage_text, factor=max(0.0, min(cam_frac, 1.0)))
+ pbr_factor = max(0.0, min(((pbr_step - 1) + cam_frac) / pbr_total, 1.0))
+ progress_col.progress(text=f"PBR: Step {pbr_step}/{pbr_total}", factor=pbr_factor)
+ else:
+ progress_text = f"{getattr(comfy_tex_op, '_stage', 'Generating')} ({getattr(comfy_tex_op, '_progress', 0):.0f}%)"
+ progress_col.progress(text=progress_text, factor=max(0.0, min(raw_progress, 1.0)))
+ total_images = getattr(comfy_tex_op, '_total_images', 0)
+ if total_images > 1:
+ current_image_idx = getattr(comfy_tex_op, '_current_image', 0)
+ overall_progress = (current_image_idx + max(0.0, min(raw_progress, 1.0))) / total_images if total_images > 0 else 0
+ cur_img = min(current_image_idx + 1, total_images)
+ progress_col.progress(text=f"Overall: Image {cur_img}/{total_images}", factor=max(0.0, min(overall_progress, 1.0)))
+
+ elif scene.trellis2_generate_from == 'image' and not scene.trellis2_input_image:
+ action_row.operator("object.trellis2_generate", text="Select an image first", icon="ERROR")
+ action_row.enabled = False
+ elif not getattr(scene, 'trellis2_available', False):
+ split = action_row.split(factor=0.8)
+ err_sub = split.row()
+ err_sub.operator("object.trellis2_generate", text="TRELLIS.2 nodes not found", icon="ERROR")
+ err_sub.enabled = False
+ split.operator("stablegen.check_server_status", text="", icon="FILE_REFRESH")
+ else:
+ action_row.operator("object.trellis2_generate", text="Generate 3D Mesh", icon="MESH_ICOSPHERE")
+ else:
+ # --- Standard Diffusion Generate Button ---
+ if config_error_message:
+ # Split the row to have the error message/disabled button and the refresh button
+ if config_error_message == "Cannot reach server":
+ split = action_row.split(factor=0.85, align=True) # Adjust factor as needed
+ split.operator("object.test_stable", text="Cannot generate: " + config_error_message, icon="ERROR") # Use ERROR icon
+ # Use the operator from __init__.py
+ split.operator("stablegen.check_server_status", text="", icon='FILE_REFRESH')
+ else:
+ action_row.operator("object.test_stable", text="Cannot generate: " + config_error_message, icon="ERROR")
+ action_row.enabled = False
+ else:
+ action_row.enabled = True
+ if not bpy.app.online_access:
+ action_row.operator("object.test_stable", text="Enable online access in preferences", icon="ERROR")
+ action_row.enabled = False
+ elif not scene.model_name or scene.model_name == "NONE_FOUND":
+ action_row.operator("object.test_stable", text="Cannot generate: Model Directory Empty", icon="ERROR")
+ action_row.enabled = False
+ elif scene.generation_status == 'idle':
+ # Check if any cameras are selected and if there is existing output
+ selected_cameras = [obj for obj in context.selected_objects if obj.type == 'CAMERA']
+ if not selected_cameras or scene.get("output_timestamp") == "":
+ action_row.operator("object.test_stable", text="Generate", icon="PLAY")
+ else:
+ # Use the regenerate operator
+ action_row.operator("object.stablegen_regenerate", text="Regenerate Selected Views", icon="PLAY")
+ elif scene.generation_status == 'running':
+ action_row.operator("object.test_stable", text="Cancel Generation", icon="CANCEL")
+
+ operator_instance = next((op for win in context.window_manager.windows for op in win.modal_operators if op.bl_idname == 'OBJECT_OT_test_stable'), None)
+ if operator_instance:
+ progress_col = layout.column()
+ raw_progress = getattr(operator_instance, '_progress', 0) / 100.0
+ pbr_active = getattr(operator_instance, '_pbr_active', False)
+
+ if pbr_active:
+ # During PBR: top bar shows camera progress within
+ # the current model step (no raw ComfyUI jitter).
+ pbr_step = getattr(operator_instance, '_pbr_step', 0)
+ pbr_total = max(getattr(operator_instance, '_pbr_total_steps', 1), 1)
+ pbr_cam = getattr(operator_instance, '_pbr_cam', 0)
+ pbr_cam_total = max(getattr(operator_instance, '_pbr_cam_total', 1), 1)
+
+ # Top bar: camera X out of N within this step
+ cam_frac = pbr_cam / pbr_cam_total
+ stage_text = getattr(operator_instance, '_stage', 'PBR Decomposition')
+ progress_col.progress(
+ text=stage_text,
+ factor=max(0.0, min(cam_frac, 1.0))
+ )
+ # Bottom bar: overall PBR progress
+ pbr_factor = max(0.0, min(
+ ((pbr_step - 1) + cam_frac) / pbr_total, 1.0))
+ progress_col.progress(
+ text=f"PBR: Step {pbr_step}/{pbr_total}",
+ factor=pbr_factor
+ )
+ else:
+ # Normal generation progress
+ progress_text = f"{getattr(operator_instance, '_stage', 'Generating')} ({getattr(operator_instance, '_progress', 0):.0f}%)"
+ progress_col.progress(text=progress_text, factor=max(0.0, min(raw_progress, 1.0)))
+
+ total_images = getattr(operator_instance, '_total_images', 0)
+ if total_images > 1:
+ current_image_idx = getattr(operator_instance, '_current_image', 0)
+ current_image_decimal_progress = max(0.0, min(raw_progress, 1.0))
+
+ # Ensure total_images is not zero to prevent division by zero
+ overall_progress_factor = (current_image_idx + current_image_decimal_progress) / total_images if total_images > 0 else 0
+ overall_progress_factor_clamped = max(0.0, min(overall_progress_factor, 1.0))
+
+ current_img = min(current_image_idx + 1, total_images) # Clamp to total_images
+
+ progress_col.progress(
+ text=f"Overall: Image {current_img}/{total_images}",
+ factor=overall_progress_factor_clamped # Ensure factor is <= 1.0 (logic maintained)
+ )
+
+ elif context.scene.generation_status == 'waiting':
+ action_row.operator("object.test_stable", text="Waiting for Cancellation", icon="TIME")
+ else:
+ action_row.operator("object.test_stable", text="Fix Issues to Generate", icon="ERROR")
+ action_row.enabled = False
+
+ bake_row = layout.row()
+ if config_error_message:
+ bake_row.operator("object.bake_textures", text="Cannot Bake: " + config_error_message, icon="ERROR")
+ bake_row.enabled = False
+ else:
+ bake_row.operator("object.bake_textures", text="Bake Textures", icon="RENDER_STILL")
+ bake_row.enabled = True
+ bake_operator = next((op for win in context.window_manager.windows for op in win.modal_operators if op.bl_idname == 'OBJECT_OT_bake_textures'), None)
+ if bake_operator:
+ bake_progress_col = layout.column()
+ bake_stage = getattr(bake_operator, '_stage', 'Baking')
+ bake_progress = getattr(bake_operator, '_progress', 0) / 100.0
+ bake_progress_col.progress(text=bake_stage, factor=bake_progress if bake_progress <=1.0 else 1.0) # Ensure factor is <= 1.0
+
+ total_objects = getattr(bake_operator, '_total_objects', 0)
+ if total_objects > 1:
+ current_object = getattr(bake_operator, '_current_object', 0)
+ # Ensure total_objects is not zero
+ overall_bake_progress = ((current_object + bake_progress) / total_objects) if total_objects > 0 else 0
+ bake_progress_col.progress(
+ text=f"{bake_stage}: Object {current_object + 1}/{total_objects}",
+ factor=overall_bake_progress if overall_bake_progress <=1.0 else 1.0 # Ensure factor is <= 1.0
+ )
+
+ export_row = layout.row()
+ export_row.operator("object.export_game_engine",
+ text="Export for Game Engine",
+ icon="EXPORT")
+
+ # --- Preset Management ---
+ preset_box = layout.box()
+ row = preset_box.row(align=True)
+ row.prop(scene, "stablegen_preset", text="Preset")
+
+ # Conditional button: Apply for stock presets, Save for custom preset
+ if not hasattr(scene, 'active_preset'):
+ scene.active_preset = scene.stablegen_preset
+
+ if scene.stablegen_preset == "CUSTOM":
+ row.operator("stablegen.save_preset", text="Save Preset", icon="PLUS")
+ else:
+ if scene.active_preset != scene.stablegen_preset:
+ row.operator("stablegen.apply_preset", text="Apply Preset", icon="CHECKMARK")
+
+ is_stock_preset = PRESETS.get(scene.stablegen_preset, {}).get("custom", False) is False
+ if not is_stock_preset and scene.stablegen_preset != "DEFAULT":
+ row.operator("stablegen.delete_preset", text="Delete", icon="TRASH")
+
+ # --- Scene Queue ---
+ queue_box = layout.box()
+ queue_col = queue_box.column()
+ wm = context.window_manager
+ show_queue = getattr(wm, 'sg_show_queue', False)
+ queue_header = queue_col.row()
+ queue_header.prop(wm, "sg_show_queue",
+ text=f"Scene Queue ({len(wm.sg_scene_queue)})",
+ icon="TRIA_DOWN" if show_queue else "TRIA_RIGHT",
+ emboss=False)
+ if _queue_mod._queue_processing:
+ status_text = "Exporting GIF..." if _queue_mod._queue_phase == 'exporting_gif' else "Processing..."
+ queue_header.label(text=status_text, icon="SORTTIME")
+
+ if show_queue:
+ queue_content = queue_col.box()
+ row = queue_content.row()
+ row.template_list("SG_UL_SceneQueueList", "",
+ wm, "sg_scene_queue",
+ wm, "sg_scene_queue_index",
+ rows=3)
+ col = row.column(align=True)
+ col.operator("stablegen.queue_move_up", text="", icon="TRIA_UP")
+ col.operator("stablegen.queue_move_down", text="", icon="TRIA_DOWN")
+ col.separator()
+ col.operator("stablegen.queue_remove", text="", icon="REMOVE")
+
+ btn_row = queue_content.row(align=True)
+ btn_row.operator("stablegen.queue_add", text="Add Scene", icon="ADD")
+ btn_row.operator("stablegen.queue_open_result", text="Open", icon="FILE_BLEND")
+ btn_row2 = queue_content.row(align=True)
+ btn_row2.operator("stablegen.queue_invalidate", text="Reset", icon="LOOP_BACK")
+ btn_row2.operator("stablegen.queue_clear", text="Clear", icon="TRASH")
+
+ process_row = queue_content.row()
+ if _queue_mod._queue_processing:
+ process_row.alert = True
+ process_row.operator("stablegen.queue_process", text="Cancel Queue", icon="CANCEL")
+ else:
+ process_row.operator("stablegen.queue_process", text="Process Queue", icon="PLAY")
+ process_row.enabled = len(wm.sg_scene_queue) > 0
+
+ # ── GIF Export settings ──
+ gif_box = queue_content.box()
+ gif_row = gif_box.row()
+ gif_row.prop(wm, "sg_queue_gif_export", text="Export Orbit GIF/MP4")
+ if getattr(wm, 'sg_queue_gif_export', False):
+ gif_col = gif_box.column(align=True)
+ row = gif_col.row(align=True)
+ row.prop(wm, "sg_queue_gif_duration")
+ row.prop(wm, "sg_queue_gif_fps")
+ row = gif_col.row(align=True)
+ row.prop(wm, "sg_queue_gif_resolution")
+ row.prop(wm, "sg_queue_gif_samples")
+ gif_col.prop(wm, "sg_queue_gif_engine")
+ gif_col.prop(wm, "sg_queue_gif_interpolation")
+ gif_col.separator()
+ gif_col.prop(wm, "sg_queue_gif_use_hdri")
+ if getattr(wm, 'sg_queue_gif_use_hdri', False):
+ gif_col.prop(wm, "sg_queue_gif_hdri_path")
+ gif_col.prop(wm, "sg_queue_gif_hdri_strength")
+ gif_col.prop(wm, "sg_queue_gif_hdri_rotation")
+ gif_col.prop(wm, "sg_queue_gif_env_mode")
+ gif_col.prop(wm, "sg_queue_gif_denoiser")
+ if bpy.app.version >= (5, 1, 0) and getattr(wm, 'sg_queue_gif_engine', 'CYCLES') == 'CYCLES':
+ gif_col.prop(wm, "sg_queue_gif_use_gpu")
+ gif_col.prop(wm, "sg_queue_gif_also_no_pbr")
+
+ # --- Main Parameters section ---
+ if not hasattr(scene, 'show_generation_params'):
+ scene.show_generation_params = True
+
+ is_trellis2 = getattr(scene, 'architecture_mode', 'sdxl') == 'trellis2'
+ trellis2_tex_mode = getattr(scene, 'trellis2_texture_mode', 'native')
+ trellis2_diffusion_texturing = is_trellis2 and trellis2_tex_mode in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein')
+
+ main_params_box = layout.box()
+ main_params_col = main_params_box.column()
+ main_params_col.prop(scene, "show_generation_params", text="Main Parameters", icon="TRIA_DOWN" if scene.show_generation_params else "TRIA_RIGHT", emboss=False)
+ if scene.show_generation_params:
+ params_container = main_params_col.box()
+ # Split for prompt
+ split = params_container.split(factor=0.25)
+ split.label(text="Prompt:")
+ prompt_row = split.row(align=True)
+ prompt_row.prop(scene, "comfyui_prompt", text="")
+ # Hide texture prompt toggle when texture mode is none/native (no separate texture pipeline)
+ if not (is_trellis2 and trellis2_tex_mode in ('none', 'native')):
+ prompt_row.prop(scene, "use_separate_texture_prompt", text="",
+ icon='BRUSH_DATA',
+ icon_only=True)
+ if scene.use_separate_texture_prompt and not (is_trellis2 and trellis2_tex_mode in ('none', 'native')):
+ split = params_container.split(factor=0.25)
+ split.label(text="Texture Prompt:")
+ split.prop(scene, "texture_prompt", text="")
+
+ # Architecture selector (architecture_mode — includes TRELLIS.2)
+ # Alert when either architecture_mode or model_architecture changes.
+ _arch_diff = 'architecture_mode' in _diff_props or 'model_architecture' in _diff_props
+ split = params_container.split(factor=0.5)
+ if _arch_diff:
+ split.alert = True
+ split.label(text="Architecture:")
+ split.prop(scene, "architecture_mode", text="")
+ if _arch_diff:
+ _has_mode = 'architecture_mode' in _diff_props
+ _has_model = 'model_architecture' in _diff_props
+ if _has_mode:
+ # architecture_mode differs — always show model in parens.
+ # Use the preset's model_architecture even if it didn't change.
+ _preset_key = scene.stablegen_preset
+ _preset_data = PRESETS.get(_preset_key, {})
+ _model_display = _fmt_diff_val(_preset_data.get(
+ 'model_architecture', scene.model_architecture))
+ split.label(text="→ " + _diff_props['architecture_mode']
+ + " (" + _model_display + ")")
+ else:
+ # Only model_architecture differs (e.g. SDXL → Qwen).
+ split.label(text="→ " + _diff_props['model_architecture'])
+
+ if is_trellis2:
+ # --- TRELLIS.2 specific layout ---
+ # Warning if TRELLIS.2 nodes not detected
+ if not getattr(scene, 'trellis2_available', False):
+ warn_row = params_container.row()
+ warn_row.alert = True
+ warn_split = warn_row.split(factor=0.9)
+ warn_split.label(text="TRELLIS.2 nodes not detected on server", icon="ERROR")
+ warn_split.operator("stablegen.check_server_status", text="", icon="FILE_REFRESH")
+
+ # Generate From toggle (Image / Prompt)
+ split = params_container.split(factor=0.5)
+ split.label(text="Generate From:")
+ split.prop(scene, "trellis2_generate_from", text="")
+
+ # Input image picker (only when generate_from = image)
+ if scene.trellis2_generate_from == 'image':
+ split = params_container.split(factor=0.25)
+ split.label(text="Input Image:")
+ split.prop(scene, "trellis2_input_image", text="")
+
+ # Preview gallery (only when generate_from = prompt)
+ if scene.trellis2_generate_from == 'prompt':
+ row = params_container.row(align=True)
+ row.prop(scene, "trellis2_preview_gallery_enabled", text="Preview Gallery", toggle=True, icon="IMAGE_REFERENCE")
+ sub = row.row(align=True)
+ sub.enabled = scene.trellis2_preview_gallery_enabled
+ sub.prop(scene, "trellis2_preview_gallery_count", text="Count")
+
+ # Texture Generation Mode
+ split = params_container.split(factor=0.5)
+ split.label(text="Texture Mode:")
+ split.prop(scene, "trellis2_texture_mode", text="")
+
+ # Prompt + native/none: show initial-image architecture & checkpoint
+ _prompt_needs_initial = (
+ scene.trellis2_generate_from == 'prompt'
+ and trellis2_tex_mode in ('native', 'none')
+ )
+ if _prompt_needs_initial:
+ split = params_container.split(factor=0.5)
+ split.label(text="Initial Image Arch:")
+ split.prop(scene, "trellis2_initial_image_arch", text="")
+
+ split = params_container.split(factor=0.25)
+ split.label(text="Checkpoint:")
+ row = split.row(align=True)
+ row.prop(scene, "model_name", text="")
+ row.operator("stablegen.refresh_checkpoint_list", text="", icon='FILE_REFRESH')
+
+ # When diffusion texturing: show checkpoint, generation mode, camera count
+ if trellis2_diffusion_texturing:
+ split = params_container.split(factor=0.25)
+ split.label(text="Checkpoint:")
+ row = split.row(align=True)
+ row.prop(scene, "model_name", text="")
+ row.operator("stablegen.refresh_checkpoint_list", text="", icon='FILE_REFRESH')
+
+ split = params_container.split(factor=0.5)
+ if 'generation_method' in _diff_props or 'qwen_generation_method' in _diff_props:
+ split.alert = True
+ split.label(text="Generation Mode:")
+ if scene.model_architecture.startswith("qwen"):
+ split.prop(scene, "qwen_generation_method", text="")
+ else:
+ split.prop(scene, "generation_method", text="")
+ _lbl = ''.join(f'→{_diff_props[k]} ' for k in ('generation_method', 'qwen_generation_method') if k in _diff_props)
+ if _lbl:
+ split.label(text=_lbl.strip())
+ else:
+ # --- Standard diffusion layout ---
+ # Split for model name
+ split = params_container.split(factor=0.25)
+ split.label(text="Checkpoint:")
+ row = split.row(align=True)
+ row.prop(scene, "model_name", text="")
+ row.operator("stablegen.refresh_checkpoint_list", text="", icon='FILE_REFRESH')
+
+ # Split for generation method
+ split = params_container.split(factor=0.5)
+ if 'generation_method' in _diff_props or 'qwen_generation_method' in _diff_props:
+ split.alert = True
+ split.label(text="Generation Mode:")
+ if scene.model_architecture.startswith("qwen"):
+ split.prop(scene, "qwen_generation_method", text="")
+ else:
+ split.prop(scene, "generation_method", text="")
+ _lbl = ''.join(f'→{_diff_props[k]} ' for k in ('generation_method', 'qwen_generation_method') if k in _diff_props)
+ if _lbl:
+ split.label(text=_lbl.strip())
+
+ # Split for object selection
+ split = params_container.split(factor=0.5)
+ split.label(text="Target Objects:")
+ split.prop(scene, "texture_objects", text="")
+
+ # Helper to create collapsible sections
+ def draw_collapsible_section(parent_layout, toggle_prop_name, title, icon="NONE"):
+ if not hasattr(scene, toggle_prop_name):
+ setattr(bpy.types.Scene, toggle_prop_name, bpy.props.BoolProperty(name=title, default=False))
+
+ box = parent_layout.box()
+ col = box.column()
+ is_expanded = getattr(scene, toggle_prop_name, False)
+ col.prop(scene, toggle_prop_name, text=title, icon="TRIA_DOWN" if is_expanded else "TRIA_RIGHT", emboss=False)
+ if is_expanded:
+ return col.box() # Return a new box for content if expanded
+ return None
+
+ core_settings_props = [
+ "show_core_settings", "show_lora_settings", "show_camera_options",
+ "show_scene_understanding_settings",
+ "show_output_material_settings", "show_image_guidance_settings",
+ "show_masking_inpainting_settings", "show_mode_specific_settings"
+ ]
+ for prop_name in core_settings_props:
+ if not hasattr(scene, prop_name):
+ setattr(bpy.types.Scene, prop_name, bpy.props.BoolProperty(name=prop_name.replace("_", " ").title(), default=False))
+
+ # --- ADVANCED PARAMETERS ---
+ advanced_params_box = layout.box()
+ advanced_params_box = advanced_params_box.column()
+ advanced_params_box.prop(scene, "show_advanced_params", text="Advanced Parameters", icon="TRIA_DOWN" if scene.show_advanced_params else "TRIA_RIGHT", emboss=False)
+ if context.scene.show_advanced_params:
+
+ # --- TRELLIS.2: Mesh Generation Settings ---
+ if is_trellis2:
+ content_box = draw_collapsible_section(advanced_params_box, "show_trellis2_mesh_settings", "Mesh Generation Settings", icon="MESH_DATA")
+ if content_box:
+ # Core mesh params
+ row = content_box.row()
+ row.prop(scene, "trellis2_seed", text="Seed")
+
+ content_box.separator()
+
+ # Post-processing (ComfyUI-side decimation + remesh)
+ content_box.label(text="Post-Processing:", icon="OUTLINER_OB_MESH")
+ row = content_box.row()
+ row.prop(scene, "trellis2_post_processing_enabled", text="Enable Post-Processing", toggle=True, icon="MOD_DECIM")
+
+ if scene.trellis2_post_processing_enabled:
+ row = content_box.row()
+ row.prop(scene, "trellis2_decimation", text="Decimation Target")
+ row = content_box.row()
+ row.prop(scene, "trellis2_remesh", text="Remesh", toggle=True, icon="MOD_REMESH")
+
+ content_box.separator()
+
+ # Model settings
+ content_box.label(text="Model:", icon="SETTINGS")
+ split = content_box.split(factor=0.5)
+ split.label(text="Resolution:")
+ split.prop(scene, "trellis2_resolution", text="")
+
+ split = content_box.split(factor=0.5)
+ split.label(text="VRAM Mode:")
+ split.prop(scene, "trellis2_vram_mode", text="")
+
+ split = content_box.split(factor=0.5)
+ split.label(text="Attention:")
+ split.prop(scene, "trellis2_attn_backend", text="")
+
+ content_box.separator()
+
+ # Shape generation
+ content_box.label(text="Shape Generation:", icon="MESH_ICOSPHERE")
+ row = content_box.row(align=True)
+ row.prop(scene, "trellis2_ss_guidance", text="SS Guidance")
+ row.prop(scene, "trellis2_ss_steps", text="SS Steps")
+ row = content_box.row(align=True)
+ row.prop(scene, "trellis2_shape_guidance", text="Shape Guidance")
+ row.prop(scene, "trellis2_shape_steps", text="Shape Steps")
+ row = content_box.row()
+ row.prop(scene, "trellis2_max_tokens", text="Max Tokens (VRAM)")
+
+ content_box.separator()
+
+ # Conditioning
+ content_box.label(text="Conditioning:", icon="IMAGE_DATA")
+ split = content_box.split(factor=0.5)
+ split.label(text="Background:")
+ split.prop(scene, "trellis2_background_color", text="")
+ content_box.separator()
+
+ # Misc
+ content_box.label(text="Misc:", icon="PREFERENCES")
+ split = content_box.split(factor=0.5)
+ split.label(text="BG Removal:")
+ split.prop(scene, "trellis2_bg_removal", text="")
+
+ split = content_box.split(factor=0.5)
+ split.label(text="Shading:")
+ split.prop(scene, "trellis2_shade_mode", text="")
+ content_box.separator()
+
+ # --- TRELLIS.2: Native Texture Settings ---
+ if is_trellis2 and trellis2_tex_mode == 'native':
+ content_box = draw_collapsible_section(advanced_params_box, "show_trellis2_texture_settings", "Texture Settings (TRELLIS.2 Native)", icon="TEXTURE")
+ if content_box:
+ row = content_box.row(align=True)
+ row.prop(scene, "trellis2_tex_guidance", text="Tex Guidance")
+ row.prop(scene, "trellis2_tex_steps", text="Tex Steps")
+ row = content_box.row()
+ row.prop(scene, "trellis2_texture_size", text="Texture Size")
+
+ content_box.separator()
+ row = content_box.row()
+ row.prop(scene, "trellis2_auto_lighting", text="Studio Lighting", icon="LIGHT_AREA")
+
+ # --- TRELLIS.2: Camera Placement Settings (diffusion texturing) ---
+ if is_trellis2 and trellis2_diffusion_texturing:
+ content_box = draw_collapsible_section(advanced_params_box, "show_trellis2_camera_settings", "Camera Placement (TRELLIS.2)", icon="CAMERA_DATA")
+ if content_box:
+ _t2_pm = getattr(scene, 'trellis2_placement_mode', 'normal_weighted')
+
+ row = content_box.row()
+ row.prop(scene, "trellis2_import_scale", text="Import Scale (BU)")
+
+ content_box.separator()
+
+ split = content_box.split(factor=0.4)
+ split.label(text="Placement:")
+ split.prop(scene, "trellis2_placement_mode", text="")
+
+ # Camera count (not used by greedy)
+ if _t2_pm != 'greedy_coverage':
+ row = content_box.row()
+ row.prop(scene, "trellis2_camera_count", text="Camera Count")
+
+ # Greedy-specific
+ if _t2_pm == 'greedy_coverage':
+ row = content_box.row(align=True)
+ row.prop(scene, "trellis2_coverage_target", text="Coverage Target")
+ row.prop(scene, "trellis2_max_auto_cameras", text="Max Cameras")
+
+ # Fan-specific
+ if _t2_pm == 'fan_from_camera':
+ row = content_box.row()
+ row.prop(scene, "trellis2_fan_angle", text="Fan Angle")
+
+ content_box.separator()
+
+ row = content_box.row()
+ row.prop(scene, "trellis2_auto_prompts", text="Auto View Prompts", toggle=True, icon="OUTLINER_OB_CAMERA")
+
+ split = content_box.split(factor=0.4)
+ split.label(text="Auto Aspect:")
+ split.prop(scene, "trellis2_auto_aspect", text="")
+
+ split = content_box.split(factor=0.4)
+ split.label(text="Occlusion:")
+ split.prop(scene, "trellis2_occlusion_mode", text="")
+
+ row = content_box.row()
+ row.prop(scene, "trellis2_exclude_bottom", text="Exclude Bottom Faces", toggle=True, icon="TRIA_DOWN_BAR")
+ if scene.trellis2_exclude_bottom:
+ row = content_box.row()
+ row.prop(scene, "trellis2_exclude_bottom_angle", text="Bottom Angle")
+
+ row = content_box.row()
+ row.prop(scene, "trellis2_consider_existing", text="Consider Existing Cameras", toggle=True)
+
+ row = content_box.row()
+ row.prop(scene, "trellis2_delete_cameras", text="Delete Cameras After", toggle=True, icon="TRASH")
+
+ content_box.separator()
+
+ row = content_box.row()
+ row.prop(scene, "trellis2_clamp_elevation", text="Clamp Elevation", toggle=True, icon="CON_ROTLIMIT")
+ if scene.trellis2_clamp_elevation:
+ row = content_box.row(align=True)
+ row.prop(scene, "trellis2_min_elevation", text="Min")
+ row.prop(scene, "trellis2_max_elevation", text="Max")
+
+ # --- Diffusion-based advanced sections ---
+ # Each is individually guarded: shown for standard arches or TRELLIS.2 with diffusion texturing
+ _show_diffusion_sections = not is_trellis2 or trellis2_diffusion_texturing
+ # Mesh-only + prompt mode still needs initial-image pipeline settings
+ _show_initial_image_settings = (
+ is_trellis2 and trellis2_tex_mode == 'none'
+ and getattr(scene, 'trellis2_generate_from', 'image') == 'prompt'
+ )
+
+ # --- Core Generation Settings ---
+
+ if _show_diffusion_sections or _show_initial_image_settings:
+ content_box = draw_collapsible_section(advanced_params_box, "show_core_settings", "Core Generation Settings", icon="SETTINGS")
+ else:
+ content_box = None
+ if content_box:
+ row = content_box.row()
+ row.prop(scene, "seed", text="Seed")
+ if width_mode == 'narrow':
+ row = content_box.row()
+ sub = row.row()
+ if 'steps' in _diff_props:
+ sub.alert = True
+ sub.prop(scene, "steps", text="Steps")
+ if 'steps' in _diff_props:
+ sub.label(text="→" + _diff_props['steps'])
+ if width_mode == 'narrow':
+ row = content_box.row()
+ sub = row.row()
+ if 'cfg' in _diff_props:
+ sub.alert = True
+ sub.prop(scene, "cfg", text="CFG")
+ if 'cfg' in _diff_props:
+ sub.label(text="→" + _diff_props['cfg'])
+
+ split = content_box.split(factor=0.5)
+ split.label(text="Negative Prompt:")
+ split.prop(scene, "comfyui_negative_prompt", text="")
+
+ split = content_box.split(factor=0.5)
+ split.label(text="Control After Generate:")
+ split.prop(scene, "control_after_generate", text="")
+
+ split = content_box.split(factor=0.5)
+ if 'sampler' in _diff_props:
+ split.alert = True
+ split.label(text="Sampler:")
+ split.prop(scene, "sampler", text="")
+ if 'sampler' in _diff_props:
+ split.label(text="→ " + _diff_props['sampler'])
+
+ split = content_box.split(factor=0.5)
+ if 'scheduler' in _diff_props:
+ split.alert = True
+ split.label(text="Scheduler:")
+ split.prop(scene, "scheduler", text="")
+ if 'scheduler' in _diff_props:
+ split.label(text="→ " + _diff_props['scheduler'])
+
+ row = content_box.row()
+ row.prop(scene, "clip_skip", text="Clip Skip")
+
+ # --- LoRA Settings ---
+ if _show_diffusion_sections or _show_initial_image_settings:
+ content_box = draw_collapsible_section(advanced_params_box, "show_lora_settings", "LoRA Management", icon="MODIFIER")
+ else:
+ content_box = None
+ if content_box:
+ row = content_box.row()
+ row.alignment = 'CENTER'
+ row.label(text="LoRA Units", icon="BRUSHES_ALL") # Using decimate icon for LoRA
+
+ if scene.lora_units:
+ for i, lora_unit in enumerate(scene.lora_units):
+ is_selected_lora = (scene.lora_units_index == i)
+ unit_box = content_box.box()
+ row = unit_box.row()
+ row.prop(lora_unit, "model_name", text=f"LoRA {i+1}") # Shows selected model
+
+ sub_row = unit_box.row(align=True)
+ sub_row.prop(lora_unit, "model_strength", text="Model Strength")
+ if not scene.model_architecture.startswith("qwen") and scene.model_architecture != 'flux2_klein': # Qwen/Klein use model only loras
+ sub_row.prop(lora_unit, "clip_strength", text="CLIP Strength")
+
+ # Icon to indicate selection more clearly alongside the alert state
+ select_icon = 'CHECKBOX_HLT' if is_selected_lora else 'CHECKBOX_DEHLT'
+
+ # Selection button (now more like a radio button)
+ op_select_lora = row.operator("wm.context_set_int", text="", icon=select_icon, emboss=True) # Keep emboss for the button itself
+ op_select_lora.data_path = "scene.lora_units_index"
+ op_select_lora.value = i
+
+ btn_row_lora = content_box.row(align=True)
+
+ if not scene.lora_units:
+ # Only one button if no LoRA units are present
+ button_text = "Add LoRA Unit" # Default text
+
+ # Draw the operator with the dynamically determined text
+ btn_row_lora.operator("stablegen.add_lora_unit", text=button_text, icon="ADD")
+ # The enabled state (greying out) will be handled by AddLoRAUnit.poll()
+ else:
+ # Multiple buttons if LoRA units exist
+ btn_row_lora.operator("stablegen.add_lora_unit", text="Add Another LoRA", icon="ADD")
+ btn_row_lora.operator("stablegen.remove_lora_unit", text="Remove Selected", icon="REMOVE")
+
+ # --- Camera Options ---
+ if _show_diffusion_sections:
+ content_box = draw_collapsible_section(advanced_params_box, "show_camera_options", "Camera Settings", icon="CAMERA_DATA")
+ else:
+ content_box = None
+ if content_box:
+ row = content_box.row(align=True)
+ row.prop(scene, "use_camera_prompts", text="Use Camera Prompts", toggle=True, icon="OUTLINER_OB_CAMERA")
+
+ # ── Camera Generation Order ──
+ row = content_box.row(align=True)
+ row.prop(scene, "sg_use_custom_camera_order",
+ text="Custom Generation Order", toggle=True, icon="SORTALPHA")
+ if scene.sg_use_custom_camera_order:
+ order_box = content_box.box()
+ # Preset strategy selector + sync
+ row = order_box.row(align=True)
+ row.operator_menu_enum(
+ "stablegen.apply_camera_order_preset", "strategy",
+ text="Sort Preset", icon="PRESET")
+ row.operator("stablegen.sync_camera_order",
+ text="", icon="FILE_REFRESH")
+
+ # UIList with move buttons
+ row = order_box.row()
+ row.template_list(
+ "SG_UL_CameraOrderList", "",
+ scene, "sg_camera_order",
+ scene, "sg_camera_order_index",
+ rows=4, maxrows=8)
+ col = row.column(align=True)
+ op = col.operator("stablegen.move_camera_order",
+ text="", icon="TRIA_UP")
+ op.direction = 'UP'
+ op = col.operator("stablegen.move_camera_order",
+ text="", icon="TRIA_DOWN")
+ op.direction = 'DOWN'
+
+ # --- Viewpoint Blending Settings ---
+ if _show_diffusion_sections:
+ content_box = draw_collapsible_section(advanced_params_box, "show_scene_understanding_settings", "Viewpoint Blending Settings", icon="ZOOM_IN")
+ else:
+ content_box = None
+ if content_box:
+ # Row 1: Discard-Over Angle | Weight Exponent
+ split = content_box.split(factor=0.5, align=True)
+ sub = split.row()
+ if 'discard_factor' in _diff_props:
+ sub.alert = True
+ sub.prop(scene, "discard_factor", text="Discard Angle")
+ if 'discard_factor' in _diff_props:
+ sub.label(text="→" + _diff_props['discard_factor'])
+ sub = split.row()
+ if 'weight_exponent' in _diff_props:
+ sub.alert = True
+ sub.prop(scene, "weight_exponent", text="Exponent")
+ if 'weight_exponent' in _diff_props:
+ sub.label(text="→" + _diff_props['weight_exponent'])
+
+ # Row 2 & 3: Reset toggles and values (only for sequential / qwen)
+ if scene.generation_method == 'sequential' or scene.model_architecture in ('qwen_image_edit', 'flux2_klein'):
+ split = content_box.split(factor=0.5, align=True)
+ split.prop(scene, "discard_factor_generation_only", text="Reset Angle", toggle=True)
+ split.prop(scene, "weight_exponent_generation_only", text="Reset Exponent", toggle=True)
+
+ if scene.discard_factor_generation_only or scene.weight_exponent_generation_only:
+ split = content_box.split(factor=0.5, align=True)
+ if scene.discard_factor_generation_only:
+ split.prop(scene, "discard_factor_after_generation", text="Angle After")
+ else:
+ split.label(text="")
+ if scene.weight_exponent_generation_only:
+ split.prop(scene, "weight_exponent_after_generation", text="Exp. After")
+ else:
+ split.label(text="")
+
+ row = content_box.row()
+ row.prop(scene, "early_priority", text="Prioritize Initial Views", toggle=True, icon="REW")
+ if scene.early_priority:
+ row = content_box.row()
+ row.prop(scene, "early_priority_strength", text="Priority Strength")
+
+ row = content_box.row()
+ row.prop(scene, "bake_visibility_weights", text="Bake Visibility (Transform-Stable)", toggle=True, icon="MESH_DATA")
+
+ row = content_box.row()
+ row.prop(scene, "sg_silhouette_margin", text="Silhouette Margin (px)")
+
+ row = content_box.row()
+ row.prop(scene, "sg_silhouette_depth", text="Silhouette Depth Threshold")
+
+ row = content_box.row()
+ row.prop(scene, "sg_silhouette_rays", text="Silhouette Rays")
+
+ # --- Output & Material Settings ---
+ if _show_diffusion_sections:
+ content_box = draw_collapsible_section(advanced_params_box, "show_output_material_settings", "Output & Material Settings", icon="MATERIAL")
+ else:
+ content_box = None
+ if content_box:
+ # ── PBR Decomposition ──
+ row = content_box.row()
+ if 'pbr_decomposition' in _diff_props:
+ row.alert = True
+ row.prop(scene, "pbr_decomposition", text="PBR Decomposition", toggle=True, icon="NODE_MATERIAL")
+ if 'pbr_decomposition' in _diff_props:
+ row.label(text="→ " + _diff_props['pbr_decomposition'])
+ if scene.pbr_decomposition:
+ # Warning if PBR decomposition nodes not detected
+ if not getattr(scene, 'pbr_nodes_available', False):
+ warn_row = content_box.row()
+ warn_row.alert = True
+ warn_split = warn_row.split(factor=0.9)
+ warn_split.label(text="PBR nodes not detected on server", icon="ERROR")
+ warn_split.operator("stablegen.check_server_status", text="", icon="FILE_REFRESH")
+ sub = content_box.box()
+ sub.label(text="PBR Decomposition", icon="NODE_MATERIAL")
+ # ── Map toggles ──
+ sub.label(text="Maps to Extract:", icon="IMAGE_DATA")
+ grid = sub.grid_flow(row_major=True, columns=3, even_columns=True, align=True)
+ grid.prop(scene, "pbr_map_albedo", toggle=True, icon="SHADING_SOLID")
+ grid.prop(scene, "pbr_map_roughness", toggle=True, icon="MATFLUID")
+ grid.prop(scene, "pbr_map_metallic", toggle=True, icon="META_BALL")
+ grid.prop(scene, "pbr_map_normal", toggle=True, icon="NORMALS_FACE")
+ grid.prop(scene, "pbr_map_height", toggle=True, icon="MOD_DISPLACE")
+ grid.prop(scene, "pbr_map_ao", toggle=True, icon="SHADING_RENDERED")
+ grid.prop(scene, "pbr_map_emission", toggle=True, icon="LIGHT_POINT")
+ # ── Per-map adjustment controls ──
+ if scene.pbr_map_normal:
+ adj_row = sub.row()
+ adj_row.prop(scene, "pbr_normal_strength", text="Normal Strength", slider=True)
+ if scene.pbr_map_height:
+ adj_row = sub.row()
+ adj_row.prop(scene, "pbr_height_scale", text="Height Scale", slider=True)
+ if scene.pbr_map_ao:
+ ao_row = sub.row(align=True)
+ ao_row.prop(scene, "pbr_ao_samples", text="AO Samples")
+ ao_row.prop(scene, "pbr_ao_distance", text="AO Distance")
+ if scene.pbr_map_emission:
+ adj_row = sub.row()
+ adj_row.prop(scene, "pbr_emission_method", text="Method")
+ adj_row = sub.row()
+ adj_row.prop(scene, "pbr_emission_threshold", text="Threshold", slider=True)
+ adj_row = sub.row()
+ adj_row.prop(scene, "pbr_emission_strength", text="Emission Strength", slider=True)
+ if scene.pbr_emission_method == 'hsv':
+ hsv_row = sub.row(align=True)
+ hsv_row.prop(scene, "pbr_emission_saturation_min", text="Sat Min", slider=True)
+ hsv_row.prop(scene, "pbr_emission_value_min", text="Val Min", slider=True)
+ hsv_row = sub.row()
+ hsv_row.prop(scene, "pbr_emission_bloom", text="Bloom Radius", slider=True)
+ # ── Albedo source selector (only when albedo enabled) ──
+ if scene.pbr_map_albedo:
+ sub.separator()
+ row = sub.row()
+ row.prop(scene, "pbr_albedo_source", text="Albedo Source")
+ if scene.pbr_albedo_source == 'delight':
+ adj_row = sub.row()
+ adj_row.prop(scene, "pbr_delight_strength", text="Delight Strength", slider=True)
+ adj_row = sub.row(align=True)
+ adj_row.prop(scene, "pbr_albedo_auto_saturation", text="Correct Albedo Saturation", toggle=True, icon="BRUSHES_ALL")
+ if scene.pbr_albedo_auto_saturation:
+ adj_row.prop(scene, "pbr_albedo_saturation_mode", text="")
+ sub.separator()
+ # ── Quality settings ──
+ row = sub.row(align=True)
+ row.prop(scene, "pbr_use_native_resolution", text="Native Resolution", toggle=True, icon="FULLSCREEN_ENTER")
+ row.prop(scene, "pbr_tiling", text="")
+ if scene.pbr_tiling != 'off':
+ row = sub.row(align=True)
+ row.prop(scene, "pbr_tile_grid", text="Tile Grid (N\u00d7N)")
+ row.prop(scene, "pbr_tile_superres", text="Super Res", toggle=True, icon="IMAGE_PLANE")
+ if scene.pbr_tiling == 'custom':
+ row = sub.row(align=True)
+ row.prop(scene, "pbr_tile_albedo", text="Albedo", toggle=True)
+ row.prop(scene, "pbr_tile_material", text="Material", toggle=True)
+ row.prop(scene, "pbr_tile_normal", text="Normal", toggle=True)
+ row.prop(scene, "pbr_tile_height", text="Height", toggle=True)
+ row.prop(scene, "pbr_tile_emission", text="Emission", toggle=True)
+ if not scene.pbr_use_native_resolution:
+ row = sub.row()
+ row.prop(scene, "pbr_processing_resolution", text="Processing Resolution")
+ row = sub.row(align=True)
+ row.prop(scene, "pbr_denoise_steps", text="Denoise Steps")
+ row.prop(scene, "pbr_ensemble_size", text="Ensemble Size")
+ sub.separator()
+ row = sub.row()
+ row.prop(scene, "pbr_replace_color_with_albedo", text="Use Albedo as Base Color", toggle=True, icon="SHADING_SOLID")
+ row = sub.row()
+ row.prop(scene, "pbr_auto_lighting", text="Studio Lighting", toggle=True, icon="LIGHT_AREA")
+
+ content_box.separator()
+ split = content_box.split(factor=0.5)
+ split.label(text="Fallback Color:")
+ split.prop(scene, "fallback_color", text="")
+
+ row = content_box.row()
+ row.prop(scene, "auto_rescale", text="Auto Rescale Resolution", toggle=True, icon="ARROW_LEFTRIGHT")
+ if scene.auto_rescale:
+ sub_box_rescale = content_box.box()
+ row = sub_box_rescale.row()
+ row.prop(scene, "auto_rescale_target_mp", text="Target Megapixels")
+ if scene.model_architecture.startswith('qwen'):
+ row = sub_box_rescale.row()
+ row.prop(scene, "qwen_rescale_alignment", text="Qwen VL-Aligned Rescale (112px)", toggle=True, icon="SNAP_INCREMENT")
+ row = content_box.row()
+ if 'overwrite_material' in _diff_props:
+ row.alert = True
+ row.prop(scene, "overwrite_material", text="Overwrite Material", toggle=True, icon="FILE_REFRESH")
+ if 'overwrite_material' in _diff_props:
+ row.label(text="→ " + _diff_props['overwrite_material'])
+
+ # --- Image Guidance (IPAdapter & ControlNet) ---
+ if _show_diffusion_sections:
+ if scene.model_architecture in ['sdxl', 'flux1']:
+ content_box = draw_collapsible_section(advanced_params_box, "show_image_guidance_settings", "Image Guidance (IPAdapter & ControlNet)", icon="MODIFIER")
+ elif scene.model_architecture == 'flux2_klein':
+ content_box = draw_collapsible_section(advanced_params_box, "show_image_guidance_settings", "FLUX.2 Klein Guidance", icon="MODIFIER")
+ else: # Qwen Image Edit
+ content_box = draw_collapsible_section(advanced_params_box, "show_image_guidance_settings", "Qwen-Image-Edit Guidance", icon="MODIFIER")
+ else:
+ content_box = None
+ if content_box:
+ if scene.model_architecture in ('qwen_image_edit', 'flux2_klein'):
+ if scene.model_architecture == 'flux2_klein' or scene.qwen_generation_method == 'generate':
+ split = content_box.split(factor=0.5)
+ split.label(text="Guidance Map:")
+ split.prop(scene, "qwen_guidance_map_type", text="")
+
+ row = content_box.row()
+ row.prop(scene, "qwen_use_external_style_image", text="Use External Image as Style", toggle=True, icon="FILE_IMAGE")
+
+ # TRELLIS.2 input as style (only in trellis2 architecture mode)
+ if getattr(scene, 'architecture_mode', '') == 'trellis2':
+ row = content_box.row()
+ row.prop(scene, "qwen_use_trellis2_style", text="Use TRELLIS.2 Input as Style", toggle=True, icon="MESH_MONKEY")
+
+ if scene.qwen_use_external_style_image:
+ style_box = content_box.box()
+ row = style_box.row()
+ row.prop(scene, "qwen_external_style_image", text="Style Image")
+ row = style_box.row()
+ 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:
+ 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 getattr(scene, 'architecture_mode', '') == 'trellis2' and scene.qwen_use_trellis2_style:
+ t2_style_box = content_box.box()
+ row = t2_style_box.row()
+ row.prop(scene, "qwen_trellis2_style_initial_only", text="TRELLIS.2 Style for Initial Only", toggle=True)
+ if scene.qwen_trellis2_style_initial_only:
+ subsequent_box = t2_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:
+ 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)
+ split.label(text="Context Render:")
+ split.prop(scene, "qwen_context_render_mode", text="")
+
+ row = content_box.row()
+ row.prop(scene, "qwen_voronoi_mode", text="Voronoi Projection", toggle=True, icon="MESH_GRID")
+
+ elif scene.qwen_generation_method in ('refine', 'local_edit'):
+ row = content_box.row()
+ row.prop(scene, "qwen_refine_use_prev_ref", text="Use Previous Refined View", toggle=True)
+
+ row = content_box.row()
+ row.prop(scene, "qwen_refine_use_depth", text="Use Depth Map", toggle=True, icon="MODIFIER")
+
+ row = content_box.row()
+ row.prop(scene, "qwen_use_external_style_image", text="Use External Image as Style", toggle=True, icon="FILE_IMAGE")
+
+ # TRELLIS.2 input as style (only in trellis2 architecture mode)
+ if getattr(scene, 'architecture_mode', '') == 'trellis2':
+ row = content_box.row()
+ row.prop(scene, "qwen_use_trellis2_style", text="Use TRELLIS.2 Input as Style", toggle=True, icon="MESH_MONKEY")
+
+ if scene.qwen_use_external_style_image:
+ style_box = content_box.box()
+ row = style_box.row()
+ row.prop(scene, "qwen_external_style_image", text="Style Image")
+
+ # Timestep-zero reference method — shared across all Qwen modes
+ row = content_box.row()
+ row.prop(scene, "qwen_timestep_zero_ref", text="Timestep-Zero References (color shift fix)", toggle=True, icon="COLORSET_08_VEC")
+
+ row = content_box.row()
+ row.prop(scene, "qwen_use_custom_prompts", text="Custom Guidance Prompts", toggle=True, icon="TEXT")
+ if scene.qwen_use_custom_prompts:
+ custom_prompt_box = content_box.box()
+
+ # Initial Image Prompt
+ col = custom_prompt_box.column()
+ col.label(text="Initial Image Prompt:")
+ row = col.row(align=True)
+ row.prop(scene, "qwen_custom_prompt_initial", text="")
+ op = row.operator("stablegen.reset_qwen_prompt", text="", icon='FILE_REFRESH')
+ op.prompt_type = 'initial'
+
+ # Subsequent Images Prompt (conditional)
+ if scene.generation_method == 'sequential' or (scene.qwen_generation_method in ('refine', 'local_edit') and scene.qwen_refine_mode == 'sequential'):
+ col = custom_prompt_box.column()
+ col.label(text="Subsequent Images Prompt:")
+ row = col.row(align=True)
+
+ if scene.qwen_context_render_mode == 'NONE' and scene.qwen_generation_method == 'generate':
+ row.prop(scene, "qwen_custom_prompt_seq_none", text="")
+ op_prop = 'seq_none'
+ elif scene.qwen_context_render_mode == 'REPLACE_STYLE' and scene.qwen_generation_method == 'generate':
+ row.prop(scene, "qwen_custom_prompt_seq_replace", text="")
+ op_prop = 'seq_replace'
+ elif scene.qwen_context_render_mode == 'ADDITIONAL' and scene.qwen_generation_method == 'generate':
+ row.prop(scene, "qwen_custom_prompt_seq_additional", text="")
+ op_prop = 'seq_additional'
+ else: # Refine mode or other
+ row.prop(scene, "qwen_custom_prompt_seq_none", text="")
+ op_prop = 'seq_none'
+
+ op = row.operator("stablegen.reset_qwen_prompt", text="", icon='FILE_REFRESH')
+ op.prompt_type = op_prop
+
+ if (scene.generation_method == 'sequential' and scene.qwen_generation_method == 'generate' and
+ scene.qwen_context_render_mode in {'REPLACE_STYLE', 'ADDITIONAL'}):
+ context_box = content_box.box()
+ context_box.label(text="Context Render Options")
+
+ row = context_box.row()
+ row.prop(scene, "qwen_prompt_gray_background", text="Prompt: Gray Background", toggle=True)
+
+ 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':
+ # IPAdapter Parameters
+ if not scene.generation_method == 'uv_inpaint':
+ ipadapter_main_box = content_box.box() # Group IPAdapter settings together
+ if scene.model_architecture == 'flux1':
+ row = ipadapter_main_box.row()
+ row.prop(scene, "use_flux_lora", text="Use Flux Depth LoRA", toggle=True, icon="MODIFIER")
+ row = ipadapter_main_box.row()
+ row.prop(scene, "use_ipadapter", text="Use IPAdapter (External image)", toggle=True, icon="MOD_MULTIRES")
+ if scene.use_ipadapter:
+ sub_ip_box = ipadapter_main_box.box()
+ row = sub_ip_box.row()
+ row.prop(scene, "ipadapter_image", text="Image")
+ row = sub_ip_box.row()
+ row.prop(scene, "ipadapter_strength", text="Strength")
+ if width_mode == 'narrow':
+ row = sub_ip_box.row()
+ row.prop(scene, "ipadapter_start", text="Start")
+ if width_mode == 'narrow':
+ row = sub_ip_box.row()
+ row.prop(scene, "ipadapter_end", text="End")
+ split = sub_ip_box.split(factor=0.5)
+ if context.scene.model_architecture == 'sdxl':
+ split.label(text="Weight Type:")
+ split.prop(scene, "ipadapter_weight_type", text="")
+
+ content_box.separator() # Separator between IPAdapter and ControlNet if both are shown
+ # ControlNet Parameters
+ if not (scene.model_architecture == 'flux1' and scene.use_flux_lora):
+ cn_box = content_box.box()
+ row = cn_box.row()
+ row.alignment = 'CENTER'
+ row.label(text="ControlNet Units", icon="NODETREE")
+ for i, unit in enumerate(scene.controlnet_units):
+ sub_unit_box = cn_box.box() # Each unit gets its own box
+ row = sub_unit_box.row()
+ row.label(text=f"Unit: {unit.unit_type.replace('_', ' ').title()}", icon="DOT")
+ row.alignment = 'LEFT'
+
+ if width_mode == 'narrow':
+ split = sub_unit_box.split(factor=0.35, align=True)
+ else:
+ split = sub_unit_box.split(factor=0.2, align=True)
+ split.label(text="Model:")
+ split.prop(unit, "model_name", text="")
+
+ row = sub_unit_box.row()
+ row.prop(unit, "strength", text="Strength")
+ if width_mode == 'narrow':
+ row = sub_unit_box.row()
+ row.prop(unit, "start_percent", text="Start")
+ if width_mode == 'narrow':
+ row = sub_unit_box.row()
+ row.prop(unit, "end_percent", text="End")
+
+ if unit.unit_type == 'canny':
+ row = sub_unit_box.row()
+ row.prop(scene, "canny_threshold_low", text="Canny Low")
+ if width_mode == 'narrow':
+ row = sub_unit_box.row()
+ row.prop(scene, "canny_threshold_high", text="Canny High")
+ if hasattr(unit, 'is_union') and unit.is_union:
+ row = sub_unit_box.row()
+ row.prop(unit, "use_union_type", text="Set Union Type", toggle=True, icon="MOD_BOOLEAN")
+
+ btn_row = cn_box.row(align=True)
+ if width_mode == 'wide':
+ btn_row.operator("stablegen.add_controlnet_unit", text="Add Unit", icon="ADD")
+ btn_row.operator("stablegen.remove_controlnet_unit", text="Remove Unit", icon="REMOVE")
+ else:
+ cn_box.operator("stablegen.add_controlnet_unit", text="Add ControlNet Unit", icon="ADD")
+ cn_box.operator("stablegen.remove_controlnet_unit", text="Remove Last ControlNet Unit", icon="REMOVE")
+
+ if _show_diffusion_sections and scene.model_architecture not in ('qwen_image_edit', 'flux2_klein'):
+ # --- Inpainting Options (Conditional) ---
+ if scene.generation_method == 'uv_inpaint' or scene.generation_method == 'sequential':
+ content_box = draw_collapsible_section(advanced_params_box, "show_masking_inpainting_settings", "Inpainting Options", icon="MOD_MASK")
+ if content_box: # content_box is the container for these settings
+ row = content_box.row()
+ if 'differential_diffusion' in _diff_props:
+ row.alert = True
+ row.prop(scene, "differential_diffusion", text="Use Differential Diffusion", toggle=True, icon="SMOOTHCURVE")
+ if 'differential_diffusion' in _diff_props:
+ row.label(text="→ " + _diff_props['differential_diffusion'])
+
+ if scene.differential_diffusion:
+ row = content_box.row()
+ row.prop(scene, "differential_noise", text="Add Latent Noise Mask", toggle=True, icon="MOD_NOISE")
+
+ if not (scene.differential_diffusion and not scene.differential_noise):
+ row = content_box.row()
+ row.prop(scene, "mask_blocky", text="Use Blocky Mask", icon="MOD_MASK")
+
+ if width_mode == 'narrow':
+ row = content_box.row()
+
+ row.prop(scene, "blur_mask", text="Blur Mask", toggle=True, icon="SURFACE_NSPHERE")
+
+ if scene.blur_mask:
+ row = content_box.row()
+ row.prop(scene, "blur_mask_radius", text="Blur Radius")
+ if width_mode == 'narrow':
+ row = content_box.row()
+ row.prop(scene, "blur_mask_sigma", text="Blur Sigma")
+
+ row = content_box.row() # Draw directly in content_box
+ row.prop(scene, "grow_mask_by", text="Grow Mask By")
+
+
+ # --- Generation Mode Specifics ---
+ if _show_diffusion_sections:
+ mode_specific_outer_box = draw_collapsible_section(advanced_params_box, "show_mode_specific_settings", "Generation Mode Specifics", icon="OPTIONS")
+ else:
+ mode_specific_outer_box = None
+ if mode_specific_outer_box: # This is the box where all mode-specific UIs should go
+
+ # Qwen Local Edit Mode Parameters
+ if scene.model_architecture.startswith('qwen') and scene.qwen_generation_method == 'local_edit':
+ row = mode_specific_outer_box.row()
+ row.alignment = 'CENTER'
+ row.label(text="Qwen Local Edit Parameters", icon='BRUSH_DATA')
+ row = mode_specific_outer_box.row()
+ if 'denoise' in _diff_props:
+ row.alert = True
+ row.prop(scene, "denoise", text="Denoise")
+ if 'denoise' in _diff_props:
+ row.label(text="→ " + _diff_props['denoise'])
+
+ # Angle Ramp Controls
+ box = mode_specific_outer_box.box()
+ row = box.row()
+ row.prop(scene, "refine_angle_ramp_active", text="Use Angle-Based Blending", icon="DRIVER")
+ if scene.refine_angle_ramp_active:
+ row = box.row()
+ row.prop(scene, "refine_angle_ramp_pos_0", text="Black Point")
+ row.prop(scene, "refine_angle_ramp_pos_1", text="White Point")
+
+ # Feather Ramp Controls
+ box = mode_specific_outer_box.box()
+ row = box.row()
+ row.prop(scene, "visibility_vignette", text="Use Vignette Blending", icon="DRIVER")
+ if scene.visibility_vignette:
+ row = box.row()
+ row.prop(scene, "refine_feather_ramp_pos_0", text="Black Point")
+ row.prop(scene, "refine_feather_ramp_pos_1", text="White Point")
+ row = box.row()
+ row.prop(scene, "visibility_vignette_width", text="Feather Width")
+ if width_mode == 'narrow':
+ row = box.row()
+ row.prop(scene, "visibility_vignette_softness", text="Feather Softness")
+ row = box.row()
+ row.prop(scene, "visibility_vignette_blur", text="Blur Mask", icon="SURFACE_NSPHERE")
+
+ # Edge Feather Projection Controls
+ box = mode_specific_outer_box.box()
+ row = box.row()
+ row.prop(scene, "refine_edge_feather_projection", text="Edge Feather (Projection)", icon="MOD_EDGESPLIT")
+ if scene.refine_edge_feather_projection:
+ row = box.row()
+ row.prop(scene, "refine_edge_feather_width", text="Feather Width (px)")
+ row = box.row()
+ row.prop(scene, "refine_edge_feather_softness", text="Feather Softness")
+
+ # Color Matching
+ box = mode_specific_outer_box.box()
+ row = box.row()
+ row.prop(scene, "view_blend_use_color_match", text="Match Colors to Viewport", toggle=True, icon="COLOR")
+ if scene.view_blend_use_color_match:
+ row = box.row(align=True)
+ row.prop(scene, "view_blend_color_match_method", text="Method")
+ row = box.row()
+ row.prop(scene, "view_blend_color_match_strength", text="Strength")
+
+ # Qwen Refine Mode Parameters
+ elif scene.model_architecture.startswith('qwen') and scene.qwen_generation_method == 'refine':
+ row = mode_specific_outer_box.row()
+ row.alignment = 'CENTER'
+ row.label(text="Qwen Refine Parameters", icon='SHADERFX')
+ row = mode_specific_outer_box.row()
+ if 'denoise' in _diff_props:
+ row.alert = True
+ row.prop(scene, "denoise", text="Denoise")
+ if 'denoise' in _diff_props:
+ row.label(text="→ " + _diff_props['denoise'])
+
+ # Grid Mode Parameters
+ elif scene.generation_method == 'grid':
+ # Draw Grid parameters directly into mode_specific_outer_box
+ row = mode_specific_outer_box.row()
+ row.alignment = 'CENTER'
+ row.label(text="Grid Mode Parameters", icon="MESH_GRID")
+
+ row = mode_specific_outer_box.row()
+ row.prop(scene, "refine_images", text="Refine Images", toggle=True, icon="SHADERFX")
+ if scene.refine_images:
+ split = mode_specific_outer_box.split(factor=0.5)
+ split.label(text="Refine Sampler:")
+ split.prop(scene, "refine_sampler", text="")
+
+ split = mode_specific_outer_box.split(factor=0.5)
+ split.label(text="Refine Scheduler:")
+ split.prop(scene, "refine_scheduler", text="")
+
+ row = mode_specific_outer_box.row()
+ if 'denoise' in _diff_props:
+ row.alert = True
+ row.prop(scene, "denoise", text="Denoise")
+ if 'denoise' in _diff_props:
+ row.label(text="→ " + _diff_props['denoise'])
+ if width_mode == 'narrow':
+ row = mode_specific_outer_box.row()
+ row.prop(scene, "refine_cfg", text="Refine CFG")
+ if width_mode == 'narrow':
+ row = mode_specific_outer_box.row()
+ row.prop(scene, "refine_steps", text="Refine Steps")
+
+ row = mode_specific_outer_box.row()
+ split = mode_specific_outer_box.split(factor=0.25)
+ split.label(text="Refine Prompt:")
+ split.prop(scene, "refine_prompt", text="")
+
+ split = mode_specific_outer_box.split(factor=0.5)
+ split.label(text="Refine Upscale:")
+ split.prop(scene, "refine_upscale_method", text="")
+
+ # Separate Mode Parameters
+ elif scene.generation_method == 'separate':
+ row = mode_specific_outer_box.row()
+ row.alignment = 'CENTER'
+ row.label(text="Separate Mode Parameters", icon='FORCE_FORCE')
+
+ row = mode_specific_outer_box.row()
+ row.prop(scene, "sequential_ipadapter", text="Use IPAdapter for Separate Mode", toggle=True, icon="MODIFIER")
+ if scene.sequential_ipadapter:
+ sub_ip_box_separate = mode_specific_outer_box.box()
+
+ split = sub_ip_box_separate.split(factor=0.5)
+ split.label(text="Mode:")
+ split.prop(scene, "sequential_ipadapter_mode", text="")
+
+ if scene.sequential_ipadapter_mode == 'recent':
+ sub_ip_box_separate.prop(scene, "sequential_desaturate_factor", text="Desaturate")
+ sub_ip_box_separate.prop(scene, "sequential_contrast_factor", text="Reduce Contrast")
+
+ if context.scene.model_architecture not in ('qwen_image_edit', 'flux2_klein'):
+ split = sub_ip_box_separate.split(factor=0.5)
+ if context.scene.model_architecture == 'sdxl':
+ split.label(text="Weight Type:")
+ split.prop(scene, "ipadapter_weight_type", text="")
+
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "ipadapter_strength", text="Strength")
+ if width_mode == 'narrow':
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "ipadapter_start", text="Start")
+ if width_mode == 'narrow':
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "ipadapter_end", text="End")
+
+ if context.scene.sequential_ipadapter_mode == 'first':
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "sequential_ipadapter_regenerate", text="Regenerate First Image", toggle=True, icon="FILE_REFRESH")
+ if context.scene.sequential_ipadapter_regenerate:
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "sequential_ipadapter_regenerate_wo_controlnet", text="Generate reference without ControlNet", toggle=True, icon="HIDE_OFF")
+
+ # Refine Mode Parameters
+ elif scene.generation_method == 'refine':
+ row = mode_specific_outer_box.row()
+ row.alignment = 'CENTER'
+ row.label(text="Refine Mode Parameters", icon='SHADERFX')
+ row = mode_specific_outer_box.row()
+ if 'denoise' in _diff_props:
+ row.alert = True
+ row.prop(scene, "denoise", text="Denoise")
+ if 'denoise' in _diff_props:
+ row.label(text="→ " + _diff_props['denoise'])
+ row = mode_specific_outer_box.row()
+ row.prop(scene, "sequential_ipadapter", text="Use IPAdapter for Refine Mode", toggle=True, icon="MODIFIER")
+ if scene.sequential_ipadapter:
+ sub_ip_box_separate = mode_specific_outer_box.box()
+
+ split = sub_ip_box_separate.split(factor=0.5)
+ split.label(text="Mode:")
+ split.prop(scene, "sequential_ipadapter_mode", text="")
+
+ split = sub_ip_box_separate.split(factor=0.5)
+ if context.scene.model_architecture == 'sdxl':
+ split.label(text="Weight Type:")
+ split.prop(scene, "ipadapter_weight_type", text="")
+
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "ipadapter_strength", text="Strength")
+ if width_mode == 'narrow':
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "ipadapter_start", text="Start")
+ if width_mode == 'narrow':
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "ipadapter_end", text="End")
+
+ if context.scene.sequential_ipadapter_mode == 'first':
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "sequential_ipadapter_regenerate", text="Regenerate First Image", toggle=True, icon="FILE_REFRESH")
+ if context.scene.sequential_ipadapter_regenerate:
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "sequential_ipadapter_regenerate_wo_controlnet", text="Generate reference without ControlNet", toggle=True, icon="HIDE_OFF")
+
+ # Local Edit Mode Parameters
+ elif scene.generation_method == 'local_edit':
+ row = mode_specific_outer_box.row()
+ row.alignment = 'CENTER'
+ row.label(text="Local Edit Parameters", icon='BRUSH_DATA')
+ row = mode_specific_outer_box.row()
+ if 'denoise' in _diff_props:
+ row.alert = True
+ row.prop(scene, "denoise", text="Denoise")
+ if 'denoise' in _diff_props:
+ row.label(text="→ " + _diff_props['denoise'])
+
+ # Angle Ramp Controls
+ box = mode_specific_outer_box.box()
+ row = box.row()
+ row.prop(scene, "refine_angle_ramp_active", text="Use Angle-Based Blending", icon="DRIVER")
+ if scene.refine_angle_ramp_active:
+ row = box.row()
+ row.prop(scene, "refine_angle_ramp_pos_0", text="Black Point")
+ row.prop(scene, "refine_angle_ramp_pos_1", text="White Point")
+
+ # Feather Ramp Controls
+ box = mode_specific_outer_box.box()
+ row = box.row()
+ row.prop(scene, "visibility_vignette", text="Use Vignette Blending", icon="DRIVER")
+ if scene.visibility_vignette:
+ row = box.row()
+ row.prop(scene, "refine_feather_ramp_pos_0", text="Black Point")
+ row.prop(scene, "refine_feather_ramp_pos_1", text="White Point")
+ row = box.row()
+ row.prop(scene, "visibility_vignette_width", text="Feather Width")
+ if width_mode == 'narrow':
+ row = box.row()
+ row.prop(scene, "visibility_vignette_softness", text="Feather Softness")
+ row = box.row()
+ row.prop(scene, "visibility_vignette_blur", text="Blur Mask", icon="SURFACE_NSPHERE")
+
+ # Edge Feather Projection Controls
+ box = mode_specific_outer_box.box()
+ row = box.row()
+ row.prop(scene, "refine_edge_feather_projection", text="Edge Feather (Projection)", icon="MOD_EDGESPLIT")
+ if scene.refine_edge_feather_projection:
+ row = box.row()
+ row.prop(scene, "refine_edge_feather_width", text="Feather Width (px)")
+ row = box.row()
+ row.prop(scene, "refine_edge_feather_softness", text="Feather Softness")
+
+ # Color Matching
+ box = mode_specific_outer_box.box()
+ row = box.row()
+ row.prop(scene, "view_blend_use_color_match", text="Match Colors to Viewport", toggle=True, icon="COLOR")
+ if scene.view_blend_use_color_match:
+ row = box.row(align=True)
+ row.prop(scene, "view_blend_color_match_method", text="Method")
+ row = box.row()
+ row.prop(scene, "view_blend_color_match_strength", text="Strength")
+
+ row = mode_specific_outer_box.row()
+ row.prop(scene, "sequential_ipadapter", text="Use IPAdapter for Local Edit", toggle=True, icon="MODIFIER")
+ if scene.sequential_ipadapter:
+ sub_ip_box_separate = mode_specific_outer_box.box()
+
+ split = sub_ip_box_separate.split(factor=0.5)
+ split.label(text="Mode:")
+ split.prop(scene, "sequential_ipadapter_mode", text="")
+
+ split = sub_ip_box_separate.split(factor=0.5)
+ if context.scene.model_architecture == 'sdxl':
+ split.label(text="Weight Type:")
+ split.prop(scene, "ipadapter_weight_type", text="")
+
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "ipadapter_strength", text="Strength")
+ if width_mode == 'narrow':
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "ipadapter_start", text="Start")
+ if width_mode == 'narrow':
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "ipadapter_end", text="End")
+
+ if context.scene.sequential_ipadapter_mode == 'first':
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "sequential_ipadapter_regenerate", text="Regenerate First Image", toggle=True, icon="FILE_REFRESH")
+ if context.scene.sequential_ipadapter_regenerate:
+ row = sub_ip_box_separate.row()
+ row.prop(scene, "sequential_ipadapter_regenerate_wo_controlnet", text="Generate reference without ControlNet", toggle=True, icon="HIDE_OFF")
+
+ # UV Inpainting Parameters
+ elif scene.generation_method == 'uv_inpaint':
+ row = mode_specific_outer_box.row()
+ row.alignment = 'CENTER'
+ row.label(text="UV Inpainting Parameters", icon="IMAGE_PLANE")
+ row = mode_specific_outer_box.row()
+ row.prop(scene, "allow_modify_existing_textures", text="Allow Modifying Existing Textures", toggle=True, icon="TEXTURE")
+ row = mode_specific_outer_box.row()
+ row.prop(scene, "ask_object_prompts", text="Ask for Object Specific Prompts", toggle=True, icon="QUESTION")
+
+ # Sequential Mode Parameters
+ elif scene.generation_method == 'sequential':
+ row = mode_specific_outer_box.row()
+ row.alignment = 'CENTER'
+ row.label(text="Sequential Mode Parameters", icon="SEQUENCE")
+
+ if not (scene.differential_diffusion and not scene.differential_noise):
+ row = mode_specific_outer_box.row()
+ if 'sequential_smooth' in _diff_props:
+ row.alert = True
+ row.prop(scene, "sequential_smooth", text="Use Smooth Visibility Map", toggle=True, icon="MOD_SMOOTH")
+ if 'sequential_smooth' in _diff_props:
+ row.label(text="→ " + _diff_props['sequential_smooth'])
+ if width_mode == 'narrow':
+ row = mode_specific_outer_box.row()
+ row.prop(scene, "weight_exponent_mask", text="Exponent for Visibility Map", toggle=True, icon="IPO_EXPO")
+
+ if not scene.sequential_smooth:
+ row = mode_specific_outer_box.row()
+ row.prop(scene, "sequential_factor", text="Visibility Threshold")
+ else:
+ row = mode_specific_outer_box.row()
+ row.prop(scene, "sequential_factor_smooth", text="Smooth Visibility Black Point")
+ if width_mode == 'narrow':
+ row = mode_specific_outer_box.row()
+ row.prop(scene, "sequential_factor_smooth_2", text="Smooth Visibility White Point")
+
+ row = mode_specific_outer_box.row()
+ row.prop(scene, "sequential_ipadapter", text="Use IPAdapter for Sequential Mode", toggle=True, icon="MODIFIER")
+ if scene.sequential_ipadapter:
+ sub_ip_seq_box = mode_specific_outer_box.box()
+
+ split = sub_ip_seq_box.split(factor=0.5)
+ split.label(text="Mode:")
+ split.prop(scene, "sequential_ipadapter_mode", text="")
+
+ if scene.sequential_ipadapter_mode == 'recent':
+ sub_ip_seq_box.prop(scene, "sequential_desaturate_factor", text="Desaturate")
+ sub_ip_seq_box.prop(scene, "sequential_contrast_factor", text="Reduce Contrast")
+
+ if context.scene.model_architecture not in ('qwen_image_edit', 'flux2_klein'):
+ split = sub_ip_seq_box.split(factor=0.5)
+ if context.scene.model_architecture == 'sdxl':
+ split.label(text="Weight Type:")
+ split.prop(scene, "ipadapter_weight_type", text="")
+
+ row = sub_ip_seq_box.row()
+ row.prop(scene, "ipadapter_strength", text="Strength")
+ if width_mode == 'narrow':
+ row = sub_ip_seq_box.row()
+ row.prop(scene, "ipadapter_start", text="Start")
+ if width_mode == 'narrow':
+ row = sub_ip_seq_box.row()
+ row.prop(scene, "ipadapter_end", text="End")
+
+ if context.scene.sequential_ipadapter_mode == 'first':
+ row = sub_ip_seq_box.row()
+ row.prop(scene, "sequential_ipadapter_regenerate", text="Regenerate First Image", toggle=True, icon="FILE_REFRESH")
+ if context.scene.sequential_ipadapter_regenerate:
+ row = sub_ip_seq_box.row()
+ row.prop(scene, "sequential_ipadapter_regenerate_wo_controlnet", text="Generate reference without ControlNet", toggle=True, icon="HIDE_OFF")
+
+ # --- Tools ---
+ layout.separator()
+ tools_box = layout.box()
+ row = tools_box.row()
+ row.alignment = 'CENTER'
+ row.label(text="Tools", icon="TOOL_SETTINGS")
+
+ row = tools_box.row()
+ row.operator("object.switch_material", text="Switch Material", icon="MATERIAL_DATA")
+ if width_mode == 'narrow':
+ row = tools_box.row()
+ row.operator("object.add_hdri", text="Add HDRI Light", icon="WORLD")
+
+ row = tools_box.row()
+ row.operator("object.apply_all_mesh_modifiers", text="Apply All Modifiers", icon="MODIFIER_DATA")
+ if width_mode == 'narrow':
+ row = tools_box.row()
+ row.operator("object.curves_to_mesh", text="Convert Curves to Mesh", icon="CURVE_DATA")
+
+ row = tools_box.row()
+ if config_error_message:
+ row.enabled = False
+ row.operator("object.export_orbit_gif", text=f"Cannot Export: {config_error_message}", icon="ERROR")
+ else:
+ row.enabled = True
+ row.operator("object.export_orbit_gif", text="Export Orbit GIF/MP4", icon="RENDER_ANIMATION")
+
+ if width_mode == 'narrow':
+ row = tools_box.row()
+ row.operator("object.stablegen_reproject", text="Reproject Textures", icon="FILE_REFRESH")
+
+ row = tools_box.row()
+ row.operator("object.stablegen_mirror_reproject", text="Mirror Last Projection", icon="MOD_MIRROR")
+
+ # --- Debug Tools ---
+ prefs = context.preferences.addons.get(_ADDON_PKG)
+ if prefs and prefs.preferences.enable_debug:
+ layout.separator()
+ debug_box = layout.box()
+ row = debug_box.row()
+ row.alignment = 'CENTER'
+ row.label(text="Debug / Diagnostics", icon="GHOST_ENABLED")
+
+ row = debug_box.row()
+ row.operator("stablegen.debug_solid_colors", text="Draw Solid Colors", icon="COLOR")
+ if width_mode == 'narrow':
+ row = debug_box.row()
+ row.operator("stablegen.debug_grid_pattern", text="Grid Pattern", icon="MESH_GRID")
+
+ row = debug_box.row()
+ row.operator("stablegen.debug_coverage_heatmap", text="Coverage Heatmap", icon="AREA_SWAP")
+ if width_mode == 'narrow':
+ row = debug_box.row()
+ row.operator("stablegen.debug_visibility_material", text="Visibility Material", icon="HIDE_OFF")
+
+ row = debug_box.row()
+ row.operator("stablegen.debug_uv_seam_viz", text="UV Seam Visualizer", icon="UV")
+ if width_mode == 'narrow':
+ row = debug_box.row()
+ row.operator("stablegen.debug_restore_material", text="Remove Debug Mats", icon="TRASH")
+
+ row = debug_box.row()
+ op = row.operator("stablegen.debug_per_camera_weight", text="Per-Camera Weight", icon="CAMERA_DATA")
+ if width_mode == 'narrow':
+ row = debug_box.row()
+ op2 = row.operator("stablegen.debug_feather_preview", text="Feather Preview", icon="MOD_SMOOTH")
+
+ # --- Narrow panel hint ---
+ if width_mode == 'narrow':
+ hint_row = layout.row()
+ hint_row.alignment = 'CENTER'
+ hint_row.label(text="Widen panel for side-by-side layout", icon="INFO")
+
+ layout.separator()
+
diff --git a/stablegen/ui/presets.py b/stablegen/ui/presets.py
new file mode 100644
index 0000000..fb0680d
--- /dev/null
+++ b/stablegen/ui/presets.py
@@ -0,0 +1,611 @@
+"""Preset definitions, callbacks, and preset operators."""
+
+import math
+import bpy # pylint: disable=import-error
+import mathutils # pylint: disable=import-error
+from ..utils import sg_modal_active
+
+PRESETS = {
+ "DEFAULT": {"description": "Default settings for general purpose generation", "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": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "hm-mvgd-hm", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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": False, "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, "pbr_decomposition": False, "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': 'sdxl_lightning_8step_lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]},
+ "MODEL IS IMPORTANT": {"description": "Same as default, but is more guided by the model", "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": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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": False, "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, "pbr_decomposition": False, "controlnet_units": [{'unit_type': 'depth', 'model_name': 'controlnet_depth_sdxl.safetensors', 'strength': 0.75, '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}]},
+ "CHARACTERS": {"description": "Optimized settings for character generation", "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": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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_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, "pbr_decomposition": False, "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': 'sdxl_lightning_8step_lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]},
+ "CHARACTERS (ALTERNATIVE MASKING)": {"description": "Optimized for character generation. Uses alternative masking parameters to be more consistent between images, but may produce more artifacts. Try if \"Characters\" fails.", "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": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.5, "sequential_factor_smooth": 0.3499999940395355, "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": 10, "blur_mask_sigma": 1.0, "grow_mask_by": 3, "canny_threshold_low": 0, "canny_threshold_high": 80, "pbr_decomposition": False, "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': 'sdxl_lightning_8step_lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]},
+ "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, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "grid", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "pbr_decomposition": False, "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, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "uv_inpaint", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "pbr_decomposition": False, "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, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "separate", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "pbr_decomposition": False, "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 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","qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": [{'model_name': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
+ "QWEN 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", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 6.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": [{'model_name': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
+ "QWEN 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", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "early_priority": True, "early_priority_strength": 0.5, "pbr_decomposition": False, "lora_units": [{'model_name': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
+ "QWEN VORONOI": {"description": "Voronoi projection mode with exponent 1000 for hard camera segmentation during generation, then resets to 15 for softer blending. Each surface point is dominated by its closest camera. Based on Qwen Precise.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 1000.0, "weight_exponent_generation_only": True, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": True, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": [{'model_name': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
+ "QWEN PRECISE (NUNCHAKU)": {"description": "Precise detail using Nunchaku. Meant to be used with the Nunchaku model which has the 4-step Lightning LoRA included. Requires Nunchaku nodes.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": []},
+ "QWEN SAFE (NUNCHAKU)": {"description": "Safer fallback using Nunchaku. Meant to be used with the Nunchaku model which has the 4-step Lightning LoRA included. Requires Nunchaku nodes.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 6.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": []},
+ "QWEN ALT (NUNCHAKU)": {"description": "Balanced option using Nunchaku. Meant to be used with the Nunchaku model which has the 4-step Lightning LoRA included. Requires Nunchaku nodes.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "early_priority": True, "early_priority_strength": 0.5, "pbr_decomposition": False, "lora_units": []},
+ "QWEN VORONOI (NUNCHAKU)": {"description": "Voronoi projection mode using Nunchaku. Exponent 1000 for hard camera segmentation during generation, then resets to 15 for softer blending. Requires Nunchaku nodes.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 1000.0, "weight_exponent_generation_only": True, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": True, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": []},
+ "KLEIN PRECISE": {"description": "FLUX.2 Klein with precise detail when camera overlap is good. Uses depth reference images with CFGGuider (cfg=1). Best for well-covered geometry.", "control_after_generate": "fixed", "model_architecture": "flux2_klein", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour.", "qwen_custom_prompt_seq_none": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour, adopting the visual style from image 2.", "qwen_custom_prompt_seq_replace": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray.", "qwen_custom_prompt_seq_additional": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray. Image 3 represents the overall style.", "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": []},
+ "KLEIN SAFE": {"description": "FLUX.2 Klein safer fallback when coverage is limited. Uses the previous view instead of context renders with depth references and CFGGuider (cfg=1).", "control_after_generate": "fixed", "model_architecture": "flux2_klein", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 6.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour.", "qwen_custom_prompt_seq_none": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour, adopting the visual style from image 2.", "qwen_custom_prompt_seq_replace": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray.", "qwen_custom_prompt_seq_additional": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray. Image 3 represents the overall style.", "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": []},
+ "KLEIN ALT": {"description": "FLUX.2 Klein balanced option that mixes additional context renders with sequential references. Uses depth references and CFGGuider (cfg=1).", "control_after_generate": "fixed", "model_architecture": "flux2_klein", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": False, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour.", "qwen_custom_prompt_seq_none": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour, adopting the visual style from image 2.", "qwen_custom_prompt_seq_replace": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray.", "qwen_custom_prompt_seq_additional": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray. Image 3 represents the overall style.", "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "early_priority": True, "early_priority_strength": 0.5, "pbr_decomposition": False, "lora_units": []},
+ "KLEIN VORONOI": {"description": "FLUX.2 Klein voronoi projection mode. Exponent 1000 for hard camera segmentation during generation, then resets to 15 for softer blending. Uses depth references and CFGGuider (cfg=1).", "control_after_generate": "fixed", "model_architecture": "flux2_klein", "qwen_generation_method": "generate", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 1000.0, "weight_exponent_generation_only": True, "weight_exponent_after_generation": 15.0, "qwen_voronoi_mode": True, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.8, "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": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour.", "qwen_custom_prompt_seq_none": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour, adopting the visual style from image 2.", "qwen_custom_prompt_seq_replace": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray.", "qwen_custom_prompt_seq_additional": "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray. Image 3 represents the overall style.", "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "pbr_decomposition": False, "lora_units": []},
+ "LOCAL REFINE": {"description": "Uses the SDXL local edit mode to improve detail / refine specific areas. Use new set of cameras or a single camera pointed at the area you want to refine.", "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, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "local_edit", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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.10000000149011612, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "original_render", "sequential_desaturate_factor": 0.10000000149011612, "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_voronoi_mode": True, "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 and transfer the format of '{main_prompt}' in image 1 to the style from image 2", "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": 5.0, "qwen_context_cleanup_value_adjust": 0.0, "qwen_context_fallback_dilation": 1, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05, "visibility_vignette": True, "visibility_vignette_width": 0.3, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "pbr_decomposition": False, "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': 'sdxl_lightning_8step_lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]},
+ "LOCAL EDIT (QWEN)": {"description": "Uses Qwen to make targeted edits to specific areas. Point cameras at what you want to change — you can alter colors, style, rewrite text, add details, and more. Untouched areas are preserved.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "local_edit", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": 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": False, "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, "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_voronoi_mode": False, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": False, "refine_angle_ramp_active": False, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05, "visibility_vignette": True, "visibility_vignette_width": 0.1, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6, "refine_edge_feather_projection": True, "refine_edge_feather_width": 15, "refine_edge_feather_softness": 1.0, "pbr_decomposition": False, "lora_units": [{'model_name': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
+ "REFINE (QWEN)": {"description": "Uses Qwen to restyle or globally modify the entire existing texture. Applies changes uniformly across all camera views — ideal for changing the overall color scheme, art style, or surface appearance.", "control_after_generate": "fixed", "model_architecture": "qwen_image_edit", "qwen_generation_method": "refine", "steps": 4, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "fallback_color": mathutils.Color((0.0, 0.0, 0.0)), "discard_factor": 70.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": 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": False, "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, "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_voronoi_mode": False, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": False, "pbr_decomposition": False, "lora_units": [{'model_name': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
+ "DEFAULT (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and SDXL to generate a textured mesh. Optimized for general object generation. May not work ideally for the cases which have their specialized presets.", "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": 75.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 85.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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.10000000149011612, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "trellis2_input", "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, "early_priority": False, "early_priority_strength": 0.5, "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, "qwen_guidance_map_type": "depth", "qwen_voronoi_mode": False, "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 and transfer the format of '{main_prompt}' in image 1 to the style from image 2", "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": 5.0, "qwen_context_cleanup_value_adjust": 0.0, "qwen_context_fallback_dilation": 1, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "sdxl", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 10, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": False, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": False, "trellis2_max_elevation": 1.2216999530792236, "trellis2_min_elevation": -1.0471975803375244, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": False, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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}]},
+ "TRELLIS.2 (MESH ONLY)": {"description": "Uses TRELLIS.2 to generate a mesh without texturing. Useful when you only need geometry or plan to texture manually.", "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": 75.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 85.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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.10000000149011612, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "trellis2_input", "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, "early_priority": False, "early_priority_strength": 0.5, "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, "qwen_guidance_map_type": "depth", "qwen_voronoi_mode": False, "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 and transfer the format of '{main_prompt}' in image 1 to the style from image 2", "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": 5.0, "qwen_context_cleanup_value_adjust": 0.0, "qwen_context_fallback_dilation": 1, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "none", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 10, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": False, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": False, "trellis2_max_elevation": 1.2216999530792236, "trellis2_min_elevation": -1.0471975803375244, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": False, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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}]},
+ "CHARACTERS (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and SDXL to generate a textured mesh. Optimized settings for generating characters. Will not texture bottom facing faces.", "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": 75.0, "discard_factor_generation_only": True, "discard_factor_after_generation": 85.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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.10000000149011612, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "trellis2_input", "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, "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_voronoi_mode": False, "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 and transfer the format of '{main_prompt}' in image 1 to the style from image 2", "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": 5.0, "qwen_context_cleanup_value_adjust": 0.0, "qwen_context_fallback_dilation": 1, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "sdxl", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 8, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": True, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": True, "trellis2_max_elevation": 0.8726646259971648, "trellis2_min_elevation": -0.8726646259971648, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": False, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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}]},
+ "ARCHITECTURE (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and SDXL to generate a textured mesh. Optimized settings for architecture, and other models with flat walls and sharp angles.", "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, "discard_factor_generation_only": True, "discard_factor_after_generation": 85.0, "weight_exponent": 10.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "separate", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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": "trellis2_input", "sequential_desaturate_factor": 0.0, "sequential_contrast_factor": 0.0, "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 0.800000011920929, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "early_priority": False, "early_priority_strength": 0.5, "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, "qwen_guidance_map_type": "depth", "qwen_voronoi_mode": False, "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 and transfer the format of '{main_prompt}' in image 1 to the style from image 2", "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": 5.0, "qwen_context_cleanup_value_adjust": 0.0, "qwen_context_fallback_dilation": 1, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "sdxl", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 8, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": True, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": False, "trellis2_max_elevation": 1.2216999530792236, "trellis2_min_elevation": -1.0471975803375244, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": False, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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 PRECISE (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and Qwen Image Edit to generate a textured mesh. Precise detail when camera overlap is good. Relies on context renders plus the prompt.", "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, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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, "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_voronoi_mode": False, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "sg_silhouette_margin": 3, "sg_silhouette_depth": 0.05000000074505806, "sg_silhouette_rays": "4", "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "qwen_image_edit", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 8, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": True, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_seed": 0, "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": True, "trellis2_max_elevation": 0.8726646259971648, "trellis2_min_elevation": -0.8726646259971648, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": True, "qwen_trellis2_style_initial_only": True, "trellis2_skip_texture": True, "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': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
+ "QWEN SAFE (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and Qwen Image Edit to generate a textured mesh. Safer fallback when coverage is limited. Uses TRELLIS.2 input image as sequential style reference for global coherence.", "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, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 6.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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.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_voronoi_mode": False, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "sg_silhouette_margin": 3, "sg_silhouette_depth": 0.05000000074505806, "sg_silhouette_rays": "4", "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "qwen_image_edit", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 8, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": True, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_seed": 0, "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": True, "trellis2_max_elevation": 0.8726646259971648, "trellis2_min_elevation": -0.8726646259971648, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": True, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
+ "QWEN ALT (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and Qwen Image Edit to generate a textured mesh. Uses ADDITIONAL context renders with TRELLIS.2 style transfer for consistency. Good general-purpose Qwen pipeline.", "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, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 3.0, "weight_exponent_generation_only": False, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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.10000000149011612, "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 1.0, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "early_priority": True, "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_voronoi_mode": False, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "sg_silhouette_margin": 3, "sg_silhouette_depth": 0.05000000074505806, "sg_silhouette_rays": "4", "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "qwen_image_edit", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 8, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": True, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_seed": 0, "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": True, "trellis2_max_elevation": 0.8726646259971648, "trellis2_min_elevation": -0.8726646259971648, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": True, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
+ "QWEN VORONOI (MESH + TEXTURE)": {"description": "Uses TRELLIS.2 and Qwen Image Edit to generate a textured mesh. Voronoi projection with exponent 1000 for hard camera segmentation during generation, then resets to 15 for softer blending.", "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, "discard_factor_generation_only": True, "discard_factor_after_generation": 90.0, "weight_exponent": 1000.0, "weight_exponent_generation_only": True, "weight_exponent_after_generation": 15.0, "view_blend_use_color_match": False, "view_blend_color_match_method": "reinhard", "view_blend_color_match_strength": 1.0, "clip_skip": 1, "auto_rescale": True, "auto_rescale_target_mp": 1.0, "overwrite_material": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 0.800000011920929, "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, "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_voronoi_mode": True, "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, "qwen_prompt_gray_background": True, "qwen_rescale_alignment": True, "qwen_generation_method": "generate", "qwen_refine_use_prev_ref": False, "qwen_refine_use_depth": False, "qwen_timestep_zero_ref": False, "refine_angle_ramp_active": True, "refine_angle_ramp_pos_0": 0.0, "refine_angle_ramp_pos_1": 0.05000000074505806, "visibility_vignette": True, "visibility_vignette_width": 0.15000000596046448, "visibility_vignette_softness": 1.0, "visibility_vignette_blur": False, "sg_silhouette_margin": 3, "sg_silhouette_depth": 0.05000000074505806, "sg_silhouette_rays": "4", "refine_feather_ramp_pos_0": 0.0, "refine_feather_ramp_pos_1": 0.6000000238418579, "refine_edge_feather_projection": True, "refine_edge_feather_width": 30, "refine_edge_feather_softness": 1.0, "trellis2_texture_mode": "qwen_image_edit", "trellis2_initial_image_arch": "sdxl", "trellis2_camera_count": 8, "trellis2_placement_mode": "normal_weighted", "trellis2_auto_prompts": True, "trellis2_exclude_bottom": True, "trellis2_exclude_bottom_angle": 1.5533000230789185, "trellis2_auto_aspect": "per_camera", "trellis2_occlusion_mode": "none", "trellis2_consider_existing": False, "trellis2_delete_cameras": False, "trellis2_coverage_target": 0.949999988079071, "trellis2_max_auto_cameras": 12, "trellis2_fan_angle": 90.0, "trellis2_resolution": "1024_cascade", "trellis2_vram_mode": "disk_offload", "trellis2_attn_backend": "flash_attn", "trellis2_seed": 0, "trellis2_ss_guidance": 7.5, "trellis2_ss_steps": 12, "trellis2_shape_guidance": 7.5, "trellis2_shape_steps": 12, "trellis2_tex_guidance": 7.5, "trellis2_tex_steps": 12, "trellis2_max_tokens": 32768, "trellis2_texture_size": 4096, "trellis2_decimation": 1000000, "trellis2_remesh": True, "trellis2_post_processing_enabled": True, "trellis2_bg_removal": "auto", "trellis2_background_color": "black", "trellis2_import_scale": 2.0, "trellis2_clamp_elevation": True, "trellis2_max_elevation": 0.8726646259971648, "trellis2_min_elevation": -0.8726646259971648, "trellis2_auto_lighting": True, "use_ipadapter": False, "sequential_ipadapter_regenerate_wo_controlnet": False, "allow_modify_existing_textures": False, "ask_object_prompts": True, "weight_exponent_mask": False, "mask_blocky": False, "architecture_mode": "trellis2", "use_camera_prompts": True, "sg_use_custom_camera_order": False, "pbr_decomposition": False, "generation_mode": "standard", "texture_objects": "all", "use_flux_lora": True, "qwen_use_trellis2_style": True, "qwen_trellis2_style_initial_only": False, "trellis2_skip_texture": True, "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': 'Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors', 'model_strength': 1.0, 'clip_strength': 0.0}]},
+}
+
+# Global list of all generation parameter names to check for a preset.
+GEN_PARAMETERS = [
+ "control_after_generate",
+ "model_architecture",
+ "steps",
+ "cfg",
+ "sampler",
+ "scheduler",
+ "fallback_color",
+ "discard_factor",
+ "discard_factor_generation_only",
+ "discard_factor_after_generation",
+ "weight_exponent",
+ "weight_exponent_generation_only",
+ "weight_exponent_after_generation",
+ "view_blend_use_color_match",
+ "view_blend_color_match_method",
+ "view_blend_color_match_strength",
+ "clip_skip",
+ "auto_rescale",
+ "auto_rescale_target_mp",
+ "overwrite_material",
+ "generation_method",
+ "refine_images",
+ "refine_steps",
+ "refine_sampler",
+ "refine_scheduler",
+ "denoise",
+ "refine_cfg",
+ "refine_prompt",
+ "refine_upscale_method",
+ "sequential_smooth",
+ "sequential_custom_camera_order",
+ "sequential_factor",
+ "sequential_factor_smooth",
+ "sequential_factor_smooth_2",
+ "sequential_ipadapter",
+ "sequential_ipadapter_mode",
+ "sequential_desaturate_factor",
+ "sequential_contrast_factor",
+ "sequential_ipadapter_regenerate",
+ "ipadapter_weight_type",
+ "ipadapter_strength",
+ "ipadapter_start",
+ "ipadapter_end",
+ "early_priority",
+ "early_priority_strength",
+ "differential_diffusion",
+ "differential_noise",
+ "blur_mask",
+ "blur_mask_radius",
+ "blur_mask_sigma",
+ "grow_mask_by",
+ "canny_threshold_low",
+ "canny_threshold_high",
+ "qwen_guidance_map_type",
+ "qwen_voronoi_mode",
+ "qwen_use_external_style_image",
+ "qwen_external_style_image",
+ "qwen_context_render_mode",
+ "qwen_external_style_initial_only",
+ "qwen_use_custom_prompts",
+ "qwen_custom_prompt_initial",
+ "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",
+ "qwen_prompt_gray_background",
+ "qwen_rescale_alignment",
+ "qwen_generation_method",
+ "qwen_refine_use_prev_ref",
+ "qwen_refine_use_depth",
+ "qwen_timestep_zero_ref",
+ "refine_angle_ramp_active",
+ "refine_angle_ramp_pos_0",
+ "refine_angle_ramp_pos_1",
+ "visibility_vignette",
+ "visibility_vignette_width",
+ "visibility_vignette_softness",
+ "visibility_vignette_blur",
+ "sg_silhouette_margin",
+ "sg_silhouette_depth",
+ "sg_silhouette_rays",
+ "refine_feather_ramp_pos_0",
+ "refine_feather_ramp_pos_1",
+ "refine_edge_feather_projection",
+ "refine_edge_feather_width",
+ "refine_edge_feather_softness",
+ # --- TRELLIS.2 ---
+ "trellis2_texture_mode",
+ "trellis2_initial_image_arch",
+ "trellis2_camera_count",
+ "trellis2_placement_mode",
+ "trellis2_auto_prompts",
+ "trellis2_exclude_bottom",
+ "trellis2_exclude_bottom_angle",
+ "trellis2_auto_aspect",
+ "trellis2_occlusion_mode",
+ "trellis2_consider_existing",
+ "trellis2_delete_cameras",
+ "trellis2_coverage_target",
+ "trellis2_max_auto_cameras",
+ "trellis2_fan_angle",
+ "trellis2_resolution",
+ "trellis2_vram_mode",
+ "trellis2_attn_backend",
+ "trellis2_seed",
+ "trellis2_ss_guidance",
+ "trellis2_ss_steps",
+ "trellis2_shape_guidance",
+ "trellis2_shape_steps",
+ "trellis2_tex_guidance",
+ "trellis2_tex_steps",
+ "trellis2_max_tokens",
+ "trellis2_texture_size",
+ "trellis2_decimation",
+ "trellis2_remesh",
+ "trellis2_post_processing_enabled",
+
+ "trellis2_bg_removal",
+ "trellis2_background_color",
+ "trellis2_import_scale",
+ "trellis2_clamp_elevation",
+ "trellis2_max_elevation",
+ "trellis2_min_elevation",
+ "trellis2_auto_lighting",
+
+ # --- General workflow settings (not prompt/seed/model/paths) ---
+ "use_ipadapter",
+ "sequential_ipadapter_regenerate_wo_controlnet",
+ "allow_modify_existing_textures",
+ "ask_object_prompts",
+ "weight_exponent_mask",
+ "mask_blocky",
+ "architecture_mode",
+ "use_camera_prompts",
+ "sg_use_custom_camera_order",
+ "generation_mode",
+ "texture_objects",
+ "use_flux_lora",
+ "qwen_use_trellis2_style",
+ "qwen_trellis2_style_initial_only",
+ "trellis2_skip_texture",
+ "pbr_decomposition",
+ "pbr_albedo_saturation_mode",
+]
+
+# ── Preset diff preview helpers ──────────────────────────────────────────
+
+
+def get_preset_items(self, context):
+ # Group presets by architecture for easier navigation.
+ _ARCH_GROUP_ORDER = [
+ ('sdxl', 'SDXL / FLUX.1'),
+ ('qwen_image_edit', 'Qwen Image Edit'),
+ ('flux2_klein', 'FLUX.2 Klein'),
+ ('trellis2', 'TRELLIS.2 Pipeline'),
+ ]
+ _known_groups = {k for k, _ in _ARCH_GROUP_ORDER}
+
+ # Classify each preset into its architecture group.
+ groups = {} # group_key -> list of (identifier, name, description)
+ for key, preset in PRESETS.items():
+ if preset.get('architecture_mode') == 'trellis2':
+ grp = 'trellis2'
+ else:
+ grp = preset.get('model_architecture', 'sdxl')
+ groups.setdefault(grp, []).append(
+ (key, key.title(), preset.get('description', f'Preset {key}'))
+ )
+
+ items = []
+ first_group = True
+ for grp_key, _label in _ARCH_GROUP_ORDER:
+ if grp_key not in groups:
+ continue
+ if not first_group:
+ items.append(('', '', '')) # horizontal separator
+ # Section header: empty identifier + label name renders as header
+ # in Blender 4.x enum popup menus.
+ items.append(('', _label, ''))
+ first_group = False
+ items.extend(groups[grp_key])
+
+ # Any architectures not explicitly listed above (future-proofing).
+ for grp_key, grp_items in groups.items():
+ if grp_key not in _known_groups:
+ items.append(('', '', ''))
+ items.append(('', grp_key.replace('_', ' ').title(), ''))
+ items.extend(grp_items)
+
+ items.append(('', '', '')) # separator before Custom
+ items.append(('CUSTOM', 'Custom', 'Custom configuration'))
+ return items
+
+def update_parameters(self, context):
+ scene = context.scene
+ # Build a dictionary of current parameter values
+ current = {key: getattr(scene, key) for key in GEN_PARAMETERS if hasattr(scene, key)}
+
+ # Compare current values with every stock preset's stored values
+ for name, preset in PRESETS.items():
+ # First check regular parameters
+ if all(
+ (lambda v1, v2: math.isclose(v1, v2, rel_tol=1e-7, abs_tol=0.0) if isinstance(v1, (int, float)) and isinstance(v2, (int, float)) else v1 == v2)
+ (current.get(key), preset.get(key))
+ for key in GEN_PARAMETERS if key in preset
+ ):
+ units_match = True
+
+ # Now check ControlNet units if present in the preset
+ if "controlnet_units" in preset:
+ # Get current controlnet units as comparable data
+ current_units = []
+ for unit in scene.controlnet_units:
+ unit_data = {
+ "unit_type": unit.unit_type,
+ "model_name": unit.model_name,
+ "strength": unit.strength,
+ "start_percent": unit.start_percent,
+ "end_percent": unit.end_percent,
+ "is_union": unit.is_union,
+ "use_union_type": unit.use_union_type
+ }
+
+ current_units.append(unit_data)
+
+ # Compare unit count
+ if len(current_units) != len(preset["controlnet_units"]):
+ continue # Different number of units, not a match
+
+ # Compare each unit's properties
+ units_match = True
+ for i, unit_data in enumerate(current_units):
+ preset_unit = preset["controlnet_units"][i]
+ for key, value in unit_data.items():
+ if key not in preset_unit or preset_unit[key] != value:
+ units_match = False
+ break
+ if not units_match:
+ break
+
+ if not units_match:
+ continue # ControlNet units don't match, try next preset
+
+ # Now check LoRA units if present in the preset
+ if "lora_units" in preset:
+ current_lora_units_data = []
+ for lora_unit_obj in scene.lora_units:
+ current_lora_units_data.append({
+ "model_name": lora_unit_obj.model_name,
+ "model_strength": round(lora_unit_obj.model_strength, 7),
+ "clip_strength": round(lora_unit_obj.clip_strength, 7),
+ })
+
+ preset_lora_units_data = preset["lora_units"]
+ if len(current_lora_units_data) != len(preset_lora_units_data):
+ continue # Different number of LoRA units
+
+ lora_units_match = True
+ for i, current_lora_unit_data in enumerate(current_lora_units_data):
+ preset_lora_unit_data = preset_lora_units_data[i]
+ for key, value in current_lora_unit_data.items():
+ if key not in preset_lora_unit_data or preset_lora_unit_data[key] != value:
+ lora_units_match = False
+ break
+ if not lora_units_match:
+ break
+
+ if not lora_units_match:
+ continue # LoRA units don't match, try next preset
+
+ # All parameters and ControlNet and LoRA units match
+ if scene.stablegen_preset != name:
+ scene.stablegen_preset = name
+ scene.active_preset = name
+ return
+
+ # No match found, set to custom
+ scene.active_preset = "CUSTOM"
+ scene.stablegen_preset = "CUSTOM"
+
+
+class ResetQwenPrompt(bpy.types.Operator):
+ """Resets a guidance prompt to its default value"""
+ bl_idname = "stablegen.reset_qwen_prompt"
+ bl_label = "Reset Qwen Prompt"
+ bl_description = "Reset this prompt to its default value based on the current settings"
+
+ prompt_type: bpy.props.StringProperty()
+
+ @classmethod
+ def poll(cls, context):
+ return not sg_modal_active(context)
+
+ def execute(self, context):
+ from ..workflows import WorkflowManager
+ # We need an operator instance to call the helper, a bit of a workaround
+ # This doesn't run generation, just gives us access to the method
+ wm = WorkflowManager(self)
+
+ # Choose the correct default prompt builder for the active architecture
+ is_klein = getattr(context.scene, 'model_architecture', '') == 'flux2_klein'
+ get_defaults = wm._get_klein_default_prompts if is_klein else wm._get_qwen_default_prompts
+
+ # Determine which prompt to reset
+ if self.prompt_type == 'initial':
+ default_prompt = get_defaults(context, is_initial_image=True)
+ context.scene.qwen_custom_prompt_initial = default_prompt
+ elif self.prompt_type == 'seq_none':
+ default_prompt = get_defaults(context, is_initial_image=False)
+ context.scene.qwen_custom_prompt_seq_none = default_prompt
+ elif self.prompt_type == 'seq_replace':
+ default_prompt = get_defaults(context, is_initial_image=False)
+ context.scene.qwen_custom_prompt_seq_replace = default_prompt
+ elif self.prompt_type == 'seq_additional':
+ default_prompt = get_defaults(context, is_initial_image=False)
+ context.scene.qwen_custom_prompt_seq_additional = default_prompt
+
+ self.report({'INFO'}, "Prompt reset to default.")
+ return {'FINISHED'}
+
+class ApplyPreset(bpy.types.Operator):
+ """Apply selected preset values to parameters"""
+ bl_idname = "stablegen.apply_preset"
+ bl_label = "Apply Preset"
+ bl_description = "Set multiple parameters based on selected preset for easier configuration"
+
+ @classmethod
+ def poll(cls, context):
+ return not sg_modal_active(context)
+
+ def execute(self, context):
+ preset = context.scene.stablegen_preset
+ if preset in PRESETS:
+ values = PRESETS[preset]
+
+ # Apply architecture_mode first so dynamic enums that
+ # depend on it (e.g. sequential_ipadapter_mode) are valid
+ # when their values are set in the main loop.
+ if "architecture_mode" in values and hasattr(context.scene, "architecture_mode"):
+ try:
+ setattr(context.scene, "architecture_mode", values["architecture_mode"])
+ except (TypeError, AttributeError):
+ pass
+
+ # Apply regular parameters
+ skipped = []
+ for key, value in values.items():
+ if key not in ["controlnet_units", "lora_units", "description"] and hasattr(context.scene, key):
+ try:
+ setattr(context.scene, key, value)
+ except (TypeError, AttributeError):
+ # Dynamic enum values (e.g. trellis2_input) may not
+ # exist when the current architecture differs.
+ skipped.append(key)
+
+ # Apply ControlNet units if present in the preset
+ if "controlnet_units" in values:
+ # Clear existing units
+ context.scene.controlnet_units.clear()
+
+ # Add new units from preset
+ controlnet_units = values["controlnet_units"]
+ for unit_data in controlnet_units:
+ new_unit = context.scene.controlnet_units.add()
+ for key, value in unit_data.items():
+ try:
+ setattr(new_unit, key, value)
+ except TypeError:
+ self.report({'ERROR'}, f"Failed to set {key} for ControlNet unit: {value}. Model might be missing or might not be named correctly.")
+ return {'CANCELLED'}
+
+ if "lora_units" in values:
+ # Clear existing LoRA units
+ context.scene.lora_units.clear()
+
+ # Add new LoRA units from preset
+ lora_units = values["lora_units"]
+ for lora_data in lora_units:
+ new_lora = context.scene.lora_units.add()
+ for key, value in lora_data.items():
+ try:
+ setattr(new_lora, key, value)
+ except TypeError:
+ self.report({'ERROR'}, f"Failed to set {key} for LoRA unit: {value}. Model might be missing or might not be named correctly.")
+ context.scene.lora_units.remove(len(context.scene.lora_units) - 1)
+ return {'CANCELLED'}
+
+ # Reverse-sync: if the preset set model_architecture but didn't
+ # include architecture_mode, update the visible dropdown to match.
+ if "architecture_mode" not in values:
+ arch = context.scene.model_architecture
+ if arch in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein'):
+ context.scene.architecture_mode = arch
+
+ if skipped:
+ self.report({'WARNING'}, f"Preset '{preset}' applied (skipped {len(skipped)} incompatible setting(s)).")
+ else:
+ self.report({'INFO'}, f"Preset '{preset}' applied.")
+ else:
+ self.report({'INFO'}, "Custom preset active.")
+
+ # Force update to ensure preset detection is correct after list changes
+ update_parameters(self, context)
+
+ return {'FINISHED'}
+
+class SwitchToMeshGeneration(bpy.types.Operator):
+ """Switch to mesh generation mode using TRELLIS.2"""
+ bl_idname = "stablegen.switch_to_mesh_generation"
+ bl_label = "Switch to Mesh Generation"
+ bl_description = "Apply the TRELLIS.2 (Mesh Only) preset to generate 3D meshes"
+
+ @classmethod
+ def poll(cls, context):
+ return (not sg_modal_active(context)
+ and "TRELLIS.2 (MESH ONLY)" in PRESETS
+ and getattr(context.scene, 'trellis2_available', False))
+
+ def execute(self, context):
+ context.scene.stablegen_preset = "TRELLIS.2 (MESH ONLY)"
+ bpy.ops.stablegen.apply_preset()
+ return {'FINISHED'}
+
+
+class SavePreset(bpy.types.Operator):
+ """Save the current parameter values as a custom preset"""
+ bl_idname = "stablegen.save_preset"
+ bl_label = "Save Custom Preset"
+
+ @classmethod
+ def poll(cls, context):
+ return not sg_modal_active(context)
+
+ preset_name: bpy.props.StringProperty(
+ name="Preset Name",
+ default="MyPreset"
+ ) # type: ignore
+
+ preset_description: bpy.props.StringProperty(
+ name="Description",
+ description="A short description of what this preset is good for",
+ default=""
+ ) # type: ignore
+
+ include_controlnet: bpy.props.BoolProperty(
+ name="Include ControlNet Units",
+ default=True,
+ description="Include ControlNet units in the preset"
+ ) # type: ignore
+
+ include_loras: bpy.props.BoolProperty(
+ name="Include LoRA Units",
+ default=True
+ ) # type: ignore
+
+ def execute(self, context):
+ scene = context.scene
+ key = self.preset_name.upper()
+
+ # Save all parameters defined in GEN_PARAMETERS
+ PRESETS[key] = {param: getattr(scene, param) for param in GEN_PARAMETERS if hasattr(scene, param)}
+
+ # Add description
+ PRESETS[key]["description"] = self.preset_description
+
+ # Add custom flag
+ PRESETS[key]["custom"] = True
+
+ if self.include_controlnet:
+ # Save ControlNet units
+ controlnet_units = []
+ for unit in scene.controlnet_units:
+ unit_data = {
+ "unit_type": unit.unit_type,
+ "model_name": unit.model_name,
+ "strength": unit.strength,
+ "start_percent": unit.start_percent,
+ "end_percent": unit.end_percent,
+ "is_union": unit.is_union,
+ "use_union_type": unit.use_union_type
+ }
+ controlnet_units.append(unit_data)
+
+ # Add controlnet units to the preset
+ PRESETS[key]["controlnet_units"] = controlnet_units
+
+ if self.include_loras: # Save LoRA units
+ lora_units_data = []
+ for lora_unit in scene.lora_units:
+ lora_units_data.append({
+ "model_name": lora_unit.model_name,
+ "model_strength": lora_unit.model_strength,
+ "clip_strength": lora_unit.clip_strength,
+ })
+
+ # Add LoRA units to the preset
+ PRESETS[key]["lora_units"] = lora_units_data
+
+ scene.stablegen_preset = key
+ scene.active_preset = key
+ self.report({'INFO'}, f"Preset '{self.preset_name}' saved.")
+
+ # Print in the console for debugging
+ print(f'"{key}": {{', end="")
+ print(f'"description": "{self.preset_description}", ', end="")
+ for param in GEN_PARAMETERS:
+ if hasattr(scene, param):
+ value = getattr(scene, param)
+ if isinstance(value, str):
+ print(f'"{param}": "{value}", ', end="")
+ else:
+ print(f'"{param}": {value}, ', end="")
+
+ # Print controlnet units in a compact format if included
+ 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):
+ return context.window_manager.invoke_props_dialog(self)
+
+ def draw(self, context):
+ layout = self.layout
+ layout.prop(self, "preset_name")
+ layout.prop(self, "preset_description")
+ layout.prop(self, "include_controlnet")
+ layout.prop(self, "include_loras")
+
+class DeletePreset(bpy.types.Operator):
+ """Delete a custom preset"""
+ bl_idname = "stablegen.delete_preset"
+ bl_label = "Delete Preset"
+
+ @classmethod
+ def poll(cls, context):
+ return not sg_modal_active(context)
+
+ def execute(self, context):
+ preset = context.scene.stablegen_preset
+ if preset in PRESETS:
+ del PRESETS[preset]
+ context.scene.stablegen_preset = "CUSTOM"
+ self.report({'INFO'}, f"Preset '{preset}' deleted.")
+ update_parameters(self, context)
+ return {'FINISHED'}
+ else:
+ self.report({'WARNING'}, "Preset not found.")
+ return {'CANCELLED'}
+
+
+# =====================================================================
+# Scene Queue System
+# =====================================================================
+# Allows users to queue multiple Blender scenes for unattended batch
+# processing. Each queued scene is processed in order — the manager
+# switches to the scene, invokes the appropriate generation operator
+# (TRELLIS.2 or standard texturing), waits for completion, optionally
+# saves a .blend copy, then advances to the next item.
diff --git a/stablegen/ui/queue.py b/stablegen/ui/queue.py
new file mode 100644
index 0000000..b50e96c
--- /dev/null
+++ b/stablegen/ui/queue.py
@@ -0,0 +1,1212 @@
+"""Scene-queue persistence and the SceneQueueItem PropertyGroup.
+
+Contains:
+- ``SceneQueueItem`` — PropertyGroup for a single queue entry
+- ``_sg_queue_filepath`` / ``_sg_queue_save`` / ``_sg_queue_load``
+- ``_sg_queue_load_handler`` — ``@persistent`` load_post handler
+"""
+
+import json
+import os
+
+import bpy # pylint: disable=import-error
+from bpy.app.handlers import persistent
+
+from ..core import ADDON_PKG
+from ..utils import sg_modal_active
+
+
+# ── Constants ──────────────────────────────────────────────────────────────
+
+_SG_QUEUE_FILENAME = "sg_scene_queue.json"
+
+
+# ── Filepath helper ────────────────────────────────────────────────────────
+
+def _sg_queue_filepath():
+ """Return the path to the queue JSON file.
+
+ Uses the addon output_dir if set; otherwise falls back to a
+ ``.stablegen`` folder next to the addon package.
+ """
+ try:
+ prefs = bpy.context.preferences.addons.get(ADDON_PKG)
+ if prefs and prefs.preferences.output_dir:
+ d = bpy.path.abspath(prefs.preferences.output_dir)
+ if os.path.isdir(d):
+ return os.path.join(d, _SG_QUEUE_FILENAME)
+ except Exception:
+ pass
+ # Fallback: <addon_dir>/.stablegen/
+ fallback = os.path.join(os.path.dirname(os.path.dirname(__file__)), ".stablegen")
+ os.makedirs(fallback, exist_ok=True)
+ return os.path.join(fallback, _SG_QUEUE_FILENAME)
+
+
+# ── Save / Load ────────────────────────────────────────────────────────────
+
+def _sg_queue_save(processing=False, current_idx=0, phase='idle'):
+ """Serialize the scene queue + processing state to a JSON file on disk."""
+ wm = bpy.context.window_manager
+ if not hasattr(wm, 'sg_scene_queue'):
+ return
+ items = [{"label": it.label,
+ "scene_name": it.scene_name,
+ "blend_file": it.blend_file,
+ "prompt": it.prompt,
+ "negative_prompt": it.negative_prompt,
+ "status": it.status,
+ "retries": it.retries,
+ "error_reason": it.error_reason}
+ for it in wm.sg_scene_queue]
+
+ gif_settings = {}
+ _GIF_KEYS = (
+ 'sg_queue_gif_export', 'sg_queue_gif_duration', 'sg_queue_gif_fps',
+ 'sg_queue_gif_resolution', 'sg_queue_gif_samples', 'sg_queue_gif_engine',
+ 'sg_queue_gif_interpolation', 'sg_queue_gif_use_hdri',
+ 'sg_queue_gif_hdri_path', 'sg_queue_gif_hdri_strength',
+ 'sg_queue_gif_hdri_rotation', 'sg_queue_gif_env_mode',
+ 'sg_queue_gif_denoiser', 'sg_queue_gif_use_gpu',
+ 'sg_queue_gif_also_no_pbr',
+ )
+ for key in _GIF_KEYS:
+ if hasattr(wm, key):
+ gif_settings[key] = getattr(wm, key)
+
+ data = {
+ "items": items,
+ "processing": processing,
+ "current_idx": current_idx,
+ "phase": phase,
+ "gif_settings": gif_settings,
+ }
+ try:
+ fp = _sg_queue_filepath()
+ with open(fp, 'w', encoding='utf-8') as f:
+ json.dump(data, f)
+ except Exception as e:
+ print(f"[Queue] save error: {e}")
+
+
+def _sg_queue_load():
+ """Restore the scene queue from the JSON file on disk."""
+ wm = bpy.context.window_manager
+ if not hasattr(wm, 'sg_scene_queue'):
+ return
+ fp = _sg_queue_filepath()
+ if not os.path.isfile(fp):
+ return
+ try:
+ with open(fp, 'r', encoding='utf-8') as f:
+ raw = json.load(f)
+
+ if isinstance(raw, list):
+ items = raw
+ meta = {}
+ else:
+ items = raw.get("items", [])
+ meta = raw
+
+ wm.sg_scene_queue.clear()
+ for entry in items:
+ new_item = wm.sg_scene_queue.add()
+ new_item.label = entry.get("label", "")
+ new_item.scene_name = entry.get("scene_name", "")
+ new_item.blend_file = entry.get("blend_file", "")
+ new_item.prompt = entry.get("prompt", "")
+ new_item.negative_prompt = entry.get("negative_prompt", "")
+ new_item.status = entry.get("status", "pending")
+ new_item.retries = entry.get("retries", 0)
+ new_item.error_reason = entry.get("error_reason", "")
+ wm.sg_scene_queue_index = min(0, len(wm.sg_scene_queue) - 1)
+
+ gif_settings = meta.get("gif_settings", {})
+ for key, value in gif_settings.items():
+ if hasattr(wm, key):
+ try:
+ setattr(wm, key, value)
+ except Exception:
+ pass
+
+ if meta.get("processing", False):
+ try:
+ idx = meta.get("current_idx", 0)
+ _resume_queue(idx)
+ print(f"[Queue] Auto-resuming from item {idx}")
+ except Exception as e:
+ print(f"[Queue] Failed to auto-resume: {e}")
+ except Exception as e:
+ print(f"[Queue] Failed to restore queue: {e}")
+
+
+# ── Load-post handler ─────────────────────────────────────────────────────
+
+@persistent
+def _sg_queue_load_handler(dummy):
+ """load_post handler: restore the queue when a .blend is opened."""
+ def _deferred():
+ _sg_queue_load()
+ return None
+ bpy.app.timers.register(_deferred, first_interval=0.3)
+
+
+# ── PropertyGroup ──────────────────────────────────────────────────────────
+
+class SceneQueueItem(bpy.types.PropertyGroup):
+ """A single item in the scene generation queue."""
+ label: bpy.props.StringProperty(name="Label", default="") # type: ignore
+ scene_name: bpy.props.StringProperty(name="Scene", default="") # type: ignore
+ blend_file: bpy.props.StringProperty(name="Blend File", default="") # type: ignore
+ prompt: bpy.props.StringProperty(name="Prompt", default="") # type: ignore
+ negative_prompt: bpy.props.StringProperty(name="Negative Prompt", default="") # type: ignore
+ status: bpy.props.StringProperty(name="Status", default="pending") # type: ignore
+ retries: bpy.props.IntProperty(name="Retries", default=0) # type: ignore
+ error_reason: bpy.props.StringProperty(name="Error Reason", default="") # type: ignore
+
+
+# =====================================================================
+# Queue operators and timer (extracted from stablegen.py)
+# =====================================================================
+
+_queue_timer = None # Reference to the active timer callback
+_queue_processing = False # Global flag: queue is being processed
+_queue_current_idx = 0 # Index of the currently-processing item
+_queue_phase = 'idle' # 'idle' | 'switching' | 'running' | 'settling' | 'waiting_texturing' | 'exporting_gif'
+_queue_settle_deadline = 0.0 # monotonic time after which 'settling' may finish
+_queue_force_reload = False # Force re-open of the .blend on next idle tick (for retries)
+_queue_texturing_seen = False # Set True once generation_status='running' is observed during settling
+_queue_refresh_deadline = 0.0 # monotonic deadline while waiting for model-list refresh
+_queue_original_pbr = False # stored pbr_decomposition before no-PBR reproject
+_queue_original_gen_method = 'sequential' # stored generation_method before reproject
+_queue_original_overwrite = True # stored overwrite_material before reproject
+_QUEUE_MAX_RETRIES = 3 # how many times a failed item is retried
+
+
+class SG_UL_SceneQueueList(bpy.types.UIList):
+ """UIList for the scene generation queue."""
+ bl_idname = "SG_UL_SceneQueueList"
+
+ def draw_item(self, context, layout, data, item, icon, active_data, active_property, index):
+ if self.layout_type in {'DEFAULT', 'COMPACT'}:
+ row = layout.row(align=True)
+
+ # Label (or scene name as fallback)
+ display = item.label if item.label else item.scene_name
+ row.label(text=display, icon='SCENE_DATA')
+
+ # Status icon + retry indicator
+ if item.status == 'done':
+ row.label(text="", icon='CHECKMARK')
+ elif item.status == 'error':
+ retry_txt = f"x{item.retries}" if item.retries else ""
+ if item.error_reason:
+ reason_short = item.error_reason[:28] + "…" if len(item.error_reason) > 30 else item.error_reason
+ row.label(text=f"{reason_short} {retry_txt}".strip(), icon='ERROR')
+ else:
+ row.label(text=retry_txt, icon='ERROR')
+ elif item.status == 'processing':
+ retry_txt = f"#{item.retries + 1}" if item.retries else ""
+ row.label(text=retry_txt, icon='SORTTIME')
+ elif self.layout_type == 'GRID':
+ layout.alignment = 'CENTER'
+ display = item.label if item.label else item.scene_name
+ layout.label(text=display, icon='SCENE_DATA')
+
+
+def _persist_queue():
+ """Write the queue + processing state to a JSON file on disk."""
+ try:
+ _sg_queue_save(
+ processing=_queue_processing,
+ current_idx=_queue_current_idx,
+ phase=_queue_phase,
+ )
+ except Exception:
+ pass
+
+
+def _resume_queue(idx):
+ """Resume queue processing from *idx* (called after a .blend switch).
+
+ This is invoked by ``_sg_queue_load()`` in ``__init__.py`` when the
+ JSON file indicates that processing was active.
+ """
+ global _queue_processing, _queue_current_idx, _queue_phase, _queue_timer
+
+ _queue_processing = True
+ _queue_current_idx = idx
+ _queue_phase = 'idle'
+
+ # Avoid duplicate timers
+ if _queue_timer is not None:
+ try:
+ bpy.app.timers.unregister(_queue_tick)
+ except Exception:
+ pass
+
+ _queue_timer = bpy.app.timers.register(_queue_tick, first_interval=1.5)
+ print(f"[Queue] Resumed processing from item {idx}")
+ _tag_redraw()
+
+
+class SceneQueueAdd(bpy.types.Operator):
+ """Add the current scene to the generation queue.
+
+ Saves a *copy* of the current .blend into ``<output_dir>/queue_jobs/``.
+ The queue item points at that copy so the original file is never
+ modified by queue processing.
+ """
+ bl_idname = "stablegen.queue_add"
+ bl_label = "Add to Queue"
+ bl_description = "Snapshot the current .blend and add the scene to the batch queue"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ item_label: bpy.props.StringProperty(
+ name="Name",
+ description="A short label for this queue entry",
+ default="",
+ ) # type: ignore
+
+ @classmethod
+ def poll(cls, context):
+ return not _queue_processing and not sg_modal_active(context)
+
+ def invoke(self, context, event):
+ scene = context.scene
+ cur_prompt = getattr(scene, 'comfyui_prompt', '')
+ # Pre-fill with a truncated prompt
+ self.item_label = (cur_prompt[:40]) if cur_prompt else scene.name
+ return context.window_manager.invoke_props_dialog(self, width=350)
+
+ def draw(self, context):
+ layout = self.layout
+ layout.prop(self, "item_label")
+
+ def execute(self, context):
+ import time
+
+ wm = context.window_manager
+ scene = context.scene
+ scene_name = scene.name
+ cur_prompt = getattr(scene, 'comfyui_prompt', '')
+ cur_neg = getattr(scene, 'comfyui_negative_prompt', '')
+ label = self.item_label.strip() or scene_name
+
+ # Determine queue_jobs directory
+ prefs = context.preferences.addons.get(ADDON_PKG)
+ output_dir = ''
+ if prefs:
+ output_dir = bpy.path.abspath(prefs.preferences.output_dir or '')
+ if not output_dir or not os.path.isdir(output_dir):
+ self.report({'ERROR'}, "Set an Output Directory in StableGen preferences first.")
+ return {'CANCELLED'}
+
+ jobs_dir = os.path.join(output_dir, "queue_jobs")
+ os.makedirs(jobs_dir, exist_ok=True)
+
+ # Build filename from the user-provided label
+ timestamp = time.strftime("%Y%m%d_%H%M%S")
+ safe_label = label.replace(" ", "_").replace("/", "_").replace("\\", "_")
+ # Truncate to keep filenames reasonable
+ safe_label = safe_label[:60]
+ copy_name = f"{safe_label}_{timestamp}.blend"
+ copy_path = os.path.join(jobs_dir, copy_name)
+
+ # Save a copy — current file stays active & untouched
+ # Snapshot the model_name into the plain-string backup property
+ # BEFORE saving so the copy carries the correct value even if
+ # the dynamic Enum resolves wrong on re-open (stale item cache).
+ scene.sg_model_name_backup = scene.model_name
+ try:
+ bpy.ops.wm.save_as_mainfile(filepath=copy_path, copy=True)
+ except Exception as e:
+ self.report({'ERROR'}, f"Failed to save queue snapshot: {e}")
+ return {'CANCELLED'}
+
+ new_item = wm.sg_scene_queue.add()
+ new_item.label = label
+ new_item.scene_name = scene_name
+ new_item.blend_file = copy_path
+ new_item.prompt = cur_prompt
+ new_item.negative_prompt = cur_neg
+ new_item.status = 'pending'
+ wm.sg_scene_queue_index = len(wm.sg_scene_queue) - 1
+ _persist_queue()
+
+ self.report({'INFO'}, f"Queued '{label}' ({copy_name})")
+ return {'FINISHED'}
+
+
+class SceneQueueRemove(bpy.types.Operator):
+ """Remove the selected scene from the queue"""
+ bl_idname = "stablegen.queue_remove"
+ bl_label = "Remove from Queue"
+
+ @classmethod
+ def poll(cls, context):
+ wm = context.window_manager
+ return not _queue_processing and len(wm.sg_scene_queue) > 0 and not sg_modal_active(context)
+
+ def invoke(self, context, event):
+ wm = context.window_manager
+ idx = wm.sg_scene_queue_index
+ if 0 <= idx < len(wm.sg_scene_queue):
+ item = wm.sg_scene_queue[idx]
+ display = item.label or item.scene_name
+ # Show a confirmation popup
+ return context.window_manager.invoke_confirm(self, event,
+ message=f"Remove '{display}' from queue?",
+ confirm_text="Remove")
+ return {'CANCELLED'}
+
+ def execute(self, context):
+ wm = context.window_manager
+ idx = wm.sg_scene_queue_index
+ if 0 <= idx < len(wm.sg_scene_queue):
+ item = wm.sg_scene_queue[idx]
+ display = item.label or item.scene_name
+ # Delete the snapshot .blend if the item hasn't been processed
+ if item.status != 'done' and item.blend_file:
+ try:
+ blend_path = item.blend_file
+ if os.path.isfile(blend_path):
+ os.remove(blend_path)
+ print(f"[Queue] Deleted snapshot: {blend_path}")
+ except Exception as e:
+ print(f"[Queue] Could not delete snapshot: {e}")
+ wm.sg_scene_queue.remove(idx)
+ wm.sg_scene_queue_index = min(idx, len(wm.sg_scene_queue) - 1)
+ _persist_queue()
+ self.report({'INFO'}, f"Removed '{display}' from queue.")
+ return {'FINISHED'}
+
+
+class SceneQueueClear(bpy.types.Operator):
+ """Clear the entire queue"""
+ bl_idname = "stablegen.queue_clear"
+ bl_label = "Clear Queue"
+
+ @classmethod
+ def poll(cls, context):
+ wm = context.window_manager
+ return not _queue_processing and len(wm.sg_scene_queue) > 0 and not sg_modal_active(context)
+
+ def invoke(self, context, event):
+ wm = context.window_manager
+ count = len(wm.sg_scene_queue)
+ return context.window_manager.invoke_confirm(self, event,
+ message=f"Clear all {count} item(s) from queue?",
+ confirm_text="Clear All")
+
+ def execute(self, context):
+ wm = context.window_manager
+ # Delete snapshot .blends for unprocessed items
+ for item in wm.sg_scene_queue:
+ if item.status != 'done' and item.blend_file:
+ try:
+ if os.path.isfile(item.blend_file):
+ os.remove(item.blend_file)
+ print(f"[Queue] Deleted snapshot: {item.blend_file}")
+ except Exception as e:
+ print(f"[Queue] Could not delete snapshot: {e}")
+ wm.sg_scene_queue.clear()
+ wm.sg_scene_queue_index = 0
+ _persist_queue()
+ self.report({'INFO'}, "Queue cleared.")
+ return {'FINISHED'}
+
+
+class SceneQueueMoveUp(bpy.types.Operator):
+ """Move the selected queue item up"""
+ bl_idname = "stablegen.queue_move_up"
+ bl_label = "Move Up"
+
+ @classmethod
+ def poll(cls, context):
+ wm = context.window_manager
+ return not _queue_processing and wm.sg_scene_queue_index > 0 and not sg_modal_active(context)
+
+ def execute(self, context):
+ wm = context.window_manager
+ idx = wm.sg_scene_queue_index
+ wm.sg_scene_queue.move(idx, idx - 1)
+ wm.sg_scene_queue_index -= 1
+ return {'FINISHED'}
+
+
+class SceneQueueMoveDown(bpy.types.Operator):
+ """Move the selected queue item down"""
+ bl_idname = "stablegen.queue_move_down"
+ bl_label = "Move Down"
+
+ @classmethod
+ def poll(cls, context):
+ wm = context.window_manager
+ return (not _queue_processing
+ and wm.sg_scene_queue_index < len(wm.sg_scene_queue) - 1
+ and not sg_modal_active(context))
+
+ def execute(self, context):
+ wm = context.window_manager
+ idx = wm.sg_scene_queue_index
+ wm.sg_scene_queue.move(idx, idx + 1)
+ wm.sg_scene_queue_index += 1
+ return {'FINISHED'}
+
+
+class SceneQueueOpenResult(bpy.types.Operator):
+ """Open the .blend snapshot for the selected queue item"""
+ bl_idname = "stablegen.queue_open_result"
+ bl_label = "Open"
+ bl_description = "Open the .blend snapshot of the selected queue item"
+
+ @classmethod
+ def poll(cls, context):
+ if sg_modal_active(context):
+ return False
+ wm = context.window_manager
+ idx = wm.sg_scene_queue_index
+ if idx < 0 or idx >= len(wm.sg_scene_queue):
+ return False
+ item = wm.sg_scene_queue[idx]
+ return bool(item.blend_file) and os.path.isfile(item.blend_file)
+
+ def execute(self, context):
+ wm = context.window_manager
+ item = wm.sg_scene_queue[wm.sg_scene_queue_index]
+ filepath = item.blend_file
+ try:
+ bpy.ops.wm.open_mainfile(filepath=filepath)
+ except Exception as e:
+ self.report({'ERROR'}, f"Failed to open file: {e}")
+ return {'CANCELLED'}
+ return {'FINISHED'}
+
+
+class SceneQueueInvalidate(bpy.types.Operator):
+ """Reset a processed queue item back to pending and clear its scene"""
+ bl_idname = "stablegen.queue_invalidate"
+ bl_label = "Reset to Pending"
+ bl_description = (
+ "Reset the selected item to unprocessed, "
+ "removing generated meshes and cameras from its snapshot "
+ "so it can be re-processed"
+ )
+ bl_options = {'REGISTER', 'UNDO'}
+
+ @classmethod
+ def poll(cls, context):
+ wm = context.window_manager
+ idx = wm.sg_scene_queue_index
+ if _queue_processing:
+ cls.poll_message_set("Queue is currently processing")
+ return False
+ if sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ if idx < 0 or idx >= len(wm.sg_scene_queue):
+ cls.poll_message_set("No queue item selected")
+ return False
+ item = wm.sg_scene_queue[idx]
+ if item.status not in ('done', 'error'):
+ cls.poll_message_set("Only completed or failed items can be reset")
+ return False
+ if not item.blend_file or not os.path.isfile(item.blend_file):
+ cls.poll_message_set("Snapshot .blend file not found")
+ return False
+ return True
+
+ def invoke(self, context, event):
+ wm = context.window_manager
+ idx = wm.sg_scene_queue_index
+ item = wm.sg_scene_queue[idx]
+ display = item.label or item.scene_name
+ return context.window_manager.invoke_confirm(
+ self, event,
+ message=f"Reset '{display}'?\nThis will remove generated meshes and cameras from its snapshot and mark it as unprocessed.",
+ confirm_text="Reset",
+ )
+
+ def execute(self, context):
+ wm = context.window_manager
+ idx = wm.sg_scene_queue_index
+ item = wm.sg_scene_queue[idx]
+ blend_path = item.blend_file
+ display = item.label or item.scene_name
+
+ # Open the processed .blend, clean it, save, then re-open current
+ current_file = bpy.data.filepath
+
+ try:
+ bpy.ops.wm.open_mainfile(filepath=blend_path)
+ except Exception as e:
+ self.report({'ERROR'}, f"Failed to open snapshot: {e}")
+ return {'CANCELLED'}
+
+ # Remove all mesh objects and cameras from the scene
+ scene = bpy.context.scene
+ to_remove = [obj for obj in scene.objects if obj.type in {'MESH', 'CAMERA', 'EMPTY'}]
+ for obj in to_remove:
+ bpy.data.objects.remove(obj, do_unlink=True)
+
+ # Purge orphaned data
+ for block_attr in ('meshes', 'cameras', 'materials', 'images'):
+ collection = getattr(bpy.data, block_attr, None)
+ if collection is None:
+ continue
+ orphans = [b for b in collection if b.users == 0]
+ for orphan in orphans:
+ collection.remove(orphan)
+
+ # Save the cleaned .blend in place
+ try:
+ bpy.ops.wm.save_mainfile()
+ print(f"[Queue] Invalidated and saved: {blend_path}")
+ except Exception as e:
+ print(f"[Queue] Save warning during invalidate: {e}")
+
+ # Mark the item as pending directly in the queue JSON file.
+ # After open_mainfile the in-memory queue collection is empty
+ # (it's restored 0.3 s later by the deferred load handler), so
+ # patching the JSON is the only reliable approach.
+ try:
+ fp = _sg_queue_filepath()
+ if os.path.isfile(fp):
+ import json as _json
+ with open(fp, 'r', encoding='utf-8') as f:
+ data = _json.load(f)
+ items_list = data.get("items", data) if isinstance(data, dict) else data
+ if isinstance(items_list, list) and idx < len(items_list):
+ items_list[idx]["status"] = "pending"
+ items_list[idx]["retries"] = 0
+ items_list[idx]["error_reason"] = ""
+ with open(fp, 'w', encoding='utf-8') as f:
+ _json.dump(data, f)
+ print(f"[Queue] Marked item {idx} as pending in queue JSON")
+ except Exception as e:
+ print(f"[Queue] Warning: could not update queue JSON: {e}")
+
+ # Re-open the original file (if we had one)
+ if current_file and os.path.isfile(current_file):
+ try:
+ bpy.ops.wm.open_mainfile(filepath=current_file)
+ except Exception:
+ pass
+
+ self.report({'INFO'}, f"Reset '{display}' — ready for re-processing")
+ return {'FINISHED'}
+
+
+class SceneQueueProcess(bpy.types.Operator):
+ """Start or cancel batch processing of the scene queue"""
+ bl_idname = "stablegen.queue_process"
+ bl_label = "Process Queue"
+ bl_description = "Process all queued scenes sequentially"
+
+ @classmethod
+ def poll(cls, context):
+ wm = context.window_manager
+ if _queue_processing:
+ return True # Allow clicking to cancel
+ if len(wm.sg_scene_queue) == 0:
+ cls.poll_message_set("Queue is empty — add scenes first")
+ return False
+ if sg_modal_active(context):
+ cls.poll_message_set("Another operation is in progress")
+ return False
+ return True
+
+ def execute(self, context):
+ global _queue_processing, _queue_timer, _queue_current_idx, _queue_phase
+
+ if _queue_processing:
+ # Cancel
+ _queue_processing = False
+ _queue_phase = 'idle'
+ _persist_queue() # Save stopped state so load_post won't auto-resume
+ print("[Queue] Cancelled by user.")
+ self.report({'WARNING'}, "Queue processing cancelled.")
+ return {'FINISHED'}
+
+ # Reset statuses
+ wm = context.window_manager
+ for item in wm.sg_scene_queue:
+ if item.status != 'done':
+ item.status = 'pending'
+
+ _queue_processing = True
+ _queue_current_idx = 0
+ _queue_phase = 'idle'
+
+ # Find first pending item
+ while _queue_current_idx < len(wm.sg_scene_queue):
+ if wm.sg_scene_queue[_queue_current_idx].status == 'pending':
+ break
+ _queue_current_idx += 1
+
+ if _queue_current_idx >= len(wm.sg_scene_queue):
+ _queue_processing = False
+ self.report({'INFO'}, "No pending items in queue.")
+ return {'FINISHED'}
+
+ # Start the timer-based queue driver
+ _queue_timer = bpy.app.timers.register(_queue_tick, first_interval=0.5)
+ self.report({'INFO'}, f"Queue processing started ({len(wm.sg_scene_queue)} items).")
+ return {'FINISHED'}
+
+
+def _queue_tick():
+ """Timer callback that drives queue advancement.
+
+ Returns:
+ float: Seconds until next tick, or None to stop.
+ """
+ import time as _time
+ global _queue_processing, _queue_current_idx, _queue_phase, _queue_timer
+ global _queue_settle_deadline, _queue_force_reload, _queue_texturing_seen
+ global _queue_refresh_deadline
+
+ if not _queue_processing:
+ _queue_phase = 'idle'
+ _queue_timer = None
+ _tag_redraw()
+ return None # Stop timer
+
+ wm = bpy.context.window_manager
+ queue = wm.sg_scene_queue
+
+ if _queue_current_idx >= len(queue):
+ # All done
+ _queue_processing = False
+ _queue_phase = 'idle'
+ _queue_timer = None
+ print("[Queue] All items processed.")
+ _tag_redraw()
+ return None
+
+ item = queue[_queue_current_idx]
+
+ # ── Phase: IDLE → switch to the next scene and start ──
+ if _queue_phase == 'idle':
+ # Verify the item belongs to the currently-open .blend
+ current_blend = bpy.data.filepath or "<unsaved>"
+ need_open = (item.blend_file and item.blend_file != current_blend)
+
+ # On retry, always re-open the .blend to restore clean state
+ is_retry_reload = False
+ if _queue_force_reload and item.blend_file:
+ need_open = True
+ is_retry_reload = True
+ _queue_force_reload = False
+
+ if need_open:
+ # Need to open a different .blend file
+ target = item.blend_file
+ if not os.path.isfile(target):
+ print(f"[Queue] Blend file not found: {target} — skipping.")
+ item.status = 'error'
+ item.error_reason = "Blend file not found"
+ _queue_current_idx += 1
+ _persist_queue()
+ _tag_redraw()
+ return 0.5
+
+ # Save current file first so nothing is lost — BUT skip save
+ # on retry reloads; we want to discard dirty state and revert
+ # to the original clean copy.
+ if bpy.data.filepath and not is_retry_reload:
+ try:
+ bpy.ops.wm.save_mainfile()
+ except Exception as e:
+ print(f"[Queue] Warning: could not save current file: {e}")
+
+ # Persist state so the load_post handler can resume after
+ # the new file is opened.
+ _persist_queue()
+ print(f"[Queue] Opening '{os.path.basename(target)}' for item "
+ f"'{item.scene_name}'...")
+
+ # Open the target file. After this call the current WM
+ # data (and our timer) are gone — the load_post handler
+ # will restore the queue and call _resume_queue().
+ try:
+ bpy.ops.wm.open_mainfile(filepath=target)
+ except Exception as e:
+ print(f"[Queue] Failed to open '{target}': {e}")
+ # Can't recover gracefully — stop processing
+ _queue_processing = False
+ _queue_phase = 'idle'
+ _queue_timer = None
+ _persist_queue()
+ _tag_redraw()
+ return None # Stop timer — load_post will restart it
+
+ scene_ref = bpy.data.scenes.get(item.scene_name)
+ if not scene_ref:
+ print(f"[Queue] Scene '{item.scene_name}' not found — skipping.")
+ item.status = 'error'
+ item.error_reason = "Scene not found"
+ _queue_current_idx += 1
+ _tag_redraw()
+ return 0.5 # Try next immediately
+
+ # Switch to the target scene
+ bpy.context.window.scene = scene_ref
+ item.status = 'processing'
+ _queue_phase = 'switching'
+ print(f"[Queue] Switched to scene '{item.scene_name}'")
+ _tag_redraw()
+ return 1.0 # Give Blender a moment to digest
+
+ # ── Phase: SWITCHING → invoke the generation operator ──
+ if _queue_phase == 'switching':
+ scene_ref = bpy.context.scene
+ arch_mode = getattr(scene_ref, 'architecture_mode', 'sdxl')
+
+ # Wait for any in-flight checkpoint / LoRA refreshes to finish so
+ # ``model_name`` resolves to the correct value. The load_post
+ # handler triggers an async refresh when the cached architecture
+ # doesn't match the file that was just opened.
+ from ..core import state as _state
+ if _state._pending_refreshes > 0:
+ if _queue_refresh_deadline == 0.0:
+ _queue_refresh_deadline = _time.monotonic() + 15.0
+ if _time.monotonic() < _queue_refresh_deadline:
+ return 1.0 # Re-check on next tick
+ else:
+ print("[Queue] Model-list refresh timed out — proceeding anyway")
+ _queue_refresh_deadline = 0.0 # Reset for next item
+
+ # Clear the error flag before invoking
+ scene_ref.sg_last_gen_error = False
+
+ # Disable interactive elements for unattended batch
+ if arch_mode == 'trellis2':
+ scene_ref.trellis2_preview_gallery_enabled = False
+
+ try:
+ if arch_mode == 'trellis2':
+ bpy.ops.object.trellis2_generate('INVOKE_DEFAULT')
+ print(f"[Queue] Invoked trellis2_generate for '{item.scene_name}'")
+ else:
+ # Standard texturing — select all cameras first
+ bpy.ops.object.select_all(action='DESELECT')
+ for obj in scene_ref.objects:
+ if obj.type == 'CAMERA':
+ obj.select_set(True)
+ bpy.ops.object.test_stable('INVOKE_DEFAULT')
+ print(f"[Queue] Invoked test_stable for '{item.scene_name}'")
+ _queue_phase = 'running'
+ except Exception as e:
+ print(f"[Queue] Failed to invoke operator for '{item.scene_name}': {e}")
+ _queue_handle_failure(item, str(e))
+ _tag_redraw()
+ return 1.5
+
+ # ── Phase: RUNNING → poll for completion ──
+ if _queue_phase == 'running':
+ from ..mesh_gen.trellis2 import Trellis2Generate
+ from ..texturing.generator import ComfyUIGenerate
+ scene_ref = bpy.context.scene
+ arch_mode = getattr(scene_ref, 'architecture_mode', 'sdxl')
+
+ if arch_mode == 'trellis2':
+ trellis_running = Trellis2Generate._is_running
+ gen_err = getattr(scene_ref, 'sg_last_gen_error', False)
+ tex_mode = getattr(scene_ref, 'trellis2_texture_mode', 'native')
+ pipe_active = getattr(scene_ref, 'trellis2_pipeline_active', False)
+ gen_status = getattr(scene_ref, 'generation_status', 'idle')
+ # TRELLIS.2: wait for the operator to finish
+ if trellis_running:
+ return 1.0 # Still running
+
+ # Check if Trellis2 itself failed
+ if gen_err:
+ print(f"[Queue] TRELLIS.2 generation failed for '{item.scene_name}'")
+ _queue_handle_failure(item, "TRELLIS.2 generation failed")
+ return 0.5
+
+ # Trellis2Generate finished — enter settling phase to wait for
+ # _schedule_texture_generation / _deferred_generate to fire.
+ if tex_mode in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein'):
+ _queue_phase = 'settling'
+ _queue_settle_deadline = _time.monotonic() + 15.0 # generous: server may need VRAM flush
+ _queue_texturing_seen = False
+ print(f"[Queue] TRELLIS.2 mesh done, entering settling (tex_mode='{tex_mode}'), "
+ f"waiting for texturing to start (up to 15 s)...")
+ else:
+ # Native texture mode — mesh only, done
+ print(f"[Queue] tex_mode='{tex_mode}' — no diffusion texturing, finishing item")
+ _queue_finish_item(item)
+ return 1.0
+ else:
+ # Standard texturing
+ gen_status = getattr(scene_ref, 'generation_status', 'idle')
+ if gen_status in ('idle', 'waiting'):
+ if getattr(scene_ref, 'sg_last_gen_error', False):
+ _queue_handle_failure(item, "Generation failed")
+ return 0.5
+ _queue_finish_item(item)
+ return 0.5
+ return 1.0 # Still running
+
+ # ── Phase: SETTLING → wait for TRELLIS.2 chained texturing to start ──
+ if _queue_phase == 'settling':
+ scene_ref = bpy.context.scene
+ pipeline_active = getattr(scene_ref, 'trellis2_pipeline_active', False)
+ gen_status = getattr(scene_ref, 'generation_status', 'idle')
+ if pipeline_active or gen_status == 'running':
+ _queue_texturing_seen = True
+
+ if _queue_texturing_seen:
+ if gen_status == 'running' or pipeline_active:
+ # Texturing is actively running — transition
+ _queue_phase = 'waiting_texturing'
+ print(f"[Queue] Texturing confirmed running, waiting for completion...")
+ return 1.0
+ else:
+ # Texturing already completed (fast finish during settling)
+ if getattr(scene_ref, 'sg_last_gen_error', False):
+ _queue_handle_failure(item, "Texturing failed")
+ else:
+ _queue_finish_item(item)
+ return 0.5
+
+ # Texturing hasn't appeared yet — wait until deadline
+ if _time.monotonic() >= _queue_settle_deadline:
+ print(f"[Queue] Texturing never started for '{item.scene_name}' (timeout)")
+ _queue_handle_failure(item, "Diffusion texturing failed to start")
+ return 0.5
+
+ return 1.0 # Still settling
+
+ # ── Phase: WAITING_TEXTURING → poll chained ComfyUIGenerate ──
+ if _queue_phase == 'waiting_texturing':
+ scene_ref = bpy.context.scene
+ gen_status = getattr(scene_ref, 'generation_status', 'idle')
+ pipeline_active = getattr(scene_ref, 'trellis2_pipeline_active', False)
+ gen_err = getattr(scene_ref, 'sg_last_gen_error', False)
+
+ if gen_status in ('idle', 'waiting') and not pipeline_active:
+ if getattr(scene_ref, 'sg_last_gen_error', False):
+ _queue_handle_failure(item, "Texturing failed")
+ return 0.5
+ _queue_finish_item(item)
+ return 0.5
+ return 1.0 # Still running
+
+ # ── Phase: EXPORTING_GIF → poll ExportOrbitGIF completion ──
+ if _queue_phase == 'exporting_gif':
+ from ..texturing.orbit_export import ExportOrbitGIF
+ if ExportOrbitGIF._rendering:
+ return 1.0 # Still rendering frames
+ # First GIF export finished
+ print("[Queue] GIF export completed")
+
+ # Check whether a second (no-PBR) export is needed
+ wm = bpy.context.window_manager
+ scene_ref = bpy.context.scene
+ if (getattr(wm, 'sg_queue_gif_also_no_pbr', False)
+ and getattr(scene_ref, 'pbr_decomposition', False)):
+ # Start the no-PBR reproject → second GIF pipeline
+ if _queue_start_no_pbr_reproject():
+ _queue_phase = 'reprojecting_no_pbr'
+ print("[Queue] Starting no-PBR reproject for second GIF")
+ _tag_redraw()
+ return 1.0
+ else:
+ print("[Queue] No-PBR reproject failed to start — skipping second GIF")
+
+ _queue_save_and_advance(item)
+ return 0.5
+
+ # ── Phase: REPROJECTING_NO_PBR → wait for ComfyUIGenerate to finish ──
+ if _queue_phase == 'reprojecting_no_pbr':
+ from ..texturing.generator import ComfyUIGenerate
+ if ComfyUIGenerate._is_running:
+ return 1.0
+ # Reproject done — start second GIF with 1 sample + suffix
+ print("[Queue] No-PBR reproject finished — starting second GIF export")
+ if _queue_start_gif_export(samples_override=1, suffix='_no_pbr'):
+ _queue_phase = 'exporting_gif_no_pbr'
+ _tag_redraw()
+ return 1.0
+ else:
+ print("[Queue] Second GIF export failed to start — restoring PBR")
+ _queue_restore_pbr()
+ _queue_save_and_advance(item)
+ return 0.5
+
+ # ── Phase: EXPORTING_GIF_NO_PBR → poll second GIF completion ──
+ if _queue_phase == 'exporting_gif_no_pbr':
+ from ..texturing.orbit_export import ExportOrbitGIF
+ if ExportOrbitGIF._rendering:
+ return 1.0
+ print("[Queue] Second (no-PBR) GIF export completed")
+ _queue_restore_pbr()
+ _queue_save_and_advance(item)
+ return 0.5
+
+ return 1.0
+
+
+def _queue_handle_failure(item, reason):
+ """Handle a failed queue item — retry if under the limit, else mark as error."""
+ global _queue_current_idx, _queue_phase, _queue_processing, _queue_timer
+ global _queue_force_reload
+
+ item.retries += 1
+ if item.retries < _QUEUE_MAX_RETRIES:
+ item.status = 'pending'
+ _queue_phase = 'idle' # Will re-attempt on next tick
+ _queue_force_reload = True # Force re-open to restore clean .blend state
+ print(f"[Queue] Retrying '{item.label or item.scene_name}' "
+ f"(attempt {item.retries + 1}/{_QUEUE_MAX_RETRIES}): {reason}")
+ else:
+ item.status = 'error'
+ item.error_reason = reason
+ print(f"[Queue] '{item.label or item.scene_name}' failed after "
+ f"{_QUEUE_MAX_RETRIES} attempts: {reason}")
+ _queue_current_idx += 1
+ # Find next pending
+ wm = bpy.context.window_manager
+ while _queue_current_idx < len(wm.sg_scene_queue):
+ if wm.sg_scene_queue[_queue_current_idx].status == 'pending':
+ break
+ _queue_current_idx += 1
+ if _queue_current_idx >= len(wm.sg_scene_queue):
+ _queue_processing = False
+ _queue_phase = 'idle'
+ _queue_timer = None
+ print("[Queue] All queue items processed.")
+ else:
+ _queue_phase = 'idle'
+ _persist_queue()
+ _tag_redraw()
+
+
+def _queue_finish_item(item):
+ """Mark the current queue item as done and advance to the next.
+
+ If GIF export is enabled, starts the orbit GIF export first and
+ defers the save+advance to the ``exporting_gif`` phase handler.
+ """
+ global _queue_current_idx, _queue_phase, _queue_processing, _queue_timer
+
+ item.status = 'done'
+ print(f"[Queue] Finished '{item.scene_name}'")
+
+ # ── Optional GIF export before saving ──
+ wm = bpy.context.window_manager
+ if getattr(wm, 'sg_queue_gif_export', False):
+ if _queue_start_gif_export():
+ _queue_phase = 'exporting_gif'
+ print("[Queue] GIF export started — deferring save until export finishes")
+ _tag_redraw()
+ return # save + advance will happen in the 'exporting_gif' handler
+
+ # No GIF (or export failed to start) — save and advance immediately
+ _queue_save_and_advance(item)
+
+
+def _queue_start_gif_export(samples_override=None, suffix=''):
+ """Try to invoke ExportOrbitGIF with current WM settings.
+
+ Args:
+ samples_override: If set, use this sample count instead of the WM setting.
+ suffix: Appended to orbit filenames (e.g. '_no_pbr' → orbit_no_pbr.gif).
+
+ Returns True if the operator was successfully started.
+ """
+ from ..texturing.orbit_export import ExportOrbitGIF
+
+ scene = bpy.context.scene
+
+ # ── Always use the first camera (Camera_0 or lowest-numbered) ──
+ cameras = sorted(
+ [o for o in scene.objects if o.type == 'CAMERA'],
+ key=lambda c: c.name,
+ )
+ if not cameras:
+ print("[Queue] No cameras in scene — skipping GIF export")
+ return False
+ scene.camera = cameras[0]
+ print(f"[Queue] GIF export: using camera '{cameras[0].name}'")
+
+ # ── Ensure an active mesh/empty exists (poll requirement) ──
+ vl = bpy.context.view_layer
+ active = vl.objects.active
+ if not active or active.type not in {'MESH', 'EMPTY'}:
+ for obj in scene.objects:
+ if obj.type == 'MESH':
+ vl.objects.active = obj
+ obj.select_set(True)
+ print(f"[Queue] GIF export: activated mesh '{obj.name}'")
+ break
+ else:
+ print("[Queue] No mesh in scene — skipping GIF export")
+ return False
+
+ wm = bpy.context.window_manager
+
+ # ── Explicitly apply HDRI from the queue's file path (if set) ──
+ hdri_path = getattr(wm, 'sg_queue_gif_hdri_path', '')
+ hdri_strength = getattr(wm, 'sg_queue_gif_hdri_strength', 1.0)
+ use_hdri = getattr(wm, 'sg_queue_gif_use_hdri', False)
+
+ if use_hdri and hdri_path:
+ import os as _os
+ resolved = bpy.path.abspath(hdri_path)
+ if _os.path.isfile(resolved):
+ try:
+ bpy.ops.object.add_hdri(hdri_path=resolved,
+ strength=hdri_strength)
+ print(f"[Queue] Applied HDRI: {resolved} "
+ f"(strength={hdri_strength:.2f})")
+ except Exception as e:
+ print(f"[Queue] AddHDRI failed: {e} — continuing anyway")
+ else:
+ print(f"[Queue] HDRI file not found: {resolved} — skipping HDRI")
+
+ kwargs = {
+ 'duration': getattr(wm, 'sg_queue_gif_duration', 5.0),
+ 'frame_rate': getattr(wm, 'sg_queue_gif_fps', 24),
+ 'resolution_percentage': getattr(wm, 'sg_queue_gif_resolution', 50),
+ 'samples': samples_override if samples_override is not None else getattr(wm, 'sg_queue_gif_samples', 32),
+ 'engine': getattr(wm, 'sg_queue_gif_engine', 'CYCLES'),
+ 'interpolation': getattr(wm, 'sg_queue_gif_interpolation', 'LINEAR'),
+ 'use_hdri': use_hdri,
+ 'hdri_rotation': getattr(wm, 'sg_queue_gif_hdri_rotation', 0.0),
+ 'env_mode': getattr(wm, 'sg_queue_gif_env_mode', 'FIXED'),
+ 'use_denoiser': getattr(wm, 'sg_queue_gif_denoiser', True),
+ 'use_gpu': getattr(wm, 'sg_queue_gif_use_gpu', True),
+ 'filename_suffix': suffix,
+ }
+
+ # Build a temp_override so the modal operator has a proper window
+ window = bpy.context.window
+ if not window:
+ for w in wm.windows:
+ window = w
+ break
+ if not window:
+ print("[Queue] No window available — skipping GIF export")
+ return False
+
+ try:
+ with bpy.context.temp_override(window=window):
+ result = bpy.ops.object.export_orbit_gif('EXEC_DEFAULT', **kwargs)
+ if 'RUNNING_MODAL' in result:
+ print(f"[Queue] GIF export started successfully")
+ return True
+ print(f"[Queue] GIF export returned {result} — skipping")
+ return False
+ except Exception as e:
+ print(f"[Queue] GIF export failed to start: {e}")
+ import traceback; traceback.print_exc()
+ return False
+
+
+def _queue_start_no_pbr_reproject():
+ """Disable PBR, then invoke a project-only reproject.
+
+ Saves the original PBR / generation settings so they can be restored
+ by ``_queue_restore_pbr()`` after the second GIF is done.
+
+ Returns True if the reproject operator was successfully started.
+ """
+ global _queue_original_pbr, _queue_original_gen_method, _queue_original_overwrite
+
+ scene = bpy.context.scene
+ _queue_original_pbr = scene.pbr_decomposition
+ _queue_original_gen_method = scene.generation_method
+ _queue_original_overwrite = scene.overwrite_material
+
+ scene.pbr_decomposition = False
+ scene.generation_mode = 'project_only'
+ scene.generation_method = 'separate'
+ scene.overwrite_material = True
+
+ # Select all cameras (required by ComfyUIGenerate)
+ bpy.ops.object.select_all(action='DESELECT')
+ for obj in scene.objects:
+ if obj.type == 'CAMERA':
+ obj.select_set(True)
+
+ window = bpy.context.window
+ wm = bpy.context.window_manager
+ if not window:
+ for w in wm.windows:
+ window = w
+ break
+ if not window:
+ print("[Queue] No window available — cannot start reproject")
+ return False
+
+ try:
+ with bpy.context.temp_override(window=window):
+ bpy.ops.object.test_stable('INVOKE_DEFAULT')
+ print("[Queue] No-PBR reproject invoked")
+ return True
+ except Exception as e:
+ print(f"[Queue] Failed to invoke reproject: {e}")
+ import traceback; traceback.print_exc()
+ # Restore settings on failure
+ scene.pbr_decomposition = _queue_original_pbr
+ scene.generation_mode = 'standard'
+ scene.generation_method = _queue_original_gen_method
+ scene.overwrite_material = _queue_original_overwrite
+ return False
+
+
+def _queue_restore_pbr():
+ """Restore PBR and generation settings saved by ``_queue_start_no_pbr_reproject``."""
+ scene = bpy.context.scene
+ scene.pbr_decomposition = _queue_original_pbr
+ scene.generation_mode = 'standard'
+ scene.generation_method = _queue_original_gen_method
+ scene.overwrite_material = _queue_original_overwrite
+ print(f"[Queue] Restored pbr_decomposition={_queue_original_pbr}")
+
+
+def _queue_save_and_advance(item):
+ """Save the current .blend and advance to the next queue item."""
+ global _queue_current_idx, _queue_phase, _queue_processing, _queue_timer
+
+ # Save the result in-place on the queue copy .blend
+ if bpy.data.filepath:
+ try:
+ bpy.ops.wm.save_mainfile()
+ print(f"[Queue] Saved result: {bpy.data.filepath}")
+ except Exception as e:
+ print(f"[Queue] Save warning: {e}")
+
+ # Advance to next pending item
+ _queue_current_idx += 1
+ wm = bpy.context.window_manager
+ while _queue_current_idx < len(wm.sg_scene_queue):
+ if wm.sg_scene_queue[_queue_current_idx].status == 'pending':
+ break
+ _queue_current_idx += 1
+
+ if _queue_current_idx >= len(wm.sg_scene_queue):
+ _queue_processing = False
+ _queue_phase = 'idle'
+ _queue_timer = None
+ print("[Queue] All queue items processed.")
+ else:
+ _queue_phase = 'idle' # Will switch to next scene on next tick
+
+ _persist_queue()
+ _tag_redraw()
+
+
+def _tag_redraw():
+ """Redraw all 3D viewports so the queue UI updates."""
+ try:
+ for window in bpy.context.window_manager.windows:
+ for area in window.screen.areas:
+ area.tag_redraw()
+ except Exception:
+ pass
diff --git a/stablegen/color_match.py b/stablegen/util/color_match.py
similarity index 96%
rename from stablegen/color_match.py
rename to stablegen/util/color_match.py
index 69e5644..17ad88e 100644
--- a/stablegen/color_match.py
+++ b/stablegen/util/color_match.py
@@ -1,243 +1,243 @@
-# stablegen/color_match.py
-
-from __future__ import annotations
-import numpy as np
-
-def _rgb_to_yuv(rgb: np.ndarray) -> np.ndarray:
- """
- Convert RGB [0,1] to YUV (BT.601-ish).
- rgb: (..., 3)
- """
- r = rgb[..., 0]
- g = rgb[..., 1]
- b = rgb[..., 2]
-
- y = 0.299 * r + 0.587 * g + 0.114 * b
- u = -0.14713 * r - 0.28886 * g + 0.436 * b
- v = 0.615 * r - 0.51499 * g - 0.10001 * b
-
- return np.stack((y, u, v), axis=-1)
-
-
-def _yuv_to_rgb(yuv: np.ndarray) -> np.ndarray:
- """
- Convert YUV back to RGB [0,1] and clamp.
- yuv: (..., 3)
- """
- y = yuv[..., 0]
- u = yuv[..., 1]
- v = yuv[..., 2]
-
- r = y + 1.13983 * v
- g = y - 0.39465 * u - 0.58060 * v
- b = y + 2.03211 * u
-
- rgb = np.stack((r, g, b), axis=-1)
- return np.clip(rgb, 0.0, 1.0)
-
-def _boost_chroma_yuv(
- tgt_rgb: np.ndarray,
- matched_rgb: np.ndarray,
- extra: float,
-) -> np.ndarray:
- """
- Exaggerate the chroma shift from tgt_rgb -> matched_rgb, while
- preserving luminance from tgt_rgb.
-
- extra: in [0, 1], where 0 = no extra, 1 = max extra.
- """
- extra = float(max(0.0, min(extra, 1.0)))
-
- yuv_tgt = _rgb_to_yuv(tgt_rgb) # Y from target
- yuv_mat = _rgb_to_yuv(matched_rgb) # UV from matched
-
- y = yuv_tgt[..., 0]
- uv_t = yuv_tgt[..., 1:]
- uv_m = yuv_mat[..., 1:]
-
- # Extrapolate UV away from target toward matched:
- # extra = 0 -> uv = uv_m (no extra beyond matched)
- # extra = 1 -> uv = uv_t + 2 * (uv_m - uv_t)
- scale = 1.0 + extra
- uv = uv_t + scale * (uv_m - uv_t)
-
- yuv = np.empty_like(yuv_tgt)
- yuv[..., 0] = y
- yuv[..., 1:] = uv
-
- return _yuv_to_rgb(yuv)
-
-def _split_rgb_alpha(img: np.ndarray):
- """Return (rgb, alpha or None) from [H, W, 3/4] float32 [0, 1]."""
- if img.ndim != 3 or img.shape[2] not in (3, 4):
- raise ValueError(f"Expected [H, W, 3 or 4], got {img.shape}")
- if img.shape[2] == 4:
- return img[..., :3], img[..., 3:4]
- return img, None
-
-
-def _merge_rgb_alpha(rgb: np.ndarray, alpha: np.ndarray | None, like: np.ndarray) -> np.ndarray:
- """Rebuild into 3 or 4 channels to match `like`."""
- if like.shape[2] == 4:
- if alpha is None:
- alpha = like[..., 3:4]
- return np.concatenate([rgb, alpha], axis=-1)
- return rgb
-
-
-def _reinhard_preserve_luma(ref_rgb: np.ndarray, tgt_rgb: np.ndarray) -> np.ndarray:
- """
- Reinhard-style color transfer that matches only chroma (U/V),
- while preserving the target luminance (Y).
-
- This avoids the typical darkening/brightening side effects and
- is better suited for 'make it bluer' style corrections.
- """
- ref_yuv = _rgb_to_yuv(ref_rgb)
- tgt_yuv = _rgb_to_yuv(tgt_rgb)
-
- # Flatten U/V for stats
- ref_uv = ref_yuv[..., 1:].reshape(-1, 2)
- tgt_uv = tgt_yuv[..., 1:].reshape(-1, 2)
-
- ref_mu = ref_uv.mean(axis=0)
- ref_sigma = ref_uv.std(axis=0) + 1e-6
-
- tgt_mu = tgt_uv.mean(axis=0)
- tgt_sigma = tgt_uv.std(axis=0) + 1e-6
-
- out = np.copy(tgt_yuv)
-
- uv = out[..., 1:]
- uv = (uv - tgt_mu) / tgt_sigma
- uv = uv * ref_sigma + ref_mu
- out[..., 1:] = uv # keep Y as-is, only adjust U/V
-
- return _yuv_to_rgb(out)
-
-
-
-def _hist_match(ref_rgb: np.ndarray, tgt_rgb: np.ndarray) -> np.ndarray:
- """Simple per-channel histogram matching."""
- out = tgt_rgb.copy()
- ref = (np.clip(ref_rgb, 0, 1) * 255.0).astype(np.float32)
- tgt = (np.clip(tgt_rgb, 0, 1) * 255.0).astype(np.float32)
-
- for c in range(3):
- ref_c = ref[..., c].ravel()
- tgt_c = tgt[..., c].ravel()
-
- tgt_vals, tgt_idx, tgt_counts = np.unique(
- tgt_c, return_inverse=True, return_counts=True
- )
- ref_vals, ref_counts = np.unique(ref_c, return_counts=True)
-
- tgt_cdf = np.cumsum(tgt_counts).astype(np.float64)
- tgt_cdf /= tgt_cdf[-1]
-
- ref_cdf = np.cumsum(ref_counts).astype(np.float64)
- ref_cdf /= ref_cdf[-1]
-
- mapped = np.interp(tgt_cdf, ref_cdf, ref_vals)
- matched = mapped[tgt_idx].reshape(tgt_rgb.shape[:2])
- out[..., c] = matched / 255.0
-
- return np.clip(out, 0.0, 1.0)
-
-
-def _mvgd(ref_rgb: np.ndarray, tgt_rgb: np.ndarray) -> np.ndarray:
- """Covariance-based color transfer (MVGD-ish)."""
- eps = 1e-6
- ref_flat = ref_rgb.reshape(-1, 3).astype(np.float64)
- tgt_flat = tgt_rgb.reshape(-1, 3).astype(np.float64)
-
- mu_ref = ref_flat.mean(axis=0, keepdims=True)
- mu_tgt = tgt_flat.mean(axis=0, keepdims=True)
-
- ref_c = ref_flat - mu_ref
- tgt_c = tgt_flat - mu_tgt
-
- cov_ref = np.cov(ref_c, rowvar=False) + np.eye(3) * eps
- cov_tgt = np.cov(tgt_c, rowvar=False) + np.eye(3) * eps
-
- eval_t, evec_t = np.linalg.eigh(cov_tgt)
- eval_r, evec_r = np.linalg.eigh(cov_ref)
-
- D_t_inv_sqrt = np.diag(1.0 / np.sqrt(eval_t))
- D_r_sqrt = np.diag(np.sqrt(eval_r))
-
- W_t = evec_t @ D_t_inv_sqrt @ evec_t.T # whitening
- C_r = evec_r @ D_r_sqrt @ evec_r.T # coloring
-
- tgt_whitened = tgt_c @ W_t.T
- tgt_colored = tgt_whitened @ C_r.T
-
- matched_flat = tgt_colored + mu_ref
- matched = matched_flat.reshape(tgt_rgb.shape)
- return np.clip(matched, 0.0, 1.0).astype(np.float32)
-
-
-def _apply_core(ref_rgb: np.ndarray, tgt_rgb: np.ndarray, method: str) -> np.ndarray:
- m = (method or "reinhard").lower()
- if m == "reinhard":
- return _reinhard_preserve_luma(ref_rgb, tgt_rgb)
- elif m == "hm":
- return _hist_match(ref_rgb, tgt_rgb)
- elif m in ("mvgd", "mkl"): # treat MKL ~ MVGD
- return _mvgd(ref_rgb, tgt_rgb)
- elif m == "hm-mvgd-hm":
- s1 = _hist_match(ref_rgb, tgt_rgb)
- s2 = _mvgd(ref_rgb, s1)
- s3 = _hist_match(ref_rgb, s2)
- return s3
- elif m == "hm-mkl-hm":
- s1 = _hist_match(ref_rgb, tgt_rgb)
- s2 = _mvgd(ref_rgb, s1)
- s3 = _hist_match(ref_rgb, s2)
- return s3
- else:
- return _reinhard(ref_rgb, tgt_rgb)
-
-
-def color_match_single(
- ref: np.ndarray,
- target: np.ndarray,
- method: str = "reinhard",
- strength: float = 1.0,
-) -> np.ndarray:
- """
- Color-match a single target to a reference.
- ref / target: [H, W, 3 or 4] in [0, 1].
-
- UI strength is interpreted as:
- 0.0–1.0 : blend from original -> matched
- 1.0–2.0 : full match + extra chroma push (no big luminance change)
- """
- if ref.shape[:2] != target.shape[:2]:
- raise ValueError(
- f"Size mismatch: ref {ref.shape[:2]} vs target {target.shape[:2]}"
- )
-
- # Split out RGB / A
- ref_rgb, _ = _split_rgb_alpha(ref)
- tgt_rgb, tgt_a = _split_rgb_alpha(target)
-
- # Full-strength match result (what "1.0" really means)
- base_matched_rgb = _apply_core(ref_rgb, tgt_rgb, method)
-
- # Clamp UI strength to [0, 2]
- ui_s = float(max(0.0, min(strength, 2.0)))
-
- if ui_s <= 1.0:
- # 0–1: regular blend between target and matched
- s = ui_s
- matched_rgb = tgt_rgb * (1.0 - s) + base_matched_rgb * s
- else:
- # >1: start from fully matched, then push chroma harder
- matched_rgb = base_matched_rgb
- extra = ui_s - 1.0 # in (0, 1]
- matched_rgb = _boost_chroma_yuv(tgt_rgb, matched_rgb, extra)
-
- matched_rgb = np.clip(matched_rgb, 0.0, 1.0)
- out = _merge_rgb_alpha(matched_rgb, tgt_a, like=target)
- return out.astype(np.float32)
+# stablegen/util/color_match.py
+
+from __future__ import annotations
+import numpy as np
+
+def _rgb_to_yuv(rgb: np.ndarray) -> np.ndarray:
+ """
+ Convert RGB [0,1] to YUV (BT.601-ish).
+ rgb: (..., 3)
+ """
+ r = rgb[..., 0]
+ g = rgb[..., 1]
+ b = rgb[..., 2]
+
+ y = 0.299 * r + 0.587 * g + 0.114 * b
+ u = -0.14713 * r - 0.28886 * g + 0.436 * b
+ v = 0.615 * r - 0.51499 * g - 0.10001 * b
+
+ return np.stack((y, u, v), axis=-1)
+
+
+def _yuv_to_rgb(yuv: np.ndarray) -> np.ndarray:
+ """
+ Convert YUV back to RGB [0,1] and clamp.
+ yuv: (..., 3)
+ """
+ y = yuv[..., 0]
+ u = yuv[..., 1]
+ v = yuv[..., 2]
+
+ r = y + 1.13983 * v
+ g = y - 0.39465 * u - 0.58060 * v
+ b = y + 2.03211 * u
+
+ rgb = np.stack((r, g, b), axis=-1)
+ return np.clip(rgb, 0.0, 1.0)
+
+def _boost_chroma_yuv(
+ tgt_rgb: np.ndarray,
+ matched_rgb: np.ndarray,
+ extra: float,
+) -> np.ndarray:
+ """
+ Exaggerate the chroma shift from tgt_rgb -> matched_rgb, while
+ preserving luminance from tgt_rgb.
+
+ extra: in [0, 1], where 0 = no extra, 1 = max extra.
+ """
+ extra = float(max(0.0, min(extra, 1.0)))
+
+ yuv_tgt = _rgb_to_yuv(tgt_rgb) # Y from target
+ yuv_mat = _rgb_to_yuv(matched_rgb) # UV from matched
+
+ y = yuv_tgt[..., 0]
+ uv_t = yuv_tgt[..., 1:]
+ uv_m = yuv_mat[..., 1:]
+
+ # Extrapolate UV away from target toward matched:
+ # extra = 0 -> uv = uv_m (no extra beyond matched)
+ # extra = 1 -> uv = uv_t + 2 * (uv_m - uv_t)
+ scale = 1.0 + extra
+ uv = uv_t + scale * (uv_m - uv_t)
+
+ yuv = np.empty_like(yuv_tgt)
+ yuv[..., 0] = y
+ yuv[..., 1:] = uv
+
+ return _yuv_to_rgb(yuv)
+
+def _split_rgb_alpha(img: np.ndarray):
+ """Return (rgb, alpha or None) from [H, W, 3/4] float32 [0, 1]."""
+ if img.ndim != 3 or img.shape[2] not in (3, 4):
+ raise ValueError(f"Expected [H, W, 3 or 4], got {img.shape}")
+ if img.shape[2] == 4:
+ return img[..., :3], img[..., 3:4]
+ return img, None
+
+
+def _merge_rgb_alpha(rgb: np.ndarray, alpha: np.ndarray | None, like: np.ndarray) -> np.ndarray:
+ """Rebuild into 3 or 4 channels to match `like`."""
+ if like.shape[2] == 4:
+ if alpha is None:
+ alpha = like[..., 3:4]
+ return np.concatenate([rgb, alpha], axis=-1)
+ return rgb
+
+
+def _reinhard_preserve_luma(ref_rgb: np.ndarray, tgt_rgb: np.ndarray) -> np.ndarray:
+ """
+ Reinhard-style color transfer that matches only chroma (U/V),
+ while preserving the target luminance (Y).
+
+ This avoids the typical darkening/brightening side effects and
+ is better suited for 'make it bluer' style corrections.
+ """
+ ref_yuv = _rgb_to_yuv(ref_rgb)
+ tgt_yuv = _rgb_to_yuv(tgt_rgb)
+
+ # Flatten U/V for stats
+ ref_uv = ref_yuv[..., 1:].reshape(-1, 2)
+ tgt_uv = tgt_yuv[..., 1:].reshape(-1, 2)
+
+ ref_mu = ref_uv.mean(axis=0)
+ ref_sigma = ref_uv.std(axis=0) + 1e-6
+
+ tgt_mu = tgt_uv.mean(axis=0)
+ tgt_sigma = tgt_uv.std(axis=0) + 1e-6
+
+ out = np.copy(tgt_yuv)
+
+ uv = out[..., 1:]
+ uv = (uv - tgt_mu) / tgt_sigma
+ uv = uv * ref_sigma + ref_mu
+ out[..., 1:] = uv # keep Y as-is, only adjust U/V
+
+ return _yuv_to_rgb(out)
+
+
+
+def _hist_match(ref_rgb: np.ndarray, tgt_rgb: np.ndarray) -> np.ndarray:
+ """Simple per-channel histogram matching."""
+ out = tgt_rgb.copy()
+ ref = (np.clip(ref_rgb, 0, 1) * 255.0).astype(np.float32)
+ tgt = (np.clip(tgt_rgb, 0, 1) * 255.0).astype(np.float32)
+
+ for c in range(3):
+ ref_c = ref[..., c].ravel()
+ tgt_c = tgt[..., c].ravel()
+
+ tgt_vals, tgt_idx, tgt_counts = np.unique(
+ tgt_c, return_inverse=True, return_counts=True
+ )
+ ref_vals, ref_counts = np.unique(ref_c, return_counts=True)
+
+ tgt_cdf = np.cumsum(tgt_counts).astype(np.float64)
+ tgt_cdf /= tgt_cdf[-1]
+
+ ref_cdf = np.cumsum(ref_counts).astype(np.float64)
+ ref_cdf /= ref_cdf[-1]
+
+ mapped = np.interp(tgt_cdf, ref_cdf, ref_vals)
+ matched = mapped[tgt_idx].reshape(tgt_rgb.shape[:2])
+ out[..., c] = matched / 255.0
+
+ return np.clip(out, 0.0, 1.0)
+
+
+def _mvgd(ref_rgb: np.ndarray, tgt_rgb: np.ndarray) -> np.ndarray:
+ """Covariance-based color transfer (MVGD-ish)."""
+ eps = 1e-6
+ ref_flat = ref_rgb.reshape(-1, 3).astype(np.float64)
+ tgt_flat = tgt_rgb.reshape(-1, 3).astype(np.float64)
+
+ mu_ref = ref_flat.mean(axis=0, keepdims=True)
+ mu_tgt = tgt_flat.mean(axis=0, keepdims=True)
+
+ ref_c = ref_flat - mu_ref
+ tgt_c = tgt_flat - mu_tgt
+
+ cov_ref = np.cov(ref_c, rowvar=False) + np.eye(3) * eps
+ cov_tgt = np.cov(tgt_c, rowvar=False) + np.eye(3) * eps
+
+ eval_t, evec_t = np.linalg.eigh(cov_tgt)
+ eval_r, evec_r = np.linalg.eigh(cov_ref)
+
+ D_t_inv_sqrt = np.diag(1.0 / np.sqrt(eval_t))
+ D_r_sqrt = np.diag(np.sqrt(eval_r))
+
+ W_t = evec_t @ D_t_inv_sqrt @ evec_t.T # whitening
+ C_r = evec_r @ D_r_sqrt @ evec_r.T # coloring
+
+ tgt_whitened = tgt_c @ W_t.T
+ tgt_colored = tgt_whitened @ C_r.T
+
+ matched_flat = tgt_colored + mu_ref
+ matched = matched_flat.reshape(tgt_rgb.shape)
+ return np.clip(matched, 0.0, 1.0).astype(np.float32)
+
+
+def _apply_core(ref_rgb: np.ndarray, tgt_rgb: np.ndarray, method: str) -> np.ndarray:
+ m = (method or "reinhard").lower()
+ if m == "reinhard":
+ return _reinhard_preserve_luma(ref_rgb, tgt_rgb)
+ elif m == "hm":
+ return _hist_match(ref_rgb, tgt_rgb)
+ elif m in ("mvgd", "mkl"): # treat MKL ~ MVGD
+ return _mvgd(ref_rgb, tgt_rgb)
+ elif m == "hm-mvgd-hm":
+ s1 = _hist_match(ref_rgb, tgt_rgb)
+ s2 = _mvgd(ref_rgb, s1)
+ s3 = _hist_match(ref_rgb, s2)
+ return s3
+ elif m == "hm-mkl-hm":
+ s1 = _hist_match(ref_rgb, tgt_rgb)
+ s2 = _mvgd(ref_rgb, s1)
+ s3 = _hist_match(ref_rgb, s2)
+ return s3
+ else:
+ return _reinhard(ref_rgb, tgt_rgb)
+
+
+def color_match_single(
+ ref: np.ndarray,
+ target: np.ndarray,
+ method: str = "reinhard",
+ strength: float = 1.0,
+) -> np.ndarray:
+ """
+ Color-match a single target to a reference.
+ ref / target: [H, W, 3 or 4] in [0, 1].
+
+ UI strength is interpreted as:
+ 0.0–1.0 : blend from original -> matched
+ 1.0–2.0 : full match + extra chroma push (no big luminance change)
+ """
+ if ref.shape[:2] != target.shape[:2]:
+ raise ValueError(
+ f"Size mismatch: ref {ref.shape[:2]} vs target {target.shape[:2]}"
+ )
+
+ # Split out RGB / A
+ ref_rgb, _ = _split_rgb_alpha(ref)
+ tgt_rgb, tgt_a = _split_rgb_alpha(target)
+
+ # Full-strength match result (what "1.0" really means)
+ base_matched_rgb = _apply_core(ref_rgb, tgt_rgb, method)
+
+ # Clamp UI strength to [0, 2]
+ ui_s = float(max(0.0, min(strength, 2.0)))
+
+ if ui_s <= 1.0:
+ # 0–1: regular blend between target and matched
+ s = ui_s
+ matched_rgb = tgt_rgb * (1.0 - s) + base_matched_rgb * s
+ else:
+ # >1: start from fully matched, then push chroma harder
+ matched_rgb = base_matched_rgb
+ extra = ui_s - 1.0 # in (0, 1]
+ matched_rgb = _boost_chroma_yuv(tgt_rgb, matched_rgb, extra)
+
+ matched_rgb = np.clip(matched_rgb, 0.0, 1.0)
+ out = _merge_rgb_alpha(matched_rgb, tgt_a, like=target)
+ return out.astype(np.float32)
diff --git a/stablegen/util/helpers.py b/stablegen/util/helpers.py
index 3232d26..19c5063 100755
--- a/stablegen/util/helpers.py
+++ b/stablegen/util/helpers.py
@@ -1,1822 +1,17 @@
-import random
-
-# Prompt for ComfyUI in API format (SDXL)
-prompt_text = """
- {
- "5": {
- "inputs": {
- "image": "",
- "upload": "image"
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- },
- "6": {
- "inputs": {
- "ckpt_name": "sd_xl_base_1.0.safetensors"
- },
- "class_type": "CheckpointLoaderSimple",
- "_meta": {
- "title": "Load Checkpoint"
- }
- },
- "9": {
- "inputs": {
- "text": "a monkey",
- "clip": [
- "247",
- 0
- ]
- },
- "class_type": "CLIPTextEncode",
- "_meta": {
- "title": "CLIP Text Encode (Prompt)"
- }
- },
- "10": {
- "inputs": {
- "text": "",
- "clip": [
- "247",
- 0
- ]
- },
- "class_type": "CLIPTextEncode",
- "_meta": {
- "title": "CLIP Text Encode (Prompt)"
- }
- },
- "15": {
- "inputs": {
- "seed": 24,
- "steps": 8,
- "cfg": 1.5,
- "sampler_name": "dpmpp_2s_ancestral",
- "scheduler": "sgm_uniform",
- "denoise": 1,
- "model": [
- "26",
- 0
- ],
- "positive": [
- "14",
- 0
- ],
- "negative": [
- "14",
- 1
- ],
- "latent_image": [
- "16",
- 0
- ]
- },
- "class_type": "KSampler",
- "_meta": {
- "title": "KSampler"
- }
- },
- "16": {
- "inputs": {
- "width": 1024,
- "height": 1024,
- "batch_size": 1
- },
- "class_type": "EmptyLatentImage",
- "_meta": {
- "title": "Empty Latent Image"
- }
- },
- "19": {
- "inputs": {
- "samples": [
- "15",
- 0
- ],
- "vae": [
- "6",
- 2
- ]
- },
- "class_type": "VAEDecode",
- "_meta": {
- "title": "VAE Decode"
- }
- },
- "25": {
- "inputs": {
- "images": [
- "19",
- 0
- ]
- },
- "class_type": "SaveImageWebsocket",
- "_meta": {
- "title": "SaveImageWebsocket"
- }
- },
- "27": {
- "inputs": {
- "image": "",
- "upload": "image"
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- },
- "239": {
- "inputs": {
- "type": "depth",
- "control_net": [
- "201",
- 0
- ]
- },
- "class_type": "SetUnionControlNetType",
- "_meta": {
- "title": "SetUnionControlNetType"
- }
- },
- "247": {
- "inputs": {
- "stop_at_clip_layer": -1,
- "clip": [
- "",
- 1
- ]
- },
- "class_type": "CLIPSetLastLayer",
- "_meta": {
- "title": "CLIP Set Last Layer"
- }
- },
- "235": {
- "inputs": {
- "preset": "PLUS (high strength)",
- "model": [
- "",
- 0
- ]
- },
- "class_type": "IPAdapterUnifiedLoader",
- "_meta": {
- "title": "IPAdapter Unified Loader"
- }
- },
- "236": {
- "inputs": {
- "weight": 1,
- "start_at": 0,
- "end_at": 1,
- "weight_type": "standard",
- "model": [
- "235",
- 0
- ],
- "ipadapter": [
- "235",
- 1
- ],
- "image": ["237", 0]
- },
- "class_type": "IPAdapter",
- "_meta": {
- "title": "IPAdapter"
- }
- },
- "237": {
- "inputs": {
- "upload": "image"
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- }
- }
- """
-
-prompt_text_img2img = """
-{
- "1": {
- "inputs": {
- "image": "",
- "upload": "image"
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- },
- "12": {
- "inputs": {
- "image": "",
- "upload": "image"
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- },
- "23": {
- "inputs": {
- "upscale_method": "nearest-exact",
- "width": 1024,
- "height": 1024,
- "crop": "disabled",
- "image": [
- "1",
- 0
- ]
- },
- "class_type": "ImageScale",
- "_meta": {
- "title": "Upscale Image"
- }
- },
- "13": {
- "inputs": {
- "grow_mask_by": 0,
- "pixels": [
- "23",
- 0
- ],
- "vae": [
- "38",
- 2
- ],
- "mask": [
- "25",
- 0
- ]
- },
- "class_type": "VAEEncodeForInpaint",
- "_meta": {
- "title": "VAE Encode (for Inpainting)"
- }
- },
- "25": {
- "inputs": {
- "channel": "red",
- "image": [
- "12",
- 0
- ]
- },
- "class_type": "ImageToMask",
- "_meta": {
- "title": "Convert Image to Mask"
- }
- },
- "38": {
- "inputs": {
- "ckpt_name": "realvisxlV40_v40Bakedvae.safetensors"
- },
- "class_type": "CheckpointLoaderSimple",
- "_meta": {
- "title": "Load Checkpoint"
- }
- },
- "102": {
- "inputs": {
- "text": "tifa lockhart, cosplay, final fantasy",
- "clip": [
- "247",
- 0
- ]
- },
- "class_type": "CLIPTextEncode",
- "_meta": {
- "title": "CLIP Text Encode (Prompt)"
- }
- },
- "103": {
- "inputs": {
- "text": "",
- "clip": [
- "247",
- 0
- ]
- },
- "class_type": "CLIPTextEncode",
- "_meta": {
- "title": "CLIP Text Encode (Prompt)"
- }
- },
- "105": {
- "inputs": {
- "seed": 42,
- "steps": 8,
- "cfg": 1.48,
- "sampler_name": "dpmpp_sde",
- "scheduler": "sgm_uniform",
- "denoise": 0.5,
- "model": [
- "",
- 0
- ],
- "positive": [
- "104",
- 0
- ],
- "negative": [
- "104",
- 1
- ],
- "latent_image": [
- "116",
- 0
- ]
- },
- "class_type": "KSampler",
- "_meta": {
- "title": "KSampler"
- }
- },
- "108": {
- "inputs": {
- "image": "",
- "upload": "image"
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- },
- "110": {
- "inputs": {
- "samples": [
- "105",
- 0
- ],
- "vae": [
- "38",
- 2
- ]
- },
- "class_type": "VAEDecode",
- "_meta": {
- "title": "VAE Decode"
- }
- },
- "111": {
- "inputs": {
- "images": [
- "110",
- 0
- ]
- },
- "class_type": "SaveImageWebsocket",
- "_meta": {
- "title": "SaveImageWebsocket"
- }
- },
- "116": {
- "inputs": {
- "pixels": [
- "118",
- 0
- ],
- "vae": [
- "38",
- 2
- ]
- },
- "class_type": "VAEEncode",
- "_meta": {
- "title": "VAE Encode"
- }
- },
- "117": {
- "inputs": {
- "image": "",
- "upload": "image"
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- },
- "118": {
- "inputs": {
- "upscale_method": "lanczos",
- "width": 713,
- "height": 1080,
- "crop": "disabled",
- "image": [
- "117",
- 0
- ]
- },
- "class_type": "ImageScale",
- "_meta": {
- "title": "Upscale Image"
- }
- },
- "224": {
- "inputs": {
- "expand": 0,
- "tapered_corners": true,
- "mask": [
- "25",
- 0
- ]
- },
- "class_type": "GrowMask",
- "_meta": {
- "title": "GrowMask"
- }
- },
- "225": {
- "inputs": {
- "mask": [
- "224",
- 0
- ]
- },
- "class_type": "MaskToImage",
- "_meta": {
- "title": "Convert Mask to Image"
- }
- },
- "226": {
- "inputs": {
- "blur_radius": 1,
- "sigma": 1,
- "image": [
- "225",
- 0
- ]
- },
- "class_type": "ImageBlur",
- "_meta": {
- "title": "Image Blur"
- }
- },
- "227": {
- "inputs": {
- "channel": "red",
- "image": [
- "226",
- 0
- ]
- },
- "class_type": "ImageToMask",
- "_meta": {
- "title": "Convert Image to Mask"
- }
- },
- "228": {
- "inputs": {
- "noise_mask": true,
- "positive": [
- "102",
- 0
- ],
- "negative": [
- "103",
- 0
- ],
- "vae": [
- "38",
- 2
- ],
- "pixels": [
- "1",
- 0
- ],
- "mask": [
- "227",
- 0
- ]
- },
- "class_type": "InpaintModelConditioning",
- "_meta": {
- "title": "InpaintModelConditioning"
- }
- },
- "229": {
- "inputs": {
- "model": [
- "",
- 0
- ]
- },
- "class_type": "DifferentialDiffusion",
- "_meta": {
- "title": "Differential Diffusion"
- }
- },
- "235": {
- "inputs": {
- "preset": "PLUS (high strength)",
- "model": [
- "",
- 0
- ]
- },
- "class_type": "IPAdapterUnifiedLoader",
- "_meta": {
- "title": "IPAdapter Unified Loader"
- }
- },
- "236": {
- "inputs": {
- "weight": 1,
- "start_at": 0,
- "end_at": 1,
- "weight_type": "standard",
- "model": [
- "235",
- 0
- ],
- "ipadapter": [
- "235",
- 1
- ],
- "image": ["1", 0]
- },
- "class_type": "IPAdapter",
- "_meta": {
- "title": "IPAdapter"
- }
- },
- "237": {
- "inputs": {
- "upload": "image"
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- },
- "239": {
- "inputs": {
- "type": "depth",
- "control_net": [
- "201",
- 0
- ]
- },
- "class_type": "SetUnionControlNetType",
- "_meta": {
- "title": "SetUnionControlNetType"
- }
- },
- "247": {
- "inputs": {
- "stop_at_clip_layer": -1,
- "clip": [
- "",
- 1
- ]
- },
- "class_type": "CLIPSetLastLayer",
- "_meta": {
- "title": "CLIP Set Last Layer"
- }
- }
-}
-"""
-
-prompt_text_flux = """ {
- "6": {
- "inputs": {
- "text": "a monkey",
- "clip": [
- "11",
- 0
- ]
- },
- "class_type": "CLIPTextEncode",
- "_meta": {
- "title": "CLIP Text Encode (Positive Prompt)"
- }
- },
- "8": {
- "inputs": {
- "samples": [
- "13",
- 0
- ],
- "vae": [
- "10",
- 0
- ]
- },
- "class_type": "VAEDecode",
- "_meta": {
- "title": "VAE Decode"
- }
- },
- "10": {
- "inputs": {
- "vae_name": "ae.sft"
- },
- "class_type": "VAELoader",
- "_meta": {
- "title": "Load VAE"
- }
- },
- "11": {
- "inputs": {
- "clip_name1": "t5xxl_fp8_e4m3fn.safetensors",
- "clip_name2": "clip_l.safetensors",
- "type": "flux",
- "device": "default"
- },
- "class_type": "DualCLIPLoader",
- "_meta": {
- "title": "DualCLIPLoader"
- }
- },
- "12": {
- "inputs": {
- "unet_name": "flux1-dev.sft",
- "weight_dtype": "default"
- },
- "class_type": "UNETLoader",
- "_meta": {
- "title": "Load Diffusion Model"
- }
- },
- "13": {
- "inputs": {
- "noise": [
- "25",
- 0
- ],
- "guider": [
- "22",
- 0
- ],
- "sampler": [
- "16",
- 0
- ],
- "sigmas": [
- "17",
- 0
- ],
- "latent_image": [
- "30",
- 0
- ]
- },
- "class_type": "SamplerCustomAdvanced",
- "_meta": {
- "title": "SamplerCustomAdvanced"
- }
- },
- "16": {
- "inputs": {
- "sampler_name": "euler"
- },
- "class_type": "KSamplerSelect",
- "_meta": {
- "title": "KSamplerSelect"
- }
- },
- "17": {
- "inputs": {
- "scheduler": "simple",
- "steps": 21,
- "denoise": 1,
- "model": [
- "12",
- 0
- ]
- },
- "class_type": "BasicScheduler",
- "_meta": {
- "title": "BasicScheduler"
- }
- },
- "22": {
- "inputs": {
- "model": [
- "12",
- 0
- ],
- "conditioning": [
- "26",
- 0
- ]
- },
- "class_type": "BasicGuider",
- "_meta": {
- "title": "BasicGuider"
- }
- },
- "25": {
- "inputs": {
- "noise_seed": 42
- },
- "class_type": "RandomNoise",
- "_meta": {
- "title": "RandomNoise"
- }
- },
- "26": {
- "inputs": {
- "guidance": 40,
- "conditioning": [
- "6",
- 0
- ]
- },
- "class_type": "FluxGuidance",
- "_meta": {
- "title": "FluxGuidance"
- }
- },
- "30": {
- "inputs": {
- "width": 1024,
- "height": 1024,
- "batch_size": 1
- },
- "class_type": "EmptyLatentImage",
- "_meta": {
- "title": "Empty Latent Image"
- }
- },
- "32": {
- "inputs": {
- "images": [
- "8",
- 0
- ]
- },
- "class_type": "SaveImageWebsocket",
- "_meta": {
- "title": "SaveImageWebsocket"
- }
- },
- "239": {
- "inputs": {
- "type": "",
- "control_net": [
- "201",
- 0
- ]
- },
- "class_type": "SetUnionControlNetType",
- "_meta": {
- "title": "SetUnionControlNetType"
- }
- }
-}
-"""
-
-prompt_text_img2img_flux = """ {
- "1": {
- "inputs": {
- "image": "",
- "upload": "image"
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- },
- "6": {
- "inputs": {
- "text": "a monkey",
- "clip": [
- "11",
- 0
- ]
- },
- "class_type": "CLIPTextEncode",
- "_meta": {
- "title": "CLIP Text Encode (Positive Prompt)"
- }
- },
- "8": {
- "inputs": {
- "samples": [
- "13",
- 0
- ],
- "vae": [
- "10",
- 0
- ]
- },
- "class_type": "VAEDecode",
- "_meta": {
- "title": "VAE Decode"
- }
- },
- "10": {
- "inputs": {
- "vae_name": "ae.sft"
- },
- "class_type": "VAELoader",
- "_meta": {
- "title": "Load VAE"
- }
- },
- "11": {
- "inputs": {
- "clip_name1": "t5xxl_fp8_e4m3fn.safetensors",
- "clip_name2": "clip_l.safetensors",
- "type": "flux",
- "device": "default"
- },
- "class_type": "DualCLIPLoader",
- "_meta": {
- "title": "DualCLIPLoader"
- }
- },
- "12": {
- "inputs": {
- "unet_name": "flux1-dev.sft",
- "weight_dtype": "default"
- },
- "class_type": "UNETLoader",
- "_meta": {
- "title": "Load Diffusion Model"
- }
- },
- "13": {
- "inputs": {
- "noise": [
- "25",
- 0
- ],
- "guider": [
- "22",
- 0
- ],
- "sampler": [
- "16",
- 0
- ],
- "sigmas": [
- "17",
- 0
- ],
- "latent_image": [
- "116",
- 0
- ]
- },
- "class_type": "SamplerCustomAdvanced",
- "_meta": {
- "title": "SamplerCustomAdvanced"
- }
- },
- "16": {
- "inputs": {
- "sampler_name": "euler"
- },
- "class_type": "KSamplerSelect",
- "_meta": {
- "title": "KSamplerSelect"
- }
- },
- "17": {
- "inputs": {
- "scheduler": "simple",
- "steps": 21,
- "denoise": 1,
- "model": [
- "12",
- 0
- ]
- },
- "class_type": "BasicScheduler",
- "_meta": {
- "title": "BasicScheduler"
- }
- },
- "22": {
- "inputs": {
- "model": [
- "12",
- 0
- ],
- "conditioning": [
- "26",
- 0
- ]
- },
- "class_type": "BasicGuider",
- "_meta": {
- "title": "BasicGuider"
- }
- },
- "25": {
- "inputs": {
- "noise_seed": 42
- },
- "class_type": "RandomNoise",
- "_meta": {
- "title": "RandomNoise"
- }
- },
- "26": {
- "inputs": {
- "guidance": 40,
- "conditioning": [
- "6",
- 0
- ]
- },
- "class_type": "FluxGuidance",
- "_meta": {
- "title": "FluxGuidance"
- }
- },
- "30": {
- "inputs": {
- "width": 1024,
- "height": 1024,
- "batch_size": 1
- },
- "class_type": "EmptyLatentImage",
- "_meta": {
- "title": "Empty Latent Image"
- }
- },
- "32": {
- "inputs": {
- "images": [
- "8",
- 0
- ]
- },
- "class_type": "SaveImageWebsocket",
- "_meta": {
- "title": "SaveImageWebsocket"
- }
- },
- "42": {
- "inputs": {
- "image": "",
- "upload": "image"
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- },
- "43": {
- "inputs": {
- "upscale_method": "nearest-exact",
- "width": 1024,
- "height": 1024,
- "crop": "disabled",
- "image": [
- "1",
- 0
- ]
- },
- "class_type": "ImageScale",
- "_meta": {
- "title": "Upscale Image"
- }
- },
- "44": {
- "inputs": {
- "grow_mask_by": 0,
- "pixels": [
- "43",
- 0
- ],
- "vae": [
- "10",
- 0
- ],
- "mask": [
- "45",
- 0
- ]
- },
- "class_type": "VAEEncodeForInpaint",
- "_meta": {
- "title": "VAE Encode (for Inpainting)"
- }
- },
- "45": {
- "inputs": {
- "channel": "red",
- "image": [
- "42",
- 0
- ]
- },
- "class_type": "ImageToMask",
- "_meta": {
- "title": "Convert Image to Mask"
- }
- },
- "50": {
- "inputs": {
- "model": [
- "12",
- 0
- ]
- },
- "class_type": "DifferentialDiffusion",
- "_meta": {
- "title": "Differential Diffusion"
- }
- },
- "51": {
- "inputs": {
- "noise_mask": true,
- "positive": [
- "6",
- 0
- ],
- "negative": [
- "6",
- 0
- ],
- "vae": [
- "10",
- 0
- ],
- "pixels": [
- "1",
- 0
- ],
- "mask": [
- "227",
- 0
- ]
- },
- "class_type": "InpaintModelConditioning",
- "_meta": {
- "title": "InpaintModelConditioning"
- }
- },
- "116": {
- "inputs": {
- "pixels": [
- "118",
- 0
- ],
- "vae": [
- "10",
- 0
- ]
- },
- "class_type": "VAEEncode",
- "_meta": {
- "title": "VAE Encode"
- }
- },
- "117": {
- "inputs": {
- "image": "",
- "upload": "image"
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- },
- "118": {
- "inputs": {
- "upscale_method": "lanczos",
- "width": 713,
- "height": 1080,
- "crop": "disabled",
- "image": [
- "117",
- 0
- ]
- },
- "class_type": "ImageScale",
- "_meta": {
- "title": "Upscale Image"
- }
- },
- "224": {
- "inputs": {
- "expand": 0,
- "tapered_corners": true,
- "mask": [
- "45",
- 0
- ]
- },
- "class_type": "GrowMask",
- "_meta": {
- "title": "GrowMask"
- }
- },
- "225": {
- "inputs": {
- "mask": [
- "224",
- 0
- ]
- },
- "class_type": "MaskToImage",
- "_meta": {
- "title": "Convert Mask to Image"
- }
- },
- "226": {
- "inputs": {
- "blur_radius": 1,
- "sigma": 1,
- "image": [
- "225",
- 0
- ]
- },
- "class_type": "ImageBlur",
- "_meta": {
- "title": "Image Blur"
- }
- },
- "227": {
- "inputs": {
- "channel": "red",
- "image": [
- "226",
- 0
- ]
- },
- "class_type": "ImageToMask",
- "_meta": {
- "title": "Convert Image to Mask"
- }
- },
- "239": {
- "inputs": {
- "type": "",
- "control_net": [
- "201",
- 0
- ]
- },
- "class_type": "SetUnionControlNetType",
- "_meta": {
- "title": "SetUnionControlNetType"
- }
- }
-}
-"""
-
-ipadapter_flux = """
-{
- "242": {
- "inputs": {
- "ipadapter": "ip-adapter.bin",
- "clip_vision": "google/siglip-so400m-patch14-384",
- "provider": "cuda"
- },
- "class_type": "IPAdapterFluxLoader",
- "_meta": {
- "title": "Load IPAdapter Flux Model"
- }
- },
- "243": {
- "inputs": {
- "weight": 1,
- "start_percent": 0,
- "end_percent": 1,
- "model": [
- "12",
- 0
- ],
- "ipadapter_flux": [
- "242",
- 0
- ],
- "image": [
- "244",
- 0
- ]
- },
- "class_type": "ApplyIPAdapterFlux",
- "_meta": {
- "title": "Apply IPAdapter Flux Model"
- }
- },
- "244": {
- "inputs": {
- "image": ""
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- }
-}
-"""
-
-depth_lora_flux = """
-{
- "245": {
- "inputs": {
- "image": ""
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- },
- "246": {
- "inputs": {
- "positive": [
- "6",
- 0
- ],
- "negative": [
- "6",
- 0
- ],
- "vae": [
- "10",
- 0
- ],
- "pixels": [
- "245",
- 0
- ]
- },
- "class_type": "InstructPixToPixConditioning",
- "_meta": {
- "title": "InstructPixToPixConditioning"
- }
- },
- "247": {
- "inputs": {
- "lora_name": "flux1-depth-dev-lora.safetensors",
- "strength_model": 1,
- "model": [
- "12",
- 0
- ]
- },
- "class_type": "LoraLoaderModelOnly",
- "_meta": {
- "title": "LoraLoaderModelOnly"
- }
- }
-}
-"""
-
-gguf_unet_loader = """
-{
- "12": {
- "inputs": {
- "unet_name": ""
- },
- "class_type": "UnetLoaderGGUF",
- "_meta": {
- "title": "Unet Loader (GGUF)"
- }
- }
-}
-"""
-
-prompt_text_qwen_image_edit = """
-{
- "1": {
- "inputs": {
- "seed": 157165768450326,
- "steps": 4,
- "cfg": 1,
- "sampler_name": "euler",
- "scheduler": "simple",
- "denoise": 1,
- "model": [
- "7",
- 0
- ],
- "positive": [
- "12",
- 0
- ],
- "negative": [
- "11",
- 0
- ],
- "latent_image": [
- "8",
- 0
- ]
- },
- "class_type": "KSampler",
- "_meta": {
- "title": "KSampler"
- }
- },
- "2": {
- "inputs": {
- "samples": [
- "1",
- 0
- ],
- "vae": [
- "4",
- 0
- ]
- },
- "class_type": "VAEDecode",
- "_meta": {
- "title": "VAE Decode"
- }
- },
- "3": {
- "inputs": {
- "clip_name": "qwen_2.5_vl_7b_fp8_scaled.safetensors",
- "type": "qwen_image",
- "device": "default"
- },
- "class_type": "CLIPLoader",
- "_meta": {
- "title": "Load CLIP"
- }
- },
- "4": {
- "inputs": {
- "vae_name": "qwen_image_vae.safetensors"
- },
- "class_type": "VAELoader",
- "_meta": {
- "title": "Load VAE"
- }
- },
- "5": {
- "inputs": {
- "images": [
- "2",
- 0
- ]
- },
- "class_type": "SaveImageWebsocket",
- "_meta": {
- "title": "SaveImageWebsocket"
- }
- },
- "6": {
- "inputs": {
- "shift": 3,
- "model": [
- "9",
- 0
- ]
- },
- "class_type": "ModelSamplingAuraFlow",
- "_meta": {
- "title": "ModelSamplingAuraFlow"
- }
- },
- "7": {
- "inputs": {
- "strength": 1,
- "model": [
- "6",
- 0
- ]
- },
- "class_type": "CFGNorm",
- "_meta": {
- "title": "CFGNorm"
- }
- },
- "8": {
- "inputs": {
- "pixels": [
- "14",
- 0
- ],
- "vae": [
- "4",
- 0
- ]
- },
- "class_type": "VAEEncode",
- "_meta": {
- "title": "VAE Encode"
- }
- },
- "11": {
- "inputs": {
- "prompt": "",
- "clip": [
- "3",
- 0
- ],
- "image1": [
- "14",
- 0
- ],
- "image2": [
- "15",
- 0
- ],
- "image3": [
- "16",
- 0
- ]
- },
- "class_type": "TextEncodeQwenImageEditPlus",
- "_meta": {
- "title": "TextEncodeQwenImageEditPlus"
- }
- },
- "12": {
- "inputs": {
- "prompt": "a monkey",
- "clip": [
- "3",
- 0
- ],
- "image1": [
- "14",
- 0
- ],
- "image2": [
- "15",
- 0
- ],
- "image3": [
- "16",
- 0
- ]
- },
- "class_type": "TextEncodeQwenImageEditPlus",
- "_meta": {
- "title": "TextEncodeQwenImageEditPlus"
- }
- },
- "13": {
- "inputs": {
- "unet_name": "Qwen-Image-Edit-2509-Q3_K_M.gguf"
- },
- "class_type": "UnetLoaderGGUF",
- "_meta": {
- "title": "Unet Loader (GGUF)"
- }
- },
- "14": {
- "inputs": {
- "image": ""
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- },
- "15": {
- "inputs": {
- "image": ""
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- },
- "16": {
- "inputs": {
- "image": ""
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Image"
- }
- }
-}
-"""
-
-# ---------------------------------------------------------------------------
-# FLUX.2 Klein – multi-reference edit model (Apache 2.0)
-# Based on official ComfyUI Klein image-edit workflow.
-# Nodes: all built-in ComfyUI core (no custom nodes needed)
-# UNETLoader, CLIPLoader (type=flux2, Qwen 3 4B), VAELoader,
-# CLIPTextEncode (x2), EmptyFlux2LatentImage, Flux2Scheduler,
-# KSamplerSelect, RandomNoise, CFGGuider (cfg=1), SamplerCustomAdvanced,
-# VAEDecode, SaveImageWebsocket, LoadImage (x3 reference slots).
-# Reference images are wired dynamically via:
-# VAEEncode → ReferenceLatent (pos + neg chain) → CFGGuider.
-# ---------------------------------------------------------------------------
-prompt_text_flux2_klein = """
-{
- "1": {
- "inputs": {
- "unet_name": "flux-2-klein-base-4b-fp8.safetensors",
- "weight_dtype": "default"
- },
- "class_type": "UNETLoader",
- "_meta": { "title": "Load Diffusion Model (Klein)" }
- },
- "2": {
- "inputs": {
- "clip_name": "qwen_3_4b.safetensors",
- "type": "flux2"
- },
- "class_type": "CLIPLoader",
- "_meta": { "title": "Load CLIP (Qwen 3 4B)" }
- },
- "3": {
- "inputs": {
- "vae_name": "flux2-vae.safetensors"
- },
- "class_type": "VAELoader",
- "_meta": { "title": "Load VAE (FLUX.2)" }
- },
- "4": {
- "inputs": {
- "text": "",
- "clip": ["2", 0]
- },
- "class_type": "CLIPTextEncode",
- "_meta": { "title": "CLIP Text Encode (Positive)" }
- },
- "5": {
- "inputs": {
- "text": "",
- "clip": ["2", 0]
- },
- "class_type": "CLIPTextEncode",
- "_meta": { "title": "CLIP Text Encode (Negative)" }
- },
- "6": {
- "inputs": {
- "width": 1024,
- "height": 1024,
- "batch_size": 1
- },
- "class_type": "EmptyFlux2LatentImage",
- "_meta": { "title": "Empty Flux 2 Latent" }
- },
- "7": {
- "inputs": {
- "steps": 20,
- "width": 1024,
- "height": 1024
- },
- "class_type": "Flux2Scheduler",
- "_meta": { "title": "Flux 2 Scheduler" }
- },
- "8": {
- "inputs": {
- "sampler_name": "euler"
- },
- "class_type": "KSamplerSelect",
- "_meta": { "title": "KSampler Select" }
- },
- "9": {
- "inputs": {
- "noise_seed": 0
- },
- "class_type": "RandomNoise",
- "_meta": { "title": "Random Noise" }
- },
- "10": {
- "inputs": {
- "model": ["1", 0],
- "positive": ["4", 0],
- "negative": ["5", 0],
- "cfg": 1
- },
- "class_type": "CFGGuider",
- "_meta": { "title": "CFG Guider" }
- },
- "11": {
- "inputs": {
- "noise": ["9", 0],
- "guider": ["10", 0],
- "sampler": ["8", 0],
- "sigmas": ["7", 0],
- "latent_image": ["6", 0]
- },
- "class_type": "SamplerCustomAdvanced",
- "_meta": { "title": "Sampler Custom Advanced" }
- },
- "12": {
- "inputs": {
- "samples": ["11", 0],
- "vae": ["3", 0]
- },
- "class_type": "VAEDecode",
- "_meta": { "title": "VAE Decode" }
- },
- "13": {
- "inputs": {
- "images": ["12", 0]
- },
- "class_type": "SaveImageWebsocket",
- "_meta": { "title": "SaveImageWebsocket" }
- },
- "14": {
- "inputs": {
- "image": ""
- },
- "class_type": "LoadImage",
- "_meta": { "title": "Load Reference Image 1 (Structure)" }
- },
- "15": {
- "inputs": {
- "image": ""
- },
- "class_type": "LoadImage",
- "_meta": { "title": "Load Reference Image 2 (Style)" }
- },
- "16": {
- "inputs": {
- "image": ""
- },
- "class_type": "LoadImage",
- "_meta": { "title": "Load Reference Image 3 (Context)" }
- }
-}
-"""
-
-# Workflow for TRELLIS.2 Image-to-3D generation via PozzettiAndrea/ComfyUI-TRELLIS2 nodes
-# Pipeline: LoadImage → LoadModels → RemoveBackground → GetConditioning → ImageToShape → ShapeToTexturedMesh → ExportGLB
-prompt_text_trellis2 = """
-{
- "1": {
- "inputs": {
- "image": "",
- "upload": "image"
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Input Image"
- }
- },
- "2": {
- "inputs": {
- "resolution": "1024_cascade",
- "attn_backend": "flash_attn",
- "vram_mode": "disk_offload"
- },
- "class_type": "LoadTrellis2Models",
- "_meta": {
- "title": "Load TRELLIS.2 Models"
- }
- },
- "3": {
- "inputs": {
- "image": ["1", 0],
- "low_vram": true
- },
- "class_type": "Trellis2RemoveBackground",
- "_meta": {
- "title": "Remove Background"
- }
- },
- "4": {
- "inputs": {
- "model_config": ["2", 0],
- "image": ["3", 0],
- "mask": ["3", 1],
- "include_1024": true,
- "background_color": "black"
- },
- "class_type": "Trellis2GetConditioning",
- "_meta": {
- "title": "Get Conditioning"
- }
- },
- "5": {
- "inputs": {
- "model_config": ["2", 0],
- "conditioning": ["4", 0],
- "seed": 0,
- "ss_guidance_strength": 7.5,
- "ss_sampling_steps": 12,
- "shape_guidance_strength": 7.5,
- "shape_sampling_steps": 12,
- "max_tokens": 49152
- },
- "class_type": "Trellis2ImageToShape",
- "_meta": {
- "title": "Image to Shape"
- }
- },
- "6": {
- "inputs": {
- "model_config": ["2", 0],
- "conditioning": ["4", 0],
- "shape_result": ["5", 0],
- "seed": 0,
- "tex_guidance_strength": 7.5,
- "tex_sampling_steps": 12
- },
- "class_type": "Trellis2ShapeToTexturedMesh",
- "_meta": {
- "title": "Shape to Textured Mesh"
- }
- },
- "7": {
- "inputs": {
- "voxelgrid_path": ["6", 1],
- "decimation_target": 100000,
- "texture_size": 4096,
- "remesh": true,
- "filename_prefix": "trellis2"
- },
- "class_type": "Trellis2ExportGLB",
- "_meta": {
- "title": "Export GLB"
- }
- }
-}
-"""
-
-prompt_text_trellis2_shape_only = """
-{
- "1": {
- "inputs": {
- "image": "",
- "upload": "image"
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": "Load Input Image"
- }
- },
- "2": {
- "inputs": {
- "resolution": "1024_cascade",
- "attn_backend": "flash_attn",
- "vram_mode": "disk_offload"
- },
- "class_type": "LoadTrellis2Models",
- "_meta": {
- "title": "Load TRELLIS.2 Models"
- }
- },
- "3": {
- "inputs": {
- "image": ["1", 0],
- "low_vram": true
- },
- "class_type": "Trellis2RemoveBackground",
- "_meta": {
- "title": "Remove Background"
- }
- },
- "4": {
- "inputs": {
- "model_config": ["2", 0],
- "image": ["3", 0],
- "mask": ["3", 1],
- "include_1024": true,
- "background_color": "black"
- },
- "class_type": "Trellis2GetConditioning",
- "_meta": {
- "title": "Get Conditioning"
- }
- },
- "5": {
- "inputs": {
- "model_config": ["2", 0],
- "conditioning": ["4", 0],
- "seed": 0,
- "ss_guidance_strength": 7.5,
- "ss_sampling_steps": 12,
- "shape_guidance_strength": 7.5,
- "shape_sampling_steps": 12,
- "max_tokens": 49152
- },
- "class_type": "Trellis2ImageToShape",
- "_meta": {
- "title": "Image to Shape"
- }
- },
- "6": {
- "inputs": {
- "trimesh": ["5", 1],
- "target_face_count": 100000,
- "fill_holes": false,
- "remesh": false,
- "remesh_band": 1.0
- },
- "class_type": "Trellis2Simplify",
- "_meta": {
- "title": "Simplify Mesh"
- }
- },
- "7": {
- "inputs": {
- "trimesh": ["6", 0],
- "filename_prefix": "trellis2",
- "file_format": "glb"
- },
- "class_type": "Trellis2ExportTrimesh",
- "_meta": {
- "title": "Export Trimesh"
- }
- }
-}
-"""
-
+"""helpers.py - backward-compatibility shim.
+
+Canonical location: stablegen/util/workflow_templates.py
+"""
+from .workflow_templates import ( # noqa: F401
+ prompt_text,
+ prompt_text_img2img,
+ prompt_text_flux,
+ prompt_text_img2img_flux,
+ ipadapter_flux,
+ depth_lora_flux,
+ gguf_unet_loader,
+ prompt_text_qwen_image_edit,
+ prompt_text_flux2_klein,
+ prompt_text_trellis2,
+ prompt_text_trellis2_shape_only,
+)
diff --git a/stablegen/util/mirror_color.py b/stablegen/util/mirror_color.py
index da95c35..7e48d89 100644
--- a/stablegen/util/mirror_color.py
+++ b/stablegen/util/mirror_color.py
@@ -6,8 +6,8 @@ import mathutils
from mathutils import Vector
from ..utils import get_file_path, get_last_material_index, sg_modal_active
-from ..project import project_image
-from ..color_match import color_match_single
+from ..texturing.projection import project_image
+from .color_match import color_match_single
def _build_mirrored_camera_matrix(
cam: bpy.types.Object,
@@ -351,7 +351,7 @@ class MirrorReproject(bpy.types.Operator):
def _schedule_delete_camera_later(self, cam_name: str):
"""Delete the temporary camera after StableGen reprojection finishes."""
import bpy
- from ..generator import ComfyUIGenerate
+ from ..texturing.generator import ComfyUIGenerate
def _delete():
# Wait until the generate/reproject operator is done
diff --git a/stablegen/util/workflow_templates.py b/stablegen/util/workflow_templates.py
new file mode 100644
index 0000000..f01983f
--- /dev/null
+++ b/stablegen/util/workflow_templates.py
@@ -0,0 +1,1822 @@
+import random
+
+# Prompt for ComfyUI in API format (SDXL)
+prompt_text = """
+ {
+ "5": {
+ "inputs": {
+ "image": "",
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ },
+ "6": {
+ "inputs": {
+ "ckpt_name": "sd_xl_base_1.0.safetensors"
+ },
+ "class_type": "CheckpointLoaderSimple",
+ "_meta": {
+ "title": "Load Checkpoint"
+ }
+ },
+ "9": {
+ "inputs": {
+ "text": "a monkey",
+ "clip": [
+ "247",
+ 0
+ ]
+ },
+ "class_type": "CLIPTextEncode",
+ "_meta": {
+ "title": "CLIP Text Encode (Prompt)"
+ }
+ },
+ "10": {
+ "inputs": {
+ "text": "",
+ "clip": [
+ "247",
+ 0
+ ]
+ },
+ "class_type": "CLIPTextEncode",
+ "_meta": {
+ "title": "CLIP Text Encode (Prompt)"
+ }
+ },
+ "15": {
+ "inputs": {
+ "seed": 24,
+ "steps": 8,
+ "cfg": 1.5,
+ "sampler_name": "dpmpp_2s_ancestral",
+ "scheduler": "sgm_uniform",
+ "denoise": 1,
+ "model": [
+ "26",
+ 0
+ ],
+ "positive": [
+ "14",
+ 0
+ ],
+ "negative": [
+ "14",
+ 1
+ ],
+ "latent_image": [
+ "16",
+ 0
+ ]
+ },
+ "class_type": "KSampler",
+ "_meta": {
+ "title": "KSampler"
+ }
+ },
+ "16": {
+ "inputs": {
+ "width": 1024,
+ "height": 1024,
+ "batch_size": 1
+ },
+ "class_type": "EmptyLatentImage",
+ "_meta": {
+ "title": "Empty Latent Image"
+ }
+ },
+ "19": {
+ "inputs": {
+ "samples": [
+ "15",
+ 0
+ ],
+ "vae": [
+ "6",
+ 2
+ ]
+ },
+ "class_type": "VAEDecode",
+ "_meta": {
+ "title": "VAE Decode"
+ }
+ },
+ "25": {
+ "inputs": {
+ "images": [
+ "19",
+ 0
+ ]
+ },
+ "class_type": "SaveImageWebsocket",
+ "_meta": {
+ "title": "SaveImageWebsocket"
+ }
+ },
+ "27": {
+ "inputs": {
+ "image": "",
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ },
+ "239": {
+ "inputs": {
+ "type": "depth",
+ "control_net": [
+ "201",
+ 0
+ ]
+ },
+ "class_type": "SetUnionControlNetType",
+ "_meta": {
+ "title": "SetUnionControlNetType"
+ }
+ },
+ "247": {
+ "inputs": {
+ "stop_at_clip_layer": -1,
+ "clip": [
+ "",
+ 1
+ ]
+ },
+ "class_type": "CLIPSetLastLayer",
+ "_meta": {
+ "title": "CLIP Set Last Layer"
+ }
+ },
+ "235": {
+ "inputs": {
+ "preset": "PLUS (high strength)",
+ "model": [
+ "",
+ 0
+ ]
+ },
+ "class_type": "IPAdapterUnifiedLoader",
+ "_meta": {
+ "title": "IPAdapter Unified Loader"
+ }
+ },
+ "236": {
+ "inputs": {
+ "weight": 1,
+ "start_at": 0,
+ "end_at": 1,
+ "weight_type": "standard",
+ "model": [
+ "235",
+ 0
+ ],
+ "ipadapter": [
+ "235",
+ 1
+ ],
+ "image": ["237", 0]
+ },
+ "class_type": "IPAdapter",
+ "_meta": {
+ "title": "IPAdapter"
+ }
+ },
+ "237": {
+ "inputs": {
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ }
+ }
+ """
+
+prompt_text_img2img = """
+{
+ "1": {
+ "inputs": {
+ "image": "",
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ },
+ "12": {
+ "inputs": {
+ "image": "",
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ },
+ "23": {
+ "inputs": {
+ "upscale_method": "nearest-exact",
+ "width": 1024,
+ "height": 1024,
+ "crop": "disabled",
+ "image": [
+ "1",
+ 0
+ ]
+ },
+ "class_type": "ImageScale",
+ "_meta": {
+ "title": "Upscale Image"
+ }
+ },
+ "13": {
+ "inputs": {
+ "grow_mask_by": 0,
+ "pixels": [
+ "23",
+ 0
+ ],
+ "vae": [
+ "38",
+ 2
+ ],
+ "mask": [
+ "25",
+ 0
+ ]
+ },
+ "class_type": "VAEEncodeForInpaint",
+ "_meta": {
+ "title": "VAE Encode (for Inpainting)"
+ }
+ },
+ "25": {
+ "inputs": {
+ "channel": "red",
+ "image": [
+ "12",
+ 0
+ ]
+ },
+ "class_type": "ImageToMask",
+ "_meta": {
+ "title": "Convert Image to Mask"
+ }
+ },
+ "38": {
+ "inputs": {
+ "ckpt_name": "realvisxlV40_v40Bakedvae.safetensors"
+ },
+ "class_type": "CheckpointLoaderSimple",
+ "_meta": {
+ "title": "Load Checkpoint"
+ }
+ },
+ "102": {
+ "inputs": {
+ "text": "tifa lockhart, cosplay, final fantasy",
+ "clip": [
+ "247",
+ 0
+ ]
+ },
+ "class_type": "CLIPTextEncode",
+ "_meta": {
+ "title": "CLIP Text Encode (Prompt)"
+ }
+ },
+ "103": {
+ "inputs": {
+ "text": "",
+ "clip": [
+ "247",
+ 0
+ ]
+ },
+ "class_type": "CLIPTextEncode",
+ "_meta": {
+ "title": "CLIP Text Encode (Prompt)"
+ }
+ },
+ "105": {
+ "inputs": {
+ "seed": 42,
+ "steps": 8,
+ "cfg": 1.48,
+ "sampler_name": "dpmpp_sde",
+ "scheduler": "sgm_uniform",
+ "denoise": 0.5,
+ "model": [
+ "",
+ 0
+ ],
+ "positive": [
+ "104",
+ 0
+ ],
+ "negative": [
+ "104",
+ 1
+ ],
+ "latent_image": [
+ "116",
+ 0
+ ]
+ },
+ "class_type": "KSampler",
+ "_meta": {
+ "title": "KSampler"
+ }
+ },
+ "108": {
+ "inputs": {
+ "image": "",
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ },
+ "110": {
+ "inputs": {
+ "samples": [
+ "105",
+ 0
+ ],
+ "vae": [
+ "38",
+ 2
+ ]
+ },
+ "class_type": "VAEDecode",
+ "_meta": {
+ "title": "VAE Decode"
+ }
+ },
+ "111": {
+ "inputs": {
+ "images": [
+ "110",
+ 0
+ ]
+ },
+ "class_type": "SaveImageWebsocket",
+ "_meta": {
+ "title": "SaveImageWebsocket"
+ }
+ },
+ "116": {
+ "inputs": {
+ "pixels": [
+ "118",
+ 0
+ ],
+ "vae": [
+ "38",
+ 2
+ ]
+ },
+ "class_type": "VAEEncode",
+ "_meta": {
+ "title": "VAE Encode"
+ }
+ },
+ "117": {
+ "inputs": {
+ "image": "",
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ },
+ "118": {
+ "inputs": {
+ "upscale_method": "lanczos",
+ "width": 713,
+ "height": 1080,
+ "crop": "disabled",
+ "image": [
+ "117",
+ 0
+ ]
+ },
+ "class_type": "ImageScale",
+ "_meta": {
+ "title": "Upscale Image"
+ }
+ },
+ "224": {
+ "inputs": {
+ "expand": 0,
+ "tapered_corners": true,
+ "mask": [
+ "25",
+ 0
+ ]
+ },
+ "class_type": "GrowMask",
+ "_meta": {
+ "title": "GrowMask"
+ }
+ },
+ "225": {
+ "inputs": {
+ "mask": [
+ "224",
+ 0
+ ]
+ },
+ "class_type": "MaskToImage",
+ "_meta": {
+ "title": "Convert Mask to Image"
+ }
+ },
+ "226": {
+ "inputs": {
+ "blur_radius": 1,
+ "sigma": 1,
+ "image": [
+ "225",
+ 0
+ ]
+ },
+ "class_type": "ImageBlur",
+ "_meta": {
+ "title": "Image Blur"
+ }
+ },
+ "227": {
+ "inputs": {
+ "channel": "red",
+ "image": [
+ "226",
+ 0
+ ]
+ },
+ "class_type": "ImageToMask",
+ "_meta": {
+ "title": "Convert Image to Mask"
+ }
+ },
+ "228": {
+ "inputs": {
+ "noise_mask": true,
+ "positive": [
+ "102",
+ 0
+ ],
+ "negative": [
+ "103",
+ 0
+ ],
+ "vae": [
+ "38",
+ 2
+ ],
+ "pixels": [
+ "1",
+ 0
+ ],
+ "mask": [
+ "227",
+ 0
+ ]
+ },
+ "class_type": "InpaintModelConditioning",
+ "_meta": {
+ "title": "InpaintModelConditioning"
+ }
+ },
+ "229": {
+ "inputs": {
+ "model": [
+ "",
+ 0
+ ]
+ },
+ "class_type": "DifferentialDiffusion",
+ "_meta": {
+ "title": "Differential Diffusion"
+ }
+ },
+ "235": {
+ "inputs": {
+ "preset": "PLUS (high strength)",
+ "model": [
+ "",
+ 0
+ ]
+ },
+ "class_type": "IPAdapterUnifiedLoader",
+ "_meta": {
+ "title": "IPAdapter Unified Loader"
+ }
+ },
+ "236": {
+ "inputs": {
+ "weight": 1,
+ "start_at": 0,
+ "end_at": 1,
+ "weight_type": "standard",
+ "model": [
+ "235",
+ 0
+ ],
+ "ipadapter": [
+ "235",
+ 1
+ ],
+ "image": ["1", 0]
+ },
+ "class_type": "IPAdapter",
+ "_meta": {
+ "title": "IPAdapter"
+ }
+ },
+ "237": {
+ "inputs": {
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ },
+ "239": {
+ "inputs": {
+ "type": "depth",
+ "control_net": [
+ "201",
+ 0
+ ]
+ },
+ "class_type": "SetUnionControlNetType",
+ "_meta": {
+ "title": "SetUnionControlNetType"
+ }
+ },
+ "247": {
+ "inputs": {
+ "stop_at_clip_layer": -1,
+ "clip": [
+ "",
+ 1
+ ]
+ },
+ "class_type": "CLIPSetLastLayer",
+ "_meta": {
+ "title": "CLIP Set Last Layer"
+ }
+ }
+}
+"""
+
+prompt_text_flux = """ {
+ "6": {
+ "inputs": {
+ "text": "a monkey",
+ "clip": [
+ "11",
+ 0
+ ]
+ },
+ "class_type": "CLIPTextEncode",
+ "_meta": {
+ "title": "CLIP Text Encode (Positive Prompt)"
+ }
+ },
+ "8": {
+ "inputs": {
+ "samples": [
+ "13",
+ 0
+ ],
+ "vae": [
+ "10",
+ 0
+ ]
+ },
+ "class_type": "VAEDecode",
+ "_meta": {
+ "title": "VAE Decode"
+ }
+ },
+ "10": {
+ "inputs": {
+ "vae_name": "ae.sft"
+ },
+ "class_type": "VAELoader",
+ "_meta": {
+ "title": "Load VAE"
+ }
+ },
+ "11": {
+ "inputs": {
+ "clip_name1": "t5xxl_fp8_e4m3fn.safetensors",
+ "clip_name2": "clip_l.safetensors",
+ "type": "flux",
+ "device": "default"
+ },
+ "class_type": "DualCLIPLoader",
+ "_meta": {
+ "title": "DualCLIPLoader"
+ }
+ },
+ "12": {
+ "inputs": {
+ "unet_name": "flux1-dev.sft",
+ "weight_dtype": "default"
+ },
+ "class_type": "UNETLoader",
+ "_meta": {
+ "title": "Load Diffusion Model"
+ }
+ },
+ "13": {
+ "inputs": {
+ "noise": [
+ "25",
+ 0
+ ],
+ "guider": [
+ "22",
+ 0
+ ],
+ "sampler": [
+ "16",
+ 0
+ ],
+ "sigmas": [
+ "17",
+ 0
+ ],
+ "latent_image": [
+ "30",
+ 0
+ ]
+ },
+ "class_type": "SamplerCustomAdvanced",
+ "_meta": {
+ "title": "SamplerCustomAdvanced"
+ }
+ },
+ "16": {
+ "inputs": {
+ "sampler_name": "euler"
+ },
+ "class_type": "KSamplerSelect",
+ "_meta": {
+ "title": "KSamplerSelect"
+ }
+ },
+ "17": {
+ "inputs": {
+ "scheduler": "simple",
+ "steps": 21,
+ "denoise": 1,
+ "model": [
+ "12",
+ 0
+ ]
+ },
+ "class_type": "BasicScheduler",
+ "_meta": {
+ "title": "BasicScheduler"
+ }
+ },
+ "22": {
+ "inputs": {
+ "model": [
+ "12",
+ 0
+ ],
+ "conditioning": [
+ "26",
+ 0
+ ]
+ },
+ "class_type": "BasicGuider",
+ "_meta": {
+ "title": "BasicGuider"
+ }
+ },
+ "25": {
+ "inputs": {
+ "noise_seed": 42
+ },
+ "class_type": "RandomNoise",
+ "_meta": {
+ "title": "RandomNoise"
+ }
+ },
+ "26": {
+ "inputs": {
+ "guidance": 40,
+ "conditioning": [
+ "6",
+ 0
+ ]
+ },
+ "class_type": "FluxGuidance",
+ "_meta": {
+ "title": "FluxGuidance"
+ }
+ },
+ "30": {
+ "inputs": {
+ "width": 1024,
+ "height": 1024,
+ "batch_size": 1
+ },
+ "class_type": "EmptyLatentImage",
+ "_meta": {
+ "title": "Empty Latent Image"
+ }
+ },
+ "32": {
+ "inputs": {
+ "images": [
+ "8",
+ 0
+ ]
+ },
+ "class_type": "SaveImageWebsocket",
+ "_meta": {
+ "title": "SaveImageWebsocket"
+ }
+ },
+ "239": {
+ "inputs": {
+ "type": "",
+ "control_net": [
+ "201",
+ 0
+ ]
+ },
+ "class_type": "SetUnionControlNetType",
+ "_meta": {
+ "title": "SetUnionControlNetType"
+ }
+ }
+}
+"""
+
+prompt_text_img2img_flux = """ {
+ "1": {
+ "inputs": {
+ "image": "",
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ },
+ "6": {
+ "inputs": {
+ "text": "a monkey",
+ "clip": [
+ "11",
+ 0
+ ]
+ },
+ "class_type": "CLIPTextEncode",
+ "_meta": {
+ "title": "CLIP Text Encode (Positive Prompt)"
+ }
+ },
+ "8": {
+ "inputs": {
+ "samples": [
+ "13",
+ 0
+ ],
+ "vae": [
+ "10",
+ 0
+ ]
+ },
+ "class_type": "VAEDecode",
+ "_meta": {
+ "title": "VAE Decode"
+ }
+ },
+ "10": {
+ "inputs": {
+ "vae_name": "ae.sft"
+ },
+ "class_type": "VAELoader",
+ "_meta": {
+ "title": "Load VAE"
+ }
+ },
+ "11": {
+ "inputs": {
+ "clip_name1": "t5xxl_fp8_e4m3fn.safetensors",
+ "clip_name2": "clip_l.safetensors",
+ "type": "flux",
+ "device": "default"
+ },
+ "class_type": "DualCLIPLoader",
+ "_meta": {
+ "title": "DualCLIPLoader"
+ }
+ },
+ "12": {
+ "inputs": {
+ "unet_name": "flux1-dev.sft",
+ "weight_dtype": "default"
+ },
+ "class_type": "UNETLoader",
+ "_meta": {
+ "title": "Load Diffusion Model"
+ }
+ },
+ "13": {
+ "inputs": {
+ "noise": [
+ "25",
+ 0
+ ],
+ "guider": [
+ "22",
+ 0
+ ],
+ "sampler": [
+ "16",
+ 0
+ ],
+ "sigmas": [
+ "17",
+ 0
+ ],
+ "latent_image": [
+ "116",
+ 0
+ ]
+ },
+ "class_type": "SamplerCustomAdvanced",
+ "_meta": {
+ "title": "SamplerCustomAdvanced"
+ }
+ },
+ "16": {
+ "inputs": {
+ "sampler_name": "euler"
+ },
+ "class_type": "KSamplerSelect",
+ "_meta": {
+ "title": "KSamplerSelect"
+ }
+ },
+ "17": {
+ "inputs": {
+ "scheduler": "simple",
+ "steps": 21,
+ "denoise": 1,
+ "model": [
+ "12",
+ 0
+ ]
+ },
+ "class_type": "BasicScheduler",
+ "_meta": {
+ "title": "BasicScheduler"
+ }
+ },
+ "22": {
+ "inputs": {
+ "model": [
+ "12",
+ 0
+ ],
+ "conditioning": [
+ "26",
+ 0
+ ]
+ },
+ "class_type": "BasicGuider",
+ "_meta": {
+ "title": "BasicGuider"
+ }
+ },
+ "25": {
+ "inputs": {
+ "noise_seed": 42
+ },
+ "class_type": "RandomNoise",
+ "_meta": {
+ "title": "RandomNoise"
+ }
+ },
+ "26": {
+ "inputs": {
+ "guidance": 40,
+ "conditioning": [
+ "6",
+ 0
+ ]
+ },
+ "class_type": "FluxGuidance",
+ "_meta": {
+ "title": "FluxGuidance"
+ }
+ },
+ "30": {
+ "inputs": {
+ "width": 1024,
+ "height": 1024,
+ "batch_size": 1
+ },
+ "class_type": "EmptyLatentImage",
+ "_meta": {
+ "title": "Empty Latent Image"
+ }
+ },
+ "32": {
+ "inputs": {
+ "images": [
+ "8",
+ 0
+ ]
+ },
+ "class_type": "SaveImageWebsocket",
+ "_meta": {
+ "title": "SaveImageWebsocket"
+ }
+ },
+ "42": {
+ "inputs": {
+ "image": "",
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ },
+ "43": {
+ "inputs": {
+ "upscale_method": "nearest-exact",
+ "width": 1024,
+ "height": 1024,
+ "crop": "disabled",
+ "image": [
+ "1",
+ 0
+ ]
+ },
+ "class_type": "ImageScale",
+ "_meta": {
+ "title": "Upscale Image"
+ }
+ },
+ "44": {
+ "inputs": {
+ "grow_mask_by": 0,
+ "pixels": [
+ "43",
+ 0
+ ],
+ "vae": [
+ "10",
+ 0
+ ],
+ "mask": [
+ "45",
+ 0
+ ]
+ },
+ "class_type": "VAEEncodeForInpaint",
+ "_meta": {
+ "title": "VAE Encode (for Inpainting)"
+ }
+ },
+ "45": {
+ "inputs": {
+ "channel": "red",
+ "image": [
+ "42",
+ 0
+ ]
+ },
+ "class_type": "ImageToMask",
+ "_meta": {
+ "title": "Convert Image to Mask"
+ }
+ },
+ "50": {
+ "inputs": {
+ "model": [
+ "12",
+ 0
+ ]
+ },
+ "class_type": "DifferentialDiffusion",
+ "_meta": {
+ "title": "Differential Diffusion"
+ }
+ },
+ "51": {
+ "inputs": {
+ "noise_mask": true,
+ "positive": [
+ "6",
+ 0
+ ],
+ "negative": [
+ "6",
+ 0
+ ],
+ "vae": [
+ "10",
+ 0
+ ],
+ "pixels": [
+ "1",
+ 0
+ ],
+ "mask": [
+ "227",
+ 0
+ ]
+ },
+ "class_type": "InpaintModelConditioning",
+ "_meta": {
+ "title": "InpaintModelConditioning"
+ }
+ },
+ "116": {
+ "inputs": {
+ "pixels": [
+ "118",
+ 0
+ ],
+ "vae": [
+ "10",
+ 0
+ ]
+ },
+ "class_type": "VAEEncode",
+ "_meta": {
+ "title": "VAE Encode"
+ }
+ },
+ "117": {
+ "inputs": {
+ "image": "",
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ },
+ "118": {
+ "inputs": {
+ "upscale_method": "lanczos",
+ "width": 713,
+ "height": 1080,
+ "crop": "disabled",
+ "image": [
+ "117",
+ 0
+ ]
+ },
+ "class_type": "ImageScale",
+ "_meta": {
+ "title": "Upscale Image"
+ }
+ },
+ "224": {
+ "inputs": {
+ "expand": 0,
+ "tapered_corners": true,
+ "mask": [
+ "45",
+ 0
+ ]
+ },
+ "class_type": "GrowMask",
+ "_meta": {
+ "title": "GrowMask"
+ }
+ },
+ "225": {
+ "inputs": {
+ "mask": [
+ "224",
+ 0
+ ]
+ },
+ "class_type": "MaskToImage",
+ "_meta": {
+ "title": "Convert Mask to Image"
+ }
+ },
+ "226": {
+ "inputs": {
+ "blur_radius": 1,
+ "sigma": 1,
+ "image": [
+ "225",
+ 0
+ ]
+ },
+ "class_type": "ImageBlur",
+ "_meta": {
+ "title": "Image Blur"
+ }
+ },
+ "227": {
+ "inputs": {
+ "channel": "red",
+ "image": [
+ "226",
+ 0
+ ]
+ },
+ "class_type": "ImageToMask",
+ "_meta": {
+ "title": "Convert Image to Mask"
+ }
+ },
+ "239": {
+ "inputs": {
+ "type": "",
+ "control_net": [
+ "201",
+ 0
+ ]
+ },
+ "class_type": "SetUnionControlNetType",
+ "_meta": {
+ "title": "SetUnionControlNetType"
+ }
+ }
+}
+"""
+
+ipadapter_flux = """
+{
+ "242": {
+ "inputs": {
+ "ipadapter": "ip-adapter.bin",
+ "clip_vision": "google/siglip-so400m-patch14-384",
+ "provider": "cuda"
+ },
+ "class_type": "IPAdapterFluxLoader",
+ "_meta": {
+ "title": "Load IPAdapter Flux Model"
+ }
+ },
+ "243": {
+ "inputs": {
+ "weight": 1,
+ "start_percent": 0,
+ "end_percent": 1,
+ "model": [
+ "12",
+ 0
+ ],
+ "ipadapter_flux": [
+ "242",
+ 0
+ ],
+ "image": [
+ "244",
+ 0
+ ]
+ },
+ "class_type": "ApplyIPAdapterFlux",
+ "_meta": {
+ "title": "Apply IPAdapter Flux Model"
+ }
+ },
+ "244": {
+ "inputs": {
+ "image": ""
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ }
+}
+"""
+
+depth_lora_flux = """
+{
+ "245": {
+ "inputs": {
+ "image": ""
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ },
+ "246": {
+ "inputs": {
+ "positive": [
+ "6",
+ 0
+ ],
+ "negative": [
+ "6",
+ 0
+ ],
+ "vae": [
+ "10",
+ 0
+ ],
+ "pixels": [
+ "245",
+ 0
+ ]
+ },
+ "class_type": "InstructPixToPixConditioning",
+ "_meta": {
+ "title": "InstructPixToPixConditioning"
+ }
+ },
+ "247": {
+ "inputs": {
+ "lora_name": "flux1-depth-dev-lora.safetensors",
+ "strength_model": 1,
+ "model": [
+ "12",
+ 0
+ ]
+ },
+ "class_type": "LoraLoaderModelOnly",
+ "_meta": {
+ "title": "LoraLoaderModelOnly"
+ }
+ }
+}
+"""
+
+gguf_unet_loader = """
+{
+ "12": {
+ "inputs": {
+ "unet_name": ""
+ },
+ "class_type": "UnetLoaderGGUF",
+ "_meta": {
+ "title": "Unet Loader (GGUF)"
+ }
+ }
+}
+"""
+
+prompt_text_qwen_image_edit = """
+{
+ "1": {
+ "inputs": {
+ "seed": 157165768450326,
+ "steps": 4,
+ "cfg": 1,
+ "sampler_name": "euler",
+ "scheduler": "simple",
+ "denoise": 1,
+ "model": [
+ "7",
+ 0
+ ],
+ "positive": [
+ "12",
+ 0
+ ],
+ "negative": [
+ "11",
+ 0
+ ],
+ "latent_image": [
+ "8",
+ 0
+ ]
+ },
+ "class_type": "KSampler",
+ "_meta": {
+ "title": "KSampler"
+ }
+ },
+ "2": {
+ "inputs": {
+ "samples": [
+ "1",
+ 0
+ ],
+ "vae": [
+ "4",
+ 0
+ ]
+ },
+ "class_type": "VAEDecode",
+ "_meta": {
+ "title": "VAE Decode"
+ }
+ },
+ "3": {
+ "inputs": {
+ "clip_name": "qwen_2.5_vl_7b_fp8_scaled.safetensors",
+ "type": "qwen_image",
+ "device": "default"
+ },
+ "class_type": "CLIPLoader",
+ "_meta": {
+ "title": "Load CLIP"
+ }
+ },
+ "4": {
+ "inputs": {
+ "vae_name": "qwen_image_vae.safetensors"
+ },
+ "class_type": "VAELoader",
+ "_meta": {
+ "title": "Load VAE"
+ }
+ },
+ "5": {
+ "inputs": {
+ "images": [
+ "2",
+ 0
+ ]
+ },
+ "class_type": "SaveImageWebsocket",
+ "_meta": {
+ "title": "SaveImageWebsocket"
+ }
+ },
+ "6": {
+ "inputs": {
+ "shift": 3,
+ "model": [
+ "9",
+ 0
+ ]
+ },
+ "class_type": "ModelSamplingAuraFlow",
+ "_meta": {
+ "title": "ModelSamplingAuraFlow"
+ }
+ },
+ "7": {
+ "inputs": {
+ "strength": 1,
+ "model": [
+ "6",
+ 0
+ ]
+ },
+ "class_type": "CFGNorm",
+ "_meta": {
+ "title": "CFGNorm"
+ }
+ },
+ "8": {
+ "inputs": {
+ "pixels": [
+ "14",
+ 0
+ ],
+ "vae": [
+ "4",
+ 0
+ ]
+ },
+ "class_type": "VAEEncode",
+ "_meta": {
+ "title": "VAE Encode"
+ }
+ },
+ "11": {
+ "inputs": {
+ "prompt": "",
+ "clip": [
+ "3",
+ 0
+ ],
+ "image1": [
+ "14",
+ 0
+ ],
+ "image2": [
+ "15",
+ 0
+ ],
+ "image3": [
+ "16",
+ 0
+ ]
+ },
+ "class_type": "TextEncodeQwenImageEditPlus",
+ "_meta": {
+ "title": "TextEncodeQwenImageEditPlus"
+ }
+ },
+ "12": {
+ "inputs": {
+ "prompt": "a monkey",
+ "clip": [
+ "3",
+ 0
+ ],
+ "image1": [
+ "14",
+ 0
+ ],
+ "image2": [
+ "15",
+ 0
+ ],
+ "image3": [
+ "16",
+ 0
+ ]
+ },
+ "class_type": "TextEncodeQwenImageEditPlus",
+ "_meta": {
+ "title": "TextEncodeQwenImageEditPlus"
+ }
+ },
+ "13": {
+ "inputs": {
+ "unet_name": "Qwen-Image-Edit-2509-Q3_K_M.gguf"
+ },
+ "class_type": "UnetLoaderGGUF",
+ "_meta": {
+ "title": "Unet Loader (GGUF)"
+ }
+ },
+ "14": {
+ "inputs": {
+ "image": ""
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ },
+ "15": {
+ "inputs": {
+ "image": ""
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ },
+ "16": {
+ "inputs": {
+ "image": ""
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Image"
+ }
+ }
+}
+"""
+
+# ---------------------------------------------------------------------------
+# FLUX.2 Klein – multi-reference edit model (Apache 2.0)
+# Based on official ComfyUI Klein image-edit workflow.
+# Nodes: all built-in ComfyUI core (no custom nodes needed)
+# UNETLoader, CLIPLoader (type=flux2, Qwen 3 4B), VAELoader,
+# CLIPTextEncode (x2), EmptyFlux2LatentImage, Flux2Scheduler,
+# KSamplerSelect, RandomNoise, CFGGuider (cfg=1), SamplerCustomAdvanced,
+# VAEDecode, SaveImageWebsocket, LoadImage (x3 reference slots).
+# Reference images are wired dynamically via:
+# VAEEncode → ReferenceLatent (pos + neg chain) → CFGGuider.
+# ---------------------------------------------------------------------------
+prompt_text_flux2_klein = """
+{
+ "1": {
+ "inputs": {
+ "unet_name": "flux-2-klein-base-4b-fp8.safetensors",
+ "weight_dtype": "default"
+ },
+ "class_type": "UNETLoader",
+ "_meta": { "title": "Load Diffusion Model (Klein)" }
+ },
+ "2": {
+ "inputs": {
+ "clip_name": "qwen_3_4b.safetensors",
+ "type": "flux2"
+ },
+ "class_type": "CLIPLoader",
+ "_meta": { "title": "Load CLIP (Qwen 3 4B)" }
+ },
+ "3": {
+ "inputs": {
+ "vae_name": "flux2-vae.safetensors"
+ },
+ "class_type": "VAELoader",
+ "_meta": { "title": "Load VAE (FLUX.2)" }
+ },
+ "4": {
+ "inputs": {
+ "text": "",
+ "clip": ["2", 0]
+ },
+ "class_type": "CLIPTextEncode",
+ "_meta": { "title": "CLIP Text Encode (Positive)" }
+ },
+ "5": {
+ "inputs": {
+ "text": "",
+ "clip": ["2", 0]
+ },
+ "class_type": "CLIPTextEncode",
+ "_meta": { "title": "CLIP Text Encode (Negative)" }
+ },
+ "6": {
+ "inputs": {
+ "width": 1024,
+ "height": 1024,
+ "batch_size": 1
+ },
+ "class_type": "EmptyFlux2LatentImage",
+ "_meta": { "title": "Empty Flux 2 Latent" }
+ },
+ "7": {
+ "inputs": {
+ "steps": 20,
+ "width": 1024,
+ "height": 1024
+ },
+ "class_type": "Flux2Scheduler",
+ "_meta": { "title": "Flux 2 Scheduler" }
+ },
+ "8": {
+ "inputs": {
+ "sampler_name": "euler"
+ },
+ "class_type": "KSamplerSelect",
+ "_meta": { "title": "KSampler Select" }
+ },
+ "9": {
+ "inputs": {
+ "noise_seed": 0
+ },
+ "class_type": "RandomNoise",
+ "_meta": { "title": "Random Noise" }
+ },
+ "10": {
+ "inputs": {
+ "model": ["1", 0],
+ "positive": ["4", 0],
+ "negative": ["5", 0],
+ "cfg": 1
+ },
+ "class_type": "CFGGuider",
+ "_meta": { "title": "CFG Guider" }
+ },
+ "11": {
+ "inputs": {
+ "noise": ["9", 0],
+ "guider": ["10", 0],
+ "sampler": ["8", 0],
+ "sigmas": ["7", 0],
+ "latent_image": ["6", 0]
+ },
+ "class_type": "SamplerCustomAdvanced",
+ "_meta": { "title": "Sampler Custom Advanced" }
+ },
+ "12": {
+ "inputs": {
+ "samples": ["11", 0],
+ "vae": ["3", 0]
+ },
+ "class_type": "VAEDecode",
+ "_meta": { "title": "VAE Decode" }
+ },
+ "13": {
+ "inputs": {
+ "images": ["12", 0]
+ },
+ "class_type": "SaveImageWebsocket",
+ "_meta": { "title": "SaveImageWebsocket" }
+ },
+ "14": {
+ "inputs": {
+ "image": ""
+ },
+ "class_type": "LoadImage",
+ "_meta": { "title": "Load Reference Image 1 (Structure)" }
+ },
+ "15": {
+ "inputs": {
+ "image": ""
+ },
+ "class_type": "LoadImage",
+ "_meta": { "title": "Load Reference Image 2 (Style)" }
+ },
+ "16": {
+ "inputs": {
+ "image": ""
+ },
+ "class_type": "LoadImage",
+ "_meta": { "title": "Load Reference Image 3 (Context)" }
+ }
+}
+"""
+
+# Workflow for TRELLIS.2 Image-to-3D generation via PozzettiAndrea/ComfyUI-TRELLIS2 nodes
+# Pipeline: LoadImage → LoadModels → RemoveBackground → GetConditioning → ImageToShape → ShapeToTexturedMesh → ExportGLB
+prompt_text_trellis2 = """
+{
+ "1": {
+ "inputs": {
+ "image": "",
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Input Image"
+ }
+ },
+ "2": {
+ "inputs": {
+ "resolution": "1024_cascade",
+ "attn_backend": "flash_attn",
+ "vram_mode": "disk_offload"
+ },
+ "class_type": "LoadTrellis2Models",
+ "_meta": {
+ "title": "Load TRELLIS.2 Models"
+ }
+ },
+ "3": {
+ "inputs": {
+ "image": ["1", 0],
+ "low_vram": true
+ },
+ "class_type": "Trellis2RemoveBackground",
+ "_meta": {
+ "title": "Remove Background"
+ }
+ },
+ "4": {
+ "inputs": {
+ "model_config": ["2", 0],
+ "image": ["3", 0],
+ "mask": ["3", 1],
+ "include_1024": true,
+ "background_color": "black"
+ },
+ "class_type": "Trellis2GetConditioning",
+ "_meta": {
+ "title": "Get Conditioning"
+ }
+ },
+ "5": {
+ "inputs": {
+ "model_config": ["2", 0],
+ "conditioning": ["4", 0],
+ "seed": 0,
+ "ss_guidance_strength": 7.5,
+ "ss_sampling_steps": 12,
+ "shape_guidance_strength": 7.5,
+ "shape_sampling_steps": 12,
+ "max_tokens": 49152
+ },
+ "class_type": "Trellis2ImageToShape",
+ "_meta": {
+ "title": "Image to Shape"
+ }
+ },
+ "6": {
+ "inputs": {
+ "model_config": ["2", 0],
+ "conditioning": ["4", 0],
+ "shape_result": ["5", 0],
+ "seed": 0,
+ "tex_guidance_strength": 7.5,
+ "tex_sampling_steps": 12
+ },
+ "class_type": "Trellis2ShapeToTexturedMesh",
+ "_meta": {
+ "title": "Shape to Textured Mesh"
+ }
+ },
+ "7": {
+ "inputs": {
+ "voxelgrid_path": ["6", 1],
+ "decimation_target": 100000,
+ "texture_size": 4096,
+ "remesh": true,
+ "filename_prefix": "trellis2"
+ },
+ "class_type": "Trellis2ExportGLB",
+ "_meta": {
+ "title": "Export GLB"
+ }
+ }
+}
+"""
+
+prompt_text_trellis2_shape_only = """
+{
+ "1": {
+ "inputs": {
+ "image": "",
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Input Image"
+ }
+ },
+ "2": {
+ "inputs": {
+ "resolution": "1024_cascade",
+ "attn_backend": "flash_attn",
+ "vram_mode": "disk_offload"
+ },
+ "class_type": "LoadTrellis2Models",
+ "_meta": {
+ "title": "Load TRELLIS.2 Models"
+ }
+ },
+ "3": {
+ "inputs": {
+ "image": ["1", 0],
+ "low_vram": true
+ },
+ "class_type": "Trellis2RemoveBackground",
+ "_meta": {
+ "title": "Remove Background"
+ }
+ },
+ "4": {
+ "inputs": {
+ "model_config": ["2", 0],
+ "image": ["3", 0],
+ "mask": ["3", 1],
+ "include_1024": true,
+ "background_color": "black"
+ },
+ "class_type": "Trellis2GetConditioning",
+ "_meta": {
+ "title": "Get Conditioning"
+ }
+ },
+ "5": {
+ "inputs": {
+ "model_config": ["2", 0],
+ "conditioning": ["4", 0],
+ "seed": 0,
+ "ss_guidance_strength": 7.5,
+ "ss_sampling_steps": 12,
+ "shape_guidance_strength": 7.5,
+ "shape_sampling_steps": 12,
+ "max_tokens": 49152
+ },
+ "class_type": "Trellis2ImageToShape",
+ "_meta": {
+ "title": "Image to Shape"
+ }
+ },
+ "6": {
+ "inputs": {
+ "trimesh": ["5", 1],
+ "target_face_count": 100000,
+ "fill_holes": false,
+ "remesh": false,
+ "remesh_band": 1.0
+ },
+ "class_type": "Trellis2Simplify",
+ "_meta": {
+ "title": "Simplify Mesh"
+ }
+ },
+ "7": {
+ "inputs": {
+ "trimesh": ["6", 0],
+ "filename_prefix": "trellis2",
+ "file_format": "glb"
+ },
+ "class_type": "Trellis2ExportTrimesh",
+ "_meta": {
+ "title": "Export Trimesh"
+ }
+ }
+}
+"""
+
diff --git a/stablegen/utils.py b/stablegen/utils.py
index d0c5a32..97db5b3 100644
--- a/stablegen/utils.py
+++ b/stablegen/utils.py
@@ -517,7 +517,7 @@ def configure_output_node_paths(node, directory, filename):
else:
slot = slots.new(filename)
else:
- print("Warning: Output node API lacks slot accessors; output may fail.")
+ print("[StableGen] Warning: Output node API lacks slot accessors; output may fail.")
return slot
diff --git a/stablegen/workflows.py b/stablegen/workflows.py
index b6f9d63..4b8a19d 100644
--- a/stablegen/workflows.py
+++ b/stablegen/workflows.py
@@ -2,56 +2,24 @@ import bpy
import os
import json
import uuid
-import random
import websocket
import socket
import urllib.request
import urllib.parse
-from datetime import datetime, timedelta
-from .util.helpers import prompt_text_qwen_image_edit, prompt_text_flux2_klein, prompt_text_trellis2, prompt_text_trellis2_shape_only
-from .utils import get_generation_dirs
from .timeout_config import get_timeout
from io import BytesIO
import numpy as np
from PIL import Image
+from .texturing.workflows import _TexturingWorkflowMixin
+from .mesh_gen.workflows import _Trellis2WorkflowMixin
-def _texturing_prompt(scene):
- """Return the texturing portion of the prompt.
- Priority order:
- 1. Dedicated ``texture_prompt`` field (if the separate-prompt toggle is on
- and the field is non-empty).
- 2. Legacy ``||`` separator in the main prompt (text after ``||``).
- 3. The full main prompt as-is.
- """
- if getattr(scene, 'use_separate_texture_prompt', False):
- tex = getattr(scene, 'texture_prompt', '').strip()
- if tex:
- return tex
- raw = scene.comfyui_prompt
- if '||' in raw:
- return raw.split('||', 1)[1].strip()
- return raw
+class _WorkflowBase:
+ """Shared infrastructure for WorkflowManager."""
-
-def _generation_prompt(scene):
- """Return the generation / mesh-creation portion of the prompt.
-
- When the separate-texture-prompt toggle is on the entire main prompt is
- treated as the generation prompt (no ``||`` splitting). Otherwise the
- legacy ``||`` separator is respected.
- """
- raw = scene.comfyui_prompt
- if getattr(scene, 'use_separate_texture_prompt', False):
- return raw.strip()
- if '||' in raw:
- return raw.split('||', 1)[0].strip()
- return raw
-
-class WorkflowManager:
def __init__(self, operator):
"""
Initializes the WorkflowManager.
@@ -61,1084 +29,6 @@ class WorkflowManager:
"""
self.operator = operator
- def _crop_and_vignette(
- self,
- img_bytes,
- border_px: int = 8,
- 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.
-
- 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.
- feather : fraction of min(width, height) that is used as the feather band.
- gamma : exponent applied to the feather mask (1.0 = linear).
- """
-
- if img_bytes is None:
- return None
-
- # --- Load image as RGBA without touching color ---
- buf = BytesIO(img_bytes)
- try:
- img = Image.open(buf).convert("RGBA")
- except Exception as e:
- print(f"[StableGen] _crop_and_vignette: cannot decode image "
- 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))
- w, h = img.size
-
- # Convert to numpy for mask math
- arr = np.asarray(img, dtype=np.float32) / 255.0 # [H, W, 4]
- rgb = arr[..., :3]
- alpha_orig = arr[..., 3]
-
- # If there was no alpha, assume fully opaque as starting point
- if np.all(alpha_orig == 0):
- alpha_orig = np.ones_like(alpha_orig, dtype=np.float32)
-
- # --- Build rectangular feather mask based on distance to nearest edge ---
-
- # Normalized distance (in pixels) to each edge
- yy, xx = np.mgrid[0:h, 0:w]
- dist_to_left = xx
- dist_to_right = (w - 1) - xx
- dist_to_top = yy
- dist_to_bottom = (h - 1) - yy
-
- dist_to_edge = np.minimum(
- np.minimum(dist_to_left, dist_to_right),
- np.minimum(dist_to_top, dist_to_bottom),
- ).astype(np.float32)
-
- # Feather band thickness in pixels
- # (0.0–0.5 of the smaller dimension is reasonable)
- min_dim = float(min(w, h))
- feather_px = max(1.0, feather * 0.5 * min_dim)
-
- # 0 at the border, 1 in the interior beyond the feather band
- mask = dist_to_edge / feather_px
- mask = np.clip(mask, 0.0, 1.0)
-
- # Shape the transition with gamma (only on the mask, not on RGB!)
- if gamma != 1.0:
- # stronger bias toward 1.0 in mid-range
- mask = np.clip(mask, 1e-6, 1.0)
- mask = np.power(mask, gamma)
-
- # Compose final alpha: original alpha * vignette mask
- alpha_new = alpha_orig * mask
-
- # Reassemble RGBA, leaving RGB completely unchanged
- out = np.empty_like(arr)
- out[..., :3] = rgb
- out[..., 3] = alpha_new
- out = np.clip(out * 255.0, 0, 255).astype(np.uint8)
-
- img_out = Image.fromarray(out, mode="RGBA")
-
- out_buf = BytesIO()
- img_out.save(out_buf, format="PNG")
- return out_buf.getvalue()
-
- def _build_qwen_reference_latent_chain(self, prompt, NODES, start_node_id=700):
- """
- Build VAEEncode + ReferenceLatent chain for the Qwen unzoom fix.
-
- When the VAE input is disconnected from TextEncodeQwenImageEditPlus,
- the node no longer creates reference latents (and no longer forces
- a 1 MP rescale). We replicate the reference-latent injection
- manually so the latents match the actual output resolution.
-
- For each active image (image1 / image2 / image3) still present in
- the positive text-encode node, a VAEEncode node is created and a
- ReferenceLatent node is chained onto both positive and negative
- conditioning. The KSampler inputs are then updated to point at
- the tail of the chain.
-
- When ``qwen_timestep_zero_ref`` is enabled on the scene, a
- FluxKontextMultiReferenceLatentMethod node is appended with
- ``index_timestep_zero`` so the model treats reference latents as
- fully-denoised tokens, reducing color shift / over-saturation.
- """
- pos_node = NODES['pos_prompt']
- neg_node = NODES['neg_prompt']
- sampler_node = NODES['sampler']
- vae_ref = ["4", 0] # VAELoader node
-
- # Collect active image references from the pos text-encode node.
- # image1 is always present; image2/image3 may have been deleted.
- active_refs = []
- pos_inputs = prompt[pos_node]['inputs']
- for key in ('image1', 'image2', 'image3'):
- if key in pos_inputs:
- active_refs.append(pos_inputs[key])
-
- if not active_refs:
- return
-
- node_id = start_node_id
- # Avoid collisions with existing dynamic nodes
- while str(node_id) in prompt:
- node_id += 1
-
- pos_cond_ref = [pos_node, 0]
- neg_cond_ref = [neg_node, 0]
-
- for i, img_ref in enumerate(active_refs):
- # --- VAEEncode for this image ---
- # For image1 we can reuse the existing node 8 which already
- # encodes the same image (also used as KSampler latent_image).
- if i == 0:
- vae_enc_ref = ["8", 0]
- else:
- vae_enc_id = str(node_id)
- node_id += 1
- prompt[vae_enc_id] = {
- "inputs": {
- "pixels": img_ref,
- "vae": vae_ref
- },
- "class_type": "VAEEncode",
- "_meta": {"title": f"VAE Encode (Ref {i + 1})"}
- }
- vae_enc_ref = [vae_enc_id, 0]
-
- # --- ReferenceLatent for positive ---
- ref_pos_id = str(node_id)
- node_id += 1
- prompt[ref_pos_id] = {
- "inputs": {
- "conditioning": pos_cond_ref,
- "latent": vae_enc_ref
- },
- "class_type": "ReferenceLatent",
- "_meta": {"title": f"Reference Latent Pos {i + 1}"}
- }
- pos_cond_ref = [ref_pos_id, 0]
-
- # --- ReferenceLatent for negative ---
- ref_neg_id = str(node_id)
- node_id += 1
- prompt[ref_neg_id] = {
- "inputs": {
- "conditioning": neg_cond_ref,
- "latent": vae_enc_ref
- },
- "class_type": "ReferenceLatent",
- "_meta": {"title": f"Reference Latent Neg {i + 1}"}
- }
- neg_cond_ref = [ref_neg_id, 0]
-
- # Point KSampler at the final chained conditioning
- prompt[sampler_node]['inputs']['positive'] = pos_cond_ref
- prompt[sampler_node]['inputs']['negative'] = neg_cond_ref
-
- # ── Timestep-zero reference method (colour-shift reduction) ─────
- context = bpy.context
- if getattr(context.scene, 'qwen_timestep_zero_ref', False):
- # Positive conditioning
- tz_pos_id = str(node_id)
- node_id += 1
- prompt[tz_pos_id] = {
- "inputs": {
- "conditioning": pos_cond_ref,
- "reference_latents_method": "index_timestep_zero"
- },
- "class_type": "FluxKontextMultiReferenceLatentMethod",
- "_meta": {"title": "Ref Method (Pos)"}
- }
- prompt[sampler_node]['inputs']['positive'] = [tz_pos_id, 0]
-
- # Negative conditioning
- tz_neg_id = str(node_id)
- node_id += 1
- prompt[tz_neg_id] = {
- "inputs": {
- "conditioning": neg_cond_ref,
- "reference_latents_method": "index_timestep_zero"
- },
- "class_type": "FluxKontextMultiReferenceLatentMethod",
- "_meta": {"title": "Ref Method (Neg)"}
- }
- prompt[sampler_node]['inputs']['negative'] = [tz_neg_id, 0]
-
- # ------------------------------------------------------------------
- # FLUX.2 Klein – build ReferenceLatent chain for multi-reference editing
- # ------------------------------------------------------------------
- def _build_flux2_reference_chain(self, prompt, NODES, ref_image_infos, start_node_id=700):
- """Build a ReferenceLatent chain for FLUX.2 Klein (based on official workflow).
-
- For each reference image, creates:
- LoadImage (already present) → VAEEncode
- → ReferenceLatent (positive conditioning)
- → ReferenceLatent (negative conditioning)
-
- The ReferenceLatent nodes are chained so each successive reference
- appends to the previous one. The final modified positive and negative
- conditioning outputs are wired into CFGGuider.
-
- Args:
- prompt: The ComfyUI prompt dict (modified in-place).
- NODES: Node-ID mapping dict.
- ref_image_infos: List of dicts, each with at least a ``loader_node``
- key pointing to the LoadImage node ID.
- start_node_id: First dynamic node ID to use.
- """
- if not ref_image_infos:
- return
-
- pos_node = NODES['pos_prompt']
- neg_node = NODES['neg_prompt']
- guider_node = NODES['guider']
- vae_ref = [NODES['vae_loader'], 0]
-
- node_id = start_node_id
- while str(node_id) in prompt:
- node_id += 1
-
- pos_cond_ref = [pos_node, 0]
- neg_cond_ref = [neg_node, 0]
-
- for i, ref_info in enumerate(ref_image_infos):
- loader_node = ref_info['loader_node']
-
- # --- VAEEncode (reference image → latent) ---
- vae_enc_id = str(node_id); node_id += 1
- prompt[vae_enc_id] = {
- "inputs": {
- "pixels": [loader_node, 0],
- "vae": vae_ref
- },
- "class_type": "VAEEncode",
- "_meta": {"title": f"VAE Encode Ref {i + 1}"}
- }
-
- # --- ReferenceLatent (positive) ---
- ref_pos_id = str(node_id); node_id += 1
- prompt[ref_pos_id] = {
- "inputs": {
- "conditioning": pos_cond_ref,
- "latent": [vae_enc_id, 0]
- },
- "class_type": "ReferenceLatent",
- "_meta": {"title": f"Reference Latent Pos {i + 1}"}
- }
- pos_cond_ref = [ref_pos_id, 0]
-
- # --- ReferenceLatent (negative) ---
- ref_neg_id = str(node_id); node_id += 1
- prompt[ref_neg_id] = {
- "inputs": {
- "conditioning": neg_cond_ref,
- "latent": [vae_enc_id, 0]
- },
- "class_type": "ReferenceLatent",
- "_meta": {"title": f"Reference Latent Neg {i + 1}"}
- }
- neg_cond_ref = [ref_neg_id, 0]
-
- # Wire final pos/neg conditioning into CFGGuider
- prompt[guider_node]['inputs']['positive'] = pos_cond_ref
- prompt[guider_node]['inputs']['negative'] = neg_cond_ref
-
- def _get_klein_default_prompts(self, context, is_initial_image):
- """Gets the default Klein prompts based on the current context.
-
- Follows BFL's official Klein prompting guide: write like a novelist,
- front-load the subject, reference image numbers explicitly, and
- describe the desired transformation rather than the image contents.
- Camera prompts are placed after the main prompt via {camera_suffix}
- to reduce their weighting (Klein pays more attention to what comes
- first).
- """
- is_generating = hasattr(self.operator, '_current_image')
- is_subsequent_in_sequence = is_generating and self.operator._current_image > 0
-
- if is_initial_image:
- style_image_provided = context.scene.qwen_use_external_style_image
- else:
- 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:
- if not style_image_provided:
- return "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour."
- else:
- return "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour, adopting the visual style from image 2."
- else:
- if context_mode == 'ADDITIONAL':
- return "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray. Image 3 represents the overall style."
- elif context_mode == 'REPLACE_STYLE':
- return "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour. In image 2, replace all solid magenta areas with content that continues the surrounding style. Replace the background with solid gray."
- else:
- if not style_image_provided:
- return "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour."
- else:
- return "Reskin this into {main_prompt}{camera_suffix}, while preserve identity keep likeness replica contour, adopting the visual style from image 2."
-
- def _get_qwen_default_prompts(self, context, is_initial_image):
- """Gets the default Qwen prompts based on the current context."""
- # Check if we are in a real generation context vs. just resetting a prompt
- is_generating = hasattr(self.operator, '_current_image')
- is_subsequent_in_sequence = is_generating and self.operator._current_image > 0
-
- # TRELLIS.2 style reference counts as a style image for prompt selection.
- _t2_style = (
- getattr(context.scene, 'architecture_mode', '') == 'trellis2'
- and getattr(context.scene, 'qwen_use_trellis2_style', False)
- )
-
- if is_initial_image:
- # First frame has a style reference from an external image or the TRELLIS.2 input.
- style_image_provided = (
- context.scene.qwen_use_external_style_image
- or _t2_style
- )
- else:
- # Later frames can draw style from previous renders, external sources,
- # context renders, or the TRELLIS.2 input (when not initial-only).
- 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'} or
- (_t2_style and not getattr(context.scene, 'qwen_trellis2_style_initial_only', False))
- )
- context_mode = context.scene.qwen_context_render_mode
-
- # Optional sentence injected into context-render prompts.
- gray_bg = " Replace the background with solid gray." if getattr(context.scene, 'qwen_prompt_gray_background', True) else ""
-
- if is_initial_image:
- if not style_image_provided:
- return "Change the format of image 1 to '{main_prompt}'"
- else:
- return "Change and transfer the format of '{main_prompt}' in image 1 to the style from image 2"
- else: # Subsequent image
- if context_mode == 'ADDITIONAL':
- return f"Change and transfer the format of image 1 to '{{main_prompt}}'. Replace all solid magenta areas in image 2.{gray_bg} The style from image 2 should smoothly continue into the previously magenta areas. Image 3 represents the overall style of the object."
- elif context_mode == 'REPLACE_STYLE':
- return f"Change and transfer the format of image 1 to '{{main_prompt}}'. Replace all solid magenta areas in image 2.{gray_bg} The style from image 2 should smoothly continue into the previously magenta areas."
- else: # NONE or other cases
- if not style_image_provided:
- return "Change the format of image 1 to '{main_prompt}'"
- else:
- return "Change and transfer the format of '{main_prompt}' in image 1 to the style from image 2"
-
-
- def generate_qwen_refine(self, context, camera_id=None):
- """Generates an image using the Qwen-Image-Edit workflow for refinement."""
- server_address = context.preferences.addons[__package__].preferences.server_address
- client_id = str(uuid.uuid4())
- revision_dir = get_generation_dirs(context)["revision"]
-
- prompt = json.loads(prompt_text_qwen_image_edit)
-
- NODES = {
- 'sampler': "1",
- 'save_image': "5",
- 'model_sampler': "6", # ModelSamplingAuraFlow
- 'cfg_norm': "7", # CFGNorm
- 'vae_encode': "8",
- 'pos_prompt': "12",
- 'neg_prompt': "11",
- 'unet_loader': "13",
- 'guidance_map_loader': "14", # Image 1 (structure)
- 'style_map_loader': "15", # Image 2 (style)
- 'context_render_loader': "16", # Image 3 (context render)
- }
-
- # --- Build LoRA chain ---
- initial_model_input = [NODES['unet_loader'], 0]
- dummy_clip_input = [NODES['unet_loader'], 0]
-
- is_nunchaku = context.scene.model_name.lower().endswith('.safetensors')
- lora_class = "NunchakuQwenImageLoraLoader" if is_nunchaku else "LoraLoaderModelOnly"
-
- prompt, final_lora_model_out, _ = self._build_lora_chain(
- prompt, context,
- initial_model_input, dummy_clip_input,
- start_node_id=500,
- lora_class_type=lora_class
- )
-
- prompt[NODES['model_sampler']]['inputs']['model'] = final_lora_model_out
-
- # --- Configure Inputs ---
- # Image 1: The current render (structure)
- render_info = self.operator._get_uploaded_image_info(context, "inpaint", subtype="render", camera_id=camera_id)
- if not render_info:
- self.operator._error = f"Could not find or upload render for camera {camera_id}."
- return {"error": "conn_failed"}
- prompt[NODES['guidance_map_loader']]['inputs']['image'] = render_info['name']
-
- # --- Configure Style Image (Image 2) ---
- use_prev_ref = context.scene.qwen_refine_use_prev_ref
- style_image_info = None
-
- if use_prev_ref and camera_id > 0:
- # Use previous generated image
- style_image_info = self.operator._get_uploaded_image_info(context, "generated", camera_id=camera_id - 1, material_id=self.operator._material_id)
-
- if style_image_info:
- prompt[NODES['style_map_loader']]['inputs']['image'] = style_image_info['name']
- else:
- # Fallback to external style image if configured
- if context.scene.qwen_use_external_style_image:
- style_image_info = self.operator._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(context.scene.qwen_external_style_image))
- if style_image_info:
- prompt[NODES['style_map_loader']]['inputs']['image'] = style_image_info['name']
-
- # Fallback to TRELLIS.2 input image as style
- if not style_image_info and getattr(context.scene, 'architecture_mode', '') == 'trellis2' and getattr(context.scene, 'qwen_use_trellis2_style', False):
- t2_path = getattr(context.scene, 'trellis2_last_input_image', '')
- if t2_path and os.path.exists(bpy.path.abspath(t2_path)):
- style_image_info = self.operator._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(t2_path))
- if style_image_info:
- prompt[NODES['style_map_loader']]['inputs']['image'] = style_image_info['name']
-
- # If still no style image, remove Image 2 inputs
- if not style_image_info:
- del prompt[NODES['style_map_loader']]
- del prompt[NODES['pos_prompt']]['inputs']['image2']
- del prompt[NODES['neg_prompt']]['inputs']['image2']
-
- # --- Configure Depth Map (Image 3) ---
- use_depth = context.scene.qwen_refine_use_depth
- depth_info = None
- if use_depth:
- depth_info = self.operator._get_uploaded_image_info(context, "controlnet", subtype="depth", camera_id=camera_id)
-
- if depth_info:
- prompt[NODES['context_render_loader']]['inputs']['image'] = depth_info['name']
- else:
- # Remove Context Render (Image 3) if not used
- del prompt[NODES['context_render_loader']]
- del prompt[NODES['pos_prompt']]['inputs']['image3']
- del prompt[NODES['neg_prompt']]['inputs']['image3']
-
- # --- Prompt ---
- user_prompt = _texturing_prompt(context.scene)
- final_prompt = f"Modify image1 to {user_prompt}"
- if depth_info:
- final_prompt += ". Use image3 as depth map reference."
-
- prompt[NODES['pos_prompt']]['inputs']['prompt'] = final_prompt
-
- # --- Reference Latent chain (Qwen unzoom fix) ---
- self._build_qwen_reference_latent_chain(prompt, NODES)
-
- # --- Save and Execute ---
- self._save_prompt_to_file(prompt, revision_dir)
-
- ws = self._connect_to_websocket(server_address, client_id)
- if ws is None:
- return {"error": "conn_failed"}
-
- images = None
- try:
- images = self._execute_prompt_and_get_images(ws, prompt, client_id, server_address, NODES)
- finally:
- if ws:
- ws.close()
-
- if images is None or isinstance(images, dict) and "error" in images:
- return {"error": "conn_failed"}
-
- return images[NODES['save_image']][0]
-
- def generate_qwen_edit(self, context, camera_id=None):
- """Generates an image using the Qwen-Image-Edit workflow."""
- server_address = context.preferences.addons[__package__].preferences.server_address
- client_id = str(uuid.uuid4())
- revision_dir = get_generation_dirs(context)["revision"]
-
- prompt = json.loads(prompt_text_qwen_image_edit)
-
- NODES = {
- 'sampler': "1",
- 'save_image': "5",
- 'model_sampler': "6", # ModelSamplingAuraFlow
- 'cfg_norm': "7", # CFGNorm
- 'vae_encode': "8",
- 'pos_prompt': "12",
- 'neg_prompt': "11",
- 'unet_loader': "13",
- 'guidance_map_loader': "14", # Image 1 (structure)
- 'style_map_loader': "15", # Image 2 (style)
- 'context_render_loader': "16", # Image 3 (context render)
- }
-
- # --- Build LoRA chain ---
- # The Qwen workflow uses model-only LoRAs. We can reuse the existing
- # chain builder by providing a dummy CLIP input that won't be used.
- initial_model_input = [NODES['unet_loader'], 0]
- dummy_clip_input = [NODES['unet_loader'], 0] # Dummy, not used by LoraLoaderModelOnly
-
- is_nunchaku = context.scene.model_name.lower().endswith('.safetensors')
- lora_class = "NunchakuQwenImageLoraLoader" if is_nunchaku else "LoraLoaderModelOnly"
-
- prompt, final_lora_model_out, _ = self._build_lora_chain(
- prompt, context,
- initial_model_input, dummy_clip_input,
- start_node_id=500, # Use a high starting ID to avoid conflicts
- lora_class_type=lora_class # Specify model-only loader
- )
-
- # Connect the output of the LoRA chain to the next node in the model path
- prompt[NODES['model_sampler']]['inputs']['model'] = final_lora_model_out
-
-
- # --- Configure Inputs ---
- guidance_map_type = context.scene.qwen_guidance_map_type
- guidance_map_info = self.operator._get_uploaded_image_info(context, "controlnet", subtype=guidance_map_type, camera_id=camera_id)
- if not guidance_map_info:
- self.operator._error = f"Could not find or upload {guidance_map_type} map for camera {camera_id}."
- return {"error": "conn_failed"}
- prompt[NODES['guidance_map_loader']]['inputs']['image'] = guidance_map_info['name']
-
- # --- Configure Style Image (Image 2) and Prompts ---
- user_prompt = _texturing_prompt(context.scene)
- style_image_info = None
- context_render_info = None
- context_mode = context.scene.qwen_context_render_mode
- 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
- # Prepend the view description
- user_prompt = f"{view_desc}, {user_prompt}"
-
- # --- Handle Context Render (Image 3) ---
- # This is only active in sequential mode after the first image
- is_initial_image = not self.operator._current_image > 0
- if not is_initial_image and context_mode != 'NONE':
- context_render_info = self.operator._get_uploaded_image_info(context, "inpaint", subtype="render", camera_id=camera_id)
- if not context_render_info:
- self.operator._error = f"Qwen context render enabled, but could not find context render for camera {camera_id}."
- return {"error": "conn_failed"}
-
- if context_mode == 'ADDITIONAL':
- # Switch context loader and style loader ids
- NODES['context_render_loader'], NODES['style_map_loader'] = NODES['style_map_loader'], NODES['context_render_loader']
- prompt[NODES['context_render_loader']]['inputs']['image'] = context_render_info['name']
- # The prompt needs to reference image 3
- if context.scene.qwen_use_custom_prompts:
- pos_prompt_text = context.scene.qwen_custom_prompt_seq_additional.format(main_prompt=user_prompt)
- else:
- pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
- prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
- # If mode is REPLACE_STYLE, we will handle it in the style image section below.
- else:
- remove_context = True
- else:
- remove_context = True
-
- if remove_context:
- del prompt[NODES['context_render_loader']]
- del prompt[NODES['pos_prompt']]['inputs']['image3']
- del prompt[NODES['neg_prompt']]['inputs']['image3']
-
-
- # --- Handle Style Image (Image 2) ---
- # Determine if we should use the external style image for this specific frame
- use_external_this_frame = context.scene.qwen_use_external_style_image
- if use_external_this_frame and not is_initial_image and context.scene.qwen_external_style_initial_only:
- # If it's a subsequent image AND the "initial only" flag is set, DON'T use the external image.
- use_external_this_frame = False
- context.scene.sequential_ipadapter = True # Force using previous image as style
-
- # Case 1: External Style Image for this frame
- if use_external_this_frame:
- style_image_info = self.operator._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(context.scene.qwen_external_style_image))
- 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"}
- 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)
- else:
- pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
- prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
- else: # Additional mode
- if context.scene.qwen_use_custom_prompts:
- pos_prompt_text = (context.scene.qwen_custom_prompt_seq_additional if not is_initial_image else context.scene.qwen_custom_prompt_initial).format(main_prompt=user_prompt)
- else:
- pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
- prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
-
- # Case 1b: TRELLIS.2 input image as style (when no external style configured)
- elif getattr(context.scene, 'architecture_mode', '') == 'trellis2' and getattr(context.scene, 'qwen_use_trellis2_style', False):
- use_t2_this_frame = True
- if not is_initial_image and getattr(context.scene, 'qwen_trellis2_style_initial_only', False):
- # Initial-only mode: fall back to sequential style for subsequent frames
- use_t2_this_frame = False
- context.scene.sequential_ipadapter = True
- if use_t2_this_frame:
- t2_path = getattr(context.scene, 'trellis2_last_input_image', '')
- if t2_path and os.path.exists(bpy.path.abspath(t2_path)):
- style_image_info = self.operator._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(t2_path))
- if style_image_info:
- if context.scene.qwen_use_custom_prompts:
- pos_prompt_text = (context.scene.qwen_custom_prompt_initial).format(main_prompt=user_prompt)
- else:
- pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
- prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
-
- # Case 2: Sequential generation (after first image)
- elif not is_initial_image:
- if context_mode == 'REPLACE_STYLE':
- # The context render becomes the style image
- style_image_info = context_render_info
- if context.scene.qwen_use_custom_prompts:
- pos_prompt_text = context.scene.qwen_custom_prompt_seq_replace.format(main_prompt=user_prompt)
- else:
- pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
- prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
- elif context.scene.sequential_ipadapter: # Use previous generated image
- if context.scene.sequential_ipadapter_mode == 'original_render':
- # For original_render in Qwen context, fall back to first mode since
- # the render is already used as the guidance map (Image 1).
- ref_cam_id = 0
- else:
- ref_cam_id = 0 if context.scene.sequential_ipadapter_mode == 'first' else self.operator._current_image - 1
- style_image_info = self.operator._get_uploaded_image_info(context, "generated", camera_id=ref_cam_id, material_id=self.operator._material_id)
- if not style_image_info:
- self.operator._error = f"Sequential mode error: Could not find previous image for camera {ref_cam_id} to use as style."
- return {"error": "conn_failed"}
- if context_mode != 'ADDITIONAL':
- if context.scene.qwen_use_custom_prompts:
- pos_prompt_text = context.scene.qwen_custom_prompt_seq_none.format(main_prompt=user_prompt)
- else:
- pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
- prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
- # If neither of the above, style_image_info remains None, handled below
-
- # Case 3: First image of a sequence, or separate generation, or no style source in sequential
- if style_image_info is None:
- # No style image is provided — remove the style loader node and
- # its reference from the prompt encoders.
- del prompt[NODES['style_map_loader']]
- if context_mode == 'ADDITIONAL' and not is_initial_image:
- # In ADDITIONAL mode the context/style loader IDs were swapped
- # above, so the style loader now sits on the node that image3
- # originally pointed to. Remove image3 (dangling after the
- # delete) but keep image2 (the context render).
- for nid in (NODES['pos_prompt'], NODES['neg_prompt']):
- prompt[nid]['inputs'].pop('image3', None)
- else:
- del prompt[NODES['pos_prompt']]['inputs']['image2']
- del prompt[NODES['neg_prompt']]['inputs']['image2']
- 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)
- else:
- pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
- prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
- else:
- # A style image is provided, so set the loader input.
- prompt[NODES['style_map_loader']]['inputs']['image'] = style_image_info['name']
- # External user-selected images can be arbitrarily large (10+ MP).
- # Scale them down to ~1 MP so the VAEEncode in the reference
- # latent chain doesn't blow up VRAM. StableGen's own renders
- # are already roughly 1 MP and don't need this.
- if use_external_this_frame:
- 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,
- "resolution_steps": 1,
- "image": [NODES['style_map_loader'], 0]
- },
- "class_type": "ImageScaleToTotalPixels",
- "_meta": {
- "title": "Scale External Style to 1MP"
- }
- }
- # 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]
-
- # --- Reference Latent chain (Qwen unzoom fix) ---
- self._build_qwen_reference_latent_chain(prompt, NODES)
-
- # --- Configure Sampler ---
- 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 # Typically 1.0 for this kind of edit
-
- # --- Set UNET model ---
- 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
-
- # --- Execute ---
- self._save_prompt_to_file(prompt, revision_dir)
- ws = self._connect_to_websocket(server_address, client_id)
- if ws is None:
- return {"error": "conn_failed"}
-
- images = None
- try:
- images = self._execute_prompt_and_get_images(ws, prompt, client_id, server_address, NODES)
- finally:
- if ws:
- ws.close()
-
- if images is None or (isinstance(images, dict) and "error" in images):
- return {"error": "conn_failed"}
-
- print(f"Qwen image generated with prompt: {prompt[NODES['pos_prompt']]['inputs']['prompt']}")
- return images[NODES['save_image']][0]
-
- # ------------------------------------------------------------------
- # FLUX.2 Klein – multi-reference edit generation
- # ------------------------------------------------------------------
- def generate_flux2_klein(self, context, camera_id=None):
- """Generates an image using the FLUX.2 Klein multi-reference edit workflow.
-
- Klein injects reference images via ReferenceLatent chains and
- understands image references ("image 1", "image 2") in prompts.
- Following BFL's official prompting guide, prompts are written as
- natural prose describing the desired transformation, front-loading
- the subject and referencing images by number.
-
- Reference layout:
- Ref 1 → structure / guidance map (depth, normal, workbench …)
- Ref 2 → style reference (external image or prev gen)
- Ref 3 → context render (sequential mode only)
- """
- server_address = context.preferences.addons[__package__].preferences.server_address
- client_id = str(uuid.uuid4())
- revision_dir = get_generation_dirs(context)["revision"]
-
- prompt = json.loads(prompt_text_flux2_klein)
-
- NODES = {
- 'unet_loader': "1",
- 'clip_loader': "2",
- 'vae_loader': "3",
- 'pos_prompt': "4", # CLIPTextEncode
- 'neg_prompt': "5", # CLIPTextEncode (empty)
- 'empty_latent': "6", # EmptyFlux2LatentImage
- 'scheduler': "7", # Flux2Scheduler
- 'ksampler': "8", # KSamplerSelect
- 'noise': "9", # RandomNoise
- 'guider': "10", # CFGGuider
- 'sampler': "11", # SamplerCustomAdvanced
- 'vae_decode': "12", # VAEDecode
- 'save_image': "13", # SaveImageWebsocket
- 'ref1_loader': "14", # LoadImage – structure (guidance map)
- 'ref2_loader': "15", # LoadImage – style
- 'ref3_loader': "16", # LoadImage – context render
- }
-
- # --- Resolution (must be multiple of 16 for FLUX.2 latent) ---
- if context.scene.generation_method == 'grid':
- width = self.operator._grid_width
- height = self.operator._grid_height
- else:
- width = context.scene.render.resolution_x
- height = context.scene.render.resolution_y
- width = (width // 16) * 16
- height = (height // 16) * 16
- prompt[NODES['empty_latent']]['inputs']['width'] = width
- prompt[NODES['empty_latent']]['inputs']['height'] = height
- prompt[NODES['scheduler']]['inputs']['width'] = width
- prompt[NODES['scheduler']]['inputs']['height'] = height
-
- # --- Sampler settings ---
- prompt[NODES['noise']]['inputs']['noise_seed'] = context.scene.seed
- prompt[NODES['scheduler']]['inputs']['steps'] = context.scene.steps
- prompt[NODES['ksampler']]['inputs']['sampler_name'] = context.scene.sampler
- prompt[NODES['guider']]['inputs']['cfg'] = context.scene.cfg
-
- # --- Model ---
- prompt[NODES['unet_loader']]['inputs']['unet_name'] = context.scene.model_name
-
- # --- CLIP: Detect 9B vs 4B from model name ---
- # 9B uses a larger Qwen 3 8B text encoder instead of 4B.
- # Default to fp8mixed (8.66 GB) for practical VRAM usage.
- # Full-precision qwen_3_8b.safetensors (16.4 GB) is also compatible.
- # Download from: Comfy-Org/vae-text-encorder-for-flux-klein-9b
- model_lower = context.scene.model_name.lower()
- if '9b' in model_lower:
- prompt[NODES['clip_loader']]['inputs']['clip_name'] = "qwen_3_8b_fp8mixed.safetensors"
-
- # --- 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}"
-
- # ─── Collect reference images ────────────────────────────────
- ref_images = []
- remove_ref1 = True
- remove_ref2 = True
- remove_ref3 = True
-
- is_initial_image = not self.operator._current_image > 0
- context_mode = context.scene.qwen_context_render_mode
- context_render_info = None
- style_image_info = None
-
- # --- Ref 1: Structure / guidance map (always attempt) ---
- guidance_map_type = context.scene.qwen_guidance_map_type
- guidance_map_info = self.operator._get_uploaded_image_info(
- context, "controlnet", subtype=guidance_map_type, camera_id=camera_id)
- if guidance_map_info:
- prompt[NODES['ref1_loader']]['inputs']['image'] = guidance_map_info['name']
- ref_images.append({'loader_node': NODES['ref1_loader']})
- remove_ref1 = False
-
- # --- Ref 3: Context render (sequential subsequent only) ---
- # Handled BEFORE Ref 2 so ADDITIONAL mode can swap slot ids (mirrors Qwen).
- remove_context = True
- if not is_initial_image and context_mode != 'NONE':
- context_render_info = self.operator._get_uploaded_image_info(
- context, "inpaint", subtype="render", camera_id=camera_id)
- if not context_render_info:
- self.operator._error = f"Klein context render enabled, but could not find context render for camera {camera_id}."
- return {"error": "conn_failed"}
- if context_render_info and context_mode == 'ADDITIONAL':
- # Swap loaders so context render is Ref 2 and style is Ref 3
- NODES['ref3_loader'], NODES['ref2_loader'] = NODES['ref2_loader'], NODES['ref3_loader']
- prompt[NODES['ref3_loader']]['inputs']['image'] = context_render_info['name']
- ref_images.append({'loader_node': NODES['ref3_loader']})
- remove_context = False
- if context.scene.qwen_use_custom_prompts:
- pos_prompt_text = context.scene.qwen_custom_prompt_seq_additional.format(main_prompt=user_prompt, camera_suffix=camera_suffix)
- else:
- pos_prompt_text = self._get_klein_default_prompts(context, is_initial_image).format(main_prompt=user_prompt, camera_suffix=camera_suffix)
- prompt[NODES['pos_prompt']]['inputs']['text'] = pos_prompt_text
-
- if remove_context and not remove_ref3:
- pass # already assigned
- elif remove_context:
- pass # will be deleted below
-
- # --- Ref 2: Style reference (optional) ---
- # Determine if we should use the external style image for this frame
- use_external = context.scene.qwen_use_external_style_image
- if use_external and not is_initial_image and context.scene.qwen_external_style_initial_only:
- use_external = False
- context.scene.sequential_ipadapter = True
-
- # Case 1: External Style Image
- if use_external:
- style_image_info = self.operator._get_uploaded_image_info(
- context, "custom",
- filename=bpy.path.abspath(context.scene.qwen_external_style_image))
- if style_image_info and 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, camera_suffix=camera_suffix)
- else:
- pos_prompt_text = self._get_klein_default_prompts(context, is_initial_image).format(main_prompt=user_prompt, camera_suffix=camera_suffix)
- prompt[NODES['pos_prompt']]['inputs']['text'] = pos_prompt_text
-
- # Case 1b: TRELLIS.2 input image as style
- if not style_image_info and getattr(context.scene, 'architecture_mode', '') == 'trellis2' and getattr(context.scene, 'qwen_use_trellis2_style', False):
- use_t2 = True
- if not is_initial_image and getattr(context.scene, 'qwen_trellis2_style_initial_only', False):
- use_t2 = False
- context.scene.sequential_ipadapter = True
- if use_t2:
- t2_path = getattr(context.scene, 'trellis2_last_input_image', '')
- if t2_path and os.path.exists(bpy.path.abspath(t2_path)):
- style_image_info = self.operator._get_uploaded_image_info(
- context, "custom", filename=bpy.path.abspath(t2_path))
- if style_image_info:
- if context.scene.qwen_use_custom_prompts:
- pos_prompt_text = context.scene.qwen_custom_prompt_initial.format(main_prompt=user_prompt, camera_suffix=camera_suffix)
- else:
- pos_prompt_text = self._get_klein_default_prompts(context, is_initial_image).format(main_prompt=user_prompt, camera_suffix=camera_suffix)
- prompt[NODES['pos_prompt']]['inputs']['text'] = pos_prompt_text
-
- # Case 2: Sequential (after first image)
- if not style_image_info and not is_initial_image:
- if context_mode == 'REPLACE_STYLE':
- style_image_info = context_render_info
- if context.scene.qwen_use_custom_prompts:
- pos_prompt_text = context.scene.qwen_custom_prompt_seq_replace.format(main_prompt=user_prompt, camera_suffix=camera_suffix)
- else:
- pos_prompt_text = self._get_klein_default_prompts(context, is_initial_image).format(main_prompt=user_prompt, camera_suffix=camera_suffix)
- prompt[NODES['pos_prompt']]['inputs']['text'] = pos_prompt_text
- elif context.scene.sequential_ipadapter:
- if context.scene.sequential_ipadapter_mode == 'original_render':
- # For original_render in Klein context, fall back to first mode since
- # the render is already used as the guidance map (Ref 1).
- ref_cam = 0
- else:
- ref_cam = (0 if context.scene.sequential_ipadapter_mode == 'first'
- else self.operator._current_image - 1)
- style_image_info = self.operator._get_uploaded_image_info(
- context, "generated", camera_id=ref_cam,
- material_id=self.operator._material_id)
- if context_mode != 'ADDITIONAL':
- if context.scene.qwen_use_custom_prompts:
- pos_prompt_text = context.scene.qwen_custom_prompt_seq_none.format(main_prompt=user_prompt, camera_suffix=camera_suffix)
- else:
- pos_prompt_text = self._get_klein_default_prompts(context, is_initial_image).format(main_prompt=user_prompt, camera_suffix=camera_suffix)
- prompt[NODES['pos_prompt']]['inputs']['text'] = pos_prompt_text
-
- # Case 3: No style image – first image or no style source
- if style_image_info is None:
- 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, camera_suffix=camera_suffix)
- else:
- pos_prompt_text = self._get_klein_default_prompts(context, is_initial_image).format(main_prompt=user_prompt, camera_suffix=camera_suffix)
- prompt[NODES['pos_prompt']]['inputs']['text'] = pos_prompt_text
- else:
- prompt[NODES['ref2_loader']]['inputs']['image'] = style_image_info['name']
- ref_images.append({'loader_node': NODES['ref2_loader']})
- remove_ref2 = False
-
- # Remove unused LoadImage nodes
- if remove_ref1:
- del prompt[NODES['ref1_loader']]
- if remove_ref2:
- del prompt[NODES['ref2_loader']]
- if remove_context:
- del prompt[NODES['ref3_loader']]
-
- # --- Build ReferenceLatent chain ---
- self._build_flux2_reference_chain(prompt, NODES, ref_images)
-
- # --- Execute ---
- self._save_prompt_to_file(prompt, revision_dir)
- ws = self._connect_to_websocket(server_address, client_id)
- if ws is None:
- return {"error": "conn_failed"}
-
- images = None
- try:
- images = self._execute_prompt_and_get_images(
- ws, prompt, client_id, server_address, NODES)
- finally:
- if ws:
- ws.close()
-
- if images is None or (isinstance(images, dict) and "error" in images):
- return {"error": "conn_failed"}
-
- print(f"FLUX.2 Klein image generated with prompt: {prompt[NODES['pos_prompt']]['inputs']['text']}")
- return images[NODES['save_image']][0]
-
-
- def generate(self, context, controlnet_info=None, ipadapter_ref_info=None):
- """
- Generates the image using ComfyUI.
- :param context: Blender context.
- :param controlnet_info: Dict of uploaded controlnet image info.
- :param ipadapter_ref_info: Uploaded IPAdapter reference image info.
- :return: Generated image binary data.
- """
-
- # Setup connection parameters
- server_address = context.preferences.addons[__package__].preferences.server_address
- client_id = str(uuid.uuid4())
- # Get revision dir for debug file
- revision_dir = get_generation_dirs(context)["revision"]
-
- # Initialize the prompt template and get node mappings
- prompt, NODES = self._create_base_prompt(context)
-
- # Set model resolution
- self._configure_resolution(prompt, context, NODES)
-
- if ipadapter_ref_info:
- # Configure IPAdapter settings
- self._configure_ipadapter(prompt, context, ipadapter_ref_info, NODES)
- else:
- # Remove IPAdapter nodes if not used
- for node_id in ['235', '236', '237']:
- if node_id in prompt:
- del prompt[node_id]
-
- # Build controlnet chain
- prompt = self._build_controlnet_chain(prompt, context, controlnet_info, NODES)
-
- # Save prompt for debugging (in revision dir)
- self._save_prompt_to_file(prompt, revision_dir)
-
- # Execute generation and get results
- ws = self._connect_to_websocket(server_address, client_id)
-
- if ws is None:
- return {"error": "conn_failed"} # Connection error
-
- images = None
- try:
- images = self._execute_prompt_and_get_images(ws, prompt, client_id, server_address, NODES)
- finally:
- if ws:
- ws.close()
-
- if images is None or isinstance(images, dict) and "error" in images:
- return {"error": "conn_failed"}
-
- print(f"Image generated with prompt: {context.scene.comfyui_prompt}")
-
- # Return the generated image from the save_image node
- return images[NODES['save_image']][0]
-
def _check_server_alive(self, server_address, timeout=None):
"""Return True if the ComfyUI server responds to a lightweight request."""
if timeout is None:
@@ -1167,60 +57,6 @@ class WorkflowManager:
except Exception:
return None, None
- @staticmethod
- def _cleanup_trellis2_temp_files():
- """Remove stale TRELLIS2 IPC temp directories and voxelgrid cache.
-
- The ComfyUI-TRELLIS2 custom node creates one temp directory
- (``trellis2_<random>``) per worker subprocess via a module-level
- global and reuses it for the entire ComfyUI session. Deleting
- that directory while the worker is alive causes "Parent directory
- does not exist" errors on the next generation.
-
- Since only the most recently created directory can belong to the
- current (running) worker, we delete all *older* ``trellis2_*``
- directories entirely and only clean up the stale ``.pt`` tensor
- files inside the newest one, leaving the directory itself intact.
- """
- import tempfile
- import glob
- import shutil
-
- tmp_root = tempfile.gettempdir()
-
- dirs = sorted(
- (d for d in glob.glob(os.path.join(tmp_root, 'trellis2_*'))
- if os.path.isdir(d)),
- key=os.path.getctime,
- )
-
- if not dirs:
- return
-
- # Everything except the newest is from a dead worker — remove entirely.
- for d in dirs[:-1]:
- try:
- shutil.rmtree(d)
- except OSError:
- pass
-
- # Newest directory may be in use — only purge .pt files inside it.
- newest = dirs[-1]
- for f in os.listdir(newest):
- if f.endswith('.pt'):
- try:
- os.remove(os.path.join(newest, f))
- except OSError:
- pass
-
- # Voxelgrid cache (Windows: C:\tmp\trellis2_cache)
- for cache_dir in ('/tmp/trellis2_cache', r'C:\tmp\trellis2_cache'):
- if os.path.isdir(cache_dir):
- try:
- shutil.rmtree(cache_dir)
- except OSError:
- pass
-
def _flush_comfyui_vram(self, server_address, retries=3, label="Post-generation"):
"""Unload all models and free VRAM on the ComfyUI server.
@@ -1228,1704 +64,91 @@ class WorkflowManager:
actual unloading happens asynchronously in ComfyUI's execution loop.
This method therefore:
1. Sends ``/free`` (unload models + free memory).
- 2. Clears the execution cache history and queue.
- 3. Sends ``/free`` *again* — the first round triggers model unloads
- whose CUDA tensors may still reside in PyTorch's cache; the
- second round triggers ``soft_empty_cache`` which calls
- ``torch.cuda.empty_cache()`` on the now-freed allocations.
- 4. Polls ``/system_stats`` waiting for VRAM to increase.
- Retries the whole sequence up to *retries* times.
- """
- import time
-
- vram_before, vram_total = self._get_vram_stats(server_address)
- if vram_before is not None:
- print(f"[TRELLIS2] {label} VRAM before flush: {vram_before:.0f} MB free / {vram_total:.0f} MB total")
-
- # Skip the flush entirely if VRAM is already ≥90% free.
- if vram_total and vram_before > vram_total * 0.9:
- print(f"[TRELLIS2] {label} VRAM already ≥90% free — skipping flush.")
- return True
-
- def _send_free():
- d = json.dumps({"unload_models": True, "free_memory": True}).encode('utf-8')
- r = urllib.request.Request(
- f"http://{server_address}/free", data=d,
- headers={'Content-Type': 'application/json'}, method='POST'
- )
- urllib.request.urlopen(r, timeout=get_timeout('api'))
-
- def _clear_history():
- d = json.dumps({"clear": True}).encode('utf-8')
- r = urllib.request.Request(
- f"http://{server_address}/history", data=d,
- headers={'Content-Type': 'application/json'}, method='POST'
- )
- urllib.request.urlopen(r, timeout=get_timeout('api'))
-
- def _clear_queue():
- d = json.dumps({"clear": True}).encode('utf-8')
- r = urllib.request.Request(
- f"http://{server_address}/queue", data=d,
- headers={'Content-Type': 'application/json'}, method='POST'
- )
- urllib.request.urlopen(r, timeout=get_timeout('api'))
-
- for attempt in range(1, retries + 1):
- try:
- # Round 1: Unload models
- _send_free()
- time.sleep(1)
-
- # Clear caches
- try:
- _clear_history()
- except Exception:
- pass
- try:
- _clear_queue()
- except Exception:
- pass
-
- # Round 2: Free the now-released CUDA allocations
- _send_free()
-
- # Poll VRAM (up to 8s)
- for check in range(8):
- time.sleep(1)
- vram_after, _ = self._get_vram_stats(server_address)
- if vram_after is not None:
- print(f"[TRELLIS2] {label} VRAM after flush (check {check+1}): {vram_after:.0f} MB free")
- if vram_before is not None and vram_after > vram_before + 500:
- print(f"[TRELLIS2] {label} flush verified: freed {vram_after - vram_before:.0f} MB")
- return True
- if vram_total is not None and vram_after > vram_total * 0.7:
- print(f"[TRELLIS2] {label} flush OK: >70% VRAM free")
- return True
-
- print(f"[TRELLIS2] {label} flush attempt {attempt}/{retries}: VRAM didn't free enough — retrying")
- except Exception as e:
- print(f"[TRELLIS2] {label} flush attempt {attempt}/{retries} failed: {e}")
-
- if attempt < retries:
- time.sleep(2)
-
- print(f"[TRELLIS2] Warning: {label} VRAM flush unverified after {retries} attempts")
- return False
-
- # ------------------------------------------------------------------
- # Txt2Img — lightweight single-image generation for prompt-to-image
- # ------------------------------------------------------------------
-
- def generate_txt2img(self, context, seed_override=None):
- """Generate a single image from the scene prompt and checkpoint.
-
- Builds a minimal txt2img ComfyUI workflow (with LoRA support) and
- returns the generated image as raw PNG bytes delivered via the
- ``SaveImageWebsocket`` node.
-
- Args:
- seed_override: If not None, overrides ``scene.seed`` in the
- built workflow. Useful for preview-gallery generation from
- a background thread where ``scene.seed`` may not propagate.
-
- Returns:
- bytes: Raw image data on success.
- dict: ``{"error": "..."}`` on failure.
- """
- server_address = context.preferences.addons[__package__].preferences.server_address
- client_id = str(uuid.uuid4())
- scene = context.scene
- architecture = scene.model_architecture # synced from texture_mode
-
- # When using TRELLIS.2 with native/none texturing the backbone is
- # synced from trellis2_initial_image_arch instead of texture_mode.
- # Double-check here in case the property wasn't synced yet.
- if getattr(scene, 'architecture_mode', '') == 'trellis2':
- tex_mode = getattr(scene, 'trellis2_texture_mode', 'native')
- if tex_mode not in ('sdxl', 'flux1', 'qwen_image_edit', 'flux2_klein'):
- architecture = getattr(scene, 'trellis2_initial_image_arch', 'sdxl')
-
- if architecture == 'qwen_image_edit':
- prompt, save_node = self._build_qwen_txt2img(context)
- elif architecture == 'flux1':
- prompt, save_node = self._build_flux_txt2img(context)
- elif architecture == 'flux2_klein':
- prompt, save_node = self._build_klein_txt2img(context)
- else:
- prompt, save_node = self._build_sdxl_txt2img(context)
-
- # Patch seed if caller supplies an override (thread-safe)
- if seed_override is not None:
- for _node in prompt.values():
- _inp = _node.get('inputs', {})
- if 'seed' in _inp:
- _inp['seed'] = seed_override
- if 'noise_seed' in _inp:
- _inp['noise_seed'] = seed_override
-
- ws = self._connect_to_websocket(server_address, client_id)
- if ws is None:
- return {"error": "WebSocket connection failed"}
-
- # Let the operator close this WS on cancel
- if hasattr(self.operator, '_active_ws'):
- self.operator._active_ws = ws
- # Also set on class level so cancel from a new instance can find it
- type(self.operator)._active_ws = ws
-
- try:
- images = self._execute_prompt_and_get_images(
- ws, prompt, client_id, server_address, {"save_image": save_node}
- )
- finally:
- if hasattr(self.operator, '_active_ws'):
- self.operator._active_ws = None
- type(self.operator)._active_ws = None
- try:
- ws.close()
- except Exception:
- pass
-
- if images is None or not isinstance(images, dict) or not images:
- return {"error": "txt2img generation failed"}
-
- if save_node not in images or not images[save_node]:
- return {"error": "No image received from txt2img"}
-
- return images[save_node][0]
-
- def _build_sdxl_txt2img(self, context):
- """Return (prompt_dict, save_node_id) for a minimal SDXL txt2img with LoRA support."""
- scene = context.scene
- prompt = {
- "1": {
- "class_type": "CheckpointLoaderSimple",
- "inputs": {"ckpt_name": scene.model_name}
- },
- "2": {
- "class_type": "CLIPTextEncode",
- "inputs": {"text": _generation_prompt(scene), "clip": ["1", 1]}
- },
- "3": {
- "class_type": "CLIPTextEncode",
- "inputs": {"text": scene.comfyui_negative_prompt, "clip": ["1", 1]}
- },
- "4": {
- "class_type": "EmptyLatentImage",
- "inputs": {"width": 1024, "height": 1024, "batch_size": 1}
- },
- "5": {
- "class_type": "KSampler",
- "inputs": {
- "model": ["1", 0],
- "positive": ["2", 0],
- "negative": ["3", 0],
- "latent_image": ["4", 0],
- "seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31),
- "steps": scene.steps,
- "cfg": scene.cfg,
- "sampler_name": scene.sampler,
- "scheduler": scene.scheduler,
- "denoise": 1.0
- }
- },
- "6": {
- "class_type": "VAEDecode",
- "inputs": {"samples": ["5", 0], "vae": ["1", 2]}
- },
- "7": {
- "class_type": "SaveImageWebsocket",
- "inputs": {"images": ["6", 0]}
- }
- }
-
- # LoRA chain (LoraLoader): model + clip
- prompt, final_model, final_clip = self._build_lora_chain(
- prompt, context,
- initial_model_input=["1", 0],
- initial_clip_input=["1", 1],
- start_node_id=300,
- lora_class_type="LoraLoader"
- )
- prompt["5"]["inputs"]["model"] = final_model
- prompt["2"]["inputs"]["clip"] = final_clip
- prompt["3"]["inputs"]["clip"] = final_clip
-
- return prompt, "7"
-
- # ── PBR Decomposition (Marigold IID) ───────────────────────────────
- def generate_pbr_maps(self, context, input_image_path, model_name=None,
- force_native_resolution=False):
- """Run a Marigold model on a generated texture and collect the output.
-
- Uploads *input_image_path* to the ComfyUI server, runs it through the
- ``MarigoldModelLoader`` + ``MarigoldDepthEstimation_v2`` pipeline,
- and collects the resulting map(s) via ``SaveImageWebsocket``.
-
- Different models produce different outputs:
-
- * **IID-Appearance** → 3 images: [Albedo, Roughness, Metallicity]
- * **IID-Lighting** → 3 images: [Albedo, Shading, Residual]
- * **Normals** → 1 image: [Normal map]
- * **Depth** → 1 image: [Depth map]
-
- Args:
- input_image_path: Absolute path to the generated texture PNG.
- model_name: HuggingFace model repo name to use, e.g.
- ``'prs-eth/marigold-iid-appearance-v1-1'``. If *None*,
- defaults to the IID-Appearance model.
- force_native_resolution: If *True*, always process at the
- image's native longest edge (rounded to 64) regardless
- of the user-configured resolution settings. Used by
- the tiling path so each tile gets full-detail processing.
-
- Returns:
- list[bytes] | dict: A list of raw-PNG byte buffers on success,
- or ``{"error": "..."}`` on failure.
- """
- server_address = context.preferences.addons[__package__].preferences.server_address
- client_id = str(uuid.uuid4())
- scene = context.scene
-
- if model_name is None:
- model_name = 'prs-eth/marigold-iid-appearance-v1-1'
-
- # Pre-flight: verify that ComfyUI-Marigold nodes are installed
- required_nodes = ['MarigoldModelLoader', 'MarigoldDepthEstimation_v2']
- for node_class in required_nodes:
- try:
- resp = urllib.request.urlopen(
- f"http://{server_address}/object_info/{node_class}",
- timeout=get_timeout('api')
- )
- data = json.loads(resp.read())
- if node_class not in data:
- return {"error": f"ComfyUI-Marigold node '{node_class}' not found. "
- f"Install ComfyUI-Marigold (run installer option 9 "
- f"or git clone https://github.com/kijai/ComfyUI-Marigold "
- f"into ComfyUI/custom_nodes/) and ensure 'diffusers>=0.28' "
- f"is installed in ComfyUI's Python, then restart ComfyUI."}
- except urllib.error.HTTPError as e:
- if e.code == 404:
- return {"error": f"ComfyUI-Marigold node '{node_class}' not found. "
- f"Install ComfyUI-Marigold (run installer option 9 "
- f"or git clone https://github.com/kijai/ComfyUI-Marigold "
- f"into ComfyUI/custom_nodes/) and ensure 'diffusers>=0.28' "
- f"is installed in ComfyUI's Python, then restart ComfyUI."}
- return {"error": f"PBR model error: failed to query ComfyUI for node "
- f"'{node_class}' (HTTP {e.code}). Is ComfyUI running?"}
- except Exception as e:
- return {"error": f"PBR model error: could not reach ComfyUI at "
- f"'{server_address}' while checking for node '{node_class}' "
- f"({type(e).__name__}: {e}). Verify ComfyUI is running and "
- f"the server address is correct."}
-
- # Upload the source image to ComfyUI's input folder
- from .generator import upload_image_to_comfyui
- image_info = upload_image_to_comfyui(server_address, input_image_path)
- if image_info is None:
- return {"error": f"Failed to upload image for PBR decomposition: {input_image_path}"}
-
- uploaded_name = image_info.get("name", os.path.basename(input_image_path))
-
- # Determine the processing resolution.
- # When 'Native Resolution' is enabled (or forced by tiling), use
- # the image's own longest edge (rounded to 64) so Marigold
- # processes at full detail.
- # Otherwise fall back to the user-specified fixed resolution.
- use_native = force_native_resolution or getattr(
- scene, 'pbr_use_native_resolution', False)
- if use_native:
- try:
- from PIL import Image as _PILImage
- with _PILImage.open(input_image_path) as _img:
- _w, _h = _img.size
- proc_res = max(_w, _h)
- proc_res = ((proc_res + 63) // 64) * 64
- print(f"[StableGen] Marigold: native resolution {proc_res}px "
- f"(input {_w}\u00d7{_h})")
- except Exception:
- proc_res = scene.pbr_processing_resolution
- else:
- proc_res = scene.pbr_processing_resolution
-
- # Build the minimal ComfyUI workflow
- prompt = {
- "1": {
- "class_type": "LoadImage",
- "inputs": {"image": uploaded_name}
- },
- "2": {
- "class_type": "MarigoldModelLoader",
- "inputs": {
- "model": model_name,
- }
- },
- "3": {
- "class_type": "MarigoldDepthEstimation_v2",
- "inputs": {
- "marigold_model": ["2", 0],
- "image": ["1", 0],
- "seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31),
- "denoise_steps": scene.pbr_denoise_steps,
- "ensemble_size": scene.pbr_ensemble_size,
- "processing_resolution": proc_res,
- "scheduler": "LCMScheduler",
- "use_taesd_vae": False,
- # Keep loaded to avoid offloading to CPU — newer diffusers
- # raises ValueError when moving a float16 pipeline to CPU.
- "keep_model_loaded": True,
- }
- },
- "4": {
- "class_type": "SaveImageWebsocket",
- "inputs": {"images": ["3", 0]}
- }
- }
-
- NODES = {"save_image": "4"}
-
- ws = self._connect_to_websocket(server_address, client_id)
- if ws is None:
- return {"error": "WebSocket connection failed for PBR decomposition"}
-
- if hasattr(self.operator, '_active_ws'):
- self.operator._active_ws = ws
- type(self.operator)._active_ws = ws
-
- try:
- images = self._execute_prompt_and_get_images(
- ws, prompt, client_id, server_address, NODES
- )
- finally:
- if hasattr(self.operator, '_active_ws'):
- self.operator._active_ws = None
- type(self.operator)._active_ws = None
- try:
- ws.close()
- except Exception:
- pass
-
- if images is None or not isinstance(images, dict) or not images:
- return {"error": "PBR decomposition failed — no output received"}
-
- save_node = NODES['save_image']
- if save_node not in images or not images[save_node]:
- return {"error": "PBR decomposition failed — no images from save node"}
-
- pbr_maps = images[save_node] # list of raw PNG bytes
- print(f"[StableGen] Marigold '{model_name}' returned {len(pbr_maps)} image(s)")
-
- return pbr_maps
-
- def generate_delight_map(self, context, input_image_path):
- """Run StableDelight on an image to produce a specular-free (delighted) version.
-
- Uses the ``LoadStableDelightModel`` + ``ApplyStableDelight`` nodes
- from the *ComfyUI_StableDelight_ll* custom-node package.
-
- The delighted image preserves diffuse shading and texture detail
- while removing specular highlights — making it a high-quality
- alternative to Marigold IID's flat albedo for the Base Color slot.
-
- Args:
- input_image_path: Absolute path to the generated texture PNG.
-
- Returns:
- bytes | dict: Raw PNG bytes of the delighted image on success,
- or ``{"error": "..."}`` on failure.
- """
- server_address = context.preferences.addons[__package__].preferences.server_address
- client_id = str(uuid.uuid4())
- scene = context.scene
-
- # Pre-flight: verify that StableDelight nodes are installed
- required_nodes = ['LoadStableDelightModel', 'ApplyStableDelight']
- for node_class in required_nodes:
- try:
- resp = urllib.request.urlopen(
- f"http://{server_address}/object_info/{node_class}",
- timeout=get_timeout('api')
- )
- data = json.loads(resp.read())
- if node_class not in data:
- return {"error": f"StableDelight node '{node_class}' not found. "
- f"Install ComfyUI_StableDelight_ll (run installer "
- f"option 10 or git clone "
- f"https://github.com/lldacing/ComfyUI_StableDelight_ll "
- f"into ComfyUI/custom_nodes/) and download the model "
- f"'Stable-X/yoso-delight-v0-4-base' into "
- f"ComfyUI/models/diffusers/, then restart ComfyUI."}
- except urllib.error.HTTPError as e:
- if e.code == 404:
- return {"error": f"StableDelight node '{node_class}' not found. "
- f"Install ComfyUI_StableDelight_ll (run installer "
- f"option 10 or git clone "
- f"https://github.com/lldacing/ComfyUI_StableDelight_ll "
- f"into ComfyUI/custom_nodes/) and download the model "
- f"'Stable-X/yoso-delight-v0-4-base' into "
- f"ComfyUI/models/diffusers/, then restart ComfyUI."}
- return {"error": f"Normal estimation error: failed to query ComfyUI for node "
- f"'{node_class}' (HTTP {e.code}). Is ComfyUI running?"}
- except Exception as e:
- return {"error": f"Normal estimation error: could not reach ComfyUI at "
- f"'{server_address}' while checking for node '{node_class}' "
- f"({type(e).__name__}: {e}). Verify ComfyUI is running and "
- f"the server address is correct."}
-
- # Resolve model path — the node expects a relative path under
- # ComfyUI/models/diffusers/ containing model_index.json.
- delight_model_path = "Stable-X--yoso-delight-v0-4-base"
-
- # Upload the source image to ComfyUI's input folder
- from .generator import upload_image_to_comfyui
- image_info = upload_image_to_comfyui(server_address, input_image_path)
- if image_info is None:
- return {"error": f"Failed to upload image for StableDelight: {input_image_path}"}
-
- uploaded_name = image_info.get("name", os.path.basename(input_image_path))
-
- # Determine the processing resolution.
- #
- # The official Stable-X Predictor uses processing_resolution=2048,
- # while the ComfyUI node defaults to 1024. Both use
- # MarigoldImageProcessor.preprocess() → resize_to_max_edge() which
- # scales the image so its *longest edge* equals processing_resolution,
- # then pads to a multiple of vae_scale_factor (8).
- #
- # Problems with a fixed resolution:
- # - Too small (e.g. 1024): images larger than 1024px get downscaled
- # then bilinearly upscaled back → blurry output.
- # - Too large: small images get massively upscaled → wastes VRAM
- # and can exceed available memory.
- # - resolution=0: triggers skip_preprocess=True in the node, which
- # has a 3D/4D tensor bug in the pipeline's VAE encoder.
- #
- # Solution: use max(native_longest_edge, 1024). This ensures:
- # - Large images (>1024) are processed at native res (no downscale).
- # - Small images (<1024) are upscaled to 1024 — giving the SD1.5-
- # based model enough spatial tokens to produce clean output.
- # The value is rounded up to the nearest multiple of 64 (not just 8)
- # to match the official Stable-X resize_image() convention, which
- # aligns to 64 for clean integer dimensions through every UNet level
- # (VAE /8, then 3 downsampling stages at /2 each = 64× total).
- try:
- from PIL import Image as _PILImage
- with _PILImage.open(input_image_path) as _img:
- _w, _h = _img.size
- # Floor: at least 1024 (the model's ComfyUI default)
- # Ceiling: native resolution (avoid downscaling)
- proc_res = max(max(_w, _h), 1024)
- # Round up to next multiple of 64 (official alignment)
- proc_res = ((proc_res + 63) // 64) * 64
- print(f"[StableGen] StableDelight: processing at {proc_res}px "
- f"(input {_w}×{_h})")
- except Exception:
- proc_res = 1024 # safe fallback
-
- # Build the ComfyUI workflow
- prompt = {
- "1": {
- "class_type": "LoadImage",
- "inputs": {"image": uploaded_name}
- },
- "2": {
- "class_type": "LoadStableDelightModel",
- "inputs": {
- "model": delight_model_path,
- "device": "AUTO",
- }
- },
- "3": {
- "class_type": "ApplyStableDelight",
- "inputs": {
- "model": ["2", 0],
- "images": ["1", 0],
- "strength": getattr(scene, 'pbr_delight_strength', 1.0),
- "resolution": proc_res,
- # Official Stable-X Predictor uses LANCZOS for resize.
- # F.interpolate (used by MarigoldImageProcessor) doesn't
- # support lanczos, but "bicubic" is the closest PyTorch
- # equivalent and much sharper than "bilinear" for both
- # input downscale and output upscale.
- "upscale_method": "bicubic",
- }
- },
- "4": {
- "class_type": "SaveImageWebsocket",
- "inputs": {"images": ["3", 0]}
- }
- }
-
- NODES = {"save_image": "4"}
-
- ws = self._connect_to_websocket(server_address, client_id)
- if ws is None:
- return {"error": "WebSocket connection failed for StableDelight"}
-
- if hasattr(self.operator, '_active_ws'):
- self.operator._active_ws = ws
- type(self.operator)._active_ws = ws
-
- try:
- images = self._execute_prompt_and_get_images(
- ws, prompt, client_id, server_address, NODES
- )
- finally:
- if hasattr(self.operator, '_active_ws'):
- self.operator._active_ws = None
- type(self.operator)._active_ws = None
- try:
- ws.close()
- except Exception:
- pass
-
- if images is None or not isinstance(images, dict) or not images:
- return {"error": "StableDelight failed — no output received"}
-
- save_node = NODES['save_image']
- if save_node not in images or not images[save_node]:
- return {"error": "StableDelight failed — no images from save node"}
-
- delight_images = images[save_node]
- print(f"[StableGen] StableDelight returned {len(delight_images)} image(s)")
-
- # StableDelight returns a single image
- return delight_images[0] if delight_images else {"error": "StableDelight returned empty"}
-
- def _build_klein_txt2img(self, context):
- """Return (prompt_dict, save_node_id) for a minimal FLUX.2 Klein txt2img.
-
- Uses the same node types as the full Klein multi-reference workflow
- (UNETLoader, CLIPLoader with type flux2, VAELoader, Flux2Scheduler,
- CFGGuider, SamplerCustomAdvanced) but without any reference images.
- """
- scene = context.scene
- unet_name = scene.model_name
-
- # Detect 9B vs 4B CLIP from model name
- model_lower = unet_name.lower()
- clip_name = "qwen_3_8b_fp8mixed.safetensors" if '9b' in model_lower else "qwen_3_4b.safetensors"
-
- prompt = {
- "1": {
- "class_type": "UNETLoader",
- "inputs": {"unet_name": unet_name, "weight_dtype": "default"}
- },
- "2": {
- "class_type": "CLIPLoader",
- "inputs": {"clip_name": clip_name, "type": "flux2"}
- },
- "3": {
- "class_type": "VAELoader",
- "inputs": {"vae_name": "flux2-vae.safetensors"}
- },
- "4": {
- "class_type": "CLIPTextEncode",
- "inputs": {"text": _generation_prompt(scene), "clip": ["2", 0]}
- },
- "5": {
- "class_type": "CLIPTextEncode",
- "inputs": {"text": "", "clip": ["2", 0]}
- },
- "6": {
- "class_type": "EmptyFlux2LatentImage",
- "inputs": {"width": 1024, "height": 1024, "batch_size": 1}
- },
- "7": {
- "class_type": "Flux2Scheduler",
- "inputs": {"steps": scene.steps, "width": 1024, "height": 1024}
- },
- "8": {
- "class_type": "KSamplerSelect",
- "inputs": {"sampler_name": scene.sampler}
- },
- "9": {
- "class_type": "RandomNoise",
- "inputs": {"noise_seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31)}
- },
- "10": {
- "class_type": "CFGGuider",
- "inputs": {
- "model": ["1", 0],
- "positive": ["4", 0],
- "negative": ["5", 0],
- "cfg": scene.cfg
- }
- },
- "11": {
- "class_type": "SamplerCustomAdvanced",
- "inputs": {
- "noise": ["9", 0],
- "guider": ["10", 0],
- "sampler": ["8", 0],
- "sigmas": ["7", 0],
- "latent_image": ["6", 0]
- }
- },
- "12": {
- "class_type": "VAEDecode",
- "inputs": {"samples": ["11", 0], "vae": ["3", 0]}
- },
- "13": {
- "class_type": "SaveImageWebsocket",
- "inputs": {"images": ["12", 0]}
- }
- }
-
- return prompt, "13"
-
- def _build_flux_txt2img(self, context):
- """Return (prompt_dict, save_node_id) for a minimal Flux txt2img with LoRA support."""
- scene = context.scene
- unet_name = scene.model_name
- is_gguf = ".gguf" in unet_name.lower()
- unet_loader_class = "UnetLoaderGGUF" if is_gguf else "UNETLoader"
-
- prompt = {
- "10": {
- "class_type": "VAELoader",
- "inputs": {"vae_name": "ae.sft"}
- },
- "11": {
- "class_type": "DualCLIPLoader",
- "inputs": {
- "clip_name1": "t5xxl_fp8_e4m3fn.safetensors",
- "clip_name2": "clip_l.safetensors",
- "type": "flux",
- "device": "default"
- }
- },
- "12": {
- "class_type": unet_loader_class,
- "inputs": {"unet_name": unet_name}
- },
- "6": {
- "class_type": "CLIPTextEncode",
- "inputs": {"text": _generation_prompt(scene), "clip": ["11", 0]}
- },
- "26": {
- "class_type": "FluxGuidance",
- "inputs": {"guidance": scene.cfg, "conditioning": ["6", 0]}
- },
- "25": {
- "class_type": "RandomNoise",
- "inputs": {"noise_seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31)}
- },
- "16": {
- "class_type": "KSamplerSelect",
- "inputs": {"sampler_name": scene.sampler}
- },
- "17": {
- "class_type": "BasicScheduler",
- "inputs": {
- "scheduler": scene.scheduler,
- "steps": scene.steps,
- "denoise": 1.0,
- "model": ["12", 0]
- }
- },
- "22": {
- "class_type": "BasicGuider",
- "inputs": {"model": ["12", 0], "conditioning": ["26", 0]}
- },
- "30": {
- "class_type": "EmptyLatentImage",
- "inputs": {"width": 1024, "height": 1024, "batch_size": 1}
- },
- "13": {
- "class_type": "SamplerCustomAdvanced",
- "inputs": {
- "noise": ["25", 0],
- "guider": ["22", 0],
- "sampler": ["16", 0],
- "sigmas": ["17", 0],
- "latent_image": ["30", 0]
- }
- },
- "8": {
- "class_type": "VAEDecode",
- "inputs": {"samples": ["13", 0], "vae": ["10", 0]}
- },
- "7": {
- "class_type": "SaveImageWebsocket",
- "inputs": {"images": ["8", 0]}
- }
- }
-
- # LoRA chain (LoraLoaderModelOnly): model only, no clip
- prompt, final_model, _ = self._build_lora_chain(
- prompt, context,
- initial_model_input=["12", 0],
- initial_clip_input=["12", 0], # dummy, not used by LoraLoaderModelOnly
- start_node_id=300,
- lora_class_type="LoraLoaderModelOnly"
- )
- prompt["17"]["inputs"]["model"] = final_model
- prompt["22"]["inputs"]["model"] = final_model
-
- return prompt, "7"
-
- def _build_qwen_txt2img(self, context):
- """Return (prompt_dict, save_node_id) for a minimal Qwen txt2img.
-
- Qwen-Image-Edit can generate images from text alone when no input
- images are provided to the ``TextEncodeQwenImageEditPlus`` nodes.
- """
- scene = context.scene
- unet_name = scene.model_name
- is_gguf = ".gguf" in unet_name.lower()
- unet_loader_class = "UnetLoaderGGUF" if is_gguf else "UNETLoader"
-
- prompt = {
- "3": {
- "class_type": "CLIPLoader",
- "inputs": {
- "clip_name": "qwen_2.5_vl_7b_fp8_scaled.safetensors",
- "type": "qwen_image",
- "device": "default"
- }
- },
- "4": {
- "class_type": "VAELoader",
- "inputs": {"vae_name": "qwen_image_vae.safetensors"}
- },
- "13": {
- "class_type": unet_loader_class,
- "inputs": {"unet_name": unet_name}
- },
- "6": {
- "class_type": "ModelSamplingAuraFlow",
- "inputs": {"shift": 3, "model": ["13", 0]}
- },
- "9": {
- "class_type": "CFGNorm",
- "inputs": {"strength": 1, "model": ["6", 0]}
- },
- "12": {
- "class_type": "TextEncodeQwenImageEditPlus",
- "inputs": {
- "prompt": _generation_prompt(scene),
- "clip": ["3", 0]
- }
- },
- "11": {
- "class_type": "TextEncodeQwenImageEditPlus",
- "inputs": {
- "prompt": "",
- "clip": ["3", 0]
- }
- },
- "30": {
- "class_type": "EmptyLatentImage",
- "inputs": {"width": 1024, "height": 1024, "batch_size": 1}
- },
- "1": {
- "class_type": "KSampler",
- "inputs": {
- "model": ["9", 0],
- "positive": ["12", 0],
- "negative": ["11", 0],
- "latent_image": ["30", 0],
- "seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31),
- "steps": scene.steps,
- "cfg": scene.cfg,
- "sampler_name": scene.sampler,
- "scheduler": scene.scheduler,
- "denoise": 1.0
- }
- },
- "2": {
- "class_type": "VAEDecode",
- "inputs": {"samples": ["1", 0], "vae": ["4", 0]}
- },
- "7": {
- "class_type": "SaveImageWebsocket",
- "inputs": {"images": ["2", 0]}
- }
- }
-
- # LoRA chain: Qwen uses NunchakuQwenImageLoraLoader for nunchaku
- # models, LoraLoaderModelOnly otherwise
- is_nunchaku = unet_name.lower().endswith('.safetensors')
- lora_class = "NunchakuQwenImageLoraLoader" if is_nunchaku else "LoraLoaderModelOnly"
-
- prompt, final_model, _ = self._build_lora_chain(
- prompt, context,
- initial_model_input=["13", 0],
- initial_clip_input=["13", 0], # dummy
- start_node_id=500,
- lora_class_type=lora_class
- )
- # ModelSamplingAuraFlow takes the final LoRA output
- prompt["6"]["inputs"]["model"] = final_model
-
- return prompt, "7"
-
- @staticmethod
- def _is_local_server(server_address):
- """Return True if server_address points to localhost."""
- host = server_address.split(':')[0].strip()
- return host in ('127.0.0.1', 'localhost', '0.0.0.0', '::1', '')
-
- def generate_trellis2(self, context, input_image_path):
- """
- Generates a 3D mesh using TRELLIS.2 via ComfyUI.
-
- Uploads an input image, runs the TRELLIS.2 pipeline (background removal,
- conditioning, shape generation, optionally texture generation, GLB export),
- and downloads the resulting GLB file from the ComfyUI server.
- VRAM is always flushed before AND after generation.
-
- Args:
- context: Blender context.
- input_image_path: Local path to the input image file.
-
- Returns:
- bytes: GLB file binary data on success.
- dict: {"error": "message"} on failure.
- """
- import time
-
- server_address = context.preferences.addons[__package__].preferences.server_address
-
- # Pre-generation flush: free any loaded diffusion/other models so
- # TRELLIS.2 has maximum VRAM available.
- print("[TRELLIS2] Pre-generation VRAM flush — freeing loaded models...")
- self._flush_comfyui_vram(server_address, label="Pre-generation")
-
- time.sleep(1) # Brief pause for CUDA memory to be released
-
- # Verify the server is alive before proceeding
- if not self._check_server_alive(server_address):
- return {"error": "ComfyUI server is not responding. Please restart it and try again."}
-
- try:
- client_id = str(uuid.uuid4())
- result = self._generate_trellis2_inner(
- context, input_image_path, server_address, client_id)
- return result
- finally:
- self._flush_comfyui_vram(server_address, label="Post-generation")
- self._cleanup_trellis2_temp_files()
-
- def _generate_trellis2_inner(self, context, input_image_path, server_address, client_id):
- """Inner implementation of generate_trellis2, called within a try/finally VRAM flush."""
- import urllib.parse
-
- # Upload the input image to ComfyUI
- from .generator import upload_image_to_comfyui
- image_info = upload_image_to_comfyui(server_address, input_image_path)
- if image_info is None:
- self.operator._error = f"Failed to upload input image: {input_image_path}"
- return {"error": self.operator._error}
-
- scene = context.scene
- skip_texture = scene.trellis2_skip_texture
-
- # Load the appropriate workflow template
- if skip_texture:
- prompt = json.loads(prompt_text_trellis2_shape_only)
- NODES = {
- 'input_image': '1',
- 'load_models': '2',
- 'remove_bg': '3',
- 'get_conditioning': '4',
- 'image_to_shape': '5',
- 'simplify': '6',
- 'export_trimesh': '7',
- }
- export_node_key = 'export_trimesh'
- else:
- prompt = json.loads(prompt_text_trellis2)
- NODES = {
- 'input_image': '1',
- 'load_models': '2',
- 'remove_bg': '3',
- 'get_conditioning': '4',
- 'image_to_shape': '5',
- 'shape_to_textured_mesh': '6',
- 'export_glb': '7',
- }
- export_node_key = 'export_glb'
-
- # Set input image
- prompt[NODES['input_image']]["inputs"]["image"] = image_info['name']
-
- # Configure model settings from scene properties
- prompt[NODES['load_models']]["inputs"]["resolution"] = scene.trellis2_resolution
- prompt[NODES['load_models']]["inputs"]["vram_mode"] = scene.trellis2_vram_mode
- prompt[NODES['load_models']]["inputs"]["attn_backend"] = scene.trellis2_attn_backend
-
- # Configure background removal (or bypass it)
- skip_bg = getattr(scene, 'trellis2_bg_removal', 'auto') == 'skip'
- if skip_bg:
- # Remove the RemoveBackground node from the workflow.
- # Wire GetConditioning to use LoadImage outputs directly:
- # LoadImage[0] → image, LoadImage[1] → mask (alpha channel).
- # If the input has no alpha, ComfyUI's LoadImage provides an
- # all-white mask (= entire image is foreground).
- remove_bg_node_id = NODES.pop('remove_bg')
- del prompt[remove_bg_node_id]
- prompt[NODES['get_conditioning']]["inputs"]["image"] = [NODES['input_image'], 0]
- prompt[NODES['get_conditioning']]["inputs"]["mask"] = [NODES['input_image'], 1]
- else:
- prompt[NODES['remove_bg']]["inputs"]["low_vram"] = False
-
- # Configure conditioning
- prompt[NODES['get_conditioning']]["inputs"]["background_color"] = scene.trellis2_background_color
- # Auto-determine whether 1024 conditioning is needed from resolution mode
- prompt[NODES['get_conditioning']]["inputs"]["include_1024"] = scene.trellis2_resolution != '512'
-
- # Configure shape generation
- seed = scene.trellis2_seed
- prompt[NODES['image_to_shape']]["inputs"]["seed"] = seed
- prompt[NODES['image_to_shape']]["inputs"]["ss_guidance_strength"] = scene.trellis2_ss_guidance
- prompt[NODES['image_to_shape']]["inputs"]["ss_sampling_steps"] = scene.trellis2_ss_steps
- prompt[NODES['image_to_shape']]["inputs"]["shape_guidance_strength"] = scene.trellis2_shape_guidance
- prompt[NODES['image_to_shape']]["inputs"]["shape_sampling_steps"] = scene.trellis2_shape_steps
- prompt[NODES['image_to_shape']]["inputs"]["max_tokens"] = scene.trellis2_max_tokens
-
- # Configure export with unique prefix for identification
- unique_prefix = f"trellis2_{uuid.uuid4().hex[:8]}"
-
- # When post-processing is disabled, maximize the decimation target
- # and disable remeshing so the user gets the rawest possible mesh.
- use_pp = getattr(scene, 'trellis2_post_processing_enabled', True)
-
- if skip_texture:
- # Configure simplify node
- if use_pp:
- prompt[NODES['simplify']]["inputs"]["target_face_count"] = scene.trellis2_decimation
- prompt[NODES['simplify']]["inputs"]["remesh"] = scene.trellis2_remesh
- else:
- # Raw mesh — max faces, no remesh
- prompt[NODES['simplify']]["inputs"]["target_face_count"] = 5000000
- prompt[NODES['simplify']]["inputs"]["remesh"] = False
- prompt[NODES['simplify']]["inputs"]["fill_holes"] = False
- prompt[NODES['export_trimesh']]["inputs"]["filename_prefix"] = unique_prefix
- else:
- # Configure texture generation
- prompt[NODES['shape_to_textured_mesh']]["inputs"]["seed"] = seed
- prompt[NODES['shape_to_textured_mesh']]["inputs"]["tex_guidance_strength"] = scene.trellis2_tex_guidance
- prompt[NODES['shape_to_textured_mesh']]["inputs"]["tex_sampling_steps"] = scene.trellis2_tex_steps
-
- # Configure GLB export
- if use_pp:
- prompt[NODES['export_glb']]["inputs"]["decimation_target"] = scene.trellis2_decimation
- prompt[NODES['export_glb']]["inputs"]["remesh"] = scene.trellis2_remesh
- else:
- prompt[NODES['export_glb']]["inputs"]["decimation_target"] = 5000000
- prompt[NODES['export_glb']]["inputs"]["remesh"] = False
- prompt[NODES['export_glb']]["inputs"]["texture_size"] = scene.trellis2_texture_size
- prompt[NODES['export_glb']]["inputs"]["filename_prefix"] = unique_prefix
-
- # Save prompt for debugging
- revision_dir = get_generation_dirs(context).get("revision", "")
- if revision_dir:
- self._save_prompt_to_file(prompt, revision_dir)
-
- # --- Two-phase VRAM management for textured path ---
- # NOTE: Two-phase execution (shape first, flush, then texture) was
- # removed because ComfyUI requires at least one OUTPUT_NODE per prompt
- # and the shape-only subset has none. The TRELLIS pipeline stages
- # handle VRAM management internally (unload_shape_pipeline before
- # loading texture models), so a single full-prompt submission works.
-
- # Connect WebSocket
- ws = self._connect_to_websocket(server_address, client_id)
- if ws is None:
- return {"error": "conn_failed"}
-
- # TRELLIS.2 simplification / post-processing can take several
- # minutes without sending any WS messages. Use the user-
- # configurable mesh generation timeout.
- ws.settimeout(get_timeout('mesh_gen'))
-
- # Let the operator close this WS on cancel
- if hasattr(self.operator, '_active_ws'):
- self.operator._active_ws = ws
-
- prompt_id = None
- try:
- # Queue prompt
- prompt_id = self._queue_prompt(prompt, client_id, server_address)
-
- # Node-level progress (isolated subprocess doesn't emit within-node progress)
- # Weights approximate actual time spent per node
- if skip_texture:
- NODE_PROGRESS = {
- NODES['input_image']: 2,
- NODES['load_models']: 3,
- NODES['get_conditioning']: 25,
- NODES['image_to_shape']: 80,
- NODES['simplify']: 90,
- NODES['export_trimesh']: 98,
- }
- if not skip_bg:
- NODE_PROGRESS[NODES['remove_bg']] = 12
- else:
- # Full textured pipeline (single submission)
- NODE_PROGRESS = {
- NODES['input_image']: 2,
- NODES['load_models']: 3,
- NODES['get_conditioning']: 18,
- NODES['image_to_shape']: 55,
- NODES['shape_to_textured_mesh']: 85,
- NODES['export_glb']: 98,
- }
- if not skip_bg:
- NODE_PROGRESS[NODES['remove_bg']] = 10
-
- # Wait for execution to complete via WebSocket
- # Also capture 'executed' events which may contain the output path
- export_node_id = NODES[export_node_key]
- glb_output_path = None
-
- # Friendly node labels for the progress bars
- NODE_LABELS = {
- 'input_image': 'Loading Image',
- 'load_models': 'Loading Models',
- 'remove_bg': 'Removing Background',
- 'get_conditioning': 'Conditioning',
- 'image_to_shape': 'Generating Shape',
- 'shape_to_textured_mesh': 'Generating Texture',
- 'simplify': 'Simplifying Mesh',
- 'export_trimesh': 'Exporting Mesh',
- 'export_glb': 'Exporting GLB',
- }
-
- # Build a lookup of known step counts so we can identify
- # which sampling sub-phase a within-node progress event
- # belongs to (the TRELLIS nodes may emit separate runs of
- # progress events for SS, shape, and texture sampling).
- _ss_steps = scene.trellis2_ss_steps
- _shape_steps = scene.trellis2_shape_steps
- _tex_steps = getattr(scene, 'trellis2_tex_steps', 0)
- _current_exec_node = None # node-key of the currently executing node
-
- # Track accumulated sub-phases within a node so we can
- # compute a node-internal overall progress.
- _sub_phase_idx = 0 # resets when the executing node changes
- _sub_phase_count = 1 # how many sub-phases this node has
-
- while True:
- try:
- out = ws.recv()
- except websocket.WebSocketTimeoutException:
- # Timeout on recv — server is still alive but a heavy
- # step (e.g. simplification) took longer than expected.
- # Keep waiting instead of treating it as a crash.
- print("[TRELLIS2] WebSocket recv timed out — "
- "server may still be processing. Retrying...")
- continue
- except (ConnectionError, OSError, Exception) as ws_err:
- err_name = type(ws_err).__name__
- print(f"[TRELLIS2] WebSocket died ({err_name}): {ws_err}")
- # Distinguish user-initiated cancel from a real crash
- if getattr(self.operator, '_cancelled', False):
- return {"error": "cancelled"}
- return {"error": (
- "ComfyUI server crashed during TRELLIS.2 generation "
- "(likely VRAM exhaustion). Please restart ComfyUI, "
- "reduce max_tokens or resolution, and try again."
- )}
- if isinstance(out, str):
- message = json.loads(out)
-
- if message['type'] == 'executing':
- data = message['data']
- if data['prompt_id'] == prompt_id:
- if data['node'] is None:
- self.operator._phase_progress = 100
- self.operator._detail_progress = 100
- if hasattr(self.operator, '_update_overall'):
- self.operator._update_overall()
- break # Execution complete
- else:
- node_id = data['node']
- node_names = {v: k for k, v in NODES.items()}
- node_key = node_names.get(node_id, node_id)
- node_label = NODE_LABELS.get(node_key, node_key)
- print(f"[TRELLIS2] Executing: {node_label}")
- self.operator._phase_stage = f"TRELLIS.2: {node_label}"
- # Update progress based on node weight
- if node_id in NODE_PROGRESS:
- self.operator._phase_progress = NODE_PROGRESS[node_id]
- if hasattr(self.operator, '_update_overall'):
- self.operator._update_overall()
- self.operator._detail_stage = node_label
- self.operator._detail_progress = 0
-
- # Reset sub-phase tracking for the new node
- _current_exec_node = node_key
- _sub_phase_idx = 0
- if node_key == 'image_to_shape':
- # SS sampling + shape sampling = 2 sub-phases
- _sub_phase_count = 2
- elif node_key == 'shape_to_textured_mesh':
- _sub_phase_count = 1
- else:
- _sub_phase_count = 1
-
- elif message['type'] == 'executed':
- # Capture output from export node (newer ComfyUI versions)
- data = message.get('data', {})
- if data.get('node') == export_node_id:
- ws_output = data.get('output', {})
- if ws_output:
- for key in ['glb_path', 'file_path', 'text', 'string']:
- val = ws_output.get(key)
- if val:
- if isinstance(val, list) and len(val) > 0:
- val = val[0]
- if isinstance(val, str) and val:
- glb_output_path = val
- print(f"[TRELLIS2] Got path from WS executed event: {glb_output_path}")
- break
- # Also check any key with a file-like value
- if not glb_output_path:
- for key, val in ws_output.items():
- if isinstance(val, list) and len(val) > 0:
- val = val[0]
- if isinstance(val, str) and val.endswith(('.glb', '.obj', '.ply')):
- glb_output_path = val
- print(f"[TRELLIS2] Got path from WS (key={key}): {glb_output_path}")
- break
-
- elif message['type'] == 'progress':
- # Within-node progress (sampler steps).
- # TRELLIS.2 nodes may emit separate runs of progress
- # events for each internal sampling phase. We detect
- # the sub-phase by matching ``max`` against known step
- # counts (SS, shape, texture).
- p_data = message['data']
- p_value = p_data['value']
- p_max = p_data['max']
- step_progress = (p_value / p_max) * 100 if p_max else 0
-
- # ── Sub-phase identification ──
- sub_label = ""
- if _current_exec_node == 'image_to_shape':
- if p_max == _ss_steps:
- sub_label = "Sampling SS"
- # First sub-phase: reset index if we see it again
- if _sub_phase_idx == 0 or p_value == 1:
- _sub_phase_idx = 0
- elif p_max == _shape_steps:
- sub_label = "Sampling Shape SLat"
- if _sub_phase_idx < 1:
- _sub_phase_idx = 1
- else:
- # Unknown max — show generic label
- sub_label = "Sampling"
- elif _current_exec_node == 'shape_to_textured_mesh':
- if p_max == _tex_steps:
- sub_label = "Sampling Texture"
- else:
- sub_label = "Sampling"
- else:
- sub_label = "Processing"
-
- # Compute node-internal overall progress accounting for sub-phases
- if _sub_phase_count > 1:
- node_overall = ((_sub_phase_idx + step_progress / 100.0)
- / _sub_phase_count) * 100.0
- else:
- node_overall = step_progress
-
- if step_progress != 0:
- self.operator._detail_progress = node_overall
- self.operator._detail_stage = (
- f"{sub_label}: Step {p_value}/{p_max}"
- )
- print(f"[TRELLIS2] {sub_label}: Step {p_value}/{p_max} ({step_progress:.0f}%)")
-
- elif message['type'] == 'execution_error':
- error_data = message.get('data', {})
- error_msg = error_data.get('exception_message', 'Unknown error')
- self.operator._error = f"TRELLIS.2 execution error: {error_msg}"
- print(f"[TRELLIS2] Error: {self.operator._error}")
- return {"error": self.operator._error}
- finally:
- if hasattr(self.operator, '_active_ws'):
- self.operator._active_ws = None
- if ws:
- try:
- ws.close()
- except Exception:
- pass
-
- # ── Retrieve the GLB file ──────────────────────────────────────
- # Strategy 1: path from WS 'executed' event (already captured above)
- # Strategy 2: path from history API
- # Strategy 3: direct file read from disk (local ComfyUI)
- # Strategy 4: HTTP download via /view endpoint
- # Strategy 5: scan ComfyUI output dir for our prefix
-
- glb_source_path = glb_output_path # May already be set from WS event
-
- # Helper: bail early when the user hits Cancel
- def _cancelled():
- return getattr(self.operator, '_cancelled', False)
-
- # Strategy 2: Query history for the output path
- if not glb_source_path and not _cancelled():
- try:
- history_url = f"http://{server_address}/history/{prompt_id}"
- history_response = json.loads(urllib.request.urlopen(
- history_url, timeout=get_timeout('api')).read())
-
- if prompt_id in history_response:
- outputs = history_response[prompt_id].get("outputs", {})
- export_output = outputs.get(export_node_id, {})
- print(f"[TRELLIS2] History output for node {export_node_id}: {export_output}")
-
- for key in ['glb_path', 'file_path', 'text', 'string']:
- val = export_output.get(key)
- if val:
- if isinstance(val, list) and len(val) > 0:
- val = val[0]
- if isinstance(val, str) and val:
- glb_source_path = val
- print(f"[TRELLIS2] Got path from history (key={key}): {glb_source_path}")
- break
-
- # Check any key with a path-like value
- if not glb_source_path:
- for key, val in export_output.items():
- if isinstance(val, list) and len(val) > 0:
- val = val[0]
- if isinstance(val, str) and val.endswith(('.glb', '.obj', '.ply')):
- glb_source_path = val
- print(f"[TRELLIS2] Got path from history (key={key}): {glb_source_path}")
- break
- except Exception as e:
- print(f"[TRELLIS2] History query failed: {e}")
-
- # Strategy 3: Read directly from disk (works when ComfyUI is local)
- if glb_source_path and not _cancelled() and os.path.isfile(glb_source_path):
- try:
- print(f"[TRELLIS2] Reading GLB directly from disk: {glb_source_path}")
- with open(glb_source_path, 'rb') as f:
- glb_data = f.read()
- print(f"[TRELLIS2] Read {len(glb_data)} bytes from disk")
- if glb_data and len(glb_data) > 0:
- return glb_data
- except Exception as e:
- print(f"[TRELLIS2] Direct file read failed: {e}")
-
- # Strategy 4: HTTP download via /view endpoint
- if glb_source_path and not _cancelled():
- glb_filename = os.path.basename(glb_source_path)
- try:
- view_url = f"http://{server_address}/view?filename={urllib.parse.quote(glb_filename)}&type=output"
- print(f"[TRELLIS2] Downloading GLB via HTTP: {view_url}")
- glb_response = urllib.request.urlopen(view_url, timeout=get_timeout('transfer'))
- glb_data = glb_response.read()
- print(f"[TRELLIS2] Downloaded GLB: {len(glb_data)} bytes")
- if glb_data and len(glb_data) > 0:
- return glb_data
- except Exception as e:
- print(f"[TRELLIS2] HTTP download failed: {e}")
-
- if _cancelled():
- return {"error": "cancelled"}
-
- # Strategy 5: Scan ComfyUI output directory for files matching our prefix
- # This handles the case where the export node path wasn't captured via API
- print(f"[TRELLIS2] Scanning for files with prefix '{unique_prefix}'...")
-
- # 5a: Try to discover ComfyUI's output directory from the server
- comfyui_output_dir = None
- if not _cancelled():
- try:
- system_url = f"http://{server_address}/system_stats"
- system_response = json.loads(urllib.request.urlopen(
- system_url, timeout=get_timeout('api')).read())
- # Some ComfyUI versions include directory info
- comfyui_output_dir = system_response.get("output_dir")
- except Exception:
- pass
-
- # 5b: Try common local paths relative to server
- if not comfyui_output_dir:
- # Check if server is localhost - if so, try to find output dir
- host = server_address.split(':')[0]
- if host in ('127.0.0.1', 'localhost', '0.0.0.0', '::1'):
- # Try to discover via the /view endpoint with a known file
- # Or just check common ComfyUI locations
- common_paths = [
- os.path.join(os.environ.get('COMFYUI_PATH', ''), 'output'),
- 'E:/tools/ComfyUI/output',
- 'C:/ComfyUI/output',
- os.path.expanduser('~/ComfyUI/output'),
- ]
- for candidate in common_paths:
- if candidate and os.path.isdir(candidate):
- comfyui_output_dir = candidate
- break
-
- if comfyui_output_dir and os.path.isdir(comfyui_output_dir):
- print(f"[TRELLIS2] Scanning output dir: {comfyui_output_dir}")
- try:
- matching_files = sorted(
- [f for f in os.listdir(comfyui_output_dir)
- if f.startswith(unique_prefix) and f.endswith(('.glb', '.obj', '.ply'))],
- key=lambda f: os.path.getmtime(os.path.join(comfyui_output_dir, f)),
- reverse=True
- )
- if matching_files:
- found_path = os.path.join(comfyui_output_dir, matching_files[0])
- print(f"[TRELLIS2] Found matching file: {found_path}")
- with open(found_path, 'rb') as f:
- glb_data = f.read()
- print(f"[TRELLIS2] Read {len(glb_data)} bytes from disk")
- if glb_data and len(glb_data) > 0:
- return glb_data
- except Exception as e:
- print(f"[TRELLIS2] Output dir scan failed: {e}")
-
- # 5c: Try HTTP download with timestamp-based filename guesses
- # The export nodes use format: {prefix}_{YYYYMMDD_HHMMSS}.glb
- # Use a short timeout per request so slow remote connections don't
- # block for minutes, and check for cancellation each iteration.
- is_remote = not self._is_local_server(server_address)
- scan_range = 30 if is_remote else 120 # fewer guesses for remote
- scan_timeout = 5 if is_remote else 10 # per-request timeout (seconds)
- now = datetime.now()
- for delta_seconds in range(0, scan_range):
- if _cancelled():
- return {"error": "cancelled"}
- for delta in [timedelta(seconds=-delta_seconds), timedelta(seconds=delta_seconds)]:
- candidate_time = now + delta
- candidate_name = f"{unique_prefix}_{candidate_time.strftime('%Y%m%d_%H%M%S')}.glb"
- try:
- view_url = f"http://{server_address}/view?filename={urllib.parse.quote(candidate_name)}&type=output"
- glb_response = urllib.request.urlopen(view_url, timeout=scan_timeout)
- glb_data = glb_response.read()
- if glb_data and len(glb_data) > 0:
- print(f"[TRELLIS2] Found GLB via timestamp scan: {candidate_name} ({len(glb_data)} bytes)")
- return glb_data
- except Exception:
- continue
-
- if _cancelled():
- return {"error": "cancelled"}
-
- self.operator._error = (
- f"Failed to retrieve GLB from ComfyUI. "
- f"The workflow completed but the output file could not be located. "
- f"Prefix: {unique_prefix}"
- )
- return {"error": self.operator._error}
-
- def _create_base_prompt(self, context):
- """Creates and configures the base prompt with user settings."""
- from .util.helpers import prompt_text
-
- # Load the base prompt template
- prompt = json.loads(prompt_text)
-
- # Node IDs organized by functional category
- NODES = {
- # Text Prompting
- 'pos_prompt': "9",
- 'neg_prompt': "10",
- 'clip_skip': "247",
-
- # Sampling Control
- 'sampler': "15",
- 'seed_control': "15", # Same as sampler node but for seed parameter
-
- # Model Loading
- 'checkpoint': "6",
-
- # Latent Space
- 'latent': "16",
-
- # Image Output
- 'save_image': "25",
-
- # IPAdapter
- 'ipadapter_loader': "235",
- 'ipadapter': "236",
- 'ipadapter_image': "237",
- }
-
- 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
- # Prepend the view description
- base_prompt_text = f"{view_desc}, {base_prompt_text}"
-
- # Set text prompts
- prompt[NODES['pos_prompt']]["inputs"]["text"] = base_prompt_text
- prompt[NODES['neg_prompt']]["inputs"]["text"] = context.scene.comfyui_negative_prompt
-
- # Set sampling parameters
- 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
-
- # Set clip skip
- prompt[NODES['clip_skip']]["inputs"]["stop_at_clip_layer"] = -context.scene.clip_skip
-
- # Set the model name
- prompt[NODES['checkpoint']]["inputs"]["ckpt_name"] = context.scene.model_name
-
- # Build LoRA chain
- initial_model_input_lora = [NODES['checkpoint'], 0]
- initial_clip_input_lora = [NODES['checkpoint'], 1]
-
- prompt, final_lora_model_out, final_lora_clip_out = self._build_lora_chain(
- prompt, context,
- initial_model_input_lora, initial_clip_input_lora,
- start_node_id=400 # Starting node ID for LoRA chain
- )
-
- current_model_out = final_lora_model_out
-
- # Set the input for the clip skip node
- prompt[NODES['clip_skip']]["inputs"]["clip"] = final_lora_clip_out
-
- # If using IPAdapter, set the model input
- _is_trellis2_input_ipadapter = (
- context.scene.sequential_ipadapter
- and context.scene.sequential_ipadapter_mode == 'trellis2_input'
- )
- if (context.scene.use_ipadapter
- or _is_trellis2_input_ipadapter
- or (context.scene.generation_method == 'separate'
- and context.scene.sequential_ipadapter
- and self.operator._current_image > 0)):
- # Set the model input for IPAdapter
- prompt[NODES['ipadapter_loader']]["inputs"]["model"] = current_model_out
- current_model_out = [NODES['ipadapter'], 0]
-
- # Set the model for sampler node
- prompt[NODES['sampler']]["inputs"]["model"] = current_model_out
-
- return prompt, NODES
-
- def _configure_resolution(self, prompt, context, NODES):
- """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.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
- prompt[NODES['latent']]["inputs"]["height"] = context.scene.render.resolution_y
-
- def _configure_ipadapter(self, prompt, context, ipadapter_ref_info, NODES):
- # Configure IPAdapter if enabled
-
- # Connect IPAdapter output to the appropriate node
- prompt[NODES['sampler']]["inputs"]["model"] = [NODES['ipadapter'], 0]
-
- # Set IPAdapter image source
- prompt[NODES['ipadapter_image']]["inputs"]["image"] = ipadapter_ref_info['name']
-
- # Connect ipadapter image to the input
- prompt[NODES['ipadapter']]["inputs"]["image"] = [NODES['ipadapter_image'], 0]
-
- # Configure IPAdapter settings
- prompt[NODES['ipadapter']]["inputs"]["weight"] = context.scene.ipadapter_strength
- prompt[NODES['ipadapter']]["inputs"]["start_at"] = context.scene.ipadapter_start
- prompt[NODES['ipadapter']]["inputs"]["end_at"] = context.scene.ipadapter_end
-
- # Set weight type
- weight_type_mapping = {
- 'standard': "standard",
- 'prompt': "prompt is more important",
- 'style': "style transfer"
- }
- prompt[NODES['ipadapter']]["inputs"]["weight_type"] = weight_type_mapping.get(context.scene.ipadapter_weight_type, "standard")
-
- def _build_controlnet_chain_extended(self, context, base_prompt, pos_input, neg_input, vae_input, controlnet_info_dict):
+ 2. Clears the execution cache history and queue.
+ 3. Sends ``/free`` *again* — the first round triggers model unloads
+ whose CUDA tensors may still reside in PyTorch's cache; the
+ second round triggers ``soft_empty_cache`` which calls
+ ``torch.cuda.empty_cache()`` on the now-freed allocations.
+ 4. Polls ``/system_stats`` waiting for VRAM to increase.
+ Retries the whole sequence up to *retries* times.
"""
- Builds a chain of ControlNet units dynamically based on scene settings.
-
- Args:
- context: Blender context, used to access addon preferences and scene data.
- base_prompt (dict): The ComfyUI prompt dictionary to be modified.
- pos_input (list): The [node_id, output_idx] for the initial positive conditioning.
- neg_input (list): The [node_id, output_idx] for the initial negative conditioning.
- vae_input (list): The [node_id, output_idx] for the VAE, used by ControlNetApplyAdvanced.
- Typically, this is [checkpoint_node_id, 2] for SDXL or
- [checkpoint_node_id, 0] for some VAE loaders.
- controlnet_info_dict (dict): A dictionary mapping ControlNet types (e.g., "depth",
- "canny") to their corresponding uploaded image info.
+ import time
- Returns:
- tuple: (modified_prompt, final_positive_conditioning, final_negative_conditioning)
- """
- addon_prefs = context.preferences.addons[__package__].preferences
- try:
- mapping = json.loads(addon_prefs.controlnet_mapping)
- except Exception:
- mapping = {}
-
- # Get the dynamic collection of ControlNet units
- controlnet_units = getattr(context.scene, "controlnet_units", [])
- current_pos = pos_input
- current_neg = neg_input
- has_union = False
- for idx, unit in enumerate(controlnet_units):
- # Get uploaded info for this unit's type
- uploaded_info = controlnet_info_dict.get(unit.unit_type)
- if not uploaded_info:
- print(f"Warning: Uploaded info for ControlNet type '{unit.unit_type}' not found. Skipping unit.")
- continue # Skip this unit
+ vram_before, vram_total = self._get_vram_stats(server_address)
+ if vram_before is not None:
+ print(f"[TRELLIS2] {label} VRAM before flush: {vram_before:.0f} MB free / {vram_total:.0f} MB total")
- # Generate unique keys for nodes in this chain unit.
- load_key = str(200 + idx * 3) # LoadImage node
- loader_key = str(200 + idx * 3 + 1) # ControlNetLoader node
- apply_key = str(200 + idx * 3 + 2) # ControlNetApplyAdvanced node
+ # Skip the flush entirely if VRAM is already ≥90% free.
+ if vram_total and vram_before > vram_total * 0.9:
+ print(f"[TRELLIS2] {label} VRAM already ≥90% free — skipping flush.")
+ return True
- # Create the LoadImage node.
- base_prompt[load_key] = {
- "inputs": {
- "image": uploaded_info['name'],
- },
- "class_type": "LoadImage",
- "_meta": {
- "title": f"Load Image ({unit.unit_type})"
- }
- }
- # Create the ControlNetLoader node.
- base_prompt[loader_key] = {
- "inputs": {
- "control_net_name": unit.model_name # updated to use selected property
- },
- "class_type": "ControlNetLoader",
- "_meta": {
- "title": f"Load ControlNet ({unit.unit_type})"
- }
- }
- # Create the ControlNetApplyAdvanced node.
- base_prompt[apply_key] = {
- "inputs": {
- "strength": unit.strength,
- "start_percent": unit.start_percent,
- "end_percent": unit.end_percent,
- "positive": [current_pos, 0],
- "negative": [current_neg, 1] if (idx > 0 or current_neg == "228" or current_neg == "51") else [current_neg, 0],
- "control_net": [loader_key, 0],
- "image": [load_key, 0],
- "vae": [vae_input, 2] if context.scene.model_architecture == "sdxl" else [vae_input, 0],
- },
- "class_type": "ControlNetApplyAdvanced",
- "_meta": {
- "title": f"Apply ControlNet ({unit.unit_type})"
- }
- }
- # Update chain inputs: the output of this apply node becomes the new input.
- current_pos = apply_key
- current_neg = apply_key
- # If the controlnet is of the union type, connect the ControlNetApplyAdvanced input into the SetUnionControlNetType node (239)
- if unit.is_union and unit.use_union_type:
- base_prompt[apply_key]["inputs"]["control_net"] = ["239", 0]
- base_prompt["239"]["inputs"]["control_net"] = [loader_key, 0]
- if unit.unit_type == "depth":
- base_prompt["239"]["inputs"]["type"] = "depth"
- elif unit.unit_type == "canny":
- base_prompt["239"]["inputs"]["type"] = "canny/lineart/anime_lineart/mlsd"
- elif unit.unit_type == "normal":
- base_prompt["239"]["inputs"]["type"] = "normal"
- has_union = True
- if not has_union:
- # Remove the node
- if "239" in base_prompt:
- del base_prompt["239"]
+ def _send_free():
+ d = json.dumps({"unload_models": True, "free_memory": True}).encode('utf-8')
+ r = urllib.request.Request(
+ f"http://{server_address}/free", data=d,
+ headers={'Content-Type': 'application/json'}, method='POST'
+ )
+ urllib.request.urlopen(r, timeout=get_timeout('api'))
- return base_prompt, current_pos
+ def _clear_history():
+ d = json.dumps({"clear": True}).encode('utf-8')
+ r = urllib.request.Request(
+ f"http://{server_address}/history", data=d,
+ headers={'Content-Type': 'application/json'}, method='POST'
+ )
+ urllib.request.urlopen(r, timeout=get_timeout('api'))
- def _build_lora_chain(self, prompt, context, initial_model_input, initial_clip_input, start_node_id=300, lora_class_type="LoraLoader"):
- """
- Builds a chain of LoRA loaders dynamically.
+ def _clear_queue():
+ d = json.dumps({"clear": True}).encode('utf-8')
+ r = urllib.request.Request(
+ f"http://{server_address}/queue", data=d,
+ headers={'Content-Type': 'application/json'}, method='POST'
+ )
+ urllib.request.urlopen(r, timeout=get_timeout('api'))
- Args:
- prompt (dict): The ComfyUI prompt dictionary to modify.
- context: Blender context.
- initial_model_input (list): The [node_id, output_idx] for the initial model.
- initial_clip_input (list): The [node_id, output_idx] for the initial CLIP.
- start_node_id (int): The starting integer for generating unique LoRA node IDs.
- lora_class_type (str): The class type of the LoRA loader node to use.
+ for attempt in range(1, retries + 1):
+ try:
+ # Round 1: Unload models
+ _send_free()
+ time.sleep(1)
- Returns:
- tuple: (modified_prompt, final_model_output, final_clip_output)
-
- The final model and CLIP outputs are [node_id, output_idx] lists.
- """
- scene = context.scene
-
- current_model_out = initial_model_input
- current_clip_out = initial_clip_input
+ # Clear caches
+ try:
+ _clear_history()
+ except Exception:
+ pass
+ try:
+ _clear_queue()
+ except Exception:
+ pass
- if not scene.lora_units:
- return prompt, current_model_out, current_clip_out
+ # Round 2: Free the now-released CUDA allocations
+ _send_free()
- for i, lora_unit in enumerate(scene.lora_units):
- if not lora_unit.model_name or lora_unit.model_name == "NONE":
- continue # Skip if no LoRA model is selected for this unit
+ # Poll VRAM (up to 8s)
+ for check in range(8):
+ time.sleep(1)
+ vram_after, _ = self._get_vram_stats(server_address)
+ if vram_after is not None:
+ print(f"[TRELLIS2] {label} VRAM after flush (check {check+1}): {vram_after:.0f} MB free")
+ if vram_before is not None and vram_after > vram_before + 500:
+ print(f"[TRELLIS2] {label} flush verified: freed {vram_after - vram_before:.0f} MB")
+ return True
+ if vram_total is not None and vram_after > vram_total * 0.7:
+ print(f"[TRELLIS2] {label} flush OK: >70% VRAM free")
+ return True
- lora_node_id_str = str(start_node_id + i)
-
- lora_inputs = {
- "lora_name": lora_unit.model_name,
- "strength_model": lora_unit.model_strength,
- "model": current_model_out,
- }
+ print(f"[TRELLIS2] {label} flush attempt {attempt}/{retries}: VRAM didn't free enough — retrying")
+ except Exception as e:
+ print(f"[TRELLIS2] {label} flush attempt {attempt}/{retries} failed: {e}")
- if lora_class_type == "LoraLoader":
- lora_inputs["strength_clip"] = lora_unit.clip_strength
- lora_inputs["clip"] = current_clip_out
- elif lora_class_type == "NunchakuQwenImageLoraLoader":
- lora_inputs = {
- "lora_name": lora_unit.model_name,
- "lora_strength": lora_unit.model_strength,
- "cpu_offload": "disable",
- "model": current_model_out
- }
+ if attempt < retries:
+ time.sleep(2)
- prompt[lora_node_id_str] = {
- "inputs": lora_inputs,
- "class_type": lora_class_type,
- "_meta": {
- "title": f"Load LoRA {i+1} ({lora_unit.model_name[:20]})"
- }
- }
- # Update outputs for the next LoRA in the chain
- current_model_out = [lora_node_id_str, 0]
- if lora_class_type == "LoraLoader":
- current_clip_out = [lora_node_id_str, 1]
-
- return prompt, current_model_out, current_clip_out
+ print(f"[TRELLIS2] Warning: {label} VRAM flush unverified after {retries} attempts")
+ return False
- def _build_controlnet_chain(self, prompt, context, controlnet_info, NODES):
- """Builds the ControlNet processing chain."""
- # Build controlnet chain with guidance images
- prompt, final_node = self._build_controlnet_chain_extended(
- context, prompt, NODES['pos_prompt'], NODES['neg_prompt'], NODES['checkpoint'],
- controlnet_info
- )
-
- # Connect final node outputs to the KSampler
- prompt[NODES['sampler']]["inputs"]["positive"] = [final_node, 0]
- prompt[NODES['sampler']]["inputs"]["negative"] = [final_node, 1]
-
- return prompt
def _save_prompt_to_file(self, prompt, output_dir):
"""Saves the prompt to a file for debugging."""
@@ -2933,7 +156,7 @@ class WorkflowManager:
with open(os.path.join(output_dir, "prompt.json"), 'w') as f:
json.dump(prompt, f, indent=2) # Added indent for better readability
except Exception as e:
- print(f"Failed to save prompt to file: {str(e)}")
+ print(f"[StableGen] Failed to save prompt to file: {str(e)}")
@staticmethod
def _validate_image_bytes(img_bytes):
@@ -3074,7 +297,10 @@ class WorkflowManager:
break # Execution is complete
else:
current_node = data['node']
- print(f"Executing node: {current_node}")
+ node_info = prompt.get(current_node, {})
+ node_title = (node_info.get('_meta', {}).get('title')
+ or node_info.get('class_type', current_node))
+ print(f"[StableGen] Executing node: {node_title} ({current_node})")
elif message['type'] == 'progress':
progress = (message['data']['value'] / message['data']['max']) * 100
@@ -3097,21 +323,24 @@ class WorkflowManager:
self.operator._phase_progress = progress
if hasattr(self.operator, '_update_overall'):
self.operator._update_overall()
- print(f"Progress: {progress:.1f}%")
+ print(f"[StableGen] Progress: {progress:.1f}%")
elif message['type'] == 'execution_error':
error_data = message.get('data', {})
error_msg = error_data.get('exception_message', 'Unknown error')
node_id = error_data.get('node_id', '?')
+ err_node_info = prompt.get(str(node_id), {})
+ err_node_name = (err_node_info.get('_meta', {}).get('title')
+ or err_node_info.get('class_type', node_id))
self.operator._error = (
- f"ComfyUI execution error (node {node_id}): {error_msg}"
+ f"ComfyUI execution error ({err_node_name}): {error_msg}"
)
print(f"[StableGen] {self.operator._error}")
break
else:
# Binary data (image)
if current_node == NODES['save_image']: # SaveImageWebsocket node
- print("Receiving generated image")
+ print("[StableGen] Receiving generated image")
images_output = output_images.get(current_node, [])
images_output.append(out[8:]) # Skip the first 8 bytes (header)
output_images[current_node] = images_output
@@ -3157,833 +386,15 @@ class WorkflowManager:
return response['prompt_id']
except Exception as e:
- print(f"Failed to queue prompt: {str(e)}")
+ print(f"[StableGen] Failed to queue prompt: {str(e)}")
raise
- def refine(self, context, controlnet_info=None, mask_info=None, render_info=None, ipadapter_ref_info=None):
- """
- Refines the image using ComfyUI.
- :param context: Blender context.
- :param controlnet_info: Dict of uploaded controlnet image info.
- :param mask_info: Uploaded mask image info.
- :param render_info: Uploaded render image info.
- :param ipadapter_ref_info: Uploaded IPAdapter reference image info.
- :return: Refined image.
- """
- # Setup connection parameters
- server_address = context.preferences.addons[__package__].preferences.server_address
- client_id = str(uuid.uuid4())
- output_dir = context.preferences.addons[__package__].preferences.output_dir
-
- revision_dir = get_generation_dirs(context)["revision"]
-
- # Initialize the img2img prompt template and configure base settings
- prompt, NODES = self._create_img2img_base_prompt(context)
-
- # Configure based on generation method
- self._configure_refinement_mode(prompt, context, render_info, mask_info, NODES)
-
- if ipadapter_ref_info and context.scene.generation_method != 'uv_inpaint':
- # Configure IPAdapter settings
- self._configure_ipadapter_refine(prompt, context, ipadapter_ref_info, NODES)
- else:
- # Remove IPAdapter nodes if not used
- for node_id in ["235", "236", "237"]:
- if node_id in prompt:
- del prompt[node_id]
-
- # Set up image inputs for different controlnet types
- self._refine_configure_images(prompt, render_info, NODES)
-
- # Build controlnet chain for refinement if needed
- if not context.scene.generation_method == 'uv_inpaint':
- prompt = self._refine_build_controlnet_chain(prompt, context, controlnet_info, NODES)
- else:
- if context.scene.differential_diffusion:
- prompt[NODES['sampler']]["inputs"]["positive"] = [NODES['inpaint_conditioning'], 0]
- prompt[NODES['sampler']]["inputs"]["negative"] = [NODES['inpaint_conditioning'], 1]
- else:
- prompt[NODES['sampler']]["inputs"]["positive"] = [NODES['pos_prompt'], 0]
- prompt[NODES['sampler']]["inputs"]["negative"] = [NODES['neg_prompt'], 0]
-
- # Save prompt for debugging
- with open(os.path.join(output_dir, "prompt.json"), 'w') as f:
- json.dump(prompt, f)
-
- # Execute generation and get results
- ws = self._connect_to_websocket(server_address, client_id)
-
- if ws is None:
- return {"error": "conn_failed"} # Connection error
-
- images = None
- try:
- images = self._execute_prompt_and_get_images(ws, prompt, client_id, server_address, NODES)
- finally:
- if ws:
- ws.close()
-
- if images is None or isinstance(images, dict) and "error" in images:
- return {"error": "conn_failed"}
-
- print(f"Image refined with prompt: ...")
-
- img_bytes = images[NODES['save_image']][0]
-
- # Remove Comfy’s hard border and add a soft alpha vignette
- img_bytes = self._crop_and_vignette(
- img_bytes,
- border_px=8, # tweak this if your Comfy border is wider/narrower
- feather=0.08, # thicker feather band
- gamma=0.5, # stronger fade near edge
- )
-
- return img_bytes
-
- def _create_img2img_base_prompt(self, context):
- """Creates and configures the base prompt for img2img refinement."""
- from .util.helpers import prompt_text_img2img
-
- prompt = json.loads(prompt_text_img2img)
-
- # Node IDs organized by functional category
- NODES = {
- # Text Prompting
- 'pos_prompt': "102",
- 'neg_prompt': "103",
- 'clip_skip': "247",
-
- # Sampling Control
- 'sampler': "105",
-
- # Model Loading
- 'checkpoint': "38",
-
- # Image Processing
- 'upscale_grid': "118",
- 'upscale_uv': "23",
- 'vae_encode': "116",
- 'vae_encode_inpaint': "13",
- 'inpaint_conditioning': "228",
-
- # Input Images
- 'input_image': "1",
- 'mask_image': "12",
- 'render_image': "117",
-
- # Mask Processing
- 'grow_mask': "224",
- 'blur': "226",
- 'image_to_mask': "227",
-
- # Advanced Features
- 'differential_diffusion': "229",
- 'ipadapter_loader': "235",
- 'ipadapter': "236",
- 'ipadapter_image': "237",
-
- # Output
- 'save_image': "111"
- }
-
- 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
- # Prepend the view description
- base_prompt_text = f"{view_desc}, {base_prompt_text}"
-
- # Set positive prompt based on generation method
- if context.scene.generation_method in ['refine', 'local_edit', 'uv_inpaint', 'sequential']:
- prompt[NODES['pos_prompt']]["inputs"]["text"] = base_prompt_text
- else:
- prompt[NODES['pos_prompt']]["inputs"]["text"] = context.scene.refine_prompt if context.scene.refine_prompt != "" else _texturing_prompt(context.scene)
-
- # Set negative prompt
- prompt[NODES['neg_prompt']]["inputs"]["text"] = context.scene.comfyui_negative_prompt
-
- # Set sampling parameters
- prompt[NODES['sampler']]["inputs"]["seed"] = context.scene.seed
- prompt[NODES['sampler']]["inputs"]["steps"] = context.scene.refine_steps if context.scene.generation_method == 'grid' else context.scene.steps
- prompt[NODES['sampler']]["inputs"]["cfg"] = context.scene.refine_cfg if context.scene.generation_method == 'grid' else context.scene.cfg
- prompt[NODES['sampler']]["inputs"]["sampler_name"] = context.scene.refine_sampler if context.scene.generation_method == 'grid' else context.scene.sampler
- prompt[NODES['sampler']]["inputs"]["scheduler"] = context.scene.refine_scheduler if context.scene.generation_method == 'grid' else context.scene.scheduler
- if context.scene.generation_method in ('grid', 'refine', 'local_edit'):
- prompt[NODES['sampler']]["inputs"]["denoise"] = context.scene.denoise
- else:
- prompt[NODES['sampler']]["inputs"]["denoise"] = 1.0
-
- # Set clip skip
- prompt[NODES['clip_skip']]["inputs"]["stop_at_clip_layer"] = -context.scene.clip_skip
-
- # Set upscale method and dimensions
- prompt[NODES['upscale_grid']]["inputs"]["upscale_method"] = context.scene.refine_upscale_method
- prompt[NODES['upscale_grid']]["inputs"]["width"] = context.scene.render.resolution_x
- prompt[NODES['upscale_grid']]["inputs"]["height"] = context.scene.render.resolution_y
- prompt[NODES['upscale_uv']]["inputs"]["upscale_method"] = "nearest-exact"
- prompt[NODES['upscale_uv']]["inputs"]["width"] = 1024
- prompt[NODES['upscale_uv']]["inputs"]["height"] = 1024
-
- # Set the model name
- prompt[NODES['checkpoint']]["inputs"]["ckpt_name"] = context.scene.model_name
-
- # Build LoRA chain
- initial_model_input_lora = [NODES['checkpoint'], 0]
- initial_clip_input_lora = [NODES['checkpoint'], 1]
-
- prompt, final_lora_model_out, final_lora_clip_out = self._build_lora_chain(
- prompt, context,
- initial_model_input_lora, initial_clip_input_lora,
- start_node_id=400 # Starting node ID for LoRA chain
- )
-
- current_model_out = final_lora_model_out
-
- # Set the input for the clip skip node
- prompt[NODES['clip_skip']]["inputs"]["clip"] = final_lora_clip_out
-
- # If using IPAdapter, set the model input
- _is_original_render_ipadapter = (
- context.scene.sequential_ipadapter
- and context.scene.sequential_ipadapter_mode == 'original_render'
- and context.scene.generation_method == 'local_edit'
- )
- _is_trellis2_input_ipadapter = (
- context.scene.sequential_ipadapter
- and context.scene.sequential_ipadapter_mode == 'trellis2_input'
- )
- if (context.scene.use_ipadapter or _is_original_render_ipadapter or _is_trellis2_input_ipadapter or (context.scene.sequential_ipadapter and self.operator._current_image > 0)) and context.scene.generation_method != 'uv_inpaint':
- # Set the model input for IPAdapter
- prompt[NODES['ipadapter_loader']]["inputs"]["model"] = current_model_out
- current_model_out = [NODES['ipadapter'], 0]
-
- if context.scene.differential_diffusion and NODES['differential_diffusion'] in prompt and context.scene.generation_method not in ('refine', 'local_edit'):
- # Set model input for differential diffusion
- prompt[NODES['differential_diffusion']]["inputs"]["model"] = current_model_out
- current_model_out = [NODES['differential_diffusion'], 0]
- # Set the model for sampler node
- prompt[NODES['sampler']]["inputs"]["model"] = current_model_out
- return prompt, NODES
+class WorkflowManager(_TexturingWorkflowMixin, _Trellis2WorkflowMixin, _WorkflowBase):
+ """Full workflow manager composing texturing, mesh-gen and base infra."""
+ pass
- def _configure_refinement_mode(self, prompt, context, render_info, mask_info, NODES):
- """Configures the prompt based on the specific refinement mode."""
- # Configure based on generation method
- if context.scene.generation_method in ('refine', 'local_edit'):
- prompt[NODES['vae_encode']]["inputs"]["pixels"] = [NODES['render_image'], 0] # Use render directly
-
- elif context.scene.generation_method == 'uv_inpaint' or context.scene.generation_method == 'sequential':
- # Connect latent to KSampler
- prompt[NODES['sampler']]["inputs"]["latent_image"] = [NODES['vae_encode_inpaint'], 0] if not context.scene.differential_diffusion else [NODES['inpaint_conditioning'], 2]
-
- # Configure differential diffusion if enabled
- if context.scene.differential_diffusion:
- prompt[NODES['sampler']]["inputs"]["model"] = [NODES['differential_diffusion'], 0]
-
- # Configure mask settings
- 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
-
- # Set blur parameters
- prompt[NODES['blur']]["inputs"]["sigma"] = context.scene.blur_mask_sigma
- prompt[NODES['blur']]["inputs"]["blur_radius"] = context.scene.blur_mask_radius
-
- # Set grow mask parameter
- prompt[NODES['grow_mask']]["inputs"]["expand"] = context.scene.grow_mask_by
-
- if context.scene.generation_method == 'uv_inpaint':
- # Configure UV inpainting specific prompts
- self._configure_uv_inpainting_mode(prompt, context, render_info, NODES)
- else: # Sequential mode
- # Configure sequential mode settings
- self._configure_sequential_mode(prompt, context, NODES)
-
- def _configure_uv_inpainting_mode(self, prompt, context, render_info, NODES):
- """Configures the prompts for UV inpainting mode."""
- # Connect upscale to VAE / InpaintConditioning
- if not context.scene.differential_diffusion:
- prompt[NODES['vae_encode_inpaint']]["inputs"]["pixels"] = [NODES['upscale_uv'], 0]
- else:
- prompt[NODES['inpaint_conditioning']]["inputs"]["pixels"] = [NODES['upscale_uv'], 0]
- # Set the noise_mask flag according to context.scene.differential_noise
- prompt[NODES['inpaint_conditioning']]["inputs"]["noise_mask"] = context.scene.differential_noise
-
- # Create base UV prompt
- uv_prompt = f"seamless (UV-unwrapped texture) of {_texturing_prompt(context.scene)}, consistent material continuity, no visible seams or stretching, PBR material properties"
- uv_prompt_neg = f"seam, stitch, visible edge, texture stretching, repeating pattern, {context.scene.comfyui_negative_prompt}"
-
- prompt[NODES['pos_prompt']]["inputs"]["text"] = uv_prompt
- prompt[NODES['neg_prompt']]["inputs"]["text"] = uv_prompt_neg
-
- # Get the current object name from the file path
- if render_info and 'name' in render_info:
- current_object_name = os.path.basename(render_info['name']).split('.')[0]
-
- # Use the object-specific prompt if available
- object_prompt = self.operator._object_prompts.get(current_object_name, _texturing_prompt(context.scene))
- if object_prompt:
- uv_prompt = f"(UV-unwrapped texture) of {object_prompt}, consistent material continuity, no visible seams or stretching, PBR material properties"
- uv_prompt_neg = f"seam, stitch, visible edge, texture stretching, repeating pattern, {context.scene.comfyui_negative_prompt}"
- prompt[NODES['pos_prompt']]["inputs"]["text"] = uv_prompt
- prompt[NODES['neg_prompt']]["inputs"]["text"] = uv_prompt_neg
-
- def _configure_ipadapter_refine(self, prompt, context, ipadapter_ref_info, NODES):
- """Configures IPAdapter settings for refinement mode."""
- # Connect IPAdapter output to the appropriate node
- if context.scene.differential_diffusion and context.scene.generation_method not in ('refine', 'local_edit'):
- prompt[NODES['differential_diffusion']]["inputs"]["model"] = [NODES['ipadapter'], 0]
- else:
- prompt[NODES['sampler']]["inputs"]["model"] = [NODES['ipadapter'], 0]
-
- # Set IPAdapter image source
- prompt[NODES['ipadapter_image']]["inputs"]["image"] = ipadapter_ref_info['name']
-
-
- # Connect ipadapter image to the input
- prompt[NODES['ipadapter']]["inputs"]["image"] = [NODES['ipadapter_image'], 0]
-
- # Configure IPAdapter settings
- prompt[NODES['ipadapter']]["inputs"]["weight"] = context.scene.ipadapter_strength
- prompt[NODES['ipadapter']]["inputs"]["start_at"] = context.scene.ipadapter_start
- prompt[NODES['ipadapter']]["inputs"]["end_at"] = context.scene.ipadapter_end
-
- # Set weight type
- weight_type_mapping = {
- 'standard': "standard",
- 'prompt': "prompt is more important",
- 'style': "style transfer"
- }
- prompt[NODES['ipadapter']]["inputs"]["weight_type"] = weight_type_mapping.get(context.scene.ipadapter_weight_type, "standard")
-
- def _configure_sequential_mode(self, prompt, context, NODES):
- """Configures the prompt for sequential generation mode."""
- # Connect image directly to VAE
- prompt[NODES['vae_encode_inpaint']]["inputs"]["pixels"] = [NODES['input_image'], 0]
- if context.scene.differential_diffusion:
- # Set the noise_mask flag according to context.scene.differential_noise
- prompt[NODES['inpaint_conditioning']]["inputs"]["noise_mask"] = context.scene.differential_noise
-
- def _refine_configure_images(self, prompt, render_info, NODES):
- """Configures the input images for the refinement process."""
- # Set render image
- if render_info:
- prompt[NODES['render_image']]["inputs"]["image"] = render_info['name']
-
- def _refine_build_controlnet_chain(self, prompt, context, controlnet_info, NODES):
- """Builds the ControlNet chain for refinement process."""
- # Determine inputs for ControlNet chain
- pos_input = NODES['pos_prompt'] if (not context.scene.differential_diffusion or
- context.scene.generation_method in ["grid", "refine", "local_edit"]) else NODES['inpaint_conditioning']
- neg_input = NODES['neg_prompt'] if (not context.scene.differential_diffusion or
- context.scene.generation_method in ["grid", "refine", "local_edit"]) else NODES['inpaint_conditioning']
- vae_input = NODES['checkpoint']
-
- # Build the ControlNet chain
- prompt, final = self._build_controlnet_chain_extended(
- context, prompt, pos_input, neg_input, vae_input,
- controlnet_info
- )
-
- # Connect final outputs to KSampler
- prompt[NODES['sampler']]["inputs"]["positive"] = [final, 0]
- prompt[NODES['sampler']]["inputs"]["negative"] = [final, 1]
-
- return prompt
-
- def create_base_prompt_flux(self, context):
- """Creates and configures the base Flux prompt.
- Uses prompt_text_flux and does not include negative prompt or LoRA configuration.
- """
- from .util.helpers import prompt_text_flux
- prompt = json.loads(prompt_text_flux)
- # Define node IDs for Flux
- NODES = {
- 'pos_prompt': "6", # CLIPTextEncode for positive prompt
- 'vae_loader': "10", # VAELoader
- 'dual_clip': "11", # DualCLIPLoader
- 'unet_loader': "12", # UNETLoader
- 'sampler': "13", # SamplerCustomAdvanced
- 'ksampler': "16", # KSamplerSelect
- 'scheduler': "17", # BasicScheduler
- 'guider': "22", # BasicGuider
- 'noise': "25", # RandomNoise
- 'flux_guidance': "26", # FluxGuidance
- 'latent': "30", # EmptyLatentImage
- 'save_image': "32" # SaveImageWebsocket
- }
-
- 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
- # Prepend the view description
- base_prompt_text = f"{view_desc}, {base_prompt_text}"
-
- # Set positive prompt only (Flux doesn't use negative prompt)
- prompt[NODES['pos_prompt']]["inputs"]["text"] = base_prompt_text
-
- # Configure sampler parameters
- prompt[NODES['noise']]["inputs"]["noise_seed"] = context.scene.seed
- prompt[NODES['scheduler']]["inputs"]["steps"] = context.scene.steps
- prompt[NODES['scheduler']]["inputs"]["scheduler"] = context.scene.scheduler
- prompt[NODES['flux_guidance']]["inputs"]["guidance"] = context.scene.cfg
- prompt[NODES['ksampler']]["inputs"]["sampler_name"] = context.scene.sampler
-
- # Replace unet_loader with UNETLoaderGGUF if using GGUF model
- if ".gguf" in context.scene.model_name:
- del prompt[NODES['unet_loader']]
- from .util.helpers import gguf_unet_loader
- unet_loader_dict = json.loads(gguf_unet_loader)
- prompt.update(unet_loader_dict)
-
- # Set the model name
- prompt[NODES['unet_loader']]["inputs"]["unet_name"] = context.scene.model_name
-
- # Flux does not use negative prompt or LoRA.
- return prompt, NODES
-
- def configure_ipadapter_flux(self, prompt, context, ipadapter_ref_info, NODES):
- # Configure IPAdapter if enabled
- from .util.helpers import ipadapter_flux
- ipadapter_dict = json.loads(ipadapter_flux)
- prompt.update(ipadapter_dict)
-
- # Label nodes
- NODES['ipadapter_loader'] = "242" # IPAdapterFluxLoader
- NODES['ipadapter'] = "243" # ApplyIPAdapterFlux
- NODES['ipadapter_image'] = "244" # LoadImage for IPAdapter input
-
- # Connect IPAdapter output to guider and scheduler
- prompt[NODES['guider']]["inputs"]["model"] = [NODES['ipadapter'], 0]
- prompt[NODES['scheduler']]["inputs"]["model"] = [NODES['ipadapter'], 0]
-
- # Set IPAdapter image source
- prompt[NODES['ipadapter_image']]["inputs"]["image"] = ipadapter_ref_info['name']
-
- # Connect ipadapter image to the input
- prompt[NODES['ipadapter']]["inputs"]["image"] = [NODES['ipadapter_image'], 0]
-
- # Configure IPAdapter settings
- prompt[NODES['ipadapter']]["inputs"]["weight"] = context.scene.ipadapter_strength
- prompt[NODES['ipadapter']]["inputs"]["start_percent"] = context.scene.ipadapter_start
- prompt[NODES['ipadapter']]["inputs"]["end_percent"] = context.scene.ipadapter_end
-
- # There is no weight type for Flux IPAdapter
-
- def generate_flux(self, context, controlnet_info=None, ipadapter_ref_info=None):
- """Generates an image using Flux 1.
- Similar in structure to generate() but uses Flux nodes, skips negative prompt and LoRA.
- """
- from .util.helpers import prompt_text_flux
- server_address = context.preferences.addons[__package__].preferences.server_address
- client_id = str(uuid.uuid4())
- output_dir = context.preferences.addons[__package__].preferences.output_dir
-
- revision_dir = get_generation_dirs(context)["revision"]
-
- # Build Flux base prompt and node mapping.
- prompt, NODES = self.create_base_prompt_flux(context)
-
- self._configure_resolution(prompt, context, NODES)
-
- # Configure IPAdapter for Flux if enabled
- if ipadapter_ref_info:
- self.configure_ipadapter_flux(prompt, context, ipadapter_ref_info, NODES)
-
- # Build ControlNet chain if not using Depth LoRA
- if not context.scene.use_flux_lora:
- prompt, final_node = self._build_controlnet_chain_extended(
- context, prompt, NODES['pos_prompt'], NODES['pos_prompt'], NODES['vae_loader'],
- controlnet_info
- )
- else: # If using Depth LoRA instead of ControlNet, we do not build a ControlNet chain
- final_node = NODES['pos_prompt'] # Use positive prompt directly if not using ControlNet
- # Add Required nodes for the FLUX.1-Depth-dev LoRA
- from .util.helpers import depth_lora_flux
- depth_lora_dict = json.loads(depth_lora_flux)
- prompt.update(depth_lora_dict)
-
- # Label nodes
- NODES['flux_lora_image'] = "245" # LoadImage
- NODES['instruct_pix'] = "246" # InstructPixToPixConditioning
- NODES['flux_lora'] = "247" # LoraLoaderModelOnly
-
- # Connect nodes
- final_node = NODES['instruct_pix'] # To be connected to flux_guidance
- prompt[NODES['sampler']]["inputs"]["latent_image"] = [NODES['instruct_pix'], 2]
-
- # If using ipadapter, set the apply_ipadapter_flux node to use the flux_lora_image
- _is_original_render_ipadapter = (
- context.scene.sequential_ipadapter
- and context.scene.sequential_ipadapter_mode == 'original_render'
- and context.scene.generation_method == 'local_edit'
- )
- if context.scene.use_ipadapter or _is_original_render_ipadapter or (context.scene.generation_method == 'separate' and context.scene.sequential_ipadapter and self.operator._current_image > 0):
- prompt[NODES['ipadapter']]["inputs"]["model"] = [NODES['flux_lora'], 0]
- else:
- prompt[NODES['guider']]["inputs"]["model"] = [NODES['flux_lora'], 0]
- prompt[NODES['scheduler']]["inputs"]["model"] = [NODES['flux_lora'], 0]
-
- # Delete unnecessary nodes
- if "239" in prompt:
- del prompt["239"] # SetUnionControlNetType
- if "30" in prompt:
- del prompt["30"] # EmptyLatentImage
-
- if controlnet_info and "depth" in controlnet_info:
- prompt[NODES['flux_lora_image']]["inputs"]["image"] = controlnet_info["depth"]['name']
-
- # Connect final node to FluxGuidance
- prompt[NODES['flux_guidance']]["inputs"]["conditioning"] = [final_node, 0]
- # Note: No negative prompt is connected.
-
- # Save prompt for debugging.
- self._save_prompt_to_file(prompt, revision_dir)
-
- # Execute generation via websocket.
- # Execute generation and get results
- ws = self._connect_to_websocket(server_address, client_id)
-
- if ws is None:
- return {"error": "conn_failed"} # Connection error
-
- images = None
- try:
- images = self._execute_prompt_and_get_images(ws, prompt, client_id, server_address, NODES)
- finally:
- if ws:
- ws.close()
-
- if images is None or isinstance(images, dict) and "error" in images:
- return {"error": "conn_failed"}
-
- print(f"Flux image generated with prompt: {context.scene.comfyui_prompt}")
-
- return images[NODES['save_image']][0]
-
- def _create_img2img_base_prompt_flux(self, context):
- """Creates and configures the base Flux prompt for img2img refinement."""
- from .util.helpers import prompt_text_img2img_flux
-
- prompt = json.loads(prompt_text_img2img_flux)
-
- # Node IDs organized by functional category for Flux
- NODES = {
- # Text Prompting
- 'pos_prompt': "6", # CLIPTextEncode for positive prompt
-
- # Model Components
- 'vae_loader': "10", # VAELoader
- 'dual_clip': "11", # DualCLIPLoader
- 'unet_loader': "12", # UNETLoader
-
- # Sampling Control
- 'sampler': "13", # SamplerCustomAdvanced
- 'ksampler': "16", # KSamplerSelect
- 'scheduler': "17", # BasicScheduler
- 'guider': "22", # BasicGuider
- 'noise': "25", # RandomNoise
- 'flux_guidance': "26", # FluxGuidance
-
- # Image Processing
- 'vae_decode': "8", # VAEDecode
- 'vae_encode': "116", # VAEEncode
- 'vae_encode_inpaint': "44", # VAEEncodeForInpaint
- 'upscale': "118", # ImageScale for upscaling
- 'upscale_uv': "43", # ImageScale for UV maps
-
- # Input Images
- 'input_image': "1", # LoadImage for input
- 'mask_image': "42", # LoadImage for mask
- 'render_image': "117", # LoadImage for render
-
- # Mask Processing
- 'grow_mask': "224", # GrowMask
- 'blur': "226", # ImageBlur
- 'image_to_mask': "227", # ImageToMask
- 'mask_to_image': "225", # MaskToImage
-
- # Advanced Features
- 'differential_diffusion': "50", # DifferentialDiffusion for Flux
- 'inpaint_conditioning': "51", # InpaintModelConditioning for Flux
-
- # Latent Space
- 'latent': "30", # EmptyLatentImage
-
- # Output
- 'save_image': "32" # SaveImageWebsocket
- }
-
- 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
- # Prepend the view description
- base_prompt_text = f"{view_desc}, {base_prompt_text}"
-
- # Set positive prompt (Flux doesn't use negative prompt)
- prompt[NODES['pos_prompt']]["inputs"]["text"] = base_prompt_text
-
- # Configure sampler parameters
- prompt[NODES['noise']]["inputs"]["noise_seed"] = context.scene.seed
- prompt[NODES['scheduler']]["inputs"]["steps"] = context.scene.refine_steps if context.scene.generation_method == 'grid' else context.scene.steps
- prompt[NODES['scheduler']]["inputs"]["denoise"] = context.scene.denoise if context.scene.generation_method in ['grid', 'refine', 'local_edit'] else 1.0
- prompt[NODES['flux_guidance']]["inputs"]["guidance"] = context.scene.refine_cfg if context.scene.generation_method == 'grid' else context.scene.cfg
- prompt[NODES['ksampler']]["inputs"]["sampler_name"] = context.scene.refine_sampler if context.scene.generation_method == 'grid' else context.scene.sampler
- prompt[NODES['scheduler']]["inputs"]["scheduler"] = context.scene.refine_scheduler if context.scene.generation_method == 'grid' else context.scene.scheduler
-
- # Replace unet_loader with UNETLoaderGGUF if using GGUF model
- if ".gguf" in context.scene.model_name:
- del prompt[NODES['unet_loader']]
- from .util.helpers import gguf_unet_loader
- unet_loader_dict = json.loads(gguf_unet_loader)
- prompt.update(unet_loader_dict)
-
- # Set the model name
- prompt[NODES['unet_loader']]["inputs"]["unet_name"] = context.scene.model_name
-
- # Configure upscale settings
- prompt[NODES['upscale']]["inputs"]["upscale_method"] = context.scene.refine_upscale_method
- prompt[NODES['upscale']]["inputs"]["width"] = context.scene.render.resolution_x
- prompt[NODES['upscale']]["inputs"]["height"] = context.scene.render.resolution_y
-
- # Configure UV upscale settings
- prompt[NODES['upscale_uv']]["inputs"]["upscale_method"] = "nearest-exact"
- prompt[NODES['upscale_uv']]["inputs"]["width"] = 1024
- prompt[NODES['upscale_uv']]["inputs"]["height"] = 1024
-
- # Configure mask settings
- prompt[NODES['grow_mask']]["inputs"]["expand"] = context.scene.grow_mask_by
- prompt[NODES['blur']]["inputs"]["blur_radius"] = context.scene.blur_mask_radius
- prompt[NODES['blur']]["inputs"]["sigma"] = context.scene.blur_mask_sigma
-
- return prompt, NODES
-
- def refine_flux(self, context, controlnet_info=None, mask_info=None, render_info=None, ipadapter_ref_info=None):
- """
- Refines the image using Flux 1 in ComfyUI.
- :param context: Blender context.
- :param controlnet_info: Dict of uploaded controlnet image info.
- :param mask_info: Uploaded mask image info.
- :param render_info: Uploaded render image info.
- :param ipadapter_ref_info: Uploaded IPAdapter reference image info.
- :return: Refined image.
- """
- # Setup connection parameters
- server_address = context.preferences.addons[__package__].preferences.server_address
- client_id = str(uuid.uuid4())
- output_dir = context.preferences.addons[__package__].preferences.output_dir
-
- revision_dir = get_generation_dirs(context)["revision"]
-
- # Initialize the img2img prompt template for Flux
- prompt, NODES = self._create_img2img_base_prompt_flux(context)
-
- # Configure IPAdapter for Flux if enabled
- if ipadapter_ref_info and context.scene.generation_method != 'uv_inpaint':
- self.configure_ipadapter_flux(prompt, context, ipadapter_ref_info, NODES)
-
- # Configure based on generation method
- self._configure_refinement_mode_flux(prompt, context, render_info, mask_info, ipadapter_ref_info, NODES)
-
- # Set up image inputs for different controlnet types
- self._refine_configure_images_flux(prompt, render_info, NODES)
-
- # Build ControlNet chain if not using Depth LoRA
- if not context.scene.generation_method == 'uv_inpaint':
- if not context.scene.use_flux_lora:
- prompt = self._refine_build_controlnet_chain_flux(
- context, prompt, controlnet_info, NODES
- )
- else: # If using Depth LoRA instead of ControlNet, we do not build a ControlNet chain
- final_node = NODES['pos_prompt'] # Use positive prompt directly if not using ControlNet
- # Add Required nodes for the FLUX.1-Depth-dev LoRA
- from .util.helpers import depth_lora_flux
- depth_lora_dict = json.loads(depth_lora_flux)
- prompt.update(depth_lora_dict)
-
- # Label nodes
- NODES['flux_lora_image'] = "245" # LoadImage
- NODES['instruct_pix'] = "246" # InstructPixToPixConditioning
- NODES['flux_lora'] = "247" # LoraLoaderModelOnly
-
- # Configure InstructPixToPixConditioning inputs to InpaintModelConditioning if using differential diffusion
- if context.scene.differential_diffusion:
- prompt[NODES['instruct_pix']]["inputs"]["positive"] = [NODES['inpaint_conditioning'], 0]
- prompt[NODES['instruct_pix']]["inputs"]["negative"] = [NODES['inpaint_conditioning'], 1]
-
- # Connect nodes
- prompt[NODES['flux_guidance']]["inputs"]["conditioning"] = [NODES['instruct_pix'], 0]
- # prompt[NODES['sampler']]["inputs"]["latent_image"] = [NODES['instruct_pix'], 2] # Not doing since we need to respect the mask
-
- # If using ipadapter, set the apply_ipadapter_flux node to use the flux_lora_image
- if ipadapter_ref_info and context.scene.generation_method != 'uv_inpaint':
- prompt[NODES['ipadapter']]["inputs"]["model"] = [NODES['flux_lora'], 0]
- prompt[NODES['differential_diffusion']]["inputs"]["model"] = [NODES['ipadapter'], 0]
- else:
- prompt[NODES['guider']]["inputs"]["model"] = [NODES['flux_lora'], 0]
- prompt[NODES['scheduler']]["inputs"]["model"] = [NODES['flux_lora'], 0]
- prompt[NODES['differential_diffusion']]["inputs"]["model"] = [NODES['flux_lora'], 0]
-
- # Delete unnecessary nodes
- if "239" in prompt:
- del prompt["239"] # SetUnionControlNetType
- if "30" in prompt:
- del prompt["30"] # EmptyLatentImage
-
- # Set the image for the Flux LoRA
- if controlnet_info and "depth" in controlnet_info:
- prompt[NODES['flux_lora_image']]["inputs"]["image"] = controlnet_info["depth"]['name']
-
- # Save prompt for debugging
- self._save_prompt_to_file(prompt, revision_dir)
-
- # Execute generation and get results
- ws = self._connect_to_websocket(server_address, client_id)
-
- if ws is None:
- return {"error": "conn_failed"} # Connection error
-
- images = None
- try:
- images = self._execute_prompt_and_get_images(ws, prompt, client_id, server_address, NODES)
- finally:
- if ws:
- ws.close()
-
- if images is None or isinstance(images, dict) and "error" in images:
- return {"error": "conn_failed"}
-
- print(f"Image refined with Flux using prompt: {context.scene.comfyui_prompt}")
-
- # Return the refined image
- return images[NODES['save_image']][0]
-
- def _configure_refinement_mode_flux(self, prompt, context, render_info, mask_info, ipadapter_ref_info, NODES):
- """Configures the prompt based on the specific refinement mode for Flux."""
- # Configure based on generation method
- if context.scene.generation_method in ('refine', 'local_edit'):
- # Configure for refine mode - load render directly
- if render_info:
- prompt[NODES['render_image']]["inputs"]["image"] = render_info['name']
- prompt[NODES['vae_encode']]["inputs"]["pixels"] = [NODES['render_image'], 0]
- # Connect latent to sampler
- prompt[NODES['sampler']]["inputs"]["latent_image"] = [NODES['vae_encode'], 0]
-
- elif context.scene.generation_method in ['uv_inpaint', 'sequential']:
- # Configure for inpainting modes
- if mask_info:
- prompt[NODES['mask_image']]["inputs"]["image"] = mask_info['name']
- if render_info:
- prompt[NODES['input_image']]["inputs"]["image"] = render_info['name']
-
- # Configure mask processing
- if not context.scene.blur_mask:
- prompt[NODES['vae_encode_inpaint']]["inputs"]["mask"] = [NODES['grow_mask'], 0]
- if context.scene.differential_diffusion:
- prompt[NODES['inpaint_conditioning']]["inputs"]["mask"] = [NODES['grow_mask'], 0]
- else:
- # Configure blur chain
- prompt[NODES['image_to_mask']]["inputs"]["image"] = [NODES['blur'], 0]
- prompt[NODES['vae_encode_inpaint']]["inputs"]["mask"] = [NODES['image_to_mask'], 0]
-
- # Different setups based on differential diffusion
- if context.scene.differential_diffusion:
- # Connect differential diffusion between model loader and other components
- prompt[NODES['guider']]["inputs"]["model"] = [NODES['differential_diffusion'], 0]
- prompt[NODES['scheduler']]["inputs"]["model"] = [NODES['differential_diffusion'], 0]
-
- # Connect inpaint conditioning to differential diffusion
- if ipadapter_ref_info:
- prompt[NODES['differential_diffusion']]["inputs"]["model"] = [NODES['ipadapter'], 0]
- else:
- prompt[NODES['differential_diffusion']]["inputs"]["model"] = [NODES['unet_loader'], 0]
-
- # Configure inpaint conditioning with proper input image and mask
- prompt[NODES['inpaint_conditioning']]["inputs"]["pixels"] = [
- NODES['upscale_uv'], 0
- ] if context.scene.generation_method == 'uv_inpaint' else [NODES['input_image'], 0]
-
- # Connect latent to sampler from inpaint conditioning
- prompt[NODES['sampler']]["inputs"]["latent_image"] = [NODES['inpaint_conditioning'], 2]
-
- # Connect conditioning to flux_guidance
- prompt[NODES['flux_guidance']]["inputs"]["conditioning"] = [NODES['inpaint_conditioning'], 0]
- else:
- # Standard setup without differential diffusion
- prompt[NODES['sampler']]["inputs"]["latent_image"] = [NODES['vae_encode_inpaint'], 0]
-
- if context.scene.generation_method == 'uv_inpaint':
- self._configure_uv_inpainting_mode_flux(prompt, context, render_info, NODES)
- else: # Sequential mode
- self._configure_sequential_mode_flux(prompt, context, NODES)
-
- def _configure_uv_inpainting_mode_flux(self, prompt, context, render_info, NODES):
- """Configures the prompts for UV inpainting mode in Flux."""
- # UV inpainting specific configuration
- prompt[NODES['upscale_uv']]["inputs"]["image"] = [NODES['input_image'], 0]
-
- if not context.scene.differential_diffusion:
- prompt[NODES['vae_encode_inpaint']]["inputs"]["pixels"] = [NODES['upscale_uv'], 0]
- else:
- # Set the noise_mask flag according to context.scene.differential_noise
- prompt[NODES['inpaint_conditioning']]["inputs"]["noise_mask"] = context.scene.differential_noise
-
- # Create UV-specific prompt
- uv_prompt = f"seamless (UV-unwrapped texture) of {_texturing_prompt(context.scene)}, consistent material continuity, no visible seams or stretching"
- prompt[NODES['pos_prompt']]["inputs"]["text"] = uv_prompt
-
- # Object-specific prompt if available
- if render_info and 'name' in render_info:
- current_object_name = os.path.basename(render_info['name']).split('.')[0]
- object_prompt = self.operator._object_prompts.get(current_object_name, _texturing_prompt(context.scene))
- if object_prompt:
- uv_prompt = f"(UV-unwrapped texture) of {object_prompt}, consistent material continuity, no visible seams or stretching"
- prompt[NODES['pos_prompt']]["inputs"]["text"] = uv_prompt
-
- def _configure_sequential_mode_flux(self, prompt, context, NODES):
- """Configures the prompt for sequential generation mode in Flux."""
- # Direct connection for sequential mode
- if not context.scene.differential_diffusion:
- prompt[NODES['vae_encode_inpaint']]["inputs"]["pixels"] = [NODES['input_image'], 0]
- else:
- # Set the noise_mask flag according to context.scene.differential_noise
- prompt[NODES['inpaint_conditioning']]["inputs"]["noise_mask"] = context.scene.differential_noise
-
- def _refine_configure_images_flux(self, prompt, render_info, NODES):
- """Configures the input images for the refinement process in Flux."""
- # Set render image if provided
- if render_info:
- prompt[NODES['render_image']]["inputs"]["image"] = render_info['name']
-
- # Control images are handled by the controlnet chain builder
- def _refine_build_controlnet_chain_flux(self, context, prompt, controlnet_info, NODES):
- """Builds the ControlNet chain for refinement process with Flux."""
- input = NODES['pos_prompt'] if not context.scene.differential_diffusion else NODES['inpaint_conditioning']
- # For Flux, the controlnet chain connects to the guidance node
- prompt, final_node = self._build_controlnet_chain_extended(
- context, prompt, input, input, NODES['vae_loader'],
- controlnet_info
- )
- # Connect final node to FluxGuidance conditioning input
- prompt[NODES['flux_guidance']]["inputs"]["conditioning"] = [final_node, 0]
- return prompt
+# Backward-compat: re-export top-level helpers moved to texturing.workflows
+from .texturing.workflows import _texturing_prompt, _generation_prompt # noqa: E402,F401