PDA

View Full Version : Python Scope - Namespaces


XianPalintir
12-06-2010, 02:17 PM
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!!

mduvekot
12-06-2010, 08:33 PM
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()

XianPalintir
12-06-2010, 09:14 PM
Yep, that worked!! Thanks!! :)

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

haggi
12-06-2010, 11:24 PM
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.

XianPalintir
12-07-2010, 01:44 PM
You're right haggi, that and the *args solved my problem. Thanks!!

CGTalk Moderation
12-07-2010, 01:44 PM
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.