Skip to content

Instantly share code, notes, and snippets.

@splinecraft
Last active September 30, 2017 20:19
Show Gist options
  • Save splinecraft/e722587b7264ae320c55d039eca350da to your computer and use it in GitHub Desktop.
Save splinecraft/e722587b7264ae320c55d039eca350da to your computer and use it in GitHub Desktop.
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
type(conn_list[0])
# translate
# Result: <class 'pymel.core.nodetypes.AnimCurveTL'> #
type(conn_list[1])
# rotate
# Result: <class 'pymel.core.nodetypes.AnimCurveTA'> #
type(conn_list[2])
# 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)
print(anim_crv_list[0].getValue(0))
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment