Maya Python:从内部函数访问外部函数变量

Maya Python:从内部函数访问外部函数变量

问题描述:

我想用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)) 

您需要先定义你的内部函数,然后只是引用它们,无需使用一个字符串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

您可以尝试使用代码顶部的全局变量“global tsl”。这不是然而最美丽的,它的工作原理:)