### Run ComfyScript Example with uv Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md Execute a ComfyScript example file using `uv`. This method allows running Python scripts without a global Python installation. ```sh uv run examples/uv.py ``` -------------------------------- ### Runtime Workflow with Local ComfyUI Path Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md Example Python code to load a local ComfyUI installation for runtime. Replace 'path/to/ComfyUI' with the actual path to your ComfyUI directory. ```python from comfy_script.runtime import * # ComfyUI path load(r'path/to/ComfyUI') from comfy_script.runtime.nodes import * ``` -------------------------------- ### uv Environment Setup for Type Stubs Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md Create a virtual environment using `uv` and install ComfyScript. This setup is recommended for type stub support and Jupyter Notebook integration. ```sh uv venv --seed --python 3.13 uv pip install "comfy-script[default]" ``` -------------------------------- ### Install Solara Feature Source: https://github.com/chaoses-ib/comfyscript/blob/main/examples/ui.ipynb Installs the necessary dependencies to enable Solara widget support. ```bash # To use Solara widgets, ComfyScript must be installed with the `solara` feature !python -m pip install -e "..[default,solara]" ``` -------------------------------- ### Troubleshoot pip Installation Error Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md If you encounter 'ERROR: File "setup.py" or "setup.cfg" not found', update your pip installation to the latest version. ```sh python -m pip install -U pip ``` -------------------------------- ### CLI Transpilation Examples Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Transpiler.md Execute the transpiler from the command line to convert workflow files. ```powershell python -m comfy_script.transpile "D:\workflow.json" ``` ```sh uvx --from "comfy-script[default]" python -m comfy_script.transpile "D:\workflow.json" ``` ```powershell python -m comfy_script.transpile "tests\transpile\default.json" --runtime ``` ```powershell python -m comfy_script.transpile "tests\transpile\default.json" --runtime > script.py ``` -------------------------------- ### Install ComfyUI and ComfyScript with uv Source: https://github.com/chaoses-ib/comfyscript/blob/main/README.md Use uv to create a virtual environment, install Comfy-Cli and ComfyUI, and then clone and install ComfyScript with default dependencies. This method is recommended for managing Python environments efficiently. ```sh mkdir ComfyUI cd ComfyUI uv venv --seed --python 3.12 uv pip install comfy-cli uv run comfy --workspace . install git clone https://github.com/Chaoses-Ib/ComfyScript.git ./custom_nodes/ComfyScript uv pip install -e "./custom_nodes/ComfyScript[default]" ``` -------------------------------- ### Basic ComfyScript Workflow Example Source: https://github.com/chaoses-ib/comfyscript/blob/main/README.md A basic ComfyScript workflow that loads a ComfyUI server, creates an empty image, and saves it. Requires ComfyUI server to be running. ```python from comfy_script.runtime import * # ComfyUI server/path # or: load(r'path/to/ComfyUI') load('http://127.0.0.1:8188/') from comfy_script.runtime.nodes import * with Workflow(wait=True): image = EmptyImage() images = util.get_images(image, save=True) ``` -------------------------------- ### Install ComfyScript with Solara support Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/UI/Solara.md Installation commands for enabling Solara widgets depending on the ComfyScript environment. ```bash pip install -e ".[default,solara]" ``` ```bash pip install "comfy-script[default,solara]" ``` -------------------------------- ### Basic Workflow with Variables and f-strings Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Runtime.md This example demonstrates how to use variables for prompts, seeds, and steps, and f-strings for dynamic filenames. It simplifies workflow definition by abstracting common parameters. ```python pos = 'beautiful scenery nature glass bottle landscape, , purple galaxy bottle,' neg = 'text, watermark' seed = 123 steps = 20 model, clip, vae = CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt') latent = EmptyLatentImage(512, 512, 1) latent = KSampler(model, seed, steps, 8, 'euler', 'normal', CLIPTextEncode(pos, clip), CLIPTextEncode(neg, clip), latent, 1) SaveImage(VAEDecode(latent, vae), f'{seed} {steps}') ``` -------------------------------- ### Install Comfy-Cli and ComfyUI Source: https://github.com/chaoses-ib/comfyscript/blob/main/README.md Install Comfy-Cli and ComfyUI using pip. This is a prerequisite for installing ComfyScript via the git clone method. ```sh python -m pip install comfy-cli comfy --here install ``` -------------------------------- ### Install ComfyUI package (PyTorch installed) Source: https://github.com/chaoses-ib/comfyscript/blob/main/README.md Install the ComfyUI package from GitHub using pip, with '--no-build-isolation'. This command is suitable for environments where PyTorch is already installed, such as Google Colab. ```sh python -m pip install wheel python -m pip install --no-build-isolation git+https://github.com/hiddenswitch/ComfyUI.git ``` -------------------------------- ### Runtime Workflow with ComfyUI Server Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md Example Python code to load a ComfyUI server and define a basic workflow. Ensure ComfyUI is running at the specified address. ```python from comfy_script.runtime import * # ComfyUI server load('http://127.0.0.1:8188/') from comfy_script.runtime.nodes import * with Workflow(wait=True): image = EmptyImage() images = util.get_images(image, save=True) ``` -------------------------------- ### Install ComfyUI package (PyTorch not installed) Source: https://github.com/chaoses-ib/comfyscript/blob/main/README.md Install the ComfyUI package from GitHub using pip. This command is used when PyTorch is not already installed in your environment. ```sh python -m pip install git+https://github.com/hiddenswitch/ComfyUI.git ``` -------------------------------- ### Transpiled Python Workflow Source: https://github.com/chaoses-ib/comfyscript/blob/main/README.md Example of a basic text-to-image workflow converted into ComfyScript Python code. ```python model, clip, vae = CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt') conditioning = CLIPTextEncode('beautiful scenery nature glass bottle landscape, , purple galaxy bottle,', clip) conditioning2 = CLIPTextEncode('text, watermark', clip) latent = EmptyLatentImage(512, 512, 1) latent = KSampler(model, 156680208700286, 20, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1) image = VAEDecode(latent, vae) SaveImage(image, 'ComfyUI') ``` -------------------------------- ### Transpiled Script Output Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Transpiler.md Example output generated by the transpiler. ```python model, clip, vae = CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt') conditioning = CLIPTextEncode('beautiful scenery nature glass bottle landscape, , purple galaxy bottle,', clip) conditioning2 = CLIPTextEncode('text, watermark', clip) latent = EmptyLatentImage(512, 512, 1) latent = KSampler(model, 156680208700286, 20, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1) image = VAEDecode(latent, vae) SaveImage(image, 'ComfyUI') ``` ```python from comfy_script.runtime import * load() from comfy_script.runtime.nodes import * with Workflow(): model, clip, vae = CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt') conditioning = CLIPTextEncode('beautiful scenery nature glass bottle landscape, , purple galaxy bottle,', clip) conditioning2 = CLIPTextEncode('text, watermark', clip) latent = EmptyLatentImage(512, 512, 1) latent = KSampler(model, 156680208700286, 20, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1) image = VAEDecode(latent, vae) SaveImage(image, 'ComfyUI') ``` -------------------------------- ### Install ComfyScript with Latest Commit Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md Use this command to install ComfyScript directly from the latest commit on GitHub. The `[default]` option installs common dependencies. ```sh python -m pip install "comfy-script[default] @ git+https://github.com/Chaoses-Ib/ComfyScript.git" ``` -------------------------------- ### uv Example with ComfyScript Dependencies Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md Python script using `uv` for execution, with metadata specifying Python version and ComfyScript dependencies. This allows `uv` to manage the environment. ```python # /// script # requires-python = ">=3.9" # dependencies = [ # "comfy-script[default]", # ] # /// from comfy_script.runtime import * load('http://127.0.0.1:8188/') from comfy_script.runtime.nodes import * with Workflow(wait=True): image = EmptyImage() images = util.get_images(image, save=True) ``` -------------------------------- ### Install ComfyScript Nodes in ComfyUI Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md Steps to install ComfyScript as a custom node within a ComfyUI installation. This involves cloning the repository and installing requirements. ```sh cd ComfyUI/custom_nodes git clone https://github.com/Chaoses-Ib/ComfyScript.git cd ComfyScript python -m pip install -r requirements.txt ``` -------------------------------- ### Run uv Example with Explicit Dependencies Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md Execute a Python script with `uv`, explicitly specifying ComfyScript dependencies using the `--with` flag. This is an alternative to using script metadata. ```sh uv run --with "comfy-script[default]" examples/uv.py ``` -------------------------------- ### Install ComfyScript with Default Dependencies Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md Installs ComfyScript along with common dependencies, suitable for developing apps or libraries that interact with a ComfyUI server. ```sh python -m pip install -U "comfy-script[default]" ``` -------------------------------- ### Reproducible Workflows with Custom Metadata Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Runtime.md This example shows how to ensure reproducibility by adding custom metadata to the saved image. It defines positive and negative prompts as variables and includes them in the `extra_pnginfo` argument of `SaveImage`. ```python positive = 'beautiful scenery nature glass bottle landscape, , purple galaxy bottle,' negative = 'text, watermark' model, clip, vae = CheckpointLoaderSimple(Checkpoints.v1_5_pruned_emaonly) latent = EmptyLatentImage(512, 512, 1) latent = KSampler(model, 156680208700286, 20, 8, 'euler', 'normal', CLIPTextEncode(positive, clip), CLIPTextEncode(negative, clip), latent) image = VAEDecode(latent, vae) SaveImage(image, 'ComfyUI', extra_pnginfo={'myfield': {'positive': positive, 'negative': negative}}) ``` -------------------------------- ### Install ComfyScript with ComfyUI (pip) Source: https://github.com/chaoses-ib/comfyscript/blob/main/README.md Clone the ComfyScript repository into your ComfyUI custom nodes directory and install it with default dependencies. This method is suitable if you have Python and ComfyUI already installed. ```sh cd ComfyUI/custom_nodes git clone https://github.com/Chaoses-Ib/ComfyScript.git cd ComfyScript python -m pip install -e ".[default]" ``` -------------------------------- ### Run Pytest Tests with Hatch Source: https://github.com/chaoses-ib/comfyscript/blob/main/tests/README.md This script backs up the __init__.py file, runs pytest tests within the 'test' environment using Hatch, and then restores the backup. Ensure you have Hatch and Pytest installed. ```pwsh mv __init__.py __init__.py.bak hatch env run -e test pytest mv __init__.py.bak __init__.py ``` -------------------------------- ### Submit Workflow to Queue and Get Task Source: https://github.com/chaoses-ib/comfyscript/blob/main/examples/runtime.ipynb Constructs an image generation workflow, adds it to the ComfyUI queue, and retrieves the task object for potential later use or waiting. ```python model, clip, vae = CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt') conditioning = CLIPTextEncode('beautiful scenery nature glass bottle landscape, , purple galaxy bottle,', clip) conditioning2 = CLIPTextEncode('text, watermark', clip) latent = EmptyLatentImage(512, 512, 1) latent = KSampler(model, 156680208700286, 20, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1) image = VAEDecode(latent, vae) save = SaveImage(image, 'ComfyUI') task = queue.put(save) print(task) # To wait the task: # task.wait() # or: # await task ``` -------------------------------- ### CivitAI Checkpoint Loader definition and usage Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Models/README.md Function signature and examples for loading checkpoints from CivitAI. ```python def CivitAICheckpointLoader( ckpt_air: str = '{model_id}@{model_version}', ckpt_name: CivitAICheckpointLoader.ckpt_name = 'none', download_chunks: int | None = 4, download_path: CivitAICheckpointLoader.download_path | None = r'models\checkpoints' ) -> tuple[Model, Clip, Vae] ``` ```python model, clip, vae = CivitAICheckpointLoader('101055@128078') model, clip, vae = CivitAICheckpointLoader('https://civitai.com/models/101055?modelVersionId=128078') model, clip, vae = CivitAICheckpointLoader('https://civitai.com/models/101055/sd-xl?modelVersionId=128078') ``` -------------------------------- ### Direct Node Execution in Real Mode Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Runtime.md In real mode, calling a node executes it directly. This example shows printing a CheckpointLoaderSimple object, which triggers its execution. ```python print(CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt')) ``` -------------------------------- ### Install specific ComfyUI package version Source: https://github.com/chaoses-ib/comfyscript/blob/main/README.md Install a specific, tested version of the ComfyUI package using its git commit hash. This is useful for ensuring compatibility if the latest version causes issues. ```sh python -m pip install --no-build-isolation git+https://github.com/hiddenswitch/ComfyUI.git@95a12f42e2b0c78202af10f2337009bd769157a7 ``` -------------------------------- ### Initialize ComfyScript Runtime Source: https://context7.com/chaoses-ib/comfyscript/llms.txt Use the `load()` function to initialize the ComfyScript runtime. It can connect to a remote server, load from a local installation, or use the `comfyui` package. Custom arguments can be passed using `ComfyUIArgs`. ```python from comfy_script.runtime import * # Connect to a remote ComfyUI server load('http://127.0.0.1:8188/') # Or load from a local ComfyUI installation load(r'path/to/ComfyUI') # Or use the comfyui package load('comfyui') # With custom arguments load(args=ComfyUIArgs('--disable-metadata')) from comfy_script.runtime.nodes import * ``` -------------------------------- ### Troubleshoot ModuleNotFoundError: hatchling Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md To resolve 'ModuleNotFoundError: No module named 'hatchling'', install the `hatchling` package. ```sh python -m pip install hatchling ``` -------------------------------- ### CivitAI LoRA Loader definition and usage Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Models/README.md Function signature and examples for loading LoRAs from CivitAI. ```python def CivitAILoraLoader( model: Model, clip: Clip, lora_air: str = '{model_id}@{model_version}', lora_name: CivitAILoraLoader.lora_name = 'none', strength_model: float = 1.0, strength_clip: float = 1.0, download_chunks: int | None = 4, download_path: CivitAILoraLoader.download_path | None = r'models\loras' ) -> tuple[Model, Clip] ``` ```python model, clip = CivitAILoraLoader(model, clip, '350450@391994', strength_clip=1, strength_model=1) model, clip = CivitAILoraLoader(model, clip, 'https://civitai.com/models/350450?modelVersionId=391994', strength_clip=1, strength_model=1) model, clip = CivitAILoraLoader(model, clip, 'https://civitai.com/models/350450/sdxl-lightning-lora-2step?modelVersionId=391994', strength_clip=1, strength_model=1) ``` -------------------------------- ### Load ComfyScript Runtime and Nodes Source: https://github.com/chaoses-ib/comfyscript/blob/main/examples/runtime.ipynb Imports necessary components from the ComfyScript runtime. This setup is required before defining any workflows. ```python from comfy_script.runtime.real import * load() from comfy_script.runtime.real.nodes import * ``` -------------------------------- ### Import ComfyScript with Hardcoded Path Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md Python code demonstrating how to import the ComfyScript package by manually adding its source directory to `sys.path`. Useful when ComfyScript is not installed as a package. ```python import sys # Or just '../src' if used in the examples directory sys.path.insert(0, r'D:\...\ComfyUI\custom_nodes\ComfyScript\src') import comfy_script ``` -------------------------------- ### Generating Workflow API Format in Virtual Mode Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Runtime.md In virtual mode, you can obtain a workflow's API format by calling `api_format_json()`. This example demonstrates generating a loopback workflow for multiple img2img iterations and saving it to a JSON file. ```python with Workflow(queue=False) as wf: model, clip, vae = CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt') conditioning = CLIPTextEncode('beautiful scenery nature glass bottle landscape, , purple galaxy bottle,', clip) conditioning2 = CLIPTextEncode('text, watermark', clip) latent = EmptyLatentImage(512, 512, 1) latent = KSampler(model, 123, 20, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1) SaveImage(VAEDecode(latent, vae), '0') for i in range(5): latent = KSampler(model, 123, 20, 8, 'euler', 'normal', conditioning, conditioning2, latent, 0.8) SaveImage(VAEDecode(latent, vae), f'{i}') json = wf.api_format_json() with open('prompt.json', 'w') as f: f.write(json) ``` -------------------------------- ### Jupyter Notebook Image Viewer with Python Source: https://context7.com/chaoses-ib/comfyscript/llms.txt Provides a Python example for displaying multiple images with optional titles in a grid layout within Jupyter notebooks using ipywidgets. Customizes display with specified rows or columns. ```python import comfy_script.ui as ui from comfy_script.runtime import * load() from comfy_script.runtime.nodes import * def generate(blur_radius): image, _ = LoadImage('test.png') image = ImageBlur(image, blur_radius) return PreviewImage(image).wait() # Generate images with different blur values images = [] titles = [] for radius in [1, 5, 10, 15, 20]: images.append(generate(radius)) titles.append(f'radius {radius}') # Display in grid ui.ipy.ImageViewer(images, titles=titles) # Customize layout ui.ipy.ImageViewer(images, titles=titles).display(rows=2) ui.ipy.ImageViewer(images, titles=titles).display(columns=3) ``` -------------------------------- ### Transpiler CLI Help Output Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Transpiler.md Displays the available options and usage instructions for the transpiler CLI. ```text Usage: python -m comfy_script.transpile [OPTIONS] WORKFLOW Transpile workflow to ComfyScript. Options: --api TEXT [default: http://127.0.0.1:8188/] --runtime Wrap the script with runtime imports and workflow context. --args [pos|pos2orkwd|kwd] Format node inputs as positional or keyword arguments. [default: Pos2OrKwd] --help Show this message and exit. ``` -------------------------------- ### Execute a basic workflow Source: https://github.com/chaoses-ib/comfyscript/blob/main/README.md Initializes the runtime and executes a standard image generation workflow within a context manager. ```python from comfy_script.runtime import * load() from comfy_script.runtime.nodes import * with Workflow(): model, clip, vae = CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt') conditioning = CLIPTextEncode('beautiful scenery nature glass bottle landscape, , purple galaxy bottle,', clip) conditioning2 = CLIPTextEncode('text, watermark', clip) latent = EmptyLatentImage(512, 512, 1) latent = KSampler(model, 156680208700286, 20, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1) image = VAEDecode(latent, vae) SaveImage(image, 'ComfyUI') # To retrieve `image` instead of saving it, replace `SaveImage` with: # images = util.get_images(image) # `images` is of type `list[PIL.Image.Image]` ``` -------------------------------- ### Display Metadata Viewer Source: https://github.com/chaoses-ib/comfyscript/blob/main/examples/ui.ipynb Initializes the Solara metadata viewer component. ```python ui.solara.MetadataViewer() ``` -------------------------------- ### Execute Flux Workflow with CheckpointLoaderSimple Source: https://github.com/chaoses-ib/comfyscript/blob/main/examples/flux.ipynb Runs a basic Flux workflow using a single checkpoint file. Note that CFG should be set to 1.0 for Flux models as negative prompts are ignored. ```python with Workflow(): model, clip, vae = CheckpointLoaderSimple('flux1-dev-fp8.safetensors') positive_conditioning = CLIPTextEncode('cute anime girl with massive fluffy fennec ears and a big fluffy tail blonde messy long hair blue eyes wearing a maid outfit with a long black gold leaf pattern dress and a white apron mouth open placing a fancy black forest cake with candles on top of a dinner table of an old dark Victorian mansion lit by candlelight with a bright window to the foggy forest and very expensive stuff everywhere there are paintings on the walls', clip) positive_conditioning = FluxGuidance(positive_conditioning, 3.5) negative_conditioning = CLIPTextEncode('', clip) latent = EmptySD3LatentImage(1024, 1024, 1) # Note that Flux dev and schnell do not have any negative prompt so CFG should be set to 1.0. Setting CFG to 1.0 means the negative prompt is ignored. latent = KSampler(model, 972054013131368, 20, 1, 'euler', 'simple', positive_conditioning, negative_conditioning, latent, 1) image = VAEDecode(latent, vae) SaveImage(image, 'ComfyUI') ``` -------------------------------- ### Define and Execute a Basic Workflow Source: https://github.com/chaoses-ib/comfyscript/blob/main/examples/runtime.ipynb Creates a workflow by chaining nodes for image generation, including checkpoint loading, text encoding, latent sampling, and image decoding. The output is saved to disk. ```python with Workflow(): model, clip, vae = CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt') conditioning = CLIPTextEncode('beautiful scenery nature glass bottle landscape, , purple galaxy bottle,', clip) conditioning2 = CLIPTextEncode('text, watermark', clip) latent = EmptyLatentImage(512, 512, 1) latent = KSampler(model, 156680208700286, 20, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1) image = VAEDecode(latent, vae) SaveImage(image, 'ComfyUI') # To retrieve `image` instead of saving it, replace `SaveImage` with: # images = util.get_images(image) # `images` is of type `list[PIL.Image.Image]` ``` -------------------------------- ### List available checkpoints in ComfyScript Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Models/README.md Initializes the runtime and retrieves a list of available checkpoint files. ```python from comfy_script.runtime import load load() from comfy_script.runtime.nodes import Checkpoints print(list(Checkpoints)) # [, , ..., ] ``` -------------------------------- ### Troubleshoot ModuleNotFoundError: editables Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md To resolve 'ModuleNotFoundError: No module named 'editables'', install the `editables` package. ```sh python -m pip install editables ``` -------------------------------- ### Initialize MetadataViewer widget Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/UI/Solara.md Basic instantiation of the MetadataViewer widget for viewing image or workflow metadata. ```python import comfy_script.ui as ui ui.solara.MetadataViewer() ``` -------------------------------- ### Load Models from CivitAI with Python Source: https://context7.com/chaoses-ib/comfyscript/llms.txt Shows how to download and load models (checkpoints and LoRAs) from CivitAI using model IDs or URLs. Supports specifying custom download paths and chunk sizes. ```python from comfy_script.runtime import * load() from comfy_script.runtime.nodes import * with Workflow(): # Load checkpoint by model ID and version model, clip, vae = CivitAICheckpointLoader('101055@128078') # Or using full URL model, clip, vae = CivitAICheckpointLoader( 'https://civitai.com/models/101055?modelVersionId=128078' ) # Load LoRA model, clip = CivitAILoraLoader( model, clip, lora_air='350450@391994', strength_model=1.0, strength_clip=1.0 ) # Custom download path model, clip, vae = CivitAICheckpointLoader( '101055@128078', download_path=r'models\checkpoints', download_chunks=4 ) ``` -------------------------------- ### Troubleshoot ModuleNotFoundError: comfy_extras.nodes_model_merging Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/README.md This error can occur if ComfyUI is installed in the same virtual environment as ComfyScript. Uninstalling ComfyUI from the venv can fix this. ```sh python -m pip uninstall comfyui ``` -------------------------------- ### Configure ComfyUI environment paths Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Models/README.md Command-line arguments for setting working directories and model paths. ```sh -w CWD, --cwd CWD Specify the working directory. If not set, this is the current working directory. models/, input/, output/ and other directories will be located here by default. [env var: COMFYUI_CWD] --base-paths BASE_PATHS [BASE_PATHS ...] Additional base paths for custom nodes, models and inputs. [env var: COMFYUI_BASE_PATHS] --extra-model-paths-config PATH [PATH ...] Load one or more extra_model_paths.yaml files. [env var: COMFYUI_EXTRA_MODEL_PATHS_CONFIG] --output-directory OUTPUT_DIRECTORY Set the ComfyUI output directory. [env var: COMFYUI_OUTPUT_DIRECTORY] --temp-directory TEMP_DIRECTORY Set the ComfyUI temp directory (default is in the ComfyUI directory). [env var: COMFYUI_TEMP_DIRECTORY] --input-directory INPUT_DIRECTORY Set the ComfyUI input directory. [env var: COMFYUI_INPUT_DIRECTORY] --create-directories Creates the default models/, input/, output/ and temp/ directories, then exits. [env var: COMFYUI_CREATE_DIRECTORIES] --user-directory USER_DIRECTORY Set the ComfyUI user directory with an absolute path. [env var: COMFYUI_USER_DIRECTORY] ``` -------------------------------- ### Multi-line and Raw Strings in ComfyScript Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Runtime.md Illustrates the use of multi-line strings (triple quotes) and raw strings for defining prompts and file paths. Shows how to load an image from a specified path. ```python pos = '''beautiful scenery nature glass bottle landscape, purple galaxy bottle,''' # or pos = ( '''beautiful scenery nature glass bottle landscape, purple galaxy bottle,''') image = LoadImageFromPath(r'ComfyUI_00001_-assets\ComfyUI_00001_.png [output]') ``` -------------------------------- ### Save Generated Images in Python Source: https://context7.com/chaoses-ib/comfyscript/llms.txt Demonstrates various methods to save generated images, including to a specified directory, as animated formats, or for preview. Also shows how to send images over WebSocket. ```python from comfy_script.runtime import * load() from comfy_script.runtime.nodes import * with Workflow(): model, clip, vae = CheckpointLoaderSimple(Checkpoints.v1_5_pruned_emaonly) latent = EmptyLatentImage(512, 512, 1) latent = KSampler(model, 123, 20, 8, 'euler', 'normal', CLIPTextEncode('landscape', clip), CLIPTextEncode('watermark', clip), latent) image = VAEDecode(latent, vae) # Save to output directory SaveImage(image, 'ComfyUI') # Save to temp directory (preview) PreviewImage(image) # Save as animation SaveAnimatedWEBP(image, 'animation', fps=6.0, lossless=True) SaveAnimatedPNG(image, 'animation', fps=6.0, compress_level=4) # Send over WebSocket (for external tools) ETNSendImageWebSocket(image) ``` ```python from comfy_script.runtime.real import * load() from comfy_script.runtime.real.nodes import * with Workflow(): image = EmptyImage(512, 512) pil_images = ImageToPIL(image) # Returns PIL.Image.Image ``` -------------------------------- ### Basic Image Generation Workflow Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Runtime.md This snippet demonstrates a complete image generation workflow using ComfyScript. It loads a checkpoint, encodes positive and negative prompts, creates an empty latent image, samples using KSampler, decodes the latent image, and saves the result. ```python with Workflow(): model, clip, vae = CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt') print(model, clip, vae, sep='\n') # # # conditioning = CLIPTextEncode('beautiful scenery nature glass bottle landscape, , purple galaxy bottle,', clip) conditioning2 = CLIPTextEncode('text, watermark', clip) print(conditioning2) # [[ # tensor([[ # [-0.3885, ..., 0.0674], # ... # [-0.8676, ..., -0.0057] # ]]), # {'pooled_output': tensor([[-1.2670e+00, ..., -1.5058e-01]])} # ]] latent = EmptyLatentImage(512, 512, 1) print(latent) # {'samples': tensor([[ # [[0., ..., 0.], # ... # [0., ..., 0.]], # [[0., ..., 0.], # ... # [0., ..., 0.]], # [[0., ..., 0.], # ... # [0., ..., 0.]], # [[0., ..., 0.], # ... # [0., ..., 0.]] # ]])} latent = KSampler(model, 156680208700286, 20, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1) image = VAEDecode(latent, vae) print(image) # tensor([[ # [[0.3389, 0.3652, 0.3428], # ... # [0.4277, 0.3789, 0.1445]], # ... # [[0.6348, 0.5898, 0.5270], # ... # [0.7012, 0.6680, 0.5952]] # ]]) print(SaveImage(image, 'ComfyUI')) # {'ui': {'images': [ # {'filename': 'ComfyUI_00001_.png', # 'subfolder': '', # 'type': 'output'} # ]}} ``` -------------------------------- ### Define a Basic Image Generation Workflow Source: https://github.com/chaoses-ib/comfyscript/blob/main/examples/runtime.ipynb Creates a simple workflow to generate an image from a checkpoint. It loads a model, encodes text prompts, creates latent space, samples the image, decodes it, and saves the result. Ensure the checkpoint file 'v1-5-pruned-emaonly.ckpt' exists in the ComfyUI models directory. ```python with Workflow(): model, clip, vae = CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt') conditioning = CLIPTextEncode('beautiful scenery nature glass bottle landscape, , purple galaxy bottle,', clip) conditioning2 = CLIPTextEncode('text, watermark', clip) latent = EmptyLatentImage(512, 512, 1) latent = KSampler(model, 156680208700286, 20, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1) image = VAEDecode(latent, vae) SaveImage(image, 'ComfyUI') ``` -------------------------------- ### Display images with ImageViewer Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/UI/ipywidgets.md Initializes an image viewer to display a list of images with corresponding titles. ```python import comfy_script.ui as ui def gen(x): image, _ = LoadImage(r'D:\ComfyUI\output\test_00001_.png') image = ImageBlur(image, x) return PreviewImage(image).wait() images = [] titles = [] for x in [1, 5, 10, 15, 20, 25, 30]: title = f'radius {x}' print(title) images.append(gen(x)) titles.append(title) ui.ipy.ImageViewer(images, titles=titles) ``` -------------------------------- ### Transpile Workflow via CLI Source: https://github.com/chaoses-ib/comfyscript/blob/main/README.md Use the command line to transpile a workflow JSON file. Requires a running ComfyUI instance for API access. ```sh python -m comfy_script.transpile "workflow.json" --api http://127.0.0.1:8188/ ``` ```sh uvx --from "comfy-script[default]" python -m comfy_script.transpile "workflow.json" --api http://127.0.0.1:8188/ ``` -------------------------------- ### Retrieve Generated Images from ComfyScript Source: https://context7.com/chaoses-ib/comfyscript/llms.txt Output nodes return result objects that can be awaited to retrieve generated PIL images. The `util.get_images` helper provides a convenient way to get images directly. ```python from comfy_script.runtime import * load() from comfy_script.runtime.nodes import * from PIL import Image with Workflow(): image = EmptyImage(512, 512, 1) result = SaveImage(image, 'ComfyUI') # Wait for the result image_batch = result.wait() # Get the first image as PIL.Image.Image first_image: Image.Image = image_batch[0] # Get all images in the batch all_images: list[Image.Image] = image_batch.wait() # Using util helper to get images directly with Workflow(wait=True): image = EmptyImage(512, 512) images = util.get_images(image, save=True) # Returns list[PIL.Image.Image] ``` -------------------------------- ### Full SDXL Workflow with ComfyScript Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Runtime.md This snippet shows a complete Stable Diffusion XL workflow, including loading base and refiner models, encoding positive and negative prompts, generating latent images, sampling with KSamplerAdvanced, decoding with VAE, and saving the final image. It iterates 10 times, generating a new image in each loop. ```python with Workflow(): checkpointloadersimple = CheckpointLoaderSimple() checkpointloadersimple_4 = checkpointloadersimple.load_checkpoint( ckpt_name="sd_xl_base_1.0.safetensors" ) emptylatentimage = EmptyLatentImage() emptylatentimage_5 = emptylatentimage.generate( width=1024, height=1024, batch_size=1 ) cliptextencode = CLIPTextEncode() cliptextencode_6 = cliptextencode.encode( text="evening sunset scenery blue sky nature, glass bottle with a galaxy in it", clip=checkpointloadersimple_4[1], ) cliptextencode_7 = cliptextencode.encode( text="text, watermark", clip=checkpointloadersimple_4[1] ) checkpointloadersimple_12 = checkpointloadersimple.load_checkpoint( ckpt_name="sd_xl_refiner_1.0.safetensors" ) cliptextencode_15 = cliptextencode.encode( text="evening sunset scenery blue sky nature, glass bottle with a galaxy in it", clip=checkpointloadersimple_12[1], ) cliptextencode_16 = cliptextencode.encode( text="text, watermark", clip=checkpointloadersimple_12[1] ) ksampleradvanced = KSamplerAdvanced() vaedecode = VAEDecode() saveimage = SaveImage() for q in range(10): ksampleradvanced_10 = ksampleradvanced.sample( add_noise="enable", noise_seed=random.randint(1, 2**64), steps=25, cfg=8, sampler_name="euler", scheduler="normal", start_at_step=0, end_at_step=20, return_with_leftover_noise="enable", model=checkpointloadersimple_4[0], positive=cliptextencode_6[0], negative=cliptextencode_7[0], latent_image=emptylatentimage_5[0], ) ksampleradvanced_11 = ksampleradvanced.sample( add_noise="disable", noise_seed=random.randint(1, 2**64), steps=25, cfg=8, sampler_name="euler", scheduler="normal", start_at_step=20, end_at_step=10000, return_with_leftover_noise="disable", model=checkpointloadersimple_12[0], positive=cliptextencode_15[0], negative=cliptextencode_16[0], latent_image=ksampleradvanced_10[0], ) vaedecode_17 = vaedecode.decode( samples=ksampleradvanced_11[0], vae=checkpointloadersimple_12[2] ) saveimage_19 = saveimage.save_images( filename_prefix="ComfyUI", images=vaedecode_17[0] ) ``` -------------------------------- ### Enabling Naked Mode in ComfyScript Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Runtime.md This example shows how to enable 'naked mode' in ComfyScript by calling `load(naked=True)`. Naked mode alters script execution, skipping certain code after `load()` except for `Workflow()`, which can be replaced by `torch.inference_mode()`. ```python import random from comfy_script.runtime.real import * load(naked=True) from comfy_script.runtime.real.nodes import * ``` -------------------------------- ### Solara Metadata Viewer Widget in Python Source: https://context7.com/chaoses-ib/comfyscript/llms.txt Demonstrates how to display an interactive metadata viewer widget using Solara in Jupyter notebooks. This widget supports drag-and-drop of PNG images and workflow JSON files. ```python import comfy_script.ui as ui # Display interactive metadata viewer in Jupyter # Supports drag-and-drop of PNG images and workflow JSON files ui.solara.MetadataViewer() ``` -------------------------------- ### Import ComfyScript UI Source: https://github.com/chaoses-ib/comfyscript/blob/main/examples/ui.ipynb Initializes the UI module for ComfyScript. ```python import comfy_script.ui as ui ``` -------------------------------- ### Basic Image Generation with ComfyScript Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Runtime.md A simple ComfyScript workflow to generate an image from latent space and save it. Requires model, VAE, and conditioning inputs. ```python latent = KSampler(model, 156680208700286, 20, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1) image = VAEDecode(latent, vae) SaveImage(image, 'ComfyUI') ``` -------------------------------- ### Implement Redux with Flux1 Source: https://github.com/chaoses-ib/comfyscript/blob/main/examples/flux.ipynb Configures a workflow using the Redux model for image-based prompting. Ensure all required model files are placed in their respective ComfyUI directories before execution. ```python with Workflow(): '''If you get an error in any of the nodes above make sure the files are in the correct directories. See the top of the examples page for the links : https://comfyanonymous.github.io/ComfyUI_examples/flux/ flux1-dev.safetensors goes in: ComfyUI/models/unet/ t5xxl_fp16.safetensors and clip_l.safetensors go in: ComfyUI/models/clip/ ae.safetensors goes in: ComfyUI/models/vae/ Tip: You can set the weight_dtype above to one of the fp8 types if you have memory issues.''' noise = RandomNoise(958831004022715) width = 1024 height = 1024 model = UNETLoader('flux1-dev.safetensors', 'default') # The reference sampling implementation auto adjusts the shift value based on the resolution, if you don't want this you can just comment out (Ctrl+/) this ModelSamplingFlux node. model = ModelSamplingFlux(model, 1.15, 0.5, width, height) clip = DualCLIPLoader('t5xxl_fp16.safetensors', 'clip_l.safetensors', 'flux') positive_conditioning = CLIPTextEncode('cute anime girl with massive fluffy fennec ears', clip) positive_conditioning = FluxGuidance(positive_conditioning, 3.5) style_model = StyleModelLoader('flux1-redux-dev.safetensors') clip_vision = CLIPVisionLoader('sigclip_vision_patch14_384.safetensors') # You can chain multiple `StyleModelApply` nodes if you want to mix multiple images together. image, _ = LoadImage('sd3_controlnet_example.png') clip_vision_output = CLIPVisionEncode(clip_vision, image) positive_conditioning = StyleModelApply(positive_conditioning, style_model, clip_vision_output) guider = BasicGuider(model, positive_conditioning) sampler = KSamplerSelect('euler') sigmas = BasicScheduler(model, 'simple', 20, 1) latent = EmptySD3LatentImage(width, height, 1) latent, _ = SamplerCustomAdvanced(noise, guider, sampler, sigmas, latent) vae = VAELoader('ae.safetensors') image = VAEDecode(latent, vae) SaveImage(image, 'ComfyUI') ``` -------------------------------- ### Create Empty Image Node Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Images/README.md Generates a blank image with specified dimensions, batch size, and color. Defaults to a 512x512 black image with a batch size of 1. ```python def EmptyImage( width: int = 512, height: int = 512, batch_size: int = 1, color: int = 0 ) -> Image ``` -------------------------------- ### List Checkpoints Source: https://github.com/chaoses-ib/comfyscript/blob/main/docs/Models/README.md Demonstrates how to list all available checkpoints loaded by ComfyScript. Supports various checkpoint formats. ```APIDOC ## List Checkpoints ### Description This section shows how to list all available checkpoints that ComfyScript can recognize. ComfyUI supports multiple checkpoint formats including `.ckpt`, `.pt`, `.bin`, and `.safetensors`. ### Method Python Script ### Endpoint N/A ### Parameters None ### Request Example ```python from comfy_script.runtime import load load() from comfy_script.runtime.nodes import Checkpoints print(list(Checkpoints)) ``` ### Response #### Success Response (Output) - List of checkpoint objects, e.g., `[, ...]` #### Response Example ``` [, , ..., ] ``` ``` -------------------------------- ### Generate Images with Nodes Source: https://github.com/chaoses-ib/comfyscript/blob/main/examples/ui.ipynb Defines a workflow to load an image, apply a blur effect, and display the result using the UI viewer. ```python def gen(x): image, _ = LoadImage(r'D:\ComfyUI\output\test_00001_.png') image = ImageBlur(image, x) return PreviewImage(image).wait() images = [] titles = [] for x in [1, 5, 10, 15, 20, 25, 30]: title = f'radius {x}' print(title) images.append(gen(x)) titles.append(title) ui.ipy.ImageViewer(images, titles=titles) ``` -------------------------------- ### Transpiler CLI Usage Source: https://context7.com/chaoses-ib/comfyscript/llms.txt Command-line interface for transpiling workflows without writing Python code. Supports basic usage, runtime wrapper, specifying ComfyUI server, saving to file, and using uvx. ```bash # Basic usage python -m comfy_script.transpile "workflow.json" # Output: model, clip, vae = CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt')... # With runtime wrapper python -m comfy_script.transpile "workflow.json" --runtime # Output includes: from comfy_script.runtime import * # load() # with Workflow(): # ... # Specify ComfyUI server python -m comfy_script.transpile "workflow.json" --api http://192.168.1.100:8188/ # Save to file python -m comfy_script.transpile "workflow.json" --runtime > script.py # Using uv without installing uvx --from "comfy-script[default]" python -m comfy_script.transpile "workflow.json" ```