Parsing a graph

Hello,

Is it possible to parse a graph to find, for example, all the nodes of a certain type? And if it is possible, is there any example?

Thanks,
Dnaiele

Comments

  • EricTEricT Administrator, Moderator, Fabric Employee Posts: 305 admin
    edited August 2016

    No real examples but here is a script I built off of the other RTVal in Python thread that can get you part of the way there. You'll probably have to traverse the nodes and test if they are sub-graphs and then iterate over them too. It's all about getting the JSON descriptions and parsing them.

    import json
    import FabricEngine.Core as fabric
    import maya.cmds as cmds
    
    PORT_TYPE_MAP = {
        0: 'In',
        1: 'IO',
        2: 'Out'
    }
    
    
    # Get Client
    contextID = cmds.fabricSplice('getClientContextID')
    if contextID == '':
        cmds.fabricSplice('constructClient')
        contextID = cmds.fabricSplice('getClientContextID')
    client = fabric.createClient({"contextID": contextID})
    host = client.DFG.host
    
    # Get Binding for Operator
    # NOTE: YOU'LL HAVE TO CHANGE THE NAME TO YOUR NODE NAME BELOW!!!!!
    # I created a graph by hand with 2 Vec3 nodes and an Add node.
    opBindingID = cmds.FabricCanvasGetBindingID(n="canvasNode1")
    opBinding = host.getBindingForID(opBindingID)
    graph = opBinding.getExec()
    
    # Get the Graph Description and print to see the structure if needed
    graphDesc = graph.getDesc()
    # print graphDesc
    
    numNodes = graph.getNodeCount()
    print "Graph has '" + str(numNodes) + "' nodes."
    for i in xrange(numNodes):
        nodeName = graph.getNodeName(i)
        nodeDesc = json.loads(graph.getNodeDesc(nodeName))
        nodeType = nodeDesc['execTitle']
    
        print "\t" + nodeName + "[" + nodeType + "]"
    
    
    client.close()
    

    Eric Thivierge
    Kraken Developer
    Kraken Rigging Framework

  • Daniele NieroDaniele Niero Posts: 233 ✭✭

    Thanks Eric,

    However I have the feeling that going through the Json representation is... fragile.
    For example using you code, I got immediately an error as it turned out that not every node's json in my graph has 'execTitle'.
    I tried also to find out what else is available in the api, but, let me say this, the default output of Sphinx is horrible to document an API. I know that is the Python's choice, but having to scroll down a huge long page to find out what functions are available is insane! whith a layout an coloring that doesn't help neither. Much better the output you produce for KL documentation, that should be the style used also for C/C++/Python API... arrg, ok rant finished.

    Difficulty to read the doc apart, I really think that some higher level wrappers wouldn't be a bad idea here.
    Also, maybe finding a node of a certain type is not a correct thing to do, as node are DFGExec, did I got this right? If so then what I'm after is identify first if a node is a preset, and if it is what is the preset path? I don't think there is a logic connection between a node and, let's say, a function in my KL extension, isn't it?

    Daniele

  • EricTEricT Administrator, Moderator, Fabric Employee Posts: 305 admin

    Hi Daniele,

    Yes sorry I quickly through that code together to hopefully get you going with parsing the graph. I've talked to the core team who also pointed me to some other good information and also did a little looking into the json and I have a new script that should run more cleanly on many graphs (no guarantee I'm not finding a few edge cases to sort through). See below.

    Yes each node is essentially a DFGExec. In the code below we evaluate if a node's description is an "Inst" type. if it is we look for a presetPath entry in the node description too. If it exists then we know the path to the preset. You can also look up extension dependencies from the DFGExec which I illustrate below too.

    As for the organization of the Canvas Core API docs yes I believe it can get some improvements and I logged this issue: FE-7230

    import json
    import FabricEngine.Core as fabric
    import maya.cmds as cmds
    
    
    # Get Client
    contextID = cmds.fabricSplice('getClientContextID')
    if contextID == '':
        cmds.fabricSplice('constructClient')
        contextID = cmds.fabricSplice('getClientContextID')
    client = fabric.createClient({"contextID": contextID})
    host = client.DFG.host
    
    # Get Binding for Operator
    # NOTE: YOU'LL HAVE TO CHANGE THE NAME TO YOUR NODE NAME BELOW!!!!!
    # I created a graph by hand with 2 Vec3 nodes and an Add node.
    opBindingID = cmds.FabricCanvasGetBindingID(n="canvasNode1")
    opBinding = host.getBindingForID(opBindingID)
    graph = opBinding.getExec()
    
    # Get the Graph Description and print to see the structure if needed
    graphDesc = graph.getDesc()
    # print graphDesc
    
    numNodes = graph.getNodeCount()
    print "Graph has '" + str(numNodes) + "' nodes."
    
    for i in xrange(numNodes):
    
        node = None
        nodeName = graph.getNodeName(i)
        nodeDesc = json.loads(graph.getNodeDesc(nodeName))
        nodeIndex = nodeDesc.get('index')
        execTitle = nodeDesc.get('execTitle', None)
        nodeObjType = nodeDesc.get('objectType', None)
        presetPath = nodeDesc.get('presetPath', None)
    
        # Get the DFGExec of the graph node if it's of type 'Inst'
        # Variables don't have this ability.
        if nodeObjType == 'Inst':
            node = graph.getSubExec(nodeName)
    
            print "\tNode: " + nodeName
            print "\t\tExecTitle: " + execTitle
            print "\t\tType: " + nodeObjType
            print "\t\tExt Dependencies: " + node.getExtDeps()
    
            if presetPath is not None:
                print "\t\tPresetPath: " + presetPath
        else:
            print "\tNode: " + nodeName
            print "\t\tType: " + nodeObjType
            if nodeObjType == 'Var':
                print "\t\tDataType: " + graph.getVarType(nodeName)
    
        print ""
    
    
    
    client.close()
    

    Eric Thivierge
    Kraken Developer
    Kraken Rigging Framework

  • Daniele NieroDaniele Niero Posts: 233 ✭✭

    Hi Eric,

    Yes this works great, I was trying to get exactly here after I red you post, but I couldn't. Thank you so much for the help.
    Now, knowing the preset I'm looking for, I should be able to identify all the nodes that comes form that preset.

    Another question if I can. The result of a Canvas' graph is KL code or could it be converted in KL code?

    Thanks for the help,
    Daniele

  • EricTEricT Administrator, Moderator, Fabric Employee Posts: 305 admin

    There is no mechanism for converting a Canvas graph to KL currently.

    Eric Thivierge
    Kraken Developer
    Kraken Rigging Framework

Sign In or Register to comment.