2013-07-14 52 views
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中,寫在蟒蛇。

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) 

有了一個邊緣選擇,我得到一組。 選擇了兩個不相連的邊,我得到了兩組,但兩組的邊都有相同的邊。

+0

注意,當DOI ng(因爲我在範圍內(len(foo))',你也可以'爲我,_在枚舉(foo)'。 – Amber

+0

這是如何「排序」?我認爲說你正在分類多邊形是更正確的。另外,如果沒有獲得的輸出以及希望獲得的預期輸出,很難準確理解您想要的結果。 – Bakuriu

+0

true,編輯帖子以添加示例輸入,輸出和預期輸出。 –

回答

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) 
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()) 

的時候,它的面向對象的更加流暢。

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]'])} # 

的所有值均爲集,所以你可以測試一下,看看如果兩個綠黨都在交叉連接兩個VERT的集:

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