2020年5月31日日曜日

メッシュのシェル毎に頂点グループを作成

# http://www.janpijpers.com/finding-vertex-groups-in-maya/
import maya.OpenMaya as oldOpenMaya
import time
# メッシュのシェル毎に頂点グループを作成
def getVertexGroups(mesh, vtxSelectionSet):
    selList = oldOpenMaya.MSelectionList()
    selList.add(mesh)
    mObject = oldOpenMaya.MObject()
    selList.getDependNode(0, mObject)
    iterVertLoop = oldOpenMaya.MItMeshVertex(mObject)
    talkedToNeighbours = set()
    districtList = []
    for currentIndex in vtxSelectionSet:
        districtHouses = set()
        if not currentIndex in talkedToNeighbours:
            districtHouses.add(currentIndex)
            currentNeighbours = getNeighbours(iterVertLoop, currentIndex)
            while currentNeighbours:
                newNeighbours = set()
                for neighbour in currentNeighbours:
                    if neighbour in vtxSelectionSet and not neighbour in talkedToNeighbours:
                        talkedToNeighbours.add(neighbour)
                        districtHouses.add(neighbour)
                        newNeighbours = newNeighbours.union(getNeighbours(iterVertLoop, neighbour))
                currentNeighbours = newNeighbours
            districtList.append(districtHouses)
    return sorted(districtList, key=lambda x:min(x))

def getNeighbours(mVtxItter, index):
    mVtxItter.setIndex(index, oldOpenMaya.MScriptUtil().asIntPtr())
    intArray = oldOpenMaya.MIntArray()
    mVtxItter.getConnectedVertices(intArray)
    return set(int(x) for x in intArray)

selected = set(cmds.ls(sl=True, o=False, fl=True)) - set(cmds.ls(sl=True, o=True))
vtxSelectionSet = set([int(x.split(".vtx[")[-1][:-1]) for x in selected])
meshName = "pSphere1"
start = time.time()
vertexGroups = getVertexGroups(meshName, vtxSelectionSet)
print "Duration: %s seconds" %str(time.time()- start)

for groep, entries in enumerate(vertexGroups):
    print "Groep : " ,groep
    print "\tMin : " ,min(entries)
    print "\tMax : " ,max(entries)

0 件のコメント:

コメントを投稿