标题有点抽象,但我认为它与我正在寻找的内容是准确的。具体给定多边形边的列表,将所有由顶点连接的边分组。
我知道这应该是一个简单的问题,但我一直在寻找错误的解决方案。
示例输入:
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)
选择一个边缘,我得到一组。选择了两个未连接的边,我得到了两组,但两组都有相同的边。