Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions comfy_extras/nodes_lora_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from __future__ import annotations

from typing_extensions import override

import comfy.sd
import comfy.utils
import folder_paths
from comfy_api.latest import ComfyExtension, io


def _load_lora_file(lora_name: str):
lora_path = folder_paths.get_full_path_or_raise("loras", lora_name)
return comfy.utils.load_torch_file(lora_path, safe_load=True, return_metadata=True)


def _lora_template() -> list[io.Input]:
return [
io.Combo.Input("lora_name", options=folder_paths.get_filename_list("loras"),
tooltip="The name of the LoRA file to apply."),
io.Float.Input("strength", default=1.0, min=-100.0, max=100.0, step=0.01,
tooltip="How strongly to apply this LoRA. 0 = off, negative inverts the effect."),
]


class LoadLoraModel(io.ComfyNode):
@classmethod
def define_schema(cls):
return io.Schema(
node_id="LoadLoraModel",
display_name="Load LoRA (Model)",
search_aliases=["lora", "load lora", "apply lora", "lora model", "lora stack"],
category="model/loaders",
description="Apply a stack of LoRAs to a diffusion model. Add one row per LoRA; "
"each row picks a LoRA file and its strength.",
inputs=[
io.Model.Input("model", tooltip="The diffusion model the LoRAs will be applied to."),
io.DynamicGroup.Input(
"loras",
template=_lora_template(),
min=1,
max=50,
tooltip="Each row applies one LoRA to the model.",
group_name="LoRA",
),
],
outputs=[io.Model.Output(tooltip="The modified diffusion model.")],
)

@classmethod
def execute(cls, model, loras: list[dict]) -> io.NodeOutput:
for row in loras:
lora_name = row.get("lora_name")
strength = row.get("strength", 1.0)
if not lora_name or lora_name == "none" or strength == 0:
continue
lora, metadata = _load_lora_file(lora_name)
model, _ = comfy.sd.load_lora_for_models(model, None, lora, strength, 0, lora_metadata=metadata)
Comment on lines +52 to +57

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Normalize None strength values before applying LoRAs.

DynamicGroup reconstruction can emit a row field with value None; when lora_name is present but strength is omitted, this passes None into load_lora_for_models.

🐛 Proposed fix
-            strength = row.get("strength", 1.0)
-            if not lora_name or lora_name == "none" or strength == 0:
+            strength = row.get("strength")
+            if not lora_name or lora_name == "none":
+                continue
+            if strength is None:
+                strength = 1.0
+            if strength == 0:
                 continue
@@
-            strength = row.get("strength", 1.0)
-            if not lora_name or lora_name == "none" or strength == 0:
+            strength = row.get("strength")
+            if not lora_name or lora_name == "none":
+                continue
+            if strength is None:
+                strength = 1.0
+            if strength == 0:
                 continue

Also applies to: 111-116

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@comfy_extras/nodes_lora_stack.py` around lines 75 - 80, Normalize the LoRA
row strength before calling load_lora_for_models in the LoRA stack
reconstruction path, since DynamicGroup rows can contain strength=None even when
lora_name is present. In the logic around lora_name/strength in
nodes_lora_stack.py, make sure omitted or None strength values fall back to the
default numeric strength (or are skipped consistently) before _load_lora_file
and comfy.sd.load_lora_for_models are invoked; apply the same change in the
duplicate reconstruction path referenced by the second occurrence.

return io.NodeOutput(model)


class LoadLoraTextEncoder(io.ComfyNode):
@classmethod
def define_schema(cls):
return io.Schema(
node_id="LoadLoraTextEncoder",
display_name="Load LoRA (Text Encoder)",
search_aliases=["lora", "load lora", "apply lora", "clip lora", "lora stack"],
category="model/loaders",
description="Apply a stack of LoRAs to a CLIP text encoder. Add one row per LoRA; "
"each row picks a LoRA file and its strength.",
inputs=[
io.Clip.Input("clip", tooltip="The CLIP text encoder the LoRAs will be applied to."),
io.DynamicGroup.Input(
"loras",
template=_lora_template(),
min=1,
max=50,
tooltip="Each row applies one LoRA to the text encoder.",
group_name="LoRA",
),
],
outputs=[io.Clip.Output(tooltip="The modified CLIP text encoder.")],
)

@classmethod
def execute(cls, clip, loras: list[dict]) -> io.NodeOutput:
for row in loras:
lora_name = row.get("lora_name")
strength = row.get("strength", 1.0)
if not lora_name or lora_name == "none" or strength == 0:
continue
lora, metadata = _load_lora_file(lora_name)
_, clip = comfy.sd.load_lora_for_models(None, clip, lora, 0, strength, lora_metadata=metadata)
return io.NodeOutput(clip)


class LoraStackExtension(ComfyExtension):
@override
async def get_node_list(self) -> list[type[io.ComfyNode]]:
return [
LoadLoraModel,
LoadLoraTextEncoder,
]


async def comfy_entrypoint() -> LoraStackExtension:
return LoraStackExtension()
1 change: 1 addition & 0 deletions nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2502,6 +2502,7 @@ async def init_builtin_extra_nodes():
"nodes_triposplat.py",
"nodes_depth_anything_3.py",
"nodes_seed.py",
"nodes_lora_stack.py",
]

import_failed = []
Expand Down
Loading