Adding UI buttons to Maya viewport 2.0

Hello everyone!

Stuck with little problem, searching for a way to add some ui buttons to the maya viewport 2.0,
can’t find some example or documentation, maybe some one did something same?

Quick update!
Finally found a way to parent button into wrapped around qwidget viewport, it is seems working!

Any sharable code snippet for posterity?

Yes, definetly, this will create separate QDialog with viewport inside of it, over viewport wil be red test button:

from PySide2 import QtWidgets
import shiboken2
import maya.cmds as cmds
import maya.OpenMayaUI as OpenMayaUI

class TestDialog(QtWidgets.QDialog):
    def __init__(self, parent, **kwargs):
        super(TestDialog, self).__init__(parent, **kwargs)
        self.resize(600, 800)
        self.verticalLayout = QtWidgets.QVBoxLayout(self)

        # need to set a name so it can be referenced by maya node path

        # First use SIP to unwrap the layout into a pointer
        # Then get the full path to the UI in maya as a string
        layout = OpenMayaUI.MQtUtil.fullName(long(shiboken2.getCppPointer(self.verticalLayout)[0]))

        paneLayoutName = cmds.paneLayout()
        for model_panel in cmds.getPanel(type="modelPanel"):
            if cmds.modelEditor(model_panel, q=1, av=1):
                cmds.modelEditor(model_panel, e=1, allObjects=False)
        # Find a pointer to the paneLayout that we just created
        ptr = OpenMayaUI.MQtUtil.findControl(paneLayoutName)

        # Wrap the pointer into a python QObject
        self.paneLayout_widget = shiboken2.wrapInstance(long(ptr), QtWidgets.QWidget)

        self.camera_transform_name = "persp"
        self.cameraName = cmds.listRelatives(self.camera_transform_name, children=True, shapes=True)[0]

        self.modelPanelName = "customModelPanel"
        self.modelPanelName = cmds.modelPanel(self.modelPanelName, label="TestDialog", cam=self.cameraName)

        cmds.modelEditor(self.modelPanelName, e=1, rnm="vp2Renderer", displayLights="all", displayAppearance="smoothShaded",
                         cameras=False, wireframeOnShaded=True, shadows=True, headsUpDisplay=False)

        # Find a pointer to the modelPanel that we just created
        ptr = OpenMayaUI.MQtUtil.findControl(self.modelPanelName)

        # Wrap the pointer into a python QObject
        self.modelPanel = shiboken2.wrapInstance(long(ptr), QtWidgets.QWidget)

        self.test_button = QtWidgets.QPushButton("Test Button!", self.modelPanel)
        self.test_button.move(440, 400)

        # add our QObject reference to the paneLayout to our layout

    def show(self):
        super(TestDialog, self).show()

    def showEvent(self, event):
        super(TestDialog, self).showEvent(event)
        # maya can lag in how it repaints UI. Force it to repaint
        # when we show the window.

    def get_main_maya_window(self):

        Returns maya main window pointer wrapped around QWidget

        maya_main_window_pointer = OpenMayaUI.MQtUtil.mainWindow()
        maya_qtwidget_main_window = wrapInstance(long(maya_main_window_pointer), QWidget)

        return maya_qtwidget_main_window

def launch_window():
    # pointer to the maya main window
    maya_main_window = OpenMayaUI.MQtUtil.mainWindow()

    # use sip to wrap the pointer into a QObject
    maya_window_wrapped = shiboken2.wrapInstance(long(maya_main_window), QtWidgets.QWidget)
    d = TestDialog(maya_window_wrapped)

    return d


1 Like

That is a magnificent hack :slight_smile:

Another option is the hudButton command.

1 Like

@golubevcg just a note, in Python 3 (Maya 2022) long is depreciated and folded into int. Thank you for that snippet!

Thank you for your reply! It is a great note,
I’m sorry for the possible inconvenience, it’s just in the studio we still have 2017 Maya, so no python 3 yet:)

Just in case. Maya 2022.1 does have UI Overlays.

Thank’s for your reply!
Looks amazing, can’t wait to work with this!