0

Before I start I just want to clarify that am quite new to python and programming in general. I still couldn't write a class and don't understand object orientated programming yet.

I am currently writing tools using python for maya.

The problem is I wrote a main UI for my scripts, and everything functioned just fine. However I don't want to copy/paste my script every time I update my UI so I wrote another script to call on my main UI.

import maya.cmds as mc ;
import sys ;

mp = "C:/Users/Administrator/Desktop/script/birdScript20170707" ;
if not mp in sys.path :
    sys.path.append ( mp ) ;

import library.mainUI as mui ;

mui.birdMainUI () ;

This is my main UI:

import sys ;
import os ;
import maya.cmds as mc ;

mp = "C:/Users/Administrator/Desktop/script/testPackage" ;
if not mp in sys.path :
    sys.path.append ( mp ) ;

import library.mayaLibrary as mlb ;

def printSelfPath( ) :
    import general.printSelfPath as psp ;
    reload ( psp ) ;
    psp.printPath ( ) ;

###############
''' MAIN UI '''
###############

def birdMainUI ( ) :

    # check if window exists

    if mc.window ( 'birdMainUI' , exists = True ) :
        mc.deleteUI ( 'birdMainUI' ) ;

    # create window 

    window = mc.window ( 'birdMainUI', title = "birdMainUI v0.0", width = 200,
        mnb = False , mxb = False , sizeable = True , rtf = True ) ;


    general = mc.rowColumnLayout ( nc = 1 , cw = ( 1 , 200 ) ) ;

    mc.button ( 'print self path' , c = 'printSelfPath( )' ) ;

    mc.setParent( '..' ) ;

    mc.showWindow ( window ) ;

#birdMainUI () ;

This is the function which the main UI uses:

import os ;
import sys ;

import maya.cmds as mc ;
import maya.mel as ml ;

def printPath ( *args ) :

    path =  ml.eval ( "file -q -sn" ) ;

    print ( path ) ;

The UI appeared but when I click on my UI button this error appeared:

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

I have no idea what is going on. This is my file structure:

file structure

This is the package if anyone is interested: https://drive.google.com/file/d/0B9acOuH0SmXsSk9aX0x6Nl9KbTQ/view?usp=sharing

Thank you.

  • did you try close and restart Maya? Would it work in that case? I'm not suggesting this as solution; itwould just help figuring out whether your problem is related to python modules not being reloaded by Maya – mapofemergence Jul 08 '17 at 08:47
  • I test my script only with freshly opened maya. However if I run the main UI once, calling the main UI via call script works without the problem. Thank you. – birdiesama Jul 08 '17 at 08:57
  • in your `birdMainUI` function try replace this line: `mc.button('print self path', c='printSelfPath()')` with `mc.button('print self path', c=printSelfPath)`; if that works, you might want to read this: https://theodox.github.io/2014/maya_callbacks_cheat_sheet via: https://stackoverflow.com/questions/44753094/maya-python-ui-local-variable-not-defined-error-when-used-inside-button-comman – mapofemergence Jul 08 '17 at 16:59
  • Regrettably it still doesn't work but I found your link very useful. Thank you for the resources. – birdiesama Jul 08 '17 at 18:37
  • You sure you typed `c=printSelfPath` and not `c='printSelfPath'`? Sorry if I double-check; it's just that I tried your code and it works for me. Are you getting a different error now, by any chance? Did you try deleting the `.pyc` files, to be sure they're up to date? Useful read for pyc creation/update: https://stackoverflow.com/a/15839646/8200213 although with Maya this works differently and is triggered by `realod()` instead than just `import` (a bit confusing, I know, but that's why it's easier to just delete the `.pyc` files, as a first attempt) – mapofemergence Jul 09 '17 at 10:47
  • It seems that I forgot to reload my imported module in my caller, but the script still doesn't work for me. I typed c=printSelfPath without the '. I still get same error. However, I managed to get the script to work just now with your advice and putting the *arg when defining the import function in the main UI. I don't know what is going on, but it is now working. Here's the revised script. https://drive.google.com/file/d/0B9acOuH0SmXsRVhwVVhBRHQ0YjQ/view?usp=sharing Thank you so much for the advices. – birdiesama Jul 09 '17 at 14:17

0 Answers0