Author Topic: Node Selection order  (Read 453 times)

I'm doing a loop to connect a bunch of color masks to blend nodes via python loop , but the connection occurs randomly and nost in order as one would expect... what's wrong?

this is the code
Code: [Select]
import sd
from PySide2 import QtGui
from sd.api.sduimgr import SDUIMgr
from sd.api.sdproperty import SDPropertyCategory
from sd.api.sdapplication import SDApplicationPath

class CompNodeCreator(object):
    def __init__(self):
        # Get app and UI manager.
        ctx = sd.getContext()
        app = ctx.getSDApplication()
        self.ui_manager = app.getQtForPythonUIMgr()
       
 
   
    def add_node(self, name, pos):
     
        # Get the comp graph.
        sd_graph = self.ui_manager.getCurrentGraph()
       
        # Create a node.
        node_def_id = 'sbs::compositing::{definition}'.format(definition=name)
        sd_node = sd_graph.newNode(node_def_id)
        sd_node.setPosition( sd.api.sdbasetypes.float2(pos[0],pos[1]))
       
        return sd_node

# Run


def NodeProps(node):
    nodePropsA = node.getProperties(SDPropertyCategory.Output)
    for i in range(len(nodePropsA)):
    print(nodePropsA[i].getId())
    print(nodePropsA[i].getType())
   
    nodePropsB = node.getProperties(SDPropertyCategory.Input)
    for i in range(len(nodePropsB)):
    print(nodePropsB[i].getId())
    print(nodePropsB[i].getType())

selection = uiMgr.getCurrentGraphSelection()
i = 300
i2 = 0
pos = [0,0]
n = {}

for e,node in enumerate(selection):
    print(pos)
    print(str(e))
    name = 'blend'
    blend = node_creator.add_node(name,pos)
    node.newPropertyConnectionFromId('output',  blend  ,'opacity')   
    pos[0] += i2
    pos[1] += i
Last Edit: June 08, 2021, 03:17:19 am

The problem is that you cannot assume that the list of selected nodes is sorted in any specific order.

An alternative could be creating the blend nodes and set their positions relative to the input color mask nodes.

Est.


Thanks!, Ok I see!
Last Edit: June 08, 2021, 02:53:56 pm


Ok now it's Working!
Code: [Select]
import sd
from PySide2 import QtGui
from sd.api.sduimgr import SDUIMgr
from sd.api.sdproperty import SDPropertyCategory
from sd.api.sdapplication import SDApplicationPath

def Add_Node(name,pos):
 
        # Get app and UI manager.
        ctx = sd.getContext()
        app = ctx.getSDApplication()
        ui_manager = app.getQtForPythonUIMgr()
        # Get the comp graph.
        sd_graph = ui_manager.getCurrentGraph()
        # Create a node.
        node_def_id = 'sbs::compositing::{definition}'.format(definition=name)
        sd_node = sd_graph.newNode(node_def_id)
        sd_node.setPosition( sd.api.sdbasetypes.float2( pos[0],pos[1]))
       
        return sd_node
# Run


def NodeProps(node):
    nodePropsA = node.getProperties(SDPropertyCategory.Output)
    for i in range(len(nodePropsA)):
    print(nodePropsA[i].getId())
    print(nodePropsA[i].getType())
   
    nodePropsB = node.getProperties(SDPropertyCategory.Input)
    for i in range(len(nodePropsB)):
    print(nodePropsB[i].getId())
    print(nodePropsB[i].getType())


Pos = [0,0]
PosOffset = [500, 50]
blends = []
Name = 'blend'

def Select():
    ctx = sd.getContext()
    app = ctx.getSDApplication()
    uiMgr = app.getQtForPythonUIMgr()
    selection = uiMgr.getCurrentGraphSelection()
Select()
for e,node in enumerate(selection):
   
    NodePos = node.getPosition()
    print(NodePos)
    Pos[0] = NodePos.x + PosOffset[0]
    Pos[1] = NodePos.y + PosOffset[1]
    print(Pos)
    NodeBlend = Add_Node(Name,Pos)
    blends.append(NodeBlend)
    node.newPropertyConnectionFromId('output',  NodeBlend  ,'opacity')