maya python check是用户选择了面部或对象

问题描述 投票:0回答:2

我想做一个简单的测试并确认用户是否选择了面,顶点或对象。返回数字0表示面,1表示顶点,如果选择了对象则返回3。

这段代码似乎无法提供我可以轻松测试的信息

import maya.cmds as cmds
selection = cmds.ls(sl=True)[0]
print type(selection)
print selection

我想简单地尝试实现的目标:

if selection == 0:
    do...
elif selection == 1
    do...
elif selection == 2:
    do...
python maya
2个回答
1
投票

cmds.polyListComponentConversion用作组件选择的过滤器。如果您将'从'转换'为类型,则可以对其进行过滤。在ls命令中添加type = float3以忽略整个对象,你会得到这样的结果:

import maya.cmds as cmds
from collections import namedtuple
selectiontype = namedtuple('selectiontype', 'faces verts edges')

def get_selected_components():
    sel = cmds.ls(sl=True, type = 'float3') # this is obscure maya way to get only components
    faces = cmds.polyListComponentConversion(sel, ff=True, tf =True)
    verts = cmds.polyListComponentConversion(sel, fv=True, tv =True)
    edges = cmds.polyListComponentConversion(sel, fe=True, te =True)
    return selectiontype (faces, verts, edges)


get_selected_components()
# Result: selectiontype(faces=[], verts=[u'pSphere1.vtx[0:381]', u'pTorus1.vtx[112]', u'pTorus1.vtx[131:132]', u'pTorus1.vtx[151:154]', u'pTorus1.vtx[171:174]', u'pTorus1.vtx[191:194]', u'pTorus1.vtx[211:214]', u'pTorus1.vtx[231:234]', u'pTorus1.vtx[251:254]', u'pTorus1.vtx[272:274]'], edges=[]) # 

而不是使用它的代码中的数字,只需使用vanilla python if-check:

selected = get_selected_components()
if selected.faces:
     # do something

if selected.verts:
     # something else

你可能不想做if 1:.... if 2:....,因为选择可能包含各种各样的东西;用户一次选择所有三种类型中的一些并不困难


0
投票
if '.f[' in selection:
    do...
elif '.e[' in selection
    do...
elif '.vtx[' in selection:
    do...
elif '.' not in selection:
    do...
© www.soinside.com 2019 - 2024. All rights reserved.