Maya Python: Accessing outer function variable from inner function -
i want write ui window using python, following code, function running correctly, there problem, when choose item in textscrolllist, should call inner function 'update()' , highlight corresponding object in scene. however, object cannot chosen correctly , shows error message this:
"object 'alertwindow|formlayout164|textscrolllist27' not found."
i think happens because inner function update() cannot access variable tsl in outer function, know how can revise code?
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 following objects :') tsl = cmds.textscrolllist(width = 200, height = 200, enable = true, allowmultiselection = true, selectcommand = 'update()') count = len(obj) 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))
you need define inner functions first, reference them, no need use string 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 following objects :') tsl = cmds.textscrolllist(width = 200, height = 200, enable = true, allowmultiselection = true, selectcommand = update) count = len(obj) 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'])
you use class keep state of things.
Comments
Post a Comment