diff --git a/.gitignore b/.gitignore index 9f0389241..fdddc4f23 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ __pycache__/ *.py[cod] +*.egg-info/ /output/ /input/ !/input/example.png @@ -15,4 +16,7 @@ venv/ !/web/extensions/logging.js.example !/web/extensions/core/ /tests-ui/data/object_info.json -/user/ \ No newline at end of file +/user/ +.history/ +.vscode/ +*.code-workspace diff --git a/nodes.py b/nodes.py index 99645b81c..c72cf6088 100644 --- a/nodes.py +++ b/nodes.py @@ -9,6 +9,7 @@ import math import time import random import logging +import re from PIL import Image, ImageOps, ImageSequence, ImageFile from PIL.PngImagePlugin import PngInfo @@ -31,6 +32,7 @@ import comfy.model_management from comfy.cli_args import args import importlib +from importlib.metadata import entry_points import folder_paths import latent_preview @@ -1887,46 +1889,60 @@ NODE_DISPLAY_NAME_MAPPINGS = { EXTENSION_WEB_DIRS = {} -def load_custom_node(module_path, ignore=set()): - module_name = os.path.basename(module_path) - if os.path.isfile(module_path): - sp = os.path.splitext(module_path) - module_name = sp[0] - try: - logging.debug("Trying to load custom node {}".format(module_path)) - if os.path.isfile(module_path): - module_spec = importlib.util.spec_from_file_location(module_name, module_path) - module_dir = os.path.split(module_path)[0] - else: - module_spec = importlib.util.spec_from_file_location(module_name, os.path.join(module_path, "__init__.py")) - module_dir = module_path +EXTENSION_MODULES_LOADED = set() - module = importlib.util.module_from_spec(module_spec) - sys.modules[module_name] = module - module_spec.loader.exec_module(module) +_mod_name_re = re.compile("-") +NORMALIZE_MOD_NAME = lambda x: _mod_name_re.sub("_", x.strip().lower()) - if hasattr(module, "WEB_DIRECTORY") and getattr(module, "WEB_DIRECTORY") is not None: - web_dir = os.path.abspath(os.path.join(module_dir, getattr(module, "WEB_DIRECTORY"))) - if os.path.isdir(web_dir): - EXTENSION_WEB_DIRS[module_name] = web_dir +def _load_custom_node(ext_mod, ext_mod_name, ext_mod_dir, ignore): + if ext_mod_dir is not None and hasattr(ext_mod, "WEB_DIRECTORY") and getattr(ext_mod, "WEB_DIRECTORY") is not None: + web_dir = os.path.abspath(os.path.join(ext_mod_dir, getattr(ext_mod, "WEB_DIRECTORY"))) + if os.path.isdir(web_dir): + EXTENSION_WEB_DIRS[ext_mod_name] = web_dir - if hasattr(module, "NODE_CLASS_MAPPINGS") and getattr(module, "NODE_CLASS_MAPPINGS") is not None: - for name in module.NODE_CLASS_MAPPINGS: - if name not in ignore: - NODE_CLASS_MAPPINGS[name] = module.NODE_CLASS_MAPPINGS[name] - if hasattr(module, "NODE_DISPLAY_NAME_MAPPINGS") and getattr(module, "NODE_DISPLAY_NAME_MAPPINGS") is not None: - NODE_DISPLAY_NAME_MAPPINGS.update(module.NODE_DISPLAY_NAME_MAPPINGS) - return True - else: - logging.warning(f"Skip {module_path} module for custom nodes due to the lack of NODE_CLASS_MAPPINGS.") - return False - except Exception as e: - logging.warning(traceback.format_exc()) - logging.warning(f"Cannot import {module_path} module for custom nodes: {e}") + if hasattr(ext_mod, "NODE_CLASS_MAPPINGS") and getattr(ext_mod, "NODE_CLASS_MAPPINGS") is not None: + for node_class_name in ext_mod.NODE_CLASS_MAPPINGS: + if node_class_name not in ignore: + NODE_CLASS_MAPPINGS[node_class_name] = ext_mod.NODE_CLASS_MAPPINGS[node_class_name] + if hasattr(ext_mod, "NODE_DISPLAY_NAME_MAPPINGS") and getattr(ext_mod, "NODE_DISPLAY_NAME_MAPPINGS") is not None: + NODE_DISPLAY_NAME_MAPPINGS.update(ext_mod.NODE_DISPLAY_NAME_MAPPINGS) + return True + else: + logging.warning(f"Skip loading custom nodes from {ext_mod_name}. No NODE_CLASS_MAPPINGS found in module.") return False -def load_custom_nodes(): - base_node_names = set(NODE_CLASS_MAPPINGS.keys()) +def load_custom_node(ext_mod_path, ignore=set()): + if os.path.isfile(ext_mod_path): + ext_mod_name = NORMALIZE_MOD_NAME(os.path.splitext(ext_mod_path)[0]) + else: + ext_mod_name = NORMALIZE_MOD_NAME(os.path.basename(ext_mod_path)) + + if ext_mod_name in EXTENSION_MODULES_LOADED: + logging.warning(f"Skip loading custom nodes from {ext_mod_path}. Module already loaded.") + return False + else: + EXTENSION_MODULES_LOADED.add(ext_mod_name) + + try: + logging.debug("Trying to load custom node {}".format(ext_mod_path)) + if os.path.isfile(ext_mod_path): + ext_mod_spec = importlib.util.spec_from_file_location(ext_mod_name, ext_mod_path) + ext_mod_dir = os.path.split(ext_mod_path)[0] + else: + ext_mod_spec = importlib.util.spec_from_file_location(ext_mod_name, os.path.join(ext_mod_path, "__init__.py")) + ext_mod_dir = ext_mod_path + + ext_mod = importlib.util.module_from_spec(ext_mod_spec) + sys.modules[ext_mod_name] = ext_mod + ext_mod_spec.loader.exec_module(ext_mod) + return _load_custom_node(ext_mod=ext_mod, ext_mod_name=ext_mod_name, ext_mod_dir=ext_mod_dir, ignore=ignore) + + except Exception as e: + logging.warning(traceback.format_exc()) + logging.warning(f"Cannot import {ext_mod_path} module for custom nodes: {e}") + return False + +def load_custom_nodes(ignore=set()): node_paths = folder_paths.get_folder_paths("custom_nodes") node_import_times = [] for custom_node_path in node_paths: @@ -1935,12 +1951,12 @@ def load_custom_nodes(): possible_modules.remove("__pycache__") for possible_module in possible_modules: - module_path = os.path.join(custom_node_path, possible_module) - if os.path.isfile(module_path) and os.path.splitext(module_path)[1] != ".py": continue - if module_path.endswith(".disabled"): continue + ext_mod_path = os.path.join(custom_node_path, possible_module) + if os.path.isfile(ext_mod_path) and os.path.splitext(ext_mod_path)[1] != ".py": continue + if ext_mod_path.endswith(".disabled"): continue time_before = time.perf_counter() - success = load_custom_node(module_path, base_node_names) - node_import_times.append((time.perf_counter() - time_before, module_path, success)) + success = load_custom_node(ext_mod_path, ignore) + node_import_times.append((time.perf_counter() - time_before, ext_mod_path, success)) if len(node_import_times) > 0: logging.info("\nImport times for custom nodes:") @@ -1952,6 +1968,27 @@ def load_custom_nodes(): logging.info("{:6.1f} seconds{}: {}".format(n[0], import_message, n[1])) logging.info("") +def load_custom_nodes_entry_points(ignore=set()): + for ep in entry_points(group="comfyui.extension_module"): + ext_mod_name = NORMALIZE_MOD_NAME(ep.module) + + if ext_mod_name in EXTENSION_MODULES_LOADED: + logging.warning(f"Skip loading custom nodes from {ext_mod_name}. Module already loaded.") + continue + else: + EXTENSION_MODULES_LOADED.add(ext_mod_name) + + try: + ext_mod = ep.load() + ext_mod_dir = os.path.dirname(os.path.realpath(ext_mod.__file__)) + + _load_custom_node(ext_mod=ext_mod, ext_mod_name=ext_mod_name, ext_mod_dir=ext_mod_dir, ignore=ignore) + + except Exception as e: + logging.warning(traceback.format_exc()) + logging.warning(f"Cannot import custom nodes from {ext_mod_name}: {e}") + continue + def init_custom_nodes(): extras_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras") extras_files = [ @@ -1999,7 +2036,9 @@ def init_custom_nodes(): if not load_custom_node(os.path.join(extras_dir, node_file)): import_failed.append(node_file) - load_custom_nodes() + base_node_names = set(NODE_CLASS_MAPPINGS.keys()) + load_custom_nodes_entry_points(ignore=base_node_names) + load_custom_nodes(ignore=base_node_names) if len(import_failed) > 0: logging.warning("WARNING: some comfy_extras/ nodes did not import correctly. This may be because they are missing some dependencies.\n")