Jump to content
Sign in to follow this  
keqiquan

python simple question

Recommended Posts

keqiquan    0

I am a rookie for python . so I have a lot of questions about python. Hope someone  can help me because I really want to learn python.

i have a simple code :

sel = hou.selectedNodes()

if len(sel) >== 1:
    sel[0].parm("px").setExpression("$CEX")
    sel[0].parm("py").setExpression("$CEY")
    sel[0].parm("pz").setExpression("$CEZ")

is ablout set centroid pivot in transformation.I hope that this script can be used on many transformation node at once.

not just single node so i try write this:


sel = hou.selectedNodes()

if len(sel)>=1:

    for i in sel:

        i[0].parm("px").setExpression("$CEX")
        i[0].parm("py").setExpression("$CEY")
        i[0].parm("pz").setExpression("$CEZ")

I got a Error : 'ObjNode' object does not support indexing.

how i can fix this.and how can find simple houdini python simple tutor?

Thank you for your help!


 

Share this post


Link to post
Share on other sites
anim    589

since for i in sel will not be run if sel is empty, you don't need the first if

then i already contains single object per iteration, so lose the []

sel = hou.selectedNodes()
for i in sel:
    i.parm("px").setExpression("$CEX")
    i.parm("py").setExpression("$CEY")
    i.parm("pz").setExpression("$CEZ")
  • Like 1

Share this post


Link to post
Share on other sites

 

# Get the selected nodes in a list
nodes = hou.selectedNodes()

# Loop over the selected nodes list
for node in nodes :
        node.parm("px").setExpression("$CEX")
        node.parm("py").setExpression("$CEY")
        node.parm("pz").setExpression("$CEZ")

In the second sample you were trying to index ( i[0] )  a node.  Whereas, in the first code example, you were indexing the node list ( sel[0] ).

 

A good way to learn python is printing things out for example,

print type(i)

would have told you that 'i' was an ObjNode and not a Parm like in the first example.

  • Like 1

Share this post


Link to post
Share on other sites
anim    589

you can as well safeguard it for certain node type like Transform SOP

sel = hou.selectedNodes()
for i in sel:
    if i.type().nameWithCategory() == 'Sop/xform':
        i.parm("px").setExpression("$CEX")
        i.parm("py").setExpression("$CEY")
        i.parm("pz").setExpression("$CEZ")

 

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×