Skip to content

Instantly share code, notes, and snippets.

@fredrikaverpil
Last active June 26, 2022 11:45
Show Gist options
  • Save fredrikaverpil/b76bd91b9924435465ecb1b2270c1a9d to your computer and use it in GitHub Desktop.
Save fredrikaverpil/b76bd91b9924435465ecb1b2270c1a9d to your computer and use it in GitHub Desktop.
Window floating atop Maya 2017
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()
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()
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()
@fredrikaverpil
Copy link
Author

Hi,
Sorry, I don't have access to Maya. I would advise you to get in touch with the developers through the beta program for the best support.

@sohamp28
Copy link

sohamp28 commented Mar 27, 2020

That works perfectly fine for me. Thanks a lot for the help.
but i don't want to run this whole code in maya, i want to source the code in maya.
can you please help me with that.

I am just creating new method and running it, but it gives me error.
below is the exact error i am getting.

import PySide
pySIdeTest.openWin()

# Error: TypeError: file /Users/soham/Library/Preferences/Autodesk/maya/scripts/spDev/rigging/pyQt_tests/spTransferWeightsUI.py line 20: 'PySide2.QtWidgets.QWidget.setSizePolicy' called with wrong argument types:
  PySide2.QtWidgets.QWidget.setSizePolicy(PySide.QtGui.QSizePolicy)
Supported signatures:
  PySide2.QtWidgets.QWidget.setSizePolicy(PySide2.QtWidgets.QSizePolicy)
  PySide2.QtWidgets.QWidget.setSizePolicy(PySide2.QtWidgets.QSizePolicy.Policy, PySide2.QtWidgets.QSizePolicy.Policy) # `

Thanks in advance.
Soham

@fredrikaverpil
Copy link
Author

fredrikaverpil commented Mar 27, 2020

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 the PYTHONPATH 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment