Search code examples
python3dmayamelpymel

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:

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.


Solution

  • 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).)

    Everything in that list will be a subclass of PyNode, so you can rely on them having a method 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; MeshVertex for example.

    You can use isinstance(obj, type_sequence) to filter out components:

    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.