Last active
August 29, 2015 13:57
-
-
Save theodox/9633300 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 Nested(Control): | |
''' | |
Base class for all the nested context-manager classes which automatically parent themselves | |
''' | |
ACTIVE_LAYOUT = None | |
def __init__(self, key, *args, **kwargs): | |
self.Controls = [] | |
super(Nested, self).__init__(key, *args, **kwargs) | |
def __enter__(self): | |
self.__cache_layout = Nested.ACTIVE_LAYOUT | |
Nested.ACTIVE_LAYOUT = self | |
return self | |
def __exit__(self, typ, value, traceback): | |
self.layout() | |
Nested.ACTIVE_LAYOUT = self.__cache_layout | |
self.__cache_layout = None | |
cmds.setParent("..") | |
def layout(self): | |
''' | |
this is called at the end of a context, it can be used to (for example) perform attachments | |
in a formLayout. Override in derived classes for different behaviors. | |
''' | |
return len(self.Controls) | |
def add(self, control): | |
path_difference = control.Widget[len(self.Widget):].count('|') - 1 | |
if not path_difference: | |
self.Controls.append(control) | |
if control.Key and not control.Key[0] == "_": | |
if control.Key in self.__dict__: | |
raise RuntimeError('Children of a layout must have unique IDs') | |
self.__dict__[control.Key] = control | |
def remove(self, control): | |
self.Controls.remove(control) | |
k = [k for k, v in self.__dict__.items() if v == control] | |
if k: | |
del self.__dict__[k[0]] | |
def __iter__(self): | |
for item in self.Controls: | |
for sub in item: | |
yield sub | |
yield self | |
@classmethod | |
def add_current(cls, control): | |
if cls.ACTIVE_LAYOUT: | |
Nested.ACTIVE_LAYOUT.add(control) | |
# IMPORTANT NOTE | |
# this intentionally duplicates redundant property names from Control. That forces the metaclass to re-define the CtlProperties using cmds.layout | |
# instead of cmds.control. In Maya 2014, using cmds.control to query a layout fails, evem for flags they have in common | |
class Layout(Nested): | |
CMD = cmds.layout | |
_ATTRIBS = ['annotation', 'backgroundColor', 'defineTemplate', 'docTag', 'dragCallback', 'dropCallback', 'enable', 'enableBackground', 'exists', 'fullPathName', 'height', 'manage', 'noBackground', 'numberOfPopupMenus', 'parent', 'popupMenuArray', 'preventOverride', 'useTemplate', 'visible', 'visibleChangeCommand', 'width'] | |
_CALLBACKS = ['dragCallback', 'dropCallback', 'visibleChangeCommand'] | |
_READ_ONLY = ['isObscured', 'popupMenuArray', 'numberOfPopupMenus', 'childArray', 'numberOfChildren'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment