2

Python を使用して Maya 内でカスタム UI を作成していますが、次の行で発生するこのエラーで立ち往生しています。

parts = button2.split(",") # NameError: global name 'button2' is not defined 

これが私のスクリプトです:

import maya.cmds as cmds

def createMyLayout():
    window = cmds.window(widthHeight=(1000, 600), title="lalala",   resizeToFitChildren=1)
    cmds.rowLayout("button1, button2, button3", numberOfColumns=5)

    cmds.columnLayout(adjustableColumn=True, columnAlign="center", rowSpacing=10)

    button2 = cmds.textFieldButtonGrp(label="LocatorCurve",
                                    text="Please key in your coordinates",
                                    changeCommand=edit_curve,
                                    buttonLabel="Execute",
                                    buttonCommand=locator_curve)
    cmds.setParent(menu=True)

    cmds.showWindow(window)

def locator_curve(*args):
    # Coordinates of the locator-shaped curve.
    crv = cmds.curve(degree=1,
                 point=[(1, 0, 0),
                        (-1, 0, 0),
                        (0, 0, 0),
                        (0, 1, 0),
                        (0, -1, 0),
                        (0, 0, 0),
                        (0, 0, 1),
                        (0, 0, -1),
                        (0, 0, 0)])


    return crv

def edit_curve(*args):
    parts = button2.split(",")
    print parts


createMyLayout()    

基本的に、私のスクリプトは、何かを行うボタンを内部に持つ UI を作成しようとしています。この場合、ユーザーが一連の座標をキー入力するテキストフィールド ボタンを作成しようとしています。ロケータ ベースのカーブは、指定された一連の座標に従って作成されます。ただし、デフォルトのカーブを作成するボタンしか作成できませんでした。人が与える座標を考慮して特定の曲線を出力するボタンを作成する方法を誰か教えてもらえますか?

4

2 に答える 2

0

「button2」などの関数間で変数を共有するには、変数を「global」として宣言する必要があります。現在、2 つの関数はそれぞれ button2 変数を個別に作成しており、互いの変数を見ることはできません。

まず、関数の外のグローバル スコープで変数を宣言します。次に、使用する関数内で再度宣言すると、通常の変数のように使用できます。

global myVar
myVar = 1

def test1():
    global myVar
    print myVar
    myVar += 1

def test2():
    global myVar
    print myVar
    myVar += 1
    print myVar

test1()
test2()

# output:
# 1
# 2
# 3

エントリ文字列のフォーマットに関しては、正しい軌道に乗っているように見えます.button2をグローバルに宣言し、各関数内にグローバルなbutton2宣言を追加したところ、ロケーター曲線がうまくいきました.

于 2011-05-04T20:10:20.610 に答える