How to select the new keys to be created?

How to select the new keys to be created?

dg3duy
Collaborator Collaborator
246 Views
2 Replies
Message 1 of 3

How to select the new keys to be created?

dg3duy
Collaborator
Collaborator

How to select the new keys to be created based on the percentage?

keys unselect.gif

 

import maya.cmds as mc


def insertKeyPercentage(percentage = 100):

ckey = mc.currentTime(q = True)
keynxt = mc.findKeyframe(t = (ckey,ckey), w = "next")

p = ((keynxt-ckey)/100)*percentage


key1 = mc.setKeyframe(i = True, s = True, t = (round(ckey +p*0.9)))
key2 = mc.setKeyframe(i = True, t = (round(ckey +p*0.8)))
key3 = mc.setKeyframe(i = True, t = (round(ckey +p*0.7)))


insertKeyPercentage(percentage = 100)

 

0 Likes
Accepted solutions (1)
247 Views
2 Replies
Replies (2)
Message 2 of 3

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

Since you are creating the keyframe on all keyables and you are inserting it into the timeline at a position where no other keyframes are, you simply can run the selectKey command in the timerange where the keys were created:

import maya.cmds as mc


def insertKeyPercentage(percentage = []):
    
    ckey = mc.currentTime(q = True)
    keynxt = mc.findKeyframe(t = (ckey,ckey), w = "next")
    
    t = []
    for pv in percentage:
        p = ((keynxt-ckey)/100)*pv
        
        
        key1 = mc.setKeyframe(i = True, s = True, t = (round(ckey +p)))
        t.append((round(ckey +p)))
        
    mc.selectKey(t=(t[0],t[-1]))

    


insertKeyPercentage(percentage = [70,80,90])

 

Also I changed the kwarg "percentage" so it is now a list that takes multiple percentages, so you don't have to hardcode that into the function.

 

I hope it helps!

 

Message 3 of 3

dg3duy
Collaborator
Collaborator

@Kahylan Excellent, I look at the code and my head explodes a little at how you solved the problem. Thank you very much.

0 Likes