Find last keyframe for scene with animation layers

Find last keyframe for scene with animation layers

RFlannery1
Collaborator Collaborator
2,744 Views
1 Reply
Message 1 of 2

Find last keyframe for scene with animation layers

RFlannery1
Collaborator
Collaborator

I am trying to find the last keyframe in a scene doing something like the following:

allXforms = cmds.ls(transforms=True)
last = cmds.findKeyframe(allXforms, which='last')

However, I came across a scene where it doesn't work.  I reproduced a simple example as follows:

  • The scene has multiple animation layers.
  • The base animation layer has keys from 1-30.
  • The other animation layer has keys from 40-60.
  • Animation curves "post infinity" is set to cycle.

When the scene is set up like this, the "findKeyframe" command works on only the actively selected animation layer.  So depending on what I have selected, sometimes it returns 30 and sometimes it returns 60.

 

Question: Is there some way to find the actual last keyframe, taking into account all layers?

 

Right now I am thinking of finding the currently selected anim layer(s), saving that selection, selecting all the layers, running the "findKeyframe" command, and then restoring the anim layer selection.  However, if someone has a suggestion where I don't have to change the selection, that would be much appreciated.

 

(Attached the simple Maya example scene.  But it wouldn't let me attach a .ma file.  So I attached it as a .txt file.)

0 Likes
Accepted solutions (1)
2,745 Views
1 Reply
Reply (1)
Message 2 of 2

RFlannery1
Collaborator
Collaborator
Accepted solution

Okay, I figured it out.  If you want to find the last keyframe in the entire scene, you can do this:

animCurves = cmds.ls(type='animCurve')
last = cmds.findKeyframe(animCurves, which='last')

If you want to limit the search to specific objects, you can do this:

objs = cmds.ls(sl=True)
historyNodes = cmds.listHistory(objs, pruneDagObjects=True, leaf=False)
animCurves = cmds.ls(historyNodes, type='animCurve')
last = cmds.findKeyframe(animCurves, which='last')