[Maya Python] Display .ui created by Qt Designer in Maya

Introduction

[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.

Prepare the UI file

For the time being, I made a UI like this. UI.JPG

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 a note of a little jam

** 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. ui01.jpg As shown in the figure, if the central widget is set to break Layout, it will not be displayed at all. ui02.jpg 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. ui03.jpg Initial startup diagram after layout setting

Prepare a program to display the window

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. ui04.JPG

Program details

Load UI

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`.

Get value from 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. ui05.JPG Setting in this way is convenient because you can change the UI file without rewriting it programmatically.

Place UI

self.setCentralWidget(self.UI) Place the UI.

Create button behavior

self.UI.refreshPushButton.clicked.connect(self.refreshPushButton) Connect the function when clicked to the button whose objectName is refreshPushButton. ui06.JPG 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.

in conclusion

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

reference

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

[Maya Python] Display .ui created by Qt Designer in Maya
Create Qt designer parts in Python (PyQt)
UI Automation in Python
mayapy --Python in Maya
Display UTM-30LX data in Python
UI Automation Part 2 in Python
Sort by date in python
Waveform display of audio in Python
Display characters like AA in python
Display LaTeX notation formulas in Python, matplotlib
Display a list of alphabets in Python 3
Display Python 3 in the browser with MAMP
How to display multiplication table in python
Read the file line by line in Python
Automate jobs by manipulating files in Python
Read the file line by line in Python
Python interpreter in Maya, Houdini, blender, Nuke
[Python] Progress display by progress bar using tqdm
Display pyopengl in a browser (python + eel)
Common mock by moto in Python Unittest
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
How to display Hello world in python
I created a password tool in Python.
Alignment algorithm by insertion method in Python
Scene recognition by GIST features in Python
Make the library created by Eigen in C ++ available from Python with Boost.Numpy.
[python] How to display list elements side by side
The first web app created by Python beginners
Receive and display HTML form data in Python
[Python] Display the Altair legend in the plot area
Split camel case string word by word in Python
I want to display the progress in Python!