2016-07-28 280 views
0

我想用python編寫一個UI窗口,下面是我的代碼,函數運行正常,但出現問題,當我在textScrollList中選擇一個項時,它應該調用內部函數'update()'並突出顯示場景中的相應對象。 然而,對象不能被正確地選擇,它顯示了這樣的錯誤消息:Maya Python:從內部函數訪問外部函數變量

「對象‘alertWindow | formLayout164 | textScrollList27’未找到」。

我認爲這是因爲內部函數update()無法訪問外部函數中的變量tsl,沒有人知道我可以如何修改我的代碼?

def alertWindow(): 
    if(cmds.window('mainWindow', q =True, exists = True,)): 
     cmds.deleteUI('mainWindow') 
    UI = cmds.window('mainWindow', title = 'Alert!', maximizeButton = False, minimizeButton = False, resizeToFitChildren = True, widthHeight = (250, 300), sizeable = False) 
    myForm=cmds.formLayout() 

    txt = cmds.text(label = 'Please check the following objects :') 
    tsl = cmds.textScrollList(width = 200, height = 200, enable = True, allowMultiSelection = True, selectCommand = 'update()') 

    count = len(obj) 
    for i in range(count): 
     cmds.textScrollList(tsl, edit=True, append = obj[i]) 

    delete = cmds.button(label = 'delete', width = 100, command = 'remove()') 
    clz = cmds.button(label = 'Close', width = 100, command = 'cmds.deleteUI("mainWindow")') 

    cmds.formLayout(myForm, edit = True, attachForm = [(txt, 'top', 20),(txt, 'left', 65),(tsl, 'left', 25),(tsl, 'top', 50),(delete,'bottom',10),(delete,'left',15),(clz,'bottom',10),(clz,'right',20)]) 
    cmds.showWindow(UI) 

    def update(): 
     cmds.select(cmds.textScrollList(tsl, q=True, selectItem = True)) 

    def remove(): 
     cmds.DeleteHistory() 
     cmds.textScrollList(tsl, edit=True, removeItem = cmds.ls(sl=True)) 

回答

0

您需要先定義你的內部函數,然後只是引用它們,無需使用一個字符串command=

def alertWindow(obj): 
    def update(): 
     cmds.select(cmds.textScrollList(tsl, q=True, selectItem = True)) 

    def remove(): 
     cmds.DeleteHistory() 
     cmds.textScrollList(tsl, edit=True, removeItem = cmds.ls(sl=True)) 

    if(cmds.window('mainWindow', q =True, exists = True,)): 
     cmds.deleteUI('mainWindow') 
    UI = cmds.window('mainWindow', title = 'Alert!', maximizeButton = False, minimizeButton = False, resizeToFitChildren = True, widthHeight = (250, 300), sizeable = False) 
    myForm=cmds.formLayout() 

    txt = cmds.text(label = 'Please check the following objects :') 
    tsl = cmds.textScrollList(width = 200, height = 200, enable = True, allowMultiSelection = True, selectCommand = update) 

    count = len(obj) 
    for i in range(count): 
     cmds.textScrollList(tsl, edit=True, append = obj[i]) 

    delete = cmds.button(label = 'delete', width = 100, command = remove) 
    clz = cmds.button(label = 'Close', width = 100, command = 'cmds.deleteUI("mainWindow")') 

    cmds.formLayout(myForm, edit = True, attachForm = [(txt, 'top', 20),(txt, 'left', 65),(tsl, 'left', 25),(tsl, 'top', 50),(delete,'bottom',10),(delete,'left',15),(clz,'bottom',10),(clz,'right',20)]) 
    cmds.showWindow(UI) 




cmds.polyCube() 
alertWindow(['pCube1']) 

你也可以使用一個類來保持事物的狀態。

+0

它的工作原理!感謝您的幫助:) – Kenzie

+0

嗨〜該項目現在可以正確突出顯示,但是當我按'刪除'按鈕時,它會顯示另一個錯誤消息「remove()不帶任何參數(給出1)」。 (在我修改代碼之前,可以正確刪除該對象。) – Kenzie

+0

@Kenzie:您可以將'* args'添加到remove函數並查看它傳入的內容。可能是按鈕?看起來你正在處理沒有任何輸入的刪除。 – Brett

0

您可以嘗試使用代碼頂部的全局變量「global tsl」。這不是然而最美麗的,它的工作原理:)

相關問題