How To Make Functions Work With Each Other?

Hello there.
I have problem with converting one of my mel scripts to Python and haven’t find solution, so I came here for help. Now let me tell you that I am newbie when it comes to Python, I have some experience with mel scripts and that’s it. I do however converted my simple scriptedRig.mel to Python successfully, but functions there were independant of each other, not like in this case…

So to simulate problem I created very simple testWindow.py file, then created a button on Maya shelf with command:

CODE
import testWindow
reload (testWindow)
testWindow.testWindow()

I know that I calling only the first function by doing it like that. But if I wrote testWindow() I get TypeError: 'module' object is not callable.

Script itself contains this code:

CODE
import maya.cmds as mc
def testWindow():
    MyWin = mc.window( title=“Long Name”, iconName=‘Short Name’, widthHeight=(200, 55) )
    mc.columnLayout( adjustableColumn=True )
    mc.button( label=‘Do Nothing’ )
    mc.button( label=‘Close’, command=(‘mc.deleteUI("’ + MyWin + ‘", window=True)’) )
    mc.button( label=‘delUI’, command=deleteWindow())
    mc.setParent( ‘..’ )
    mc.showWindow( MyWin )
    
def deleteWindow():
    mc.deleteUI( MyWin, window=True )

MyWin variable is contained in the first function, but the second function executes as a button by first.

If I write it like this:

CODE
import maya.cmds as mc
def testWindow():
    MyWin = mc.window( title=“Long Name”, iconName=‘Short Name’, widthHeight=(200, 55) )
    mc.columnLayout( adjustableColumn=True )
    mc.button( label=‘Do Nothing’ )
    mc.button( label=‘Close’, command=(‘mc.deleteUI("’ + MyWin + ‘", window=True)’) )
     mc.button( label=‘delUI’, command=deleteWindow())
    mc.setParent( ‘..’ )
    mc.showWindow( MyWin )
    
    def deleteWindow():
        mc.deleteUI( MyWin, window=True )

it will give me: \# Error: local variable 'deleteWindow' referenced before assignment
Which is of course totally true, but how can I pass that? If I put deleteWindow() function first, then MyWin variable will not be defind and this is such an easy script. Does functions in Python have no chance to act like global procedures in mel? How to make them work with each other?

I downloaded two Kel Solaar's Python scripts to examine them on how this problem solved, but I failed to execute them!

I realize that this question may be stupid and answer may be right under my nose, but I really can't find answer on this topic.

p.s. Oh and I hope that you understand me, because my english is really bad...

Here is my example code that I always use as a starting point to create windows in Python.

This should help you. Just copy the code in a file named windowExample.py for the execution code at the end to work

[codebox]import maya.cmds as mc

def window():
“”" create a window “”"
window = “myWindow”
windowTitle = “My Window | version 1.0”
if (mc.window(window, exists=True)):
mc.deleteUI(window, window=True)

if( mc.windowPref(window, exists=True) ):

# mc.windowPref(window, remove=True)
pass

window = mc.window(window,sizeable=True, toolbox=False, resizeToFitChildren=True, title=windowTitle)

# Create window form layout.
windowForm = mc.formLayout("windowForm")
# Create the tab Layout.
mainForm = mc.formLayout("mainForm")

co = mc.columnLayout('columnLeft', adjustableColumn=True, columnAlign='left', columnAttach=['both',0])


buttonForm = mc.formLayout(parent=windowForm)
buttonHeight = 26
applyButton = mc.button( height=buttonHeight, label='Apply', command='windowExample.\_\_windowApply()' )
applyCloseButton = mc.button( height=buttonHeight, label='Apply and Close', command='windowExample.\_\_windowApply("%s",True)' %window)
closeButton = mc.button( height=buttonHeight, label='Close', command='windowExample.\_\_windowClose("%s")' %window )

#	Apply attachments to the buttons so that they appear centred 
#	in the window.
#
space = 4 # Space between buttons.
mc.formLayout(buttonForm, edit=True, numberOfDivisions=100,
			  attachForm=[(applyButton,'top',0), (applyButton,'left',0), (applyButton,'bottom',0), (applyCloseButton,'top',0), (applyCloseButton,'bottom',0), (closeButton,'top',0), (closeButton,'bottom',0), (closeButton,'right',0)],
			  attachPosition=[(applyButton,'right',space/2,33), (applyCloseButton,'left',space/2,33), (applyCloseButton,'right',space/2,66), (closeButton,'left',space/2,66)])

# Attach the mainTab and the buttonRow to the windowForm
mc.formLayout(windowForm, edit=True, 
			  attachForm=[(mainForm,'top',3),(mainForm,'right',3),(mainForm,'left',3), (buttonForm,'right',3),(buttonForm,'left',3),(buttonForm,'bottom',3)],
			  attachControl=[(mainForm,'bottom',3,buttonForm)],
			  attachNone=[(buttonForm,'top')])

mc.showWindow(window)

def __windowApply(window=None, closeWindow=False ):
“”" read the option in the window and call the proper function “”"
# read window value
print ‘getting window values’

# close the window	
if closeWindow == True and window is not None:
	mc.deleteUI(window, window=True)	

def __windowClose(window=None):
if window is not None:
mc.deleteUI(window, window=True)

“”"
#example
import windowExample
reload(windowExample)
windowExample.window()
“”"[/codebox]

Thanks Kobayashy!

Your script example is really helpful.

So it seems that variables can be used in other functions, but they must be referenced as arguments (in function’s curly brackets)?

By the way, is double underscores mean something when defining functions? I erased them in your example and script seems to work just as fine.
Just curious because I had read somewhere, that Python is sensible for double underscores.
Or they have meaning only to special functions like __init__, __name__ and so on?

Also it seems that functions can be referenced before creation, but variables can’t.

For example this works:

CODE
import maya.cmds as mc

def testWindow():
    MyWin = “MyWindow”
    MyWord = “Window is deleted”
    MyWin = mc.window(MyWin, title=“Long Name”, iconName=‘Short Name’, widthHeight=(200, 55) )
    mc.columnLayout( adjustableColumn=True )
    
    createUI(MyWin, MyWord)
    
    mc.setParent( ‘..’ )
    mc.showWindow( MyWin )    
    
def printName(MyWin):
    print MyWin

def delUI(MyWin, MyWord):
    mc.deleteUI(MyWin, window=True)
    print MyWord

def createUI(MyWin, MyWord):
    mc.button( label=‘Do Nothing’ )
    mc.button( label=‘Close’, command=(‘mc.deleteUI("’ + MyWin + ‘", window=True)’) )
    mc.button( label=‘Window Name’, command=‘windowMaker.printName(“%s”)’ %MyWin)
    mc.button( label=‘Delete Window’, command=‘windowMaker.delUI(“%s”,“%s”)’ %(MyWin, MyWord))

But this doesn’t:
CODE
import maya.cmds as mc

def testWindow():
    MyWin = “MyWindow”
    MyWin = mc.window(MyWin, title=“Long Name”, iconName=‘Short Name’, widthHeight=(200, 55) )
    mc.columnLayout( adjustableColumn=True )
    
    createUI(MyWin)
    
    mc.setParent( ‘..’ )
    mc.showWindow( MyWin )    
    
def printName(MyWin):
    print MyWin

def delUI(MyWin, MyWord):
    mc.deleteUI(MyWin, window=True)
    print MyWord

def createUI(MyWin):
    mc.button( label=‘Do Nothing’ )
    mc.button( label=‘Close’, command=(‘mc.deleteUI("’ + MyWin + ‘", window=True)’) )
    mc.button( label=‘Window Name’, command=‘windowMaker.printName(“%s”)’ %MyWin)
    mc.button( label=‘Delete Window’, command=‘windowMaker.delUI(“%s”,“%s”)’ %(MyWin, MyWord))
    
    MyWord = “Window is deleted”

# UnboundLocalError: local variable ‘MyWord’ referenced before assignment #
It’s okay though, just checking.

Thanks again!