Maya Python:从内部函数

时间:2016-07-28 07:58:20

标签: python maya

我想用python编写一个UI窗口,以下是我的代码,函数运行正常,但是有一个问题,当我在textScrollList中选择一个项目时,它应该调用内部函数'更新()'并突出显示场景中的相应对象。 但是,无法正确选择对象,并显示如下错误消息:

  

"对象' 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))

2 个答案:

答案 0 :(得分:0)

您可以尝试在代码顶部使用全局变量“global tsl”。这不是最美丽的,但是有效:)

答案 1 :(得分: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'])

你也可以使用一个类来保持事物的状态。