From 8f0a47307e01d1f0d95e3937b8c799dffb741db2 Mon Sep 17 00:00:00 2001 From: atmaranto <35581227+atmaranto@users.noreply.github.com> Date: Sat, 9 Mar 2024 05:59:41 -0600 Subject: [PATCH] Update README, add template args --- README.md | 12 ++++++++---- comfyui_to_python.py | 14 +++++++------- comfyui_to_python_utils.py | 6 +++++- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0c2ba8e..b10c403 100644 --- a/README.md +++ b/README.md @@ -48,15 +48,19 @@ For instance, if you have a simple default workflow, but have converted the text Then the unfilled required variable will be available as an argument: ```bash -usage: default-workflow.py [-h] text1 +usage: workflow.py [-h] [--queue-size QUEUE_SIZE] [--comfyui-directory COMFYUI_DIRECTORY] filename_prefix1 -A converted ComfyUI workflow. Required inputs listed below. Values passed should be in JSON +A converted ComfyUI workflow. Required inputs listed below. Values passed should be valid JSON (assumes string if not valid JSON). positional arguments: - text1 Argument 0, input `text` for node "CLIP Text Encode (Prompt)" id 6 (autogenerated) + filename_prefix1 Argument 1, input `filename_prefix` for node "Save Image" id 64 (autogenerated) options: - -h, --help show this help message and exit + -h, --help show this help message and exit + --queue-size QUEUE_SIZE, -q QUEUE_SIZE + How many times the workflow will be executed (default: 10) + --comfyui-directory COMFYUI_DIRECTORY, -c COMFYUI_DIRECTORY + Where to look for ComfyUI (default: current directory) ``` 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. diff --git a/comfyui_to_python.py b/comfyui_to_python.py index 651345d..d8a5c37 100644 --- a/comfyui_to_python.py +++ b/comfyui_to_python.py @@ -306,12 +306,12 @@ class CodeGenerator: for func in [get_value_at_index, find_path, add_comfyui_directory_to_sys_path, add_extra_model_paths, import_custom_nodes, parse_arg]: func_strings.append(f'\n{inspect.getsource(func)}') - argparse_code = "" - if arg_inputs: - argparse_code = f'\nparser = argparse.ArgumentParser(description="A converted ComfyUI workflow. Required inputs listed below. Values passed should be in JSON")\n' - for i, (input_name, arg_desc) in enumerate(arg_inputs): - argparse_code += f'parser.add_argument("{input_name}", help="{arg_desc} (autogenerated)")\n' - argparse_code += 'args = parser.parse_args()\nsys.argv = [sys.argv[0]]\n' + argparse_code = f'\nparser = argparse.ArgumentParser(description="A converted ComfyUI workflow. Required inputs listed below. Values passed should be valid JSON (assumes string if not valid JSON).")\n' + for i, (input_name, arg_desc) in enumerate(arg_inputs): + argparse_code += f'parser.add_argument("{input_name}", help="{arg_desc} (autogenerated)")\n' + argparse_code += f'parser.add_argument("--queue-size", "-q", type=int, default={queue_size}, help="How many times the workflow will be executed (default: {queue_size})")\n' + argparse_code += f'parser.add_argument("--comfyui-directory", "-c", default=None, help="Where to look for ComfyUI (default: current directory)")\n' + argparse_code += 'args = parser.parse_args()\nsys.argv = [sys.argv[0]]\n' # Define static import statements required for the script static_imports = ['import os', 'import random', 'import sys', 'import json', 'import argparse', 'from typing import Sequence, Mapping, Any, Union', @@ -327,7 +327,7 @@ class CodeGenerator: 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) + + f'\n\n\t\tfor q in range(args.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 diff --git a/comfyui_to_python_utils.py b/comfyui_to_python_utils.py index 5fa181d..dc7bc62 100644 --- a/comfyui_to_python_utils.py +++ b/comfyui_to_python_utils.py @@ -4,6 +4,7 @@ import sys sys.path.append('../') +args = None def import_custom_nodes() -> None: """Find all custom nodes in the custom_nodes folder and add those node objects to NODE_CLASS_MAPPINGS @@ -35,7 +36,10 @@ def find_path(name: str, path: str = None) -> str: """ # If no path is given, use the current working directory if path is None: - path = os.getcwd() + if args is None or args.comfyui_directory is None: + path = os.getcwd() + else: + path = args.comfyui_directory # Check if the current directory contains the name if name in os.listdir(path):