def _ph_add_toolkit(menu, toolkit, label): """Add PsyHive toolkit option. Args: menu (str): menu to add to toolkit (mod): toolkit module to add label (str): label for toolkit """ _name = getattr(toolkit, 'PYGUI_TITLE', to_nice(toolkit.__name__.split('.')[-1]) + ' tools') _cmd = '\n'.join([ 'import {} as toolkit', 'import {} as py_gui', 'py_gui.MayaPyGui(toolkit.__file__)' ]).format(toolkit.__name__, py_gui.__name__) cmds.menuItem(parent=menu, command=_cmd, image=toolkit.ICON, label=_name) _btn = _add_psyhive_btn(label=label, cmd=None, icon=toolkit.ICON, tooltip=_name) py_gui.MayaPyShelfButton(mod=toolkit, parent='PsyHive', image=toolkit.ICON, label=_name, button=_btn)
def __init__( self, get_default=None, get_choices=None, label='Update', width=50): """Constructor. Args: get_default (fn): function to get arg default value get_choices (fn): function to get arg choices label (str): label for update button width (int): update button width """ self._get_default = get_default self._get_choices = get_choices self.label = label if not self.label and get_choices: self.label = to_nice(get_choices.__name__) if not self.label and get_default: self.label = to_nice(get_default.__name__) self.width = width
def add_def(self, def_, opts, last_, verbose=0): """Add a def to the interface. The opts dict will be empty in the case of adding all defs (ie. not using the install_gui decorator) - so the defaults here need to be applied. Args: def_ (PyDef): def to add opts (dict): display options last_ (bool): whether this is last def in interface verbose (int): print process data """ _update = opts.get('update') or {} _choices = opts.get('choices') or {} _hide = opts.get('hide') or [] _disable_reload = opts.get('disable_reload') or False _section = opts.get('section') _catch_error = opts.get('catch_error_', True) # If browser if list convert to default dict _browser = opts.get('browser') or {} if isinstance(_browser, list): _browser_dict = {} for _arg_name in _browser: print 'FIND ARG', _arg_name _arg = def_.find_arg(_arg_name) print 'ARG', _arg _browser_dict[_arg_name] = pyg_install.BrowserLauncher( default_dir=_arg.default) _browser = _browser_dict if _section: self.set_section(_section) self.read_settings_fns['def'][def_.name] = {} self.set_settings_fns['def'][def_.name] = {} # Add args for _arg in def_.find_args(): if _hide == '*' or _arg.name in _hide: continue lprint(' ADDING ARG', _arg, verbose=verbose) # Check for update fn _default = _arg.default _arg_choices = _choices.get(_arg.name) _arg_update = _update.get(_arg.name) if _arg_update and not isinstance(_arg_update, pyg_install.ArgUpdater): # Convert function to updater _arg_update = pyg_install.ArgUpdater(get_choices=_arg_update) if _arg_update: _arg_choices = _arg_update.get_choices() or _arg_choices _default = _arg_update.get_default() or _default _read_fn, _set_fn = self.add_arg( _arg, default=_default, label=to_nice(_arg.name), choices=_arg_choices, label_width=opts.get('label_width') or self.label_width, update=_arg_update, browser=_browser.get(_arg.name)) self.read_settings_fns['def'][def_.name][_arg.name] = _read_fn self.set_settings_fns['def'][def_.name][_arg.name] = _set_fn # Add execute _icon = opts.get('icon') or get_def_icon(def_.name, set_=self.icon_set) _label = opts.get('label') or to_nice(def_.name) _col = opts.get('col') or self.base_col _exec_fn = get_exec_fn( def_=def_, read_arg_fns=self.read_settings_fns['def'][def_.name], disable_reload=_disable_reload, catch_error_=True, interface=self) _help_fn = get_help_fn(def_) _code_fn = get_code_fn(def_) self.add_execute(def_=def_, exec_fn=_exec_fn, help_fn=_help_fn, icon=_icon, label=_label, col=_col, code_fn=_code_fn) if not last_: self.add_separator()
def _ph_add_show_toolkits(parent, verbose=0): """Add show toolkits options. Args: parent (str): parent menu verbose (int): print process data """ _shows = cmds.menuItem(label='Shows', parent=parent, subMenu=True, image=icons.EMOJI.find('Top Hat', catch=True)) _shows_dir = File(shows.__file__).parent() # Get list of toolkits _toolkits = [] for _py in _shows_dir.find(extn='py', depth=1, type_='f'): _file = PyFile(_py) if _file.basename.startswith('_'): continue try: _mod = _file.get_module(catch=True) except ImportError: continue if not _mod: continue _toolkits.append((_file, _file.basename)) for _dir in _shows_dir.find(depth=1, type_='d'): _toolkit = PyFile('{}/toolkit.py'.format(_dir)) if Dir(_dir).filename.startswith('_'): continue lprint(' - ADDING DIR', _dir, verbose=verbose) try: _mod = _toolkit.get_module(catch=True) except ImportError: continue if not _mod: continue _name = _toolkit.parent().filename _toolkits.append((_toolkit, _name)) _toolkits.sort(key=operator.itemgetter(1)) lprint('FOUND TOOLKITS', verbose=verbose) # Build show toolkit buttons for _toolkit, _name in _toolkits: lprint(' - ADDING TOOLKIT', _name, verbose=verbose) _mod = _toolkit.get_module() _rand = str_to_seed(_name) _icon = getattr(_mod, 'ICON', _rand.choice(icons.ANIMALS)) _label = getattr(_mod, 'LABEL', to_nice(_name)) _title = '{} tools'.format(_label) _cmd = '\n'.join([ 'import {py_gui} as py_gui', '_path = "{file}"', '_title = "{title}"', 'py_gui.MayaPyGui(_path, title=_title, all_defs=True)', ]).format(py_gui=py_gui.__name__, file=_toolkit.path, title=_title) cmds.menuItem(command=_cmd, image=_icon, label=_label, parent=_shows) _btn_label = getattr(_mod, 'BUTTON_LABEL', _label) _btn = _add_psyhive_btn(label=_btn_label, cmd=None, icon=_icon, tooltip=_title) py_gui.MayaPyShelfButton(mod=_mod, parent='PsyHive', image=_icon, label=_label, button=_btn)
def test_to_nice(self): assert to_nice('_get_flex_opts') == 'Get flex opts'
def _help_fn(*args): del args qt.notify( def_.get_docs().text, title='Help - '+to_nice(def_.name), icon=icons.EMOJI.find('Info'))