Last active
June 26, 2022 11:45
-
-
Save fredrikaverpil/b76bd91b9924435465ecb1b2270c1a9d to your computer and use it in GitHub Desktop.
Window floating atop Maya 2017
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PySide2 import QtWidgets, QtCore | |
def _maya_main_window(): | |
"""Return Maya's main window""" | |
for obj in QtWidgets.qApp.topLevelWidgets(): | |
if obj.objectName() == 'MayaWindow': | |
return obj | |
raise RuntimeError('Could not find MayaWindow instance') | |
class MyWindow(QtWidgets.QMainWindow): | |
def __init__(self, parent): | |
super(MyWindow, self).__init__(parent) | |
self.setWindowTitle('My window') # Window title | |
self.setObjectName('MyWindowObj') # Window object name | |
self.setWindowFlags(QtCore.Qt.Window) # Window type | |
# Makes Maya perform magic which makes the window stay | |
# on top in OS X and Linux. As an added bonus, it'll | |
# make Maya remember the window position | |
self.setProperty("saveWindowPref", True) | |
# Widgets setup | |
self.widget = QtWidgets.QWidget() | |
self.layout = QtWidgets.QVBoxLayout() | |
self.button = QtWidgets.QPushButton('Yoghurt') | |
self.layout.addWidget(self.button) | |
self.widget.setLayout(self.layout) | |
self.setCentralWidget(self.widget) | |
window = MyWindow(parent=_maya_main_window()) | |
window.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PySide2 import QtWidgets, QtCore | |
from shiboken2 import wrapInstance | |
from maya import OpenMayaUI as omui | |
class MyWindow(QtWidgets.QMainWindow): | |
def __init__(self, parent): | |
super(MyWindow, self).__init__(parent) | |
self.setWindowTitle('My window') # Window title | |
self.setObjectName('MyWindow') # Window object name | |
self.setWindowFlags(QtCore.Qt.Window) # Window type | |
# Makes Maya perform magic which makes the window stay | |
# on top in OS X and Linux. As an added bonus, it'll | |
# make Maya remember the window position | |
self.setProperty("saveWindowPref", True) | |
# Widgets setup | |
self.widget = QtWidgets.QWidget() | |
self.layout = QtWidgets.QVBoxLayout() | |
self.button = QtWidgets.QPushButton('Yoghurt') | |
self.layout.addWidget(self.button) | |
self.widget.setLayout(self.layout) | |
self.setCentralWidget(self.widget) | |
mainWindowPtr = omui.MQtUtil.mainWindow() | |
mainWindow = wrapInstance(long(mainWindowPtr), QtWidgets.QMainWindow) | |
window = MyWindow(parent=mainWindow) | |
window.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PySide2 import QtWidgets, QtCore | |
from maya import OpenMayaUI as omui | |
class MyWindow(QtWidgets.QMainWindow): | |
def __init__(self, parent): | |
super(MyWindow, self).__init__() | |
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) # Stays on top of *all* windows in the desktop | |
self.widget = QtWidgets.QWidget() | |
self.layout = QtWidgets.QVBoxLayout() | |
self.button = QtWidgets.QPushButton('Yoghurt') | |
self.layout.addWidget(self.button) | |
self.widget.setLayout(self.layout) | |
self.setCentralWidget(self.widget) | |
window = MyWindow(parent=omui.MQtUtil.mainWindow()) | |
window.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have no idea what you mean about wanting to source the code but not running it. Place your code somewhere Maya recognizes scripts, like in My documents/Maya/scripts or inside the Maya installation's site-packages. Or add a custom path to
sys.path
(which effectively adds the path to thePYTHONPATH
environment variable).You have not provided your code so I can't help you with your error other than that you are not calling
PySide2.QtWidgets.QWidget.setSizePolicy
with a valid argument, as per the error message. Also, judging by the error, you are mixing PySide and PySide2 which you cannot do. Use either PySide or PySide2, depending on which version of Maya you are using. If you need to write code which should work in both older PySide and PySide2, consider using Qt.py.