[PYTHON] [Complement] [PySide] Let's play with Qt Designer

This article is taken care of by studying PySide (studying QtDesigner accompanying it)

** [PySide] Let's play with QtDesigner ** @ * mtazmi-Lab * http://mtazmi-lab.blogspot.jp/2013/01/QtDesiner.html

About In response to the UI created here, I would like to write a script that acquires information and publishes something.

Thank you mtazmi, thank you for your help m (_ _) m

Before accepting

What do you mean? This article is a wonderful article about ** Creating UI with QtDesigner **. The articles before and after that also explain examples using PySide and Qt Designer.

However, the UI itself that appears in the article did not have a working part, so I would like to write a small story that complements that point while respecting MAX, I thought.

Supplement

The articles before and after are here. Please come together

[PySide] Let's play with PySide http://mtazmi-lab.blogspot.jp/2012/12/pythonPySide01.html

[PySide] Let's take various values from the UI http://mtazmi-lab.blogspot.jp/2013/01/pysidegetvalue.html

The former is relatively close to this line, The latter is a scratch by PySide without using Qt Designer

UI preparation

Let's create a UI according to the original article. By the way, Qt Designer is under bin in the Maya installation folder. ** designer.exe ** is that.

C:\Program Files\Autodesk\Maya{ver}\bin\designer.exe

Shinchoku 1 01.png

Shinchoku 2 02.png

I was able to Save the .ui file in a good location. The file name is mayapyAdCl.ui here.

Moving part

I will make a moving part.

First of all, here is the script of the article

# -*- coding: utf-8 -*-
 
import os
import sys
 
from PySide import QtCore, QtGui
from PySide.QtUiTools import QUiLoader
 
CURRENT_PATH = os.path.dirname(__file__)
 
#----------------------------------------------------------------------------
##Class to make GUI
class GUI(QtGui.QMainWindow):
 
    def __init__(self, parent=None):
        super(GUI, self).__init__(parent)
        loader = QUiLoader()
        uiFilePath = os.path.join(CURRENT_PATH, 'mayapyAdCl.ui')
        self.UI = loader.load(uiFilePath)
        self.setCentralWidget(self.UI)
 
#------------------------------------------------------------------------------ 
##GUI launch
def main():
    app = QtGui.QApplication(sys.argv)
    app.setStyle('plastique')    #← Specify the style here
    ui = GUI()
    ui.show()
    sys.exit(app.exec_())
 
 
if __name__ == '__main__':
    main()
 
#-----------------------------------------------------------------------------
# EOF
#-----------------------------------------------------------------------------

Save this in the same place as the previous .ui. Name it mayapyAdCl.py.

Added moving parts

Refer to the previous article of the original article, Also, while looking at the PySide documentation, try the following.

# -*- coding: utf-8 -*-

from PySide import QtCore, QtGui
from PySide.QtUiTools import QUiLoader

import maya.cmds as cmds
from maya.app.general.mayaMixin import MayaQWidgetBaseMixin

CURRENT_PATH = os.path.dirname(__file__)

#----------------------------------------------------------------------------
##Class to make GUI
class GUI(MayaQWidgetBaseMixin, QtGui.QMainWindow):
 
    def __init__(self, parent=None):
        super(GUI, self).__init__(parent)
         
        #ui file loading
        loader = QUiLoader()
        uiFilePath = os.path.join(CURRENT_PATH, 'mayapyAdCl.ui')
        self.UI = loader.load(uiFilePath)
        self.setCentralWidget(self.UI)
         
        #Specify the title of the window
        self.setWindowTitle(u'It's a UI sample')
        #Specify the size of the window
        self.resize(256, 128)
         
        #Add signal to UI element
        self.setSignals()
 
    #----------------------------------------
    ##Signal registration
    # @param None
    # @return None
    def setSignals(self):
        #Behavior when the apply button is pressed
        self.UI.apply_btn.clicked.connect(self.doIt)
        #When you press the apply and close button(ry
        self.UI.applyClose_btn.clicked.connect(self.doIt_close)
        #close button(ry
        self.UI.close_btn.clicked.connect(self.close)

    ##Lock when checked
    def doIt(self):
        chbxState = {
                     'tx':self.UI.tx_chbx.isChecked(),
                     'ty':self.UI.ty_chbx.isChecked(),
                     'tz':self.UI.tz_chbx.isChecked(),
                     'rx':self.UI.rx_chbx.isChecked(),
                     'ry':self.UI.ry_chbx.isChecked(),
                     'rz':self.UI.rz_chbx.isChecked(),
                     'sx':self.UI.sx_chbx.isChecked(),
                     'sy':self.UI.sy_chbx.isChecked(),
                     'sz':self.UI.sz_chbx.isChecked()
                    }
        
        objs = cmds.ls(sl=True)
        if not objs:
            print 'no object selected...'
            return
            
        for obj in objs:
            for ch in chbxState:
                attr = '.'.join([obj,ch])
                cmds.setAttr(attr,lock=False)
                if chbxState[ch]:
                    cmds.setAttr(attr,lock=True)

    ##doIt and then close
    def doIt_close(self):
        self.doIt()
        self.close()



#------------------------------------------------------------------------------ 
##GUI launch
def main():
    app = QtGui.QApplication.instance()
    ui = GUI()
    ui.show()
    
    return ui
 
 
if __name__ == '__main__':
    main()
 
#-----------------------------------------------------------------------------
# EOF
#-----------------------------------------------------------------------------

An example is to get if a checkbox is in and lock the channel if it is.

Run

import sys
sys.path.append('path/to/your/scripts')

import mayapyAdCl

myWin = mayapyAdCl.main()

Through the path where you put the .ui and .py Import and execute.

Remarks

QApplication

The original article is the content to be thrown to python.exe, but since we are thinking of executing it on Maya here, it is necessary to change the place where the instance of QApplication is taken.

In particular…

app = QtGui.QApplication(sys.argv)
# Error: A QApplication instance already exists.
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# RuntimeError: A QApplication instance already exists. # 

Because such an error will occur

app = QtGui.QApplication.instance()

I will rewrite it like this. At the same time, delete or comment out sys.exit (app.exec_ ()) here.

mayaMixin

I have to parent and child the created window to the main window of Maya You will be a shy child who hides behind you.

In the past, it was customary to use shiboken to take a pointer, but If you use mayaMixin, you can omit the description.

from maya.app.general.mayaMixin import MayaQWidgetBaseMixin

Import

class GUI(MayaQWidgetBaseMixin, QtGui.QMainWindow):

Inherit.

reference: http://help.autodesk.com/view/MAYAUL/2017/JPN/?guid=__files_GUID_66ADA1FF_3E0F_469C_84C7_74CEB36D42EC_htm

main return

I'm adding return ui to the main function. If you receive this, you will be able to debug while creating the UI.

close

To close the window Use the method that comes with QMainWindow.

I use it in the setSignals and doIt_close functions, With the same glue as other UI elements such as self.UI.tx_chbx Be careful not to inadvertently write self.UI.close ().

in conclusion

It is a supplement to the wonderfulness of the original article.

I hope it helps someone. (Please let me know if there is something strange such as not working ...)

Next is mono_g's article ** "Kneading Maya Materials" **. http://qiita.com/mono-g/items/520f4967356456eb4cc5

Recommended Posts

[Complement] [PySide] Let's play with Qt Designer
Let's play with 4D 4th
Let's play with Amedas data-Part 1
Let's play with Amedas data-Part 4
Let's play with Amedas data-Part 3
Let's play with Amedas data-Part 2
Let's play with Excel with Python [Beginner]
[Introduction to WordCloud] Let's play with scraping ♬
Play with Prophet
Python hand play (let's get started with AtCoder?)
[Piyopiyokai # 1] Let's play with Lambda: Creating a Lambda function
Play with PyTorch
Play with 2016-Python
Create a color bar with Python + Qt (PySide)
Create a color-specified widget with Python + Qt (PySide)
Play with CentOS 8
Play with Pyramid
Play with Fathom
[Let's play with Python] Make a household account book
Let's play with JNetHack 3.6.2 which is easier to compile!
[Piyopiyokai # 1] Let's play with Lambda: Get a Twitter account
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
Play with Othello (Reversi)
[Let's play with Python] Image processing to monochrome and dots