views:

230

answers:

4

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",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' )
    mnMakeWin()
mnProgRun()


+1  A: 

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.

recursive
thanks for the reply, i m learning something new all the time
marcus
+1  A: 

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.

Peter
there is a reason for nesting it within the big method. so that maya can call and source it as one big method when it starts up, defining outside would cause it to not source it in the 1st place. would there be a go around for this?
marcus
+2  A: 

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

gnibbler
thanks for the reply, can you give a small simple example of passing a function instead of a name, maybe with a print "hello" example.
marcus
A: 

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()

Ahmad Dwaik
is there a way to still execute the python script as i am sourcing it in the usersetup.mel.
marcus
yes..as said move your functions to the top scoop level (module level)then try to source them, like so on my edit
Ahmad Dwaik