-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
70 lines (56 loc) · 3.67 KB
/
__init__.py
File metadata and controls
70 lines (56 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ComfyUI-AllergicPack/
#├── __init__.py (Manages one central JS folder)
#├── ExampleNode/
#│ └── example_node.py
#└── js/ (A single folder for ALL javascript)
# └── ExampleNode.js
import os
import importlib.util
import sys
import traceback # For detailed error logging
# Get the directory of the current __init__.py file (which is AllergicPack folder)
allergic_pack_dir = os.path.dirname(os.path.abspath(__file__))
# __name__ will be the name of your pack's root folder, e.g., "ComfyUI-AllergicPack"
# Register shared utilities so submodules can use: from allergic_utils import sanitize_path
# We load it via importlib and register in sys.modules so dynamically-loaded
# node files can find it, even though ComfyUI's own 'utils' module exists.
_utils_path = os.path.join(allergic_pack_dir, "allergic_utils.py")
_utils_spec = importlib.util.spec_from_file_location("allergic_utils", _utils_path)
if _utils_spec and _utils_spec.loader:
_utils_module = importlib.util.module_from_spec(_utils_spec)
sys.modules["allergic_utils"] = _utils_module
_utils_spec.loader.exec_module(_utils_module)
NODE_CLASS_MAPPINGS = {}
NODE_DISPLAY_NAME_MAPPINGS = {}
# --- THIS IS THE DIRECTORY for JavaScript loading ---
WEB_DIRECTORY = "js"
# Iterate over all items (files and directories) in the AllergicPack directory
for node_folder_name in os.listdir(allergic_pack_dir):
node_folder_path = os.path.join(allergic_pack_dir, node_folder_name)
# Check if the item is a directory (this will be our node's specific subfolder)
# Also, ignore the WEB_DIRECTORY folder itself and common non-node folders like .git, __pycache__
if os.path.isdir(node_folder_path) and node_folder_name != WEB_DIRECTORY and not node_folder_name.startswith(('.', '_')):
for py_filename in os.listdir(node_folder_path):
# Ensure we are only processing .py files and not __init__.py from the subfolder itself
if py_filename.endswith(".py") and py_filename != "__init__.py":
py_module_name_only = py_filename[:-3] # Remove .py extension
module_file_full_path = os.path.join(node_folder_path, py_filename)
try:
full_module_spec_name = f"{__name__}.{node_folder_name}.{py_module_name_only}"
spec = importlib.util.spec_from_file_location(full_module_spec_name, module_file_full_path)
if spec and spec.loader:
module = importlib.util.module_from_spec(spec)
sys.modules[full_module_spec_name] = module # Add to sys.modules BEFORE exec_module
spec.loader.exec_module(module)
if hasattr(module, "NODE_CLASS_MAPPINGS"):
NODE_CLASS_MAPPINGS.update(module.NODE_CLASS_MAPPINGS)
if hasattr(module, "NODE_DISPLAY_NAME_MAPPINGS"):
NODE_DISPLAY_NAME_MAPPINGS.update(module.NODE_DISPLAY_NAME_MAPPINGS)
else:
print(f" [!] [{__name__}] Warning: Could not create spec for module at {module_file_full_path}")
except Exception as e:
print(f" [!] [{__name__}] Error importing module from {module_file_full_path}: {e}")
traceback.print_exc()
# Ensure WEB_DIRECTORY is included in __all__ for ComfyUI to recognize it for this pack
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS', 'WEB_DIRECTORY']
print(f"[*] [{__name__}] Initialization complete. Total classes: {len(NODE_CLASS_MAPPINGS)}, Total display names: {len(NODE_DISPLAY_NAME_MAPPINGS)}")