Skip to content

Instantly share code, notes, and snippets.

@r4inm4ker
Last active May 11, 2016 19:11
Select enclosed faces by edge loop and one inner vertex in Maya
import maya.cmds as cmds
import re
import maya.OpenMaya as om
def getComponentId(component):
"""get id number from a component (i.e. pCube.vtx[12]) , and return as int (i.e. 12 ) ."""
tokens = re.split('[\[\]]',str(component))
try:
return int(tokens[1])
except (ValueError,IndexError):
return -1
def getDagPath(obj):
'''get the dagPath node from object
'''
if not obj:
return []
cmds.select(obj,r=1)
selList = om.MSelectionList()
om.MGlobal.getActiveSelectionList(selList)
selListcompIter = om.MItSelectionList(selList)
dagPathList = []
while not selListcompIter.isDone():
currDagPath = om.MDagPath()
currComponent = om.MObject()
selListcompIter.getDagPath(currDagPath, currComponent)
dagPathList.append(currDagPath)
selListcompIter.next()
return currDagPath
def getVtxList(innerVtx,wallVtx):
obj = cmds.ls(innerVtx,o=1)
dagPath = getDagPath(obj[0])
meshFn = om.MFnMesh(dagPath)
numVtx = meshFn.numVertices()
processedVtxList = []
dummy = [processedVtxList.append(0) for index in range(0,numVtx)]
selectedVtx = []
transform = wallVtx[0].split('.')[0]
for vtx in wallVtx:
currId = getComponentId(vtx)
selectedVtx.append(currId)
processedVtxList[currId] = 1
vtxStack = []
if type(innerVtx) == type([]):
for vtx in innerVtx:
vtxStack.append(getComponentId(vtx))
else:
vtxStack.append(getComponentId(innerVtx))
vtxIter = om.MItMeshVertex(dagPath)
faceIter = om.MItMeshPolygon(dagPath)
dummyUtil = om.MScriptUtil()
dummyPtr = dummyUtil.asIntPtr()
while vtxStack and len(selectedVtx)<numVtx:
currVtxId = vtxStack.pop(0)
if processedVtxList[currVtxId]:
continue
selectedVtx.append(currVtxId)
processedVtxList[currVtxId] = 1
vtxIter.setIndex(currVtxId,dummyPtr)
connectedFaceList = om.MIntArray()
vtxIter.getConnectedFaces(connectedFaceList)
index = 0
ringVtxList = []
while(index < connectedFaceList.length()):
connFaceId = connectedFaceList[index]
faceIter.setIndex(connFaceId, dummyPtr)
connectedVtxList = om.MIntArray()
faceIter.getVertices(connectedVtxList)
index2 = 0
while(index2 < connectedVtxList.length()):
connVtxId = connectedVtxList[index2]
if not processedVtxList[connVtxId] and not connVtxId in ringVtxList:
ringVtxList.append(connVtxId)
index2+=1
index+=1
dummy = [vtxStack.append(member) for member in ringVtxList]
return selectedVtx
def selectEnclosedFaces():
edges = cmds.filterExpand(ex=0,sm=32)
vtx = cmds.filterExpand(ex=1,sm=31)
if edges and vtx:
transform = edges[0].split('.')[0]
wallVtx = cmds.ls(cmds.polyListComponentConversion(edges,fe=1,tv=1),fl=1)
vtxList = getVtxList(vtx,wallVtx)
vtxNameList = []
for each in vtxList:
vtxNameList.append(transform+'.vtx['+str(each)+']')
faceList = cmds.polyListComponentConversion(vtxNameList,fv=1,tf=1,internal=1)
if faceList:
cmds.select(faceList)
selectEnclosedFaces()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment