0

标题有点抽象,但我认为它与我正在寻找的内容是准确的。具体给定多边形边的列表,将所有由顶点连接的边分组。

我知道这应该是一个简单的问题,但我一直在寻找错误的解决方案。

示例输入:

looseComponents = cmds.ls(sl=True, fl=True)
# Result: [u'pCube1.e[3]', u'pCube1.e[6]'] # 

获取输出:

found 2 component sets
[u'pCube1.e[3]']
[u'pCube1.e[3]']

预期输出:

found 2 component sets
[u'pCube1.e[3]']
[u'pCube1.e[6]']

这就是我目前拥有的,它在 Maya 中运行,用 Python 编写。

looseComponents = cmds.ls(sl=True, fl=True)

#build sub item lookup
componentVerts = {}
for component in looseComponents:
    verts = cmds.ls(cmds.polyListComponentConversion(component, toVertex=True), fl=True)
    vertIds = []
    for vert in verts:
        i = int( vert.split('[')[-1].strip(']'))
        vertIds.append(i)
    componentVerts[component] = set(vertIds)

sortedComponents = 0
totalComponents = len(looseComponents)
componentSets = [ [] ]
componentSets[0].append( looseComponents[0] )
sortedComponents += 1

while sortedComponents < totalComponents:
    # for each subSetA of setA
    foundConnectingSet = False
    for i in range(len(componentSets)):
        # for each itemA of subSetA
        for j in range(len(componentSets[i])):
            # for each itemB of setB
            for h in range(len(looseComponents)):
                # check if itemA and itemB share a sub item
                if componentVerts[componentSets[i][j]] & componentVerts[looseComponents[h]]:
                    # if itemB isn't already part of this subSetA
                    if looseComponents[h] not in componentSets[i]:
                        # add itemB to this subSetA
                        componentSets[i].append(looseComponents[h])
                        sortedComponents += 1
                        foundConnectingSet = True

    if not foundConnectingSet:
        starter = looseComponents[0]
        for i in range(len(componentSets)):
            looseComponents = list( set(looseComponents) - set(componentSets[i]))
        componentSets.append( [ starter ] )         
        sortedComponents += 1

print 'found ' +str(len(componentSets))+ ' component sets'
for componentSet in componentSets:
    cmds.confirmDialog()
    cmds.select(componentSet, replace=True)
    print cmds.ls(sl=True)

选择一个边缘,我得到一组。选择了两个未连接的边,我得到了两组,但两组都有相同的边。

4

3 回答 3

0

好吧,我有一个解决方案可以始终如一地工作。

looseComponents = cmds.ls(sl=True, fl=True)

totalComponents = len( looseComponents )
sortedComponents = 0    
componentSets = []

vertComponent = {}
componentVert = {}
for component in looseComponents:
    verts = cmds.ls(cmds.polyListComponentConversion(component, toVertex=True), fl=True)
    for vert in verts:
        if vert in vertComponent:
            vertComponent[vert].append(component)
        else:
            vertComponent[vert] = [component]
    componentVert[component] = verts

def getConnectedComponent(component):
    verts = componentVert[component]
    connections = []
    for vert in verts:
        connections.extend( vertComponent[vert] )
    return list(set(connections) - set([component]))

def getRemainingComponent():
    remainingComponents = looseComponents
    for componentSet in componentSets:
        remainingComponents = list(set(remainingComponents)-set(componentSet))
    if remainingComponents:
        return remainingComponents[0]
    else:
        return None

while sortedComponents < totalComponents:
    component = getRemainingComponent()
    if component:
        componentSets.append( [component] )
        sortedComponents += 1
    connections = getConnectedComponent(component)
    while set(connections) - set(componentSets[-1]):
        newConnections = list( set(connections) - set(componentSets[-1]) )
        componentSets[-1].extend( newConnections )
        sortedComponents += len(newConnections)
        connections = []
        for component in newConnections:
            connections.extend( getConnectedComponent(component) )

print 'found ' +str(len(componentSets))+ ' component sets'
for componentSet in componentSets:
    cmds.confirmDialog()
    cmds.select(componentSet, replace=True)
    print cmds.ls(sl=True)
于 2013-07-14T16:04:32.013 回答
0

我知道我经常在这里使用 pymel,但是看着每个人都经历 cmds 的麻烦真是太痛苦了。如果您对它们没有义务,则替代方案是:

from pymel.core import *

verts = [i for i in polyCube()[0].vtx]

for j in verts:
    print "{0} is connected to {1}".format(j, j.connectedEdges())

当它面向对象时,它更加流畅。

于 2013-07-15T01:04:41.670 回答
0

我只是通过将所有转换为边缘的顶点制作字典来尝试它:

import maya.cmds as cmds

def vert_edge_map(obj):
    verts = cmds.polyEvaluate(obj, v=True)
    mapping = {}
    for r in range(0, verts):
        edges = cmds.polyListComponentConversion(obj + ".vtx[%i]" % r, fv=True, te=True)
        edges = cmds.filterExpand(edges, sm=32)
        mapping[r] = set(edges)
    return mapping

vert_edge_map('pCube1')
# Result: {0: set([u'pCube1.e[4]', u'pCube1.e[0]', u'pCube1.e[10]']), 1: set([u'pCube1.e[0]', u'pCube1.e[11]', u'pCube1.e[5]']), 2: set([u'pCube1.e[4]', u'pCube1.e[1]', u'pCube1.e[6]']), 3: set([u'pCube1.e[1]', u'pCube1.e[7]', u'pCube1.e[5]']), 4: set([u'pCube1.e[2]', u'pCube1.e[8]', u'pCube1.e[6]']), 5: set([u'pCube1.e[2]', u'pCube1.e[9]', u'pCube1.e[7]']), 6: set([u'pCube1.e[3]', u'pCube1.e[8]', u'pCube1.e[10]']), 7: set([u'pCube1.e[3]', u'pCube1.e[9]', u'pCube1.e[11]'])} # 

所有的值都是集合,因此您可以通过与两个顶点的集合相交来测试两个顶点是否连接:

def are_connected(v1, v2, mapping):
    return len(mapping[v1].intersection(mapping[v2])) > 0

并使用工会创建连接的东西岛:

def lots_of_edges(mapping, *verts):
   result = set()
   for v in verts:
       result = result.union(mapping[v])
   return result
于 2013-07-15T22:40:11.487 回答