🧮AGSoft Math Expression
Math in your graph, written as actual math — not five primitive nodes
- a
- b
- c
- d
- int
- float
- text
ComfyUI's stock math situation is a daisy chain: add a node, multiply a node, and by the time you've computed (a - b) / c your graph looks like spaghetti. AGSoftMathExpression is the "just type it" answer - you write sqrt(a**2 + b**2) in a box and it computes it, feeding in whatever values you connect. It's the pack's version of a calculator node, and it's genuinely nicer than the alternative for anything past a + b.
How it works
The expression is evaluated with Python's eval() against a restricted globals dictionary - the source explicitly builds a safe dict of allowed functions and constants, so you're not getting arbitrary code execution. You get four variables, a, b, c, d, each an optional input that accepts any type (int, float, or whatever you feed it - the node coerces). The expression field (default "a + b") supports:
- Operators:
+ - * / ** % // - Functions:
abs, round, min, max, pow, sqrt, sin, cos, tan(the source also exposesasin, acos, atan, log, log10, exp) - Constants:
pi,e,tau,inf
The tooltip even gives the classic trig usage: sin(pi * a / 180) - degrees to radians in one line.
The nice touch is the output. It returns three formats from one expression:
- int - the result rounded to an integer (for step counts, batch sizes, seeds).
- float - the precise result.
- text - the result as a string (for filename prefixes, prompt fragments, UI display).
So one node feeds a KSampler's step count and a filename and a label, without conversions.
What you'll actually type
- expression - the formula. Use the variables you connected; leave
a–dat 0 if you're not using them (they're all optional). - a / b / c / d - wire in numbers from other nodes. Because they accept
*any-type, you can feed a seed INT, a width FLOAT, whatever.
Where people get burned: division by zero and sqrt() of a negative just raise a normal Python error - there's no guard, so if your workflow can hit a = 0, clamp it upstream. And % and // are floor/modulo, not "percent" - a stray % in your expression is modulo, not "percent of," which surprises people once per session.
Installing
It's the standard AGSoft install:
cd ComfyUI/custom_nodes
git clone https://github.com/Art-xmaster/comfyui-AGSoft
Restart ComfyUI, or ComfyUI Manager → "comfyui-AGSoft". No models, no extra deps beyond the pack's usual numpy/opencv. The expression box has a multiline field off by default, so keep expressions on one line unless you're testing something fancy. For computing latent aspect ratios, batch counts, or a CFG that varies with step, this is the node that makes the graph readable again.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| expression | STRING | a + b | RU: Математическое выражение. EN: Mathematical expression. Доступные переменные: a, b, c, d Доступные операции: +, -, *, /, **, %, // Доступные функции: abs, round, min, max, pow, sqrt, sin, cos, tan Примеры: - a + b * 2 - (a - b) / c - sqrt(a**2 + b**2) - sin(pi * a / 180) - a + b + c + d |
| aopt | * | 0 | RU: Значение переменной a (опционально). EN: Value of variable a (optional). Принимает любые типы: INT, FLOAT и другие. Используется в выражении как 'a' |
| bopt | * | 0 | RU: Значение переменной b (опционально). EN: Value of variable b (optional). Принимает любые типы: INT, FLOAT и другие. Используется в выражении как 'b' |
| copt | * | 0 | RU: Значение переменной c (опционально). EN: Value of variable c (optional). Принимает любые типы: INT, FLOAT и другие. Используется в выражении как 'c' |
| dopt | * | 0 | RU: Значение переменной d (опционально). EN: Value of variable d (optional). Принимает любые типы: INT, FLOAT и другие. Используется в выражении как 'd' |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| int | INT | — |
| float | FLOAT | — |
| text | STRING | — |