From 04856acc699f1559a11e00a9f68d2f9f9b9b8e96 Mon Sep 17 00:00:00 2001 From: drozbay <17261091+drozbay@users.noreply.github.com> Date: Fri, 15 May 2026 08:30:02 -0600 Subject: [PATCH] Allow negative `batch_index` on `ImageFromBatch` and `LatentFromBatch` (CORE-195) (#13857) --- comfy_extras/nodes_images.py | 6 ++++-- nodes.py | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/comfy_extras/nodes_images.py b/comfy_extras/nodes_images.py index 1ac740d1d..e48b2ea2d 100644 --- a/comfy_extras/nodes_images.py +++ b/comfy_extras/nodes_images.py @@ -136,7 +136,7 @@ class ImageFromBatch(IO.ComfyNode): category="image/batch", inputs=[ IO.Image.Input("image"), - IO.Int.Input("batch_index", default=0, min=0, max=4095), + IO.Int.Input("batch_index", default=0, min=-MAX_RESOLUTION, max=MAX_RESOLUTION), IO.Int.Input("length", default=1, min=1, max=4096), ], outputs=[IO.Image.Output()], @@ -145,7 +145,9 @@ class ImageFromBatch(IO.ComfyNode): @classmethod def execute(cls, image, batch_index, length) -> IO.NodeOutput: s_in = image - batch_index = min(s_in.shape[0] - 1, batch_index) + if batch_index < 0: + batch_index += s_in.shape[0] + batch_index = max(0, min(s_in.shape[0] - 1, batch_index)) length = min(s_in.shape[0] - batch_index, length) s = s_in[batch_index:batch_index + length].clone() return IO.NodeOutput(s) diff --git a/nodes.py b/nodes.py index 991238fb8..a59e8ebde 100644 --- a/nodes.py +++ b/nodes.py @@ -1221,7 +1221,7 @@ class LatentFromBatch: @classmethod def INPUT_TYPES(s): return {"required": { "samples": ("LATENT",), - "batch_index": ("INT", {"default": 0, "min": 0, "max": 63}), + "batch_index": ("INT", {"default": 0, "min": -MAX_RESOLUTION, "max": MAX_RESOLUTION}), "length": ("INT", {"default": 1, "min": 1, "max": 64}), }} RETURN_TYPES = ("LATENT",) @@ -1232,7 +1232,9 @@ class LatentFromBatch: def frombatch(self, samples, batch_index, length): s = samples.copy() s_in = samples["samples"] - batch_index = min(s_in.shape[0] - 1, batch_index) + if batch_index < 0: + batch_index += s_in.shape[0] + batch_index = max(0, min(s_in.shape[0] - 1, batch_index)) length = min(s_in.shape[0] - batch_index, length) s["samples"] = s_in[batch_index:batch_index + length].clone() if "noise_mask" in samples: