Question

This is part of my python code. I keep having a problem with my lists and it says it is not defined. Can you help or explain to me how to fix that?

import maya.cmds as cmds
def changeXtransformVal(percentage=1.0, myList = myList):

    """
    Changes the value of each transform in the scene by a percentange.
    Parameters:
    percentage - Percentange to change each transform's value. Default value is 1.
    Returns:
    Nothing.
    """
    # The ls command is the list command. It is used to list various nodes
    # in the current scene. You can also use it to list selected nodes.
    transformInScene = cmds.ls(type='transform')
    found = False
    for thisTransform in transformInScene:
        if thisTransform not in ['front','persp','side','top']:
            found = True
            break
        else:
             found = False
    if found == False:
           sphere1 = cmds.polySphere()[0]
           cmds.xform(sphere1, t = (0.5, 0.5, 0.5))
    transformInScene = cmds.ls(type='transform')
    # If there are no transforms in the scene, there is no point running this script
    if not transformInScene:
          raise RuntimeError, 'There are no transforms in the scene!'
    badAttrs = list('front','persp','side','top')
    # Loop through each transform
    for thisTransform in transformInScene:
          if thisTransform not in ['front','persp','side','top']:
              allAttrs = cmds.listAttr(thisTransform, keyable=True, scalar=True)
          allAttrs = [ i for i in badAttrs if i != "visibility" ]
          print allAttrs     
    for attr in myList:
               if attr in allAttrs:
                   currentVal = cmds.getAttr( thisTransform + "." + attr )
                   newVal = currentVal * percentage
                   cmds.setAttr(thisTransform + "." + attr, newval)
                   print "Changed %s. %s from %s to %s" % (thisTransform,attr,currentVal,newVal)
               else:
                   badAttrs.append(attr)

    if badAttrs:
        print "These attributes %s are not valid" % str()

myList = ("translateX", "translateY", "translateZ", "scaleX" )
changeXtransformVal(percentage=2.0, myList = myList)
Was it helpful?

Solution

You are giving your function default parameters:

def changeXtransformVal(percentage=1.0, myList = myList):

These are evaluated when the function object is created, but at that time you have no myList object yet. You don't have to give these parameters default values, and you don't appear to use the values you do give.

If you don't need default values, use:

def changeXtransformVal(percentage, myList):

If you do need a default value for percentage, move the parameter to the end:

def changeXtransformVal(myList, percentage=1.0):

and adjust your function call accordingly:

changeXtransformVal(myList, percentage=2.0)

Note that you don't have to name parameters either; positional parameters work by position, and in this case percentage= is optional too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top