Last active
August 29, 2015 13:56
-
-
Save theodox/9064962 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CtlProperty (object): | |
''' | |
Property descriptor. When applied to a Control-derived class, invokes the correct Maya command under the hood to get or set values | |
''' | |
def __init__(self, flag, cmd): | |
assert callable(cmd), "cmd flag must be a maya command for editing gui objects" | |
self.Flag = flag | |
self.Command = cmd | |
def __get__(self, obj, objtype): | |
''' | |
Class instance <obj> and its type <objtype> are passed in automatically. | |
<self> is this descriptor object, NOT an owning class instance! | |
''' | |
ctrl = obj.Widget if hasattr(obj, "Widget") else str(obj) | |
return self.Command(ctrl, **{'q':True, self.Flag:True}) | |
def __set__(self, obj, value): | |
''' | |
Again, the owning instance is passed in as <obj> automatically | |
''' | |
ctrl = obj.Widget if hasattr(obj, "Widget") else str(obj) | |
self.Command(ctrl, **{'e':True, self.Flag:value}) | |
class ExampleButton(object): | |
CMD = cmds.button | |
def __init__(self, *args, **kwargs): | |
self.Widget = self.CMD (*args, **kwargs) | |
Label = CtlProperty('label', CMD) | |
BackgroundColor = CtlProperty('bgc', CMD) | |
# same example as before | |
example = cmds.window() | |
col = cmds.columnLayout() | |
btn = ExampleButton("hello world") | |
cmds.showWindow(example) | |
btn.Label = "Thunderbirds are GO!" | |
btn.BackgroundColor = (.25, 1, .25) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment