|
1 | 1 | import os |
2 | 2 | from typing import Sequence, Mapping, Any, Union |
| 3 | +from pathlib import Path |
3 | 4 | import sys |
4 | 5 |
|
5 | 6 | sys.path.append('../') |
@@ -27,32 +28,54 @@ def import_custom_nodes() -> None: |
27 | 28 | # Initializing custom nodes |
28 | 29 | init_custom_nodes() |
29 | 30 |
|
30 | | -def add_comfyui_directory_to_sys_path() -> None: |
| 31 | +def find_path(name: str, path: str = None) -> Union[Path, None]: |
31 | 32 | """ |
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. |
34 | 35 | """ |
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) |
36 | 48 |
|
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 |
43 | 52 |
|
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) |
46 | 55 |
|
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 |
50 | 56 |
|
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") |
53 | 65 |
|
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 | + |
56 | 79 |
|
57 | 80 |
|
58 | 81 | def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any: |
|
0 commit comments