Skip to content

Commit ffe21d2

Browse files
authored
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
2 parents baf5e91 + bfa3160 commit ffe21d2

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

comfyui_to_python.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
import black
1313

14-
from utils import import_custom_nodes, add_comfyui_directory_to_sys_path, get_value_at_index
15-
1614
sys.path.append('../')
1715

16+
try:
17+
from utils import import_custom_nodes, find_path, add_comfyui_directory_to_sys_path, add_extra_model_paths, get_value_at_index
18+
except ImportError:
19+
sys.path.append('.')
20+
from utils import import_custom_nodes, find_path, add_comfyui_directory_to_sys_path, add_extra_model_paths, get_value_at_index
21+
1822
from nodes import NODE_CLASS_MAPPINGS
1923

2024

@@ -307,11 +311,11 @@ def assemble_python_code(self, import_statements: set, speical_functions_code: L
307311
"""
308312
# Get the source code of the utils functions as a string
309313
func_strings = []
310-
for func in [add_comfyui_directory_to_sys_path, get_value_at_index]:
314+
for func in [find_path, add_comfyui_directory_to_sys_path, add_extra_model_paths, get_value_at_index]:
311315
func_strings.append(f'\n{inspect.getsource(func)}')
312316
# Define static import statements required for the script
313-
static_imports = ['import os', 'import random', 'import sys', 'from typing import Sequence, Mapping, Any, Union',
314-
'import torch'] + func_strings + ['\n\nadd_comfyui_directory_to_sys_path()']
317+
static_imports = ['import os', 'import random', 'from pathlib import Path', 'import sys', 'from typing import Sequence, Mapping, Any, Union',
318+
'import torch'] + func_strings + ['\n\nadd_comfyui_directory_to_sys_path()\nadd_extra_model_paths()\n']
315319
# Check if custom nodes should be included
316320
if custom_nodes:
317321
static_imports.append(f'\n{inspect.getsource(import_custom_nodes)}\n')

utils.py

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from typing import Sequence, Mapping, Any, Union
3+
from pathlib import Path
34
import sys
45

56
sys.path.append('../')
@@ -27,32 +28,54 @@ def import_custom_nodes() -> None:
2728
# Initializing custom nodes
2829
init_custom_nodes()
2930

30-
def add_comfyui_directory_to_sys_path() -> None:
31+
def find_path(name: str, path: str = None) -> Union[Path, None]:
3132
"""
32-
Recursively looks at parent folders starting from the current working directory until it finds 'ComfyUI'.
33-
Once found, the directory is added to sys.path.
33+
Recursively looks at parent folders starting from the given path until it finds the given name.
34+
Returns the path as a Path object if found, or None otherwise.
3435
"""
35-
start_path = os.getcwd() # Get the current working directory
36+
# If no path is given, use the current working directory
37+
if path is None:
38+
path = os.getcwd()
39+
40+
# Check if the current directory contains the name
41+
if name in os.listdir(path):
42+
path_name = os.path.join(path, name)
43+
print(f"{name} found: {path_name}")
44+
return Path(path_name)
45+
46+
# Get the parent directory
47+
parent_directory = os.path.dirname(path)
3648

37-
def search_directory(path: str) -> None:
38-
# Check if the current directory contains 'ComfyUI'
39-
if 'ComfyUI' in os.listdir(path):
40-
directory_path = os.path.join(path, 'ComfyUI')
41-
sys.path.append(directory_path)
42-
print(f"ComfyUI found and added to sys.path: {directory_path}")
49+
# If the parent directory is the same as the current directory, we've reached the root and stop the search
50+
if parent_directory == path:
51+
return None
4352

44-
# Get the parent directory
45-
parent_directory = os.path.dirname(path)
53+
# Recursively call the function with the parent directory
54+
return find_path(name, parent_directory)
4655

47-
# If the parent directory is the same as the current directory, we've reached the root and stop the search
48-
if parent_directory == path:
49-
return
5056

51-
# Recursively call the function with the parent directory
52-
search_directory(parent_directory)
57+
def add_comfyui_directory_to_sys_path() -> None:
58+
"""
59+
Add 'ComfyUI' to the sys.path
60+
"""
61+
comfyui_path = find_path('ComfyUI')
62+
if comfyui_path is not None and os.path.isdir(comfyui_path):
63+
sys.path.append(comfyui_path)
64+
print(f"'{comfyui_path}' added to sys.path")
5365

54-
# Start the search from the current working directory
55-
search_directory(start_path)
66+
def add_extra_model_paths() -> Path:
67+
"""
68+
Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path.
69+
"""
70+
from main import load_extra_path_config
71+
72+
extra_model_paths = find_path("extra_model_paths.yaml")
73+
74+
if extra_model_paths is not None:
75+
load_extra_path_config(extra_model_paths)
76+
else:
77+
print("Could not find the extra_model_paths config file.")
78+
5679

5780

5881
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:

0 commit comments

Comments
 (0)