Question

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.

Was it helpful?

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.

OTHER TIPS

You could use the maya native filterExpand command to sort each into their respective types. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top