|
| 1 | +## Overview |
| 2 | + |
| 3 | +The `ComfyUI-to-Python-Extension` is a powerful tool that translates ComfyUI workflow into executable Python code. Designed to bridge the gap between ComfyUI's visual interface and Python's programming environment, this script facilitates the seamless transition from design to code execution. Whether you're a data scientist, a software developer, or an AI enthusiast, this tool streamlines the process of implementing ComfyUI workflows in Python. The output makes it easy to queue a large amount of images for generation and provides a base script to easily modify for experimination. |
| 4 | + |
| 5 | +**Convert this:** |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +**To this:** |
| 11 | + |
| 12 | +``` |
| 13 | +import random |
| 14 | +import sys |
| 15 | +
|
| 16 | +sys.path.append("../") |
| 17 | +from nodes import KSamplerAdvanced |
| 18 | +from nodes import EmptyLatentImage |
| 19 | +from nodes import VAEDecodeTiled |
| 20 | +from nodes import SaveImage |
| 21 | +from nodes import CheckpointLoaderSimple |
| 22 | +from nodes import CLIPTextEncode |
| 23 | +
|
| 24 | +
|
| 25 | +def main(): |
| 26 | + checkpointloadersimple = CheckpointLoaderSimple() |
| 27 | + checkpointloadersimple_4 = checkpointloadersimple.load_checkpoint( |
| 28 | + ckpt_name="sd_xl_base_1.0.safetensors" |
| 29 | + ) |
| 30 | +
|
| 31 | + emptylatentimage = EmptyLatentImage() |
| 32 | + emptylatentimage_5 = emptylatentimage.generate( |
| 33 | + width=1024, height=1024, batch_size=1 |
| 34 | + ) |
| 35 | +
|
| 36 | + cliptextencode = CLIPTextEncode() |
| 37 | + cliptextencode_6 = cliptextencode.encode( |
| 38 | + text="evening sunset scenery blue sky nature, glass bottle with a galaxy in it", |
| 39 | + clip=checkpointloadersimple_4[1], |
| 40 | + ) |
| 41 | +
|
| 42 | + cliptextencode_7 = cliptextencode.encode( |
| 43 | + text="text, watermark", clip=checkpointloadersimple_4[1] |
| 44 | + ) |
| 45 | +
|
| 46 | + checkpointloadersimple_12 = checkpointloadersimple.load_checkpoint( |
| 47 | + ckpt_name="sd_xl_refiner_1.0.safetensors" |
| 48 | + ) |
| 49 | +
|
| 50 | + cliptextencode_15 = cliptextencode.encode( |
| 51 | + text="evening sunset scenery blue sky nature, glass bottle with a galaxy in it", |
| 52 | + clip=checkpointloadersimple_12[1], |
| 53 | + ) |
| 54 | +
|
| 55 | + cliptextencode_16 = cliptextencode.encode( |
| 56 | + text="text, watermark", clip=checkpointloadersimple_12[1] |
| 57 | + ) |
| 58 | +
|
| 59 | + ksampleradvanced = KSamplerAdvanced() |
| 60 | + vaedecodetiled = VAEDecodeTiled() |
| 61 | + saveimage = SaveImage() |
| 62 | +
|
| 63 | + for q in range(1, 10): |
| 64 | + ksampleradvanced_10 = ksampleradvanced.sample( |
| 65 | + add_noise="enable", |
| 66 | + noise_seed=random.randint(1, 2**64), |
| 67 | + steps=25, |
| 68 | + cfg=8, |
| 69 | + sampler_name="euler", |
| 70 | + scheduler="normal", |
| 71 | + start_at_step=0, |
| 72 | + end_at_step=20, |
| 73 | + return_with_leftover_noise="enable", |
| 74 | + model=checkpointloadersimple_4[0], |
| 75 | + positive=cliptextencode_6[0], |
| 76 | + negative=cliptextencode_7[0], |
| 77 | + latent_image=emptylatentimage_5[0], |
| 78 | + ) |
| 79 | +
|
| 80 | + ksampleradvanced_11 = ksampleradvanced.sample( |
| 81 | + add_noise="disable", |
| 82 | + noise_seed=random.randint(1, 2**64), |
| 83 | + steps=25, |
| 84 | + cfg=8, |
| 85 | + sampler_name="euler", |
| 86 | + scheduler="normal", |
| 87 | + start_at_step=20, |
| 88 | + end_at_step=10000, |
| 89 | + return_with_leftover_noise="disable", |
| 90 | + model=checkpointloadersimple_12[0], |
| 91 | + positive=cliptextencode_15[0], |
| 92 | + negative=cliptextencode_16[0], |
| 93 | + latent_image=ksampleradvanced_10[0], |
| 94 | + ) |
| 95 | +
|
| 96 | + vaedecodetiled_17 = vaedecodetiled.decode( |
| 97 | + samples=ksampleradvanced_11[0], vae=checkpointloadersimple_12[2] |
| 98 | + ) |
| 99 | +
|
| 100 | + saveimage_19 = saveimage.save_images( |
| 101 | + filename_prefix="ComfyUI", images=vaedecodetiled_17[0].detach() |
| 102 | + ) |
| 103 | +
|
| 104 | +
|
| 105 | +if __name__ == "__main__": |
| 106 | + main() |
| 107 | +``` |
| 108 | + |
| 109 | +## Usage |
| 110 | + |
| 111 | +1. **Clone this repo** |
| 112 | + ```bash |
| 113 | + git clone https://github.com/pydn/ComfyUI-to-Python-Extension.git |
| 114 | + ``` |
| 115 | + |
| 116 | +2. **Install requirements** |
| 117 | + ```bash |
| 118 | + pip install -r requirements.txt |
| 119 | + ``` |
| 120 | + |
| 121 | +3. **Copy files into ComfyUI**: Copy [`ComfyUI-to-Python-Extension/`](https://github.com/pydn/ComfyUI-to-Python-Extension/tree/main/ComfyUI-to-Python-Extension) into the parent folder of your cloned version of ComfyUI. |
| 122 | + |
| 123 | + After copying `ComfyUI-to-Python-Extension/` your local `ComfyUI` directory should look like this: |
| 124 | + ``` |
| 125 | + /comfy |
| 126 | + /comfy_extras |
| 127 | + /ComfyUI-to-Python-Extension |
| 128 | + /custom_nodes |
| 129 | + /input |
| 130 | + /models |
| 131 | + /output |
| 132 | + /script_examples |
| 133 | + /web |
| 134 | + .gitignore |
| 135 | + LICENSE |
| 136 | + README.md |
| 137 | + comfyui_screenshot.png |
| 138 | + cuda_mollac.py |
| 139 | + execution.py |
| 140 | + extra_model_paths.yaml.example |
| 141 | + folder_paths.py |
| 142 | + latent_preview.py |
| 143 | + main.py |
| 144 | + nodes.py |
| 145 | + requirements.txt |
| 146 | + server.py |
| 147 | + ``` |
| 148 | + |
| 149 | +4. **Navigate to the `ComfyUI-to-Python-Extension` folder in your local `ComfyUI` directory.** |
| 150 | + |
| 151 | +5. **Run the Script**: Use the following command to run the script with a specific JSON file: |
| 152 | + |
| 153 | + ```bash |
| 154 | + python generate_python_code.py |
| 155 | + ``` |
0 commit comments