Python Script
A safe little calculator in your graph
- A
- B
- C
- D
- A
- B
- C
- D
Every workflow eventually needs a calculation too awkward for a pile of math nodes - a conditional value, a clamped result, a sum of a list. Python Script is FairLab's answer: a text box where you write a limited Python expression, plus four optional inputs (A, B, C, D) and four outputs that carry up to four results. It's a safe little calculator that lives inside the graph, and it's the node that saves you from building a twenty-node arithmetic tower for a one-line formula.
The key word is safe, and the pack is serious about it. This is not a general "run any Python you want" node. The evaluator parses your text into an AST and walks it against a whitelist - only specific operators and a curated set of builtins (abs, bool, float, int, len, max, min, round, sorted, str, sum, tuple, list). exec and eval are impossible, imports are impossible, function definitions are impossible. If your expression uses anything off the list, you get a ValueError telling you the offending name.
Inputs and outputs
text(STRING, multiline) - your expression. The default isRESULT = (A, B, C, D), which just passes inputs through.A,B,C,D(*) - four optional inputs of any type, available to your expression by name.A,B,C,D(*) out - the four results. If your expression evaluates to a tuple, its elements fill the outputs in order; otherwise the first output carries the value and the rest areNone.
How it works
You write either a bare expression (A * B + 1) or an assignment (RESULT = ... - assignment to any other name is rejected). Supported syntax covers arithmetic, comparisons, boolean logic, conditionals (x if cond else y), indexing/slicing, tuples/lists/dicts, and calls to the whitelisted functions. The four inputs are injected as variables, so RESULT = (A + B, C - D) turns two wires into two outputs.
Where it fits
Any place a bit of logic beats a wall of nodes - computing a clamp, choosing a value with a conditional, summing a batch of numbers, formatting a label. It's the "script" escape hatch for the plumbing layer, and its safety means you can drop one into a shared workflow without turning it into a security hole.
Install
FairLab, one pack: ComfyUI Manager (search ComfyUI-FairLab) or:
cd ComfyUI/custom_nodes
git clone https://github.com/yanhuifair/ComfyUI-FairLab.git
cd ComfyUI-FairLab
pip install -r requirements.txt
Restart, then look under Fair/utility. No extra dependencies - it uses only Python's standard ast module.
Gotchas
- It's an expression evaluator, not a Python environment. No
forloops, nodef, noimport, no attribute access. Trying to do real scripting here fails loudly and correctly. - Outputs that don't fill from your tuple come back as
None, which can quietly become "unconnected-looking" sockets downstream - keep the tuple length matched to what you're using. - Because it's marked as an output node, it always runs. If you hand it a big input like a full image tensor, the expression had better not try to iterate it.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| textopt | STRING | RESULT = (A, B, C, D) | — |
| Aopt | * | — | |
| Bopt | * | — | |
| Copt | * | — | |
| Dopt | * | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| A | * | — |
| B | * | — |
| C | * | — |
| D | * | — |