PDA

View Full Version : Storing lastSelected key in a variable


Horganovski
01-03-2011, 03:05 AM
Hey Folks,

Probably a simple one for those who know their MEL, but I'm still pretty new to it and I'm afraid it has me stumped.

I'm trying to write a little animation helper and I want to get the last selected keyframe and store it in a variable. I can do this :

keyframe -q -lastSelected;

and it returns the value I need.. but I can't seem to store that in a variable, I've tried :

float $endKey=`keyframe -q -lastSelected`;
and
float $endKey[]=`keyframe -q -lastSelected`;

but both return errors (// Error: Cannot convert data of type float[] to type float. // ) and (// Error: Invalid redeclaration of variable "$endKey" as a different type. // ) respectively, but I'm afraid I'm still a little too green to be able to understand/make use of those messages. It seems to me that the variable should just be a float since I'm looking for a single number, but adding the brackets in the 2nd example to make the variable an array doesn't seem to work either. I'm probably overlooking something simple.

Any ideas much appreciated.

Cheers,
Brian

r4inm4ker
01-03-2011, 03:14 AM
Your second try would have worked actually. It failed only because you've originally defined your variable to a float type. Technically speaking, in MEL you cannot redefine your variable in the same scope.
So the solution is : restart your maya and reexecute that line. It'll work fine, in think. :D

Horganovski
01-03-2011, 03:50 AM
Many thanks, I understand now, it was meant to be an array, but as you said it can't be declared again without restarting Maya. That's very useful for me to know.

I got what I was looking for working now, very simple but just to practice, it's a little script that will flip the selected keys time-wise. Not bullet proof (you need to reselect the keys before running it again) but it does the trick for right now.

Thanks again,
Cheers,
Brian


// script to reverse the selected keys in graph editor (time-wise)
float $selectedKeys[]=`keyframe -q -selected`;
float $firstKey =$selectedKeys[0];
float $lastKey =$selectedKeys[size($selectedKeys) - 1];
float $pivot =(($lastKey+$firstKey)/2);
scaleKey -iub false -ts -1 -tp $pivot -fs -1 -vs 1 -vp 0 -animation keys ;

djx
01-03-2011, 04:56 AM
You can avoid restarting maya if you limit the scope of your variable declarations, by enclosing your code in curly brackets {} like this:
{
float $selectedKeys[]=`keyframe -q -selected`;
float $firstKey =$selectedKeys[0];
float $lastKey =$selectedKeys[size($selectedKeys) - 1];
float $pivot =(($lastKey+$firstKey)/2);
scaleKey -iub false -ts -1 -tp $pivot -fs -1 -vs 1 -vp 0 -animation keys ;
}
You have to select and execute all the lines, including the brackets. As far as maya is concerned the variable will only exist for the duration of that single execution, which can be good if you are experimenting and likely to change type declarations.

David

Horganovski
01-03-2011, 06:04 AM
Many thanks, that's very useful to know.

Cheers,
Brian

CGTalk Moderation
01-03-2011, 06:04 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.