Add popup() function for the menu so we can trigger it to pop-up

This commit is contained in:
Roy Nieterau 2017-09-12 10:57:00 +02:00
parent 4a09ffd0f1
commit 1d7a43e5ca

View file

@ -2,7 +2,7 @@ import sys
import os import os
import logging import logging
from avalon.vendor.Qt import QtWidgets, QtCore from avalon.vendor.Qt import QtWidgets, QtCore, QtGui
import maya.cmds as cmds import maya.cmds as cmds
@ -12,6 +12,15 @@ self._menu = "colorbleed"
log = logging.getLogger(__name__) 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(): def deferred():
import scriptsmenu.launchformaya as launchformaya import scriptsmenu.launchformaya as launchformaya
@ -37,12 +46,11 @@ def deferred():
def uninstall(): def uninstall():
log.info("Attempting to uninstall ..") menu = _get_menu()
app = QtWidgets.QApplication.instance()
widgets = dict((w.objectName(), w) for w in app.allWidgets())
menu = widgets.get(self._menu)
if menu: if menu:
log.info("Attempting to uninstall ..")
try: try:
menu.deleteLater() menu.deleteLater()
del menu del menu
@ -55,3 +63,12 @@ def install():
uninstall() uninstall()
# Allow time for uninstallation to finish. # Allow time for uninstallation to finish.
cmds.evalDeferred(deferred) 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)