diff --git a/src/lib/litegraph/src/subgraph/SubgraphNode.ts b/src/lib/litegraph/src/subgraph/SubgraphNode.ts index 57c86894c..8ee6dcdc6 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphNode.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphNode.ts @@ -16,7 +16,10 @@ import type { GraphOrSubgraph, Subgraph } from '@/lib/litegraph/src/subgraph/Subgraph' -import type { ExportedSubgraphInstance } from '@/lib/litegraph/src/types/serialisation' +import type { + ExportedSubgraphInstance, + ISerialisedNode +} from '@/lib/litegraph/src/types/serialisation' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import type { UUID } from '@/lib/litegraph/src/utils/uuid' import { toConcreteWidget } from '@/lib/litegraph/src/widgets/widgetMap' @@ -540,4 +543,36 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { } } } + + /** + * Synchronizes widget values from this SubgraphNode instance to the + * corresponding widgets in the subgraph definition before serialization. + * This ensures nested subgraph widget values are preserved when saving. + */ + override serialize(): ISerialisedNode { + // Sync widget values to subgraph definition before serialization + for (let i = 0; i < this.widgets.length; i++) { + const widget = this.widgets[i] + const input = this.inputs.find((inp) => inp.name === widget.name) + + if (input) { + const subgraphInput = this.subgraph.inputNode.slots.find( + (slot) => slot.name === input.name + ) + + if (subgraphInput) { + // Find all widgets connected to this subgraph input + const connectedWidgets = subgraphInput.getConnectedWidgets() + + // Update the value of all connected widgets + for (const connectedWidget of connectedWidgets) { + connectedWidget.value = widget.value + } + } + } + } + + // Call parent serialize method + return super.serialize() + } }