Skip to content

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

custom lora support, lora chaining

sakalond7e63ff81a176
diff --git a/stablegen/__init__.py b/stablegen/__init__.py
index dcd5ab2..6fc6596 100755
--- a/stablegen/__init__.py
+++ b/stablegen/__init__.py
@@ -11,11 +11,11 @@ bl_info = {
"name": "StableGen",
"category": "Object",
"author": "Ondrej Sakala",
- "version": (0, 0, 1),
+ "version": (0, 0, 4),
'blender': (4, 2, 0)
}
-def update_model_dir(self, context): # Combined with load_handler to load controlnet unit on first setup
+def update_combined(self, context): # Combined with load_handler to load controlnet unit on first setup
update_parameters(self, context)
load_handler(None)
return None
@@ -31,7 +31,15 @@ class StableGenAddonPreferences(bpy.types.AddonPreferences):
description="Directory containing SD models",
default="",
subtype='DIR_PATH',
- update=update_model_dir,
+ update=update_combined
+ ) # type: ignore
+
+ lora_dir: bpy.props.StringProperty(
+ name="LoRA Directory",
+ description="Directory containing LoRA models",
+ default="",
+ subtype='DIR_PATH',
+ update=update_combined
) # type: ignore
server_address: bpy.props.StringProperty(
@@ -74,6 +82,8 @@ class StableGenAddonPreferences(bpy.types.AddonPreferences):
layout.prop(self, "model_dir")
layout.prop(self, "server_address")
layout.prop(self, "output_dir")
+ layout.separator()
+ layout.prop(self, "lora_dir")
layout.prop(self, "controlnet_mapping")
layout.prop(self, "save_blend_file")
@@ -152,6 +162,30 @@ class ControlNetUnit(bpy.types.PropertyGroup):
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 for a given type.
@@ -166,6 +200,21 @@ def get_controlnet_models(context, unit_type):
return []
return []
+def get_lora_models(self, context):
+ """
+ Populates the EnumProperty items with LoRA models from the lora_dir.
+ """
+ addon_prefs = context.preferences.addons[__package__].preferences
+ lora_dir = addon_prefs.lora_dir
+ loras = []
+ if os.path.isdir(lora_dir):
+ for f in os.listdir(lora_dir):
+ if f.endswith(('.safetensors')): # Common LoRA extensions
+ loras.append((f, f, ""))
+ if not loras:
+ loras.append(("NONE", "No LoRAs Found in Directory", "Please set the LoRA directory in preferences"))
+ return loras
+
class AddControlNetUnit(bpy.types.Operator):
bl_idname = "stablegen.add_controlnet_unit"
bl_label = "Add ControlNet Unit"
@@ -253,6 +302,112 @@ class RemoveControlNetUnit(bpy.types.Operator):
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[__package__].preferences
+ lora_dir = addon_prefs.lora_dir
+
+ if not lora_dir or not os.path.isdir(lora_dir):
+ cls.poll_message_set("LoRA directory not set or invalid in preferences.")
+ return False
+
+ # Get actual LoRA files, not the enum items which might include placeholders
+ available_lora_files = []
+ try:
+ for f in os.listdir(lora_dir):
+ if f.endswith(('.safetensors')):
+ available_lora_files.append(f)
+ except FileNotFoundError: # Should be caught by os.path.isdir, but good practice
+ cls.poll_message_set("LoRA directory not found.")
+ return False
+ except PermissionError:
+ cls.poll_message_set("Permission denied for LoRA directory.")
+ return False
+
+
+ num_available_loras = len(available_lora_files)
+ num_current_lora_units = len(scene.lora_units)
+
+ if num_available_loras == 0:
+ cls.poll_message_set("No LoRA model files found in the specified directory.")
+ return False
+
+ if num_current_lora_units >= num_available_loras:
+ cls.poll_message_set("All available LoRA models have been added.")
+ return False
+
+ return True
+
+ def execute(self, context):
+ loras = context.scene.lora_units
+ new_lora = loras.add()
+
+ # Attempt to set a default model if available from the actual files
+ lora_dir = context.preferences.addons[__package__].preferences.lora_dir
+ available_lora_files = []
+ if os.path.isdir(lora_dir):
+ for f in os.listdir(lora_dir):
+ if f.endswith(('.safetensors')):
+ available_lora_files.append(f)
+
+ if available_lora_files:
+ # Try to assign a LoRA that isn't already used, if possible,
+ # or just the first one if all are new.
+ current_lora_model_names = {unit.model_name for unit in loras if unit.model_name != "NONE"}
+ assigned = False
+ for lora_file_name in available_lora_files:
+ if lora_file_name not in current_lora_model_names:
+ try:
+ new_lora.model_name = lora_file_name
+ assigned = True
+ break
+ except TypeError: # Happens if the model_name enum is not yet updated with this specific file
+ pass
+ if not assigned: # Fallback to first available if all unique ones are taken or error
+ try:
+ new_lora.model_name = available_lora_files[0]
+ except TypeError: # Enum not ready
+ pass # It will pick the default "NONE" or whatever EnumProperty does
+
+ new_lora.model_strength = 1.0
+ new_lora.clip_strength = 1.0
+ context.scene.lora_units_index = len(loras) - 1 # Select the new unit
+ update_parameters(self, context) # Assuming this function exists and updates presets/UI state
+ for area in context.screen.areas: # Force UI refresh
+ 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)
+
+ 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'}
# load handler to set default ControlNet unit
@persistent
@@ -262,6 +417,19 @@ def load_handler(dummy):
if hasattr(scene, "controlnet_units") and not scene.controlnet_units:
default_unit = scene.controlnet_units.add()
default_unit.unit_type = 'depth'
+ # If possible, also set 'sdxl_lightning_8step_lora.safetensors'
+ if hasattr(scene, "lora_units") and not scene.lora_units:
+ # Need to check if the file exists in the directory
+ addon_prefs = bpy.context.preferences.addons[__package__].preferences
+ lora_dir = addon_prefs.lora_dir
+ if os.path.isdir(lora_dir):
+ for f in os.listdir(lora_dir):
+ if f == 'sdxl_lightning_8step_lora.safetensors':
+ new_lora = scene.lora_units.add()
+ new_lora.model_name = f
+ new_lora.model_strength = 1.0
+ new_lora.clip_strength = 1.0
+ break
def register():
"""
@@ -278,6 +446,7 @@ def register():
bpy.utils.register_class(ApplyModifiers)
bpy.utils.register_class(CurvesToMesh)
bpy.utils.register_class(ControlNetUnit)
+ bpy.utils.register_class(LoRAUnit)
bpy.utils.register_class(CameraPromptItem)
bpy.utils.register_class(CollectCameraPrompts)
bpy.types.Scene.comfyui_prompt = bpy.props.StringProperty(
@@ -364,12 +533,6 @@ def register():
default=False,
update=update_parameters
)
- bpy.types.Scene.show_depthmap_testing = bpy.props.BoolProperty(
- name="Show Depthmap Testing",
- description="Show or hide depthmap testing section",
- default=True,
- update=update_parameters
- )
bpy.types.Scene.show_generation_params = bpy.props.BoolProperty(
name="Show Generation Parameters",
description="Most important parameters",
@@ -765,21 +928,6 @@ def register():
min=1,
update=update_parameters
)
- bpy.types.Scene.lora_type = bpy.props.EnumProperty(
- name="LoRA Type",
- description="Type of LoRA to use",
- items=[
- ('hyper_1step', 'Hyper (1 step)', ''),
- ('hyper_4step', 'Hyper (4 steps)', ''),
- ('hyper_8step', 'Hyper (8 steps)', ''),
- ('lightning_2step', 'Lightning (2 steps)', ''),
- ('lightning_4step', 'Lightning (4 steps)', ''),
- ('lightning_8step', 'Lightning (8 steps)', ''),
- ('none', 'None', '')
- ],
- default='lightning_8step',
- update=update_parameters
- )
bpy.types.Scene.stablegen_preset = bpy.props.EnumProperty(
name="Preset",
description="Select a preset for easy mode",
@@ -828,6 +976,13 @@ def register():
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_scene_understanding_settings = bpy.props.BoolProperty(
name="Viewpoint Blending Settings",
description="Settings for how the addon blends different viewpoints together.",
@@ -878,7 +1033,11 @@ def register():
bpy.types.Scene.controlnet_units = bpy.props.CollectionProperty(type=ControlNetUnit)
bpy.utils.register_class(AddControlNetUnit)
bpy.utils.register_class(RemoveControlNetUnit)
+ bpy.types.Scene.lora_units = bpy.props.CollectionProperty(type=LoRAUnit)
+ bpy.utils.register_class(AddLoRAUnit)
+ bpy.utils.register_class(RemoveLoRAUnit)
bpy.types.Scene.controlnet_units_index = bpy.props.IntProperty(default=0)
+ bpy.types.Scene.lora_units_index = bpy.props.IntProperty(default=0)
bpy.utils.register_class(ApplyPreset)
bpy.utils.register_class(SavePreset)
bpy.utils.register_class(DeletePreset)
@@ -900,7 +1059,6 @@ def unregister():
del bpy.types.Scene.sampler
del bpy.types.Scene.scheduler
del bpy.types.Scene.show_advanced_params
- del bpy.types.Scene.show_depthmap_testing
del bpy.types.Scene.show_generation_params
del bpy.types.Scene.auto_rescale
del bpy.types.Scene.generation_method
@@ -928,6 +1086,8 @@ def unregister():
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
@@ -951,13 +1111,13 @@ def unregister():
del bpy.types.Scene.sequential_ipadapter_regenerate
del bpy.types.Scene.ipadapter_weight_type
del bpy.types.Scene.clip_skip
- del bpy.types.Scene.lora_type
del bpy.types.Scene.stablegen_preset
del bpy.types.Scene.model_architecture
del bpy.types.Scene.output_timestamp
del bpy.types.Scene.camera_prompts
del bpy.types.Scene.use_camera_prompts
del bpy.types.Scene.show_core_settings
+ del bpy.types.Scene.show_lora_settings
del bpy.types.Scene.show_scene_understanding_settings
del bpy.types.Scene.show_output_material_settings
del bpy.types.Scene.show_image_guidance_settings
@@ -978,6 +1138,9 @@ def unregister():
bpy.utils.unregister_class(ControlNetUnit)
bpy.utils.unregister_class(AddControlNetUnit)
bpy.utils.unregister_class(RemoveControlNetUnit)
+ bpy.utils.unregister_class(LoRAUnit)
+ bpy.utils.unregister_class(AddLoRAUnit)
+ bpy.utils.unregister_class(RemoveLoRAUnit)
bpy.utils.unregister_class(ExportOrbitGIF)
bpy.utils.unregister_class(CameraPromptItem)
bpy.utils.unregister_class(CollectCameraPrompts)
diff --git a/stablegen/blender_manifest.toml b/stablegen/blender_manifest.toml
index c9fcf48..ed2f030 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.2"
+version = "0.0.4"
name = "StableGen"
tagline = "AI-Powered Texturing"
maintainer = "Ondrej Sakala <[email protected]>"
diff --git a/stablegen/generator.py b/stablegen/generator.py
index 8f68799..527c8ad 100644
--- a/stablegen/generator.py
+++ b/stablegen/generator.py
@@ -662,6 +662,11 @@ class ComfyUIGenerate(bpy.types.Operator):
if context.scene.use_ipadapter or (context.scene.generation_method == 'separate' and context.scene.sequential_ipadapter and self._current_image > 0):
# Configure IPAdapter settings
self._configure_ipadapter(prompt, context, 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, depth_path, canny_path, normal_path, NODES)
@@ -710,7 +715,6 @@ class ComfyUIGenerate(bpy.types.Operator):
# Model Loading
'checkpoint': "6",
- 'lora': "26",
# Latent Space
'latent': "16",
@@ -719,6 +723,7 @@ class ComfyUIGenerate(bpy.types.Operator):
'save_image': "25",
# IPAdapter
+ 'ipadapter_loader': "235",
'ipadapter': "236",
'ipadapter_image': "237",
}
@@ -748,31 +753,34 @@ class ComfyUIGenerate(bpy.types.Operator):
# Set clip skip
prompt[NODES['clip_skip']]["inputs"]["stop_at_clip_layer"] = -context.scene.clip_skip
- # Set LoRA parameters
- self._configure_lora(prompt, context, NODES['lora'])
-
# Set the model name
prompt[NODES['checkpoint']]["inputs"]["ckpt_name"] = context.scene.model_name
-
- return prompt, NODES
- def _configure_lora(self, prompt, context, lora_node):
- """Configures the LoRA settings based on user selection."""
- lora_mapping = {
- 'hyper_8step': "Hyper-SDXL-8steps-lora.safetensors",
- 'lightning_8step': "sdxl_lightning_8step_lora.safetensors",
- 'hyper_4step': "Hyper-SDXL-4steps-lora.safetensors",
- 'lightning_4step': "sdxl_lightning_4step_lora.safetensors",
- 'hyper_1step': "Hyper-SDXL-1step-lora.safetensors",
- 'lightning_2step': "sdxl_lightning_2step_lora.safetensors"
- }
-
- if context.scene.lora_type in lora_mapping:
- prompt[lora_node]["inputs"]["lora_name"] = lora_mapping[context.scene.lora_type]
- else:
- # Disable LoRA if not selected
- prompt[lora_node]["inputs"]["strength_model"] = 0
- prompt[lora_node]["inputs"]["strength_clip"] = 0
+ # 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
+ if context.scene.use_ipadapter or (context.scene.generation_method == 'separate' and context.scene.sequential_ipadapter and self._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."""
@@ -817,10 +825,150 @@ 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):
+ """
+ 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.
+ image_dict (dict): A dictionary mapping ControlNet types (e.g., "depth",
+ "canny") to their corresponding image file paths.
+
+ 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
+ for idx, unit in enumerate(controlnet_units):
+ # 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": image_dict.get(unit.unit_type, ""),
+ "upload": "image"
+ },
+ "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"
+ else:
+ # 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):
+ """
+ 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.
+
+ 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)
+
+ prompt[lora_node_id_str] = {
+ "inputs": {
+ "lora_name": lora_unit.model_name,
+ "strength_model": lora_unit.model_strength,
+ "strength_clip": lora_unit.clip_strength,
+ "model": current_model_out, # Connect to previous LoRA or initial model
+ "clip": current_clip_out # Connect to previous LoRA or initial CLIP
+ },
+ "class_type": "LoraLoader",
+ "_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]
+ current_clip_out = [lora_node_id_str, 1]
+
+ return prompt, current_model_out, current_clip_out
+
def _build_controlnet_chain(self, prompt, context, depth_path, canny_path, normal_path, NODES):
"""Builds the ControlNet processing chain."""
# Build controlnet chain with guidance images
- prompt, final_node = build_controlnet_chain_extended(
+ prompt, 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}
)
@@ -945,6 +1093,11 @@ class ComfyUIGenerate(bpy.types.Operator):
if (context.scene.use_ipadapter or (context.scene.sequential_ipadapter and self._current_image > 0)) and context.scene.generation_method != 'uv_inpaint':
# Configure IPAdapter settings
self._configure_ipadapter_refine(prompt, context, 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, depth_path, canny_path, normal_path, render_path, NODES)
@@ -1003,7 +1156,6 @@ class ComfyUIGenerate(bpy.types.Operator):
# Model Loading
'checkpoint': "38",
- 'lora': "37",
# Image Processing
'upscale_grid': "118",
@@ -1025,6 +1177,7 @@ class ComfyUIGenerate(bpy.types.Operator):
# Advanced Features
'differential_diffusion': "229",
+ 'ipadapter_loader': "235",
'ipadapter': "236",
'ipadapter_image': "237",
@@ -1077,28 +1230,36 @@ class ComfyUIGenerate(bpy.types.Operator):
# Set the model name
prompt[NODES['checkpoint']]["inputs"]["ckpt_name"] = context.scene.model_name
- # Configure LoRA
- self._configure_lora_for_refinement(prompt, context, NODES['lora'])
-
- return prompt, NODES
+ # Build LoRA chain
+ initial_model_input_lora = [NODES['checkpoint'], 0]
+ initial_clip_input_lora = [NODES['checkpoint'], 1]
- def _configure_lora_for_refinement(self, prompt, context, lora_node):
- """Configures the LoRA settings for the refinement process."""
- lora_mapping = {
- 'hyper_8step': "Hyper-SDXL-8steps-lora.safetensors",
- 'lightning_8step': "sdxl_lightning_8step_lora.safetensors",
- 'hyper_4step': "Hyper-SDXL-4steps-lora.safetensors",
- 'lightning_4step': "sdxl_lightning_4step_lora.safetensors",
- 'hyper_1step': "Hyper-SDXL-1step-lora.safetensors",
- 'lightning_2step': "sdxl_lightning_2step_lora.safetensors"
- }
-
- if context.scene.lora_type in lora_mapping:
- prompt[lora_node]["inputs"]["lora_name"] = lora_mapping[context.scene.lora_type]
- else:
- # Disable LoRA if not selected
- prompt[lora_node]["inputs"]["strength_model"] = 0
- prompt[lora_node]["inputs"]["strength_clip"] = 0
+ 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
+ if (context.scene.use_ipadapter or (context.scene.sequential_ipadapter and self._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 not context.scene.generation_method == 'refine':
+ # 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_path, mask_path, NODES):
"""Configures the prompt based on the specific refinement mode."""
@@ -1227,7 +1388,7 @@ class ComfyUIGenerate(bpy.types.Operator):
vae_input = NODES['checkpoint']
# Build the ControlNet chain
- prompt, final = build_controlnet_chain_extended(
+ prompt, final = self._build_controlnet_chain_extended(
context, prompt, pos_input, neg_input, vae_input,
{"depth": depth_path, "canny": canny_path, "normal": normal_path}
)
@@ -1581,7 +1742,7 @@ class ComfyUIGenerate(bpy.types.Operator):
self._configure_resolution(prompt, context, NODES)
- prompt, final_node = build_controlnet_chain_extended(
+ 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}
)
@@ -1860,83 +2021,10 @@ class ComfyUIGenerate(bpy.types.Operator):
"""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 = build_controlnet_chain_extended(
+ prompt, final_node = self._build_controlnet_chain_extended(
context, prompt, input, input, NODES['vae_loader'],
{"depth": depth_path, "canny": canny_path, "normal": normal_path}
)
# Connect final node to FluxGuidance conditioning input
prompt[NODES['flux_guidance']]["inputs"]["conditioning"] = [final_node, 0]
return prompt
-
-import json # ensure json is imported
-
-def build_controlnet_chain_extended(context, base_prompt, pos_input, neg_input, vae_input, image_dict):
- 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
- for idx, unit in enumerate(controlnet_units):
- # 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": image_dict.get(unit.unit_type, ""),
- "upload": "image"
- },
- "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"
-
- return base_prompt, current_pos
\ No newline at end of file
diff --git a/stablegen/stablegen.py b/stablegen/stablegen.py
index 60db776..4705fdb 100755
--- a/stablegen/stablegen.py
+++ b/stablegen/stablegen.py
@@ -7,13 +7,13 @@ import math # pylint: disable=import-error
# 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.5, 0.5, 0.5)), "lora_type": "lightning_8step", "discard_factor": 90.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.7, "sequential_factor_smooth": 0.15, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": 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, "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}],},
- "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.5, 0.5, 0.5)), "lora_type": "lightning_8step", "discard_factor": 90.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.7, "sequential_factor_smooth": 0.15, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": 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, "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}],},
- "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.5, 0.5, 0.5)), "lora_type": "lightning_8step", "discard_factor": 80.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.7, "sequential_factor_smooth": 0.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, "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}],},
- "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.5, 0.5, 0.5)), "lora_type": "lightning_8step", "discard_factor": 80.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.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, "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}],},
- "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.5, 0.5, 0.5)), "lora_type": "hyper_4step", "discard_factor": 70.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "grid", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.7, "sequential_factor_smooth": 0.1, "sequential_ipadapter": True, "sequential_ipadapter_mode": "first", "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 1.0, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "differential_diffusion": True, "differential_noise": True, "blur_mask": True, "blur_mask_radius": 2, "blur_mask_sigma": 1.0, "grow_mask_by": 2, "canny_threshold_low": 0, "canny_threshold_high": 80, "controlnet_units": [{'unit_type': 'depth', 'model_name': 'controlnet_depth_sdxl.safetensors', 'strength': 0.5, 'start_percent': 0.0, 'end_percent': 1.0, 'is_union': False, 'use_union_type': True}],},
- "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.5, 0.5, 0.5)), "lora_type": "lightning_8step", "discard_factor": 80.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "uv_inpaint", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "3,0,1,2", "sequential_factor": 0.6000000238418579, "sequential_factor_smooth": 0.11000001430511475, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "first", "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 1.0, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "differential_diffusion": True, "differential_noise": True, "blur_mask": True, "blur_mask_radius": 3, "blur_mask_sigma": 1.0, "grow_mask_by": 3, "canny_threshold_low": 0, "canny_threshold_high": 80, },
- "ARCHITECTURE": {"description": "Prioritizes onlt the most straigh-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 picutre 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.5, 0.5, 0.5)), "lora_type": "lightning_8step", "discard_factor": 80.0, "weight_exponent": 10.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "separate", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": False, "sequential_custom_camera_order": "", "sequential_factor": 0.75, "sequential_factor_smooth": 0.15000000596046448, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "first", "ipadapter_weight_type": "style", "ipadapter_strength": 0.800000011920929, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "differential_diffusion": True, "differential_noise": False, "blur_mask": True, "blur_mask_radius": 3, "blur_mask_sigma": 1.0, "grow_mask_by": 3, "canny_threshold_low": 0, "canny_threshold_high": 80, "controlnet_units": [{'unit_type': 'depth', 'model_name': 'controlnet_depth_sdxl.safetensors', 'strength': 0.6000000238418579, 'start_percent': 0.0, 'end_percent': 1.0, 'is_union': False, 'use_union_type': True}],},
+ "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.5, 0.5, 0.5)), "discard_factor": 90.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.7, "sequential_factor_smooth": 0.15, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": 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, "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.5, 0.5, 0.5)), "discard_factor": 90.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.7, "sequential_factor_smooth": 0.15, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": 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, "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.5, 0.5, 0.5)), "discard_factor": 80.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.7, "sequential_factor_smooth": 0.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, "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.5, 0.5, 0.5)), "discard_factor": 80.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "sequential", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.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, "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.5, 0.5, 0.5)), "discard_factor": 70.0, "weight_exponent": 3.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "grid", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "", "sequential_factor": 0.7, "sequential_factor_smooth": 0.1, "sequential_ipadapter": True, "sequential_ipadapter_mode": "first", "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 1.0, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "differential_diffusion": True, "differential_noise": True, "blur_mask": True, "blur_mask_radius": 2, "blur_mask_sigma": 1.0, "grow_mask_by": 2, "canny_threshold_low": 0, "canny_threshold_high": 80, "controlnet_units": [{'unit_type': 'depth', 'model_name': 'controlnet_depth_sdxl.safetensors', 'strength': 0.5, 'start_percent': 0.0, 'end_percent': 1.0, 'is_union': False, 'use_union_type': True}], "lora_units": [{'model_name': 'Hyper-SDXL-4steps-lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]},
+ "UV INPAINTING": {"description": "Recommended UV Inpainting setup. It is recommended to bake texutures manually before running the generation to fine-tune unwrapping and avoid lag when generating.", "control_after_generate": "fixed", "model_architecture": "sdxl", "steps": 10, "cfg": 1.5, "sampler": "dpmpp_2s_ancestral", "scheduler": "sgm_uniform", "fallback_color": mathutils.Color((0.5, 0.5, 0.5)), "discard_factor": 80.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "uv_inpaint", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": True, "sequential_custom_camera_order": "3,0,1,2", "sequential_factor": 0.6000000238418579, "sequential_factor_smooth": 0.11000001430511475, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "first", "sequential_ipadapter_regenerate": False, "ipadapter_weight_type": "style", "ipadapter_strength": 1.0, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "differential_diffusion": True, "differential_noise": True, "blur_mask": True, "blur_mask_radius": 3, "blur_mask_sigma": 1.0, "grow_mask_by": 3, "canny_threshold_low": 0, "canny_threshold_high": 80, "lora_units": [{'model_name': 'sdxl_lightning_8step_lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]}, # No ControlNet for UV Inpainting by default
+ "ARCHITECTURE": {"description": "Prioritizes only the most straight-on camera for each point. This means details generated on flat surfaces will not get blurred by getting generated differently from two or more viewpoints. Does not use visibility masking. Each picture will get generated as new, consistency depends on IPAdapter + geometry.", "control_after_generate": "fixed", "model_architecture": "sdxl", "steps": 8, "cfg": 1.5, "sampler": "dpmpp_2s_ancestral", "scheduler": "sgm_uniform", "fallback_color": mathutils.Color((0.5, 0.5, 0.5)), "discard_factor": 80.0, "weight_exponent": 10.0, "clip_skip": 1, "auto_rescale": True, "overwrite_material": True, "bake_texture": False, "bake_texture_size": 2048, "bake_unwrap_method": "none", "bake_unwrap_overlap_only": True, "generation_method": "separate", "refine_images": False, "refine_steps": 8, "refine_sampler": "dpmpp_2s_ancestral", "refine_scheduler": "sgm_uniform", "denoise": 1.0, "refine_cfg": 1.5, "refine_prompt": "", "refine_upscale_method": "lanczos", "sequential_smooth": False, "sequential_custom_camera_order": "", "sequential_factor": 0.75, "sequential_factor_smooth": 0.15000000596046448, "sequential_factor_smooth_2": 1.0, "sequential_ipadapter": True, "sequential_ipadapter_mode": "first", "ipadapter_weight_type": "style", "ipadapter_strength": 0.800000011920929, "ipadapter_start": 0.0, "ipadapter_end": 1.0, "differential_diffusion": True, "differential_noise": False, "blur_mask": True, "blur_mask_radius": 3, "blur_mask_sigma": 1.0, "grow_mask_by": 3, "canny_threshold_low": 0, "canny_threshold_high": 80, "controlnet_units": [{'unit_type': 'depth', 'model_name': 'controlnet_depth_sdxl.safetensors', 'strength': 0.6000000238418579, 'start_percent': 0.0, 'end_percent': 1.0, 'is_union': False, 'use_union_type': True}], "lora_units": [{'model_name': 'sdxl_lightning_8step_lora.safetensors', 'model_strength': 1.0, 'clip_strength': 1.0}]},
}
# Global list of all generation parameter names to check for a preset.
@@ -25,7 +25,6 @@ GEN_PARAMETERS = [
"sampler",
"scheduler",
"fallback_color",
- "lora_type",
"discard_factor",
"weight_exponent",
"clip_skip",
@@ -119,10 +118,37 @@ def update_parameters(self, context):
if not units_match:
break
- if not units_match:
- continue # ControlNet units don't match, try next preset
+ 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 units match
+ # All parameters and ControlNet and LoRA units match
if scene.stablegen_preset != name:
scene.stablegen_preset = name
scene.active_preset = name
@@ -314,7 +340,7 @@ class StableGenPanel(bpy.types.Panel):
return None
core_settings_props = [
- "show_core_settings", "show_scene_understanding_settings",
+ "show_core_settings", "show_lora_settings", "show_scene_understanding_settings",
"show_output_material_settings", "show_image_guidance_settings",
"show_masking_inpainting_settings", "show_mode_specific_settings"
]
@@ -356,14 +382,66 @@ class StableGenPanel(bpy.types.Panel):
split = content_box.split(factor=0.5)
split.label(text="Scheduler:")
split.prop(scene, "scheduler", text="")
-
- split = content_box.split(factor=0.5)
- split.label(text="LoRa Type:")
- split.prop(scene, "lora_type", text="")
row = content_box.row()
row.prop(scene, "clip_skip", text="Clip Skip")
+ # --- LoRA Settings ---
+ content_box = draw_collapsible_section(advanced_params_box, "show_lora_settings", "LoRA Management", icon="MODIFIER")
+ if content_box:
+ row = content_box.row()
+ row.alignment = 'CENTER'
+ row.label(text="LoRA Units", icon="BRUSHES_ALL") # Using decimate icon for LoRA
+
+ lora_dir = addon_prefs.lora_dir
+ lora_dir_is_set_and_valid = lora_dir and os.path.isdir(lora_dir)
+
+ available_lora_files_count = 0
+ if lora_dir_is_set_and_valid:
+ try:
+ for f_name in os.listdir(lora_dir):
+ if f_name.endswith(('.safetensors')):
+ available_lora_files_count += 1
+ except Exception: # Catch potential permission errors, etc.
+ lora_dir_is_set_and_valid = False # Treat as invalid if unreadable
+
+ 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")
+ 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
+
+ if not lora_dir_is_set_and_valid:
+ button_text = "Set LoRA Directory in Preferences"
+ elif available_lora_files_count == 0:
+ button_text = "No LoRAs Found in Directory"
+
+ # 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")
# --- Image & Scene Understanding ---
content_box = draw_collapsible_section(advanced_params_box, "show_scene_understanding_settings", "Viewpoint Blending Settings", icon="ZOOM_IN")
@@ -412,7 +490,7 @@ class StableGenPanel(bpy.types.Panel):
content_box = draw_collapsible_section(advanced_params_box, "show_image_guidance_settings", "Image Guidance (IPAdapter & ControlNet)", icon="MODIFIER")
if content_box:
# IPAdapter Parameters
- if scene.model_architecture == 'sdxl':
+ if scene.model_architecture == 'sdxl' and not scene.generation_method == 'uv_inpaint':
ipadapter_main_box = content_box.box() # Group IPAdapter settings together
row = ipadapter_main_box.row()
row.prop(scene, "use_ipadapter", text="Use IPAdapter (External image)", toggle=True, icon="MOD_MULTIRES")
@@ -728,7 +806,7 @@ class ApplyPreset(bpy.types.Operator):
# Apply regular parameters
for key, value in values.items():
- if key not in ["controlnet_units", "description"] and hasattr(context.scene, key):
+ if key not in ["controlnet_units", "lora_units", "description"] and hasattr(context.scene, key):
setattr(context.scene, key, value)
# Apply ControlNet units if present in the preset
@@ -747,6 +825,22 @@ class ApplyPreset(bpy.types.Operator):
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'}
+
self.report({'INFO'}, f"Preset '{preset}' applied.")
else:
self.report({'INFO'}, "Custom preset active.")
@@ -773,7 +867,12 @@ class SavePreset(bpy.types.Operator):
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()
@@ -804,6 +903,18 @@ class SavePreset(bpy.types.Operator):
# 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
@@ -834,6 +945,7 @@ class SavePreset(bpy.types.Operator):
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"""
diff --git a/stablegen/util/helpers.py b/stablegen/util/helpers.py
index 110baf6..6685156 100755
--- a/stablegen/util/helpers.py
+++ b/stablegen/util/helpers.py
@@ -117,25 +117,6 @@ prompt_text = """
"title": "SaveImageWebsocket"
}
},
- "26": {
- "inputs": {
- "lora_name": "sdxl_lightning_8step_lora.safetensors",
- "strength_model": 1,
- "strength_clip": 1,
- "model": [
- "6",
- 0
- ],
- "clip": [
- "6",
- 1
- ]
- },
- "class_type": "LoraLoader",
- "_meta": {
- "title": "Load LoRA"
- }
- },
"27": {
"inputs": {
"image": "",
@@ -163,7 +144,7 @@ prompt_text = """
"inputs": {
"stop_at_clip_layer": -1,
"clip": [
- "26",
+ "",
1
]
},
@@ -176,7 +157,7 @@ prompt_text = """
"inputs": {
"preset": "PLUS (high strength)",
"model": [
- "26",
+ "",
0
]
},
@@ -290,25 +271,6 @@ prompt_text_img2img = """
"title": "Convert Image to Mask"
}
},
- "37": {
- "inputs": {
- "lora_name": "sdxl_lightning_8step_lora.safetensors",
- "strength_model": 1,
- "strength_clip": 1,
- "model": [
- "38",
- 0
- ],
- "clip": [
- "38",
- 1
- ]
- },
- "class_type": "LoraLoader",
- "_meta": {
- "title": "Load LoRA"
- }
- },
"38": {
"inputs": {
"ckpt_name": "realvisxlV40_v40Bakedvae.safetensors"
@@ -353,7 +315,7 @@ prompt_text_img2img = """
"scheduler": "sgm_uniform",
"denoise": 0.5,
"model": [
- "37",
+ "",
0
],
"positive": [
@@ -539,7 +501,7 @@ prompt_text_img2img = """
"229": {
"inputs": {
"model": [
- "37",
+ "",
0
]
},
@@ -552,7 +514,7 @@ prompt_text_img2img = """
"inputs": {
"preset": "PLUS (high strength)",
"model": [
- "37",
+ "",
0
]
},
@@ -608,7 +570,7 @@ prompt_text_img2img = """
"inputs": {
"stop_at_clip_layer": -1,
"clip": [
- "37",
+ "",
1
]
},