Listing all nodes in a tree through python

   5141   2   1
User Avatar
Member
9 posts
Joined: Dec. 2011
Offline
I have a little logic problem, that should be easily implemented enough if I give up my neophyte ways and learn proper coding.

Let me see if I can explain it, but since it's a logical error on my behalf, let me apologize in advance if it stops making sense.

-I want a for loop to keep running itself, replacing the list its iterating over, until the list is empty. In order to list everything which is connected to a single node.

-A switch node check should go in there at some point, but for the sake of simplicity, lets assume there are no switch nodes in the scene.

-I'm trying to have my script run up a ‘tree’ where one root node has something connected to it, and then check if something is connected to the node it returned.

rootNode=hou.node('/out/mantra1')
allNodes=
for s in rootNode.inputs(): # This will return anything connected to it.
allNodes.append(s)
for x in s.inputs():
allNodes.append(x)

This gives me a list of all nodes for two levels of depth, is horribly ugly code, and I have no way of knowing beforehand the exact depth level.

-Anyone out there that knows how I should approach this?
-Is there a default recursive python function in Houdini, that resembles the os walk module?
User Avatar
Member
7737 posts
Joined: July 2005
Offline
hou.Node.inputAncestors() ?
User Avatar
Member
9 posts
Joined: Dec. 2011
Offline
wicked, thanks

ended up throwing this together while pondering over it though:

def nodeCheck(self,node,checked=None):
if checked is None: checked=
connected =
checked.append(node)

for n in node.inputs():
if n not in checked:
if str(n.type()).endswith ('switch>'):
index = n.parm(“index”).eval()
checked.append(n)
thislist = ]
for y in thislist:
checked.append(self.nodeCheck(y,checked))
else:
connected.append(n)

connected += self.nodeCheck(n,checked)

return connected

this will listAncestors and follow switch nodes along the way, dirty but works.
  • Quick Links