File size: 3,061 Bytes
6efa2d8
 
 
 
 
0b8999d
6efa2d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
import sys
from pathlib import Path
import importlib.util
import traceback

__version__ = "1.4.4"
print(f"[BonsoTools Loader] Loading comfyui_Bonso_Tools v{__version__}")

try:
    current_path = Path(__file__)
    comfyui_root_path = current_path.parent.parent.parent
    if str(comfyui_root_path) not in sys.path:
        sys.path.insert(0, str(comfyui_root_path))
except Exception as e:
    print(f"[BonsoTools Loader] Error adding ComfyUI root path: {e}")

try:
    import comfy
except Exception as e:
    print(f"[BonsoTools Loader] Warning: Could not pre-import 'comfy'. Error: {e}")


NODE_CLASS_MAPPINGS = {}
NODE_DISPLAY_NAME_MAPPINGS = {}


def load_module(module_name: str, file_path: str):
    spec = importlib.util.spec_from_file_location(module_name, file_path)
    if spec is None or spec.loader is None:
        raise ImportError(f"Could not create module spec for {module_name} at {file_path}")
    module = importlib.util.module_from_spec(spec)
    if module_name not in sys.modules:
        sys.modules[module_name] = module
    spec.loader.exec_module(module)
    return module


def _module_name_for(path: Path, base_pkg: str, nodes_path: Path) -> str:
    rel = path.relative_to(nodes_path)
    parts = list(rel.with_suffix('').parts)
    return ".".join([base_pkg] + parts) if parts else base_pkg


def load_nodes():
    base_pkg = "nodes"
    nodes_path = Path(__file__).parent / base_pkg

    if not nodes_path.is_dir():
        print(f"[BonsoTools Loader] The '{nodes_path}' directory does not exist.")
        return

    py_files = [file for file in nodes_path.rglob("*.py") if not (file.name == "__init__.py" and file.parent == nodes_path)]
    py_files.sort(key=lambda p: str(p.relative_to(nodes_path)))

    for file in py_files:
        try:
            module_name = _module_name_for(file, "comfyui_bonso_tools.nodes", nodes_path)
            module = load_module(module_name, str(file))

            if hasattr(module, "NODE_CLASS_MAPPINGS") and isinstance(module.NODE_CLASS_MAPPINGS, dict):
                for k, v in module.NODE_CLASS_MAPPINGS.items():
                    if k in NODE_CLASS_MAPPINGS:
                        print(f"[BonsoTools Loader] Warning: Duplicate key '{k}' in NODE_CLASS_MAPPINGS. Overwriting.")
                    NODE_CLASS_MAPPINGS[k] = v

            if hasattr(module, "NODE_DISPLAY_NAME_MAPPINGS") and isinstance(module.NODE_DISPLAY_NAME_MAPPINGS, dict):
                for k, v in module.NODE_DISPLAY_NAME_MAPPINGS.items():
                    if k in NODE_DISPLAY_NAME_MAPPINGS:
                        print(f"[BonsoTools Loader] Warning: Duplicate key '{k}' in NODE_DISPLAY_NAME_MAPPINGS. Overwriting.")
                    NODE_DISPLAY_NAME_MAPPINGS[k] = v

        except Exception as e:
            rel_path = file.relative_to(nodes_path)
            print(f"[BonsoTools Loader] Error loading node from {rel_path}: {e}\n{traceback.format_exc()}")


load_nodes()

WEB_DIRECTORY = "./js"

__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS", "WEB_DIRECTORY", "__version__"]