Welcome to od|forum

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

Sign in to follow this  
Followers 0
keqiquan

python simple question

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

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")
1 person likes this

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.

1 person likes this

Share this post


Link to post
Share on other sites

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")

 

1 person likes this

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  
Followers 0