Skip to content

Instantly share code, notes, and snippets.

@patwooky
Created August 22, 2017 05:27
Show Gist options
  • Save patwooky/bf85adc7f84bb281b0afa90898c68fca to your computer and use it in GitHub Desktop.
Save patwooky/bf85adc7f84bb281b0afa90898c68fca to your computer and use it in GitHub Desktop.
ReSpeed keyframe Timings
'''
Problem:
keyframes from camera tx, ty, tz, rx, ry, rz ranging from 1001 to 1195
need to have their timing remapped, such that the new timing is sped up by 2x
thus, the formula where kT = original frame, is ((kT - 1001) * 0.5) + 1001
This will give us new keyframes from frames 1001 - 1098.
New frame range after re-speed is 1001 - 1093, we just truncate frames 1094 - 1098.
'''
from pymel.core import *
kfList = listConnections('VFX_0081_8220_Cam', plugs=False, type='animCurve')
for kf in kfList:
print ('kf is {}'.format(kf))
# keyframe(kf, q=True, a=True, tc=True, vc=True) gets a list of key time/value tuples.
for idx, k in enumerate(keyframe(kf, q=True, a=True, tc=True, vc=True)):
print 'idx is {}, keyframe is {}'.format(idx, repr(k))
kTime = k[0]
kTimeNew = ((kTime - 1001) * 0.5) + 1001
print 'new time is {}'.format(kTimeNew)
keyframe(kf, e=True, index=idx, a=True, tc=kTimeNew)
print keyframe(kf, q=True, index=idx, tc=True)
@BigRoy
Copy link

BigRoy commented Jan 18, 2021

Note that there's also the scaleKeys command which also takes into account weighted curves, tangents, etc.

Also, to query keys of an object you can also use the maya.cmds.keys command to query to avoid the need for listConnections. Note that listConnections defaults to also listing out connections (outputs) and thus your code could also "shift keys" for those that are related to driven keyframes (Set Driven Keyframe).

Anyway, good to see these gists public! Keep on sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment