9/7/2016 - 6:44 PM

Some pymel examples for working with animation

Some pymel examples for working with animation

import pymel.core as pm

sel = pm.ls(sl=True)
attr_path = '{0}.{1}'.format(sel[0], 'ty')

key_frames = pm.keyframe(attr_path, q=True)
key_values = pm.keyframe( attr_path, q=True, vc=True)
in_tangents = pm.keyTangent( attr_path, q=True, itt=True)

Fom online docs MFnAnimCurve Class Reference

There are eight different types of Anim Curve nodes:

timeToAngular (animCurveTA)
timeToLinear (animCurveTL)
timeToTime (animCurveTT)
timeToUnitless (animCurveTU)
unitlessToAngular (animCurveUA)
unitlessToLinear (animCurveUL)
unitlessToTime (animCurveUT)
unitlessToUnitless (animCurveUU)

conn_list = pm.listConnections(sel[0])
# this type checking requires that index 0, 1 and 2 is an animation 
# curve of type translation, rotation and scale

# translate
# Result: <class 'pymel.core.nodetypes.AnimCurveTL'> #

# rotate
# Result: <class 'pymel.core.nodetypes.AnimCurveTA'> #

# scale
# Result: <class 'pymel.core.nodetypes.AnimCurveTU'> # 

# filter the list
anim_crv_list = [ x for x in conn_list if type(x) == pm.nodetypes.AnimCurveTL]

# set the value of the first keyframe to 4            
anim_crv_list[0].setValue(0, 4)


import pymel.core as pm

# get the selected anim crv
anim_crv = pm.ls(sl=True)[0]

num_keys = anim_crv.numKeyframes()

# print the values of the keys
for k in range(num_keys):
    print('value at index {0} is: {1}'.format(k, anim_crv.getValue(k)))

# set a key, index, value
anim_crv.setValue(1, 5)