ComfyUI Extension: ASTERR
Abstract Syntax Trees Evaluated Restricted Run (ASTERR) is a Python Script executor for ComfyUI
README
ASTERR
Abstract Syntax Trees Evaluated Restricted Run (ASTERR) is a Python Script executor for ComfyUI
[!WARNING] ASTERR runs Python Code from a Web Interface! It is highly recommended to run this in a closed-off environment, as it could have potential security risks.
Installation
Clone the reposity to ComfyUI/custom_nodes and restart ComfyUI if running.
Configuration
The default configuration specifies a maximum recursion limit of 100 for loops and nested operations. The allowed modules list is empty, and uses the internal default allowed imports.
DEFAULT CONFIG:
{
"recursion_limit": 100,
"allowed_modules": []
}
You can added allowed modules by their name, and optionally plus their submodule. For exmaple to allow PIL import and it's submodules you could either do
"allowed_modules": ["PIL", "PIL.Image", "PIL.ImageFilter"]
And so on, or you could do
"allowed_modules": ["PIL.*"]
To live dangerously and allow any imports like a og boss, you can use:
"allowed_modules": ["*"]
Nodes
ASTERR Script Node
Required Params
script(STRING): The script to process. Can be converted to input to provide custom text string input.
Optional params
trigger_run_01(INT):0or1INTthat defines whether the script should run or not.athroughk(*): Arbitrary input (STRING,INT,FLOAT,IMAGE,MODEL, etc) to pass as a variable to the execution namespace.
[!WARNING] Be sure to expect and handle the data you input. If
ais aIMAGEtensor input, we need to handle the batched tensor ASTERR will receive accordingly.
preset_script(COMBO): List of.pyscripts located inASTERR/scriptsfor preset execution. This will override thescriptinput.always_run(COMBO):falseortrueon whether to always run the script (similar to trigger on run, but a constant state). If false, it will only run when a change has been made.
Hidden params
- Accessible within scripts is
prmptandextra_pnginfovars which can be used to investigate, fetch data. In the case ofextra_pnginfoyou can overwrite the variable with an updated version of theextra_pnginfo(dict) to return with the node to the executor.
Save ASTERR Script Node
Save a ASTERR script which would then be available under ASTERR/scripts and the preset_scripts menu in the ASTERR Script node.
Required Params
script_string(STRING): Script content string input. This string is saved to a python script file underASTERR/scriptsscript_name(STRING): Script name to use, does not include the extension.overwrite_script(COMBO):falseortrueon whether to overwrite the script that already exists on disk.
PRESET SCRIPTS
Preset scripts allow you to pre-define functions to pick from and run any time. Scripts are saved a .py files (so editing is familiar).
Return methods
asterr_resultvar can be used to define the result to return to the node.extra_pnginfocan be used and overwritten to return edited extra_pnginfo
Available Scripts
- resize_maxsize.py
- Resize input
a(IMAGE) propotionally withb(INT) as max size of the result.
- Resize input
Script Code
# Inside functions, we must import our modules as they are scoped from the main script
# tensor2pil() and pil2tensor() are built-in methods to help with `IMAGE` input
# a = image (tensor batched image)
# b = max size (INT)
def resizeImage(image, max_size):
import PIL
width, height = image.size
new_width, new_height = width, height
if width > max_size or height > max_size:
ratio = min(max_size / width, max_size / height)
new_width = int(width * ratio)
new_height = int(height * ratio)
elif max_size > max(width, height):
ratio = max_size / max(width, height)
new_width = int(width * ratio)
new_height = int(height * ratio)
resized_image = image.resize((new_width, new_height))
return resized_image
# We define the result to return to the node with the `asterr_result` var
asterr_result = pil2tensor(resizeImage(tensor2pil(a), b))