Nodes/ComfyUI-Magic-Assistant/🧠 可自己编辑算法的逻辑计算 (带教程版) Magic Programmable Logic & Calc (Tutorial)
ComfyUI Node

🧠 可自己编辑算法的逻辑计算 (带教程版) Magic Programmable Logic & Calc (Tutorial)

A math node that lets you write IF statements instead of wiring five nodes

By shigjfg·Created 8 months ago·Updated 11 days ago· 37
🧠 可自己编辑算法的逻辑计算 (带教程版) Magic Programmable Logic & Calc (Tutorial)
  • image
  • latent
  • result_1
  • result_2
  • ratio
  • bool
operation
a0
b2

ComfyUI is a programming language with no keyboard. You want "if the image is wider than 1024, cap it, otherwise keep it" and the normal way is to assemble that from Math nodes, Compare nodes, and switches like some kind of wire-based origami. This node lets you type the thought instead.

MagicLogicCompute runs a tiny pseudo-code script - the pack calls it "Magic Script" - inside a single node. The headline example from the README is IF w > 1024 RETURN 1024, h: read the width, and if it's over 1024, hand back 1024 and the height unchanged. That's a conditional resize decision expressed in one line instead of a rat's nest of plumbing. For the readers of this site's node-plumbing essay, this is the "graph as a small program" idea taken literally.

What the script can see

The node auto-detects the dimensions of whatever you feed it. Connect an image and w/h become its pixel width and height; connect a latent instead and it reads the latent's dimensions multiplied by 8 (the standard VAE downscale), which is the right thing for latent-space math. Feed neither and w/h fall back to your a and b inputs. The environment also exposes a, b, plus abs, min, max, round, and math, so you can do real arithmetic in the expressions.

The script itself is line-based. IF <condition> RETURN <results> runs the condition and, when true, returns. A plain RETURN line is the fallback. You can return one, two, or three values - one value becomes both results, three lets you force the boolean.

The inputs and outputs that matter

The dropdown operation picks which saved script runs (Max, Min, Scale, Divide, and a "自定义测试" custom one ship by default - the actual list comes from the config file, and the tutorial version of the node walks you through writing your own). Then image, latent, a, and b are the values the script sees.

The four outputs cover the use cases:

  • result_1 and result_2 - the two numbers your script returned, as INT
  • ratio - result_1 / result_2 computed for you, so a "divide" of a 1536×1024 image gives you the 1.5 aspect ratio without a second node
  • bool - a BOOLEAN from your IF, which you can feed straight into a switch node or a gated branch

That's the real payoff of this thing: one node gives you a compare, a math op, a ratio, and a boolean simultaneously. You can wire the boolean to mute a branch while the integers drive a resize - four decisions from one place.

What to watch for

  • It's a script interpreter, not Python. eval-based under the hood, so keep to the documented syntax. There's a tutorial mode for a reason - lean on it before writing exotic logic.
  • b can't be zero - the node's own range enforces min: 1, because the ratio output divides by it. The division-by-zero protection exists in the code too, but you'll never hit it.
  • The boolean semantics of a bare RETURN are "fallback" - if an IF above it didn't fire, the bool comes back False. If you want True, write RETURN w, h, True explicitly.

Installing

It ships in the ComfyUI-Magic-Assistant pack:

cd ComfyUI/custom_nodes/
git clone https://github.com/shigjfg/ComfyUI-Magic-Assistant.git
cd ComfyUI-Magic-Assistant && pip install -r requirements.txt

Restart and search for it under the "✨ Magic Assistant" category. The dependencies are just the pack's usual openai + aiohttp, nothing GPU-related - this is a pure logic node, so it works on any install.

Category✨ Magic Assistant

Inputs (5)

NameTypeDefaultDescription
operationCOMBO5 options: ⚖️ 取大值 (Max), ⚖️ 取小值 (Min), ✖️ 乘法 (Scale), ➗ 除法 (Divide), 1️⃣ 自定义测试
imageoptIMAGE
latentoptLATENT
aoptINT00–999999
boptINT21–999999

Outputs (4)

NameTypeDescription
result_1INT
result_2INT
ratioFLOAT
boolBOOLEAN