Jump to content

python simple question


keqiquan

Recommended Posts

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!


 

Link to comment
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")
  • Like 1
Link to comment
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
Link to comment
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")

 

  • Like 1
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...