Fix workflow metadata and subgraph export names
This commit is contained in:
+28
-14
@@ -194,6 +194,15 @@ class CodeGenerator:
|
||||
self.node_class_mappings = node_class_mappings
|
||||
self.base_node_class_mappings = base_node_class_mappings
|
||||
|
||||
@staticmethod
|
||||
def sanitize_node_id(node_id: str) -> str:
|
||||
"""Convert node IDs into variable-safe tokens without collapsing separators."""
|
||||
sanitized = re.sub(r"[^a-z0-9_]", "_", str(node_id).lower().strip())
|
||||
sanitized = re.sub(r"_+", "_", sanitized).strip("_")
|
||||
if not sanitized:
|
||||
sanitized = "node"
|
||||
return sanitized
|
||||
|
||||
def generate_workflow(
|
||||
self,
|
||||
load_order: List,
|
||||
@@ -284,7 +293,7 @@ class CodeGenerator:
|
||||
# Create executed variable and generate code
|
||||
executed_variables[idx] = (
|
||||
f"{self.clean_variable_name(class_type)}_"
|
||||
f"{self.clean_variable_name(str(idx))}"
|
||||
f"{self.sanitize_node_id(str(idx))}"
|
||||
)
|
||||
inputs = self.update_inputs(inputs, executed_variables)
|
||||
|
||||
@@ -315,7 +324,7 @@ class CodeGenerator:
|
||||
special_functions_code,
|
||||
code,
|
||||
workflow_data,
|
||||
metadata_workflow_data or workflow_data,
|
||||
metadata_workflow_data,
|
||||
queue_size,
|
||||
custom_nodes,
|
||||
)
|
||||
@@ -386,7 +395,7 @@ class CodeGenerator:
|
||||
speical_functions_code: List[str],
|
||||
code: List[str],
|
||||
workflow_data: Dict,
|
||||
metadata_workflow_data: Dict,
|
||||
metadata_workflow_data: Dict | None,
|
||||
queue_size: int,
|
||||
custom_nodes=False,
|
||||
) -> str:
|
||||
@@ -397,13 +406,23 @@ class CodeGenerator:
|
||||
speical_functions_code (List[str]): A list of special functions code strings.
|
||||
code (List[str]): A list of code strings.
|
||||
workflow_data (Dict): The API workflow data used for runtime prompt execution.
|
||||
metadata_workflow_data (Dict): The workflow metadata to embed into saved outputs.
|
||||
metadata_workflow_data (Dict | None): The workflow metadata to embed into saved outputs.
|
||||
queue_size (int): Number of photos that will be generated by the script.
|
||||
custom_nodes (bool): Whether to include custom nodes in the code.
|
||||
|
||||
Returns:
|
||||
str: Generated final code as a string.
|
||||
"""
|
||||
if metadata_workflow_data is None:
|
||||
extra_pnginfo_code = "extra_pnginfo = None"
|
||||
else:
|
||||
extra_pnginfo_code = (
|
||||
"extra_pnginfo = "
|
||||
'{"workflow": json.loads('
|
||||
+ json.dumps(json.dumps(metadata_workflow_data))
|
||||
+ ")}"
|
||||
)
|
||||
|
||||
# Get the source code of the utils functions as a string
|
||||
func_strings = []
|
||||
for func in [
|
||||
@@ -429,12 +448,7 @@ class CodeGenerator:
|
||||
"\n\nadd_comfyui_directory_to_sys_path()\nadd_extra_model_paths()\n",
|
||||
f"workflow = json.loads({json.dumps(json.dumps(workflow_data))})",
|
||||
"prompt = json.loads(json.dumps(workflow))",
|
||||
(
|
||||
"extra_pnginfo = "
|
||||
'{"workflow": json.loads('
|
||||
+ json.dumps(json.dumps(metadata_workflow_data))
|
||||
+ ")}"
|
||||
),
|
||||
extra_pnginfo_code,
|
||||
]
|
||||
)
|
||||
# Check if custom nodes should be included
|
||||
@@ -628,12 +642,12 @@ class ComfyUItoPython:
|
||||
else:
|
||||
data = json.loads(self.workflow)
|
||||
|
||||
frontend_workflow_data = None
|
||||
metadata_workflow_data = None
|
||||
if self.frontend_workflow:
|
||||
if isinstance(self.frontend_workflow, str):
|
||||
frontend_workflow_data = json.loads(self.frontend_workflow)
|
||||
metadata_workflow_data = json.loads(self.frontend_workflow)
|
||||
else:
|
||||
frontend_workflow_data = self.frontend_workflow
|
||||
metadata_workflow_data = self.frontend_workflow
|
||||
|
||||
# Step 2: Initialize extra/custom nodes when requested or when the workflow references
|
||||
# a node class that is not currently loaded in the runtime.
|
||||
@@ -657,7 +671,7 @@ class ComfyUItoPython:
|
||||
generated_code = code_generator.generate_workflow(
|
||||
load_order,
|
||||
data,
|
||||
frontend_workflow_data,
|
||||
metadata_workflow_data,
|
||||
queue_size=self.queue_size,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user