I can’t speak for Linux behavior, but to get Maya tools set up correctly on Windows I have to do 2 things:
- Parent the tool dialog to the Maya main window
- Set the dialog’s window flags to either
QtCore.Qt.Window
or QtCore.Qt.Tool
, depending on the look you want. I always prefer the former for anything other than small confirmation dialogs.
My code usually looks something like this
from PySide2 import QtCore, QtGui, QtWidgets
import maya.OpenMayaUI as omui
from shiboken2 import wrapInstance
def getMayaMainWindow():
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance(long(main_window_ptr), QtWidgets.QMainWindow)
class MyDialog(QtWidgets.QDialog):
def __init__(self, parent=getMayaMainWindow()):
super(MyDialog, self).__init__(parent)
self.setWindowFlags(QtCore.Qt.Window)
if __name__ == '__main__':
try:
dialog.deleteLater()
except:
pass
dialog = MyDialog()
dialog.show()
I’ve not had any issues with custom dialogs since I started setting them up this way.
If you have issues with the dialog not appearing at the center of the screen, you can center it when showing the UI. I prefer to override QDialog’s show method for this, since it will have to happen after any widget creation that can change the dimensions of the dialog, and that include’s QtDialog’s show()
method. Here’s an example:
# you can use this method or max's method
def centerWindowOn(childWindow, parentWindow):
childWindow.move(parentWindow.window().frameGeometry().topLeft() +
parentWindow.window().rect().center() -
childWindow.rect().center())
class MyDialog(QtWidgets.QDialog):
# def __init__() ...
def show(self):
super(MyDialog, self).show()
# if you don't do this last, you won't get the result you expect!
centerWindowOn(self, self.parent())
I find it very useful to create a library for functions like getMayaMainWindow
and centerWindowOn
since they tend to be used very often. It also cuts down on the number of import statements you have to write to get something simple working.