Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb0edf881b | ||
|
|
14afc6640a | ||
|
|
05f3a935ab | ||
|
|
45157969a7 | ||
|
|
da6c8df932 | ||
|
|
20c19d76e1 | ||
|
|
b6f2be020c | ||
|
|
d7b7b8492e | ||
|
|
334a4c48ad |
@@ -169,12 +169,16 @@ if __name__ == "__main__":
|
||||
|
||||
6. Move the downloaded .json workflow file to your `ComfyUI/ComfyUI-to-Python-Extension` folder
|
||||
|
||||
7. If needed, update the `input_file` and `output_file` variables at the bottom of `comfyui_to_python.py` to match the name of 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.
|
||||
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.
|
||||
|
||||
8. Run the script:
|
||||
8a. Run the script with default arguments:
|
||||
```bash
|
||||
python comfyui_to_python.py
|
||||
```
|
||||
8b. 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. If you made no changes, look for `workflow_api.py`.
|
||||
|
||||
|
||||
+71
-14
@@ -6,7 +6,8 @@ import os
|
||||
import random
|
||||
import sys
|
||||
import re
|
||||
from typing import Dict, List, Any, Callable, Tuple
|
||||
from typing import Dict, List, Callable, Tuple
|
||||
from argparse import ArgumentParser
|
||||
|
||||
import black
|
||||
|
||||
@@ -23,6 +24,11 @@ sys.path.append("../")
|
||||
from nodes import NODE_CLASS_MAPPINGS
|
||||
|
||||
|
||||
DEFAULT_INPUT_FILE = "workflow_api.json"
|
||||
DEFAULT_OUTPUT_FILE = "workflow_api.py"
|
||||
DEFAULT_QUEUE_SIZE = 10
|
||||
|
||||
|
||||
class FileHandler:
|
||||
"""Handles reading and writing files.
|
||||
|
||||
@@ -224,7 +230,6 @@ class CodeGenerator:
|
||||
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"]
|
||||
class_def = self.node_class_mappings[class_type]()
|
||||
@@ -249,14 +254,18 @@ class CodeGenerator:
|
||||
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 key in class_def_params
|
||||
key: value
|
||||
for key, value in inputs.items()
|
||||
if no_params or key in class_def_params
|
||||
}
|
||||
# Deal with hidden variables
|
||||
if "unique_id" in class_def_params:
|
||||
inputs["unique_id"] = random.randint(1, 2**64)
|
||||
if 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}"
|
||||
@@ -466,7 +475,11 @@ class CodeGenerator:
|
||||
name: param.default if param.default != param.empty else None
|
||||
for name, param in signature.parameters.items()
|
||||
}
|
||||
return list(parameters.keys())
|
||||
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.
|
||||
@@ -552,13 +565,57 @@ class ComfyUItoPython:
|
||||
print(f"Code successfully generated and written to {self.output_file}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Update class parameters here
|
||||
input_file = "workflow_api.json"
|
||||
output_file = "workflow_api.py"
|
||||
queue_size = 10
|
||||
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.
|
||||
|
||||
# Convert ComfyUI workflow to Python
|
||||
ComfyUItoPython(
|
||||
input_file=input_file, output_file=output_file, queue_size=queue_size
|
||||
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
|
||||
"""
|
||||
ComfyUItoPython(input_file, output_file, 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()
|
||||
ComfyUItoPython(**vars(pargs))
|
||||
print("Done.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
"""Run the main function."""
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user