Nodes/ComfyUI_Swwan/Insert Latent To Index (Swwan)
ComfyUI Node

Insert Latent To Index (Swwan)

The name is a lie — it replaces, it doesn't insert

By aining2022·Created 9 months ago·Updated 10 days ago· 33
Insert Latent To Index (Swwan)
  • source
  • destination
  • LATENT
index0

InsertLatentToIndex sounds like it splices one latent into the middle of a batch, shifting everything after it down. It doesn't. Read the source and it does the more destructive thing: it replaces the latent at the given index. The destination's item at index is dropped and the source latent takes its place, keeping the batch size identical.

That's worth knowing before you build a workflow on the assumption that inserting means growing the batch. If you actually want to grow the batch - append a latent into the middle and push the rest along - this isn't the node, and you'll need a concat-based approach instead. But if you want to swap a latent into a fixed position, this is exactly right and it's the tool the KJNodes latent family expects.

How it works

Three inputs, one output:

  • source (LATENT) - the latent(s) you're putting in
  • destination (LATENT) - the batch being modified
  • index (INT, default 0) - where the source lands

It works on the samples tensors: the output is destination[:index] + source + destination[index+1:]. It supports both 2D-image latents (shape B, C, H, W) and video latents (shape B, C, T, H, W, where the insert happens along the time axis). The source is moved to the destination's device and dtype automatically, which saves you the "tensor on wrong device" errors that plague hand-rolled concat graphs.

The one gotcha buried in the code: index must be 0 <= index < batch_size, and it raises a clear ValueError otherwise. The widget advertises a minimum of -1, but the implementation rejects negative indices - so don't try Python-style -1 to mean "last slot." It won't work.

Why you'd use it

This is the standard building block for latent reordering in a KJNodes-style pipeline: you sample a batch, want one specific frame or image regenerated and swapped back in at its original position, or you're splicing a freshly-sampled latent into a slot you've been keeping warm. Pair it with batch-splitting nodes to carve out the source and you can do "regenerate just frame 3" workflows without resampling the whole batch.

Installing it

The category says KJNodes, but this copy actually ships inside ComfyUI_Swwan - it's one of the nodes the pack migrated and re-homed. Install the pack via ComfyUI Manager (search "ComfyUI_Swwan") or:

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

Where people get burned

The naming trap is the big one - assume replacement semantics, not insertion semantics, or your batch silently ends up one latent shorter than your mental model. Also keep index strictly in-bounds; the error is clear when it fires, but it fires at runtime mid-batch. If you're feeding this from a node whose batch count can change between runs (a latent from a single-image path vs. a batch path), guard the index, because a latent batch of size 1 means any index above 0 throws immediately.

CategoryKJNodes/latents

Inputs (3)

NameTypeDefaultDescription
sourceLATENT
destinationLATENT
indexINT0-1–4096

Outputs (1)

NameTypeDescription
LATENTLATENT