|
295 | 295 | }; |
296 | 296 | //no need to define node.configure, the default method detects node.subgraph and passes the object to node.subgraph.configure() |
297 | 297 |
|
| 298 | + Subgraph.prototype.reassignSubgraphUUIDs = function(graph) { |
| 299 | + const idMap = { nodeIDs: {}, linkIDs: {} } |
| 300 | + |
| 301 | + for (const node of graph.nodes) { |
| 302 | + const oldID = node.id |
| 303 | + const newID = LiteGraph.uuidv4() |
| 304 | + node.id = newID |
| 305 | + |
| 306 | + if (idMap.nodeIDs[oldID] || idMap.nodeIDs[newID]) { |
| 307 | + throw new Error(`New/old node UUID wasn't unique in changed map! ${oldID} ${newID}`) |
| 308 | + } |
| 309 | + |
| 310 | + idMap.nodeIDs[oldID] = newID |
| 311 | + idMap.nodeIDs[newID] = oldID |
| 312 | + } |
| 313 | + |
| 314 | + for (const link of graph.links) { |
| 315 | + const oldID = link[0] |
| 316 | + const newID = LiteGraph.uuidv4(); |
| 317 | + link[0] = newID |
| 318 | + |
| 319 | + if (idMap.linkIDs[oldID] || idMap.linkIDs[newID]) { |
| 320 | + throw new Error(`New/old link UUID wasn't unique in changed map! ${oldID} ${newID}`) |
| 321 | + } |
| 322 | + |
| 323 | + idMap.linkIDs[oldID] = newID |
| 324 | + idMap.linkIDs[newID] = oldID |
| 325 | + |
| 326 | + const nodeFrom = link[1] |
| 327 | + const nodeTo = link[3] |
| 328 | + |
| 329 | + if (!idMap.nodeIDs[nodeFrom]) { |
| 330 | + throw new Error(`Old node UUID not found in mapping! ${nodeFrom}`) |
| 331 | + } |
| 332 | + |
| 333 | + link[1] = idMap.nodeIDs[nodeFrom] |
| 334 | + |
| 335 | + if (!idMap.nodeIDs[nodeTo]) { |
| 336 | + throw new Error(`Old node UUID not found in mapping! ${nodeTo}`) |
| 337 | + } |
| 338 | + |
| 339 | + link[3] = idMap.nodeIDs[nodeTo] |
| 340 | + } |
| 341 | + |
| 342 | + // Reconnect links |
| 343 | + for (const node of graph.nodes) { |
| 344 | + if (node.inputs) { |
| 345 | + for (const input of node.inputs) { |
| 346 | + if (input.link) { |
| 347 | + input.link = idMap.linkIDs[input.link] |
| 348 | + } |
| 349 | + } |
| 350 | + } |
| 351 | + if (node.outputs) { |
| 352 | + for (const output of node.outputs) { |
| 353 | + if (output.links) { |
| 354 | + output.links = output.links.map(l => idMap.linkIDs[l]); |
| 355 | + } |
| 356 | + } |
| 357 | + } |
| 358 | + } |
| 359 | + |
| 360 | + // Recurse! |
| 361 | + for (const node of graph.nodes) { |
| 362 | + if (node.type === "graph/subgraph") { |
| 363 | + const merge = reassignGraphUUIDs(node.subgraph); |
| 364 | + idMap.nodeIDs.assign(merge.nodeIDs) |
| 365 | + idMap.linkIDs.assign(merge.linkIDs) |
| 366 | + } |
| 367 | + } |
| 368 | + }; |
| 369 | + |
298 | 370 | Subgraph.prototype.clone = function() { |
299 | 371 | var node = LiteGraph.createNode(this.type); |
300 | 372 | var data = this.serialize(); |
| 373 | + |
| 374 | + if (LiteGraph.use_uuids) { |
| 375 | + // LGraph.serialize() seems to reuse objects in the original graph. But we |
| 376 | + // need to change node IDs here, so clone it first. |
| 377 | + const subgraph = LiteGraph.cloneObject(data.subgraph) |
| 378 | + |
| 379 | + this.reassignSubgraphUUIDs(subgraph); |
| 380 | + |
| 381 | + data.subgraph = subgraph; |
| 382 | + } |
| 383 | + |
301 | 384 | delete data["id"]; |
302 | 385 | delete data["inputs"]; |
303 | 386 | delete data["outputs"]; |
|
0 commit comments