Create key

Create key

dg3duy
Collaborator Collaborator
520 Views
5 Replies
Message 1 of 6

Create key

dg3duy
Collaborator
Collaborator

Let's suppose I have 2 animation keys...
they are separated by X amount of frames, which we do not know.

How is it possible to create keys in percentage between those 2 keys?.....
for example, to create a key in 25%
another at 50%
another at 75%
Any idea where to investigate this?

0 Likes
Accepted solutions (2)
521 Views
5 Replies
Replies (5)
Message 2 of 6

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

The "findKeyframe" command can help you find out the time value of the keys and from there on out it's just a little bit of math and using the "setKeyframe" command to acctually key what you want.

So as a simple example, if you have the first key selected in the timeline and you want to insert a keyframe at 50% of time to the next keyframe on the selected object:

import maya.cmds as mc

def insertKeyPercentage(percentage = 50):
    
    key1 = mc.currentTime(q = True)
    key2 = mc.findKeyframe(t = (key1,key1), w = "next")
    
    p = ((key2-key1)/100)*percentage
    
    newKey = mc.setKeyframe(i = True, t = (key1 +p))
    
insertKeyPercentage(percentage = 50)

 

I hope it helps!

Message 3 of 6

dg3duy
Collaborator
Collaborator

it's great, thank you very much...

0 Likes
Message 4 of 6

dg3duy
Collaborator
Collaborator

What if you want to automatically select the new key created in the curve editor?

0 Likes
Message 5 of 6

Kahylan
Advisor
Advisor
Accepted solution

Since you already have the timerange defined, you can just use the select key command on that timerange with a buffer off 0.0001 on each side to select all the keyframes created by the command:

 

import maya.cmds as mc

def insertKeyPercentage(percentage = 50):
    
    mc.selectKey(clear = True)
    key1 = mc.currentTime(q = True)
    key2 = mc.findKeyframe(t = (key1,key1), w = "next")
    
    p = ((key2-key1)/100)*percentage
    
    
    newKey = mc.setKeyframe(i = True, t = (key1 +p))
    
    mc.selectKey(time = (key1+0.0001, key2-0.0001), k = True)
    
insertKeyPercentage(percentage = 50)

 

 

Now this will cause problems if the timespan between key1 and key2 is less than 0.0002 frames, but if that is the case, your scene has bigger problems anyways.

 

But I should also mention, that I added in a clearing of the key selection, since otherwise the program could not be run twice in a row without manually deselecting the keys

Message 6 of 6

dg3duy
Collaborator
Collaborator

fantastic! very useful, thank you very much.

0 Likes