PDA

View Full Version : Understanding PyMEL nodes.


Koochy
10-23-2011, 04:10 AM
I've been trying to print ouf the world position of each vertex on a selected piece of geo. This is what I've written:

import pymel.core as pm

sel = pm.ls(sl=1)
vertCount = pm.polyEvaluate(sel, v=1)

for vert in range(vertCount):
print pointPosition("%s.vtx[%s]" % (sel, vert))

Which brings up the error:

# Error: Maya Attribute does not exist: u"[nt.Transform(u'pCube1')].vtx[0]"

I don't understand these little PyMEL nodes. How do I access the string name of the selected object? I could manually type in the loop "pCube1.vtx[]" but that's not very dynamic. Accessing the shape of object selected doesn't work either. The node [nt.Transform(u'')] contains all the information I need (shape, transform, etc) right? I just have trouble accessing verts and other shape related information. What am I doing wrong here?

Thanks for any help!

Koochy
10-23-2011, 04:18 AM
Durrrr, I just changed this and it worked.

sel = pm.ls(sel=1)[0]
I... I'm not too sure why this suddenly made it work. Could someone please point me in the right direction to read up on this?

EDIT: What if I had two seperate geos selected? I'd then have to remove the [0] from pm.ls, which means I'd end up with the same problem in my first post.

djx
10-23-2011, 08:25 AM
pm.ls is returning a list, even though it only has a single member in your first example. Notice the square brackets around the cubes name in the error message. When you added the [0] it works because now you are referring to the first member in the list and not the list itself. If you have multiple selections your code will need to iterate through the list a process each member.
For example

for s in sel:
vertCount = pm.polyEvaluate(s, v=1)

David

Koochy
10-24-2011, 08:51 AM
Fantastic! Thank you, Dave. I ended up with this:

import pymel.core as pm

sel = pm.ls(sl=1)

for s in sel:
vertCount = pm.polyEvaluate(s, v=1)
print "item selected: " + "%s" % (s)
for i in range(vertCount):
print pm.pointPosition("%s.vtx[%s]" % (s, i))

CGTalk Moderation
10-24-2011, 08:51 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.