Skip to content

Commit a26219b

Browse files
author
Peyton
committed
Hotfix for node class names with trailing white spaces. Removed the need to import functions from the utils file when running the generated code.
1 parent e46283d commit a26219b

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

comfyui_to_python.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from typing import Dict, List, Any, Callable, Tuple
1212

13-
from utils import import_custom_nodes, add_comfyui_directories_to_sys_path
13+
from utils import import_custom_nodes, add_comfyui_directory_to_sys_path, get_value_at_index
1414

1515

1616
sys.path.append('../')
@@ -218,7 +218,7 @@ def generate_workflow(self, load_order: List, filename: str = 'generated_code_wo
218218
continue
219219

220220
class_type, import_statement, class_code = self.get_class_info(class_type)
221-
initialized_objects[class_type] = class_type.lower()
221+
initialized_objects[class_type] = class_type.lower().strip()
222222
if class_type in self.base_node_class_mappings.keys():
223223
import_statements.add(import_statement)
224224
if class_type not in self.base_node_class_mappings.keys():
@@ -232,7 +232,7 @@ def generate_workflow(self, load_order: List, filename: str = 'generated_code_wo
232232
inputs = {key: value for key, value in inputs.items() if key in class_def_params}
233233

234234
# Create executed variable and generate code
235-
executed_variables[idx] = f'{class_type.lower()}_{idx}'
235+
executed_variables[idx] = f'{class_type.lower().strip()}_{idx}'
236236
inputs = self.update_inputs(inputs, executed_variables)
237237

238238
if is_special_function:
@@ -302,14 +302,16 @@ def assemble_python_code(self, import_statements: set, speical_functions_code: L
302302
Returns:
303303
str: Generated final code as a string.
304304
"""
305-
# Get the source code of the function as a string
306-
add_comfyui_directories_to_sys_path_code = inspect.getsource(add_comfyui_directories_to_sys_path)
305+
# Get the source code of the utils functions as a string
306+
func_strings = []
307+
for func in [add_comfyui_directory_to_sys_path, get_value_at_index]:
308+
func_strings.append(f'\n{inspect.getsource(func)}')
307309
# Define static import statements required for the script
308-
static_imports = ['import os', 'import random', 'import sys', 'import torch', f'\n{add_comfyui_directories_to_sys_path_code}',
309-
'\n\nadd_comfyui_directories_to_sys_path()']
310+
static_imports = ['import os', 'import random', 'import sys', 'from typing import Sequence, Mapping, Any, Union',
311+
'import torch'] + func_strings + ['\n\nadd_comfyui_directory_to_sys_path()']
310312
# Check if custom nodes should be included
311313
if custom_nodes:
312-
static_imports.append('\nfrom utils import import_custom_nodes, get_value_at_index\n')
314+
static_imports.append(f'\n{inspect.getsource(import_custom_nodes)}\n')
313315
custom_nodes = 'import_custom_nodes()\n\t'
314316
else:
315317
custom_nodes = ''
@@ -336,9 +338,9 @@ def get_class_info(self, class_type: str) -> Tuple[str, str, str]:
336338
"""
337339
import_statement = class_type
338340
if class_type in self.base_node_class_mappings.keys():
339-
class_code = f'{class_type.lower()} = {class_type}()'
341+
class_code = f'{class_type.lower().strip()} = {class_type.strip()}()'
340342
else:
341-
class_code = f'{class_type.lower()} = NODE_CLASS_MAPPINGS["{class_type}"]()'
343+
class_code = f'{class_type.lower().strip()} = NODE_CLASS_MAPPINGS["{class_type}"]()'
342344

343345
return class_type, import_statement, class_code
344346

utils.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
import asyncio
2-
import json
3-
import glob
41
import os
52
from typing import Sequence, Mapping, Any, Union
63
import sys
74

85
sys.path.append('../')
96

10-
import execution
11-
from nodes import init_custom_nodes
12-
137

148
def import_custom_nodes() -> None:
159
"""Find all custom nodes in the custom_nodes folder and add those node objects to NODE_CLASS_MAPPINGS
1610
1711
This function sets up a new asyncio event loop, initializes the PromptServer,
1812
creates a PromptQueue, and initializes the custom nodes.
1913
"""
14+
import asyncio
15+
import execution
16+
from nodes import init_custom_nodes
2017
import server
2118

2219
# Creating a new event loop and setting it as the default loop
@@ -30,21 +27,19 @@ def import_custom_nodes() -> None:
3027
# Initializing custom nodes
3128
init_custom_nodes()
3229

33-
34-
def add_comfyui_directories_to_sys_path() -> None:
30+
def add_comfyui_directory_to_sys_path() -> None:
3531
"""
36-
Recursively looks at parent folders starting from the current working directory until it finds 'ComfyUI' and 'ComfyUI-to-Python-Extension'.
37-
Once found, the directories are added to sys.path.
32+
Recursively looks at parent folders starting from the current working directory until it finds 'ComfyUI'.
33+
Once found, the directory is added to sys.path.
3834
"""
3935
start_path = os.getcwd() # Get the current working directory
4036

4137
def search_directory(path: str) -> None:
42-
# Check if the current directory contains 'ComfyUI' or 'ComfyUI-to-Python-Extension'
43-
for directory_name in ['ComfyUI', 'ComfyUI-to-Python-Extension']:
44-
if directory_name in os.listdir(path):
45-
directory_path = os.path.join(path, directory_name)
46-
sys.path.append(directory_path)
47-
print(f"'{directory_name}' found and added to sys.path: {directory_path}")
38+
# Check if the current directory contains 'ComfyUI'
39+
if 'ComfyUI' in os.listdir(path):
40+
directory_path = os.path.join(path, 'ComfyUI')
41+
sys.path.append(directory_path)
42+
print(f"ComfyUI found and added to sys.path: {directory_path}")
4843

4944
# Get the parent directory
5045
parent_directory = os.path.dirname(path)

0 commit comments

Comments
 (0)