2010-08-08 135 views
2

創建於瑪雅UI我使用python創建在Maya中自定義UI和我卡在這個錯誤是在這條線出現:使用Python腳本

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

基本上我的腳本是試圖創建一個用戶界面內的按鈕做東西。在這種情況下,我試圖創建一個用戶在一組座標中鍵入的文本字段按鈕,並根據給定的座標集創建基於定位器的曲線。但是,我只能設法創建一個創建默認曲線的按鈕。有人可以告訴我如何創建一個按鈕,考慮到一個人給出的座標並輸出特定的曲線?

+0

你要必須向我們展示錯誤,或者我們無法弄清楚出了什麼問題...... – 2010-08-08 16:45:04

+0

這是我得到的錯誤,parts = button2.split(「,」) #NameError:全球名稱'button2'沒有定義# 我想通過文本字段空間內的任何用戶密鑰,並打破它們並存儲em作爲創建曲線函數的變量。 但是,我不知道如何去做,所以我一直在隨機嘗試。我希望你們能給我一些關於如何去做的建議?謝謝! – Tom 2010-08-08 17:11:25

回答

0

要共享函數之間的變量,如「button2」,您需要聲明它爲「全局」。現在這兩個函數分別創建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聲明,我得到了你的定位曲線就好了。

+1

注意:第一個「全局myVar」(兩個函數之外)不是絕對必要的。另一方面,它是**這種聲明的良好實踐,因此即使讀者不瞭解函數的細節,閱讀者也會意識到全局。有問題,createMyLayout&edit_curve開始處的「全局按鈕2」會修復它。任何聲明之前的「全局button2」都是可選的,但很好。 – ToolmakerSteve 2013-11-28 00:16:33

3

你可以使用全局變量,但更優雅的方法是實現一個類。

下面是一個如何解決這個問題的例子。注意我們如何使用self.button2而不是僅僅使用button2。這使其成爲該類的實例變量並可在其他函數中訪問,而button2是本地變量,在離開函數時不再可訪問。

import maya.cmds as cmds 

class createMyLayoutCls(object): 
    def __init__(self, *args): 
     pass 
    def show(self): 
     self.createMyLayout() 
    def createMyLayout(self): 
     self.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) 

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

     cmds.showWindow(self.window) 

    def locator_curve(self,*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(self,*args): 
     parts = self.button2.split(",") 
     print parts 
     txt_val = cmds.textFieldButtonGrp(self.button2, q=True,text=True) 
     print txt_val 


b_cls = createMyLayoutCls() 
b_cls.show() 

而且我知道這是tooooooooo來不及回覆,你會現在肯定是在python的高手;)

希望這將有助於一些別人:)

+0

使用類而不是EVIL全局變量+1! – theodox 2013-06-22 04:48:52

+0

肯定這是更好的方式去。我會補充說,既然你有一個類,除非你打算重寫__init__方法,否則你不需要定義__init__方法。 – renderbox 2013-11-13 00:56:27

+0

@ theodox-在python中,「globals」不像**在某些語言中是**,因爲它們僅限於它們聲明的模塊。然而,我同意這個解決方案(類)更好比使用全球。 :) – ToolmakerSteve 2013-11-28 00:22:08