Merge e350d453dfb2092fb4d0cbe2f13c35342600cd67 into 69d710e40f43b87dd7124b9ee1e8982f987827e9

This commit is contained in:
Max Klein 2024-06-25 11:07:54 -04:00 committed by GitHub
commit 4d009e69c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 85 additions and 42 deletions

6
.gitignore vendored
View File

@ -1,5 +1,6 @@
__pycache__/ __pycache__/
*.py[cod] *.py[cod]
*.egg-info/
/output/ /output/
/input/ /input/
!/input/example.png !/input/example.png
@ -15,4 +16,7 @@ venv/
!/web/extensions/logging.js.example !/web/extensions/logging.js.example
!/web/extensions/core/ !/web/extensions/core/
/tests-ui/data/object_info.json /tests-ui/data/object_info.json
/user/ /user/
.history/
.vscode/
*.code-workspace

121
nodes.py
View File

@ -9,6 +9,7 @@ import math
import time import time
import random import random
import logging import logging
import re
from PIL import Image, ImageOps, ImageSequence, ImageFile from PIL import Image, ImageOps, ImageSequence, ImageFile
from PIL.PngImagePlugin import PngInfo from PIL.PngImagePlugin import PngInfo
@ -31,6 +32,7 @@ import comfy.model_management
from comfy.cli_args import args from comfy.cli_args import args
import importlib import importlib
from importlib.metadata import entry_points
import folder_paths import folder_paths
import latent_preview import latent_preview
@ -1887,46 +1889,60 @@ NODE_DISPLAY_NAME_MAPPINGS = {
EXTENSION_WEB_DIRS = {} EXTENSION_WEB_DIRS = {}
def load_custom_node(module_path, ignore=set()): EXTENSION_MODULES_LOADED = 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
module = importlib.util.module_from_spec(module_spec) _mod_name_re = re.compile("-")
sys.modules[module_name] = module NORMALIZE_MOD_NAME = lambda x: _mod_name_re.sub("_", x.strip().lower())
module_spec.loader.exec_module(module)
if hasattr(module, "WEB_DIRECTORY") and getattr(module, "WEB_DIRECTORY") is not None: def _load_custom_node(ext_mod, ext_mod_name, ext_mod_dir, ignore):
web_dir = os.path.abspath(os.path.join(module_dir, getattr(module, "WEB_DIRECTORY"))) if ext_mod_dir is not None and hasattr(ext_mod, "WEB_DIRECTORY") and getattr(ext_mod, "WEB_DIRECTORY") is not None:
if os.path.isdir(web_dir): web_dir = os.path.abspath(os.path.join(ext_mod_dir, getattr(ext_mod, "WEB_DIRECTORY")))
EXTENSION_WEB_DIRS[module_name] = web_dir 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: if hasattr(ext_mod, "NODE_CLASS_MAPPINGS") and getattr(ext_mod, "NODE_CLASS_MAPPINGS") is not None:
for name in module.NODE_CLASS_MAPPINGS: for node_class_name in ext_mod.NODE_CLASS_MAPPINGS:
if name not in ignore: if node_class_name not in ignore:
NODE_CLASS_MAPPINGS[name] = module.NODE_CLASS_MAPPINGS[name] NODE_CLASS_MAPPINGS[node_class_name] = ext_mod.NODE_CLASS_MAPPINGS[node_class_name]
if hasattr(module, "NODE_DISPLAY_NAME_MAPPINGS") and getattr(module, "NODE_DISPLAY_NAME_MAPPINGS") is not None: if hasattr(ext_mod, "NODE_DISPLAY_NAME_MAPPINGS") and getattr(ext_mod, "NODE_DISPLAY_NAME_MAPPINGS") is not None:
NODE_DISPLAY_NAME_MAPPINGS.update(module.NODE_DISPLAY_NAME_MAPPINGS) NODE_DISPLAY_NAME_MAPPINGS.update(ext_mod.NODE_DISPLAY_NAME_MAPPINGS)
return True return True
else: 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 {ext_mod_name}. No NODE_CLASS_MAPPINGS found in module.")
return False
except Exception as e:
logging.warning(traceback.format_exc())
logging.warning(f"Cannot import {module_path} module for custom nodes: {e}")
return False return False
def load_custom_nodes(): def load_custom_node(ext_mod_path, ignore=set()):
base_node_names = set(NODE_CLASS_MAPPINGS.keys()) 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_paths = folder_paths.get_folder_paths("custom_nodes")
node_import_times = [] node_import_times = []
for custom_node_path in node_paths: for custom_node_path in node_paths:
@ -1935,12 +1951,12 @@ def load_custom_nodes():
possible_modules.remove("__pycache__") possible_modules.remove("__pycache__")
for possible_module in possible_modules: for possible_module in possible_modules:
module_path = os.path.join(custom_node_path, possible_module) ext_mod_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 os.path.isfile(ext_mod_path) and os.path.splitext(ext_mod_path)[1] != ".py": continue
if module_path.endswith(".disabled"): continue if ext_mod_path.endswith(".disabled"): continue
time_before = time.perf_counter() time_before = time.perf_counter()
success = load_custom_node(module_path, base_node_names) success = load_custom_node(ext_mod_path, ignore)
node_import_times.append((time.perf_counter() - time_before, module_path, success)) node_import_times.append((time.perf_counter() - time_before, ext_mod_path, success))
if len(node_import_times) > 0: if len(node_import_times) > 0:
logging.info("\nImport times for custom nodes:") 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("{:6.1f} seconds{}: {}".format(n[0], import_message, n[1]))
logging.info("") 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(): def init_custom_nodes():
extras_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras") extras_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras")
extras_files = [ extras_files = [
@ -1999,7 +2036,9 @@ def init_custom_nodes():
if not load_custom_node(os.path.join(extras_dir, node_file)): if not load_custom_node(os.path.join(extras_dir, node_file)):
import_failed.append(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: 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") logging.warning("WARNING: some comfy_extras/ nodes did not import correctly. This may be because they are missing some dependencies.\n")