top of page

How to get mesh vertex position through Maya API

Sometime back, I was in need of getting the vertex position, through Maya API.Then I have written the one, which suits my requirement. Here is the code:

How to get mesh vertex position through Maya API

<!-- CSS style sheets for Prism.js -->
<link
href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.19.0/themes/prism.min.css"
rel="stylesheet"
/>
<link
href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.19.0/plugins/line-numbers/prism-line-numbers.min.css"
rel="stylesheet"
/>
<!-- Code snippet content -->
<pre class="line-numbers">
<code style="font-family: Monaco, sans-serif" class="language-python">

#_ Import required modules
from maya.cmds import *
import maya.cmds as cmds
from maya.mel import *
import maya.mel as mel
from maya.OpenMaya import *
import maya.OpenMaya as om

def getMeshVtxPos(vtxName):
#_ Get vertex number and object having that vertex
testVtx =re.search('(?<=\[)(?P<vtxNum>[\d]+)(?=\])', str(vtxName))
if testVtx:
vtxNum =int(testVtx.group('vtxNum'))
vtxObj =vtxName.split('.')[0]
else:
return

#_ Get Api MDagPath for object
activList =MSelectionList()
activList.add(vtxObj)
pathDg =MDagPath()
activList.getDagPath(0, pathDg)

#_ Iterate over all the mesh vertices and get position of required vtx
mItVtx =MItMeshVertex(pathDg)
vtxPos=[]
while not mItVtx.isDone():
if mItVtx.index() == vtxNum:
point =MPoint()
point =mItVtx.position(MSpace.kWorld)
vtxPos =[round(point.x, 5), round(point.y, 5), round(point.z, 5)]
break
mItVtx.next()

#_ Print or return your vtx position
print [vtxPos]

#For Example
getMeshVtxPos('pSphere1.vtx[60]')

</code>
</pre>
<!-- Javascript for Prism.js core, line number display, and python syntax highlight -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.19.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.19.0/plugins/line-numbers/prism-line-numbers.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.19.0/components/prism-python.min.js"></script>

bottom of page