From 9d472b7a27b926e3c676a5738d618f8a0444ab45 Mon Sep 17 00:00:00 2001 From: WAS Date: Sun, 25 Jun 2023 13:48:28 -0700 Subject: [PATCH 1/4] Add custom_nodes monitoring daemon --- nodes.py | 88 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 20 deletions(-) diff --git a/nodes.py b/nodes.py index 7280d7880..82942bd41 100644 --- a/nodes.py +++ b/nodes.py @@ -15,7 +15,7 @@ import safetensors.torch sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy")) - +from comfy.cli_args import args import comfy.diffusers_load import comfy.samplers import comfy.sample @@ -26,6 +26,8 @@ import comfy.clip_vision import comfy.model_management import importlib +import threading +import traceback import folder_paths import latent_preview @@ -37,6 +39,7 @@ def interrupt_processing(value=True): comfy.model_management.interrupt_current_processing(value) MAX_RESOLUTION=8192 +NODE_MODIFICATION_TIMES = {} class CLIPTextEncode: @classmethod @@ -1408,29 +1411,46 @@ NODE_DISPLAY_NAME_MAPPINGS = { } def load_custom_node(module_path): - module_name = os.path.basename(module_path) + + def update_modified_times(module_path): + if os.path.isdir(module_path): + for root, _, files in os.walk(module_path): + for file_name in files: + file_path = os.path.join(root, file_name) + if file_name.endswith(".py"): + NODE_MODIFICATION_TIMES[file_path] = os.path.getmtime(file_path) + else: + NODE_MODIFICATION_TIMES[module_path] = os.path.getmtime(module_path) + + if os.path.isfile(module_path): + module_name = os.path.splitext(os.path.basename(module_path))[0] + else: + module_name = os.path.basename(module_path) if os.path.isfile(module_path): sp = os.path.splitext(module_path) module_name = sp[0] try: if os.path.isfile(module_path): - module_spec = importlib.util.spec_from_file_location(module_name, module_path) + loader = importlib.machinery.SourceFileLoader(module_name, module_path) else: - module_spec = importlib.util.spec_from_file_location(module_name, os.path.join(module_path, "__init__.py")) - module = importlib.util.module_from_spec(module_spec) + loader = importlib.machinery.SourceFileLoader(module_name, os.path.join(module_path, "__init__.py")) + module = loader.load_module() sys.modules[module_name] = module - module_spec.loader.exec_module(module) + if hasattr(module, "NODE_CLASS_MAPPINGS") and getattr(module, "NODE_CLASS_MAPPINGS") is not None: NODE_CLASS_MAPPINGS.update(module.NODE_CLASS_MAPPINGS) 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: - print(f"Skip {module_path} module for custom nodes due to the lack of NODE_CLASS_MAPPINGS.") - return False + + update_modified_times(module_path) + + return True + except Exception as e: print(traceback.format_exc()) print(f"Cannot import {module_path} module for custom nodes:", e) + update_modified_times(module_path) + return False def load_custom_nodes(): @@ -1443,8 +1463,10 @@ def load_custom_nodes(): 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 + if os.path.isfile(module_path) and os.path.splitext(module_path)[1] != ".py": + continue + if module_path.endswith(".disabled"): + continue time_before = time.perf_counter() success = load_custom_node(module_path) node_import_times.append((time.perf_counter() - time_before, module_path, success)) @@ -1456,15 +1478,41 @@ def load_custom_nodes(): import_message = "" else: import_message = " (IMPORT FAILED)" - print("{:6.1f} seconds{}:".format(n[0], import_message), n[1]) + print("{:6.1f} seconds{}:".format(n[0], import_message), os.path.basename(n[1])) print() +def start_custom_node_monitor(): + + def monitor_custom_nodes(): + while True: + try: + for file_path, modification_time in list(NODE_MODIFICATION_TIMES.items()): + current_modification_time = os.path.getmtime(file_path) + if current_modification_time != modification_time: + print(f"{os.path.basename(file_path)} has been modified. Reloading.") + success = load_custom_node(file_path) + if success: + print(f"{os.path.basename(file_path)} has been reloaded.") + else: + print(f"Reloading {os.path.basename(file_path)} failed.") + time.sleep(5) + except Exception as e: + print("An error occurred in the monitoring loop:") + print(e) + print(traceback.format_exc()) + + monitor_thread = threading.Thread(target=monitor_custom_nodes) + monitor_thread.daemon = True + monitor_thread.start() + def init_custom_nodes(): - load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_hypernetwork.py")) - load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_upscale_model.py")) - load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_post_processing.py")) - load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_mask.py")) - load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_rebatch.py")) - load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_model_merging.py")) - load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_tomesd.py")) + module_directory = os.path.dirname(os.path.abspath(__file__)) + load_custom_node(os.path.join(module_directory, "comfy_extras", "nodes_hypernetwork.py")) + load_custom_node(os.path.join(module_directory, "comfy_extras", "nodes_upscale_model.py")) + load_custom_node(os.path.join(module_directory, "comfy_extras", "nodes_post_processing.py")) + load_custom_node(os.path.join(module_directory, "comfy_extras", "nodes_mask.py")) + load_custom_node(os.path.join(module_directory, "comfy_extras", "nodes_rebatch.py")) load_custom_nodes() + if args.monitor_nodes: + print("Monitoring custom nodes for modifications.\n") + start_custom_node_monitor() From f768f05dbb1087547b516d63b2ce6230b1c8173f Mon Sep 17 00:00:00 2001 From: WAS Date: Sun, 25 Jun 2023 13:49:58 -0700 Subject: [PATCH 2/4] Add --monitor-nodes launch argument `--monitor-nodes` launch argument/flag is required to monitor custom_nodes for changes. --- comfy/cli_args.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/comfy/cli_args.py b/comfy/cli_args.py index f1306ef7f..1faaf92e7 100644 --- a/comfy/cli_args.py +++ b/comfy/cli_args.py @@ -66,11 +66,12 @@ vram_group.add_argument("--lowvram", action="store_true", help="Split the unet i vram_group.add_argument("--novram", action="store_true", help="When lowvram isn't enough.") vram_group.add_argument("--cpu", action="store_true", help="To use the CPU for everything (slow).") - parser.add_argument("--dont-print-server", action="store_true", help="Don't print server output.") parser.add_argument("--quick-test-for-ci", action="store_true", help="Quick test for CI.") parser.add_argument("--windows-standalone-build", action="store_true", help="Windows standalone build: Enable convenient things that most people using the standalone windows build will probably enjoy (like auto opening the page on startup).") +parser.add_argument("--monitor-nodes", action="store_true", help="Monitor custom_nodes for modifications, and reload them.") + args = parser.parse_args() if args.windows_standalone_build: From d8a953ef53eac255a9b87f3c2883dac5909ace0b Mon Sep 17 00:00:00 2001 From: WAS Date: Tue, 27 Jun 2023 14:49:04 -0700 Subject: [PATCH 3/4] Add support for submodules with no __init__ --- nodes.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/nodes.py b/nodes.py index 82942bd41..689c14725 100644 --- a/nodes.py +++ b/nodes.py @@ -1411,7 +1411,6 @@ NODE_DISPLAY_NAME_MAPPINGS = { } def load_custom_node(module_path): - def update_modified_times(module_path): if os.path.isdir(module_path): for root, _, files in os.walk(module_path): @@ -1424,16 +1423,30 @@ def load_custom_node(module_path): if os.path.isfile(module_path): module_name = os.path.splitext(os.path.basename(module_path))[0] + loader = importlib.machinery.SourceFileLoader(module_name, module_path) else: module_name = os.path.basename(module_path) - if os.path.isfile(module_path): - sp = os.path.splitext(module_path) - module_name = sp[0] - try: - if os.path.isfile(module_path): - loader = importlib.machinery.SourceFileLoader(module_name, module_path) + init_file_path = os.path.join(module_path, "__init__.py") + if os.path.exists(init_file_path): + loader = importlib.machinery.SourceFileLoader(module_name, init_file_path) else: - loader = importlib.machinery.SourceFileLoader(module_name, os.path.join(module_path, "__init__.py")) + file_paths = [os.path.join(module_path, file_name) for file_name in os.listdir(module_path) if file_name.endswith(".py")] + for file_path in file_paths: + sub_module_name = os.path.splitext(os.path.basename(file_path))[0] + sub_loader = importlib.machinery.SourceFileLoader(sub_module_name, file_path) + sub_module = sub_loader.load_module() + sys.modules[sub_module_name] = sub_module + + if hasattr(sub_module, "NODE_CLASS_MAPPINGS") and getattr(sub_module, "NODE_CLASS_MAPPINGS") is not None: + NODE_CLASS_MAPPINGS.update(sub_module.NODE_CLASS_MAPPINGS) + if hasattr(sub_module, "NODE_DISPLAY_NAME_MAPPINGS") and getattr(sub_module, "NODE_DISPLAY_NAME_MAPPINGS") is not None: + NODE_DISPLAY_NAME_MAPPINGS.update(sub_module.NODE_DISPLAY_NAME_MAPPINGS) + + # Update modified times for the main module path + update_modified_times(module_path) + return True + + try: module = loader.load_module() sys.modules[module_name] = module @@ -1445,13 +1458,12 @@ def load_custom_node(module_path): update_modified_times(module_path) return True - + except Exception as e: print(traceback.format_exc()) print(f"Cannot import {module_path} module for custom nodes:", e) update_modified_times(module_path) - - return False + return False def load_custom_nodes(): node_paths = folder_paths.get_folder_paths("custom_nodes") From 15385b592d6302426401b9d9dc25d2a025d8e232 Mon Sep 17 00:00:00 2001 From: WAS Date: Tue, 27 Jun 2023 15:03:08 -0700 Subject: [PATCH 4/4] Handle parent packages to allow for relative imports --- nodes.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/nodes.py b/nodes.py index 689c14725..25dbac7df 100644 --- a/nodes.py +++ b/nodes.py @@ -1421,34 +1421,37 @@ def load_custom_node(module_path): else: NODE_MODIFICATION_TIMES[module_path] = os.path.getmtime(module_path) + + parent_dir = os.path.dirname(module_path) + parent_package = os.path.basename(parent_dir) + if os.path.isfile(module_path): module_name = os.path.splitext(os.path.basename(module_path))[0] - loader = importlib.machinery.SourceFileLoader(module_name, module_path) + loader = importlib.machinery.SourceFileLoader(f"{parent_package}.{module_name}", module_path) else: module_name = os.path.basename(module_path) init_file_path = os.path.join(module_path, "__init__.py") if os.path.exists(init_file_path): - loader = importlib.machinery.SourceFileLoader(module_name, init_file_path) + loader = importlib.machinery.SourceFileLoader(f"{parent_package}.{module_name}", init_file_path) else: file_paths = [os.path.join(module_path, file_name) for file_name in os.listdir(module_path) if file_name.endswith(".py")] for file_path in file_paths: sub_module_name = os.path.splitext(os.path.basename(file_path))[0] - sub_loader = importlib.machinery.SourceFileLoader(sub_module_name, file_path) + sub_loader = importlib.machinery.SourceFileLoader(f"{parent_package}.{module_name}.{sub_module_name}", file_path) sub_module = sub_loader.load_module() - sys.modules[sub_module_name] = sub_module + sys.modules[f"{parent_package}.{module_name}.{sub_module_name}"] = sub_module if hasattr(sub_module, "NODE_CLASS_MAPPINGS") and getattr(sub_module, "NODE_CLASS_MAPPINGS") is not None: NODE_CLASS_MAPPINGS.update(sub_module.NODE_CLASS_MAPPINGS) if hasattr(sub_module, "NODE_DISPLAY_NAME_MAPPINGS") and getattr(sub_module, "NODE_DISPLAY_NAME_MAPPINGS") is not None: NODE_DISPLAY_NAME_MAPPINGS.update(sub_module.NODE_DISPLAY_NAME_MAPPINGS) - # Update modified times for the main module path update_modified_times(module_path) return True try: module = loader.load_module() - sys.modules[module_name] = module + sys.modules[f"{parent_package}.{module_name}"] = module if hasattr(module, "NODE_CLASS_MAPPINGS") and getattr(module, "NODE_CLASS_MAPPINGS") is not None: NODE_CLASS_MAPPINGS.update(module.NODE_CLASS_MAPPINGS) @@ -1463,7 +1466,7 @@ def load_custom_node(module_path): print(traceback.format_exc()) print(f"Cannot import {module_path} module for custom nodes:", e) update_modified_times(module_path) - return False + return False def load_custom_nodes(): node_paths = folder_paths.get_folder_paths("custom_nodes")