|
1 | 1 | import comfy.utils |
2 | 2 | import comfy_extras.nodes_post_processing |
3 | 3 | import torch |
| 4 | +import nodes |
4 | 5 |
|
5 | 6 |
|
6 | 7 | def reshape_latent_to(target_shape, latent, repeat_batch=True): |
@@ -137,6 +138,41 @@ def op(self, samples1, samples2, dim): |
137 | 138 | samples_out["samples"] = torch.cat(c, dim=dim) |
138 | 139 | return (samples_out,) |
139 | 140 |
|
| 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 | + |
140 | 176 | class LatentBatch: |
141 | 177 | @classmethod |
142 | 178 | def INPUT_TYPES(s): |
@@ -312,6 +348,7 @@ def sharpen(latent, **kwargs): |
312 | 348 | "LatentMultiply": LatentMultiply, |
313 | 349 | "LatentInterpolate": LatentInterpolate, |
314 | 350 | "LatentConcat": LatentConcat, |
| 351 | + "LatentCut": LatentCut, |
315 | 352 | "LatentBatch": LatentBatch, |
316 | 353 | "LatentBatchSeedBehavior": LatentBatchSeedBehavior, |
317 | 354 | "LatentApplyOperation": LatentApplyOperation, |
|
0 commit comments