List Vertices

Where is the physical location of the vertices of my mesh in (X,Y,Z)?

#This Python API prints out the vertice values of the selected Objects.
#Simple Version. Assumes the objects selected are polygons.
    import maya.OpenMaya as OM
     
#Assign all selected objects to a selection list
    list = OM.MSelectionList()
    OM.MGlobal_getActiveSelectionList(list)
#loops through list
       #MSelectionList has a function of length()
    for i in range(0,list.length()):
     
 #create some pointers
       obj= OM.MObject()
       dpath= OM.MDagPath()
 #iterating through list with i
 #dpath is output to the dag path of item
 #obj is handle for component-which in this use is null
 #note the obj parameter since it is going to be null
 #does not Need to be listed
 #list.getDagPath(i,dpath) would work fine
       list.getDagPath(i,dpath,obj)
     
 #attach a mesh function to this item named by its dag path
       meshFn=OM.MFnMesh(dpath)
 #create a pointArray and use the mesh function getPoints
 #to fill the array with the vertice information
 #note that this is using the default parameters of world space
 #otherwise the function getPoints needs (pointArray , MSpace)
       vts=OM.MPointArray()
       meshFn.getPoints(vts)
     
 #print the information
 #the MPointArray function of length() returns an integer of size naturally
 #
 #the MDagPath function partialPathName() returns from root to DAG object the
 #minimum path such that the name is unique. It can include wildcards
 #
 # each child (x,y,z) of the particular vertex can be retrieved as PointArray[vertex].X 
 # as detailed below
       for j in range(0,vts.length()):
          print ("The object "+dpath.partialPathName()+ "'s vertex #"+ str(j+1)+" is :")
          print ("X= " + str(vts[j].x) + "     Y= " + str(vts[j].y) + "     Z= " + str(vts[j].z))

Without Comments:

    import maya.OpenMaya as OM
    list = OM.MSelectionList()
    OM.MGlobal_getActiveSelectionList(list)
    for i in range(0,list.length()):
       obj= OM.MObject()
       dpath= OM.MDagPath()
       list.getDagPath(i,dpath,obj)
       meshFn=OM.MFnMesh(dpath)
       vts=OM.MPointArray()
       meshFn.getPoints(vts)
       for j in range(0,vts.length()):
          print ("The object "+dpath.partialPathName()+ "'s vertex #"+ str(j+1)+" is :")
          print ("X= " + str(vts[j].x) + "     Y= " + str(vts[j].y) + "     Z= " + str(vts[j].z))

This script first gets a selection and it will iterate through the list for each polygon.

It is going to treat every object as if it was a polygon mesh without any error checking.

MStatus MSelectionList::getDagPath ( unsigned int  index,
MDagPath &  dagPath,
MObject &  component = MObject::kNullObj  
) const

This is the API Reference. In python we take out selectionList --> list and arrange it like

thus.               list.getDagPath(i,dpath,obj)

If we ignore selecting components of our meshes we could write the same code as

list.getDagPath(i,dpath) and dpath would be the handle to our mesh.

We then use the mesh function class in conjunction with MPointArray class to simply get a

list of all the points in the mesh.

Lastly we print out the location of each point.

 

In MEL all we are given back for vertex (or face or edge..) requests is a string. And with a tokenize here and there we can parse through the string to find each vertice. Then I would ask myself ,"How do I know the X,Y,Z location of a vertex"? All that tokenizing just gives up a vertex number.

Where is that mel command: VertexLocation(string mesh int vertNumber)?