From 80528da463791c0c03dff1c738405e52b787fdf9 Mon Sep 17 00:00:00 2001 From: huchenlei Date: Thu, 4 Jul 2024 10:38:27 -0400 Subject: [PATCH] Use module name relative to root dir --- nodes.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/nodes.py b/nodes.py index 10b091a6f..81efecf65 100644 --- a/nodes.py +++ b/nodes.py @@ -1887,11 +1887,33 @@ NODE_DISPLAY_NAME_MAPPINGS = { EXTENSION_WEB_DIRS = {} -def load_custom_node(module_path, ignore=set()): - module_name = os.path.basename(module_path) - # Drop '.py' file extension + +def get_module_name(module_path: str) -> str: + """ + Returns the module name based on the given module path. + Examples: + get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node.py") -> "custom_nodes.my_custom_node" + get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node") -> "custom_nodes.my_custom_node" + get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node/") -> "custom_nodes.my_custom_node" + get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node/__init__.py") -> "custom_nodes.my_custom_node" + get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node/__init__") -> "custom_nodes.my_custom_node" + get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node/__init__/") -> "custom_nodes.my_custom_node" + get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node.disabled") -> "custom_nodes.my + + Args: + module_path (str): The path of the module. + + Returns: + str: The module name. + """ + relative_path = os.path.relpath(module_path, folder_paths.base_path) if os.path.isfile(module_path): - module_name = os.path.splitext(module_name)[0] + relative_path = os.path.splitext(relative_path)[0] + return relative_path.replace(os.sep, '.') + + +def load_custom_node(module_path, ignore=set()): + module_name = get_module_name(module_path) try: logging.debug("Trying to load custom node {}".format(module_path)) if os.path.isfile(module_path):