如何获取选择的对象类型
我需要根据当前的选择,用PYMEL查询并执行一些任务,比如说:
from pymel.core import *
s = selected()
if (s.selType() == 'poly'):
#do something
if (s.selType() == 'surface'):
#do something
if (s.selType() == 'cv'):
#do something
if (s.selType() == 'vertex'):
#do something
if (s.selType() == 'face'):
#do something
if (s.selType() == 'edge'):
#do something
if (s.selType() == 'curve'):
#do something
我知道selType()
并不是一个真正的pymel函数,我想利用pymel的API命令,而不是使用标准的mel命令,这样说清楚吗?
2 个回答
1
你可以使用maya自带的filterExpand命令来把选中的东西按类型分类。这个命令会帮你筛选出你所选的对象,并列出你想要的那种类型的物体。
举个例子:
import maya.cmds as cmds
selection = cmds.ls(sl=1) # Lists the current selection and
# stores it in the selection variable
polyFaces = cmds.filterExpand(sm=34) # sm (selectionMask) = 34 looks for polygon faces.
# Store the result in polyFaces variable.
if (polyFaces != None): # If there was any amount of polygon faces.
for i in polyFaces: # Go through each of them.
print(i) # And print them out.
关于这个命令和对应的过滤器的详细信息,可以在python或mel命令参考中找到。
2
PyMEL会把你选择的列表转换成节点(和MEL不同,MEL里的东西都是简单的数据类型)。至少在使用ls
和相关命令时是这样的(selected
其实就是ls(sl=True)
的简写)。
在这个列表里的每个东西都会是PyNode
的一个子类,所以你可以放心它们都有一个叫nodeType
的方法。
这样一来,根据每个选择的类型进行处理就变得简单了。
组件是从pymel.core.Component
继承来的,每种组件类型都有一个对应的类,比如MeshVertex
就是其中之一。
你可以使用isinstance(obj, type_sequence)
来筛选出组件:
filter(lambda x: isinstance(x, (pm.MeshVertex, pm.MeshEdge, pm.MeshFace)), pm.selected())
你可以在PyMEL文档的general
部分找到它们。