Tensor Slice
Tensor Slice — cutting out the parts you actually want
- tensor
- *
Tensor Slice extracts a chunk of a tensor - one frame out of a batch, the first 10 channels, a single row - using a Python-style slice string. Where the pack's Reshape and Permute rearrange data, this one just takes a piece. If you've ever wanted to say "give me the first image in this batch" or "drop everything after frame 30," this is the node.
It's part of Basic data handling by StableLlama, a dependency-free utility pack that wraps everyday Python and PyTorch into ComfyUI nodes. The tensor corner's answer to "how do I grab a subset?"
How it works
The slice string is comma-separated: one slice expression per dimension, written exactly like Python indexing. For a [batch, height, width, channels] image, 0:1, :, :, : grabs the first image of the batch; :, :, 0:512, : keeps only the top 512 rows of every image.
Each dimension's expression supports:
:- everything along that dim5- the single index 5 (this drops that dimension, Python-style - tensor[5] on a 4D tensor gives you a 3D tensor)0:10- range from 0 to 10 (exclusive)::2- step every other element-1- the last element (negative indices count from the end)
The default : alone is the identity - it returns the whole tensor, which is a handy no-op to start from.
The inputs that matter
Two inputs:
tensor(wildcard*) - what you're slicing.slice_str(STRING, default:) - the per-dimension slice expressions, comma-separated.
One wildcard output - the extracted chunk.
Where you'd actually use it
In practice it's batch and sequence surgery. Take the first N frames of a video batch before feeding them to a node that can't handle the full length. Pull a single image out of a batched result to inspect it. Grab one channel of a multi-channel tensor. It's the node you drop in when "everything works but I only need part of it."
Installing it
# ComfyUI Manager (recommended): search "Basic data handling", install, restart.
# or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/StableLlama/ComfyUI-basic_data_handling
# restart ComfyUI
No dependencies beyond torch, no model downloads.
Where people get burned
One expression per dimension, or the parser does something weird. The node splits on commas, so :, 0:10 means "all of dim 0, indices 0–10 of dim 1" - not "all of dim 0 and a bit of dim 0." Get the comma count wrong and you're slicing the wrong axis.
Python's "single index drops the dimension" quirk. tensor[0] on a [4, 512, 512, 3] image doesn't give you a [1, 512, 512, 3] batch - it gives you a [512, 512, 3] image with the batch axis gone. If a downstream node insists on 4D input, use 0:1 instead of 0. This catches people constantly, because the node follows Python semantics faithfully and never warns you about it.
It's basic slicing, not fancy indexing. The parser handles integers, ranges, steps, and negatives - it won't do string indices or lists of indices. That's by design ("manual parsing is safer," per the source), so don't expect NumPy-grade indexing magic.
An empty or malformed expression errors at runtime with a message naming the slice string - re-read it, count your commas, remember ranges are end-exclusive (0:10 is elements 0–9).
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| tensor | * | — | |
| slice_str | STRING | : | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| * | * | — |