You could either structure it as a class, as suggested, or you could use my
most favorite member of the stdlib, and wrap your callback:
import maya.cmds as mcfrom functools import partial
def launch_UI():
if mc.window("myWindow", ex=True):
mc.deleteUI("myWindow", window=True)
mc.window("myWindow",title="Test Window", s=False, wh=(300,100))
mc.columnLayout(adj=True)
toggle_test = mc.checkBox(label = 'Toggle me!', value = False)
mc.button(l="Press to print the value of the checkbox below",
w=300, h=100, command=partial(printIt, toggle_test))
mc.showWindow("myWindow")
def printIt(checkBox, *args):
var = mc.checkBox(checkBox, query = True, value = True )
print "Checkbox's status is:", var
launch_UI()
partial() lets you pass a callable and a number of args and kwargs to it to
bind it up as a new callable. So in this example we just set the callback
to already know the checkbox name, and to pass it to printIt().
The bit about that extra arg is the stuff that the Maya button callback
sends along. If it were a checkbox callback, it would tell you if it were
checked or not. You just have to add *args to swallow up that extra arg
since we don't care about it right now.
On Thu, Jul 17, 2014 at 4:08 AM, Yi Liang Siew <[email protected]> wrote:
> Hi Gabriele:
>
> You might want to look at some sample code for creating windows. (e.g.
> http://stackoverflow.com/questions/3492106/creating-a-maya-ui)
>
> However, more to the point, the reason you cannot access toggle_test is
> because it is out of scope, and only launchUI() knows about it. If you want
> printIt to have access to toggle_test, I would place these two methods in a
> class and have that variable be a instance variable.
>
>
> On 7/16/2014 8:49 AM, Gabriele Bartoli wrote:
>
> Following everybody's hints, I got to this point:
>
> # Load maya commands module
> import maya.cmds as mc
>
> # Build the UI
> def launch_UI():
>
> # This prevents multiple windows to pop up
> if mc.window("myWindow", ex=True):
> mc.deleteUI("myWindow", window=True)
>
> # Window set-up
> mc.window("myWindow",title="Test Window", s=False, wh=(300,100))
> mc.columnLayout(adj=True)
> toggle_test = mc.checkBox(label = 'Toggle me!', value = False)
> mc.button(l="Press to print the value of the checkbox below", w=300, h
> =100,
> command = printIt)
> mc.showWindow("myWindow")
>
> # Print the value
> def printIt():
>
> # Query toggle status
> var = mc.checkBox(toggle_test, query = True, value = True )
>
> # Print the status
> print "Checkbox's status is:", var
>
> launch_UI()
>
> And the shelf button still looks like this:
>
> import testScript
> reload (testScript)
>
> I'm sticking with having the UI launched from within the script simply
> because it allows me to run the code from both the script editor and the
> shelf. I seem to understand it shouldn't be like that, so I'll fix it when
> the debugging is done :)
>
> When I run it, the UI pops up and at button press I get this error:
>
> # Error: printIt() takes no arguments (1 given)
> # TypeError: printIt() takes no arguments (1 given) #
>
> So I edited the printIt function like this:
>
> # Print the value
> def printIt(test):
>
> print test
>
> # Query toggle status
> var = mc.checkBox(toggle_test, query = True, value = True )
>
> # Print the status
> print "Checkbox's status is:", var
>
> I get a printout of test as "False", but I am at a loss to what that value
> refers to and from where it comes from.
>
> Plus, I get a new error :P
>
> # Error: NameError: file
> D:/GoogleDrive/Maya/shared_folder/scripts\testScript_01.py line 25: global
> name 'toggle_test' is not defined #
>
> I was thinking of feeding "toggle_test" to print it using the call back,
> like this:
>
> mc.button(l="Press to print the value of the checkbox below", w=300, h=
> 100, command=printIt(toggle_test) )
>
> This doesn't seem to work though. This is what I get:
>
> # Error: TypeError: file
> D:/GoogleDrive/Maya/shared_folder/scripts\testScript_01.py line 16: Invalid
> arguments for flag 'command'. Expected string or function, got NoneType #
>
> I guess I could simply define it as a global variable and be done with it,
> but I don't think is the best option. Unless it is the ONLY option, which
> would make it the best option too >P
>
> Thanks a lot guys, and I apologize if I am being a N00b
>
> Cheers
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/3075c5c8-5215-461f-86d3-39adde494131%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/3075c5c8-5215-461f-86d3-39adde494131%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/53C6A36F.5050309%40gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/53C6A36F.5050309%40gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2nq3REBij-rm%3DtdT431nYOZk4gPGGDs8ygkYjThueCaA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.