Python Scope - Namespaces


#1

Hi there!!

 I did a little script with MEL and as an exercise I decided to translate it to Python.
  
 I have some interface controls defined within a def and some other commands querying those object's values within another def. When I try to query this way in Python it gives me an Error telling the object doesn't exist (because it is in the namespace of another def). An example:

     import maya.cmds as cmds
     
     def changedValue () :
     	if cmds.checkBox("myCHKBOX" , q = True, enable = True) != True :
     		cmds.checkBox("myOtherCHKBOX" , e = True , enable  = False)
     	else :
     		cmds.checkBox("myOtherCHKBOX" , e = True , enable  = True)  
     
     def createWindow ():
     	cmds.window("myWindowUI" , title = "myWindowUI" , wh = (282,200) )
     	cmds.columnLayout("mainCol" , adj = True)
     	cmds.checkBox("myCHKBOX" , label = "myCHKBOX" , v = False , changeCommand = changedValue())
     	cmds.checkBox("myOtherCHKBOX" , label = "myOtherCHKBOX" , v = True , enable = False)
     	cmds.showWindow("myWindowUI")
     
  This is the Error message: # Error: RuntimeError: file <maya console> line 5: Object 'myCHKBOX' not found.
 
 To solve this with MEL I define the proc as a "global proc" but I'm not sure if you can do this with Python.
 
 Thanks!!

#2

I think you want this:

def changedValue(*args):
    if (cmds.checkBox("myCHKBOX" , q = True, value = True) != True) :
        cmds.checkBox("myOtherCHKBOX" , e = True , enable  = False)
    else :
        cmds.checkBox("myOtherCHKBOX" , e = True , enable  = True)  
        
def createWindow():
    if (cmds.window ('myWindowUI', exists=True)) :
        cmds.deleteUI ('myWindowUI')
    cmds.window("myWindowUI" , title = "myWindowUI" , wh = (282,200) )
    cmds.columnLayout("mainCol" , adj = True)	
    cmds.checkBox("myCHKBOX" , label = "myCHKBOX" , v = False, changeCommand = changedValue)
    cmds.checkBox("myOtherCHKBOX" , label = "myOtherCHKBOX" , v = True , enable = False)
    cmds.showWindow("myWindowUI")
    	
createWindow()

#3

Yep, that worked!! Thanks!! :slight_smile:

I’ll try to find out what *args means.


#4

Your problem was the line with:
… changeCommand = changedValue())
With the brakets the procedure changedValue is called during the construction of the checkbox.
changeCommand = changedValue() tries to assign the result of changedValue() to the variable changeCommand.
changeCommand = changedValue assigns the function to the variable instead of the result of the function.


#5

You’re right haggi, that and the *args solved my problem. Thanks!!


#6

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.