Math Expression (mtb)
Evaluate a small arithmetic string in-graph
- result (float)
- result (int)
Type a math expression as text, get the number back. This is the node for computing a value inside your graph instead of hand-calculating it and pasting a magic number that breaks the moment anything upstream changes. Derive a frame count from a duration, a target dimension from an aspect ratio, a strength from some other parameter - that kind of thing.
Set expectations up front, though, because this is a deliberately simple version. The description is refreshingly blunt: it "only supports literal_eval." That means it leans on Python's restrictive literal evaluator rather than a full expression engine - it's built for small, safe arithmetic, not a scientific calculator. If you need variables wired in as operands, trig functions, or complex conditional logic, the math-expression nodes in packs like Derfuu, efficiency-nodes, or rgthree are more capable. Reach for this one when you want something lightweight that's already in the pack.
How it works
It parses the string you give it and evaluates it safely - no arbitrary code execution, which is the upside of the literal_eval approach. It then returns the result twice, in two types, so you can wire it into whatever the next node expects.
The input and outputs
expression- a multi-line text field. You type the arithmetic here as a string, e.g.1920 * 0.5.
Two outputs: result (float) and result (int). Same computed value, offered as both a FLOAT and an INT - grab the float for a scale factor, the int for something that needs a whole number like a frame count or a dimension. The int is the value coerced to a whole number, so factor in that fractional results get truncated when you take the int output.
Note what the node schema doesn't have: numbered operand inputs. There are no a/b/c sockets on this node, so it evaluates the literal text you type. To make it dynamic - computing off a value from elsewhere in the graph - you assemble the expression string upstream (concatenating your number into the text) and feed that in.
How to install it
ComfyUI Manager: search MTB Nodes (comfy_mtb), install, restart. Or manually: cd ComfyUI/custom_nodes && git clone https://github.com/melMass/comfy_mtb and restart. Pure Python - nothing to download.
Common issues
The main surprise is hitting the literal_eval ceiling. Basic arithmetic on numbers works; anything fancier - function calls, some operator combinations, variable names - may simply not evaluate, and you'll get an error or nothing useful. If your expression won't run, simplify it, or move to a fuller math node from another pack. This isn't the node to fight; it's the node to use when your math is genuinely simple.
The second thing is the truncation on the int output. If you compute 10 / 3 and read the int, you get 3, not a rounded 3.33. When rounding behavior matters, take the float output and round it explicitly downstream, or structure the expression so it lands on a whole number to begin with.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| expression | STRING | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| result (float) | FLOAT | — |
| result (int) | INT | — |