Animation layers insert keyframes

Animation layers insert keyframes

dg3duy
Collaborator Collaborator
512 Views
4 Replies
Message 1 of 5

Animation layers insert keyframes

dg3duy
Collaborator
Collaborator

By default the script works without animation layers.
I would like that someone could incorporate the option to make it work in animation layer, to incorporate the keys in the animation layer only and not in the animation base, only if there is a layer that adds the new keys to the layer and not to the animation base.
And if there is no layer that works in the animation base only. To detect if it has animation layers is what I am looking for.

 

import maya.cmds as mc

import pymel.core as pm

objects=pm.ls(sl=1)
attrs = pm.selectionConnection('graphEditor1FromOutliner', q=1, object=1)
buffer = []

for attr in attrs:   
    buffer = attr.split(".")
    buffer = buffer[1]
    print buffer


def insertKeyPercentage(percentage = []):
    
    
    objects=pm.ls(sl=1)
    attrs = pm.selectionConnection('graphEditor1FromOutliner', q=1, object=1)
    buffer = []
    for attr in attrs:   
        buffer = attr.split(".")
        buffer = buffer[1]        
        print buffer
 

    
    
    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)),attribute=buffer)
        t.append((round(ckey +p)))
        
    mc.selectKey(t=(t[0],t[-1]))

insertKeyPercentage(percentage = [10,20,30,40,50,60,70,80,90])




keys layers.gif

0 Likes
Accepted solutions (1)
513 Views
4 Replies
Replies (4)
Message 2 of 5

Kahylan
Advisor
Advisor

Hi!

 

I'm not gonna lie, what you are doing with your "buffer" variable is a mess and I don't know what exactly you are going for there.

I had to remove the attribute flag from the setKeyframe command to get my test working with animation layers since you were referencing the inputB argument on the animation layer with it which is unkeyable...

I'm assuming that you are using this to find out what curves are displayed in the graph editor, I'm not sure why you aren't using the solution that uses the animCurveEditor command that I posted here just a few days ago. I'm also not sure why you are looping through all selected channels to just use the last one.

 

Anyways, I kept my hands from that, this is the code that you would need to do what you are asking in this post. Set Keyframes on relevant animLayers if they exist, set them on base if they don't (given you get the buffer stuff working with animlayers):

        for o in objects:
            lyrs = mc.ls(typ = "animLayer")

            if lyrs == []:
                #run the normal keying here
                key1 = mc.setKeyframe(i = True, s = True, t = (round(ckey +p)),attribute=buffer)
            else:
                #run the animlayer keying here
                base = mc.animLayer(q= True, root = True)
                lyrs.remove(base)
                for l in lyrs:
                    key1 = mc.setKeyframe(al = l,i = True, s = True, t = (round(ckey +p)),attribute=buffer)
    
            
            t.append((round(ckey +p)))
            
    mc.selectKey(t=(t[0],t[-1]))

 

I hope it helps!

Message 3 of 5

dg3duy
Collaborator
Collaborator

@Kahylan these days I've been trying to make it work all together and I haven't been able to make it work... 

insert keys layers.gif

0 Likes
Message 4 of 5

Kahylan
Advisor
Advisor
Accepted solution

Ok...

 

So in your video the indentation level is off, and I don't see the logic of getting the attribute, so I can't tell you what is going wrong there.

 

The biggest problem is, that you are keying ALL the animation layers except the base layer, instead of just keying the curves that are visible in the grapheditor, which would be simple because you can just feed the the setKeyframe command the animation curve and don't have to worry about arguments:

import maya.cmds as mc



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

insertKeyPercentage(percentage = [10,20,30,40,50,60,70,80,90])

 

Now if you want to key on every animation layer except the base animation layer, that means that you need to key curves that are potentially not displayed in the graph editor at the time the script is executed. So you need to find the object from the current displayed animation curve and need to find out which attributes are exactly displayed in the graph editor. But the problem is, that since you have animation layers, the curves are no longer connected to the object, but to an BlendAnimCurveAdditionXX node and if you have multiple layers, each of those blend nodes is hooked up to the blend node of the next layer. So in order to find the attribute you basically need to do node hopping to find it, which can get incredibly complex really fast, I even needed to split up the script into different functions so it doesn't get too confusing:

import maya.cmds as mc
import pymel.core as pm

def insertKeyPercentage(percentage = []):
    
    attributes = findAttrs()
    print(attributes)
    lyrs = getLayers()
    
    ckey = mc.currentTime(q = True)
    keynxt = mc.findKeyframe(t = (ckey,ckey), w = "next")
    
    t = []
        
    for pv in percentage:
        p = ((keynxt-ckey)/100)*pv
        
        
        for o in attributes:
            for attr in attributes[o]:

                if lyrs == []:
                    key1 = mc.setKeyframe(o,i = True, s = True, t = (round(ckey +p)),attribute=attr)
                else:    
                    for l in lyrs:
                        key1 = mc.setKeyframe(o, al = l,i = True, s = True, t = (round(ckey +p)),attribute=attr)
        
                
                t.append((round(ckey +p)))
            
    mc.selectKey(t=(t[0],t[-1]))
    


def findAttrs():
    animCurves = mc.animCurveEditor( 'graphEditor1GraphEd', q =True, cs = True)
    blendNodeTypes = ['animBlendNodeAdditive', 'animBlendNodeAdditiveDA', 'animBlendNodeAdditiveDL', 'animBlendNodeAdditiveF', 'animBlendNodeAdditiveFA', 'animBlendNodeAdditiveFL', 'animBlendNodeAdditiveI16', 'animBlendNodeAdditiveI32', 'animBlendNodeAdditiveRotation', 'animBlendNodeAdditiveScale', 'animBlendNodeBoolean', 'animBlendNodeEnum', 'animBlendNodeTime']
    attrDict = {}
    
    for ac in animCurves:
            
        targetOld = ac
        con = False
        
        while con == False:
            targets = mc.listConnections("{0}.output".format(targetOld), p = True)
            o = targets[0].split(".",1)[0]
            at = targets[0].split(".",1)[1]
            targetOld = o
            
            if mc.objectType(o) not in blendNodeTypes:
                con = True
                
                if o in attrDict:
                    tempAttr = attrDict[o]
                    tempAttr.append(at)
                    attrDict[o] = tempAttr
                else:
                    attrDict[o] = [at]

        
    for k in attrDict:
        temp = attrDict[k]
        new = list(dict.fromkeys(temp)) 
        attrDict[k] = new    
          
    return attrDict

def getLayers():
    lyrs = mc.ls(typ = "animLayer")
    if lyrs != []:
        base = mc.animLayer(q= True, root = True)
        lyrs.remove(base)
    
    return lyrs    


insertKeyPercentage(percentage = [10,20,30,40,50,60,70,80,90])

,

This is a lot of additional work and computation for something that is in my eyes a counterintuitive mechanic. Since I would not expect to have keyframes on layers that aren't selected in the graph edior if the graph editor selection plays a role...

 

I hope it helps

 

Message 5 of 5

dg3duy
Collaborator
Collaborator

My mind explodes with the options you have incorporated, a lot of study material. Case solved, thank you very much!

0 Likes