<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: setKeyframe on a locked animation layer in Maya Programming Forum</title>
    <link>https://forums.autodesk.com/t5/maya-programming-forum/setkeyframe-on-a-locked-animation-layer/m-p/5299387#M14210</link>
    <description>&lt;P&gt;Slight modification to avoid "'NoneType' object is not iterable" error.&lt;/P&gt;&lt;PRE&gt;def setAnimLayerLockState(animLayer, lockState):
    allAnimLayers = cmds.ls(type='animLayer')
    if animLayer not in allAnimLayers:
        print '"{0}" is not a valid animation layer'.format(animLayer)
        return
    
    # Lock or unlock the animation layer
    cmds.animLayer(animLayer, edit=True, lock=lockState)
    
    # Now lock or unlock all animation curves in the specified layer
    baseLayer = cmds.animLayer(q=True, root=True)
    if animLayer == baseLayer:
        allAnimCurves = set(cmds.ls(type='animCurve'))
        for animLayerName in allAnimLayers:
            if animLayerName == baseLayer:
                continue
            layerCurves = cmds.animLayer(animLayerName, q=True, animCurves=True) or []
            allAnimCurves -= set(layerCurves)            
        for animCurve in allAnimCurves:
            cmds.setAttr(animCurve+'.ktv', lock=lockState)
    else:
        layerAnimCurves = cmds.animLayer(animLayer, q=True, animCurves=True) or []
        for animCurve in layerAnimCurves:
            cmds.setAttr(animCurve+'.ktv', lock=lockState)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 25 Sep 2014 20:09:30 GMT</pubDate>
    <dc:creator>RFlannery1</dc:creator>
    <dc:date>2014-09-25T20:09:30Z</dc:date>
    <item>
      <title>setKeyframe on a locked animation layer?</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/setkeyframe-on-a-locked-animation-layer/m-p/5293905#M14208</link>
      <description>&lt;P&gt;I have a script that sets keyframes on an object.&amp;nbsp; If the base animation layer is locked, the keyframe does not get set.&amp;nbsp; I tried adding code to unlock the base animation layer, set the keyframe, and then relock it.&amp;nbsp; This does not work.&amp;nbsp; However, if I manually unlock the layer and THEN run my script, it works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Reproducing the problem:&lt;/P&gt;
&lt;P&gt;Create a new scene.&amp;nbsp; Add an object (cube, sphere, whatever) to the scene.&amp;nbsp; Set a couple of keys on the object.&amp;nbsp; Now create a new animation layer with nothing in it.&amp;nbsp; Lock the base animation layer.&lt;/P&gt;
&lt;P&gt;Select the object, and run the code below.&amp;nbsp; The keyframe does not get set.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sample code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;def setKeyOnSelected(frameNumber, value):
    selObjs = cmds.ls(sl=True, o=True)
    
    currTime = cmds.currentTime(q=True)
    cmds.currentTime(frameNumber)
    
    baseLayer = cmds.animLayer(q=True, root=True)
    if baseLayer:
        baseLayerLockState = cmds.animLayer(baseLayer, q=True, lock=True)
        cmds.animLayer(baseLayer, edit=True, lock=False)
    
    for obj in selObjs:
        cmds.move(value, 0, 0, obj)
        res = cmds.setKeyframe(obj)
        print res
    
    cmds.currentTime(currTime)
    if baseLayer:
        cmds.animLayer(baseLayer, edit=True, lock=baseLayerLockState)&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;setKeyOnSelected(10, 5)&lt;/PRE&gt;
&lt;P&gt;Is this a bug, or do I have some fundamental misunderstanding about animation layers and setting keyframes?&lt;/P&gt;</description>
      <pubDate>Tue, 30 Sep 2014 15:39:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/setkeyframe-on-a-locked-animation-layer/m-p/5293905#M14208</guid>
      <dc:creator>RFlannery1</dc:creator>
      <dc:date>2014-09-30T15:39:36Z</dc:date>
    </item>
    <item>
      <title>Re: setKeyframe on a locked animation layer</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/setkeyframe-on-a-locked-animation-layer/m-p/5298373#M14209</link>
      <description>&lt;P&gt;I think I figured something out.&amp;nbsp; Looking through layerEditor.mel, it seems that when you unlock an animation layer through the GUI, it not only unlocks the layer but also unlocks all animation curves associated with that layer.&amp;nbsp; So I wrote a function to emulate that behavior, but without all the GUI-specific behavior.&lt;/P&gt;&lt;PRE&gt;def setAnimLayerLockState(animLayer, lockState):
    allAnimLayers = cmds.ls(type='animLayer')
    if animLayer not in allAnimLayers:
        print '"{0}" is not a valid animation layer'.format(animLayer)
        return
    
    # Lock or unlock the animation layer
    cmds.animLayer(animLayer, edit=True, lock=lockState)
    
    # Now lock or unlock all animation curves in the specified layer
    baseLayer = cmds.animLayer(q=True, root=True)
    if animLayer == baseLayer:
        allAnimCurves = set(cmds.ls(type='animCurve'))
        for animLayerName in allAnimLayers:
            if animLayerName == baseLayer:
                continue
            layerCurves = cmds.animLayer(animLayerName, q=True, animCurves=True)
            if layerCurves:
                allAnimCurves -= set(layerCurves)            
        for animCurve in allAnimCurves:
            cmds.setAttr(animCurve+'.ktv', lock=lockState)
    else:
        layerAnimCurves = cmds.animLayer(animLayer, q=True, animCurves=True)
        for animCurve in layerAnimCurves:
            cmds.setAttr(animCurve+'.ktv', lock=lockState)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Then, I just use this function where I previously used the line:&lt;/P&gt;&lt;PRE&gt;cmds.animLayer(baseLayer, edit=True, lock=baseLayerLockState)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;This it not 100% tested, but it seems to work pretty well for simple cases.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Sep 2014 15:13:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/setkeyframe-on-a-locked-animation-layer/m-p/5298373#M14209</guid>
      <dc:creator>RFlannery1</dc:creator>
      <dc:date>2014-09-25T15:13:41Z</dc:date>
    </item>
    <item>
      <title>Re: setKeyframe on a locked animation layer</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/setkeyframe-on-a-locked-animation-layer/m-p/5299387#M14210</link>
      <description>&lt;P&gt;Slight modification to avoid "'NoneType' object is not iterable" error.&lt;/P&gt;&lt;PRE&gt;def setAnimLayerLockState(animLayer, lockState):
    allAnimLayers = cmds.ls(type='animLayer')
    if animLayer not in allAnimLayers:
        print '"{0}" is not a valid animation layer'.format(animLayer)
        return
    
    # Lock or unlock the animation layer
    cmds.animLayer(animLayer, edit=True, lock=lockState)
    
    # Now lock or unlock all animation curves in the specified layer
    baseLayer = cmds.animLayer(q=True, root=True)
    if animLayer == baseLayer:
        allAnimCurves = set(cmds.ls(type='animCurve'))
        for animLayerName in allAnimLayers:
            if animLayerName == baseLayer:
                continue
            layerCurves = cmds.animLayer(animLayerName, q=True, animCurves=True) or []
            allAnimCurves -= set(layerCurves)            
        for animCurve in allAnimCurves:
            cmds.setAttr(animCurve+'.ktv', lock=lockState)
    else:
        layerAnimCurves = cmds.animLayer(animLayer, q=True, animCurves=True) or []
        for animCurve in layerAnimCurves:
            cmds.setAttr(animCurve+'.ktv', lock=lockState)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Sep 2014 20:09:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/setkeyframe-on-a-locked-animation-layer/m-p/5299387#M14210</guid>
      <dc:creator>RFlannery1</dc:creator>
      <dc:date>2014-09-25T20:09:30Z</dc:date>
    </item>
    <item>
      <title>Re: setKeyframe on a locked animation layer?</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/setkeyframe-on-a-locked-animation-layer/m-p/10750339#M14211</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've find that this issue still happens in both &lt;STRONG&gt;Maya 2020.4&lt;/STRONG&gt; and&amp;nbsp;&lt;STRONG&gt;Maya 2022.2&lt;/STRONG&gt;. Just wanted to know if this bug will be fixed at some point as it makes working with animation layers quite cumbersome. Just to be clear, the issue only happens when trying to set a keyframe on a previously locked base layer that has been unlocked by code. With other layers this does not seem to happen.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've also wanted to report on some findings in case it helps someone else with the same problem or it helps Autodesk solve the issue:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN style="font-family: inherit;"&gt;Using the &lt;/SPAN&gt;&lt;EM&gt;MEL&lt;/EM&gt;&lt;SPAN style="font-family: inherit;"&gt;&lt;SPAN style="font-family: inherit;"&gt; equivalent command does not solve the issue:&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;mel.eval('animLayer -edit -lock 0 layerName')​​&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;SPAN&gt;Using the &lt;EM&gt;animLayer&lt;/EM&gt; parameters &lt;EM&gt;forceUIRefresh(uir)&lt;/EM&gt; or &lt;EM&gt;forceUIRebuild(fur)&lt;/EM&gt; after unlocking the layer and before setting the keyframe does not work either.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;cmds.animLayer('BaseAnimation', e=True, lock=False)
cmds.animLayer(uir=True)
cmds.animLayer(fur=True)
cmds.setKeyframe('pCube1.tx', t=10, v=5.0, al='BaseAnimation')​​​​&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Calling setKeyframe two times in a row does set the keyframe, not an ideal solution though. Also, If you execute the animLayer command and then the setKeyframe it works just fine. They need to be executed separately in the script editor one after the other, if executed together it does not work.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;cmds.animLayer('BaseAnimation', e=True, lock=False)
# Calling setKeyframe two times in a row does set the keyframe
cmds.setKeyframe('pCube1.tx', t=10, v=5.0, al='BaseAnimation')
cmds.setKeyframe('pCube1.tx', t=10, v=5.0, al='BaseAnimation')​​&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;By echoing all commands in the Script Editor you can see all the MEL code that's being called under the hood when editing the lock state of an animation layer. Using this MEL call as a way to edit the lock state of a layer solves the issue.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import maya.mel as mel

anim_layer = 'BaseAnimation'
lock_state = False

mel.eval('''animLayerLockCallBack "{0}" "{1}";
             animLayer -edit -lock {1} {0};
             updateEditorFeedbackAnimLayers("AnimLayerTab");
             onAnimLayersBaseLockChanged("AnimLayerTab");
             doUpdateTangentFeedback;'''
             .format(anim_layer, int(lock_state)))​

cmds.setKeyframe('pCube1.tx', t=10, v=5.0, al='BaseAnimation')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1267610"&gt;@RFlannery1&lt;/a&gt;&amp;nbsp;solution also works fine for me in Maya 2022.4 and 2022.2. Thanks a lot for posting the code&amp;nbsp;! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Nov 2021 15:19:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/setkeyframe-on-a-locked-animation-layer/m-p/10750339#M14211</guid>
      <dc:creator>victor.maso</dc:creator>
      <dc:date>2021-11-11T15:19:24Z</dc:date>
    </item>
  </channel>
</rss>

