curve copy work
import pymel.core as pm
import collections
import anim_util as au
reload(au)
class AnimCurve:
"""Stores all data of an animation curve for copying/rebuilding"""
def __init__(self, curve):
self.name = curve
# check for anim layers
if au.anim_layers_exist():
attr_index = -3
else:
attr_index = -1
self.attribute = curve.split('_')[attr_index]
self.key_times = pm.keyframe(self.name, query=True, timeChange=True)
self.key_values = pm.keyframe(self.name, query=True, valueChange=True)
self._tangent_opts = [
'lock',
'weightLock',
'inTangentType',
'inAngle',
'inWeight',
'outTangentType',
'outAngle',
'outWeight'
]
self.tangent_attrs = collections.OrderedDict() # order that tangent data is added matters
self._get_tangent_values()
def _get_tangent_values(self):
"""Iterates through the tangent values and populates the dict with them"""
for option in self._tangent_opts:
arg = {option: True}
self.tangent_attrs[option] = pm.keyTangent(self.name, q=True, **arg)
def paste_curve(self, destination, destination_attribute=None):
"""Paste the stored curve elsewhere.
Attributes:
destination: the object where the curve should be pasted
destination_attribute: (str) define an attribute here if you want the curve pasted on a different attribute
than it came from e.g. 'tx', 'rotateZ'
"""
if destination_attribute is None:
destination_attribute = self.attribute
# delete any existing animation from the destination curve as it will affect the curve rebuild
pm.cutKey(destination, attribute=destination_attribute, clear=True)
# copy core keyframe data - time/value
for frame, value in zip(self.key_times, self.key_values):
pm.setKeyframe(destination, attribute=destination_attribute, time=(frame,), v=value)
# add in tangent data
for attr in self.tangent_attrs:
for i, val in enumerate(self.tangent_attrs[attr]):
arg = {attr: val}
pm.keyTangent(destination, attribute=destination_attribute, e=True, absolute=True, index=(i,), **arg)
def __str__(self):
return '\nname: {}\nattribute: {}\nkey times: {}\nkey values: {}\ntangent data: {}\n'.format(self.name,
self.attribute,
self.key_times,
self.key_values,
self.tangent_attrs)