Nodes/KJNodes for ComfyUI/Get Latent Range From Batch
ComfyUI Node Runs on cloud

Get Latent Range From Batch

Slice a sub-range out of a latent batch

By kijai·Created 3 years ago·Updated about 6 hours ago· 2,930
Get Latent Range From Batch
  • latents
  • LATENT
start_index0
num_frames1

When you're working with video in latent space, you often want just a slice - the first few frames, the middle chunk, everything from frame 20 on. GetLatentRangeFromBatch does that in the latent domain: hand it a latent batch, a start index, and a count, and it returns that sub-range. It's the latent-space cousin of image-batch slicing, and it pairs naturally with its sibling InsertLatentToIndexed for the "pull a range out, mess with it, put it back" pattern.

It's part of the latents family in Kijai's KJNodes - the batch-surgery nodes that make video and multi-image workflows bearable. Core ComfyUI gives you a couple of latent batch nodes; KJNodes fills in the ones you actually keep reaching for.

How it works

A latent batch is a stack indexed along the batch (for video, temporal) dimension. This node returns a contiguous window of that stack - num_frames latents starting at start_index - as a new latent. Nothing is modified; you get a copy of the slice. The negative-index support is the nice touch: it lets you say "from the end" without knowing the exact length.

The inputs and outputs that matter

  • latents (LATENT) - the batch to slice.
  • start_index (INT, default 0, min -1) - where the slice begins. 0 is the first latent; negative counts from the end.
  • num_frames (INT, default 1, min -1) - how many to take. The -1 sentinel is the useful one: it means "the rest," so start_index = 10, num_frames = -1 gives you everything from index 10 onward.

The single output is a LATENT - the sliced range. Wire it into another sampler pass, a decode, or an insert node.

How to install it

ComfyUI Manager: search KJNodes for ComfyUI, install, restart. Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/kijai/ComfyUI-KJNodes
pip install -r ComfyUI-KJNodes/requirements.txt

then restart (portable: run pip via python_embeded\python.exe). No downloads.

Common issues & troubleshooting

Latent frames aren't decoded frames. This is the big one, and it bites everybody doing video. Modern video VAEs compress time - Wan's, for instance, squeezes roughly 4 frames of video into 1 latent (the "4n+1" thing). So slicing 8 latents does not give you 8 output frames; it gives you a chunk that decodes to a different, larger number. If your slice comes out a weird length after decode, this compression is why. Plan your indices in latent units, not video-frame units.

Off-by-one and empty slices. Indices are 0-based. Asking for a range that starts past the end, or a num_frames that overshoots, gives you an empty or truncated latent - which then errors somewhere downstream that looks unrelated. Sanity-check the batch length first.

You wanted images, not latents. If your goal is to grab decoded frames, slice after decode with the image-batch version instead. Do this node's work in latent space only when you're staying in latent space - e.g. resampling just part of a clip.

Using it to trim, then re-insert. That's the intended combo with InsertLatentToIndexed. Keep your indices consistent between the two, and remember the temporal-compression caveat applies on both ends.

CategoryKJNodes/latents

Inputs (3)

NameTypeDefaultDescription
latentsLATENT
start_indexINT0-1–4096
num_framesINT1-1–4096

Outputs (1)

NameTypeDescription
LATENTLATENT