Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

Add ReferenceMultiLatent node. (#17009)

comfyanonymous6da3d1a5f512
diff --git a/comfy_extras/nodes_edit_model.py b/comfy_extras/nodes_edit_model.py
index d0d20ae7a..962aa1769 100644
--- a/comfy_extras/nodes_edit_model.py
+++ b/comfy_extras/nodes_edit_model.py
@@ -1,3 +1,6 @@
+import math
+
+import comfy.utils
import node_helpers
from typing_extensions import override
from comfy_api.latest import ComfyExtension, io
@@ -27,11 +30,69 @@ class ReferenceLatent(io.ComfyNode):
return io.NodeOutput(conditioning)
+class ReferenceMultiLatent(io.ComfyNode):
+ @classmethod
+ def define_schema(cls):
+ return io.Schema(
+ node_id="ReferenceMultiLatent",
+ display_name="Set Reference Multi Latent",
+ category="model/conditioning",
+ description="Encodes reference images and appends their latents to positive and optional negative conditioning for edit models.",
+ inputs=[
+ io.Conditioning.Input("positive"),
+ io.Conditioning.Input("negative", optional=True),
+ io.Vae.Input("vae", optional=True, tooltip="Required when reference images are connected."),
+ io.DynamicCombo.Input("resolution", options=[
+ io.DynamicCombo.Option("keep resolution", []),
+ io.DynamicCombo.Option("resize", [
+ io.Float.Input("megapixels", default=1.0, min=0.01, max=16.0, step=0.01,
+ tooltip="Target size of each reference image, preserving aspect ratio. 1.0 is about 1024 x 1024 pixels."),
+ ]),
+ ]),
+ io.Autogrow.Input("images", template=io.Autogrow.TemplatePrefix(
+ io.Image.Input("image"), prefix="image", min=1, max=100,
+ ), optional=True),
+ ],
+ outputs=[
+ io.Conditioning.Output(display_name="positive"),
+ io.Conditioning.Output(display_name="negative", tooltip="Returns the positive output when negative conditioning is not connected."),
+ io.Latent.Output(display_name="empty_latent", tooltip="First reference latent multiplied by 0.0."),
+ ],
+ )
+
+ @classmethod
+ def execute(cls, positive, resolution: io.DynamicCombo.Type, negative=None, vae=None, images: io.Autogrow.Type = None) -> io.NodeOutput:
+ ref_latents = []
+ images = images or {}
+ for name in sorted(images, key=lambda name: int(name.removeprefix("image"))):
+ image = images[name]
+ if image is None:
+ continue
+ if vae is None:
+ raise ValueError("Connect a VAE to encode reference images.")
+ if resolution["resolution"] == "resize":
+ height, width = image.shape[1:3]
+ scale = math.sqrt(resolution["megapixels"] * 1024 * 1024 / (width * height))
+ width = max(1, round(width * scale))
+ height = max(1, round(height * scale))
+ image = comfy.utils.common_upscale(image.movedim(-1, 1), width, height, "lanczos", "disabled").movedim(1, -1)
+ ref_latents.append(vae.encode(image))
+
+ empty_latent = None
+ if ref_latents:
+ positive = node_helpers.conditioning_set_values(positive, {"reference_latents": ref_latents}, append=True)
+ if negative is not None:
+ negative = node_helpers.conditioning_set_values(negative, {"reference_latents": ref_latents}, append=True)
+ empty_latent = {"samples": ref_latents[0] * 0.0}
+ return io.NodeOutput(positive, negative if negative is not None else positive, empty_latent)
+
+
class EditModelExtension(ComfyExtension):
@override
async def get_node_list(self) -> list[type[io.ComfyNode]]:
return [
ReferenceLatent,
+ ReferenceMultiLatent,
]