Skip to content

Commit 2e95dd1

Browse files
authored
Merge pull request #396 from space-nuko/use-uuids
Option to use UUIDs instead of integers for node IDs
2 parents 0f2a905 + acb3a4a commit 2e95dd1

2 files changed

Lines changed: 1781 additions & 1658 deletions

File tree

src/litegraph.js

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@
142142

143143
ctrl_shift_v_paste_connect_unselected_outputs: false, //[true!] allows ctrl + shift + v to paste nodes with the outputs of the unselected nodes connected with the inputs of the newly pasted nodes
144144

145+
// if true, all newly created nodes/links will use string UUIDs for their id fields instead of integers.
146+
// use this if you must have node IDs that are unique across all graphs and subgraphs.
147+
use_uuids: false,
148+
145149
/**
146150
* Register a node class so it can be listed when the user wants to create a new one
147151
* @method registerNodeType
@@ -601,6 +605,13 @@
601605
return target;
602606
},
603607

608+
/*
609+
* https://gist.github.com/jed/982883?permalink_comment_id=852670#gistcomment-852670
610+
*/
611+
uuidv4: function() {
612+
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,a=>(a^Math.random()*16>>a/4).toString(16));
613+
},
614+
604615
/**
605616
* Returns if the types of two slots are compatible (taking into account wildcards, etc)
606617
* @method isValidConnection
@@ -1405,18 +1416,29 @@
14051416
console.warn(
14061417
"LiteGraph: there is already a node with this ID, changing it"
14071418
);
1408-
node.id = ++this.last_node_id;
1419+
if (LiteGraph.use_uuids) {
1420+
node.id = LiteGraph.uuidv4();
1421+
}
1422+
else {
1423+
node.id = ++this.last_node_id;
1424+
}
14091425
}
14101426

14111427
if (this._nodes.length >= LiteGraph.MAX_NUMBER_OF_NODES) {
14121428
throw "LiteGraph: max number of nodes in a graph reached";
14131429
}
14141430

14151431
//give him an id
1416-
if (node.id == null || node.id == -1) {
1417-
node.id = ++this.last_node_id;
1418-
} else if (this.last_node_id < node.id) {
1419-
this.last_node_id = node.id;
1432+
if (LiteGraph.use_uuids) {
1433+
if (node.id == null || node.id == -1)
1434+
node.id = LiteGraph.uuidv4();
1435+
}
1436+
else {
1437+
if (node.id == null || node.id == -1) {
1438+
node.id = ++this.last_node_id;
1439+
} else if (this.last_node_id < node.id) {
1440+
this.last_node_id = node.id;
1441+
}
14201442
}
14211443

14221444
node.graph = this;
@@ -2412,7 +2434,12 @@
24122434
enumerable: true
24132435
});
24142436

2415-
this.id = -1; //not know till not added
2437+
if (LiteGraph.use_uuids) {
2438+
this.id = LiteGraph.uuidv4();
2439+
}
2440+
else {
2441+
this.id = -1; //not know till not added
2442+
}
24162443
this.type = null;
24172444

24182445
//inputs available: array of inputs
@@ -2626,6 +2653,11 @@
26262653
}
26272654

26282655
delete data["id"];
2656+
2657+
if (LiteGraph.use_uuids) {
2658+
data["id"] = LiteGraph.uuidv4()
2659+
}
2660+
26292661
//remove links
26302662
node.configure(data);
26312663

@@ -4264,10 +4296,16 @@
42644296
break;
42654297
}
42664298
}
4299+
4300+
var nextId
4301+
if (LiteGraph.use_uuids)
4302+
nextId = LiteGraph.uuidv4();
4303+
else
4304+
nextId = ++this.graph.last_link_id;
42674305

42684306
//create link class
42694307
link_info = new LLink(
4270-
++this.graph.last_link_id,
4308+
nextId,
42714309
input.type || output.type,
42724310
this.id,
42734311
slot,
@@ -7071,19 +7109,21 @@ LGraphNode.prototype.executeAction = function(action)
70717109
var selected_nodes_array = [];
70727110
for (var i in this.selected_nodes) {
70737111
var node = this.selected_nodes[i];
7112+
if (node.clonable === false)
7113+
continue;
70747114
node._relative_id = index;
70757115
selected_nodes_array.push(node);
70767116
index += 1;
70777117
}
70787118

70797119
for (var i = 0; i < selected_nodes_array.length; ++i) {
70807120
var node = selected_nodes_array[i];
7081-
var cloned = node.clone();
7082-
if(!cloned)
7083-
{
7084-
console.warn("node type not found: " + node.type );
7085-
continue;
7086-
}
7121+
var cloned = node.clone();
7122+
if(!cloned)
7123+
{
7124+
console.warn("node type not found: " + node.type );
7125+
continue;
7126+
}
70877127
clipboard_info.nodes.push(cloned.serialize());
70887128
if (node.inputs && node.inputs.length) {
70897129
for (var j = 0; j < node.inputs.length; ++j) {
@@ -12947,7 +12987,7 @@ LGraphNode.prototype.executeAction = function(action)
1294712987
var newSelected = {};
1294812988

1294912989
var fApplyMultiNode = function(node){
12950-
if (node.clonable == false) {
12990+
if (node.clonable === false) {
1295112991
return;
1295212992
}
1295312993
var newnode = node.clone();

0 commit comments

Comments
 (0)