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
This commit is contained in:
Peyton DeNiro
2023-08-23 14:19:27 -05:00
committed by GitHub
2 changed files with 53 additions and 26 deletions
+9 -5
View File
@@ -11,10 +11,14 @@ from typing import Dict, List, Any, Callable, Tuple
import black
from utils import import_custom_nodes, add_comfyui_directory_to_sys_path, get_value_at_index
sys.path.append('../')
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
@@ -307,11 +311,11 @@ class CodeGenerator:
"""
# 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]:
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', 'import sys', 'from typing import Sequence, Mapping, Any, Union',
'import torch'] + func_strings + ['\n\nadd_comfyui_directory_to_sys_path()']
static_imports = ['import os', 'import random', 'from pathlib import Path', 'import sys', 'from typing import Sequence, Mapping, Any, Union',
'import torch'] + func_strings + ['\n\nadd_comfyui_directory_to_sys_path()\nadd_extra_model_paths()\n']
# Check if custom nodes should be included
if custom_nodes:
static_imports.append(f'\n{inspect.getsource(import_custom_nodes)}\n')
+44 -21
View File
@@ -1,5 +1,6 @@
import os
from typing import Sequence, Mapping, Any, Union
from pathlib import Path
import sys
sys.path.append('../')
@@ -27,32 +28,54 @@ def import_custom_nodes() -> None:
# Initializing custom nodes
init_custom_nodes()
def find_path(name: str, path: str = None) -> Union[Path, None]:
"""
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.
"""
# 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)
# 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
# Recursively call the function with the parent directory
return find_path(name, parent_directory)
def add_comfyui_directory_to_sys_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.
Add 'ComfyUI' to the sys.path
"""
start_path = os.getcwd() # Get the current working directory
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 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}")
def add_extra_model_paths() -> Path:
"""
Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path.
"""
from main import load_extra_path_config
# 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
search_directory(parent_directory)
# Start the search from the current working directory
search_directory(start_path)
extra_model_paths = find_path("extra_model_paths.yaml")
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.")
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any: