Skip to content

Instantly share code, notes, and snippets.

@internetimagery
Created January 2, 2016 21:33
Show Gist options
  • Save internetimagery/9a8df2bdfc731eda518d to your computer and use it in GitHub Desktop.
Save internetimagery/9a8df2bdfc731eda518d to your computer and use it in GitHub Desktop.
Collecting keyframe times
# Testing gathering keyframe speeds
import timeit
import itertools
import collections
import maya.cmds as cmds
sel = cmds.ls(sl=True, type="transform")
if not sel: raise RuntimeError("Select something")
def slow():
data = {}
curves = cmds.keyframe(sel, q=True, n=True)
for curve in curves:
times = cmds.keyframe(curve, q=True, tc=True)
values = cmds.keyframe(curve, q=True, vc=True)
data[curve] = zip(times, values)
return data
def mid():
data = {}
curves = cmds.keyframe(sel, q=True, n=True)
for curve in curves:
keys = iter(cmds.keyframe(curve, q=True, tc=True, vc=True))
data[curve] = zip(keys, keys)
return data
def fast():
data = collections.defaultdict(list)
curves = iter(cmds.keyframe(sel, q=True, n=True) or [])
keys = iter(cmds.keyframe(sel, q=True, iv=True, tc=True, vc=True) or [])
for i, t, v in itertools.izip(keys, keys, keys):
if not i:
curve = next(curves)
data[curve].append((t,v))
return data
assert slow() == mid() == fast()
slow_time = timeit.timeit(slow, number=100)
mid_time = timeit.timeit(mid, number=100)
fast_time = timeit.timeit(fast, number=100)
print "Slow: %s" % slow_time
chunk = 100.0 / slow_time
print "Mid: %s, %s%% reducton" % (mid_time, round(100 - (mid_time * chunk), 2))
print "Fast: %s, %s%% reducton" % (fast_time, round(100 - (fast_time * chunk), 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment