From f1d597452d28ada2bc6460a556284d781da39c4d Mon Sep 17 00:00:00 2001 From: telamonian Date: Fri, 17 May 2024 09:58:17 -0400 Subject: [PATCH 01/11] added prototype of alternative entry point-based custom_node loading mechanism --- nodes.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/nodes.py b/nodes.py index 34821ca3f..a400cf41c 100644 --- a/nodes.py +++ b/nodes.py @@ -31,6 +31,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 @@ -1925,6 +1926,19 @@ def load_custom_nodes(): logging.info("{:6.1f} seconds{}: {}".format(n[0], import_message, n[1])) logging.info("") +def load_custom_nodes_entry_points(): + base_node_names = set(NODE_CLASS_MAPPINGS.keys()) + + for ep in entry_points(group='comfyui.node_class_mappings'): + class_mapping = ep.load() + for name in class_mapping: + if name not in base_node_names: + NODE_CLASS_MAPPINGS[name] = mapping[name] + + for ep in entry_points(group='comfyui.node_display_name_mappings'): + display_name_mapping = ep.load() + NODE_DISPLAY_NAME_MAPPINGS.update(display_name_mapping) + def init_custom_nodes(): extras_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras") extras_files = [ @@ -1970,6 +1984,7 @@ def init_custom_nodes(): import_failed.append(node_file) load_custom_nodes() + load_custom_nodes_entry_points() 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") From 968595b1239ebb8ed2df51204949fc91e5d8b999 Mon Sep 17 00:00:00 2001 From: telamonian Date: Fri, 17 May 2024 14:08:25 -0400 Subject: [PATCH 02/11] bugfix. Custom nodes as entry point plugins has now been tested and is working --- nodes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nodes.py b/nodes.py index a400cf41c..31e30b687 100644 --- a/nodes.py +++ b/nodes.py @@ -1929,13 +1929,13 @@ def load_custom_nodes(): def load_custom_nodes_entry_points(): base_node_names = set(NODE_CLASS_MAPPINGS.keys()) - for ep in entry_points(group='comfyui.node_class_mappings'): + for ep in entry_points(group="comfyui.node_class_mappings"): class_mapping = ep.load() for name in class_mapping: if name not in base_node_names: - NODE_CLASS_MAPPINGS[name] = mapping[name] + NODE_CLASS_MAPPINGS[name] = class_mapping[name] - for ep in entry_points(group='comfyui.node_display_name_mappings'): + for ep in entry_points(group="comfyui.node_display_name_mappings"): display_name_mapping = ep.load() NODE_DISPLAY_NAME_MAPPINGS.update(display_name_mapping) From 0d2b1eb95bc1cf1316af13184c313e35db19cb9b Mon Sep 17 00:00:00 2001 From: telamonian Date: Sat, 1 Jun 2024 02:37:45 -0400 Subject: [PATCH 03/11] added EXTENSION_MODULES_LOADED guard set to prevent double loading of extensions --- .gitignore | 6 +++++- nodes.py | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) 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 31e30b687..9522c7097 100644 --- a/nodes.py +++ b/nodes.py @@ -1861,11 +1861,20 @@ NODE_DISPLAY_NAME_MAPPINGS = { EXTENSION_WEB_DIRS = {} +EXTENSION_MODULES_LOADED = set() + 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] + + if module_name in EXTENSION_MODULES_LOADED: + logging.warning(f"Skip {module_path} module for custom nodes due to it already being loaded.") + return False + else: + EXTENSION_MODULES_LOADED.add(module_name) + try: logging.debug("Trying to load custom node {}".format(module_path)) if os.path.isfile(module_path): @@ -1928,16 +1937,30 @@ def load_custom_nodes(): def load_custom_nodes_entry_points(): base_node_names = set(NODE_CLASS_MAPPINGS.keys()) + extension_modules = set() for ep in entry_points(group="comfyui.node_class_mappings"): + if ep.module in EXTENSION_MODULES_LOADED: + logging.warning(f"Skip {ep.module} module for node_class_mappings entry point due to it already being loaded.") + continue + class_mapping = ep.load() for name in class_mapping: if name not in base_node_names: NODE_CLASS_MAPPINGS[name] = class_mapping[name] + extension_modules.add(ep.module) for ep in entry_points(group="comfyui.node_display_name_mappings"): + if ep.module in EXTENSION_MODULES_LOADED: + logging.warning(f"Skip {ep.module} module for node_display_name_mappings entry point due to it already being loaded.") + continue + display_name_mapping = ep.load() NODE_DISPLAY_NAME_MAPPINGS.update(display_name_mapping) + extension_modules.add(ep.module) + + # delay adding new module names to the guard set until after all possible entry_points are loaded + EXTENSION_MODULES_LOADED |= extension_modules def init_custom_nodes(): extras_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras") From c2d59d9c59320f51b9b57798a6ff8030d8cbaa3c Mon Sep 17 00:00:00 2001 From: telamonian Date: Sat, 1 Jun 2024 03:47:43 -0400 Subject: [PATCH 04/11] added loading for WEB_DIRECTORY entry_point in extensions --- nodes.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/nodes.py b/nodes.py index 9522c7097..8f6a57b37 100644 --- a/nodes.py +++ b/nodes.py @@ -1,3 +1,4 @@ +import importlib.metadata import torch import os @@ -1939,6 +1940,19 @@ def load_custom_nodes_entry_points(): base_node_names = set(NODE_CLASS_MAPPINGS.keys()) extension_modules = set() + for ep in entry_points(group="comfyui.web_directory"): + if ep.module in EXTENSION_MODULES_LOADED: + logging.warning(f"Skip {ep.module} module for web_directory entry point due to it already being loaded.") + continue + + web_directory = ep.load() + EXTENSION_WEB_DIRS[module_name] = web_directory + for path in importlib.metadata.files(ep.module): + if path.parent.name == ep.module and path.name == web_directory: + EXTENSION_WEB_DIRS[ep.module] = str(path.locate()) + + extension_modules.add(ep.module) + for ep in entry_points(group="comfyui.node_class_mappings"): if ep.module in EXTENSION_MODULES_LOADED: logging.warning(f"Skip {ep.module} module for node_class_mappings entry point due to it already being loaded.") @@ -1948,6 +1962,7 @@ def load_custom_nodes_entry_points(): for name in class_mapping: if name not in base_node_names: NODE_CLASS_MAPPINGS[name] = class_mapping[name] + extension_modules.add(ep.module) for ep in entry_points(group="comfyui.node_display_name_mappings"): @@ -1957,6 +1972,7 @@ def load_custom_nodes_entry_points(): display_name_mapping = ep.load() NODE_DISPLAY_NAME_MAPPINGS.update(display_name_mapping) + extension_modules.add(ep.module) # delay adding new module names to the guard set until after all possible entry_points are loaded From 4fb7600a453607477f2953b86fed9a0f51962797 Mon Sep 17 00:00:00 2001 From: telamonian Date: Sat, 1 Jun 2024 03:51:45 -0400 Subject: [PATCH 05/11] fixed grammar of "Skip loading..." log warnings --- nodes.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/nodes.py b/nodes.py index 8f6a57b37..acb2402fe 100644 --- a/nodes.py +++ b/nodes.py @@ -1871,7 +1871,7 @@ def load_custom_node(module_path, ignore=set()): module_name = sp[0] if module_name in EXTENSION_MODULES_LOADED: - logging.warning(f"Skip {module_path} module for custom nodes due to it already being loaded.") + logging.warning(f"Skip loading custom nodes from {module_path}. Module already loaded.") return False else: EXTENSION_MODULES_LOADED.add(module_name) @@ -1902,7 +1902,7 @@ def load_custom_node(module_path, ignore=set()): 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.") + logging.warning(f"Skip loading custom nodes from {module_path}. No NODE_CLASS_MAPPINGS found in module.") return False except Exception as e: logging.warning(traceback.format_exc()) @@ -1942,7 +1942,7 @@ def load_custom_nodes_entry_points(): for ep in entry_points(group="comfyui.web_directory"): if ep.module in EXTENSION_MODULES_LOADED: - logging.warning(f"Skip {ep.module} module for web_directory entry point due to it already being loaded.") + logging.warning(f"Skip loading web_directory entry point from {ep.module}. Module already loaded.") continue web_directory = ep.load() @@ -1955,7 +1955,7 @@ def load_custom_nodes_entry_points(): for ep in entry_points(group="comfyui.node_class_mappings"): if ep.module in EXTENSION_MODULES_LOADED: - logging.warning(f"Skip {ep.module} module for node_class_mappings entry point due to it already being loaded.") + logging.warning(f"Skip loading node_class_mappings entry point from {ep.module}. Module already loaded.") continue class_mapping = ep.load() @@ -1967,7 +1967,7 @@ def load_custom_nodes_entry_points(): for ep in entry_points(group="comfyui.node_display_name_mappings"): if ep.module in EXTENSION_MODULES_LOADED: - logging.warning(f"Skip {ep.module} module for node_display_name_mappings entry point due to it already being loaded.") + logging.warning(f"Skip loading node_display_name_mappings entry point from {ep.module}. Module already loaded.") continue display_name_mapping = ep.load() From ac53b004b762b357a8f2beb50a46fb39d079a5e0 Mon Sep 17 00:00:00 2001 From: telamonian Date: Sat, 1 Jun 2024 15:35:53 -0400 Subject: [PATCH 06/11] load entry_point extensions before old-style extensions --- nodes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodes.py b/nodes.py index acb2402fe..93868fff4 100644 --- a/nodes.py +++ b/nodes.py @@ -2022,8 +2022,8 @@ def init_custom_nodes(): if not load_custom_node(os.path.join(extras_dir, node_file)): import_failed.append(node_file) - load_custom_nodes() load_custom_nodes_entry_points() + load_custom_nodes() 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") From 9b8916f0b11410caa1c2853c30bf7a2b0a2a622a Mon Sep 17 00:00:00 2001 From: telamonian Date: Sat, 1 Jun 2024 15:41:09 -0400 Subject: [PATCH 07/11] picked lint --- nodes.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nodes.py b/nodes.py index 93868fff4..e8f2f7102 100644 --- a/nodes.py +++ b/nodes.py @@ -1,4 +1,3 @@ -import importlib.metadata import torch import os From 9b112c7fa5e3cbe35b6e7b642d07426d477ef3c0 Mon Sep 17 00:00:00 2001 From: telamonian Date: Wed, 19 Jun 2024 21:09:02 -0400 Subject: [PATCH 08/11] refactored entry_point to single "comfyui.extension_module" - refactored part of load_custom_node into _load_custom_node to allow for max code reuse between manual module loading and entry_point module loading - fixed potential issue with base_node_names by passing node name ignore list down --- nodes.py | 149 ++++++++++++++++++++++++++----------------------------- 1 file changed, 71 insertions(+), 78 deletions(-) diff --git a/nodes.py b/nodes.py index e8f2f7102..419fed43e 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 @@ -1863,53 +1864,59 @@ EXTENSION_WEB_DIRS = {} EXTENSION_MODULES_LOADED = set() -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] +_mod_name_re = re.compile("-") +NORMALIZE_MOD_NAME = lambda x: _mod_name_re.sub("_", x.strip().lower()) - if module_name in EXTENSION_MODULES_LOADED: - logging.warning(f"Skip loading custom nodes from {module_path}. Module already loaded.") +def _load_custom_node(ext_mod, ext_mod_name, ignore, ext_mod_dir=None): + #TODO factor out optionality of ext_mod_dir + 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(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_node(ext_mod_path, ignore): + ext_mod_name = os.path.basename(ext_mod_path) + if os.path.isfile(ext_mod_path): + ext_mod_name, *_ = os.path.splitext(ext_mod_path) + ext_mod_name = NORMALIZE_MOD_NAME(ext_mod_name) + + 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(module_name) + EXTENSION_MODULES_LOADED.add(ext_mod_name) 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] + 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: - module_spec = importlib.util.spec_from_file_location(module_name, os.path.join(module_path, "__init__.py")) - module_dir = module_path + 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 - module = importlib.util.module_from_spec(module_spec) - sys.modules[module_name] = module - module_spec.loader.exec_module(module) - - 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 - - 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 loading custom nodes from {module_path}. No NODE_CLASS_MAPPINGS found in module.") - return False + 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 {module_path} module for custom nodes: {e}") + logging.warning(f"Cannot import {ext_mod_path} module for custom nodes: {e}") return False -def load_custom_nodes(): - base_node_names = set(NODE_CLASS_MAPPINGS.keys()) +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: @@ -1918,12 +1925,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:") @@ -1935,47 +1942,32 @@ def load_custom_nodes(): logging.info("{:6.1f} seconds{}: {}".format(n[0], import_message, n[1])) logging.info("") -def load_custom_nodes_entry_points(): - base_node_names = set(NODE_CLASS_MAPPINGS.keys()) - extension_modules = set() +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) - for ep in entry_points(group="comfyui.web_directory"): - if ep.module in EXTENSION_MODULES_LOADED: - logging.warning(f"Skip loading web_directory entry point from {ep.module}. Module already loaded.") + 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) - web_directory = ep.load() - EXTENSION_WEB_DIRS[module_name] = web_directory - for path in importlib.metadata.files(ep.module): - if path.parent.name == ep.module and path.name == web_directory: - EXTENSION_WEB_DIRS[ep.module] = str(path.locate()) + try: + ext_mod = ep.load() + + # web_directory = ep.load() + # EXTENSION_WEB_DIRS[module_name] = web_directory + # for path in importlib.metadata.files(ep.module): + # if path.parent.name == ep.module and path.name == web_directory: + # EXTENSION_WEB_DIRS[ep.module] = str(path.locate()) - extension_modules.add(ep.module) + #TODO make ext_mod_dir a real value + _load_custom_node(ext_mod=ext_mod, ext_mod_name=ext_mod_name, ext_mod_dir=None, ignore=ignore) - for ep in entry_points(group="comfyui.node_class_mappings"): - if ep.module in EXTENSION_MODULES_LOADED: - logging.warning(f"Skip loading node_class_mappings entry point from {ep.module}. Module already loaded.") + except Exception as e: + logging.warning(traceback.format_exc()) + logging.warning(f"Cannot import custom nodes from {ext_mod_name}: {e}") continue - - class_mapping = ep.load() - for name in class_mapping: - if name not in base_node_names: - NODE_CLASS_MAPPINGS[name] = class_mapping[name] - - extension_modules.add(ep.module) - - for ep in entry_points(group="comfyui.node_display_name_mappings"): - if ep.module in EXTENSION_MODULES_LOADED: - logging.warning(f"Skip loading node_display_name_mappings entry point from {ep.module}. Module already loaded.") - continue - - display_name_mapping = ep.load() - NODE_DISPLAY_NAME_MAPPINGS.update(display_name_mapping) - - extension_modules.add(ep.module) - - # delay adding new module names to the guard set until after all possible entry_points are loaded - EXTENSION_MODULES_LOADED |= extension_modules def init_custom_nodes(): extras_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras") @@ -2021,8 +2013,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_entry_points() - 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") From a441bae96f5e5e00481a6ad018bba133a6ce1da6 Mon Sep 17 00:00:00 2001 From: telamonian Date: Thu, 20 Jun 2024 17:05:55 -0400 Subject: [PATCH 09/11] fix `ignore` arg in `load_custom_node` signature --- nodes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nodes.py b/nodes.py index 419fed43e..39858ae76 100644 --- a/nodes.py +++ b/nodes.py @@ -1885,8 +1885,7 @@ def _load_custom_node(ext_mod, ext_mod_name, ignore, ext_mod_dir=None): logging.warning(f"Skip loading custom nodes from {ext_mod_name}. No NODE_CLASS_MAPPINGS found in module.") return False -def load_custom_node(ext_mod_path, ignore): - ext_mod_name = os.path.basename(ext_mod_path) +def load_custom_node(ext_mod_path, ignore=set()): if os.path.isfile(ext_mod_path): ext_mod_name, *_ = os.path.splitext(ext_mod_path) ext_mod_name = NORMALIZE_MOD_NAME(ext_mod_name) From bac089095ae74636e36cb348c21115bd1441116a Mon Sep 17 00:00:00 2001 From: telamonian Date: Thu, 20 Jun 2024 17:06:35 -0400 Subject: [PATCH 10/11] fixup logic for getting extension module name in `load_custom_node` --- nodes.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nodes.py b/nodes.py index 39858ae76..b4b1e1f90 100644 --- a/nodes.py +++ b/nodes.py @@ -1887,8 +1887,9 @@ def _load_custom_node(ext_mod, ext_mod_name, ignore, ext_mod_dir=None): def load_custom_node(ext_mod_path, ignore=set()): if os.path.isfile(ext_mod_path): - ext_mod_name, *_ = os.path.splitext(ext_mod_path) - ext_mod_name = NORMALIZE_MOD_NAME(ext_mod_name) + 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.") From e350d453dfb2092fb4d0cbe2f13c35342600cd67 Mon Sep 17 00:00:00 2001 From: telamonian Date: Thu, 20 Jun 2024 17:07:50 -0400 Subject: [PATCH 11/11] support finding ext_mod_dir/WEB_DIRECTORY in `load_custom_nodes_entry_points` --- nodes.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/nodes.py b/nodes.py index b4b1e1f90..7b59a426e 100644 --- a/nodes.py +++ b/nodes.py @@ -1867,8 +1867,7 @@ EXTENSION_MODULES_LOADED = set() _mod_name_re = re.compile("-") NORMALIZE_MOD_NAME = lambda x: _mod_name_re.sub("_", x.strip().lower()) -def _load_custom_node(ext_mod, ext_mod_name, ignore, ext_mod_dir=None): - #TODO factor out optionality of ext_mod_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): @@ -1954,15 +1953,9 @@ def load_custom_nodes_entry_points(ignore=set()): try: ext_mod = ep.load() + ext_mod_dir = os.path.dirname(os.path.realpath(ext_mod.__file__)) - # web_directory = ep.load() - # EXTENSION_WEB_DIRS[module_name] = web_directory - # for path in importlib.metadata.files(ep.module): - # if path.parent.name == ep.module and path.name == web_directory: - # EXTENSION_WEB_DIRS[ep.module] = str(path.locate()) - - #TODO make ext_mod_dir a real value - _load_custom_node(ext_mod=ext_mod, ext_mod_name=ext_mod_name, ext_mod_dir=None, ignore=ignore) + _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())