Using xform

Here is the xform version of the script:

def getVtxPos( shapeNode ) :
	vtxWorldPosition = []    # will contain positions un space of all object vertex
	vtxIndexList = cmds.getAttr( shapeNode+".vrts", multiIndices=True )
	for i in vtxIndexList :
		curPointPosition = cmds.xform( str(shapeNode)+".pnts["+str(i)+"]", query=True, translation=True, worldSpace=True )    # [1.1269192869360154, 4.5408735275268555, 1.3387055339628269]
		vtxWorldPosition.append( curPointPosition )
	return vtxWorldPosition

With Maya API

Alternative to xform.

import maya.OpenMaya as OpenMaya
def particleFillSelection(  ):
	# get the active selection
	selection = OpenMaya.MSelectionList()
	OpenMaya.MGlobal.getActiveSelectionList( selection )
	iterSel = OpenMaya.MItSelectionList(selection, OpenMaya.MFn.kMesh)
	# go througt selection
	while not iterSel.isDone():
		# get dagPath
		dagPath = OpenMaya.MDagPath()
		iterSel.getDagPath( dagPath )
		# create empty point array
		inMeshMPointArray = OpenMaya.MPointArray()
		# create function set and get points in world space
		currentInMeshMFnMesh = OpenMaya.MFnMesh(dagPath)
		currentInMeshMFnMesh.getPoints(inMeshMPointArray, OpenMaya.MSpace.kWorld)
		# put each point to a list
		pointList = []
		for i in range( inMeshMPointArray.length() ) :
			pointList.append( [inMeshMPointArray[i][0], inMeshMPointArray[i][1], inMeshMPointArray[i][2]] )
		return pointList

Notice: I thought that specifying the size of the list from the start would gain time but it changes nothing. It seems that Python handles it well. So, I learned a new saying: Premature optimization is the root of all evil. :gniarkgniark:


Well, it's fine to say it's faster. But how much? :reflexionIntense:

Here are times I have with a 50x50 pSphere smoothed 4 (633602 vertices):

  • With xform: 39 sec
  • With Python Maya API: 4 sec

10x faster! This would give me almost want to code in C++, just to see results (which is suppose to be 10x faster than Python!)!!! :grenadelauncher:


Well now it's up to you to know when use it.

You can try to optimize Djelloul's algorythm and let me know your results! :sourit:

Now, I couldn't do without the API if I have to retrieve vertex positions of a mesh. It's much faster. Even if it's a little more complicate, I admit, but when you have this code, you keep it and use it at the right time! :aupoil:

Hope you like this trick!

See you soon!