Maya Python: 从内部函数访问外部函数变量
Maya Python: Accessing outer function variable from inner function
我想用python写一个UIwindow,下面是我的代码,函数是运行正确的,但是有个问题,当我在 textScrollList 中选择一个项目,它应该调用内部函数 'update()' 并突出显示场景中的相应对象。
但是,无法正确选择对象并显示如下错误消息:
"Object 'alertWindow|formLayout164|textScrollList27' not found."
我认为发生这种情况是因为内部函数 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))
您可以尝试在代码顶部使用全局变量 "global tsl"。这不是最漂亮的,但它有效:)
你需要先定义你的内部函数,然后引用它们,不需要为 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'])
您也可以使用 class 来保持事物的状态。
我想用python写一个UIwindow,下面是我的代码,函数是运行正确的,但是有个问题,当我在 textScrollList 中选择一个项目,它应该调用内部函数 'update()' 并突出显示场景中的相应对象。 但是,无法正确选择对象并显示如下错误消息:
"Object 'alertWindow|formLayout164|textScrollList27' not found."
我认为发生这种情况是因为内部函数 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))
您可以尝试在代码顶部使用全局变量 "global tsl"。这不是最漂亮的,但它有效:)
你需要先定义你的内部函数,然后引用它们,不需要为 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'])
您也可以使用 class 来保持事物的状态。