0

これは私の python コードの一部です。リストに問題があり続け、定義されていないと表示されます。それを修正する方法を教えてください。

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)
4

1 に答える 1

0

関数のデフォルトパラメータを指定しています:

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

これらは関数オブジェクトの作成時に評価されますが、その時点ではまだ myListオブジェクトがありません。これらのパラメーターにデフォルト値を指定する必要はありませ。指定した値を使用しているようには見えません。

デフォルト値が必要ない場合は、次を使用します。

def changeXtransformVal(percentage, myList):

のデフォルト値がどうしても必要な場合はpercentage、パラメーターを最後に移動します。

def changeXtransformVal(myList, percentage=1.0):

それに応じて関数呼び出しを調整します。

changeXtransformVal(myList, percentage=2.0)

パラメータに名前を付ける必要もないことに注意してください。位置パラメーターは位置によって機能し、この場合percentage=もオプションです。

于 2013-11-08T18:48:32.807 に答える