get vtx local position[python]


#1

Hello I’m trying to get vertex local position. Actually I can get them with this:


xPos = cmds.getAttr("pPlane2.vtx[5].pntx")
yPos = cmds.getAttr("pPlane2.vtx[5].pnty")
zPos = cmds.getAttr("pPlane2.vtx[5].pntz")
print xPos, yPos, zPos

But what I’m looking for is something like xform command. I have tried both, xform and pointPosition but what I’m getting is worldSpace position of vertexes


curPointPosition = cmds.xform( "pPlaneShape1.pnts[5]", query=True, translation=True, worldSpace=False )
print curPointPosition
cmds.pointPosition( 'pPlaneShape1.vtx[5]', local=True)

#2

With pymel

pm.PyNode(‘pPlane2’).vtx[5].getPosition(space=‘object’)

Maybe you can check whether cmds has something equivalent to this.

David


#3

Thanks djx. I’ll give it a try


#4

I think you were pretty close…

cmds.xform(‘pCylinder1.vtx[107]’, q=True, objectSpace=True, t=True)

David


#5

Yep! Thanks!


#6

if you want the position of only 1 point then cmds.pointPosition(point, l=1) would be probably the easiest way.
If you’ll need to work with a lot of points positions then iterating with xform or pointPosition could be very slow. Using cmds.xform to get all points positions from the object at once (using .vtx[*]) and then work with the points IDs and list indexes would be the fastest way to do it without using the API.


#7

Thanks myara!


#8

I forgot to wrote you the command. if you get all the vtx with xform you’ll need to arrage them in groups of 3 items, you can do it with zip.

xformPos = cmds.xform(shapeName + ‘.vtx[li]’, q=True, objectSpace=True, t=True)
[/li]listPos = zip(xformPos [0::3], xformPos [1::3], xformPos [2::3])

In my tests, when you need more than 10 points, getting all points position with xform like this is the fastest way. Virtually as fast as OpenMaya.

But on the other hand, you won’t feel any real difference unless you iterate like 10000+ vertices.

Anyway, since I’m writing a tool using vertices points, I though this piece of my code may help you.

# Get Vtx Selection
sel = cmds.ls(sl=1, fl=1)
# Get Shape Selection
shapeVtx = cmds.ls(selection=True, o=True)

# Get Position
xformPos = cmds.xform(str(shapeVtx[0]) + '.vtx[li]', q=True, objectSpace=True, t=True)
[/li]listPos = zip(xformPos [0::3], xformPos [1::3], xformPos [2::3])

# List Position by vertex ID
for vtx in sel:
    vtxID = int(vtx[vtx.find("[")+1:vtx.find("]")])
    print listPos[vtxID]


#9
# all indices in vertex selection:
import re

_re = re.compile('\[(\d+)\]')
ids = [int(_re.findall(x)[0]) for x in sel]


if you want sorted and unique for sure:

ids = sorted(set([int(_re.findall(x)[0]) for x in sel]))

#10

Sorry for delay.
Thanks guys, all of you!