Skip to content
Prev Previous commit
Next Next commit
Clean up the code a bit
  • Loading branch information
dimtoneff committed Aug 22, 2023
commit 2a819fabf08419064e54c5db7ad69833b08b37f7
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,3 @@ cython_debug/
*.sql
*.sqlite
*.xml
/workflow_api.json
/workflow_api.py
/custom_nodes/
11 changes: 7 additions & 4 deletions comfyui_to_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import black

sys.path.append('../')
sys.path.append('.')

from utils import import_custom_nodes, add_comfyui_directory_to_sys_path, get_value_at_index, add_extra_model_paths
try:
from utils import import_custom_nodes, find_path, add_comfyui_directory_to_sys_path, add_extra_model_paths, get_value_at_index
except ImportError:
sys.path.append('.')
from utils import import_custom_nodes, find_path, add_comfyui_directory_to_sys_path, add_extra_model_paths, get_value_at_index

from nodes import NODE_CLASS_MAPPINGS

Expand Down Expand Up @@ -307,7 +310,7 @@ def assemble_python_code(self, import_statements: set, speical_functions_code: L
"""
# Get the source code of the utils functions as a string
func_strings = []
for func in [add_comfyui_directory_to_sys_path, get_value_at_index, add_extra_model_paths]:
for func in [find_path, add_comfyui_directory_to_sys_path, add_extra_model_paths, get_value_at_index]:
func_strings.append(f'\n{inspect.getsource(func)}')
# Define static import statements required for the script
static_imports = ['import os', 'import random', 'from pathlib import Path', 'import sys', 'from typing import Sequence, Mapping, Any, Union',
Expand Down Expand Up @@ -434,7 +437,7 @@ def execute(self):
# Update class parameters here
input_file = 'workflow_api.json'
output_file = 'workflow_api.py'
queue_size = 10
queue_size = 2

# Convert ComfyUI workflow to Python
ComfyUItoPython(input_file=input_file, output_file=output_file, queue_size=queue_size)
71 changes: 30 additions & 41 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,62 +28,51 @@ def import_custom_nodes() -> None:
# Initializing custom nodes
init_custom_nodes()

def add_comfyui_directory_to_sys_path() -> None:
def find_path(name: str, path: str = None) -> Union[Path, None]:
"""
Recursively looks at parent folders starting from the current working directory until it finds 'ComfyUI'.
Once found, the directory is added to sys.path.
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.
"""
start_path = os.getcwd() # Get the current working directory
# 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(path_name)

def search_directory(path: str) -> None:
# Check if the current directory contains 'ComfyUI'
if 'ComfyUI' in os.listdir(path):
directory_path = os.path.join(path, 'ComfyUI')
sys.path.append(directory_path)
print(f"ComfyUI found and added to sys.path: {directory_path}")
# Get the parent directory
parent_directory = os.path.dirname(path)

# 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

# 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
# Recursively call the function with the parent directory
return find_path(name, parent_directory)

# Recursively call the function with the parent directory
search_directory(parent_directory)

# Start the search from the current working directory
search_directory(start_path)
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() -> Path:
"""
Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path.
"""
from pathlib import Path
from main import load_extra_path_config

def find_config_file(path: str, name: str = "extra_model_paths.yaml") -> Path:
# Check if the current directory contains the file
if name in os.listdir(path):
directory_path = os.path.join(path, name)
print(f"{name} found: {directory_path}")
return Path(directory_path)

# 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

# Recursively call the function with the parent directory
return find_config_file(parent_directory)

start_path = os.getcwd() # Get the current working directory
file = find_config_file(start_path)
extra_model_paths = find_path("extra_model_paths.yaml")

if os.path.isfile(file):
load_extra_path_config(file)
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.")

Expand Down