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.

Improve ReferenceMultiLatent UX a bit. (#17053)

comfyanonymouse61e1a21fb03
diff --git a/comfy_extras/nodes_edit_model.py b/comfy_extras/nodes_edit_model.py
index 962aa1769..80b957a11 100644
--- a/comfy_extras/nodes_edit_model.py
+++ b/comfy_extras/nodes_edit_model.py
@@ -4,6 +4,7 @@ import comfy.utils
import node_helpers
from typing_extensions import override
from comfy_api.latest import ComfyExtension, io
+from nodes import EmptyLatentImage
class ReferenceLatent(io.ComfyNode):
@@ -41,7 +42,7 @@ class ReferenceMultiLatent(io.ComfyNode):
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.Vae.Input("vae", optional=True, tooltip="Reference images are ignored when no VAE is connected."),
io.DynamicCombo.Input("resolution", options=[
io.DynamicCombo.Option("keep resolution", []),
io.DynamicCombo.Option("resize", [
@@ -50,40 +51,42 @@ class ReferenceMultiLatent(io.ComfyNode):
]),
]),
io.Autogrow.Input("images", template=io.Autogrow.TemplatePrefix(
- io.Image.Input("image"), prefix="image", min=1, max=100,
+ io.Image.Input("image", optional=True), 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."),
+ io.Latent.Output(display_name="empty_latent", tooltip="First reference latent multiplied by 0.0, or a square empty latent at the selected resolution."),
],
)
@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))
+ if vae is not None:
+ for name in sorted(images or {}, key=lambda name: int(name.removeprefix("image"))):
+ image = images[name]
+ if image is None:
+ continue
+ 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}
+ else:
+ size = 1024
+ if resolution["resolution"] == "resize":
+ size = round(math.sqrt(resolution["megapixels"] * 1024 * 1024))
+ empty_latent = EmptyLatentImage().generate(size, size)[0]
return io.NodeOutput(positive, negative if negative is not None else positive, empty_latent)