Skip to content

Instantly share code, notes, and snippets.

View theodox's full-sized avatar

Steve Theodore theodox

View GitHub Profile
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
import maya.cmds as cmds
class ExampleButton(object):
CMD = cmds.button
def __init__(self, *args, **kwargs):
self.Widget = self.CMD (*args, **kwargs)
@property
def Label(self):
@Label.setter
def Label(self, val):
return self.CMD(self.Widget, e=True, label=val)
# add this to the example above:
btn.Label = "Goodbye cruel world"
def constructor(self, name):
self.Name = name
example = type('Example', (), {'__init__':constructor } )
Test = example("Hello world")
# <__main__.Example object at 0x00000000022D6198>
Test.Name
# Hello world
class ControlMeta(type):
'''
Metaclass which creates CtlProperty objects for maya gui proxies
'''
CONTROL_ATTRIBS = ['annotation', 'backgroundColor', 'defineTemplate',
'docTag', 'dragCallback', 'dropCallback', 'enable',
'enableBackground', 'exists', 'fullPathName', 'height',
'manage', 'noBackground', 'numberOfPopupMenus', 'parent',
'popupMenuArray', 'preventOverride', 'useTemplate', 'visible',
'visibleChangeCommand', 'width']
class MetaButton(object):
CMD = cmds.button
__metaclass__ = ControlMeta
def __init__(self, *args, **kwargs):
self.Widget = self.CMD (*args, **kwargs)
w = cmds.window()
c = cmds.columnLayout()
class ControlMeta(type):
'''
Metaclass which creates CtlProperty objects for Control classes
'''
CONTROL_ATTRIBS = ['annotation', 'backgroundColor', 'defineTemplate', 'docTag',
'dragCallback', 'dropCallback', 'enable', 'enableBackground',
'exists', 'fullPathName', 'height', 'manage', 'noBackground',
'numberOfPopupMenus', 'parent', 'popupMenuArray', 'preventOverride',
'useTemplate', 'visible', 'visibleChangeCommand', 'width']
class FloatSlider(Control):
'''sample output from mGui.helpers.tools.generate_commands()'''
CMD = cmds.floatSlider
_ATTRIBS = ['horizontal','step','maxValue','value','minValue']
_CALLBACKS = ['changeCommand','dragCommand']
from mGui.core.controls import FloatSlider
slider = FloatSlider('testslider')
# pythonic properties, thanks to the metaclass...
slider.width = 100
slider.minValue = 50
slider.maxValue = 250
class ControlMeta(type):
'''
Metaclass which creates CtlProperty and CallbackProperty objects for Control classes
'''
def __new__(cls, name, parents, kwargs):
CMD = kwargs.get('CMD', None)
_READ_ONLY = kwargs.get('_READ_ONLY',[])