From 1d7a43e5ca973f83277dc6fe17e6a20b66de696b Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Tue, 12 Sep 2017 10:57:00 +0200 Subject: [PATCH] Add `popup()` function for the menu so we can trigger it to pop-up --- colorbleed/maya/menu.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/colorbleed/maya/menu.py b/colorbleed/maya/menu.py index 330507d90e..236879e061 100644 --- a/colorbleed/maya/menu.py +++ b/colorbleed/maya/menu.py @@ -2,7 +2,7 @@ import sys import os import logging -from avalon.vendor.Qt import QtWidgets, QtCore +from avalon.vendor.Qt import QtWidgets, QtCore, QtGui import maya.cmds as cmds @@ -12,6 +12,15 @@ self._menu = "colorbleed" log = logging.getLogger(__name__) +def _get_menu(): + """Return the menu instance if it currently exists in Maya""" + + app = QtWidgets.QApplication.instance() + widgets = dict((w.objectName(), w) for w in app.allWidgets()) + menu = widgets.get(self._menu) + return menu + + def deferred(): import scriptsmenu.launchformaya as launchformaya @@ -37,12 +46,11 @@ def deferred(): def uninstall(): - log.info("Attempting to uninstall ..") - app = QtWidgets.QApplication.instance() - widgets = dict((w.objectName(), w) for w in app.allWidgets()) - menu = widgets.get(self._menu) + menu = _get_menu() if menu: + log.info("Attempting to uninstall ..") + try: menu.deleteLater() del menu @@ -55,3 +63,12 @@ def install(): uninstall() # Allow time for uninstallation to finish. cmds.evalDeferred(deferred) + + +def popup(): + """Pop-up the existing menu near the mouse cursor""" + menu = _get_menu() + + cursor = QtGui.QCursor() + point = cursor.pos() + menu.exec_(point)