Source code for bookmarks.maya.contextmenu

"""Maya-specific context menus.

"""
import collections
import functools

from PySide2 import QtCore

try:
    from maya import cmds
except ImportError:
    raise ImportError('Could not find the Maya modules.')

from .. import common
from .. import ui
from .. import contextmenu
from . import actions
from . import export


[docs]class PluginContextMenu(contextmenu.BaseContextMenu): """Maya plugin context menu. """
[docs] def setup(self): """Creates the context menu. """ self.apply_bookmark_settings_menu() self.separator() self.save_menu() self.separator() self.open_import_scene_menu() self.separator() self.export_menu() self.separator() self.import_camera_menu() self.shader_tool_menu() self.separator() self.viewport_presets_menu() self.capture_menu()
[docs] def apply_bookmark_settings_menu(self): """Apply settings action. """ server = common.active('server') job = common.active('job') root = common.active('root') asset = common.active('asset') if not all((server, job, root, asset)): return self.menu[contextmenu.key()] = { 'text': 'Apply scene settings...', 'icon': ui.get_icon('check', color=common.color(common.color_green)), 'action': actions.apply_settings }
[docs] def save_menu(self): """Save scene action. """ if not all(common.active('asset', args=True)): return scene = QtCore.QFileInfo(cmds.file(query=True, expandName=True)) self.menu[contextmenu.key()] = { 'text': 'Save Scene...', 'icon': ui.get_icon('add_file', color=common.color(common.color_green)), 'action': lambda: actions.save_scene(increment=False) } if common.get_sequence(scene.fileName()): self.menu[contextmenu.key()] = { 'text': 'Incremental Save...', 'icon': ui.get_icon('add_file'), 'action': lambda: actions.save_scene(increment=True) }
[docs] def open_import_scene_menu(self): """Scene open actions. """ if not self.index.isValid(): return path = self.index.data(common.PathRole) path = common.get_sequence_end_path(path) file_info = QtCore.QFileInfo(path) _s = file_info.suffix().lower() if _s not in ('ma', 'mb', 'abc'): return maya_pixmap = ui.get_icon('maya', color=None) maya_reference_pixmap = ui.get_icon('maya_reference', color=None) self.menu[contextmenu.key()] = { 'text': 'Open', 'icon': maya_pixmap, 'action': functools.partial( actions.open_scene, file_info.filePath() ) } self.menu[contextmenu.key()] = { 'text': 'Import', 'icon': maya_pixmap, 'action': functools.partial( actions.import_scene, file_info.filePath(), reference=False ) } self.menu[contextmenu.key()] = { 'text': 'Reference', 'icon': maya_reference_pixmap, 'action': functools.partial( actions.import_scene, file_info.filePath(), reference=True ) }
[docs] def export_menu(self): """Cache export actions. """ k = contextmenu.key() self.menu[k] = { 'text': 'Export...', 'icon': ui.get_icon('set', color=None), 'action': export.show }
[docs] def shader_tool_menu(self): """Shader tool action. """ k = contextmenu.key() self.menu[k] = { 'text': 'Show Shader Tool', 'action': actions.show_shader_tool }
[docs] def import_camera_menu(self): """Import camera template action. """ k = contextmenu.key() self.menu[k] = { 'text': 'Import Camera Template', 'action': actions.import_camera_preset }
[docs] def viewport_presets_menu(self): """Viewport display preset action. """ from . import viewport k = 'Viewport Presets' self.menu[k] = collections.OrderedDict() self.menu[f'{k}:icon'] = ui.get_icon('image') for _k in viewport.presets: self.menu[k][contextmenu.key()] = { 'icon': ui.get_icon('image'), 'text': _k, 'action': functools.partial(actions.apply_viewport_preset, _k) }
[docs] def capture_menu(self): """Capture viewport action. """ k = 'Capture Viewport' self.menu[k] = collections.OrderedDict() self.menu[f'{k}:icon'] = ui.get_icon('capture_thumbnail') width = cmds.getAttr("defaultResolution.width") height = cmds.getAttr("defaultResolution.height") def _size(n): return int(int(width) * n), int(int(height) * n) for n in (1.0, 0.5, 0.25, 1.5, 2.0): w, h = _size(n) self.menu[k][f'capture{n}'] = { 'text': f'Capture | @{n} | {w}x{h}px', 'action': functools.partial(actions.capture_viewport, size=n), 'icon': ui.get_icon('capture_thumbnail'), }
[docs] def show_window_menu(self): """Show plugin window action. """ if not hasattr(self.parent(), 'clicked'): return self.menu['show'] = { 'icon': ui.get_icon('icon_bw', color=None), 'text': f'Toggle {common.product.title()}', 'action': self.parent().clicked.emit }
[docs]class MayaButtonWidgetContextMenu(PluginContextMenu): """The context-menu associated with the BrowserButton.""" def __init__(self, parent=None): super().__init__( QtCore.QModelIndex(), parent=parent )
[docs]class MayaWidgetContextMenu(PluginContextMenu): """Context menu associated with :class:`MayaWidget`. """
[docs] @common.error @common.debug def setup(self): """Creates the context menu. """ self.apply_bookmark_settings_menu() self.separator() self.save_menu() self.separator() self.open_import_scene_menu() self.separator() self.export_menu() self.separator() self.import_camera_menu() self.shader_tool_menu() self.separator() self.viewport_presets_menu() self.capture_menu()