Compare commits

..
101 Commits
Author SHA1 Message Date
Peyton DeNiro 32d42bf472 Merge pull request #133 from xiaoxidashen/main
Fix comfyui_to_python_utils import error, Fix conflict between comfy/utils.py and utils,Fix not await for load extra nodes
2025-09-26 18:53:00 -05:00
Peyton DeNiro 281b8408c9 Merge pull request #132 from nuaimat/main
Fix "Save as Script" option not available in ComfyUI v0.3.50
2025-09-26 18:23:32 -05:00
chengxi b215c83b76 fix not await for load extra nodes 2025-09-07 14:33:48 +08:00
chengxi 76bc1c343e Fix conflict between comfy/utils.py and utils 2025-09-07 14:33:27 +08:00
chengxi 42bc6926ae fix not found comfyui_to_python_utils 2025-09-07 14:32:28 +08:00
Mo Nuaimat 351a68831d Fix "Save as Script" option not available in ComfyUI v0.3.50 2025-08-27 21:36:29 -07:00
pydn 1c0585fabe Update version number 2025-07-27 19:09:46 -05:00
Peyton DeNiro 60d55d904b Merge pull request #125 from pydn/licence-update
Licence update
2025-07-27 19:06:38 -05:00
pydn 3a5ef069d9 Update .toml file to have MIT license 2025-07-27 18:56:13 -05:00
Peyton DeNiro 77a493f0eb Merge pull request #113 from ComfyNodePRs/update-publish-yaml
Update Github Action for Publishing to Comfy Registry
2025-07-27 18:32:41 -05:00
snomiao 2f6a649801 chore(publish): update GitHub Actions workflow for node publishing
- Added permissions for issue writing
- Updated action version to v1 for publish-node-action
- Added condition to run job only for 'pydn' repository owner
2025-01-25 07:27:37 +00:00
snomiao 4d0de66b0b chore(licence-update): Update PyProject Toml - License 2025-01-21 01:31:31 +00:00
Peyton DeNiro 0aa2747736 Merge pull request #95 from swilcock0/main
Modification to add menu item in new UI
2025-01-14 10:59:52 -06:00
Sam Wilcock 0351a2eaab Update save-as-script.js
Change icon
2025-01-07 00:23:46 +01:00
Sam Wilcock 4276ddbfb1 Update save-as-script.js 2025-01-07 00:19:50 +01:00
Sam Wilcock 0094c08d1c Update save-as-script.js
Add "Save as Script" item to new "Workflow" menu
2025-01-07 00:04:25 +01:00
Peyton DeNiro 14824e3473 Merge pull request #77 from pydn/kwargs-hotfix
Kwargs hotfix
2024-09-15 19:54:41 -05:00
pydn ea32a8fbbe Remove unneeded print statements. 2024-09-15 19:52:36 -05:00
pydn e7363070f1 Made updates to check for missing required arguments and for cases when unique id is passed through kwargs. 2024-09-15 19:48:43 -05:00
Peyton DeNiro 39c22295bc Merge pull request #52 from haohaocreates/publish
Add Github Action for Publishing to Comfy Registry
2024-09-15 10:25:17 -05:00
Peyton DeNiro 881e48a10c Merge pull request #76 from pydn/registry/pyproject
Registry/pyproject
2024-09-15 10:25:00 -05:00
pydn dc6b71b494 Updated .toml 2024-09-15 10:16:09 -05:00
Peyton DeNiro 4f689e1ad5 Merge pull request #75 from pydn/dev
Add UI button to save script directly form ComfyUI web app
2024-09-14 14:06:28 -05:00
pydn fa57d09325 Update readme to show latest changes for the last few releases. 2024-09-14 14:05:33 -05:00
Peyton DeNiro af9a0dfe75 Merge pull request #74 from pydn/atmaranto-main
Add UI button to save script directly form ComfyUI web app
2024-09-14 14:01:52 -05:00
pydn aa5416634c Small update to allow comfyui_to_python.py to run from any subdirectory inside of ComfyUI. 2024-09-14 13:43:10 -05:00
Peyton DeNiro d5b1896fec Merge branch 'dev' into atmaranto-main 2024-09-14 13:01:38 -05:00
pydn b360c53cea Add banner image. 2024-09-14 12:52:45 -05:00
pydn ef9637f257 Modifications to code generation match current state of main repo. 2024-09-14 12:50:21 -05:00
Peyton DeNiro d0c9b00336 Update README.md 2024-09-13 22:27:43 -05:00
pydn 14a9e6006b Add repo banner image. 2024-09-13 22:26:12 -05:00
Peyton DeNiro fb0edf881b Merge pull request #73 from pydn/catch_kwargs
Hotfix to pass all parameters to generated code if function allows kw…
2024-09-12 13:36:22 -05:00
pydn 14afc6640a Hotfix to pass all parameters to generated code if function allows kwargs 2024-09-12 13:35:44 -05:00
Peyton DeNiro 05f3a935ab Merge pull request #72 from pydn/pydn-patch-1
Update README.md to include CLI arguments
2024-09-12 12:33:02 -05:00
Peyton DeNiro 45157969a7 Update README.md to include CLI arguments 2024-09-12 12:32:40 -05:00
pydn da6c8df932 Merge branch 'dev' 2024-09-12 12:20:26 -05:00
pydn 20c19d76e1 Updated default queue size 2024-09-12 11:51:22 -05:00
Peyton DeNiro b6f2be020c Merge pull request #71 from pydn/dev
Add args to dynamically adjust input file, output file, and queue size
2024-09-12 11:29:22 -05:00
pydn d7b7b8492e Merge branch 'feat/callable_script' into dev 2024-09-12 11:25:16 -05:00
pydn 26f224fb4f Quick rename to better identify extension util file. 2024-09-11 13:59:37 -05:00
Peyton DeNiro 4a70486ec9 Merge pull request #70 from pydn/fix/custom_module_init
Hotfixes for latest ComfyUI release
2024-09-11 13:30:51 -05:00
Peyton DeNiro 2b9dfbef44 Merge branch 'main' into fix/custom_module_init 2024-09-11 13:30:36 -05:00
pydn c1550d86d1 Renamed utils to common to prevent future conflicts with latest comfyui version. 2024-09-11 13:24:09 -05:00
Peyton DeNiro aeecc9254f Merge pull request #69 from pydn/formatting-update
Reformatted code with black.
2024-09-11 12:30:07 -05:00
pydn 599bb7e1b0 Reformatted code with black. 2024-09-11 12:29:42 -05:00
Albert Purnama dbbc0d5217 Update init based on latest comfyui 2024-08-10 21:25:28 -07:00
atmaranto be237bd10a Ignore missing metadata 2024-05-23 17:59:23 -05:00
haohaocreates d938df9f4f Update pyproject.toml desc 2024-05-21 20:36:17 -04:00
haohaocreates 9ea8e993c4 chore(publish): Add Github Action for Publishing to Comfy Registry 2024-05-22 00:35:46 +00:00
haohaocreates cdca60ff04 chore(pyproject): Add pyproject.toml for Custom Node Registry 2024-05-22 00:35:46 +00:00
atmaranto 9694b84c9c Don't alter sys.argv when imported 2024-05-15 05:11:54 -05:00
atmaranto 26ed53e53b Don't send prompt, unique_id 2024-05-04 06:12:29 -05:00
atmaranto a548adf762 Add prompt data and handle kwargs 2024-05-04 05:56:30 -05:00
atmaranto fa4d3631f6 Set __main__ path to ComfyUI's main.py 2024-03-20 03:47:48 -05:00
atmaranto e53f61cc8c Update README.md 2024-03-19 17:35:55 -05:00
atmaranto e42b98048f Module API and --output 2024-03-19 01:27:41 -05:00
atmaranto fc76d349eb Update comfyui_to_python_utils.py 2024-03-18 16:23:26 -05:00
atmaranto a0f665c5f6 Allow argument to determine save location 2024-03-18 16:12:13 -05:00
atmaranto 8c7cbf6ea3 Only randomize seed if it's a set value 2024-03-10 05:51:38 -05:00
atmaranto 507e096676 Fixed code block 2024-03-10 04:41:12 -05:00
atmaranto 8f0a47307e Update README, add template args 2024-03-09 05:59:41 -06:00
atmaranto 0b4788d2af Bugfixes for args, updated the README 2024-03-09 05:32:08 -06:00
atmaranto 793854d21b Fixed Windows paths, missing inputs used as args 2024-03-09 04:59:32 -06:00
atmaranto f0bc65ed1b Fix punctuation 2024-03-07 05:35:22 -06:00
atmaranto 8f71ab9c39 Update usage info 2024-03-07 05:34:53 -06:00
atmaranto ac6e6b660a Fix const/var 2024-03-07 05:28:38 -06:00
atmaranto 58ea247a16 Update README.md 2024-03-07 04:50:38 -06:00
atmaranto 38d941b829 Update README.md 2024-03-07 04:49:33 -06:00
atmaranto 5b00e27355 Alphabetize Names 2024-03-07 04:48:28 -06:00
atmaranto 5564e0b313 Add TODO and fork status 2024-03-07 04:43:52 -06:00
atmaranto 16b4858b48 Fix CLI bugs 2024-03-07 04:43:42 -06:00
atmaranto 77cd75a332 Download as Python file
Added fork warning
2024-03-07 04:41:54 -06:00
atmaranto 3ad6de7ce4 Updated README and comments 2024-03-07 04:09:07 -06:00
atmaranto 5fe795c13e Reduce requirements, use install script 2024-03-07 03:59:46 -06:00
atmaranto 9aaee0bc8c Initial SaveAsScript changes 2024-03-07 03:45:37 -06:00
Omer Lerinman 334a4c48ad feat: make the script callable; make the input and output files configurable 2023-11-14 15:00:47 +02:00
Peyton DeNiro a1d651b61d Merge pull request #12 from pydn/dev
Dev
2023-08-25 16:15:49 -05:00
pydn 9549e3fe7a Updated docstring.: 2023-08-25 11:29:09 -05:00
pydn 1430658064 Added docstring. 2023-08-24 22:23:16 -05:00
Peyton 27f7a84eec Clean up load extra models code. 2023-08-24 21:38:50 -05:00
Peyton DeNiro ffe21d24c3 Merge pull request #9 from dimtoneff/add_extra_model_paths
Added a function to load the directories in the optional file: extra_model_paths.yaml
2023-08-23 14:19:27 -05:00
Peyton DeNiro baf5e91a00 Merge pull request #11 from rossaai/main
[Fix] Clean the variable names to avoid conflict
2023-08-23 14:15:03 -05:00
Felipe Murguia c0830e58bc [Fix] Cleans the variable name to avoid comflict characters and numbers. #10 2023-08-23 09:18:08 -06:00
Dimitar Tonev bfa3160bea revert back the queue size 2023-08-22 21:17:42 +02:00
Dimitar Tonev 2a819fabf0 Clean up the code a bit 2023-08-22 21:16:26 +02:00
Dimitar Tonev c01ac61bc1 Added a function to load the directories in the optional file: extra_model_paths.yaml 2023-08-21 22:50:24 +02:00
Peyton DeNiro 48248e0330 Merge pull request #7 from pydn/pydn-patch-1
Update README.md
2023-08-20 09:59:08 -05:00
Peyton DeNiro f4050b2466 Update README.md 2023-08-20 09:58:13 -05:00
Peyton DeNiro 18fea66efd Merge pull request #6 from pydn/2023-08-10_custom_node_support
Add support for custom nodes
2023-08-19 21:40:06 -05:00
Peyton 66e2778ff0 Merge branch '2023-08-10_custom_node_support' of https://github.com/pydn/ComfyUI-to-Python-Extension into 2023-08-10_custom_node_support 2023-08-19 21:19:03 -05:00
Peyton DeNiro d86c480797 Update README.md 2023-08-19 21:18:29 -05:00
Peyton 22ed131160 Merge branch '2023-08-10_custom_node_support' of https://github.com/pydn/ComfyUI-to-Python-Extension into 2023-08-10_custom_node_support 2023-08-19 21:15:41 -05:00
Peyton 7a85aade18 Updated import order. 2023-08-19 21:15:30 -05:00
Peyton DeNiro e6587d82f7 Update README.md 2023-08-19 21:14:37 -05:00
Peyton DeNiro b58cb3c189 Update README.md 2023-08-19 21:05:28 -05:00
Peyton DeNiro c9a02f6eff Update README.md 2023-08-19 20:50:50 -05:00
Peyton 203a675d6b Changed default parameters back. 2023-08-19 20:49:16 -05:00
Peyton 95ed7d5426 Added logic to deal with hidden variables. 2023-08-19 20:48:23 -05:00
Peyton a26219b639 Hotfix for node class names with trailing white spaces. Removed the need to import functions from the utils file when running the generated code. 2023-08-19 19:41:29 -05:00
Peyton e46283db70 Major refactor to break up code into smaller, task based classes. 2023-08-19 18:07:45 -05:00
Peyton d25223d65b First draft of major code refactoring and adding support for custom nodes. 2023-08-19 17:27:42 -05:00
11 changed files with 923 additions and 300 deletions
+25
View File
@@ -0,0 +1,25 @@
name: Publish to Comfy registry
on:
workflow_dispatch:
push:
branches:
- main
paths:
- "pyproject.toml"
permissions:
issues: write
jobs:
publish-node:
name: Publish Custom Node to registry
runs-on: ubuntu-latest
if: ${{ github.repository_owner == 'pydn' }}
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Publish Custom Node
uses: Comfy-Org/publish-node-action@v1
with:
## Add your own personal access token to your Github Repository secrets and reference it here.
personal_access_token: ${{ secrets.REGISTRY_ACCESS_TOKEN }}
+53 -12
View File
@@ -1,6 +1,7 @@
## ComfyUI-to-Python-Extension
![banner](images/comfyui_to_python_banner.png)
The `ComfyUI-to-Python-Extension` is a powerful tool that translates [ComfyUI](https://github.com/comfyanonymous/ComfyUI) workflows 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.
The `ComfyUI-to-Python-Extension` is a powerful tool that translates [ComfyUI](https://github.com/comfyanonymous/ComfyUI) workflows 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.
**Convert this:**
@@ -109,11 +110,33 @@ def main():
if __name__ == "__main__":
main()
```
## Potential Use Cases
- Streamlining the process for creating a lean app or pipeline deployment that uses a ComfyUI workflow
- Creating programmatic experiments for various prompt/parameter values
- Creating large queues for image generation (For example, you could adjust the script to generate 1000 images without clicking ctrl+enter 1000 times)
- Easily expanding or iterating on your architecture in Python once a foundational workflow is in place in the GUI
## Usage
## V1.3.0 Release Notes
- Generate .py file directly from the ComfyUI Web App
![Save As Script](images/save_as_script.png)
## V1.2.1 Release Notes
- Dynamically change `comfyui_to_python.py` parameters with CLI arguments
- Hotfix to handle nodes that accept kwargs.
## V1.2.0 Release Notes
- Updates to adhere to latest changes from `ComfyUI`
## V1.0.0 Release Notes
- **Use all the custom nodes!**
- Custom nodes are now supported. If you run into any issues with code execution, first ensure that the each node works as expected in the GUI. If it works in the GUI, but not in the generated script, please submit an issue.
1. Navigate to your `ComfyUI` directory
## Installation
1. Navigate to your `ComfyUI/custom_nodes` directory
2. Clone this repo
```bash
@@ -124,8 +147,8 @@ if __name__ == "__main__":
```
/comfy
/comfy_extras
/ComfyUI-to-Python-Extension
/custom_nodes
--/ComfyUI-to-Python-Extension
/input
/models
/output
@@ -146,26 +169,44 @@ if __name__ == "__main__":
server.py
```
3. Navigate to the `ComfyUI-to-Python-Extension` folder and install requirements
## Web App Use
1. Launch ComfyUI
2. Load your favorite workflow and click `Save As Script`
![Save As Script](images/save_as_script.png)
3. Type your desired file name into the pop up screen.
4. Move .py file from your downloads folder to your `ComfyUI` directory.
5. Now you can execute the newly created .py file to generate images without launching a server.
## CLI Usage
1. Navigate to the `ComfyUI-to-Python-Extension` folder and install requirements
```bash
pip install -r requirements.txt
```
4. Launch ComfyUI, click the gear icon over `Queue Prompt`, then check `Enable Dev mode Options`. **THE SCRIPT WILL NOT WORK IF YOU DO NOT ENABLE THIS OPTION!**
2. Launch ComfyUI, click the gear icon over `Queue Prompt`, then check `Enable Dev mode Options`. **THE SCRIPT WILL NOT WORK IF YOU DO NOT ENABLE THIS OPTION!**
![Enable Dev Mode Options](images/dev_mode_options.jpg)
5. Load up your favorite workflows, then click the newly enabled `Save (API Format)` button under Queue Prompt
3. Load up your favorite workflows, then click the newly enabled `Save (API Format)` button under Queue Prompt
6. Move the downloaded .json workflow file to your `ComfyUI/ComfyUI-to-Python-Extension` folder
4. Move the downloaded .json workflow file to your `ComfyUI/ComfyUI-to-Python-Extension` folder
7. If needed, update the `input` variable at the bottom of `comfyui_to_python.py` to match the name of your .json workflow file. By default, the script will look for a file called `workflow_api.json`. You can also update the `queue_size` variable to your desired number of images that you want to generate in a single script execution. By default, the scripts will generate 10 images.
5. If needed, add arguments when executing `comfyui_to_python.py` to update the default `input_file` and `output_file` to match your .json workflow file and desired .py file name. By default, the script will look for a file called `workflow_api.json`. You can also update the `queue_size` variable to your desired number of images that you want to generate in a single script execution. By default, the scripts will generate 10 images. Run `python comfyui_to_python.py --help` for more details.
8. Run the script:
6a. Run the script with default arguments:
```bash
python comfyui_to_python.py
```
6b. Run the script with optional arguments:
```bash
python comfyui_to_python.py --input_file "workflow_api (2).json" --output_file my_workflow.py --queue_size 100
```
9. After running `comfyui_to_python.py`, a new .py file will be created in the current working directory that contains the same name as the `input` variable. If you made no changes, look for `workflow_api.py`.
7. After running `comfyui_to_python.py`, a new .py file will be created in the current working directory. If you made no changes, look for `workflow_api.py`.
10. Now you can execute the newly created .py file to generate images without launching a server.
8. Now you can execute the newly created .py file to generate images without launching a server.
+58
View File
@@ -0,0 +1,58 @@
import sys
import os
from io import StringIO
import traceback
from aiohttp import web
ext_dir = os.path.dirname(__file__)
sys.path.append(ext_dir)
try:
import black
except ImportError:
print("Unable to import requirements for ComfyUI-SaveAsScript.")
print("Installing...")
import importlib
spec = importlib.util.spec_from_file_location(
"impact_install", os.path.join(os.path.dirname(__file__), "install.py")
)
impact_install = importlib.util.module_from_spec(spec)
spec.loader.exec_module(impact_install)
print("Successfully installed. Hopefully, at least.")
# Prevent reimporting of custom nodes
os.environ["RUNNING_IN_COMFYUI"] = "TRUE"
from comfyui_to_python import ComfyUItoPython
sys.path.append(os.path.dirname(os.path.dirname(ext_dir)))
import server
WEB_DIRECTORY = "js"
NODE_CLASS_MAPPINGS = {}
@server.PromptServer.instance.routes.post("/saveasscript")
async def save_as_script(request):
try:
data = await request.json()
name = data["name"]
workflow = data["workflow"]
sio = StringIO()
ComfyUItoPython(workflow=workflow, output_file=sio)
sio.seek(0)
data = sio.read()
return web.Response(text=data, status=200)
except Exception as e:
traceback.print_exc()
return web.Response(text=str(e), status=500)
+585 -276
View File
@@ -1,91 +1,128 @@
import copy
import glob
import inspect
import json
import logging
import os
import random
import sys
import re
from typing import Dict, List, Any, Callable, Tuple, TextIO
from argparse import ArgumentParser
import black
from typing import Dict, List, Any, Callable
sys.path.append('../')
# Add current directory to Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from comfyui_to_python_utils import (
import_custom_nodes,
find_path,
add_comfyui_directory_to_sys_path,
add_extra_model_paths,
get_value_at_index,
)
add_comfyui_directory_to_sys_path()
from nodes import NODE_CLASS_MAPPINGS
logging.basicConfig(level=logging.INFO)
DEFAULT_INPUT_FILE = "workflow_api.json"
DEFAULT_OUTPUT_FILE = "workflow_api.py"
DEFAULT_QUEUE_SIZE = 10
def read_json_file(file_path: str) -> dict:
"""
Reads a JSON file and returns its contents as a dictionary.
class FileHandler:
"""Handles reading and writing files.
Args:
file_path (str): The path to the JSON file.
Returns:
dict: The contents of the JSON file as a dictionary.
Raises:
FileNotFoundError: If the file is not found, it lists all JSON files in the directory of the file path.
ValueError: If the file is not a valid JSON.
This class provides methods to read JSON data from an input file and write code to an output file.
"""
try:
with open(file_path, 'r') as file:
@staticmethod
def read_json_file(file_path: str | TextIO, encoding: str = "utf-8") -> dict:
"""
Reads a JSON file and returns its contents as a dictionary.
Args:
file_path (str): The path to the JSON file.
Returns:
dict: The contents of the JSON file as a dictionary.
Raises:
FileNotFoundError: If the file is not found, it lists all JSON files in the directory of the file path.
ValueError: If the file is not a valid JSON.
"""
if hasattr(file_path, "read"):
return json.load(file_path)
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
return data
except FileNotFoundError:
# Get the directory from the file_path
directory = os.path.dirname(file_path)
@staticmethod
def write_code_to_file(file_path: str | TextIO, code: str) -> None:
"""Write the specified code to a Python file.
# If the directory is an empty string (which means file is in the current directory),
# get the current working directory
if not directory:
directory = os.getcwd()
Args:
file_path (str): The path to the Python file.
code (str): The code to write to the file.
# Find all JSON files in the directory
json_files = glob.glob(f"{directory}/*.json")
# Format the list of JSON files as a string
json_files_str = "\n".join(json_files)
raise FileNotFoundError(f"\n\nFile not found: {file_path}. JSON files in the directory:\n{json_files_str}")
except json.JSONDecodeError:
raise ValueError(f"Invalid JSON format in file: {file_path}")
def determine_load_order(data: Dict) -> List:
"""
Determine the load order of each key in the provided dictionary. This code will place the
nodes without node dependencies first, then ensure that any node whose result is used
in another node will be added to the list in the order it should be executed.
Args:
data (Dict):
The dictionary for which to determine the load order.
Returns:
List:
A list of tuples where each tuple contains a key, its corresponding dictionary,
and a boolean indicating whether or not the function is dependent on the output of
a previous function, ordered by load order.
"""
# Create a dictionary to keep track of visited nodes.
visited = {}
# Create a list to store the load order for functions
load_order = []
# Boolean to indicate whether or not the class is a loader class that should not be
# reloaded during every loop
is_loader = False
def dfs(key: str) -> None:
Returns:
None
"""
Depth-First Search function.
if isinstance(file_path, str):
# Extract directory from the filename
directory = os.path.dirname(file_path)
# If the directory does not exist, create it
if directory and not os.path.exists(directory):
os.makedirs(directory)
# Save the code to a .py file
with open(file_path, "w", encoding="utf-8") as file:
file.write(code)
else:
file_path.write(code)
class LoadOrderDeterminer:
"""Determine the load order of each key in the provided dictionary.
This class places the nodes without node dependencies first, then ensures that any node whose
result is used in another node will be added to the list in the order it should be executed.
Attributes:
data (Dict): The dictionary for which to determine the load order.
node_class_mappings (Dict): Mappings of node classes.
"""
def __init__(self, data: Dict, node_class_mappings: Dict):
"""Initialize the LoadOrderDeterminer with the given data and node class mappings.
Args:
data (Dict): The dictionary for which to determine the load order.
node_class_mappings (Dict): Mappings of node classes.
"""
self.data = data
self.node_class_mappings = node_class_mappings
self.visited = {}
self.load_order = []
self.is_special_function = False
def determine_load_order(self) -> List[Tuple[str, Dict, bool]]:
"""Determine the load order for the given data.
Returns:
List[Tuple[str, Dict, bool]]: A list of tuples representing the load order.
"""
self._load_special_functions_first()
self.is_special_function = False
for key in self.data:
if key not in self.visited:
self._dfs(key)
return self.load_order
def _dfs(self, key: str) -> None:
"""Depth-First Search function to determine the load order.
Args:
key (str): The key from which to start the DFS.
@@ -94,240 +131,512 @@ def determine_load_order(data: Dict) -> List:
None
"""
# Mark the node as visited.
visited[key] = True
inputs = data[key]['inputs']
self.visited[key] = True
inputs = self.data[key]["inputs"]
# Loop over each input key.
for input_key, val in inputs.items():
# If the value is a list and the first item in the list (which should be a key)
# has not been visited yet, then recursively apply dfs on the dependency.
if isinstance(val, list) and val[0] not in visited:
dfs(val[0])
# If the value is a list and the first item in the list has not been visited yet,
# then recursively apply DFS on the dependency.
if isinstance(val, list) and val[0] not in self.visited:
self._dfs(val[0])
# Add the key and its corresponding data to the load order list.
load_order.append((key, data[key], is_loader))
self.load_order.append((key, self.data[key], self.is_special_function))
# Load Loader keys first
for key in data:
class_def = NODE_CLASS_MAPPINGS[data[key]['class_type']]()
if class_def.CATEGORY == 'loaders' or class_def.FUNCTION in ['encode'] or not any(isinstance(val, list) for val in data[key]['inputs'].values()):
is_loader = True
# If the key has not been visited, perform a DFS from that key.
if key not in visited:
dfs(key)
def _load_special_functions_first(self) -> None:
"""Load functions without dependencies, loaderes, and encoders first.
# Reset is_loader bool
is_loader = False
# Loop over each key in the data.
for key in data:
# If the key has not been visited, perform a DFS from that key.
if key not in visited:
dfs(key)
return load_order
Returns:
None
"""
# Iterate over each key in the data to check for loader keys.
for key in self.data:
class_def = self.node_class_mappings[self.data[key]["class_type"]]()
# Check if the class is a loader class or meets specific conditions.
if (
class_def.CATEGORY == "loaders"
or class_def.FUNCTION in ["encode"]
or not any(
isinstance(val, list) for val in self.data[key]["inputs"].values()
)
):
self.is_special_function = True
# If the key has not been visited, perform a DFS from that key.
if key not in self.visited:
self._dfs(key)
def create_function_call_code(obj_name: str, func: str, variable_name: str, is_loader: bool, **kwargs) -> str:
"""
This function generates Python code for a function call.
class CodeGenerator:
"""Generates Python code for a workflow based on the load order.
Args:
obj_name (str): The name of the initialized object.
func (str): The function to be called.
variable_name (str): The name of the variable that the function result should be assigned to.
is_loader (bool): Determines the code indentation.
**kwargs: The keyword arguments for the function.
Returns:
str: The generated Python code.
Attributes:
node_class_mappings (Dict): Mappings of node classes.
base_node_class_mappings (Dict): Base mappings of node classes.
"""
def format_arg(key: str, value: any) -> str:
"""Formats arguments based on key and value."""
if key == 'noise_seed':
return f'{key}=random.randint(1, 2**64)'
def __init__(self, node_class_mappings: Dict, base_node_class_mappings: Dict):
"""Initialize the CodeGenerator with given node class mappings.
Args:
node_class_mappings (Dict): Mappings of node classes.
base_node_class_mappings (Dict): Base mappings of node classes.
"""
self.node_class_mappings = node_class_mappings
self.base_node_class_mappings = base_node_class_mappings
def generate_workflow(
self,
load_order: List,
queue_size: int = 10,
) -> str:
"""Generate the execution code based on the load order.
Args:
load_order (List): A list of tuples representing the load order.
queue_size (int): The number of photos that will be created by the script.
Returns:
str: Generated execution code as a string.
"""
# Create the necessary data structures to hold imports and generated code
import_statements, executed_variables, special_functions_code, code = (
set(["NODE_CLASS_MAPPINGS"]),
{},
[],
[],
)
# This dictionary will store the names of the objects that we have already initialized
initialized_objects = {}
custom_nodes = False
# Loop over each dictionary in the load order list
for idx, data, is_special_function in load_order:
# Generate class definition and inputs from the data
inputs, class_type = data["inputs"], data["class_type"]
input_types = self.node_class_mappings[class_type].INPUT_TYPES()
class_def = self.node_class_mappings[class_type]()
# If required inputs are not present, skip the node as it will break the code if passed through to the script
missing_required_variable = False
if "required" in input_types.keys():
for required in input_types["required"]:
if required not in inputs.keys():
missing_required_variable = True
if missing_required_variable:
continue
# If the class hasn't been initialized yet, initialize it and generate the import statements
if class_type not in initialized_objects:
# No need to use preview image nodes since we are executing the script in a terminal
if class_type == "PreviewImage":
continue
class_type, import_statement, class_code = self.get_class_info(
class_type
)
initialized_objects[class_type] = self.clean_variable_name(class_type)
if class_type in self.base_node_class_mappings.keys():
import_statements.add(import_statement)
if class_type not in self.base_node_class_mappings.keys():
custom_nodes = True
special_functions_code.append(class_code)
# Get all possible parameters for class_def
class_def_params = self.get_function_parameters(
getattr(class_def, class_def.FUNCTION)
)
no_params = class_def_params is None
# Remove any keyword arguments from **inputs if they are not in class_def_params
inputs = {
key: value
for key, value in inputs.items()
if no_params or key in class_def_params
}
# Deal with hidden variables
if (
"hidden" in input_types.keys()
and "unique_id" in input_types["hidden"].keys()
):
inputs["unique_id"] = random.randint(1, 2**64)
elif class_def_params is not None:
if "unique_id" in class_def_params:
inputs["unique_id"] = random.randint(1, 2**64)
# Create executed variable and generate code
executed_variables[idx] = f"{self.clean_variable_name(class_type)}_{idx}"
inputs = self.update_inputs(inputs, executed_variables)
if is_special_function:
special_functions_code.append(
self.create_function_call_code(
initialized_objects[class_type],
class_def.FUNCTION,
executed_variables[idx],
is_special_function,
**inputs,
)
)
else:
code.append(
self.create_function_call_code(
initialized_objects[class_type],
class_def.FUNCTION,
executed_variables[idx],
is_special_function,
**inputs,
)
)
# Generate final code by combining imports and code, and wrap them in a main function
final_code = self.assemble_python_code(
import_statements, special_functions_code, code, queue_size, custom_nodes
)
return final_code
def create_function_call_code(
self,
obj_name: str,
func: str,
variable_name: str,
is_special_function: bool,
**kwargs,
) -> str:
"""Generate Python code for a function call.
Args:
obj_name (str): The name of the initialized object.
func (str): The function to be called.
variable_name (str): The name of the variable that the function result should be assigned to.
is_special_function (bool): Determines the code indentation.
**kwargs: The keyword arguments for the function.
Returns:
str: The generated Python code.
"""
args = ", ".join(self.format_arg(key, value) for key, value in kwargs.items())
# Generate the Python code
code = f"{variable_name} = {obj_name}.{func}({args})\n"
# If the code contains dependencies and is not a loader or encoder, indent the code because it will be placed inside
# of a for loop
if not is_special_function:
code = f"\t{code}"
return code
def format_arg(self, key: str, value: any) -> str:
"""Formats arguments based on key and value.
Args:
key (str): Argument key.
value (any): Argument value.
Returns:
str: Formatted argument as a string.
"""
if key == "noise_seed" or key == "seed":
return f"{key}=random.randint(1, 2**64)"
elif isinstance(value, str):
value = value.replace("\n", "\\n")
value = value.replace("\n", "\\n").replace('"', "'")
return f'{key}="{value}"'
elif isinstance(value, dict) and 'variable_name' in value:
elif isinstance(value, dict) and "variable_name" in value:
return f'{key}={value["variable_name"]}'
return f'{key}={value}'
return f"{key}={value}"
args = ', '.join(format_arg(key, value) for key, value in kwargs.items())
def assemble_python_code(
self,
import_statements: set,
speical_functions_code: List[str],
code: List[str],
queue_size: int,
custom_nodes=False,
) -> str:
"""Generates the final code string.
# Generate the Python code
code = f'{variable_name} = {obj_name}.{func}({args})\n'
Args:
import_statements (set): A set of unique import statements.
speical_functions_code (List[str]): A list of special functions code strings.
code (List[str]): A list of code strings.
queue_size (int): Number of photos that will be generated by the script.
custom_nodes (bool): Whether to include custom nodes in the code.
# If the code contains dependencies, indent the code because it will be placed inside
# of a for loop
if not is_loader:
code = f'\t{code}'
return code
def update_inputs(inputs: Dict, executed_variables: Dict) -> Dict:
"""
Update inputs based on the executed variables.
Args:
inputs (Dict): Inputs dictionary to update.
executed_variables (Dict): Dictionary storing executed variable names.
Returns:
Dict: Updated inputs dictionary.
"""
for key in inputs.keys():
if isinstance(inputs[key], list) and inputs[key][0] in executed_variables.keys():
inputs[key] = {'variable_name': f"{executed_variables[inputs[key][0]]}[{inputs[key][1]}]"}
return inputs
def get_class_info(class_type: str) -> (str, str, str):
"""
Generates and returns necessary information about class type.
Args:
class_type (str): Class type
Returns:
class_type (str): Updated class type
import_statement (str): Import statement string
class_code (str): Class initialization code
"""
import_statement = class_type
class_code = f'{class_type.lower()} = {class_type}()'
return class_type, import_statement, class_code
def assemble_python_code(import_statements: set, loader_code: List[str], code: List[str], queue_size: int) -> str:
"""
Generates final code string.
Args:
import_statements (set): A set of unique import statements
code (List[str]): A list of code strings
queue_size (int): Number of photos that will be generated by the script.
Returns:
final_code (str): Generated final code as a string
"""
static_imports = ['import random', 'import torch']
imports_code = [f"from nodes import {', '.join([class_name for class_name in import_statements])}" ]
main_function_code = f"def main():\n\t" + 'with torch.inference_mode():\n\t\t' + '\n\t\t'.join(loader_code) + f'\n\n\t\tfor q in range({queue_size}):\n\t\t' + '\n\t\t'.join(code)
final_code = '\n'.join(static_imports + ['import sys\nsys.path.append("../")'] + imports_code + ['', main_function_code, '', 'if __name__ == "__main__":', '\tmain()'])
final_code = black.format_str(final_code, mode=black.Mode())
return final_code
def write_code_to_file(filename: str, code: str) -> None:
"""
Writes given code to a .py file. If the directory does not exist, it creates it.
Args:
filename (str): The name of the Python file to save the code to.
code (str): The code to save.
"""
# Extract directory from the filename
directory = os.path.dirname(filename)
# If the directory does not exist, create it
if directory and not os.path.exists(directory):
os.makedirs(directory)
# Save the code to a .py file
with open(filename, 'w') as file:
file.write(code)
def get_function_parameters(func: Callable) -> List:
"""Get the names of a function's parameters.
Args:
func (Callable): The function whose parameters we want to inspect.
Returns:
List: A list containing the names of the function's parameters.
"""
signature = inspect.signature(func)
parameters = {name: param.default if param.default != param.empty else None
for name, param in signature.parameters.items()}
return list(parameters.keys())
def generate_workflow(load_order: List, filename: str = 'generated_code_workflow.py', queue_size: int = 10) -> str:
"""
Generate the execution code based on the load order.
Args:
load_order (List): A list of tuples representing the load order.
filename (str): The name of the Python file to which the code should be saved.
Defaults to 'generated_code_workflow.py'.
queue_size (int): The number of photos that will be created by the script.
Returns:
str: Generated execution code as a string.
"""
# Create the necessary data structures to hold imports and generated code
import_statements, executed_variables, loader_code, code = set(), {}, [], []
# This dictionary will store the names of the objects that we have already initialized
initialized_objects = {}
# Loop over each dictionary in the load order list
for idx, data, is_loader in load_order:
# Generate class definition and inputs from the data
inputs, class_type = data['inputs'], data['class_type']
class_def = NODE_CLASS_MAPPINGS[class_type]()
# If the class hasn't been initialized yet, initialize it and generate the import statements
if class_type not in initialized_objects:
class_type, import_statement, class_code = get_class_info(class_type)
initialized_objects[class_type] = class_type.lower()
import_statements.add(import_statement)
loader_code.append(class_code)
# Get all possible parameters for class_def
class_def_params = get_function_parameters(getattr(class_def, class_def.FUNCTION))
# Remove any keyword arguments from **inputs if they are not in class_def_params
inputs = {key: value for key, value in inputs.items() if key in class_def_params}
# Create executed variable and generate code
executed_variables[idx] = f'{class_type.lower()}_{idx}'
inputs = update_inputs(inputs, executed_variables)
if is_loader:
loader_code.append(create_function_call_code(initialized_objects[class_type], class_def.FUNCTION, executed_variables[idx], is_loader, **inputs))
Returns:
str: Generated final code as a string.
"""
# Get the source code of the utils functions as a string
func_strings = []
for func in [
get_value_at_index,
find_path,
add_comfyui_directory_to_sys_path,
add_extra_model_paths,
]:
func_strings.append(f"\n{inspect.getsource(func)}")
# Define static import statements required for the script
static_imports = (
[
"import os",
"import random",
"import sys",
"from typing import Sequence, Mapping, Any, Union",
"import torch",
]
+ func_strings
+ ["\n\nadd_comfyui_directory_to_sys_path()\nadd_extra_model_paths()\n"]
)
# Check if custom nodes should be included
if custom_nodes:
static_imports.append(f"\n{inspect.getsource(import_custom_nodes)}\n")
custom_nodes = "import_custom_nodes()\n\t"
else:
code.append(create_function_call_code(initialized_objects[class_type], class_def.FUNCTION, executed_variables[idx], is_loader, **inputs))
custom_nodes = ""
# Create import statements for node classes
imports_code = [
f"from nodes import {', '.join([class_name for class_name in import_statements])}"
]
# Assemble the main function code, including custom nodes if applicable
main_function_code = (
"def main():\n\t"
+ f"{custom_nodes}with torch.inference_mode():\n\t\t"
+ "\n\t\t".join(speical_functions_code)
+ f"\n\n\t\tfor q in range({queue_size}):\n\t\t"
+ "\n\t\t".join(code)
)
# Concatenate all parts to form the final code
final_code = "\n".join(
static_imports
+ imports_code
+ ["", main_function_code, "", 'if __name__ == "__main__":', "\tmain()"]
)
# Format the final code according to PEP 8 using the Black library
final_code = black.format_str(final_code, mode=black.Mode())
# Generate final code by combining imports and code, and wrap them in a main function
final_code = assemble_python_code(import_statements, loader_code, code, queue_size)
return final_code
# Save the code to a .py file
write_code_to_file(filename, final_code)
def get_class_info(self, class_type: str) -> Tuple[str, str, str]:
"""Generates and returns necessary information about class type.
return final_code
Args:
class_type (str): Class type.
Returns:
Tuple[str, str, str]: Updated class type, import statement string, class initialization code.
"""
import_statement = class_type
variable_name = self.clean_variable_name(class_type)
if class_type in self.base_node_class_mappings.keys():
class_code = f"{variable_name} = {class_type.strip()}()"
else:
class_code = f'{variable_name} = NODE_CLASS_MAPPINGS["{class_type}"]()'
return class_type, import_statement, class_code
@staticmethod
def clean_variable_name(class_type: str) -> str:
"""
Remove any characters from variable name that could cause errors running the Python script.
Args:
class_type (str): Class type.
Returns:
str: Cleaned variable name with no special characters or spaces
"""
# Convert to lowercase and replace spaces with underscores
clean_name = class_type.lower().strip().replace("-", "_").replace(" ", "_")
# Remove characters that are not letters, numbers, or underscores
clean_name = re.sub(r"[^a-z0-9_]", "", clean_name)
# Ensure that it doesn't start with a number
if clean_name[0].isdigit():
clean_name = "_" + clean_name
return clean_name
def get_function_parameters(self, func: Callable) -> List:
"""Get the names of a function's parameters.
Args:
func (Callable): The function whose parameters we want to inspect.
Returns:
List: A list containing the names of the function's parameters.
"""
signature = inspect.signature(func)
parameters = {
name: param.default if param.default != param.empty else None
for name, param in signature.parameters.items()
}
catch_all = any(
param.kind == inspect.Parameter.VAR_KEYWORD
for param in signature.parameters.values()
)
return list(parameters.keys()) if not catch_all else None
def update_inputs(self, inputs: Dict, executed_variables: Dict) -> Dict:
"""Update inputs based on the executed variables.
Args:
inputs (Dict): Inputs dictionary to update.
executed_variables (Dict): Dictionary storing executed variable names.
Returns:
Dict: Updated inputs dictionary.
"""
for key in inputs.keys():
if (
isinstance(inputs[key], list)
and inputs[key][0] in executed_variables.keys()
):
inputs[key] = {
"variable_name": f"get_value_at_index({executed_variables[inputs[key][0]]}, {inputs[key][1]})"
}
return inputs
def main(input, queue_size=10):
class ComfyUItoPython:
"""Main workflow to generate Python code from a workflow_api.json file.
Attributes:
input_file (str): Path to the input JSON file.
output_file (str): Path to the output Python file.
queue_size (int): The number of photos that will be created by the script.
node_class_mappings (Dict): Mappings of node classes.
base_node_class_mappings (Dict): Base mappings of node classes.
"""
Main function to be executed.
def __init__(
self,
workflow: str = "",
input_file: str = "",
output_file: str | TextIO = "",
queue_size: int = 1,
node_class_mappings: Dict = NODE_CLASS_MAPPINGS,
needs_init_custom_nodes: bool = False,
):
"""Initialize the ComfyUItoPython class with the given parameters. Exactly one of workflow or input_file must be specified.
Args:
workflow (str): The workflow's JSON.
input_file (str): Path to the input JSON file.
output_file (str | TextIO): Path to the output file or a file-like object.
queue_size (int): The number of times a workflow will be executed by the script. Defaults to 1.
node_class_mappings (Dict): Mappings of node classes. Defaults to NODE_CLASS_MAPPINGS.
needs_init_custom_nodes (bool): Whether to initialize custom nodes. Defaults to False.
"""
if input_file and workflow:
raise ValueError("Can't provide both input_file and workflow")
elif not input_file and not workflow:
raise ValueError("Needs input_file or workflow")
if not output_file:
raise ValueError("Needs output_file")
self.workflow = workflow
self.input_file = input_file
self.output_file = output_file
self.queue_size = queue_size
self.node_class_mappings = node_class_mappings
self.needs_init_custom_nodes = needs_init_custom_nodes
self.base_node_class_mappings = copy.deepcopy(self.node_class_mappings)
self.execute()
def execute(self):
"""Execute the main workflow to generate Python code.
Returns:
None
"""
# Step 1: Import all custom nodes if we need to
if self.needs_init_custom_nodes:
import_custom_nodes()
else:
# If they're already imported, we don't know which nodes are custom nodes, so we need to import all of them
self.base_node_class_mappings = {}
# Step 2: Read JSON data from the input file
if self.input_file:
data = FileHandler.read_json_file(self.input_file)
else:
data = json.loads(self.workflow)
# Step 3: Determine the load order
load_order_determiner = LoadOrderDeterminer(data, self.node_class_mappings)
load_order = load_order_determiner.determine_load_order()
# Step 4: Generate the workflow code
code_generator = CodeGenerator(
self.node_class_mappings, self.base_node_class_mappings
)
generated_code = code_generator.generate_workflow(
load_order, queue_size=self.queue_size
)
# Step 5: Write the generated code to a file
FileHandler.write_code_to_file(self.output_file, generated_code)
print(f"Code successfully generated and written to {self.output_file}")
def run(
input_file: str = DEFAULT_INPUT_FILE,
output_file: str = DEFAULT_OUTPUT_FILE,
queue_size: int = DEFAULT_QUEUE_SIZE,
) -> None:
"""Generate Python code from a ComfyUI workflow_api.json file.
Args:
input_file (str): Path to the input JSON file. Defaults to "workflow_api.json".
output_file (str): Path to the output Python file.
Defaults to "workflow_api.py".
queue_size (int): The number of times a workflow will be executed by the script.
Defaults to 1.
Returns:
None
"""
# Load JSON data from the input file
prompt = read_json_file(input)
load_order = determine_load_order(prompt)
output_file = input.replace('.json', '.py')
code = generate_workflow(load_order, filename=output_file, queue_size=queue_size)
logging.info(code)
ComfyUItoPython(
input_file=input_file,
output_file=output_file,
queue_size=queue_size,
needs_init_custom_nodes=True,
)
if __name__ == '__main__':
input = 'workflow_api.json'
queue_size = 10
main(input, queue_size)
def main() -> None:
"""Main function to generate Python code from a ComfyUI workflow_api.json file."""
parser = ArgumentParser(
description="Generate Python code from a ComfyUI workflow_api.json file."
)
parser.add_argument(
"-f",
"--input_file",
type=str,
help="path to the input JSON file",
default=DEFAULT_INPUT_FILE,
)
parser.add_argument(
"-o",
"--output_file",
type=str,
help="path to the output Python file",
default=DEFAULT_OUTPUT_FILE,
)
parser.add_argument(
"-q",
"--queue_size",
type=int,
help="number of times the workflow will be executed by default",
default=DEFAULT_QUEUE_SIZE,
)
pargs = parser.parse_args()
run(**vars(pargs))
print("Done.")
if __name__ == "__main__":
"""Run the main function."""
main()
+107
View File
@@ -0,0 +1,107 @@
import os
from typing import Sequence, Mapping, Any, Union
import sys
def import_custom_nodes() -> None:
"""Find all custom nodes in the custom_nodes folder and add those node objects to NODE_CLASS_MAPPINGS
This function sets up a new asyncio event loop, initializes the PromptServer,
creates a PromptQueue, and initializes the custom nodes.
"""
import asyncio
import execution
from nodes import init_extra_nodes
sys.path.insert(0, find_path("ComfyUI"))
import server
# Creating a new event loop and setting it as the default loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Creating an instance of PromptServer with the loop
server_instance = server.PromptServer(loop)
execution.PromptQueue(server_instance)
# Initializing custom nodes
asyncio.run(init_extra_nodes())
def find_path(name: str, path: str = None) -> str:
"""
Recursively looks at parent folders starting from the given path until it finds the given name.
Returns the path as a Path object if found, or None otherwise.
"""
# If no path is given, use the current working directory
if path is None:
path = os.getcwd()
# Check if the current directory contains the name
if name in os.listdir(path):
path_name = os.path.join(path, name)
print(f"{name} found: {path_name}")
return path_name
# Get the parent directory
parent_directory = os.path.dirname(path)
# If the parent directory is the same as the current directory, we've reached the root and stop the search
if parent_directory == path:
return None
# Recursively call the function with the parent directory
return find_path(name, parent_directory)
def add_comfyui_directory_to_sys_path() -> None:
"""
Add 'ComfyUI' to the sys.path
"""
comfyui_path = find_path("ComfyUI")
if comfyui_path is not None and os.path.isdir(comfyui_path):
sys.path.append(comfyui_path)
print(f"'{comfyui_path}' added to sys.path")
def add_extra_model_paths() -> None:
"""
Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path.
"""
try:
from main import load_extra_path_config
except ImportError:
print(
"Could not import load_extra_path_config from main.py. Looking in utils.extra_config instead."
)
from utils.extra_config import load_extra_path_config
extra_model_paths = find_path("extra_model_paths.yaml")
if extra_model_paths is not None:
load_extra_path_config(extra_model_paths)
else:
print("Could not find the extra_model_paths config file.")
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
"""Returns the value at the given index of a sequence or mapping.
If the object is a sequence (like list or string), returns the value at the given index.
If the object is a mapping (like a dictionary), returns the value at the index-th key.
Some return a dictionary, in these cases, we look for the "results" key
Args:
obj (Union[Sequence, Mapping]): The object to retrieve the value from.
index (int): The index of the value to retrieve.
Returns:
Any: The value at the given index.
Raises:
IndexError: If the index is out of bounds for the object and the object is not a mapping.
"""
try:
return obj[index]
except KeyError:
return obj["result"][index]
Binary file not shown.

After

Width:  |  Height:  |  Size: 712 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

+20
View File
@@ -0,0 +1,20 @@
import os
import sys
from subprocess import Popen, check_output, PIPE
requirements = open(os.path.join(os.path.dirname(__file__), "requirements.txt")).read().split("\n")
installed_packages = check_output(
[sys.executable, "-m", "pip", "list"],
universal_newlines=True
).split("\n")
installed_packages = set([package.split(" ")[0].lower() for package in installed_packages if package.strip()])
for requirement in requirements:
if requirement.lower() not in installed_packages:
print(f"Installing requirements...")
Popen([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"], stdout=PIPE, stderr=PIPE, cwd=os.path.dirname(__file__)).communicate()
print(f"Installed.")
break
+60
View File
@@ -0,0 +1,60 @@
import { api } from "../../scripts/api.js";
import { app } from "../../scripts/app.js";
import { $el } from "../../scripts/ui.js";
const extension = {
name: "Comfy.SaveAsScript",
commands: [
{
id: "triggerSaveAsScript",
label: "Save As Script",
function: () => { extension.savePythonScript(); }
}
],
menuCommands: [
{
path: ["File"],
commands: ["triggerSaveAsScript"]
}
],
init() {
$el("style", {
parent: document.head,
});
},
savePythonScript() {
var filename = prompt("Save script as:");
if(filename === undefined || filename === null || filename === "") {
return
}
app.graphToPrompt().then(async (p) => {
const json = JSON.stringify({name: filename + ".json", workflow: JSON.stringify(p.output, null, 2)}, null, 2); // convert the data to a JSON string
var response = await api.fetchApi(`/saveasscript`, { method: "POST", body: json });
if(response.status == 200) {
const blob = new Blob([await response.text()], {type: "text/python;charset=utf-8"});
const url = URL.createObjectURL(blob);
if(!filename.endsWith(".py")) {
filename += ".py";
}
const a = $el("a", {
href: url,
download: filename,
style: {display: "none"},
parent: document.body,
});
a.click();
setTimeout(function () {
a.remove();
window.URL.revokeObjectURL(url);
}, 0);
}
});
},
async setup() {
console.log("SaveAsScript loaded");
}
};
app.registerExtension(extension);
+15
View File
@@ -0,0 +1,15 @@
[project]
name = "comfyui-to-python-extension"
description = "This custom node allows you to generate pure python code from your ComfyUI workflow with the click of a button. Great for rapid experimentation or production deployment."
version = "1.3.1"
license = { text = "MIT License" }
dependencies = ["black"]
[project.urls]
Repository = "https://github.com/pydn/ComfyUI-to-Python-Extension"
# Used by Comfy Registry https://comfyregistry.org
[tool.comfy]
PublisherId = "pydn"
DisplayName = "ComfyUI-to-Python-Extension"
Icon = ""
-12
View File
@@ -1,13 +1 @@
torch
torchdiffeq
torchsde
einops
transformers>=4.25.1
safetensors>=0.3.0
aiohttp
accelerate
pyyaml
Pillow
scipy
tqdm
black