Created
September 3, 2018 23:30
-
-
Save Meatplowz/78cc12274661e0f42d0683ca809a76ef to your computer and use it in GitHub Desktop.
Test_UI.py Boilerplate code for a Maya Tool
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
""" | |
Author: Randall Hess [email protected] | |
Purpose: Boilerplate UI Code for Maya | |
Created: 8/5/2018 | |
""" | |
import maya.cmds as cmds | |
import maya.OpenMayaUI as OpenMayaUI | |
# importing Qt.py for 2015-2018 compatibility | |
# https://github.com/mottosso/Qt.py | |
#from Qt import QtGui, QtCore, QtWidgets, QtCompat | |
try: | |
# Pyside | |
from PySide import QtGui, QtCore, QtWidgets | |
except: | |
# Pyside2 | |
try: | |
from PySide2 import QtGui, QtCore, QtWidgets | |
except: | |
print "PySide2 not found" | |
# GLOBALS | |
WINDOW_TITLE = 'Test UI' | |
def get_maya_window(): | |
""" | |
Get the main Maya window for parenting purposes | |
*Arguments:* | |
* ``None`` | |
*Keyword Arguments:* | |
* ``None`` | |
*Returns:* | |
* ``ptr`` Pointer to Maya window | |
*Author:* | |
* randall.hess, [email protected], 08/05/2018 1:16:30 PM | |
""" | |
for widget in QtWidgets.QApplication.topLevelWidgets(): | |
if widget.objectName()=='MayaWindow': | |
return widget | |
raise Exception('Maya main window was not found!') | |
class MyLineEdit(QtWidgets.QLineEdit): | |
""" | |
Custom LineEdit widget | |
""" | |
def __init__(self,parent=None): | |
super(MyLineEdit, self).__init__(parent) | |
self.setText('MyLineEdit') | |
self.setStyleSheet('''QLineEdit {color: red; background-color: white }''') | |
class Test_UI(QtWidgets.QDialog): | |
def __init__( self, parent = get_maya_window(), *args): | |
""" | |
# Make sure you are starting from QDialog in QtDesigner, | |
Otherwise you will need to setup your class to inherit from you specified type | |
I.E. Test_UI(QtWidgets.QWidget) | |
Test_UI(QtWidgets.QDialog) | |
Test_UI(QtWidgets.QMainWindow) | |
*Arguments:* | |
* ``None`` | |
*Keyword Arguments:* | |
* ``parent`` Maya Window | |
*Returns:* | |
* ``None`` | |
*Author:* | |
* randall.hess, [email protected], 08/08/2018 1:27:15 PM | |
""" | |
super( Test_UI, self ).__init__(parent) | |
self.closeExistingWindow() | |
self.setWindowFlags(self.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint) | |
self.setAttribute(QtCore.Qt.WA_DeleteOnClose) | |
#self.setFocusPolicy(QtCore.Qt.FocusPolicy.StrongFocus) | |
# this will build the base ui from the QtDesigner Code .ui | |
self.setupUi(self) | |
self.setWindowTitle(WINDOW_TITLE) | |
# un-comment to make the window not resizeable | |
#self.setFixedSize(self.size()) | |
# setup custom ui | |
self.__init_ui__() | |
# setup connections | |
self.__init_connections__() | |
# setup variables | |
self.__init_variables__() | |
def __init_ui__(self): | |
""" | |
Here we will do any custom modifications to the UI after the QtDesigner code | |
*Arguments:* | |
* ``None`` | |
*Keyword Arguments:* | |
* ``parent`` Maya Window | |
*Returns:* | |
* ``None`` | |
*Author:* | |
* randall.hess, [email protected], 08/08/2018 1:27:15 PM | |
""" | |
self.new_layout = QtWidgets.QHBoxLayout() | |
self.new_layout.setContentsMargins(20, 0, 20, 0) | |
self.new_layout.setSpacing(0) | |
self.new_layout.setObjectName("new_layout") | |
self.new_button = QtWidgets.QPushButton("New Button") | |
self.new_layout.addWidget(self.new_button) | |
self.main_layout.addLayout(self.new_layout) | |
def __init_connections__(self): | |
""" | |
Setup connections for the UI | |
*Arguments:* | |
* ``None`` | |
*Keyword Arguments:* | |
* ``None`` | |
*Returns:* | |
* ``None`` | |
*Author:* | |
* randall.hess, [email protected], 08/08/2018 1:27:15 PM | |
""" | |
pass | |
def __init_variables__(self): | |
""" | |
Setup variables for the UI | |
*Arguments:* | |
* ``None`` | |
*Keyword Arguments:* | |
* ``None`` | |
*Returns:* | |
* ``None`` | |
*Author:* | |
* randall.hess, [email protected], 08/08/2018 1:27:15 PM | |
""" | |
pass | |
def closeExistingWindow(self): | |
""" | |
Handle proper clean up of removing the dock control widget | |
*Arguments:* | |
* ``None`` | |
*Keyword Arguments:* | |
* ``None`` | |
*Returns:* | |
* ``None`` | |
*Author:* | |
* randall.hess, [email protected], 08/05/2018 2:40:06 PM | |
""" | |
for widget in QtWidgets.QApplication.topLevelWidgets(): | |
try: | |
if widget.__class__.__name__ == self.__class__.__name__: | |
widget.close() | |
except: | |
pass | |
def setupUi(self, Dialog): | |
pass | |
def retranslateUi(self, Dialog): | |
pass | |
def run(): | |
""" | |
Opens the window for the Test UI | |
*Arguments:* | |
* ``none `` | |
*Keyword Arguments:* | |
* ``none`` | |
*Returns:* | |
* ``none`` | |
*Author:* | |
* Randall.Hess | |
""" | |
global test_ui_window | |
# try to close an existing window | |
try: | |
test_ui_window.close() | |
test_ui_window.deleteLater() | |
except: | |
pass | |
test_ui_window = Test_UI() | |
test_ui_window.show() | |
if __name__ == '__main__': | |
try: | |
run() | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment