简体   繁体   English

如何获取选定的对象类型

[英]How do I Get The Selected Object type

I need to basically query and perform a few tasks based on the current selection with PYMEL, example: 我基本上需要根据当前选择的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

I know that selType() is not an actual pymel function, I'd like to also take advantage of pymels api commands, not using standard mel commands if that makes sense. 我知道selType()不是真正的pymel函数,我也想利用pymels api命令,如果可以的话,不要使用标准的mel命令。

PyMEL will convert the selection list for you to nodes (unlike MEL, where everything is a simple datatype.) At least this is true with ls and related commands ( selected is just ls(sl=True) .) PyMEL将为您转换选择列表到节点(与MEL不同,在MEL中,一切都是简单的数据类型。)至少对于ls和相关命令( selected的只是ls(sl=True) ),这是正确的。

Everything in that list will be a subclass of PyNode , so you can rely on them having a method nodeType . 该列表中的所有内容都是PyNode的子类,因此您可以依靠它们使用nodeType方法。

From there, it is easy to process each selection based on its type. 从那里开始,很容易根据其类型来处理每个选择。


Components inherit from pymel.core.Component , and there is one class for each component type; 组件继承自pymel.core.Component ,每种组件类型都有一个类。 MeshVertex for example. 例如, MeshVertex

You can use isinstance(obj, type_sequence) to filter out components: 您可以使用isinstance(obj, type_sequence)筛选出组件:

filter(lambda x: isinstance(x, (pm.MeshVertex, pm.MeshEdge, pm.MeshFace)), pm.selected())

You can find them under the general section in the PyMEL docs. 您可以在PyMEL文档的general部分下找到它们。

You could use the maya native filterExpand command to sort each into their respective types. 您可以使用maya本机filterExpand命令将它们分类为各自的类型。 It essentially sifts through your selection and makes a list of the objects that correspond to the type you're looking for 它从本质上筛选您的选择,并列出与您要查找的类型相对应的对象的列表

For example: 例如:

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.

More info on the command and the filters corresponding int-value is in the python or mel command reference. 有关命令和与int值对应的过滤器的更多信息,请参见python或mel命令参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM