Jump to content

Python: Build Dict, and create node if does not exist.


martinkrol

Recommended Posts

( I posted this on the sidefx forum as well, posting here as well to hopefully get more eyes on this . )

 

Hey there guys.

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

So I am trying to create a shelf script that goes through all geo objects in the scene, creates a new specific 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/use 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? I'd 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" )


Any help would be appreciated.

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...