在左侧和右侧之间划分顶点列表

时间:2014-06-01 22:16:37

标签: python python-2.7 maya

我试图在左右之间划分选定顶点的球体 (通过x测量顶点pointPosition值)。如果我左边有两个顶点而右边有一个顶点, 它返回我左边有3个,而不是左边有2个 一个在右边。

我应该如何重构循环,以便正确创建一个专门的左或右列表?

import maya.cmds as mc

iObj = mc.ls( sl = True, flatten = True )
selSize = len( iObj )

for numVert in range ( 0, selSize ):

    posSel = mc.ls( sl = True, flatten = True )
    posSelPos = mc.pointPosition( posSel[ numVert ], world = True )

    if posSelPos[ 0 ] > 0:

        leftVerts.append( posSel )
        leftSideSize = len( leftVerts[ 0 ] )
        print "Lside has " + "%s" % ( leftSideSize )
        print leftSideSize           
        print leftVerts[ 0 ]

    elif posSelPos[ 0 ] < 0:

        rightVerts.append( posSel )
        rightSideSize = len( rightVerts[ 0 ] )
        print "Rside has " + "%s" % ( rightSideSize )           
        print rightSideSize
        print rightVerts[ 0 ]

1 个答案:

答案 0 :(得分:1)

如果你得到一个误导性的答案,可能是因为你在0坐标上有顶点。在你的测试中,你将获得大于或小于零的东西,但没有任何东西恰好为零。

收集值非常简单,您可以使用python list comprehensions

更优雅地完成
import maya.cmds as cmds

# make a list of verts, using filterexpand so you don't get 
# ranges like pCube1.vtx[1:3]

selected = cmds.filterExpand(cmds.ls(sl=True), sm=31)   

items_by_position = [ (vtx, cmds.pointPosition(vtx)) for vtx in selected] 
# this gives you a list of [(vertex, position), (vertex, position)] pairs 

left = [item for item in items_by_position if item[1][0] > 0]
right = [item for item in items_by_position if item[1][0] < 0]
middle = [item for item in items_by_position if item[1][0] == 0]

print "left\n", left
print "right\n", right
print "middle\n", middle

您可能会发现将左,右和中间列表转换为词典非常方便:

selected = cmds.filterExpand(cmds.ls(sl=True), sm=31)       
items_by_position = [ (vtx, cmds.pointPosition(vtx)) for vtx in selected]
left = dict([item for item in items_by_position if item[1][0] > 0])
right = dict([item for item in items_by_position if item[1][0] < 0])
middle = dict([item for item in items_by_position if item[1][0] == 0])

所以你可以从列表中抓取顶点的位置。如果你翻转

(vtx, cmds.pointPosition(vtx))

进入

(cmds.pointPosition(vtx), vtx)

并检查项目[0]而不是项目1,您的词典将按位置键入