Transform your 3D texturing workflow with the power of generative AI, directly within Blender!
add remote comfyui backend support, rework controlnet mappings, fix progress bar skipping forward
sakalond4aa61f517c85
diff --git a/stablegen/__init__.py b/stablegen/__init__.pyindex fd6a5aa..539b335 100755--- a/stablegen/__init__.py+++ b/stablegen/__init__.py@@ -5,13 +5,15 @@ from .render_tools import BakeTextures, AddCameras, SwitchMaterial, ExportOrbitGfrom .utils import AddHDRI, ApplyModifiers, CurvesToMeshfrom .generator import ComfyUIGenerate, Reproject, Regenerateimport os+import requests+import jsonfrom bpy.app.handlers import persistentbl_info = {"name": "StableGen","category": "Object","author": "Ondrej Sakala",- "version": (0, 0, 8),+ "version": (0, 0, 9),'blender': (4, 2, 0)}@@ -34,73 +36,114 @@ classes = [Regenerate]-def update_combined(self, context): # Combined with load_handler to load controlnet unit on first setup+# Global caches for model lists fetched via API+_cached_checkpoint_list = [("NONE_AVAILABLE", "Refresh List", "Fetch models from server")]+_cached_lora_list = [("NONE_AVAILABLE", "Refresh List", "Fetch models from server")]++def update_combined(self, context):+ # This now primarily updates the preset status and might trigger Enum updates implicitly+ # Check if server is reachable+ if not check_server_availability(self.server_address, timeout=0.5):+ self.server_online = False+ print("ComfyUI server is not reachable.")+ return None+ else:+ self.server_online = True+update_parameters(self, context)load_handler(None)+ # Automatically Refresh Lists on Server Change+ # Check if server address is valid before trying to refresh+ if self.server_address:+ print("Server address changed, attempting to refresh model lists...")+ 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') # Refresh CN too+ except Exception as e:+ print(f"Error during deferred refresh: {e}")+ return None # Timer runs only once+ bpy.app.timers.register(deferred_refresh, first_interval=0.1)+ else:+ print("Server address cleared, cannot refresh lists.")+ global _cached_checkpoint_list, _cached_lora_list+ _cached_checkpoint_list = [("NO_SERVER", "Set Server Address", "...")]+ _cached_lora_list = [("NO_SERVER", "Set Server Address", "...")]+# Checkpoint model resetcurrent_checkpoint = context.scene.model_name+ # update_model_list is now the API versioncheckpoint_items = update_model_list(self, context)valid_checkpoint_ids = {item[0] for item in checkpoint_items}-- placeholder_id = 'NONE_AVAILABLE'+ placeholder_id = next((item[0] for item in checkpoint_items if item[0].startswith("NO_") or item[0] == "NONE_FOUND"), None)if current_checkpoint not in valid_checkpoint_ids:- if placeholder_id in valid_checkpoint_ids:+ if placeholder_id:context.scene.model_name = placeholder_id- elif checkpoint_items: # If no placeholder but other items, pick first+ elif checkpoint_items: # If no placeholder but other items, pick first valid onecontext.scene.model_name = checkpoint_items[0][0]+ # else: # No models found at all, leave it potentially invalid or set to a default if possible- # LoRA unit reset+ # LoRA unit reset (uses API now)if hasattr(context.scene, 'lora_units'):- lora_items = get_lora_models(self, context)+ lora_items = get_lora_models(self, context) # API versionvalid_lora_ids = {item[0] for item in lora_items}+ placeholder_lora_id = next((item[0] for item in lora_items if item[0].startswith("NO_") or item[0] == "NONE_FOUND"), None)++ # Iterate safely while removing+ indices_to_remove = []+ for i, lora_unit in enumerate(context.scene.lora_units):+ if lora_unit.model_name not in valid_lora_ids or lora_unit.model_name == placeholder_lora_id:+ indices_to_remove.append(i)- for id, lora_unit in enumerate(context.scene.lora_units):- if lora_unit.model_name not in valid_lora_ids or lora_unit.model_name == "NONE_AVAILABLE":- # Remove the unit- context.scene.lora_units.remove(id)- # Check if the current LoRA unit index is valid- if context.scene.lora_units_index >= len(context.scene.lora_units) or context.scene.lora_units_index < 0:- context.scene.lora_units_index = max(0, len(context.scene.lora_units) - 1)+ # Remove invalid units in reverse order to maintain indices+ for i in sorted(indices_to_remove, reverse=True):+ context.scene.lora_units.remove(i)++ # Check/reset current LoRA unit index+ num_loras = len(context.scene.lora_units)+ if context.scene.lora_units_index >= num_loras:+ context.scene.lora_units_index = max(0, num_loras - 1)+ elif num_loras == 0:+ context.scene.lora_units_index = 0 # Or -1 if appropriate, ensure index is validreturn 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__- comfyui_dir: bpy.props.StringProperty(- name="ComfyUI Directory",- description="Path to the ComfyUI directory.",- default="",- subtype='DIR_PATH',- update=update_combined- ) # type: ignore-- external_checkpoints_dir: bpy.props.StringProperty(- name="External Checkpoints Directory (Optional)",- description="Path to an additional directory for checkpoint models. Ensure ComfyUI is also configured to see this path (e.g., via extra_model_paths.yaml).",- default="",- subtype='DIR_PATH',- update=update_combined,- ) # type: ignore-- external_loras_dir: bpy.props.StringProperty(- name="External LoRAs Directory (Optional)",- description="Path to an additional directory for LoRA models. Ensure ComfyUI is also configured to see this path.",- default="",- subtype='DIR_PATH',- update=update_combined,- ) # type: ignore-server_address: bpy.props.StringProperty(name="Server Address",description="Address of the ComfyUI server",default="127.0.0.1:8188",- update=update_parameters+ update=update_combined) # type: ignoreoutput_dir: bpy.props.StringProperty(@@ -111,13 +154,10 @@ class StableGenAddonPreferences(bpy.types.AddonPreferences):update=update_parameters) # type: ignore- controlnet_mapping: bpy.props.StringProperty(- name="ControlNet Mapping",- description="JSON mapping of controlnet type to model files. Example: {\"depth\": [\"controlnet_depth_sdxl.safetensors\", \"control_v11f1p_sd15_depth.pth\"], \"canny\": [\"sdxl_canny.safetensors\"]}\- \nOnly following types are currently supported: depth, canny",- default='{"depth": ["controlnet_depth_sdxl.safetensors", "sdxl_depth_alt.safetensors","sdxl_promax.safetensors", "controlnet_flux1_union_pro.safetensors"], "canny": ["sdxl_promax.safetensors", "controlnet_flux1_union_pro.safetensors"], "normal": ["sdxl_promax.safetensors"]}',- update=update_parameters- ) # type: ignore+ controlnet_model_mappings: bpy.props.CollectionProperty(+ type=ControlNetModelMappingItem,+ name="ControlNet Model Mappings"+ ) # type: ignoresave_blend_file: bpy.props.BoolProperty(name="Save Blend File",@@ -126,6 +166,14 @@ class StableGenAddonPreferences(bpy.types.AddonPreferences):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+def draw(self, context):"""Draws the preferences panel.@@ -133,19 +181,298 @@ class StableGenAddonPreferences(bpy.types.AddonPreferences)::return: None"""layout = self.layout- layout.prop(self, "comfyui_dir")layout.prop(self, "output_dir")- layout.prop(self, "server_address")- layout.prop(self, "controlnet_mapping")+ 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()- split = layout.split(factor=0.5)- split.label(text=self.bl_rna.properties['external_checkpoints_dir'].name)- split.prop(self, "external_checkpoints_dir", text="")-- split = layout.split(factor=0.5)- split.label(text=self.bl_rna.properties['external_loras_dir'].name)- split.prop(self, "external_loras_dir", text="")++ 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+ )++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):+ # Can run if server address is set+ prefs = context.preferences.addons.get(__package__)+ # Also check that another check isn't running if using threading later+ # global _is_refreshing+ # return prefs and prefs.preferences.server_address and not _is_refreshing+ return prefs and prefs.preferences.server_address # Simplified for sync check++ def execute(self, context):+ prefs = context.preferences.addons[__package__].preferences+ server_addr = prefs.server_address++ print(f"Checking server status at {server_addr}...")+ # Use the existing check function with a short timeout+ is_online = check_server_availability(server_addr, timeout=0.75) # Use slightly longer than 0.5?++ prefs.server_online = is_online # Update the preference property++ if is_online:+ self.report({'INFO'}, f"ComfyUI server is online at {server_addr}.")+ # Optionally trigger full refresh here if desired on manual check success+ # 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:+ self.report({'ERROR'}, f"ComfyUI server unreachable or timed out at {server_addr}.")++ # The update function on the property handles UI redraw++ 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):+ # Can run if server address is set+ prefs = context.preferences.addons.get(__package__)+ return prefs and prefs.preferences.server_address++ def execute(self, context):+ prefs = context.preferences.addons[__package__].preferences+ server_models = fetch_from_comfyui_api(context, "/models/controlnet")++ if server_models is None: # Indicates connection/config error+ self.report({'ERROR'}, "Could not fetch models. Check server address and ensure ComfyUI is running.")+ return {'CANCELLED'}++ if not server_models:+ self.report({'WARNING'}, "No ControlNet models found on the server.")+ # Clear existing list if server returns empty+ prefs.controlnet_model_mappings.clear()+ return {'FINISHED'}++ # Synchronization Logic+ current_mappings = {item.name: item for item in prefs.controlnet_model_mappings}+ server_model_set = set(server_models)+ current_model_set = set(current_mappings.keys())++ # 1. Remove models from prefs that are no longer on the server+ models_to_remove = current_model_set - server_model_set+ indices_to_remove = []+ for i, item in enumerate(prefs.controlnet_model_mappings):+ if item.name in models_to_remove:+ indices_to_remove.append(i)++ # Remove in reverse order to avoid index issues+ for i in sorted(indices_to_remove, reverse=True):+ prefs.controlnet_model_mappings.remove(i)+ # Adjust index if necessary+ if prefs.controlnet_mapping_index >= len(prefs.controlnet_model_mappings):+ prefs.controlnet_mapping_index = max(0, len(prefs.controlnet_model_mappings) - 1)+++ # 2. Add new models found on the server+ models_to_add = server_model_set - current_model_set+ for model_name in sorted(list(models_to_add)):+ new_item = prefs.controlnet_model_mappings.add()+ new_item.name = model_name++ # Guessing Logic+ name_lower = model_name.lower()+ is_union_guess = 'union' in name_lower or 'promax' in name_lower++ # Guess based on keywords, prioritizing union+ if is_union_guess:+ new_item.supports_depth = True+ new_item.supports_canny = True+ new_item.supports_normal = True # Assume union supports all current types+ 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 no specific type keyword found (and not union), leave all as False+ if not is_union_guess and not new_item.supports_depth and not new_item.supports_canny and not new_item.supports_normal:+ print(f" Could not guess type for '{model_name}'. Please assign manually.")++ self.report({'INFO'}, f"Refreshed ControlNet list: {len(models_to_add)} added, {len(models_to_remove)} removed.")+ return {'FINISHED'}++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+ 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 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=0.5): # 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=5) # 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 failuredef get_models_from_directory(scan_root_path: str, valid_extensions: tuple, type_for_description: str, path_prefix_for_id: str = ""):@@ -208,35 +535,12 @@ def merge_and_deduplicate_models(model_lists: list):return merged_itemsdef update_model_list(self, context):- """- Populates EnumProperty items with checkpoint models from ComfyUI/models/checkpoints.- Returns a list of (identifier, name, description) tuples.- """- addon_prefs = context.preferences.addons[__package__].preferences- comfyui_base_dir = addon_prefs.comfyui_dir- external_ckpts_dir = addon_prefs.external_checkpoints_dir-- all_model_items = []-- # 1. Scan ComfyUI standard checkpoints path- if comfyui_base_dir and os.path.isdir(comfyui_base_dir):- if context.scene.model_architecture == 'sdxl':- comfy_ckpts_path = os.path.join(comfyui_base_dir, "models", "checkpoints")- types = ('.safetensors', '.ckpt', '.pth', '.sft')- else:- comfy_ckpts_path = os.path.join(comfyui_base_dir, "models", "unet") # For FLUX1- types = ('.safetensors', '.ckpt', '.pth', '.sft', '.gguf')- all_model_items.append(- get_models_from_directory(comfy_ckpts_path, types, "Checkpoint")- )-- # 2. Scan External Checkpoints Path- if external_ckpts_dir and os.path.isdir(external_ckpts_dir):- all_model_items.append(- get_models_from_directory(external_ckpts_dir, types, "Ext. Checkpoint")- )-- return merge_and_deduplicate_models(all_model_items)+ """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", "Refresh List", "Fetch models from server")]+ return _cached_checkpoint_listdef update_union(self, context):if "union" in self.model_name.lower() or "promax" in self.model_name.lower():@@ -325,40 +629,164 @@ class LoRAUnit(bpy.types.PropertyGroup):def get_controlnet_models(context, unit_type):"""- Get available ControlNet models for a given 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."""- addon_prefs = context.preferences.addons[__package__].preferences- try:- import json- mapping = json.loads(addon_prefs.controlnet_mapping)- if unit_type in mapping:- return [(model, model, "") for model in mapping[unit_type]]- except json.JSONDecodeError:- return []- return []+ 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 itemsdef get_lora_models(self, context):- addon_prefs = context.preferences.addons[__package__].preferences- comfyui_base_dir = addon_prefs.comfyui_dir- external_loras_dir = addon_prefs.external_loras_dir+ """Returns the cached list of LoRA models."""+ global _cached_lora_list+ # Basic check+ if not _cached_lora_list:+ return [("NONE_AVAILABLE", "Refresh List", "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"- all_lora_items = []+ @classmethod+ def poll(cls, context):+ prefs = context.preferences.addons.get(__package__)+ return prefs and prefs.preferences.server_address- # 1. Scan ComfyUI standard LoRAs path- if comfyui_base_dir and os.path.isdir(comfyui_base_dir):- comfy_loras_path = os.path.join(comfyui_base_dir, "models", "loras")- all_lora_items.append(- get_models_from_directory(comfy_loras_path, ('.safetensors', '.ckpt', '.pt', '.pth'), "LoRA")- )+ def execute(self, context):+ global _cached_checkpoint_list+ items = []+ model_list = None # Initialize to None+ # Determine endpoint based on current architecture setting+ if context.scene.model_architecture == 'sdxl':+ model_list = fetch_from_comfyui_api(context, "/models/checkpoints")+ model_type_desc = "Checkpoint"+ else: # flux1+ model_list = fetch_from_comfyui_api(context, "/models/unet")+ model_type_desc = "UNET"++ if model_list is None: # Config error+ _cached_checkpoint_list = [("NO_SERVER", "Set Server Address", "Cannot fetch")]+ self.report({'ERROR'}, "Cannot fetch models. Check server address.")+ # Force UI update if possible+ context.area.tag_redraw()+ return {'CANCELLED'}+ elif not model_list: # API ok, but empty list+ _cached_checkpoint_list = [("NONE_FOUND", f"No {model_type_desc}s Found", "Server list is empty")]+ self.report({'WARNING'}, f"No {model_type_desc} models found on server.")+ else: # Models found+ for model_name in sorted(model_list):+ items.append((model_name, model_name, f"{model_type_desc}: {model_name}"))+ _cached_checkpoint_list = items+ self.report({'INFO'}, f"Refreshed {model_type_desc} list ({len(items)} found).")++ # Reset Logic after refresh+ current_checkpoint = context.scene.model_name+ valid_checkpoint_ids = {item[0] for item in _cached_checkpoint_list}+ placeholder_id = next((item[0] for item in _cached_checkpoint_list if item[0].startswith("NO_") or item[0] == "NONE_FOUND"), None)++ if current_checkpoint not in valid_checkpoint_ids:+ if placeholder_id:+ context.scene.model_name = placeholder_id+ elif _cached_checkpoint_list:+ context.scene.model_name = _cached_checkpoint_list[0][0]++ # Force UI update if possible (e.g., redraw panels)+ if context.area:+ context.area.tag_redraw()+ # A more robust redraw might be needed depending on context+ # bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)- # 2. Scan External LoRAs Path- if external_loras_dir and os.path.isdir(external_loras_dir):- all_lora_items.append(- get_models_from_directory(external_loras_dir, ('.safetensors', '.ckpt', '.pt', '.pth'), "Ext. LoRA")- )-- return merge_and_deduplicate_models(all_lora_items)+ 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__)+ return prefs and prefs.preferences.server_address++ def execute(self, context):+ global _cached_lora_list+ items = []+ lora_list = fetch_from_comfyui_api(context, "/models/loras")++ if lora_list is None: # Config error+ _cached_lora_list = [("NO_SERVER", "Set Server Address", "Cannot fetch")]+ self.report({'ERROR'}, "Cannot fetch LoRAs. Check server address.")+ context.area.tag_redraw()+ return {'CANCELLED'}+ elif not lora_list: # API ok, but empty+ _cached_lora_list = [("NONE_FOUND", "No LoRAs Found", "Server list is empty")]+ self.report({'WARNING'}, "No LoRA models found on server.")+ else: # LoRAs found+ for lora_name in sorted(lora_list):+ items.append((lora_name, lora_name, f"LoRA: {lora_name}"))+ _cached_lora_list = items+ self.report({'INFO'}, f"Refreshed LoRA list ({len(items)} found).")++ # Reset Logic after refresh+ if hasattr(context.scene, 'lora_units'):+ valid_lora_ids = {item[0] for item in _cached_lora_list}+ placeholder_lora_id = next((item[0] for item in _cached_lora_list if item[0].startswith("NO_") or item[0] == "NONE_FOUND"), None)+ indices_to_remove = []+ for i, lora_unit in enumerate(context.scene.lora_units):+ if lora_unit.model_name not in valid_lora_ids or lora_unit.model_name == placeholder_lora_id:+ indices_to_remove.append(i)+ for i in sorted(indices_to_remove, reverse=True):+ context.scene.lora_units.remove(i)++ num_loras = len(context.scene.lora_units)+ if context.scene.lora_units_index >= num_loras:+ context.scene.lora_units_index = max(0, num_loras - 1)+ elif num_loras == 0:+ context.scene.lora_units_index = 0++ if context.area:+ context.area.tag_redraw()+ # bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)+ return {'FINISHED'}class AddControlNetUnit(bpy.types.Operator):bl_idname = "stablegen.add_controlnet_unit"@@ -461,15 +889,6 @@ class AddLoRAUnit(bpy.types.Operator):if not addon_prefs: # Should not happen if addon is enabledreturn Falseaddon_prefs = addon_prefs.preferences-- comfyui_dir = addon_prefs.comfyui_dir- external_loras_dir = addon_prefs.external_loras_dir-- # Initial check: at least one base directory must be set- if not (comfyui_dir and os.path.isdir(comfyui_dir)) and \- not (external_loras_dir and os.path.isdir(external_loras_dir)):- cls.poll_message_set("Neither ComfyUI nor External LoRA directory is set/valid.")- return False# Get the merged list of LoRAs.# Assuming get_lora_models is robust and returns placeholders if dirs are bad.@@ -608,7 +1027,7 @@ def load_handler(dummy):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)-classes_to_append = [StableGenAddonPreferences, ControlNetUnit, LoRAUnit, AddControlNetUnit, RemoveControlNetUnit, AddLoRAUnit, RemoveLoRAUnit]+classes_to_append = [CheckServerStatus, RefreshCheckpointList, RefreshLoRAList, STABLEGEN_UL_ControlNetMappingList, ControlNetModelMappingItem, RefreshControlNetMappings, StableGenAddonPreferences, ControlNetUnit, LoRAUnit, AddControlNetUnit, RemoveControlNetUnit, AddLoRAUnit, RemoveLoRAUnit]for cls in classes_to_append:classes.append(cls)@@ -620,6 +1039,27 @@ def register():for cls in classes:bpy.utils.register_class(cls)+ def initial_refresh():+ print("StableGen: Performing initial model list refresh...")+ try:+ # 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)+ 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="Enter the text prompt for ComfyUI generation",@@ -1264,6 +1704,12 @@ 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_loradel bpy.types.Scene.comfyui_promptdel bpy.types.Scene.comfyui_negative_promptdiff --git a/stablegen/blender_manifest.toml b/stablegen/blender_manifest.tomlindex de87f1e..aaae525 100644--- a/stablegen/blender_manifest.toml+++ b/stablegen/blender_manifest.toml@@ -1,6 +1,6 @@schema_version = "1.0.0"id = "stablegen"-version = "0.0.8"+version = "0.0.9"name = "StableGen"tagline = "AI-Powered Texturing"maintainer = "Ondrej Sakala <[email protected]>"diff --git a/stablegen/generator.py b/stablegen/generator.pyindex 691e809..53ffaab 100644--- a/stablegen/generator.py+++ b/stablegen/generator.py@@ -7,6 +7,9 @@ import jsonimport urllib.requestimport socketimport threading+import requests+import traceback+import jsonfrom datetime import datetimeimport math@@ -46,6 +49,11 @@ class Regenerate(bpy.types.Operator):"""# Check for other modal operatorsoperator = None+ addon_prefs = context.preferences.addons[__package__].preferences+ if not os.path.exists(addon_prefs.output_dir):+ return False+ if not addon_prefs.server_address or not addon_prefs.server_online:+ return Falseif not (context.scene.generation_method == 'sequential' or context.scene.generation_method == 'separate'):return Falseif context.scene.output_timestamp == "":@@ -212,7 +220,79 @@ class Reproject(bpy.types.Operator):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=120)+ 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 Noneclass ComfyUIGenerate(bpy.types.Operator):"""Generate textures using ComfyUI (to all mesh objects using all cameras in the scene)@@ -237,6 +317,7 @@ class ComfyUIGenerate(bpy.types.Operator):_to_texture = None_original_visibility = Noneproceed_with_high_res: bpy.props.BoolProperty(default=False)+ _uploaded_images_cache: dict = {}# Add properties to track progress_progress = 0.0@@ -288,9 +369,7 @@ class ComfyUIGenerate(bpy.types.Operator):addon_prefs = context.preferences.addons[__package__].preferencesif not os.path.exists(addon_prefs.output_dir):return False- if not os.path.exists(addon_prefs.comfyui_dir):- return False- if not addon_prefs.server_address:+ if not addon_prefs.server_address or not addon_prefs.server_online:return Falseif bpy.app.online_access == False: # Check if online access is disabledreturn False@@ -306,6 +385,9 @@ class ComfyUIGenerate(bpy.types.Operator):self.cancel_generate(context)return {'FINISHED'}+ # Clear the upload cache at the start of a new generation+ self._uploaded_images_cache.clear()+# Timestamp for output directoryif context.scene.generation_mode == 'standard':context.scene.output_timestamp = datetime.now().strftime("%Y-%m-%dT%H-%M-%S")@@ -696,13 +778,42 @@ class ComfyUIGenerate(bpy.types.Operator):"""self._error = Nonetry:- depth_path = None- canny_path = None- normal_path = None- mask_path = None- render_path = Nonewhile self._threads_left > 0 and not context.scene.generation_mode == 'project_only':if 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 == 'refine':+ 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 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 neededif context.scene.generation_mode == 'standard' and camera_id == 0 and (context.scene.generation_method == 'sequential' or context.scene.generation_method == 'separate' or context.scene.generation_method == 'refine')\and context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_regenerate and not context.scene.use_ipadapter and context.scene.sequential_ipadapter_mode == 'first':@@ -715,36 +826,24 @@ class ComfyUIGenerate(bpy.types.Operator):else:self._stage = "Generating Image"self._progress = 0- # Prepare paths using new get_file_path function- if context.scene.generation_method != 'uv_inpaint':- # Get paths for controlnet images for the current camera or grid- depth_path = get_file_path(context, "controlnet", subtype="depth", camera_id=camera_id) if camera_id is not None else get_file_path(context, "controlnet", subtype="depth")- canny_path = get_file_path(context, "controlnet", subtype="canny", camera_id=camera_id) if camera_id is not None else get_file_path(context, "controlnet", subtype="canny")- normal_path = get_file_path(context, "controlnet", subtype="normal", camera_id=camera_id) if camera_id is not None else get_file_path(context, "controlnet", subtype="normal")- else:- # Get paths for UV inpainting for the current object- current_obj_name = self._to_texture[self._current_image].name- mask_path = get_file_path(context, "uv_inpaint", subtype="visibility", object_name=current_obj_name)- render_path = get_file_path(context, "baked", object_name=current_obj_name) # Use baked texture as input render# Generate the imageif context.scene.generation_method == 'refine':- render_path = get_file_path(context, "inpaint", subtype="render", camera_id=camera_id)if context.scene.model_architecture == 'flux1':- image = self.refine_flux(context, depth_path=depth_path, canny_path=canny_path, normal_path=normal_path, render_path=render_path)+ image = self.refine_flux(context, controlnet_info=controlnet_info, render_info=render_info, ipadapter_ref_info=ipadapter_ref_info)else:- image = self.refine(context, depth_path=depth_path, canny_path=canny_path, normal_path=normal_path, render_path=render_path)+ image = self.refine(context, controlnet_info=controlnet_info, render_info=render_info, ipadapter_ref_info=ipadapter_ref_info)elif context.scene.generation_method == 'uv_inpaint':if context.scene.model_architecture == 'flux1':- image = self.refine_flux(context, mask_path=mask_path, render_path=render_path)+ image = self.refine_flux(context, mask_info=mask_info, render_info=render_info)else:- image = self.refine(context, mask_path=mask_path, render_path=render_path)+ image = self.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.generate_flux(context, depth_path=depth_path, canny_path=canny_path, normal_path=normal_path)+ image = self.generate_flux(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)else:- image = self.generate(context, depth_path=depth_path, canny_path=canny_path, normal_path=normal_path)+ image = self.generate(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)else:def context_callback():# Export visibility mask and render for the current camera, we need to use a callback to be in the main thread@@ -755,18 +854,18 @@ class ComfyUIGenerate(bpy.types.Operator):bpy.app.timers.register(context_callback)self._wait_event.wait()self._wait_event.clear()- # Get paths for the previous render and mask- render_path = get_file_path(context, "inpaint", subtype="render", camera_id=self._current_image)- mask_path = get_file_path(context, "inpaint", subtype="visibility", camera_id=self._current_image)+ # 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.refine_flux(context, depth_path=depth_path, render_path=render_path, mask_path=mask_path, canny_path=canny_path, normal_path=normal_path)+ image = self.refine_flux(context, controlnet_info=controlnet_info, render_info=render_info, mask_info=mask_info, ipadapter_ref_info=ipadapter_ref_info)else:- image = self.refine(context, depth_path=depth_path, render_path=render_path, mask_path=mask_path, canny_path=canny_path, normal_path=normal_path)+ image = self.refine(context, controlnet_info=controlnet_info, render_info=render_info, mask_info=mask_info, ipadapter_ref_info=ipadapter_ref_info)else: # Grid or Separateif context.scene.model_architecture == 'flux1':- image = self.generate_flux(context, depth_path=depth_path, canny_path=canny_path, normal_path=normal_path)+ image = self.generate_flux(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)else:- image = self.generate(context, depth_path=depth_path, canny_path=canny_path, normal_path=normal_path)+ image = self.generate(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)if image == {"error": "conn_failed"}:return # Error message already set@@ -781,6 +880,7 @@ class ComfyUIGenerate(bpy.types.Operator):with open(image_path, 'wb') as f:f.write(image)+# Use hack to re-generate the image using IPAdapter to match IPAdapter styleif camera_id == 0 and (context.scene.generation_method == 'sequential' or context.scene.generation_method == 'separate' or context.scene.generation_method == 'refine')\@@ -793,16 +893,17 @@ class ComfyUIGenerate(bpy.types.Operator):self._stage = "Generating Image"context.scene.use_ipadapter = Truecontext.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 == "refine":- image = self.refine(context, depth_path=depth_path, render_path=render_path, mask_path=mask_path, canny_path=canny_path, normal_path=normal_path)+ image = self.refine(context, controlnet_info=controlnet_info, render_info=render_info, mask_info=mask_info, ipadapter_ref_info=ipadapter_ref_info)else:- image = self.generate(context, depth_path=depth_path, canny_path=canny_path, normal_path=normal_path)+ image = self.generate(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)elif context.scene.model_architecture == "flux1":if context.scene.generation_method == "refine":- image = self.refine_flux(context, depth_path=depth_path, render_path=render_path, mask_path=mask_path, canny_path=canny_path, normal_path=normal_path)+ image = self.refine_flux(context, controlnet_info=controlnet_info, render_info=render_info, mask_info=mask_info, ipadapter_ref_info=ipadapter_ref_info)else:- image = self.generate_flux(context, depth_path=depth_path, canny_path=canny_path, normal_path=normal_path)+ image = self.generate_flux(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)context.scene.use_ipadapter = Falseimage_path = image_path.replace(".png", "_ipadapter.png")with open(image_path, 'wb') as f:@@ -820,28 +921,26 @@ class ComfyUIGenerate(bpy.types.Operator):# Wait for the event to be setself._wait_event.wait()self._wait_event.clear()- # Update paths for the next iteration (if any)+ # Update info for the next iteration (if any)if self._current_image < len(self._cameras) - 1:next_camera_id = self._current_image + 1- render_path = get_file_path(context, "inpaint", subtype="render", camera_id=next_camera_id)- mask_path = get_file_path(context, "inpaint", subtype="visibility", camera_id=next_camera_id)- # Update controlnet paths for the next camera- depth_path = get_file_path(context, "controlnet", subtype="depth", camera_id=next_camera_id)- canny_path = get_file_path(context, "controlnet", subtype="canny", camera_id=next_camera_id)- normal_path = get_file_path(context, "controlnet", subtype="normal", camera_id=next_camera_id)-+ # ControlNet info will be re-fetched at the start of the next loop iterationelse: # steps == 0, skip generationpass # No image generation neededif context.scene.generation_method == 'separate' or context.scene.generation_method == 'refine' or context.scene.generation_method == 'sequential':self._current_image += 1self._threads_left -= 1+ if self._threads_left > 0:+ self._progress = 0if camera_id is not None: # Increment camera_id only if it was initially providedcamera_id += 1elif context.scene.generation_method == 'uv_inpaint':self._current_image += 1self._threads_left -= 1+ if self._threads_left > 0:+ self._progress = 0elif context.scene.generation_method == 'grid':# Split the generated grid image back into multiple images@@ -850,17 +949,19 @@ class ComfyUIGenerate(bpy.types.Operator):for i, _ in enumerate(self._cameras):self._stage = f"Refining Image {i+1}/{len(self._cameras)}"self._current_image = i + 1- self._progress = 0- # Refine the split images- refine_depth_path = get_file_path(context, "controlnet", subtype="depth", camera_id=i)- refine_canny_path = get_file_path(context, "controlnet", subtype="canny", camera_id=i)- refine_normal_path = get_file_path(context, "controlnet", subtype="normal", camera_id=i)- refine_render_path = get_file_path(context, "generated", camera_id=i, material_id=self._material_id) # Use the split image as render input+ # 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.refine_flux(context, depth_path=refine_depth_path, canny_path=refine_canny_path, normal_path=refine_normal_path, render_path=refine_render_path)+ image = self.refine_flux(context, controlnet_info=refine_cn_info, render_info=refine_render_info)else:- image = self.refine(context, depth_path=refine_depth_path, canny_path=refine_canny_path, normal_path=refine_normal_path, render_path=refine_render_path)+ image = self.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."@@ -904,13 +1005,77 @@ class ComfyUIGenerate(bpy.types.Operator):elif context.scene.control_after_generate == 'randomize':context.scene.seed = np.random.randint(0, 1000000)- def generate(self, context, depth_path=None, canny_path=None, normal_path=None):+ 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.+ """+ # Ensure correct material_id is used if not provided explicitly+ # Assumes self._material_id is set correctly in the operator instance+ effective_material_id = material_id if material_id is not None else getattr(self, '_material_id', 0)++ # 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++ # 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(local_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++ 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++ def generate(self, context, controlnet_info=None, ipadapter_ref_info=None):"""Generates the image using ComfyUI.:param context: Blender context.- :param depth_path: Path to the depth map image.- :param canny_path: Path to the canny edge image.- :param normal_path: Path to the normal map image.+ :param controlnet_info: Dict of uploaded controlnet image info.+ :param ipadapter_ref_info: Uploaded IPAdapter reference image info.:return: Generated image binary data."""from .util.helpers import prompt_text@@ -927,9 +1092,9 @@ class ComfyUIGenerate(bpy.types.Operator):# Set model resolutionself._configure_resolution(prompt, context, NODES)- if context.scene.use_ipadapter or (context.scene.generation_method == 'separate' and context.scene.sequential_ipadapter and self._current_image > 0):+ if ipadapter_ref_info:# Configure IPAdapter settings- self._configure_ipadapter(prompt, context, NODES)+ self._configure_ipadapter(prompt, context, ipadapter_ref_info, NODES)else:# Remove IPAdapter nodes if not usedfor node_id in ['235', '236', '237']:@@ -937,11 +1102,11 @@ class ComfyUIGenerate(bpy.types.Operator):del prompt[node_id]# Build controlnet chain- prompt = self._build_controlnet_chain(prompt, context, depth_path, canny_path, normal_path, NODES)+ 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 resultsws = self._connect_to_websocket(server_address, client_id)@@ -1061,21 +1226,15 @@ class ComfyUIGenerate(bpy.types.Operator):prompt[NODES['latent']]["inputs"]["width"] = context.scene.render.resolution_xprompt[NODES['latent']]["inputs"]["height"] = context.scene.render.resolution_y- def _configure_ipadapter(self, prompt, context, NODES):+ def _configure_ipadapter(self, prompt, context, ipadapter_ref_info, NODES):# Configure IPAdapter if enabled# Connect IPAdapter output to the appropriate nodeprompt[NODES['sampler']]["inputs"]["model"] = [NODES['ipadapter'], 0]- # Set IPAdapter image source based on ipadapter_image- if context.scene.use_ipadapter:- image_path = bpy.path.abspath(context.scene.ipadapter_image)- prompt[NODES['ipadapter_image']]["inputs"]["image"] = image_path- elif context.scene.sequential_ipadapter:- if context.scene.sequential_ipadapter_mode == 'first':- prompt[NODES['ipadapter_image']]["inputs"]["image"] = get_file_path(context, "generated", camera_id=0, material_id=self._material_id)- else:- prompt[NODES['ipadapter_image']]["inputs"]["image"] = get_file_path(context, "generated", camera_id=self._current_image - 1, material_id=self._material_id)+ # Set IPAdapter image source+ if ipadapter_ref_info:+ prompt[NODES['ipadapter_image']]["inputs"]["image"] = ipadapter_ref_info['name']# Connect ipadapter image to the inputprompt[NODES['ipadapter']]["inputs"]["image"] = [NODES['ipadapter_image'], 0]@@ -1093,7 +1252,7 @@ class ComfyUIGenerate(bpy.types.Operator):}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, image_dict):+ 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.@@ -1105,8 +1264,8 @@ class ComfyUIGenerate(bpy.types.Operator):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.- image_dict (dict): A dictionary mapping ControlNet types (e.g., "depth",- "canny") to their corresponding image file paths.+ 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)@@ -1123,6 +1282,12 @@ class ComfyUIGenerate(bpy.types.Operator):current_neg = neg_inputhas_union = Falsefor 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+# Generate unique keys for nodes in this chain unit.load_key = str(200 + idx * 3) # LoadImage nodeloader_key = str(200 + idx * 3 + 1) # ControlNetLoader node@@ -1131,8 +1296,7 @@ class ComfyUIGenerate(bpy.types.Operator):# Create the LoadImage node.base_prompt[load_key] = {"inputs": {- "image": image_dict.get(unit.unit_type, ""),- "upload": "image"+ "image": uploaded_info['name'],},"class_type": "LoadImage","_meta": {@@ -1235,12 +1399,12 @@ class ComfyUIGenerate(bpy.types.Operator):return prompt, current_model_out, current_clip_out- def _build_controlnet_chain(self, prompt, context, depth_path, canny_path, normal_path, NODES):+ def _build_controlnet_chain(self, prompt, context, controlnet_info, NODES):"""Builds the ControlNet processing chain."""# Build controlnet chain with guidance imagesprompt, final_node = self._build_controlnet_chain_extended(context, prompt, NODES['pos_prompt'], NODES['neg_prompt'], NODES['checkpoint'],- {"depth": depth_path, "canny": canny_path, "normal": normal_path}+ controlnet_info)# Connect final node outputs to the KSampler@@ -1338,31 +1502,32 @@ class ComfyUIGenerate(bpy.types.Operator):print(f"Failed to queue prompt: {str(e)}")raise- def refine(self, context, depth_path=None, canny_path=None, mask_path=None, render_path=None, normal_path=None):+ 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 depth_path: Path to the depth map image.- :param canny_path: Path to the canny edge image.- :param mask_path: Path to the mask image for inpainting.- :param render_path: Path to the input render image.- :param normal_path: Path to the normal map image.+ :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 parametersserver_address = context.preferences.addons[__package__].preferences.server_addressclient_id = str(uuid.uuid4())- output_dir = get_generation_dirs(context)["revision"]+ output_dir = context.preferences.addons[__package__].preferences.output_dir++ revision_dir = get_generation_dirs(context)["revision"]# Initialize the img2img prompt template and configure base settingsprompt, NODES = self._create_img2img_base_prompt(context)# Configure based on generation method- self._configure_refinement_mode(prompt, context, render_path, mask_path, NODES)+ self._configure_refinement_mode(prompt, context, render_info, mask_info, NODES)- if (context.scene.use_ipadapter or (context.scene.sequential_ipadapter and self._current_image > 0)) and context.scene.generation_method != 'uv_inpaint':+ if ipadapter_ref_info and context.scene.generation_method != 'uv_inpaint':# Configure IPAdapter settings- self._configure_ipadapter_refine(prompt, context, NODES)+ self._configure_ipadapter_refine(prompt, context, ipadapter_ref_info, NODES)else:# Remove IPAdapter nodes if not usedfor node_id in ["235", "236", "237"]:@@ -1370,11 +1535,11 @@ class ComfyUIGenerate(bpy.types.Operator):del prompt[node_id]# Set up image inputs for different controlnet types- self._refine_configure_images(prompt, depth_path, canny_path, normal_path, render_path, NODES)+ self._refine_configure_images(prompt, render_info, NODES)# Build controlnet chain for refinement if neededif not context.scene.generation_method == 'uv_inpaint':- prompt = self._refine_build_controlnet_chain(prompt, context, depth_path, canny_path, normal_path, NODES)+ 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]@@ -1438,7 +1603,6 @@ class ComfyUIGenerate(bpy.types.Operator):'input_image': "1",'mask_image': "12",'render_image': "117",- 'depth_image': "108",# Mask Processing'grow_mask': "224",@@ -1531,7 +1695,7 @@ class ComfyUIGenerate(bpy.types.Operator):return prompt, NODES- def _configure_refinement_mode(self, prompt, context, render_path, mask_path, 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 methodif context.scene.generation_method == 'refine':@@ -1546,8 +1710,8 @@ class ComfyUIGenerate(bpy.types.Operator):prompt[NODES['sampler']]["inputs"]["model"] = [NODES['differential_diffusion'], 0]# Configure mask settings- prompt[NODES['mask_image']]["inputs"]["image"] = mask_path- prompt[NODES['input_image']]["inputs"]["image"] = render_path+ prompt[NODES['mask_image']]["inputs"]["image"] = mask_info['name']+ prompt[NODES['input_image']]["inputs"]["image"] = render_info['name']# Configure mask blur settingsif not context.scene.blur_mask:@@ -1563,12 +1727,12 @@ class ComfyUIGenerate(bpy.types.Operator):if context.scene.generation_method == 'uv_inpaint':# Configure UV inpainting specific prompts- self._configure_uv_inpainting_mode(prompt, context, render_path, NODES)+ self._configure_uv_inpainting_mode(prompt, context, render_info, NODES)else: # Sequential mode# Configure sequential mode settings- self._configure_sequential_mode(prompt, context, render_path, NODES)+ self._configure_sequential_mode(prompt, context, NODES)- def _configure_uv_inpainting_mode(self, prompt, context, render_path, NODES):+ def _configure_uv_inpainting_mode(self, prompt, context, render_info, NODES):"""Configures the prompts for UV inpainting mode."""# Connect upscale to VAE / InpaintConditioningif not context.scene.differential_diffusion:@@ -1586,7 +1750,8 @@ class ComfyUIGenerate(bpy.types.Operator):prompt[NODES['neg_prompt']]["inputs"]["text"] = uv_prompt_neg# Get the current object name from the file path- current_object_name = os.path.basename(render_path).split('.')[0]+ 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 availableobject_prompt = self._object_prompts.get(current_object_name, context.scene.comfyui_prompt)@@ -1596,14 +1761,17 @@ class ComfyUIGenerate(bpy.types.Operator):prompt[NODES['pos_prompt']]["inputs"]["text"] = uv_promptprompt[NODES['neg_prompt']]["inputs"]["text"] = uv_prompt_neg- def _configure_ipadapter_refine(self, prompt, context, NODES):+ def _configure_ipadapter_refine(self, prompt, context, ipadapter_ref_info, NODES):+ """Configures IPAdapter settings for refinement mode."""# Connect IPAdapter output to the appropriate nodeif context.scene.differential_diffusion and context.scene.generation_method != 'refine':prompt[NODES['differential_diffusion']]["inputs"]["model"] = [NODES['ipadapter'], 0]else:prompt[NODES['sampler']]["inputs"]["model"] = [NODES['ipadapter'], 0]- if context.scene.use_ipadapter:+ if ipadapter_ref_info:+ prompt[NODES['ipadapter_image']]["inputs"]["image"] = ipadapter_ref_info['name']+ elif context.scene.use_ipadapter:# Set IPAdapter image source based on ipadapter_imageimage_path = bpy.path.abspath(context.scene.ipadapter_image)prompt[NODES['ipadapter_image']]["inputs"]["image"] = image_path@@ -1630,7 +1798,7 @@ class ComfyUIGenerate(bpy.types.Operator):}prompt[NODES['ipadapter']]["inputs"]["weight_type"] = weight_type_mapping.get(context.scene.ipadapter_weight_type, "standard")- def _configure_sequential_mode(self, prompt, context, render_path, NODES):+ def _configure_sequential_mode(self, prompt, context, NODES):"""Configures the prompt for sequential generation mode."""# Connect image directly to VAEprompt[NODES['vae_encode_inpaint']]["inputs"]["pixels"] = [NODES['input_image'], 0]@@ -1638,17 +1806,13 @@ class ComfyUIGenerate(bpy.types.Operator):# Set the noise_mask flag according to context.scene.differential_noiseprompt[NODES['inpaint_conditioning']]["inputs"]["noise_mask"] = context.scene.differential_noise- def _refine_configure_images(self, prompt, depth_path, canny_path, normal_path, render_path, NODES):+ def _refine_configure_images(self, prompt, render_info, NODES):"""Configures the input images for the refinement process."""- # Set depth map- if depth_path:- prompt[NODES['depth_image']]["inputs"]["image"] = depth_path-# Set render image- if render_path:- prompt[NODES['render_image']]["inputs"]["image"] = render_path+ if render_info:+ prompt[NODES['render_image']]["inputs"]["image"] = render_info['name']- def _refine_build_controlnet_chain(self, prompt, context, depth_path, canny_path, normal_path, NODES):+ def _refine_build_controlnet_chain(self, prompt, context, controlnet_info, NODES):"""Builds the ControlNet chain for refinement process."""# Determine inputs for ControlNet chainpos_input = NODES['pos_prompt'] if (not context.scene.differential_diffusion or@@ -1660,7 +1824,7 @@ class ComfyUIGenerate(bpy.types.Operator):# Build the ControlNet chainprompt, final = self._build_controlnet_chain_extended(context, prompt, pos_input, neg_input, vae_input,- {"depth": depth_path, "canny": canny_path, "normal": normal_path}+ controlnet_info)# Connect final outputs to KSampler@@ -2044,7 +2208,7 @@ class ComfyUIGenerate(bpy.types.Operator):# There is no weight type for Flux IPAdapter- def generate_flux(self, context, depth_path=None, canny_path=None, normal_path=None):+ 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."""@@ -2062,13 +2226,13 @@ class ComfyUIGenerate(bpy.types.Operator):# Configure IPAdapter for Flux if enabledif context.scene.use_ipadapter or (context.scene.generation_method == 'separate' and context.scene.sequential_ipadapter and self._current_image > 0):- self.configure_ipadapter_flux(prompt, context, NODES)+ self.configure_ipadapter_flux(prompt, context, ipadapter_ref_info, NODES)# Build ControlNet chain if not using Depth LoRAif 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'],- {"depth": depth_path, "canny": canny_path, "normal": normal_path}+ controlnet_info)else: # If using Depth LoRA instead of ControlNet, we do not build a ControlNet chainfinal_node = NODES['pos_prompt'] # Use positive prompt directly if not using ControlNet@@ -2099,8 +2263,8 @@ class ComfyUIGenerate(bpy.types.Operator):if "30" in prompt:del prompt["30"] # EmptyLatentImage- # Set the image for the Flux LoRA- prompt[NODES['flux_lora_image']]["inputs"]["image"] = depth_path+ if controlnet_info and "depth" in controlnet_info:+ prompt[NODES['flux_lora_image']]["inputs"]["image"] = controlnet_info["depth"]['name']# Connect final node to FluxGuidanceprompt[NODES['flux_guidance']]["inputs"]["conditioning"] = [final_node, 0]@@ -2232,15 +2396,14 @@ class ComfyUIGenerate(bpy.types.Operator):return prompt, NODES- def refine_flux(self, context, depth_path=None, canny_path=None, mask_path=None, render_path=None, normal_path=None):+ 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 depth_path: Path to the depth map image.- :param canny_path: Path to the canny edge image.- :param mask_path: Path to the mask image for inpainting.- :param render_path: Path to the input render image.- :param normal_path: Path to the normal map image.+ :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@@ -2254,21 +2417,20 @@ class ComfyUIGenerate(bpy.types.Operator):prompt, NODES = self._create_img2img_base_prompt_flux(context)# Configure IPAdapter for Flux if enabled- if (context.scene.use_ipadapter or (context.scene.sequential_ipadapter and self._current_image > 0)) and context.scene.generation_method != 'uv_inpaint':- self.configure_ipadapter_flux(prompt, context, NODES)+ 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_path, mask_path, NODES)+ self._configure_refinement_mode_flux(prompt, context, render_info, mask_info, NODES)# Set up image inputs for different controlnet types- self._refine_configure_images_flux(prompt, depth_path, canny_path, normal_path, render_path, NODES)+ self._refine_configure_images_flux(prompt, render_info, NODES)# Build ControlNet chain if not using Depth LoRAif not context.scene.generation_method == 'uv_inpaint':if not context.scene.use_flux_lora:- prompt= self._refine_build_controlnet_chain_flux(- context, prompt, NODES['pos_prompt'], NODES['pos_prompt'], NODES['vae_loader'],- {"depth": depth_path, "canny": canny_path, "normal": normal_path}+ 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 chainfinal_node = NODES['pos_prompt'] # Use positive prompt directly if not using ControlNet@@ -2307,7 +2469,8 @@ class ComfyUIGenerate(bpy.types.Operator):del prompt["30"] # EmptyLatentImage# Set the image for the Flux LoRA- prompt[NODES['flux_lora_image']]["inputs"]["image"] = depth_path+ if controlnet_info and "depth" in controlnet_info:+ prompt[NODES['flux_lora_image']]["inputs"]["image"] = controlnet_info["depth"]['name']# Save prompt for debuggingself._save_prompt_to_file(prompt, revision_dir)@@ -2333,20 +2496,23 @@ class ComfyUIGenerate(bpy.types.Operator):# Return the refined imagereturn images[NODES['save_image']][0]- def _configure_refinement_mode_flux(self, prompt, context, render_path, mask_path, NODES):+ def _configure_refinement_mode_flux(self, prompt, context, render_info, mask_info, NODES):"""Configures the prompt based on the specific refinement mode for Flux."""# Configure based on generation methodif context.scene.generation_method == 'refine':# Configure for refine mode - load render directly- prompt[NODES['render_image']]["inputs"]["image"] = render_path- prompt[NODES['vae_encode']]["inputs"]["pixels"] = [NODES['render_image'], 0]+ 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 samplerprompt[NODES['sampler']]["inputs"]["latent_image"] = [NODES['vae_encode'], 0]elif context.scene.generation_method in ['uv_inpaint', 'sequential']:# Configure for inpainting modes- prompt[NODES['mask_image']]["inputs"]["image"] = mask_path- prompt[NODES['input_image']]["inputs"]["image"] = render_path+ 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 processingif not context.scene.blur_mask:@@ -2382,11 +2548,11 @@ class ComfyUIGenerate(bpy.types.Operator):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_path, NODES)+ self._configure_uv_inpainting_mode_flux(prompt, context, render_info, NODES)else: # Sequential modeself._configure_sequential_mode_flux(prompt, context, NODES)- def _configure_uv_inpainting_mode_flux(self, prompt, context, render_path, 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 configurationprompt[NODES['upscale_uv']]["inputs"]["image"] = [NODES['input_image'], 0]@@ -2402,11 +2568,12 @@ class ComfyUIGenerate(bpy.types.Operator):prompt[NODES['pos_prompt']]["inputs"]["text"] = uv_prompt# Object-specific prompt if available- current_object_name = os.path.basename(render_path).split('.')[0]- object_prompt = self._object_prompts.get(current_object_name, context.scene.comfyui_prompt)- 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+ if render_info and 'name' in render_info:+ current_object_name = os.path.basename(render_info['name']).split('.')[0]+ object_prompt = self._object_prompts.get(current_object_name, context.scene.comfyui_prompt)+ 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_promptdef _configure_sequential_mode_flux(self, prompt, context, NODES):"""Configures the prompt for sequential generation mode in Flux."""@@ -2416,24 +2583,22 @@ class ComfyUIGenerate(bpy.types.Operator):else:# Set the noise_mask flag according to context.scene.differential_noiseprompt[NODES['inpaint_conditioning']]["inputs"]["noise_mask"] = context.scene.differential_noise-- # Note: Flux doesn't support IPAdapter in the same way as SDXL- def _refine_configure_images_flux(self, prompt, depth_path, canny_path, normal_path, render_path, NODES):+ 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_path:- prompt[NODES['render_image']]["inputs"]["image"] = render_path+ 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, prompt, context, depth_path, canny_path, normal_path, NODES):+ def _refine_build_controlnet_chain_flux(self, prompt, context, 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 nodeprompt, final_node = self._build_controlnet_chain_extended(context, prompt, input, input, NODES['vae_loader'],- {"depth": depth_path, "canny": canny_path, "normal": normal_path}+ controlnet_info)# Connect final node to FluxGuidance conditioning inputprompt[NODES['flux_guidance']]["inputs"]["conditioning"] = [final_node, 0]diff --git a/stablegen/stablegen.py b/stablegen/stablegen.pyindex 3e33236..7035e12 100755--- a/stablegen/stablegen.py+++ b/stablegen/stablegen.py@@ -209,16 +209,24 @@ class StableGenPanel(bpy.types.Panel):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"action_row = layout.row()action_row.scale_y = 2.0 # Scale the row verticallyif config_error_message:- action_row.operator("object.test_stable", text="Cannot generate: " + config_error_message, icon="X")- action_row.enabled = False+ # 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 = Falseelse: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@@ -252,8 +260,10 @@ class StableGenPanel(bpy.types.Panel):overall_progress_factor = (current_image_idx + current_image_decimal_progress) / total_images if total_images > 0 else 0overall_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_image_idx + 1}/{total_images}",+ text=f"Overall: Image {current_img}/{total_images}",factor=overall_progress_factor_clamped # Ensure factor is <= 1.0 (logic maintained))@@ -265,7 +275,7 @@ class StableGenPanel(bpy.types.Panel):bake_row = layout.row()if config_error_message:- bake_row.operator("object.bake_textures", text="Cannot Bake: " + config_error_message, icon="X")+ bake_row.operator("object.bake_textures", text="Cannot Bake: " + config_error_message, icon="ERROR")bake_row.enabled = Falseelse:bake_row.operator("object.bake_textures", text="Bake Textures", icon="RENDER_STILL")@@ -324,7 +334,9 @@ class StableGenPanel(bpy.types.Panel):# Split for model namesplit = params_container.split(factor=0.25)split.label(text="Checkpoint:")- split.prop(scene, "model_name", text="")+ row = split.row(align=True)+ row.prop(scene, "model_name", text="")+ row.operator("stablegen.refresh_checkpoint_list", text="", icon='FILE_REFRESH')# Split for model architecture@@ -342,7 +354,7 @@ class StableGenPanel(bpy.types.Panel):split.label(text="Target Objects:")split.prop(scene, "texture_objects", text="")- # --- Helper to create collapsible sections ---+ # Helper to create collapsible sectionsdef 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))@@ -409,14 +421,6 @@ class StableGenPanel(bpy.types.Panel):row.alignment = 'CENTER'row.label(text="LoRA Units", icon="BRUSHES_ALL") # Using decimate icon for LoRA- # Get ComfyUI directory and construct the specific path for LoRAs- comfyui_dir = addon_prefs.comfyui_dir- actual_loras_path = ""- if comfyui_dir and os.path.isdir(comfyui_dir):- actual_loras_path = os.path.join(comfyui_dir, "models", "loras")-- loras_path_is_set_and_valid = bool(actual_loras_path) and os.path.isdir(actual_loras_path)-if scene.lora_units:for i, lora_unit in enumerate(scene.lora_units):is_selected_lora = (scene.lora_units_index == i)@@ -442,9 +446,6 @@ class StableGenPanel(bpy.types.Panel):# Only one button if no LoRA units are presentbutton_text = "Add LoRA Unit" # Default text- if not loras_path_is_set_and_valid:- button_text = "Set ComfyUI Directory in Preferences"-# Draw the operator with the dynamically determined textbtn_row_lora.operator("stablegen.add_lora_unit", text=button_text, icon="ADD")# The enabled state (greying out) will be handled by AddLoRAUnit.poll()