Maya API: Getting/Setting vertex positions

Hey guys, I’m trying to learn the Maya Python API, my first attempt is to create two functions to get and set vertex positions on a mesh.

I’ve managed to get the positions and output them to a list, but what I havent figured out is how I can pass that list to another function to set the positions. I think my problem is that I don’t know how to append my list over to an MPointArray, I’ve googled a lot but I can’t figure out how to do this :confused:

Here’s my code:
http://pastebin.com/mp16Pd8r

#GET VERTEX/CV POSITIONS
def getGeoPointPosition(geoObj, space='worldSpace'):
    #Create an empty objects/variables to store positions, functionSet and space
    mPoint = OpenMaya.MPointArray()
    mSpace = None
    pointPos = []

    #Get which space it should return the positions in (localSpace/worldSpace)
    if space == 'localSpace': mSpace = OpenMaya.MSpace.kObject
    elif space: == 'worldSpace': mSpace = OpenMaya.MSpace.kWorld

    #Attach a MFnMesh functionSet and get the positions
    mFnSet = OpenMaya.MFnMesh(getDagPath(geoObj[0]))
    mFnSet.getPoints(mPoint, mSpace)

    #For each vertex/CV of the object
    for x in range(0, mPoint.length()):
        #Append the current vertex position to the list
        pointPos.append([mPoint[x][0], mPoint[x][1], mPoint[x][2]])

    #Return the positions
    return pointPos

#----------------------------------------------------------------------------------------

#SET VERTEX/CV POSITIONS
def setGeoPointPos(geoObj, positions=[], space='worldSpace'):
    #Create an empty objects/variables to store positions, functionSet and space
    mPoint = OpenMaya.MPointArray()
    mSpace = None
    #Add the list to the MPointArray
    mPoint.append(positions)

    #Get which space it should return the positions in (localSpace/worldSpace)
    if space == 'localSpace': mSpace = OpenMaya.MSpace.kObject
    elif space: == 'worldSpace': mSpace = OpenMaya.MSpace.kWorld

    #Attach a MFnMesh functionSet and set the positions
    mFnSet = OpenMaya.MFnMesh(getDagPath(geoObj[0]))
    mFnSet.getPoints(mPoint, mSpace)

    #Return the positions
    return pointPos

Any tips/hints would be appreciated! :slight_smile:

http://download.autodesk.com/global/docs/mayasdk2012/en_us/index.html

MfnMesh class has everything you will need to do so, take a look at the getPoints and setPoints methods

Thanks, but if you take a look at my code you’ll see that I’ve used the MfnMesh functionset. The problem is that I don’t know how to plot a list into an MPointArray without using a loop?

1 Like

Ya that is where it gets tricky with python due to non static types.

The mpointarray constructor says it can take a array of mpoints or a array of double arrays with 4 index’s each.

How is your data currently stored?

Ok, well I’ve stored them as a list, like this:

# Result: [[-0.5, -1.1102230246251565e-16, 0.5],
 [0.0, -1.1102230246251565e-16, 0.5],
 [0.5, -1.1102230246251565e-16, 0.5],
 [-0.5, 0.0, 0.0],
 [0.0, 0.0, 0.0],
 [0.5, 0.0, 0.0],
 [-0.5, 1.1102230246251565e-16, -0.5],
 [0.0, 1.1102230246251565e-16, -0.5],
 [0.5, 1.1102230246251565e-16, -0.5]] # 

Here’s how I do it, looping


position_array = om.MFloatPointArray()
for i in range(# of verts):
    temp_point = om.MPoint()
    temp_float_point = om.MFloatPoint()
    mesh_fn.getPoint(i, temp_point)
    temp_float_point.setCast(temp_point)
    position_array.append(temp_float_point)


In your set function, you cannot append a python list (or list of lists) to an MPointArray. If you look at the MPointArray documentation, it lists the data append expects (Autodesk Maya API Guide and Reference)
Append needs either an MPoint, or double xyz values (you can pass python floats in place of doubles).

So if you have a list of lists representing positions and you want to create an MPointArray with that data, you can do this:

points = om.MPointArray()
for pos in positions:
    points.append(*pos)

You can then call MFnMesh.setPoints(points)