Add support for custom load order of external nodes\n\n- Load modules based on custom_load_order.txt if available.\n- Modules not listed are loaded after those specified.

This commit is contained in:
Mr. Casey J Sullivan 2024-09-13 10:37:49 -04:00
parent cf80d28689
commit 7845c36921
No known key found for this signature in database
GPG Key ID: 419BEFB8EB3B48C5

View File

@ -2027,11 +2027,31 @@ def init_external_custom_nodes():
base_node_names = set(NODE_CLASS_MAPPINGS.keys())
node_paths = folder_paths.get_folder_paths("custom_nodes")
node_import_times = []
# Path to the file specifying the load order for custom modules.
# Each line in the file should represent a module name.
module_order_path = 'custom_nodes/custom_load_order.txt'
# If the file exists, read module names from it; otherwise, use an empty list.
if os.path.exists(module_order_path):
with open(module_order_path, 'r') as file:
ordered_modules = [line.strip() for line in file]
else:
ordered_modules = []
# Create a dictionary that maps module names to their index in 'ordered_modules'.
# Modules not in this list will be assigned 'inf' to load them last.
order_map = {name: i for i, name in enumerate(ordered_modules)}
for custom_node_path in node_paths:
possible_modules = os.listdir(os.path.realpath(custom_node_path))
if "__pycache__" in possible_modules:
possible_modules.remove("__pycache__")
# Sort modules based on their order in 'order_map'.
# Modules without an entry are sorted to load after those listed.
possible_modules.sort(key=lambda x: order_map.get(x, float('inf')))
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