Fix ProjectLoaderDynamic output names lost on page reload

Hidden widget values for output_keys/output_types were not reliably
restored by ComfyUI on workflow reload. Store keys/types in
node.properties (always persisted by LiteGraph) as primary storage,
with hidden widgets as fallback.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-28 23:04:14 +01:00
parent 86693f608a
commit 027ef8e78a

View File

@@ -117,7 +117,12 @@ app.registerExtension({
return; return;
} }
// Store keys and types in hidden widgets for persistence (JSON-encoded) // Store keys and types for persistence
// Properties are always reliably serialized by LiteGraph
this.properties = this.properties || {};
this.properties._output_keys = keys;
this.properties._output_types = types;
// Also update hidden widgets for Python-side access
const okWidget = this.widgets?.find(w => w.name === "output_keys"); const okWidget = this.widgets?.find(w => w.name === "output_keys");
if (okWidget) okWidget.value = JSON.stringify(keys); if (okWidget) okWidget.value = JSON.stringify(keys);
const otWidget = this.widgets?.find(w => w.name === "output_types"); const otWidget = this.widgets?.find(w => w.name === "output_types");
@@ -197,15 +202,19 @@ app.registerExtension({
const okWidget = this.widgets?.find(w => w.name === "output_keys"); const okWidget = this.widgets?.find(w => w.name === "output_keys");
const otWidget = this.widgets?.find(w => w.name === "output_types"); const otWidget = this.widgets?.find(w => w.name === "output_types");
// Parse keys/types — try JSON array first, fall back to comma-split // Read keys/types — properties (always persisted) first, then widgets
let keys = []; let keys = [];
if (okWidget?.value) { if (Array.isArray(this.properties?._output_keys) && this.properties._output_keys.length > 0) {
keys = this.properties._output_keys;
} else if (okWidget?.value) {
try { keys = JSON.parse(okWidget.value); } catch (_) { try { keys = JSON.parse(okWidget.value); } catch (_) {
keys = okWidget.value.split(",").map(k => k.trim()).filter(Boolean); keys = okWidget.value.split(",").map(k => k.trim()).filter(Boolean);
} }
} }
let types = []; let types = [];
if (otWidget?.value) { if (Array.isArray(this.properties?._output_types) && this.properties._output_types.length > 0) {
types = this.properties._output_types;
} else if (otWidget?.value) {
try { types = JSON.parse(otWidget.value); } catch (_) { try { types = JSON.parse(otWidget.value); } catch (_) {
types = otWidget.value.split(",").map(t => t.trim()).filter(Boolean); types = otWidget.value.split(",").map(t => t.trim()).filter(Boolean);
} }
@@ -259,11 +268,16 @@ app.registerExtension({
this.removeOutput(this.outputs.length - 1); this.removeOutput(this.outputs.length - 1);
} }
} else if (this.outputs.length > 1) { } else if (this.outputs.length > 1) {
// Widget values empty but serialized dynamic outputs exist — sync widgets // Widget/property values empty but serialized dynamic outputs exist —
// from the outputs LiteGraph already restored (fallback, skip slot 0). // sync from the outputs LiteGraph already restored (fallback, skip slot 0).
const dynamicOutputs = this.outputs.slice(1); const dynamicOutputs = this.outputs.slice(1);
if (okWidget) okWidget.value = JSON.stringify(dynamicOutputs.map(o => o.name)); const restoredKeys = dynamicOutputs.map(o => o.name);
if (otWidget) otWidget.value = JSON.stringify(dynamicOutputs.map(o => o.type)); const restoredTypes = dynamicOutputs.map(o => o.type);
this.properties = this.properties || {};
this.properties._output_keys = restoredKeys;
this.properties._output_types = restoredTypes;
if (okWidget) okWidget.value = JSON.stringify(restoredKeys);
if (otWidget) otWidget.value = JSON.stringify(restoredTypes);
} }
this.setSize(this.computeSize()); this.setSize(this.computeSize());