MEL How-To #84

Back · Previous · Next Maya

How do I query which keyframes are selected in the Graph Editor or Dope Sheet?

To get the selected curves and keys you can use the ‘keyframe’ command.

// Selected animCurve(s)
string $animCurves[] = `keyframe -q -name`;
// Result: pSphere1_translateX //

An animCurve may be feeding more than one node/channel/whatever in Maya. You can determine its effect by finding its downstream connections:

string $effects[] = `listConnections -plugs true $animCurves[0]`;
// Result: pSphere1.translateX //

To determine the values for the key, use the ‘-timeChange’ and ‘-valueChange’ flags:

// Here I've selected the entire curve:

float $selectTimes[] = `keyframe -q -sl -timeChange $animCurves[0]`;
// Result: 1 60 //

float $selectValues[] = `keyframe -q -sl -valueChange $animCurves[0]`;
// Result: 0 0.5 //

// And here I've selected the key at frame 60:

float $selectTimes[] = `keyframe -q -sl -timeChange $animCurves[0]`;
// Result: 60 //

float $selectValues[] = `keyframe -q -sl -valueChange $animCurves[0]`;
// Result: 0.5 //

Note in the examples above how the $animCurves array is used to explicitly query a single animCurve. If more than one animCurve is selected this is required to avoid ambiguous results. This becomes important when it is necessary to modify a value for a selected curve (described below).

Consider the following Graph Editor where I've selected the keys at frame 60 for all three translation curves.

Graph Editor

string $animCurves[] = `keyframe -q -sl -name`;
// Result: pSphere1_translateX pSphere1_translateY pSphere1_translateZ //

keyframe -q -sl -vc;
// Result: 1 1 1 //

keyframe -q -sl -tc;
// Result: 60 60 60 //

This wouldn't do you much good if you were looking to modify a key at a particular frame. However, you can isolate the key and time values by querying on individual curves.

keyframe -q -sl -vc pSphere1_translateY;  // or $animCurves[1] from above
// Result: 1 //

keyframe -q -sl -tc pSphere1_translateY;  // or $animCurves[1] from above
// Result: 60 //

Editing the selected key is a bit more involved. You need to edit the key by its index, but you cannot query the selected index. Thus, you must query the selected time/value, iterate the curve looking for said key, then write. Here is where it becomes necessary to specify the target animCurve, so as to avoid modifying a key an identical time or value, but on a curve animating a different channel.

float $allTimes[] = `keyframe -q -timeChange $animCurves[0]`;
float $allValues[] = `keyframe -q -valueChange $animCurves[0]`;

// Set selected key value to 0.2
for ( $i = 0; $i < `size $allTimes`; $i++ )
  if ( ( $allTimes[$i] == $selectTime[0] ) &&
       ( $allValues[$i] == $selectValue[0] ) )
    keyframe -e -index $i -valueChange 0.2 $animCurves[0];

In summary, use ‘keyframe -q -sl -name’ to find the selected curves, and store these names in an array. Then, specify each curve name in the ‘-tc’ and ‘-vc’ queries to determine which keys are selected for each curve. Don't grab them all using ‘keyframe -q -sl -tc’ unless you really want to perform a global edit across all selected curves.

Related How-To's

05 August 2002