Skip to content

Commit d28b39d

Browse files
Add a LatentCut node to cut latents. (Comfy-Org#9609)
1 parent 1c184c2 commit d28b39d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

comfy_extras/nodes_latent.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import comfy.utils
22
import comfy_extras.nodes_post_processing
33
import torch
4+
import nodes
45

56

67
def reshape_latent_to(target_shape, latent, repeat_batch=True):
@@ -137,6 +138,41 @@ def op(self, samples1, samples2, dim):
137138
samples_out["samples"] = torch.cat(c, dim=dim)
138139
return (samples_out,)
139140

141+
class LatentCut:
142+
@classmethod
143+
def INPUT_TYPES(s):
144+
return {"required": {"samples": ("LATENT",),
145+
"dim": (["x", "y", "t"], ),
146+
"index": ("INT", {"default": 0, "min": -nodes.MAX_RESOLUTION, "max": nodes.MAX_RESOLUTION, "step": 1}),
147+
"amount": ("INT", {"default": 1, "min": 1, "max": nodes.MAX_RESOLUTION, "step": 1})}}
148+
149+
RETURN_TYPES = ("LATENT",)
150+
FUNCTION = "op"
151+
152+
CATEGORY = "latent/advanced"
153+
154+
def op(self, samples, dim, index, amount):
155+
samples_out = samples.copy()
156+
157+
s1 = samples["samples"]
158+
159+
if "x" in dim:
160+
dim = s1.ndim - 1
161+
elif "y" in dim:
162+
dim = s1.ndim - 2
163+
elif "t" in dim:
164+
dim = s1.ndim - 3
165+
166+
if index >= 0:
167+
index = min(index, s1.shape[dim] - 1)
168+
amount = min(s1.shape[dim] - index, amount)
169+
else:
170+
index = max(index, -s1.shape[dim])
171+
amount = min(-index, amount)
172+
173+
samples_out["samples"] = torch.narrow(s1, dim, index, amount)
174+
return (samples_out,)
175+
140176
class LatentBatch:
141177
@classmethod
142178
def INPUT_TYPES(s):
@@ -312,6 +348,7 @@ def sharpen(latent, **kwargs):
312348
"LatentMultiply": LatentMultiply,
313349
"LatentInterpolate": LatentInterpolate,
314350
"LatentConcat": LatentConcat,
351+
"LatentCut": LatentCut,
315352
"LatentBatch": LatentBatch,
316353
"LatentBatchSeedBehavior": LatentBatchSeedBehavior,
317354
"LatentApplyOperation": LatentApplyOperation,

0 commit comments

Comments
 (0)