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 a LatentCut node to cut latents. (#9609)

comfyanonymousd28b39d93dc4
diff --git a/comfy_extras/nodes_latent.py b/comfy_extras/nodes_latent.py
index 247d886a1..0f90cf60c 100644
--- a/comfy_extras/nodes_latent.py
+++ b/comfy_extras/nodes_latent.py
@@ -1,6 +1,7 @@
import comfy.utils
import comfy_extras.nodes_post_processing
import torch
+import nodes
def reshape_latent_to(target_shape, latent, repeat_batch=True):
@@ -137,6 +138,41 @@ class LatentConcat:
samples_out["samples"] = torch.cat(c, dim=dim)
return (samples_out,)
+class LatentCut:
+ @classmethod
+ def INPUT_TYPES(s):
+ return {"required": {"samples": ("LATENT",),
+ "dim": (["x", "y", "t"], ),
+ "index": ("INT", {"default": 0, "min": -nodes.MAX_RESOLUTION, "max": nodes.MAX_RESOLUTION, "step": 1}),
+ "amount": ("INT", {"default": 1, "min": 1, "max": nodes.MAX_RESOLUTION, "step": 1})}}
+
+ RETURN_TYPES = ("LATENT",)
+ FUNCTION = "op"
+
+ CATEGORY = "latent/advanced"
+
+ def op(self, samples, dim, index, amount):
+ samples_out = samples.copy()
+
+ s1 = samples["samples"]
+
+ if "x" in dim:
+ dim = s1.ndim - 1
+ elif "y" in dim:
+ dim = s1.ndim - 2
+ elif "t" in dim:
+ dim = s1.ndim - 3
+
+ if index >= 0:
+ index = min(index, s1.shape[dim] - 1)
+ amount = min(s1.shape[dim] - index, amount)
+ else:
+ index = max(index, -s1.shape[dim])
+ amount = min(-index, amount)
+
+ samples_out["samples"] = torch.narrow(s1, dim, index, amount)
+ return (samples_out,)
+
class LatentBatch:
@classmethod
def INPUT_TYPES(s):
@@ -312,6 +348,7 @@ NODE_CLASS_MAPPINGS = {
"LatentMultiply": LatentMultiply,
"LatentInterpolate": LatentInterpolate,
"LatentConcat": LatentConcat,
+ "LatentCut": LatentCut,
"LatentBatch": LatentBatch,
"LatentBatchSeedBehavior": LatentBatchSeedBehavior,
"LatentApplyOperation": LatentApplyOperation,