Keys array

Keys array

dg3duy
Collaborator Collaborator
1,546 Views
10 Replies
Message 1 of 11

Keys array

dg3duy
Collaborator
Collaborator

 

Thanks to a user of this forum I obtained this script to add keys, what I am needing is to be able to store the keys of the current time and the previous and following ones.
Store the selection of keys that are selected in the image I made as an example. That selection information can be used later to reselect the keys.
I want to store in an array to be able to select them later by means of the index.

 

 

import maya.cmds as mc

def insertKeyPercentage(percentage = 100):
    
    key1 = mc.currentTime(q = True)

    p = 4
    
    key2 = mc.setKeyframe(i = True, t = (key1 +p))
    key3 = mc.setKeyframe(i = True, t = (key1 -p))

insertKeyPercentage(percentage = 100)


 

keys.gif 

0 Likes
Accepted solutions (1)
1,547 Views
10 Replies
Replies (10)
Message 2 of 11

mcw0
Advisor
Advisor

So you want to store keys bracketed around the current time.  And then to be able to retrieve that info at any time.

 

//  store global variable so it's accessible at any time

global float $storedKeys[];

 

//  get keys of anim curve

float $keys[] = `keyframe -q -timeChange "yourNode.attribute"`;

 

//  loop through keys to find indices for global variable

for($i=0;$i<size($keys);$i++)

{

    //  if the key is greater than or equal to the current frame, then record the previous index

    if($keys[$i] >== `currentTime -q`)

    {

        $storedKeys[0] = $i-1;  //  recording key ahead of current frame

        $storedKeys[1] = $i;  //  recording the next key...this may or may not be the current frame

 

        //  check if key is equal to the current frame, then record the next index

        //  this check is needed in case there is no keyframe at the current frame

        //  in which case, what's been stored is all that's needed

        if($keys[$i] == `currentTime -q`)

            $storedKeys[2] = $i+1;  //  record key after current frame

 

        //  exit the loop

        break;

    }

}

 

0 Likes
Message 3 of 11

mcw0
Advisor
Advisor

Even better would be to have a global string variable for the stored keys.  Then you'd just have to...

 

selectKey -t $storedKeys

 

The variable would save the first and last keys instead of the indices as "first:last".  I'm not in Maya so this is thinking out loud.  Untested.  

0 Likes
Message 4 of 11

dg3duy
Collaborator
Collaborator
Unfortunately, several messages of this type Syntax error appear.
0 Likes
Message 5 of 11

mcw0
Advisor
Advisor

My bad.  Mistyped this line.

if($keys[$i] >== `currentTime -q`)

 

Should only have one "=" sign.  Sorry

0 Likes
Message 6 of 11

dg3duy
Collaborator
Collaborator

I appreciate your time, my thanks in advance.
Can you make a video of it working?
I can't get it to work.
it is very likely that I am not using the code correctly on my part.

0 Likes
Message 7 of 11

mcw0
Advisor
Advisor

First, are you running the code as MEL?

Second, you have to change "yourNode.attribute" to the actual node in your scene and the attribute you want to record the keys for.

 

And lastly, the global string is going to be better than the global float.

0 Likes
Message 8 of 11

dg3duy
Collaborator
Collaborator

it is very likely that I am using it wrong, here is a picture of how I imagine it is used.
keys.gif

0 Likes
Message 9 of 11

mcw0
Advisor
Advisor

You're getting an error.  Notice the error about $storedKeys.  In MEL, once you've declared a variable, you can't redeclare it as another type.  This is unlike Python.  So please restart Maya.

0 Likes
Message 10 of 11

mcw0
Advisor
Advisor

Plus the code I gave you is storing the indices.  The final "selectKey" command is expecting a string and not an array of indices.  I was typing and thinking on the fly.  You have to alter the code to save the indices as a string.

0 Likes
Message 11 of 11

mcw0
Advisor
Advisor
Accepted solution

//  store global variable so it's accessible at any time

global string $storedKeys;

float $arrayOfKeys[];

 

//  get keys of anim curve

float $keys[] = `keyframe -q -timeChange "yourNode.attribute"`;

 

//  loop through keys to find indices for global variable

for($i=0;$i<size($keys);$i++)

{

    //  if the key is greater than or equal to the current frame, then record the previous index

    if($keys[$i] >== `currentTime -q`)

    {

        $arrayOfKeys[0] = $i-1;  //  recording key ahead of current frame

        $arrayOfKeys[1] = $i;  //  recording the next key...this may or may not be the current frame

 

        //  check if key is equal to the current frame, then record the next index

        //  this check is needed in case there is no keyframe at the current frame

        //  in which case, what's been stored is all that's needed

        if($keys[$i] == `currentTime -q`)

            $arrayOfKeys[2] = $i+1;  //  record key after current frame

 

        //  exit the loop

        break;

    }

}

 

$storedKeys = ($arrayOfKeys[0]+":"+$arrayOfKeys[size($arrayOfKeys)-1]);

 

selectKey -t $storedKeys "yourNode.attribute";