Python for maya 2012 not evaluating correctly

I am having a problem getting python for maya to evaluate properly, for simplicity sake I have reduced the files and named them to make it easier to see what is going on. I have actually run this code and gotten the same error. but on some modules it runs fine the code is in two files and looks like this


#location of file
/rigAmaJig/test.py

def functionTwo():
    print " inside function Two!!!"

def functionOne():
   functionTwo()


/rmjTest.py

import module.test as mt
print dir(mt)

mt.functionTwo()
print "now run function One"
mt.functionOne()

the output looks like this


['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'functionTwo', 'functionOne']

inside function Two!!!
now run function One

# Error: name 'functionTwo' is not defined
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# NameError: name 'functionTwo' is not defined # 

That’s it, but the function is quite clearly defined and in the directory for that module. I can even call it by itself. but not from the sub function. sometimes if I re-write the file from scratch the problem will go away. but there has to be a solid solution that I am missing. Any help here would be greatly appreciated.

I think you may need to provide an actual set of files that exhibit this behavior. Without whitespace etc., it is pretty hard to see what is going on.

No problem, I forgot to use the code blocks.

both programs get the same error. but I have written a few dozen other programs using this same format and they all run great.


def rmIconify( name, icoType, icoName, icoParn, icoOrient ):
    # first the dependencies
    import pymel.core as pm
    import rigAmaJig.rmPuppetTools as pt

    pt.rmAddIcon( name, rmIconType, icoType, icoParn, icoOrient )

def rmIconUI():
    # first the dependencies
    import pymel.core as pm

    rmActiveRmp = pm.ls(selection = True)
    rmIconName = rmActiveRmp[0].split('_')
    pm.melGlobals.initVar( 'string','grmjIconName')
    pm.melGlobals['grmjIconName'] = rmIconName[1]
    pm.melGlobals.initVar( 'string','grmjIconParn')
    pm.melGlobals['grmjIconParn'] = "Master"
    rmIconType = 'Circle'
    rmIcoOrient = True
    rmMakeNow = True

    # check if window exists and delete old window if it does
    if pm.mel.eval('window -ex "rmjIconWin" ;'):
        pm.mel.eval('deleteUI "rmjIconWin";' )
    # create a new window using mel.eval becuase pymel doesn't let you name a window
    rmjWin = pm.mel.eval('window -title "Rig-A-Ma-Jig Icon Tool" -width 100 -height 200 rmjIconWin;')

    pm.rowColumnLayout( )
    pm.frameLayout( label = "Build Icon", borderStyle='in', collapsable=True )

    pm.text( label = "Icon Type:" )

    pm.rowColumnLayout()
    pm.checkBox( label = 'Orient icon axis to joint:', align = 'center', onc = 'rmIcoOrient = True', ofc = 'rmIcoOrient = False', v = True )
    pm.radioCollection()
    pm.radioButton( label='Circle', align = 'right', changeCommand = 'rmIconType = "Circle"', select = True )
    pm.radioButton( label='Box', align = 'center', changeCommand = 'rmIconType = "Box"' )
    pm.radioButton( label='Foot', align = 'left', changeCommand = 'rmIconType = "Foot"' )
    pm.radioButton( label='Compass', align = 'right', changeCommand = 'rmIconType = "Compass"' )
    pm.radioButton( label='Compass Box', align = 'center', changeCommand = 'rmIconType = "Circle"' )
    pm.radioButton( label='Finger Control', align = 'left', changeCommand = 'rmIconType = "Circle"' )
    pm.setParent( '..' )
    # need to put in a pulldown list for this guy
    #pm.mel.eval( 'textField -text $grmjRigName -changeCommand "$grmjRigName = `textField -query -text rigShortName`" rigShortName;' )
    pm.text( label = "Icon Name:" )
    pm.mel.eval( 'textField -text $grmjIconName -changeCommand "$grmjIconName = `textField -query -text rigIconName`" rigIconName;' )
    # I want to have an easy select button.
    pm.text( label = "Icon Parent:" )
    pm.mel.eval( 'textField -text $grmjIconParn -changeCommand "$grmjIconParn = `textField -query -text rigIconParn`" rigIconParn;' )

    pm.button( label = "Add Icon", command = 'rmIconify( "select" , rmIconType, pm.melGlobals.get("grmjIconName"), pm.melGlobals.get("grmjIconParn"), rmIcoOrient )')
    pm.setParent( '..' )

    pm.showWindow()


when I click on Add Icon the error code is


# Error: name 'rmIconify' is not defined
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# NameError: name 'rmIconify' is not defined # 

this is the fourth file this has happened on.

Try to use this construction to call functions


pm.button( label = "Add Icon", command = lambda *x:rmIconify( "select" , rmIconType, pm.melGlobals.get("grmjIconName"), pm.melGlobals.get("grmjIconParn"), rmIcoOrient ))

Thanks! here is what I got from that.


# Error: Error executing callback <function <lambda> at 0x0000000016A19F98> - <lambda> - module rigAmaJig.rmjIconic - C:/Users/cargoyle/Documents/maya/scripts\rigAmaJig\rmjIconic.py, line 62
# 
# Original message:
# Traceback (most recent call last):
#   File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\factories.py", line 742, in callback
#     res = origCallback( *newargs )
#   File "C:/Users/cargoyle/Documents/maya/scripts\rigAmaJig\rmjIconic.py", line 62, in <lambda>
#     pm.button( label = "Add Icon", command = lambda *x:rmIconify( "select" , rmIconType, pm.melGlobals.get("grmjIconName"), pm.melGlobals.get("grmjIconParn"), rmIcoOrient ))
#   File "C:/Users/cargoyle/Documents/maya/scripts\rigAmaJig\rmjIconic.py", line 17, in rmIconify
#     pt.rmAddIcon( name, rmIconType, icoType, icoParn, icoOrient )
# NameError: global name 'rmIconType' is not defined
# Traceback (most recent call last):
#   File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\factories.py", line 744, in callback
#     raise CallbackError(origCallback, e)
# CallbackError: Error executing callback <function <lambda> at 0x0000000016A19F98> - <lambda> - module rigAmaJig.rmjIconic - C:/Users/cargoyle/Documents/maya/scripts\rigAmaJig\rmjIconic.py, line 62
# 
# Original message:
# Traceback (most recent call last):
#   File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\factories.py", line 742, in callback
#     res = origCallback( *newargs )
#   File "C:/Users/cargoyle/Documents/maya/scripts\rigAmaJig\rmjIconic.py", line 62, in <lambda>
#     pm.button( label = "Add Icon", command = lambda *x:rmIconify( "select" , rmIconType, pm.melGlobals.get("grmjIconName"), pm.melGlobals.get("grmjIconParn"), rmIcoOrient ))
#   File "C:/Users/cargoyle/Documents/maya/scripts\rigAmaJig\rmjIconic.py", line 17, in rmIconify
#     pt.rmAddIcon( name, rmIconType, icoType, icoParn, icoOrient )
# NameError: global name 'rmIconType' is not defined # 

yes, this because your

 rmIconType = 'Circle'

in rmIconUI is not global. You can set it as global or remake args in rmIconify()

I’d make like this:


class uiControls(object):
    """ This class will help us to store ui controls ids for getting controls values """
    def __init__(self):
        self.rbcIconType = 0
        self.iconName = {'Circle':'Circle',
                         'Box':'Box',
                         'Foot':'Foot',
                         'Compass':'Compass',
                         'Compass Box':'Circle',
                         'Finger Control':'Circle'}

# create global object for ui controls
ui = uiControls()

def rmIconify( name, icoName, icoParn, icoOrient ):
        
    # first the dependencies
    import pymel.core as pm
    import rigAmaJig.rmPuppetTools as pt
    
    #!!! get selected radioButton control name
    rbSelected = pm.radioCollection(ui.rbcIconType, q=1, select=1 )
    
    #!!! get label of this control
    rmIconType = pm.radioButton( rbSelected, q=1, label=1 )
    
    #!!! get actual control name by the dict. key from ui.iconName
    icoType = ui.iconName.get(rmIconType)
    print(icoType) # for test 

    pt.rmAddIcon( name, rmIconType, icoType, icoParn, icoOrient )

def rmIconUI():
    
    # first the dependencies
    import pymel.core as pm

    rmActiveRmp = pm.ls(selection = True)
    rmIconName = rmActiveRmp[0].split('_')
    pm.melGlobals.initVar( 'string','grmjIconName')
    pm.melGlobals['grmjIconName'] = rmIconName[1]
    pm.melGlobals.initVar( 'string','grmjIconParn')
    pm.melGlobals['grmjIconParn'] = "Master"

    #!!! don't need this guy anymore
    #rmIconType = 'Circle'
    
    rmIcoOrient = True
    rmMakeNow = True

    # check if window exists and delete old window if it does
    if pm.mel.eval('window -ex "rmjIconWin" ;'):
        pm.mel.eval('deleteUI "rmjIconWin";' )
    # create a new window using mel.eval becuase pymel doesn't let you name a window
    rmjWin = pm.mel.eval('window -title "Rig-A-Ma-Jig Icon Tool" -width 100 -height 200 rmjIconWin;')

    pm.rowColumnLayout( )
    pm.frameLayout( label = "Build Icon", borderStyle='in', collapsable=True )

    pm.text( label = "Icon Type:" )

    pm.rowColumnLayout()
    pm.checkBox( label = 'Orient icon axis to joint:', align = 'center', onc = 'rmIcoOrient = True', ofc = 'rmIcoOrient = False', v = True )
    
    #!!! we have to save this control's id
    ui.rbcIconType = pm.radioCollection()
    
    #!!! don't need callbacks for radioButtons
    pm.radioButton( label='Circle', align = 'right', select = True )
    pm.radioButton( label='Box', align = 'center' )
    pm.radioButton( label='Foot', align = 'left' )
    pm.radioButton( label='Compass', align = 'right' )
    pm.radioButton( label='Compass Box', align = 'center' )
    pm.radioButton( label='Finger Control', align = 'left' )
    pm.setParent( '..' )
    # need to put in a pulldown list for this guy
    #pm.mel.eval( 'textField -text $grmjRigName -changeCommand "$grmjRigName = `textField -query -text rigShortName`" rigShortName;' )
    pm.text( label = "Icon Name:" )
    pm.mel.eval( 'textField -text $grmjIconName -changeCommand "$grmjIconName = `textField -query -text rigIconName`" rigIconName;' )
    # I want to have an easy select button.
    pm.text( label = "Icon Parent:" )
    pm.mel.eval( 'textField -text $grmjIconParn -changeCommand "$grmjIconParn = `textField -query -text rigIconParn`" rigIconParn;' )

    #!!! removed 'icoType' arg from rmIconify()
    pm.button( label = "Add Icon", command = lambda *x:rmIconify( "select", pm.melGlobals.get("grmjIconName"), pm.melGlobals.get("grmjIconParn"), rmIcoOrient ))
    pm.setParent( '..' )

    pm.showWindow()
    
rmIconUI()


i touched only things begining with comments “#!!!”

Thank you so much, this looks pretty good. I was having trouble with using class defined variables in windows before. but this is what I am getting now. Any further assistance would be awesome.


# Error: Error executing callback <function <lambda> at 0x00000000356B7F28> - <lambda> - module rigAmaJig.rmjIconic - C:/Users/cargoyle/Documents/maya/scripts\rigAmaJig\rmjIconic.py, line 85
# 
# Original message:
# Traceback (most recent call last):
#   File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\factories.py", line 742, in callback
#     res = origCallback( *newargs )
#   File "C:/Users/cargoyle/Documents/maya/scripts\rigAmaJig\rmjIconic.py", line 85, in <lambda>
#     pm.button( label = "Add Icon", command = lambda *x:rmIconify( "select" , pm.melGlobals.get("grmjIconName"), pm.melGlobals.get("grmjIconParn"), rmIcoOrient ))
#   File "C:/Users/cargoyle/Documents/maya/scripts\rigAmaJig\rmjIconic.py", line 38, in rmIconify
#     rmIconType = pm.radioButton( rmSelected, q=1, label=1 )
#   File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\factories.py", line 764, in newUiFunc
#     return beforeUiFunc(*args, **kwargs)
#   File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\factories.py", line 878, in newFuncWithReturnFunc
#     res = beforeReturnFunc(*args, **kwargs)
#   File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\pmcmds.py", line 134, in wrappedCmd
#     res = new_cmd(*new_args, **new_kwargs)
# RuntimeError: Object 'NONE' not found.
# Traceback (most recent call last):
#   File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\factories.py", line 744, in callback
#     raise CallbackError(origCallback, e)
# CallbackError: Error executing callback <function <lambda> at 0x00000000356B7F28> - <lambda> - module rigAmaJig.rmjIconic - C:/Users/cargoyle/Documents/maya/scripts\rigAmaJig\rmjIconic.py, line 85
# 
# Original message:
# Traceback (most recent call last):
#   File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\factories.py", line 742, in callback
#     res = origCallback( *newargs )
#   File "C:/Users/cargoyle/Documents/maya/scripts\rigAmaJig\rmjIconic.py", line 85, in <lambda>
#     pm.button( label = "Add Icon", command = lambda *x:rmIconify( "select" , pm.melGlobals.get("grmjIconName"), pm.melGlobals.get("grmjIconParn"), rmIcoOrient ))
#   File "C:/Users/cargoyle/Documents/maya/scripts\rigAmaJig\rmjIconic.py", line 38, in rmIconify
#     rmIconType = pm.radioButton( rmSelected, q=1, label=1 )
#   File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\factories.py", line 764, in newUiFunc
#     return beforeUiFunc(*args, **kwargs)
#   File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\factories.py", line 878, in newFuncWithReturnFunc
#     res = beforeReturnFunc(*args, **kwargs)
#   File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\pmcmds.py", line 134, in wrappedCmd
#     res = new_cmd(*new_args, **new_kwargs)
# RuntimeError: Object 'NONE' not found. # 
# for dev only delete existing and re-compile

what code you use? could you post it?

I was trying to conform the variables to a pre described naming convention. I can get it to load iconify but it has trouble querying the radio collection


class iconicControls(object):
    """ This class will help us to store ui controls ids for getting controls values """
    def __init__(self):
        self.rmIconType = 0
        self.IconName = {'Circle':'Circle',
                         'Box':'Box',
                         'Foot':'Foot',
                         'Compass':'Compass',
                         'Compass Box':'Circle',
                         'Finger Control':'Circle'}
       
# create global object for ui controls
ui = iconicControls()

def rmIconify( name, icoName, icoParn, icoOrient ):
    # first the dependencies
    import pymel.core as pm
    import rigAmaJig.rmPuppetTools as pt

    #!!! get selected radioButton control name
    rmSelected = pm.radioCollection(ui.rmIconType, q=1, select=1 )

    #!!! get label of this control
    rmIconType = pm.radioButton( rmSelected, q=1, label=1 )
    
    #!!! get actual control name by the dict. key from ui.iconName
    icoType = ui.rmIcoTypeName.get(rmIconType)
    print(icoType) # for test

    pt.rmAddIcon( name, rmIconType, icoType, icoParn, icoOrient )

def rmIconUI():
    # first the dependencies
    import pymel.core as pm

    rmActiveRmp = pm.ls(selection = True)
    rmIconName = rmActiveRmp[0].split('_')
    pm.melGlobals.initVar( 'string','grmjIconName')
    pm.melGlobals['grmjIconName'] = rmIconName[1]
    pm.melGlobals.initVar( 'string','grmjIconParn')
    pm.melGlobals['grmjIconParn'] = "Master"
    rmIcoOrient = True
    rmMakeNow = True

    # check if window exists and delete old window if it does
    if pm.mel.eval('window -ex "rmjIconWin" ;'):
        pm.mel.eval('deleteUI "rmjIconWin";' )
    # create a new window using mel.eval becuase pymel doesn't let you name a window
    rmjWin = pm.mel.eval('window -title "Rig-A-Ma-Jig Icon Tool" -width 100 -height 200 rmjIconWin;')

    pm.rowColumnLayout( )
    pm.frameLayout( label = "Build Icon", borderStyle='in', collapsable=True )

    pm.text( label = "Icon Type:" )

    pm.rowColumnLayout()
    pm.checkBox( label = 'Orient icon axis to joint:', align = 'center', onc = 'rmIcoOrient = True', ofc = 'rmIcoOrient = False', v = True )
 
    #!!! we have to save this control's id
    ui.rmIconType = pm.radioCollection()
 
    pm.setParent( '..' )
    # need to put in a pulldown list for this guy
    #pm.mel.eval( 'textField -text $grmjRigName -changeCommand "$grmjRigName = `textField -query -text rigShortName`" rigShortName;' )
    pm.text( label = "Icon Name:" )
    pm.mel.eval( 'textField -text $grmjIconName -changeCommand "$grmjIconName = `textField -query -text rigIconName`" rigIconName;' )
    # I want to have an easy select button.
    pm.text( label = "Icon Parent:" )
    pm.mel.eval( 'textField -text $grmjIconParn -changeCommand "$grmjIconParn = `textField -query -text rigIconParn`" rigIconParn;' )

    pm.button( label = "Add Icon", command = lambda *x:rmIconify( "select" , pm.melGlobals.get("grmjIconName"), pm.melGlobals.get("grmjIconParn"), rmIcoOrient ))
    pm.setParent( '..' )

    pm.showWindow()

not sure if you can tell this, but this script is part of a larger program. It has something like 20 other scripts. approximately 50 functions all together. Other functions are now randomly disappearing from other modules. again I can call the functions from the command line after import but not from UI’s. I just don’t understand how this is happening.

everything was running fine, how can changing one file effect two separate files that have nothing to do with the file that I have changed. let alone the fact that everything is there on one instance of the program and gone the next. everything appears to be called in a legal manner, possibly not the best code, but at least it’s kosher python. and then poof, gone.

12: don’t know why it posted this. guess I shouldn’t write posts from my phone.

Sorry man. the code is very messy. I don’t have full vision what is going on. According the code, you have removed radioButtons from collection since last time and now rmIconify raise an exception.

  1. For creating main window you could use this code:

class iconicControls(object):
    """ This class will help us to store ui controls ids for getting controls values """
    def __init__(self):
        self.rmIconType = 0
        self.winMain = '' # adding main window handle
        self.IconName = {'Circle':'Circle',
                         'Box':'Box',
                         'Foot':'Foot',
                         'Compass':'Compass',
                         'Compass Box':'Circle',
                         'Finger Control':'Circle'}
       
# create global object for ui controls
ui = iconicControls()

def rmIconUI():
    # first the dependencies
    import pymel.core as pm

    ...

    #!!! FIXED: you don't need MEL =)
    # check if window exists and delete old window if it does
    if pm.window(ui.winMain, ex=1):
        pm.deleteUI(ui.winMain)
    ui.winMain = pm.window( title="Rig-A-Ma-Jig Icon Tool", w=100, h=200 )

    ...
    
    #!!! FIXED: what window we want to show
    pm.showWindow(ui.winMain)

  1. Try better organize your code. This will definitely help you to understand it in future.

again I can call the functions from the command line after import but not from UI’s. I just don’t understand how this is happening.

I’ll give you a small example.

Let’s create global function which we can use in-during current Maya session.


// calc 2 float values and return result as float
global proc float getValue(float $a, float $b){
    return ($a+$b);
}

Execute this in script editor. Now you can call it in MEL tab, like you call your scripts functions:


getValue(4.3, 5.9)

or with help Python cmds:


import maya.mel as mel
print mel.eval('getValue(4.3, 5.9)')

or with help Pymel:


#1
import pymel.core as pmc
print pmc.mel.getValue(4.3, 5.9)

#2
import pymel.core as pmc
print pmc.mel.eval('getValue(4.3, 5.9)')

and from UI controls:


pm.button( label = "Get Value", command = 'print pmc.mel.getValue(4.3, 5.9)' )

Be free to use the same techniques when you need call any global functions :wink:

Thank you, you don’t know how long I have wanted to get rid of the mel.eval method for calling windows. the pymel help doesn’t tell you to store the window instance in a class at all. I’m totally going to use this.

Everything in iconify works for now. the problems are echoing to all the other files but I think I just need to take some time to clean them. I really can’t thank you enough for all of your help. PM me if you ever need anything. Thanks again.

Carl

n.p. ). just keep in mind, that using class in this case it’s for usability. it might be dictionary like this:


ui = {
'rmIconType':0,
'winMain':'',
'IconName':{'Circle':'Circle','Foot':'Foot'}
}

# to get/set value
ui['winMain'] = 0
a = ui['winMain']

or variables, BUT in this case you need define them as global in you function:

this won’t work


winMain = ''

def funcUI:
     # new local variable will be created inside funcUI()
     winMain = 12345

right way


winMain = ''

def funcUI:
    global winMain

    # now winMain will be set properly
     winMain = 12345

p.s. dictionaries are in global namespace by default.