Nodes/KJNodes for ComfyUI/Insert Latent To Index
ComfyUI Node Runs on cloud

Insert Latent To Index

Drop a latent into a batch at a chosen index

By kijai·Created 3 years ago·Updated a day ago· 2,908
Insert Latent To Index
  • source
  • destination
  • LATENT
index0

This is the "put it back" half of a very common latent workflow. You pulled a range of latents out of a batch, did something to them - resampled, swapped, edited - and now you need to place them back into the full sequence at exactly the right spot. InsertLatentToIndexed does that: as its description says, it "inserts a latent at the specified index into the original latent batch." Feed it your source latent, the destination batch, and the position, and it splices them together.

It's the natural partner to GetLatentRangeFromBatch, both from the latents family in Kijai's KJNodes. Together they give you the slice-edit-reinsert loop that video and multi-frame work leans on constantly - the latent-space equivalent of cutting a clip, tweaking it, and dropping it back on the timeline.

How it works

You provide two latents and an index. The source is what goes in; the destination is the batch it goes into; the index is where. It's a positional splice along the batch dimension. The negative-index support means you can target the end of the batch without knowing its exact length.

The inputs and outputs that matter

  • source (LATENT) - the latent(s) being inserted.
  • destination (LATENT) - the batch you're inserting into.
  • index (INT, default 0, min -1) - where the source lands. 0 is the front; negative counts from the end (-1 targets the last position).

The single output is a LATENT - the combined batch. Send it on to your sampler, decode, or another slice/insert step.

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

Shapes have to match. The source latent needs the same channel count and spatial dimensions as the destination. If you pulled a range out, resized or resampled at a different resolution, and try to reinsert, you'll get a shape mismatch. Keep the geometry consistent through the round trip.

Latent count isn't frame count. Same trap as everywhere in video-latent land: modern video VAEs compress time (Wan squeezes roughly 4 video frames into 1 latent, the "4n+1" pattern). So an index in latent space doesn't map 1:1 to a video frame. If your reinserted chunk lands at the wrong moment after decode, temporal compression is why - count in latents, not frames.

It went to the wrong place. Indices are 0-based, and negative means from the end. index = 0 inserts at the very front; if you meant "after the first frame," that's index 1. When in doubt, sanity-check by decoding a couple of frames around the target position.

Pairing with the slice node. If you're doing the classic get-range → edit → insert flow, keep the index you insert at consistent with where you pulled the range from, and apply the temporal-compression caveat on both ends. That's the usual reason a "put it back exactly where it was" ends up slightly off.

CategoryKJNodes/latents

Inputs (3)

NameTypeDefaultDescription
sourceLATENT
destinationLATENT
indexINT0-1–4096

Outputs (1)

NameTypeDescription
LATENTLATENT