[Maya Python] Crush the contents of the script 1, 2, [3] The script written in (https://qiita.com/elloneil/items/3f4a47501204af16cd0a) is fine with a small script, but it takes a lot of effort to create a complicated UI. So I'm going to use QtDesigner to assemble the UI. As a CG artist, it would be nice to have such an easy-to-understand tool rather than worrying about scripts. This time, I'd like to make it up to the point where the UI is displayed in Maya and simple operations are performed in order to get a feel for it by touching it. Since there are some methods such as PySide, PySide2, and PyQt5, I will write it as a memorandum of my own. The operating environment is Maya 2019,2020.
For the time being, I made a UI like this.
Refer to this site for how to make it.
GUI programming with PyQt5 and python3: Practice [0] Chapter 5 Let's use Qt Designer Create UI with QtDesigner
** Make sure to set the layout ** If you just place it as it is, the UI will not follow even if you change the window size, or the window will be too small when you launch the window. As shown in the figure, if the central widget is set to break Layout, it will not be displayed at all. After setting the layout and adjusting the window size, set the minimum size with Size Constraints. Then you can create a window that fits properly. The UI will follow you even if you change the window size. Initial startup diagram after layout setting
For the time being, the full code
# -*- coding: utf-8 -*-
from PySide2 import QtWidgets
from PySide2.QtUiTools import QUiLoader
from maya.app.general.mayaMixin import MayaQWidgetBaseMixin
#Specify the path directly
UIFILEPATH = 'C:/Users/YN/Desktop/testUI.ui'
##Class that creates MainWindow
class MainWindow(MayaQWidgetBaseMixin, QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
#Specify the UI path
self.UI = QUiLoader().load(UIFILEPATH)
#Get window title from UI
self.setWindowTitle(self.UI.windowTitle())
#Center widget
self.setCentralWidget(self.UI)
#Connect button
self.UI.refreshPushButton.clicked.connect(self.refreshPushButton)
def refreshPushButton(self):
print ('refresh')
##Start MainWindow
def main():
window = MainWindow()
window.show()
if __name__ == '__main__':
main()
Copy and paste into a script editor Set ʻUIFILEPATH` to the ui path you just created. And if you execute it, you can check the created UI in Maya for the time being.
from PySide2.QtUiTools import QUiLoader
To load the UI, import QUiLoader
from QtUiTools
.
self.UI = QUiLoader().load(UIFILEPATH)
ʻRead UIFILEPATH and instantiate it as
self.UI`.
self.setWindowTitle(self.UI.windowTitle())
If you do not specify the window title, a window will be created with an appropriate name, so get it from the UI file that created the window title. Of course, you can also enter string
here.
self.UI.windowTitle()
If you specify the property name in the UI, you can get the value set there.
Setting in this way is convenient because you can change the UI file without rewriting it programmatically.
self.setCentralWidget(self.UI)
Place the UI.
self.UI.refreshPushButton.clicked.connect(self.refreshPushButton)
Connect the function when clicked to the button whose objectName is refreshPushButton
.
Specify the object name in Qt Designer like this.
def refreshPushButton(self):
print ('refresh')
Added a function to print refresh when the button is pressed. Actually, write the function you want to set for this button here.
At first, I was in the dark asking where to start, so I hope it helps. For the time being, the UI is displayed quickly in Maya, and if you understand that this is the case, I think you can make the UI more and more complicated.
Maya's PySide 2 was confused because there was an update from PySide on the way and the description method changed. It's even more complicated because it hits searches such as similar PyQt5. ..
It was very helpful. Thank you very much.
GUI programming with PyQt5 and python3: Practice [0] Chapter 5 Let's use Qt Designer Create UI with QtDesigner
Recommended Posts