Nodes/SDVN_Comfy_node/πŸ‘¨πŸ»β€πŸ’» Run Python Code
ComfyUI Node

πŸ‘¨πŸ»β€πŸ’» Run Python Code

Drop a Python function straight into your workflow

By StableDiffusionVNΒ·Created 2 years agoΒ·Updated 29 days agoΒ· 118
πŸ‘¨πŸ»β€πŸ’» Run Python Code
  • input
  • input2
  • input3
  • output
β—„functiondef function(input_value, extra_value=None): "Example helper that trims text and optionally appends extra info." if input_value is None: return "No input provided" result = str(input_value).strip() if extra_value: result = f"{result} | extra: {extra_value}" return resultβ–Ί

This is the escape hatch. When you need a little logic that no existing node quite does - reformat a string, do some math on two numbers, transform a value before it hits the next node - instead of hunting for a custom node or chaining five utility nodes together, you just write the Python inline. Define a function, wire up to three inputs, get the return value out. For anyone comfortable with a few lines of code, it collapses a lot of node-graph gymnastics into one box.

What it does

The node hands you a text field pre-filled with a def function(...) template. You edit the body to do whatever you need, and it runs that function when the graph executes, passing your connected inputs as the arguments. The default example trims a string and optionally appends a second value - a hint at the intended use: glue logic. It supports unpacking dicts and lists, so you can return multiple things. Think of it as the ComfyUI equivalent of dropping into a REPL mid-workflow: quick, flexible, and exactly as safe as the code you put in it.

The inputs and outputs that matter

  • function - the multiline code box. Write a Python function here; its return value becomes the output. This is the whole node.
  • input, input2, input3 (all optional, all type *) - up to three values of any type, passed into your function as arguments. Wire in a string, an int, an image, whatever your code expects.

The single output is output, typed * and flagged as a list - it carries back whatever your function returns, ready to wire into the next node. Because it's untyped, you're responsible for making sure what you return matches what the downstream node wants.

Installing it

ComfyUI Manager: search SDVN_Comfy_node, install, restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/StableDiffusionVN/SDVN_Comfy_node

Then pip install -r custom_nodes/SDVN_Comfy_node/requirements.txt from your ComfyUI root, and restart. Nothing extra to download - the node runs Python you already have.

Where people get burned

It runs whatever you type, with your permissions. This is a real code-execution node. Your own code is fine - that's the point - but the moment you paste a "handy snippet" from a workflow you downloaded off the internet, understand that you're running a stranger's Python with full access to your machine. That's the same attack surface the whole custom-node ecosystem carries (installing any pack runs its code too), just more direct and more visible here. Read what's in the box before you hit queue. Don't run Python from a shared workflow you haven't looked at.

The output type is on you. Since output is untyped, ComfyUI won't catch a mismatch at the connection. Return a string and wire it into a node expecting an image, and you get a confusing error downstream, not a helpful one at the node. Know what your function returns.

Errors halt the run. A typo or an exception in your function stops the whole workflow with a Python traceback in the console - which is actually the good case, because it tells you exactly what broke. Read the traceback; it's pointing at your code.

CategoryπŸ“‚ SDVN/πŸ‘¨πŸ»β€πŸ’» Dev

Inputs (4)

NameTypeDefaultDescription
functionSTRINGdef function(input_value, extra_value=None): "Example helper that trims text and optionally appends extra info." if input_value is None: return "No input provided" result = str(input_value).strip() if extra_value: result = f"{result} | extra: {extra_value}" return resultHΓ m Python cαΊ§n thα»±c thi
inputopt*Tham sα»‘ 1
input2opt*Tham sα»‘ 2
input3opt*Tham sα»‘ 3

Outputs (1)

NameTypeDescription
output*KαΊΏt quαΊ£ thα»±c thi.