build Dict Python

   1170   2   1
User Avatar
Member
25 posts
Joined: Jan. 2014
Offline
Hey there guys.

So in my journey to learn some scripting/python... I am having some issues ( code below )

So I am trying to go through all geo objects in the scene, create a new geo node, and within it make a pointcloud that would be made from the values collected.

I am hoping to build the pointAttrib dict, and as it's looping through the objects, append to the list.

at the end of the script, I want to print the list of values but only one set of values prints. I am expecting at least 10 object names and their respective values.

Anyone have the time to point me in the right direction? Id love to know what I am doing wrong.


pointAttrib ={}

def selectSubChildrenOfType(node, node_type):
    for child in node.allSubChildren():
        if child.type() == node_type:
            #print child.name()
            
            #print child.worldTransform()
 
            trX =  str(child.worldTransform().at(3,0))
            trY =  str(child.worldTransform().at(3,1))
            trZ =  str(child.worldTransform().at(3,2))
            
            #create dictionary from info above.
            keys = {'name': child.name(), 'tx': trX, 'ty': trY, 'tz': trZ}
            
            pointAttrib.update(keys)

#            print "obj "+ (pointAttrib["name"]) + " transX " + (pointAttrib["tx"])

            # print a readable version of the values we generated.
#            print "translate X: " + trX
#            print "translate Y: " + trY
#            print "translate Z: " + trZ
          
# executes the defined function above.         
selectSubChildrenOfType(hou.node("/obj"), hou.objNodeTypeCategory().nodeTypes()['geo'])

#create a point per transform matrix that was found in the selctSUbChildrenOfType function.

for key in pointAttrib:
    print key
    print pointAttrib[key]




Question number 2: How would the code look for creating a node only if it doesn't exist. ( if I point to the specific node directly ). It's a bit of a chicken or an egg problem and I'm not sure how I would solve it.

So far I have tried.. but yeah it's not right, but at least thats where my head is at. Any ideas? hints? tips?

def createNodeIF(node, name)
    for child in node.allSubChildren():
        if child.name() != name:
            hou.node("/obj").createNode("geo", "pointcloud" )
User Avatar
Member
191 posts
Joined: Oct. 2018
Offline
Your first problem is that you're overwriting your pointAttrib dictionary every time when you run pointAttrib.update(keys), that's why you end up with only one set of values. What you want is to have a dictionary of dictionaries where each entry in pointAttrib refers to one node and stores the transforms with that specific node. Something like this I think should work:

pointAttrib ={}

def selectSubChildrenOfType(node, node_type):
    for child in node.allSubChildren():
        if child.type() == node_type:
            
            trX =  str(child.worldTransform().at(3,0))
            trY =  str(child.worldTransform().at(3,1))
            trZ =  str(child.worldTransform().at(3,2))
            
            # Create a new key in pointAttrib for each node
            pointAttrib[ child.name() ] = { 'tx': trX, 'ty': trY, 'tz': trZ }

The second question, you can test if a node exists already and create it if it doesn't using the hou.node() function. It returns false if it doesn't exist.

pcNode = hou.node("/obj/pointcloud")

if not pcNode :
    hou.node("/obj").createNode( "geo", "pointcloud" )
else :
    print "Node already exists."
User Avatar
Member
25 posts
Joined: Jan. 2014
Offline
Thank you so much It helped a lot.

Cheers.
  • Quick Links