Search code examples
pythonmaya

name is not defined during an import


i'm facing a problem I can't understand... here is a part of an initial code for an auto-rig :

def Legs():
    Vleg = cmds.intSliderGrp('legs', q=True ,v=True)
    if Vleg == 1:
        Leg01 = cmds.intSliderGrp('Leg1', l= 'Sym. Limb no.1', min=0, max=8, f=True)
    elif Vleg == 2:
        Leg01 = cmds.intSliderGrp('Leg1', l= 'Sym. Limb no.1', min=0, max=8, f=True)
        Leg02 = cmds.intSliderGrp('Leg2', l= 'Sym. Limb no.2', min=0, max=8, f=True)
    elif Vleg == 3:
        Leg01 = cmds.intSliderGrp('Leg1', l= 'Sym. Limb no.1', min=0, max=8, f=True)
        Leg02 = cmds.intSliderGrp('Leg2', l= 'Sym. Limb no.2', min=0, max=8, f=True)
        Leg03 = cmds.intSliderGrp('Leg3', l= 'Sym. Limb no.3', min=0, max=8, f=True)
    else :
        Leg01 = cmds.intSliderGrp('Leg1', l= 'Sym. Limb no.1', min=0, max=8, f=True)
        Leg02 = cmds.intSliderGrp('Leg2', l= 'Sym. Limb no.2', min=0, max=8, f=True)
        Leg03 = cmds.intSliderGrp('Leg3', l= 'Sym. Limb no.3', min=0, max=8, f=True)
        Leg04 = cmds.intSliderGrp('Leg4', l= 'Sym. Limb no.4', min=0, max=8, f=True)


win = 'win'
win = cmds.window(win, s=True, t = 'Auto-rig Prototype')
cmds.showWindow(win)
cmds.columnLayout(adj=True)
cmds.separator(h = 5 )
cmds.intSliderGrp ('legs', l = 'Number of symetrical parts', min=1, max=4, v=1, f=True)
cmds.separator(h = 5 )
cmds.text(l='please execute commands in the APPROPRIATE order')
cmds.separator(h = 5 )
cmds.button (l = '1 : Set', c = 'Legs()')
cmds.separator(h = 5 )
cmds.button (l = '2 : Create Guides', c = 'Guide()')
cmds.separator(h = 5 )
cmds.button( l= '3 : Set Rig', c= 'rig()')
cmds.separator(h = 5 )
cmds.button( l= '4 : Set CTRLS', c= 'ctrl()')
cmds.separator(h = 5 )
cmds.button( l= '5 : Parent CTRLS', c= 'Par()')
cmds.separator(h = 5 )
cmds.button (l = 'Delete selection and unused nodes', c = 'Suppr()')

When i run this scirpt alone, eveything works as expected, but then i tried to import it

import maya.cmds as cmds
import maya.mel as mel
def printNewMenuItem( item ):
    if item == 'Arachnoid':
        if cmds.window(InitWin, exists = True):
            cmds.deleteUI(InitWin)
        import SpidRig
    else :
        print str(item) + ' : this command is not scripted yet'
InitWin = cmds.window(t='Choose your rig')
if cmds.window(InitWin, exists = True):
    cmds.deleteUI(InitWin)
cmds.window(t='Choose your rig')
cmds.columnLayout()
cmds.optionMenu( label='Rig Type', changeCommand=printNewMenuItem)
cmds.menuItem( label='Basic Humanoid' )
cmds.menuItem( label='Arachnoid' )
cmds.menuItem( label='Bird' )
cmds.showWindow( InitWin )

And now, depending on times, either the window ' Auto-rig Prototype' never opens, either it returns me this error :

# Error: NameError: file <maya console> line 1: name 'Legs' is not defined # 

what am i doing wrong?


Solution

  • try to remove the string from your commands :

    cmds.button (l = '1 : Set', c = Legs)
    

    and write your command like so :

    def Legs(*args):
        Vleg = cmds.intSliderGrp('legs', q=True ,v=True)
        ...
    

    run from a fresh maya restarted and then, we will be able to debug. It seems to be a problem of namespace / import / global var and local var

    like this error :

    # Error: NameError: file <maya console> line 1: name 'Legs' is not defined # 
    

    it means that when you click on this button :

    cmds.button (l = '1 : Set', c = 'Legs()')
    

    it tries to exec this litteral command. But, zhen you import it like so :

    import leg_script
    

    you import it with a namespace : leg_script for example, if you import like this :

    import leg_script as leg
    

    the namespace is : leg

    in this two case you Leg proc is now written literally leg_script.Leg() or leg.Leg()

    You could get rid of the namespace by using :

    from leg_script import *
    

    but this is a bad practice beacause to debug your script it will become really difficult.


    So to recap the problem, when you write litteral strings in command :

    cmds.button (l = '1 : Set', c = 'Legs()')
    

    your script as problems with imports, if you want to ignore namespace problems :

    cmds.button (l = '1 : Set', c = Legs)
    

    which is the way to go, otherwise you would havve to write the namespace in the string :

    cmds.button (l = '1 : Set', c = 'leg_script.Legs()')
    

    but it wont work in local and futhermore, only if it is called with this namespace.... so dont do it !