如何获取选定对象类型

2024-04-27 15:22:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我基本上需要使用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函数,我还想利用pymels api命令,如果有意义的话,就不要使用标准的mel命令。


Tags: fromcoreimport命令ifsurfacedocv
2条回答

可以使用maya native 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命令参考。

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部分找到它们。

相关问题 更多 >