Author Topic: Basic graph traversal questions  (Read 3239 times)

Hello!

I am trying to do some basic traversal of a graph (in python), such as:

1) From a given input bitmap CompNode, check if there are any Blend nodes downstream
2) For each found, get the node connected to their 'Background' input
3) If that node is a 'Uniform Color' node, set it's color

I am running into a couple questions, but basically I am looking for guidance on the best way to accomplish this.  A couple key high level questions:

1) Is there something like a 'nodeType' attribute that tells me what kind of node each one is?  It seems they are essentially CompNode objects, but I am not sure how to confirm the type of a given node (blend vs levels for example)?
2) What is the best way (in python code) to get the node connected to a specific connection, such as the Blend node's 'Background' connection?

Any code examples would be fantastic - thank you!
Doug

1) Is there something like a 'nodeType' attribute that tells me what kind of node each one is?  It seems they are essentially CompNode objects, but I am not sure how to confirm the type of a given node (blend vs levels for example)?

This is pretty straight forward since you already have the nodes.
Code: [Select]
ctx = context.Context()

doc = substance.SBSDocument(ctx, sbs_filepath)
doc.parseDoc()

graph = doc.getSBSGraphList()[0]
nodes = graph.getNodeList()

for tgt_node in nodes:
    if tgt_node.isAFilter():
        tgt_id = tgt_node.getDefinition().getIdentifier()  # Returns the ID of the node.
       
        print('{id} - {node}'.format(id=tgt_id, node=tgt_node))

2) What is the best way (in python code) to get the node connected to a specific connection, such as the Blend node's 'Background' connection?
EDIT: On second glance, I misinterpret your question and found the source from the target, rather than the target from the source. Regardless, the same logic applies so you should be able to figure it out with the information below.

This is where it gets a bit confusing..
So the input ports on blend nodes are labeled "foreground", "background" and "opacity" in Designer.
SAT, on the other hand, is expecting "source", "destination" and "opacity".
Reference: https://docs.substance3d.com/sat/pysbs-python-api/api-content/libraries/sbslibrary/sbsfilters

Code: [Select]
ctx = context.Context()

doc = substance.SBSDocument(ctx, sbs_filepath)
doc.parseDoc()

graph = doc.getSBSGraphList()[0]
nodes = graph.getNodeList()

for tgt_node in nodes:
    if tgt_node.isAFilter():
        tgt_id = tgt_node.getDefinition().getIdentifier()  # Returns the ID of the node.
       
        # print('{id} - {node}'.format(id=tgt_id, node=tgt_node))
       
        # Find input connections for blend nodes.
        if tgt_id == 'blend':
            fg_nodes = graph.getNodesConnectedTo(tgt_node, sbsenum.InputEnum.SOURCE)
            bg_nodes = graph.getNodesConnectedTo(tgt_node, sbsenum.InputEnum.DESTINATION)
            op_nodes = graph.getNodesConnectedTo(tgt_node, sbsenum.InputEnum.OPACITY)
           
            print('\nConnections: {id} - {node}'.format(id=tgt_id, node=tgt_node))
            for fg_node in fg_nodes:
                print('FG: {id} - {node}'.format(id=fg_node.getDefinition().mIdentifier, node=fg_node))
           
            for bg_node in bg_nodes:
                print('BG: {id} - {node}'.format(id=bg_node.getDefinition().mIdentifier, node=bg_node))
           
            for op_node in op_nodes:
                print('OP: {id} - {node}'.format(id=op_node.getDefinition().mIdentifier, node=op_node))
       
Last Edit: September 26, 2019, 12:20:14 am

Great - thank you!!