Using maya api getPoints() method

Hi there,

i basically have two different shaped meshes with identical topology. I want to be able to getPoints from one mesh and setPoints onto the other mesh. The code im using the getPoints however doesn’t seem to store anything. Can someone show me where im going wrong?


def getPoints(geo): 

    sel = om.MSelectionList()
    dag = om.MDagPath()
   
    sel.add(geo)
    sel.getDagPath(0,dag)
  
    mesh = om.MFnMesh(dag)
  
    vts=om.MPointArray()
    mesh.getPoints(vts, om.MSpace.kObject)

thanks
Sam x

Your code is correct. vts will be an MPointArray filled with the points of your mesh. What issue are you running into?

import maya.OpenMaya as om
def getPoints(geo): 

    sel = om.MSelectionList()
    dag = om.MDagPath()
   
    sel.add(geo)
    sel.getDagPath(0,dag)
  
    mesh = om.MFnMesh(dag)
  
    vts=om.MPointArray()
    mesh.getPoints(vts, om.MSpace.kObject)
    return vts

cube = cmds.polyCube(ch=False)[0]
pts = getPoints(cube)
for i in range(pts.length()):
    print i, pts[i].x, pts[i].y, pts[i].z

0 -0.5 -0.5 0.5
1 0.5 -0.5 0.5
2 -0.5 0.5 0.5
3 0.5 0.5 0.5
4 -0.5 0.5 -0.5
5 0.5 0.5 -0.5
6 -0.5 -0.5 -0.5
7 0.5 -0.5 -0.5

May be relevant if things seem puzzling:

Correction - that link is for api2, it looks like your code is api 1. The Capper has it.

ok thanks guys.

sorry, i guess the problem i am having is a separate part of the script i didnt mention, which gets the verts of two meshes and tries to add them together, it says 'zip argument #1 must support iteration ’

def addVerts(scan1_verts, scan2_verts):
 
    add_list=om.MFloatPointArray()
    for (v1,v2) in zip(scan1_verts,scan2_verts):
        p1=om.MFloatPoint(*v1)
        p2=om.MFloatPoint(*v2) 
        add_list.append(om.MFloatPoint(p1+p2))

and then after this, add_list is what i want to use when i want set the points onto another mesh

Maybe i need to get the mesh points a different way and append one by one to a list?

thanks,
Sam

What type of values are scan1_verts and scan2_verts? Are those both MFloatPointArrays?

Because I’m fairly certain that you can’t use zip with MFloatPointArrays. Just something with how the swig wrapper was setup.
Might try this instead:

def addVerts(scan1_verts, scan2_verts):
 
    add_list=om.MFloatPointArray()
    for i in xrange(scan1_verts.length()):
        p1 = scan1_verts[i]
        p2 = scan2_verts[i] 
        add_list.append(om.MFloatPoint(p1+p2))

I think Bob has it – I don’t believe that api 1 PointArray classes are complete python iterables.

Oh, fun fact, if you call:

list(om.MFloatPointArray())

You’ll kick off an infinite loop + memory leak that will do bad things to your computer. So yeah, don’t do that to test whether the class is a proper itertable or not…

FWIW I’d look at moving the whole thing over to api 2 - where the arrays act like proper iterables. In general api 2 is faster and easier to use.

Agreed.

cool thanks guys. I find all this quite overwhelming so i dont think i can try and convert everything to API2 just yet. But i have got it all working with the information you’ve told me, thanks