Skip to content

Execute python code in a custom ComfyUI node. Write code to alter the node's input/outputs. Embed mini-scripts into your saved workflows.

Notifications You must be signed in to change notification settings

ComfyNodePRs/PR-python-interpreter-node-6ea71337

 
 

Repository files navigation

Python Interpreter ComfyUI Node 🐍

demo video

Description

  • Write Python code in a node that executes when the workflow is queued
  • The stdout/stderr (e.g., prints, error tracebacks) are displayed in the node.
  • The input values can be accessed by their UI name.
  • The output values are the same as the input values, changes made to input values in the code are reflected in the output values.

Requirements

  • python 3.10+

Installation

  1. cd into ComfyUI/custom_nodes directory
  2. git clone this repository

Reason to Use

  • Embedding scripts into workflows
  • Specialized tasks
  • Converting types
  • Doing math
  • Debugging
  • Testing custom nodes
  • Even if you can't code, you can ask ChatGPT to write a python snippet

Usage

  • The variables in the UI (e.g., image1, number1, text1, etc.) can be accessed directly in the code by the same name
    print(image1.shape)
    print(number1 * random.randint(0, 99))
  • The output values share the same names as the input values. Changes you make to these variables will be reflected in the output values.
  • The code will work as expected in almost all cases except for i. re-assignment and ii. passing the variables as arguments to functions.
    1. To re-assign a variable, you must use its to() method, regardless of the variable's type
      # Instead of number1 = float(number1):
      number1.to(float(number1))
      
      # Instead of image1 = 0.5 * (image1 + image2):
      image1.to(0.5 * (image1 + image2))
      
      # Instead of mask1 = 1 - mask1:
      mask1.to(1 - mask1)
      
      # Instead of text1 = text1.replace("bad", ""):
      text1.to(text1.replace("bad", ""))
      
      # In-place operators (+=, ++, /=, etc.) will work normally
      number1++
      text1 += " is good"
    2. To pass the variables as arguments to functions, just pass the .data attribute of the variable instead (regardless of the variable's type)
      # Instead of pil_img = ToPILImage()(image1):
      pil_img = ToPILImage()(image1.data)
      
      # Instead of random.sample[number1, number2]:
      random.sample([number1.data, number2.data])
      
      # Not necessary for built-in functions
      print(image1) # works
      print(len(text1)) # works
  • To understand the type and shape of some of the inputs, please refer to the Images, Latents, and Masks section of comfydocs.org.
  • This situation is unlikely to occur, but try to avoid re-assigning variables to objects of a different type. You may lose access to some instance methods. If it must be done (e.g., to get an ouput of a novel type), just do it at the end of the code, and use your own variables in the meantime.

Examples

demo picture - complementary color palette

demo picture = caption composite


Disclaimer: Do not put this on a server that is accessible to the public. This node can run any Python code provided by a user, including malicious code.

About

Execute python code in a custom ComfyUI node. Write code to alter the node's input/outputs. Embed mini-scripts into your saved workflows.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 88.0%
  • JavaScript 12.0%