running a method within another method. python
I am calling a method within another. and the error for this script i am getting is
NameError: name 'mnDialog' is not defined
Is there a reason for it? I think it has something to do with executing a command which isn't on the global level. (i didn't have the impression that python has a global and local variable declaration.) What is the right syntax or the go around this? thank you for your time.
import maya.cmds as cmds
def mnProgRun():
def mnDialog(*args):
cmds.confirmDialog( title='Confirm', message='Are you sure?',button=['Yes','No'], defaultButton='Yes',cancelButton='No',dismissString='No' )
def mnMakeWin():
cmds.window( 'mnWin', title = 'testman', wh=(260,100))
cmds.columnLayout(adjustableColumn=False, columnAlign='center')
cmds.button( label="Yes,it works",al开发者_如何转开发ign='center',width=120,height=25, backgroundColor=[0.5,1,0.5],command='cmds.scriptJob( event=["SelectionChanged","mnDialog"])')
cmds.button( label="No, Thank You!",align='center',width=120,height=25, backgroundColor=[1,0.5,0.5],command='cmds.deleteUI("mnWin")')
cmds.showWindow( 'mnWin' )
mnMakeWin()
mnProgRun()
The problem is that the mnDialog
is not being looked up from mnMakeWin
, you are passing the name and it gets looked up later when you are not in the correct scope.
It may work to pass the function in instead of the name. I don't have maya installed, so I can't try it.
Otherwise you'll have to define mnDialog in the global scope which seems like an odd restriction to me
mnDialog
is a local variable in mnProgRun
. It is not accessible outside the function scope. If you want it to be, define it at the appropriate scope.
(i didn't have the impression that python has a global and local variable declaration.)
You have the wrong impression.
You should define mnDialog
at the top level. It is not in the correct namespace.
Also, it's (almost) always unnecessarily complicating to nest functions in Python.
maya always have problems with scoops, you may define mnDialog() and mnMakeWin() outside the function, at the top scoop level, its maya problem not from python, as i faced problem when calling class methods from maya ui command (ex button event).
hope that will help you :)
##edit
import maya.cmds as cmds
def mnDialog(*args):
cmds.confirmDialog( title='Confirm', message='Are you sure?',button=['Yes','No'],
defaultButton='Yes',cancelButton='No',dismissString='No' )
def mnMakeWin():
cmds.window( 'mnWin', title = 'testman', wh=(260,100))
cmds.columnLayout(adjustableColumn=False, columnAlign='center')
cmds.button( label="Yes,it works",align='center',width=120,height=25,
backgroundColor=[0.5,1,0.5],command='cmds.scriptJob( event=
["SelectionChanged","mnDialog"])')
cmds.button( label="No, Thank You!",align='center',width=120,height=25,
backgroundColor=[1,0.5,0.5],command='cmds.deleteUI("mnWin")')
cmds.showWindow( 'mnWin' )
def mnProgRun():
mnMakeWin()
#run
mnProgRun()
精彩评论