HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

PyMel / Python script for setting animation keys

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scriptanimationpymelkeyssettingforpython

Problem

I'm currently preparing some basic code samples for a trainee position. I've formatted some of my older tools that I had previously written, but feel confident about. I would very much appreciate a pair of experienced eyes to look over the code and its formatting.

It's written originally in Python 2.7, but I formatted it to work with Python 3.0.

It's a script for usage with Maya, which sets Keys for object animations in certain patterns.

```
import pymel.core as pm

def createSin(attr):
#list of keyframes to be set:
kf = ['1', '4', '7', '10', '13']
#add all selected objects to a list
objList = pm.ls(selection=1)
for obj in objList:
# set max keyframe
pm.setKeyframe(obj, v=1, attribute=attr, t=[kf[1]])
# set min keyframe
pm.setKeyframe(obj, v=-1, attribute=attr, t=[kf[3]])
# set intersection keyframe
pm.setKeyframe(obj, v=0, attribute=attr, t=[kf[0], kf[2], kf[4]])
# Set the tangents for the given attribute to 'Spline'
pm.keyTangent(obj, at=attr, itt='spline', ott='spline')

def createBump(attr):
#list of keyframes to be set:
kf = ['1', '4', '7', '10', '13', '16', '19', '22', '25']
#add all selected objects to a list
objList = pm.ls(selection=1)
for obj in objList:
#Set the Max KeyFrame value
pm.setKeyframe(obj, v=1, attribute=attr, t=[kf[4]])
#Set Transistion Key 1
pm.setKeyframe(obj, v=.5, attribute=attr, t=[kf[5]])
#Set x Intersection Keys
pm.setKeyframe(obj, v=0, at=attr,
t=[kf[0], kf[1], kf[2], kf[3], kf[6], kf[7], kf[8]])
#Modify tangents to smooth out the curve
pm.keyTangent(obj, at=attr, itt='spline', ott='spline', t=[kf[5]])
pm.keyTangent(obj, at=attr, itt='flat', ott='flat', t=[kf[4]])
pm.keyTangent(obj, at=attr, itt='flat', ott='spline', t=[kf[3]])
pm.keyTangent(obj, at=attr, itt='spline', ott='flat', t=[kf[6]])

def createStep(attr):
#list of

Solution

You should not have code in the global namespace.
If you want to run this as a shell script,
then the first line should be:

#!/usr/bin/env python


Move the code that's currently in the global namespace inside a main() method,
like this:

def main():
    # create window for tools
    attrList = ['translateX', 'translateY', 'translateZ',
                'rotateX', 'rotateY', 'rotateZ']
    window = pm.window(widthHeight=(250, 200), title='AnimCurve Toolbox')
    form = pm.formLayout()
    tabs = pm.tabLayout(innerMarginWidth=5, innerMarginHeight=5)

    # ... and so on ...


And then call it like this:

if __name__ == '__main__':
    main()


Formatting

If you care about formatting,
then you should follow PEP8.

The most obvious violations I see:

-
Use snake_case instead of camelCase for method and variable names

-
Put a space after a # in comments, # something instead of #something

Code Snippets

#!/usr/bin/env python
def main():
    # create window for tools
    attrList = ['translateX', 'translateY', 'translateZ',
                'rotateX', 'rotateY', 'rotateZ']
    window = pm.window(widthHeight=(250, 200), title='AnimCurve Toolbox')
    form = pm.formLayout()
    tabs = pm.tabLayout(innerMarginWidth=5, innerMarginHeight=5)

    # ... and so on ...
if __name__ == '__main__':
    main()

Context

StackExchange Code Review Q#69634, answer score: 3

Revisions (0)

No revisions yet.