Jump to content

getting children of objNode


salik

Recommended Posts

I am trying to check whether a node exists in my scene and also to check for its children within. Suppose this is the full path of the node that I am trying to check which is `/obj/my_node`

In a simpler term to get the children, this is one of the method I know of:

node = hou.node('/obj/my_node')
for item in node.children():
    print item

But as mentioned, I am trying to check whether if a node exists.. this is how I did mine:

# Check if my_node exists
check_for_node = hou.node('/obj').recursiveGlob('my_node')
if check_for_node:
    """
    # This gives me an error...
    for item in check_for_node.children():
        print item
        # AttributeError: 'tuple' object has no attribute 'children'
    """

    for item in hou.node(check_for_node[0].path()).children():
        print item

Can someone kindly tell me why is it that I am unable to use the first method (that error code part). 

So far, the differences I am seeing is that check_for_node[0] is of hou.objNode class while the method I know is of hou.Node class. What are the significant differences between them?
Is there any way for me to simplify hou.node(check_for_node[0].path()).children() into shorter form? Also, is the line I wrote for filtering using recursiveGlob a good way to check if the node exists or not?

Edited by salik
Link to comment
Share on other sites

This is a good way of finding nodes, you've simply lost a little detail:

hou.node('/obj').recursiveGlob('my_node')

already returns you a tuple of nodes, not a node, which has a method children() - so you can't call check_for_node.children(). children()  will be available in any possible element of that tuple, but you don't need that anymore with recursiveGlob(). 

candidates = hou.node("/").recursiveGlob("my_node*", filter=hou.nodeTypeFilter.Sop)

if candidates:

   # if my_node[postfix] exists and its type is SOP, candidates includes my_node along with all its children (if any)

 


 

Quote

 

Is there any way for me to simplify hou.node(check_for_node[0].path()).children() into shorter form?


 

as follows from former part: 

for node in check_for_node:

   print node.children()

   # will repeat what was already in check_for_node

 

hth,

skk.

Link to comment
Share on other sites

14 hours ago, symek said:

 


candidates = hou.node("/").recursiveGlob("my_node*", filter=hou.nodeTypeFilter.Sop)

if candidates:

   # if my_node[postfix] exists and its type is SOP, candidates includes my_node along with all its children (if any)

Hi, by using `my_node*`, would that not find all similar nodes that have the name - my_node, eg. my_node, my_node1, my_node2 etc.?


So there isn't a way to find a node of specific naming then?

Link to comment
Share on other sites

Here is what I have been using for a while. I fetch node children based upon type. For instance if I need all the NULLs or all the Bones in a rig.

USER_DATA_DELIMITER = "~" 
def childrenOfNode(node, filter):
    # Return child nodes of type matching the filter (i.e. geo etc...).
    result = []
    if node != None:
        for n in node.children():
            t = str(n.type())
            if t != None:
                for filter_item in filter:
                    if (t.find(filter_item) != -1):
                        # Filter nodes based upon passed list of strings.
                        result.append('%s%s%s' % (n.name(), USER_DATA_DELIMITER, t))                    # name~type.
                    result += childrenOfNode(n, filter)
    return result

def toggleDisplayFlags():
    # Scan nodes based upon types and set their display flag based upon toggles.
    lst_toggles = [("Object geo", `ch("tgl_mesh")`),("Object null", `ch("tgl_controllers")`),("Object bone", `ch("tgl_bones")`)]
    for entry in lst_toggles:
        display_flag = entry[1]
        nodes = childrenOfNode(hou.node(node_path),[entry[0]])
        for (i,node) in enumerate(nodes):
            ary = node.split("~")
            if len(ary) > 0:
                node_candidate = "%s/%s" % (node_path, ary[0])
                n = hou.node(node_candidate)
                if n !=None:
                    n.setDisplayFlag(display_flag)

 

Link to comment
Share on other sites

1 minute ago, symek said:

Just remove the wild card? Sorry for a confusion, just wanted to show you what glob is about.

Okay, I may have probably lost you instead :)

Quick question though, if I am continuing to implement back using the `recursiveGlob` command with the wild card, is there anyway that I can simply grab at the /obj level - /obj/my_node, /obj/my_node1 without its children? Currently the results is showing my every nodes including its children such as `/obj/my_node, /obj/my_node/my_node_child, /obj/my_node1, /obj/my_node1/child`...

Because I am trying to add in a check logic in my script such that if more than 1 my_node exists, it will prompt user some notice..

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...