Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // to call this function no need to select the composite in the node view, it will be chosen with a prompt.
  2.  
  3. function ConnectButton(){
  4.     if (KeyModifiers.IsAlternatePressed()){
  5.         disconnectAllSellNodes()
  6.     }else{
  7.         connectAllSellNodes()
  8.     }
  9. }
  10.  
  11.  
  12. function connectAllSellNodes(){
  13.     // connects selected Nodes to a chosen Composite
  14.     scene.beginUndoRedoAccum("connectAllSelNodes");
  15.  
  16.     // filter out some nodes types
  17.     var typesFilter = ["PEG"]
  18.     var sel = selection.selectedNodes()
  19.     sel = sel.filter(function(x){return typesFilter.indexOf(node.type(x)) == -1})
  20.  
  21.     // get nodes sorted by coordinates
  22.     sel = sel.sort(function (b,a){return (node.coordY(a)*10000+node.coordX(a)) - (node.coordY(b)*10000+node.coordX(b))});
  23.  
  24.     if (sel.length==0) return;
  25.  
  26.     var homeGroup = node.parentNode(sel[0])
  27.     var sceneComps = getSceneComposites(homeGroup);
  28.  
  29.     // prompt for a composite to connect to if there is more than  one
  30.     if (sceneComps.length > 1){
  31.         var compsNames = sceneComps.map(function(x){return node.getName(x)})
  32.         var comp =  homeGroup+"/"+Input.getItem("Select a composite to link to:", compsNames); 
  33.     }else{
  34.         var comp =  sceneComps[0]
  35.     }
  36.  
  37.     // link the nodes to the comp
  38.     for (var i in sel){
  39.         node.link(sel[i], 0, comp, node.numberOfInputPorts(comp))
  40.     }
  41.  
  42.     scene.endUndoRedoAccum();
  43.  
  44. }
  45.  
  46.  
  47. function disconnectAllSellNodes(){
  48.  
  49.     scene.beginUndoRedoAccum("disconnectAllSelNodes");
  50.  
  51.     var sel = selection.selectedNodes();
  52.     for (var i in sel){
  53.         for (var j=0; j<node.numberOfOutputPorts(sel[i]); j++){
  54.             for (var k=0; k<node.numberOfOutputLinks(sel[i], j); j++){
  55.                 var dstNode = node.dstNode(sel[i], j, k)
  56.                 var port = getInputPort(sel[i], dstNode)
  57.                 if (port != -1) node.unlink(dstNode, port)
  58.             }
  59.         }
  60.     }
  61.  
  62.     scene.endUndoRedoAccum();
  63. }
  64.  
  65.  
  66. function getSceneComposites(group){
  67.     // returns a sorted list of all the composite Nodes in a group
  68.     var nodes = node.subNodes(group).filter(function(x){return node.type(x) == "COMPOSITE"})
  69.     return nodes.sort(function (a,b){return (node.coordY(a)*10000+node.coordX(a)) - (node.coordY(b)*10000+node.coordX(b))});
  70. }
  71.  
  72.  
  73. function getInputPort(srcNode, dstNode){
  74.     // find the InPort through which two nodes are connected
  75.     for (var i=0; i<node.numberOfInputPorts(dstNode); i++){
  76.         var linkedNode = node.srcNode(dstNode, i);
  77.         if (linkedNode == srcNode) return i;
  78.     }
  79.     return -1
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement