Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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 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 start
        pm.setKeyframe(obj, v=0, attribute=attr, t=[kf[0]])
        #set transition
        pm.setKeyframe(obj, v=0.25, attribute=attr, t=[kf[1]])
        #set flat keys
        pm.setKeyframe(obj, v=0.5, attribute=attr,
            t=[kf[2], kf[3], kf[4], kf[5], kf[6]])
        #set transition
        pm.setKeyframe(obj, v=0.75, attribute=attr, t=[kf[7]])
        #set transition
        pm.setKeyframe(obj, v=1, attribute=attr, t=[kf[8]])
        #set min keyframe
        pm.keyTangent(obj, at=attr, itt='linear', ott='linear')


def createRamp(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:
        keyValue = 0
        for keyframe in kf:
            #set max keyframe
            pm.setKeyframe(obj, v=keyValue, attribute=attr, t=[keyframe])
            keyValue += 0.2
        pm.keyTangent(obj, at=attr, itt='linear', ott='linear')


def createBridge(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 max keyframe
        pm.setKeyframe(obj, v=1, attribute=attr, t=[kf[4], kf[5]])
        #set min keyframe
        pm.setKeyframe(obj, v=0, attribute=attr,
            t=[kf[0], kf[1], kf[2], kf[3], kf[6], kf[7], kf[8]])
        pm.keyTangent(obj, at=attr, itt='flat', ott='flat')

#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)
pm.formLayout(
    form, edit=True, attachForm=(
        (tabs, 'top', 0),
        (tabs, 'left', 0),
        (tabs, 'bottom', 0),
        (tabs, 'right', 0)
        )
    )

child1 = pm.columnLayout()
for attr in attrList:
    pm.button(label=attr, command='createSin("%s")' % attr)
pm.setParent('..')

child2 = pm.columnLayout()
for attr in attrList:
    pm.button(label=attr, command='createStep("%s")' % attr)
pm.setParent('..')

child3 = pm.columnLayout()
for attr in attrList:
    pm.button(label=attr, command='createBridge("%s")' % attr)
pm.setParent('..')

child4 = pm.columnLayout()
for attr in attrList:
    pm.button(label=attr, command='createBump("%s")' % attr)
pm.setParent('..')

child5 = pm.columnLayout()
for attr in attrList:
    pm.button(label=attr, command='createRamp("%s")' % attr)
pm.setParent('..')

pm.tabLayout(tabs, edit=True,
    tabLabel=(
        (child1, 'Sin'),
        (child2, 'Step'),
        (child3, 'Bridge'),
        (child4, 'Bump'),
        (child5, 'Ramp')
        )
    )

pm.showWindow()

This is a script which is stored as a shelf button in Maya, not a script fired from a shell.

share|improve this question
    
@NilsDiefenbach I suggest you request a merge of your two accounts. –  200_success Nov 13 '14 at 6:55

1 Answer 1

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.