Skip to content

Instantly share code, notes, and snippets.

@MattiaPezzanoAiv
Created May 13, 2018 08:54
Show Gist options
  • Save MattiaPezzanoAiv/2c36000c50b67412e6b6da8a4b82de4b to your computer and use it in GitHub Desktop.
Save MattiaPezzanoAiv/2c36000c50b67412e6b6da8a4b82de4b to your computer and use it in GitHub Desktop.
import maya.OpenMaya as OpenMaya
# Get the selection and create a selection list of all the nodes meshes
selection = OpenMaya.MSelectionList()
OpenMaya.MGlobal.getActiveSelectionList(selection);
# Create an MItSelectionList class to iterate over the selection
# Use the MFn class to as a filter to filter node types
iter = OpenMaya.MItSelectionList ( selection, OpenMaya.MFn.kGeometric );
# This uses built in functions of the MItSelectionList class to loop through the list of objects
# NB: isn't a basic array, you must use the built in functions
while not iter.isDone(): #iterate selection
vertexList = []
edgeList = []
polytriVertsList = []
polyList = []
connectedPolyList = []
#get dag path of current iterated selection
dagPath = OpenMaya.MDagPath()
iter.getDagPath( dagPath )
# get the selection as an MObject
mObj = OpenMaya.MObject()
iter.getDependNode( mObj )
# create iterator of current mesh polygons
polygonsIterator = OpenMaya.MItMeshPolygon( mObj )
# Iterate through polygons on current mesh
while not polygonsIterator.isDone():
# Get current polygons index
polyList.append (polygonsIterator.index())
# Get current polygons vertices
verts = OpenMaya.MIntArray()
polygonsIterator.getVertices( verts )
# Append the current polygons vertex indices
for i in range( verts.length() ):
vertexList.append (verts[i])
# Get current polygons edges
edges = OpenMaya.MIntArray()
polygonsIterator.getEdges( edges )
# Append the current polygons edge indices
for i in range( edges.length() ):
edgeList.append (edges[i])
# Get current polygons connected faces
indexConnectedFaces = OpenMaya.MIntArray()
polygonsIterator.getConnectedFaces( indexConnectedFaces )
# Append the connected polygons indices
for i in range( indexConnectedFaces.length() ):
connectedPolyList.append (indexConnectedFaces[i])
# Get current polygons triangles
pointArray = OpenMaya.MPointArray()
intArray = OpenMaya.MIntArray()
space = OpenMaya.MSpace.kObject
# Get the vertices and vertex positions of all the triangles in the current face's triangulation.
polygonsIterator.getTriangles(pointArray, intArray, space)
# Append vertices that are part of the triangles
for i in range( intArray.length() ):
polytriVertsList.append (intArray[i])
# next poligon
polygonsIterator.next()
# print data for current selection being iterated on
print ("Object name: {}".format(dagPath.fullPathName()))
print ("Vertex list: {}".format(vertexList))
print ("Edge list: {}".format(edgeList))
print ("Poly Triangle Vertices: {}".format(polytriVertsList))
print ("Polygon index list: (index buffer?) {}".format(polyList))
print ("Connected Polygons list: (or index buffer?){}".format(connectedPolyList))
# next selected
iter.next()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment