Maya python: execute error name 'xxxx' is not defined


#1

hello guys:
I have four files, my purpose is create a shelf button in custom shelf after drag and drop the install file into the scene, and then you click the button to toggle the UI windows of the tool

The four files are like:
install.mel (import the shelf.py file, because the python file doesn’t work with drag and drop as installer)

python(“import install as ins”);
python(“ins.setShelf”)

shelf.py (create a shelf button to custom shelf)

import UI                   #import ui file
def setShelf():
    #create shelfbutton as the child of custom shelf 
    cmds.shelfButton(command='UI.createUI(), parent='Custom')

ui.py (the tool’s UI)
import fun
def createUI():

fun.py (the tool’s functions)

after drag and drop the install.mel file into the scene, the button will show in shelf as expected, but when click it give an error said

“name’UI’ is not defined”

but if i execute (click the execute button)the shelf.py file from script editor, everything back to work, seems because the first time the py file doesn’t execute properly? But how to solve it?


#2

You need to do something like this:

cmds.shelfButton(command=‘import UI; UI.createUI()’, parent=‘Custom’)

even though shelf.py is importing UI, that code is not run when you click the button - only the code that you specify in the command string gets run.