Nodes/JNComfy/Slice Operation
ComfyUI Node

Slice Operation

Python-style slicing, without leaving the graph

By jn-jairo·Created 2 years ago·Updated 2 years ago· 5
Slice Operation
  • values
  • *
a0
b0
operation
only_activetrue
merge_array_itemstrue

You know how in Python you write my_list[3:7] and get items 3 through 6? JN_SliceOperation is that, as a node. It takes a pile of values, applies one of six slice operations defined by two integers a and b, and hands back the subset. If you've ever wanted "just the first 10 frames" or "everything after item 5" without writing a custom node, this is the tool.

How it works

The pipeline is the same as the pack's other array processors: values is a dynamic multiple input (connect things and new slots appear), merge_array_items flattens any arrays into one list, only_active drops None entries, and then the slice happens. The six operations are a dropdown on the node:

  • [a:b] - items from a up to (not including) b
  • [a:a+b] - b items starting at a
  • [a-b:a] - b items ending just before a
  • [a-b:a+b] - b items on each side of a
  • [a:] - everything from a to the end
  • [:a] - everything from the start up to (not including) a

These are real Python slice semantics, which means negative numbers work the way you'd hope: a=-3 with [:a]... actually [-3:] isn't in the list, but a=-3 in [a:] gives you the last three items, and [a:b] with negatives counts from the end. If a and b are both 0 you get an empty slice, so watch that default.

The output is a * that carries the sliced list, not a single item. That trips people up - a slice of a 12-item list is still a list.

Inputs that matter

  • a, b - the two integers. You can wire math nodes into these for dynamic ranges, e.g. "take the next N items" with N computed elsewhere.
  • operation - the six-way dropdown; this is what you'll actually fiddle with most.
  • only_active and merge_array_items - same semantics as JN_SelectItem: indexes count connected values only, and arrays get flattened first.
  • values - the dynamic input pile.

Where you'd use it

Anywhere you process batches or sequences. Grab the first N frames of a video workflow. Chop the leading junk off a batch of images. Take a middle window out of an array and re-slice it to iterate over it. Because a and b can be computed, it slots neatly next to JN_MathOperation for "give me a sliding window" patterns - compute the window start, feed it in as a, use [a:a+b] with a fixed b.

Install

It's part of JNComfy. ComfyUI Manager → search "JNComfy" → Install, restart. Or by hand:

cd ComfyUI/custom_nodes
git clone https://github.com/jn-jairo/jn_comfyui

restart, and run pip install -r requirements.txt inside the folder if you're not using Manager. The requirements list is big (audio, vision, transformers…) but this node needs none of it - pure Python list slicing.

Gotchas

  • The output is a list, not an item. If you want one thing out of the slice, chain it into JN_SelectItem.
  • Slicing happens after only_active filters, so the positions you address are positions among connected values, not raw slot numbers - same trap as its sibling nodes.
  • Default a and b are both 0, which yields an empty list on most operations. Type your numbers before you run, or you'll wonder where your data went.
CategoryJN/Primitive/Process

Inputs (6)

NameTypeDefaultDescription
aINT0-18446744073709550000–18446744073709550000
bINT0-18446744073709550000–18446744073709550000
operationCOMBO6 options: [a:b], [a:a+b], [a-b:a], [a-b:a+b], [a:], [:a]
only_activeBOOLEANtrue
merge_array_itemsBOOLEANtrue
valuesopt*

Outputs (1)

NameTypeDescription
**