Get/Fetch Node from an Existing Xpresso Tag



  • Hi,

    Is there a way to get/fetch a node from an existing xpresso tag? For instance, of all 5 existing math nodes, select a node with a particular name or a particular operation type (i.e. multiply as opposed to add).

    In the documentation there is GetNode method but it is specific to port. Basically, what I am after is something like.

    obj = doc.SearchObject("obj")
    tag = obj.GetTag(c4d.Texpresso)
    nodemaster = tag.GetNodeMaster()
    
    # get/fetch a particular node  in  the list of nodes of nodemaster
    
    

    Is this possible? Thank you for looking at my problem.



  • Hi @bentraje GvNode are standard BaseList2 that means you can iterate them as you can iterate object in the ObjectManager.

    With that's said here is an example:

    """
    Copyright: MAXON Computer GmbH
    
    Author: Maxime Adam
    
    Description:
        - Loops through all nodes from the Xpresso Tag of the selected object.
        - Checks if it's a Math Node.
    
    Class/method highlighted:
        - c4d.modules.graphview.GvNodeMaster
        - c4d.modules.graphview.GvNode
        - GvNodeMaster.GetRoot()
        - GvNode.GetNext() / GvNode.GetUp() / GvNode.GetDown()
        - GvNode.GetOperatorID()
    
    Compatible:
        - Win / Mac
        - R13, R14, R15, R16, R17, R18, R19, R20
    """
    import c4d
    
    
    def iterateNodes(node):
        """
        This function iterates over a BaseList2D, GvNode inherit from BaseList2D.
        :param node: GvNode to iterate.
        """
        while node:
            nodeName = node.GetName()
            isMathNode = node.GetOperatorID() == c4d.ID_OPERATOR_MATH
            
            # If it's a math node and set to multiply, prints the node name
            if isMathNode:
                isMultiply = node[c4d.GV_MATH_FUNCTION_ID] == c4d.GV_MUL_NODE_FUNCTION
                if isMultiply:
                    print "Name: {0}".format(node.GetName())
    
            # If it's a group retrieves all inner GvNode.
            if node.IsGroupNode():
                iterateNodes(node.GetDown())
    
            node = node.GetNext()
    
    def main():
        # Checks if selected object is valid
        if op is None:
            raise ValueError("op is none, please select one object.")
    
        # Retrieves the xpresso Tag
        xpressoTag = op.GetTag(c4d.Texpresso)
        if xpressoTag is None:
            raise ValueError("Make sure the selected object get an Xpresso Tag.")
    
        # Retrieves the node master
        gvNodeMaster = xpressoTag.GetNodeMaster()
        if gvNodeMaster is None:
            raise RuntimeError("Failed to retrieves the Node Master.")
    
        # Retrieves the Root node (the Main XGroup) that holds all others nodes
        gvRoot = gvNodeMaster.GetRoot()
        if gvRoot is None:
            raise RuntimeError("Failed to retrieves the Root Node.")
    
        # Iterates overs all nodes of this root node.
        iterateNodes(gvRoot)
    
    
    if __name__=='__main__':
        main()
    

    Cheers,
    Maxime.



  • Thanks @m_adam. Works as expected.
    My bad, I was looking at the documentation hierarchy erroneously (i.e c4d > modules > graphview > GvNode)

    Just a note: I think the if Multiply: line is supposed to be if isMultiply:.

    Thanks again. Have a great day ahead!


Log in to reply