Drake's Weblog

3 minute read

The quick answer is as below:

from pymel import *
filter( lambda n: 'light' in n.classification(), ls(sl=True, dag=True, leaf=True))

In pymel’s google group, there are 2 people giving different opinions for this and the following are from their post:

take out the ‘sl=True’ by using pymel’s “selected()” function by Ofer Koren

from pymel import *
filter( lambda n: 'light' in n.classification(), selected(dag=True, leaf=True)) 

Doing it through the API requires more code but is considerably faster by Dean Edmonds

import maya.OpenMaya as om
sel = om.MSelectionList()
iter = om.MItSelectionList(sel, om.MFn.kLight)
lights = []

while not iter.isDone():
    light = om.MDagPath()
    lights += [light]

The following article was written by Bryan Ewert located once at http://www.ewertb.com/maya/mel which was down for a while because some reason. Fortunately, you can still see his great work from a mirror site http://ewertb.soundlinker.com/ or Wayback Machine. I re-post it for convenient reference.

Well, logically you would think this would do it:

string $selectedLights[] = `ls -sl -lights`;  

But – while this does indeed return the list of selected lights, unfortunately it is only reliable if the user selects the shape node of the light. Often they won’t, nor should they be required to. Fortunately, the “ls” command offers a convenient method for retrieving the shape nodes from the current selection:

string $selectedShapes[] = `ls -selection -dag -leaf`;  

By using this query, you need not concern yourself if the user has selected the transform or the shape for a particular node. From here, you can typically use the “nodeType” command to query if the shape is the type you’re looking for:

if ( "mesh" == `nodeType $shape` ) {
  // Do something with a mesh shape. }  
if ( "camera" == `nodeType $shape` ) {
  // Do something with a camera. }  

However, “light” is not a node type; rather, there are several types of lights: spotLight, directionalLight, etc. A more generalized query is convenient in this case - that provided by the “getClassification” command. This command returns a classification string for the node’s type. Be aware that this returns a string array and not just a string.

  getClassification spotLight;
 // Result: light //
  getClassification volumeLight;
 // Result: light //

The following procedure demonstrates its use for extracting all lights from the current selection.

proc string[] getSelectedLights()
  string $selectedLights[];
  string $select[] = `ls -sl -dag -leaf`;
  for ( $shape in $select )
    // Determine if this is a light.
    string $class[] = getClassification( `nodeType $shape` );
    if ( ( `size $class` ) > 0 && ( "light" == $class[0] ) )
      $selectedLights[ `size $selectedLights` ] = $shape;
  // Result is an array of all lights included in
  // current selection list.
  return $selectedLights;
comments powered by Disqus

Recent posts



You're looking at Drake's words or statements. All opinions are my own.