Definitely not the first one. Avoid relying on globals when possible.
The second and third are equally fine right now. But if you start needing
more state such as storing more widgets or values, then the class will end
up becoming the better choice.
On 18/07/2014 2:54 AM, "Gabriele Bartoli" <[email protected]> wrote:
> Hello again. I came up with three versions of the script, all of them
> working properly. I'd like your opinion regarding which one you guys prefer!
>
> Version one, which works because I changed the scope of toggle_test to
> 'global'
>
> # Build the UI
> def launch_UI():
>
> global toggle_test
>
> # 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(*args):
> # Query toggle status
> var = mc.checkBox(toggle_test, query = True, value = True )
>
> # Print the status
> print "Checkbox's status is:", var
>
> launch_UI()
>
> Version two, which uses the partial command (which is a wrapper, isn't it?
> I still have to 'wrap' may head around wrappers)
>
> # 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 = partial(printIt, toggle_test))
> mc.showWindow("myWindow")
>
> # Print the value
> def printIt(check_box, *args):
> # Query toggle status
> var = mc.checkBox(check_box, query = True, value = True )
>
> # Print the status
> print "Checkbox's status is:", var
>
> launch_UI()
>
> Version three, which works with a Class:
>
> class Custom_tool:
> def __init__(self):
>
> # 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)
> self.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 = self.printIt)
> mc.showWindow("myWindow")
>
> # Print the value
> def printIt(self, *args):
>
> # Query toggle status
> var = mc.checkBox(self.toggle_test, query = True, value = True )
>
> # Print the status
> print "Checkbox's status is:", var
>
> new_tool = Custom_tool()
>
> I like the second better, because the global variable in v1 kinda throws
> me off and because using a custom class to create only one object feels
> like overshooting. But hey, I might be wrong! Any feedback would be welcome!
>
> Thanks a lot!
>
> --
> 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/733dcb16-55b0-43c4-8f5c-9de1765993e4%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/733dcb16-55b0-43c4-8f5c-9de1765993e4%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/CAPGFgA2b9R%2BQ4_kJXg4oq0r%3DcHXJghc0dmLZWmEdequH8kfV%3Dg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.