Skip to content

Commit ef9637f

Browse files
committed
Modifications to code generation match current state of main repo.
1 parent be237bd commit ef9637f

File tree

5 files changed

+341
-452
lines changed

5 files changed

+341
-452
lines changed

LICENSE

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
The MIT License (MIT)
22

33
Copyright (c) 2023 Peyton DeNiro
4-
Copyright (c) 2024 Anthony Maranto
54

65
Permission is hereby granted, free of charge, to any person obtaining a copy
76
of this software and associated documentation files (the "Software"), to deal

README.md

+62-121
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,5 @@
1-
**Status: Fork in development**. It works on my machine. It might not on yours.
2-
Let me know of any major issues by submitting an Issue.
3-
I plan to do some fresh install testing when I have time.
4-
5-
## ComfyUI-SaveAsScript
6-
7-
ComfyUI-SaveAsScript (or SaS) is a fork of [ComfyUI-To-Python-Extension](https://github.com/pydn/ComfyUI-to-Python-Extension). It provides a more robust command-line interface and the ability to export your current workflow as a script directly from a button on the ComfyUI web interface.
8-
9-
Once exported, this script can be run to run the workflow without a frontend, or it can be imported and the `main()` function can be used to call the workflow programatically.
10-
11-
### New Feauture: Module Support
12-
13-
Now, scripts exported with SaS can be imported as modules! Once you have a script exported, you can use it like:
14-
```python
15-
>>> import exported_script
16-
>>> results = exported_script.main("A prompt that would be sent to the command-line arguments", queue_size=1)
17-
```
18-
19-
The first `SaveImage` node reached will instead *return* the output to the calling function.
20-
21-
### New Feature: Custom Output Path
22-
23-
When running the exported script normally, you can now specify an `--output` option that will override the default path of `SaveImage` nodes.
24-
If only a single image is exported by the node, then the path will be used verbatim. Otherwise, the path will be used as a prefix, and `_#####.png` will be appended
25-
to ensure uniqueness. Note that files *will be clobbered* if only one image is exported.
26-
If the path is a directory, the `SaveImage` node's `filename_prefix` will be used.
27-
28-
If `-` is selected as the output path, normal ComfyUI output will be piped to stderr and the resultant image will be piped to stdout, allowing one to use the script
29-
like:
30-
```bash
31-
python3 script.py "A painting of outer space" --output - --queue-size 1 > image.png
32-
```
33-
34-
### Usage (Web)
35-
36-
Upon installation, there will be a button labeled "Save as Script" on the interface, pictured below:
37-
38-
![A button that exports the current script on the ComfyUI](images/save_as_script.png)
39-
40-
Click that button and enter your preferred name for the downloaded script (extension added automatically), and your file will be downloaded.
41-
42-
Note that if you use the script this way, you do **not** need to enable developer mode. You *will* if you use the CLI (to get the API JSON).
43-
44-
### Usage (CLI)
45-
46-
Navigating to the extension directory, you can also use the basic CLI included in the script to save your script without changing the Python file.
47-
48-
```bash
49-
usage: comfyui_to_python.py [-h] [--output OUTPUT] [--queue-size QUEUE_SIZE] [--yes] workflow
50-
51-
Converts a ComfyUI-style workflow.json file to a Python file. Must have been exported with API calls
52-
53-
positional arguments:
54-
workflow The workflow.json file to convert
55-
56-
options:
57-
-h, --help show this help message and exit
58-
--output OUTPUT, -o OUTPUT
59-
The output file (defaults to [input file].py)
60-
--queue-size QUEUE_SIZE, -q QUEUE_SIZE
61-
The queue size per run
62-
--yes, --overwrite, -y
63-
Overwrite the output file if it exists
64-
```
65-
66-
### Arguments
67-
68-
It is now possible to pass command-line arguments to a generated script file. Any time a **required** input variable for *any* node in your the ComfyUI workflow is left unfilled, SaveAsScript will automatically convert that node into an argument.
69-
70-
For instance, if you have a simple default workflow, but have converted the text widget of the positive prompt into an input and left it unfilled like so:
71-
72-
![An altered version of the default ComfyUI workflow](images/default_altered.png)
73-
74-
Then the unfilled required variable will be available as an argument:
75-
```bash
76-
usage: default.py [-h] [--queue-size QUEUE_SIZE] [--comfyui-directory COMFYUI_DIRECTORY] text1
77-
78-
A converted ComfyUI workflow. Required inputs listed below. Values passed should be valid JSON (assumes string if not valid JSON).
79-
80-
positional arguments:
81-
text1 Argument 0, input `text` for node "CLIP Text Encode (Prompt)" id 6 (autogenerated)
82-
83-
options:
84-
-h, --help show this help message and exit
85-
--queue-size QUEUE_SIZE, -q QUEUE_SIZE
86-
How many times the workflow will be executed (default: 1)
87-
--comfyui-directory COMFYUI_DIRECTORY, -c COMFYUI_DIRECTORY
88-
Where to look for ComfyUI (default: current directory)
89-
--output OUTPUT, -o OUTPUT
90-
The location to save the output image. Either a file path, a directory, or - for stdout (default: the ComfyUI output directory)
91-
--disable-metadata Disables writing workflow metadata to the outputs
92-
```
93-
94-
Arguments are new. **If you have any suggestions on how to improve them or on how to effectively specify defaults in the workflow and override in the command-line**, feel free to suggest that in an Issue.
95-
96-
#### Passing Arguments to ComfyUI
97-
98-
In case you want to pass anything to the ComfyUI server as an argument, you can use `--` to indicate you're done with SaS arguments and are now passing ComfyUI arguments.
99-
For instance:
100-
101-
```bash
102-
python3 script.py "A painting of outer space" --queue-size 1 -- --cpu
103-
```
104-
105-
### Other Changes
106-
107-
#### Bugfixes
108-
- Windows paths are now properly escaped.
109-
- I also fixed what seemed to be a minor bug with exporting certain Crystools nodes, possibly due to their unusual name.
110-
111-
#### TODO
112-
- Improve compatibility with module API
113-
114-
## Old Description of ComfyUI-to-Python-Extension (usage altered)
1+
## ComfyUI-to-Python-Extension
2+
![banner](images/comfyui_to_python_banner.png)
1153

1164
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.
1175

@@ -236,10 +124,63 @@ if __name__ == "__main__":
236124
## Usage
237125

238126

239-
1. Install the custom_node (via git cloning or the Manager)
240-
241-
2. Click the button in the web UI OR run the CLI
242-
243-
3. Move the created .py file to your `ComfyUI` folder
244-
245-
4. Run the generated script!
127+
1. Navigate to your `ComfyUI` directory
128+
129+
2. Clone this repo
130+
```bash
131+
git clone https://github.com/pydn/ComfyUI-to-Python-Extension.git
132+
```
133+
134+
After cloning the repo, your `ComfyUI` directory should look like this:
135+
```
136+
/comfy
137+
/comfy_extras
138+
/ComfyUI-to-Python-Extension
139+
/custom_nodes
140+
/input
141+
/models
142+
/output
143+
/script_examples
144+
/web
145+
.gitignore
146+
LICENSE
147+
README.md
148+
comfyui_screenshot.png
149+
cuda_mollac.py
150+
execution.py
151+
extra_model_paths.yaml.example
152+
folder_paths.py
153+
latent_preview.py
154+
main.py
155+
nodes.py
156+
requirements.txt
157+
server.py
158+
```
159+
160+
3. Navigate to the `ComfyUI-to-Python-Extension` folder and install requirements
161+
```bash
162+
pip install -r requirements.txt
163+
```
164+
165+
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!**
166+
167+
![Enable Dev Mode Options](images/dev_mode_options.jpg)
168+
169+
5. Load up your favorite workflows, then click the newly enabled `Save (API Format)` button under Queue Prompt
170+
171+
6. Move the downloaded .json workflow file to your `ComfyUI/ComfyUI-to-Python-Extension` folder
172+
173+
7. 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.
174+
175+
8a. Run the script with default arguments:
176+
```bash
177+
python comfyui_to_python.py
178+
```
179+
8b. Run the script with optional arguments:
180+
```bash
181+
python comfyui_to_python.py --input_file "workflow_api (2).json" --output_file my_workflow.py --queue_size 100
182+
```
183+
184+
9. 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`.
185+
186+
10. Now you can execute the newly created .py file to generate images without launching a server.

__init__.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
print("Installing...")
1818

1919
import importlib
20-
spec = importlib.util.spec_from_file_location('impact_install', os.path.join(os.path.dirname(__file__), 'install.py'))
20+
21+
spec = importlib.util.spec_from_file_location(
22+
"impact_install", os.path.join(os.path.dirname(__file__), "install.py")
23+
)
2124
impact_install = importlib.util.module_from_spec(spec)
2225
spec.loader.exec_module(impact_install)
2326

@@ -35,19 +38,20 @@
3538
WEB_DIRECTORY = "js"
3639
NODE_CLASS_MAPPINGS = {}
3740

41+
3842
@server.PromptServer.instance.routes.post("/saveasscript")
3943
async def save_as_script(request):
4044
try:
4145
data = await request.json()
42-
name = data['name']
43-
workflow = data['workflow']
44-
46+
name = data["name"]
47+
workflow = data["workflow"]
48+
4549
sio = StringIO()
4650
ComfyUItoPython(workflow=workflow, output_file=sio)
47-
51+
4852
sio.seek(0)
4953
data = sio.read()
50-
54+
5155
return web.Response(text=data, status=200)
5256
except Exception as e:
5357
traceback.print_exc()

0 commit comments

Comments
 (0)