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