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

Import and export of animation keys in Maya

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

Problem

I have programming this importing and exporting of animation keys. It is working, but I would like to gather any feedback/advice as I am still pretty much a noob in coding.

```
import maya.cmds as cmds
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class mainWindow(QDialog):
def __init__(self, parent=None):
super(mainWindow, self).__init__(parent)
self.resize(300,200)
self.initUI()
self.createConnections()

def initUI(self):
self.importLbl = QLabel('Import Directory')
self.importTxt = QLineEdit()
self.openAnimBtn = QToolButton()
self.importAnimBtn = QPushButton('Import ANIM')

self.exportLbl = QLabel('Export Directory')
self.exportTxt = QLineEdit()
self.setAnimBtn = QToolButton()
self.exportAnimBtn = QPushButton('Export ANIM')

self.exportCombo = QComboBox()
self.exportCombo.addItem('Use keyframe range from Time Slider')
self.exportCombo.addItem('Use keyframe range from selection only')

self.separator = QFrame()
self.separator.setFrameShape(QFrame.HLine)
self.separator.setFrameShadow(QFrame.Sunken)

gridLayout = QGridLayout()

gridLayout.addWidget(self.importLbl, 0, 1, 1, 2)
gridLayout.addWidget(self.importTxt, 1, 1, 1, 1)
gridLayout.addWidget(self.openAnimBtn, 1, 2, 1, 1)
gridLayout.addWidget(self.importAnimBtn, 2, 1, 1, 2)

gridLayout.addWidget(self.separator, 3, 1, 1, 2)

gridLayout.addWidget(self.exportLbl, 4, 1, 1, 2)
gridLayout.addWidget(self.exportTxt, 5, 1, 1, 1)
gridLayout.addWidget(self.setAnimBtn, 5, 2, 1, 1)

gridLayout.addWidget(self.exportCombo, 6, 1)

gridLayout.addWidget(self.exportAnimBtn, 7, 1, 1, 2)

self.setLayout(gridLayout)
self.setWindowTitle("IMPORT / EXPORT ANIMATION KEYFRAMES")

def createConnections(self):
# Connections for Import
self.connect(self.openAnim

Solution

Avoid wildcard imports

Don't use wildcard imports like this:

from PyQt4.QtCore import *
from PyQt4.QtGui import *


Quoting from PEP8:


Wildcard imports (from import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.

I guess you do this because you're using many classes from these modules.
A better way would be to do like this:

import PyQt4.QtCore as core
import PyQt4.QtGui as gui


This way you will have to prefix all the classes you used with core. or gui. appropriately, but it's better because it will be clear where everything comes from,
and automated tools can warn you when you try to use a name that doesn't exist.

Avoid using global

The abuse of the global keyword is especially clear in the exportFunc and comboSel methods:

def exportFunc(self):
    """ Function of the ExportAnim button """

    self.comboSel()
    self.exportAnim(startTime, endTime)


This method makes use of startTime and endTime globals which are set in comboSel. You should rewrite this to make comboSel return a tuple, and use that instead of globals:

def exportFunc(self):
    """ Function of the ExportAnim button """

    startTime, endTime = self.comboSel()
    self.exportAnim(startTime, endTime)


Then in comboSel:

def comboSel(self):
    # no no !
    #global startTime
    #global endTime

    # ...

    return startTime, endTime


Review all the uses of global in the rest of the code and try to eliminate all.

Code Snippets

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import PyQt4.QtCore as core
import PyQt4.QtGui as gui
def exportFunc(self):
    """ Function of the ExportAnim button """

    self.comboSel()
    self.exportAnim(startTime, endTime)
def exportFunc(self):
    """ Function of the ExportAnim button """

    startTime, endTime = self.comboSel()
    self.exportAnim(startTime, endTime)
def comboSel(self):
    # no no !
    #global startTime
    #global endTime

    # ...

    return startTime, endTime

Context

StackExchange Code Review Q#69476, answer score: 2

Revisions (0)

No revisions yet.