[PYTHON] How to write a GUI using the maya command

** Introduction **

When creating a tool in maya, you may create a GUI for the tool. When making with MEL, use the MEL command When making with Python, I think that it is like using PySide or calling MEL from maya.cmds or pymel.

When looking at Twitter, many people use PySide when writing in Python. This time when creating a GUI using maya.cmds aiming at a niche layer It's convenient to use the with syntax and LayoutManager! I will introduce that.

** GUI example (MEL) **

Before explaining the example using maya.cmds, if you make it with MEL, it will be like this, for example.

global proc testWindow()
{
//Processing to erase the old one when the GUI already exists
if(`window -ex testWindow`) deleteUI testWindow;
string $windowName = `window -title "testWindow" testWindow`;
//Create GUI
string $tabTest = `tabLayout -innerMarginWidth 1 -innerMarginHeight 5 -scrollable false`;
string $tabTestColumn =`columnLayout -adj true -rowSpacing 10`;
    button -label "TestButton";
    button -label "TestButton"; 
    textField -tx "TestTextField" -w 500; 
    rowLayout -numberOfColumns 4 -columnWidth4 100 150 150 150 -columnAttach4 left left left left;
        button -label "TestButton";
        checkBox -label "TestCheckBox";
        checkBox -label "TestCheckBox";
        checkBox -label "TestCheckBox";
    setParent..;  
    text -label "TestText";
    textField -tx "TestTextField" -w 500;    
setParent..;
tabLayout -edit -tabLabel $tabTestColumn "Test" $tabTest;
//Show GUI
showWindow;
}

testWindow()

When you execute the above MEL, a GUI like this will be launched. 2019-12-02_12h30_17.png

Let's color-code the part of the code that makes the GUI. The largest red frame is columnLayout (the layout is to arrange the elements vertically) Place buttons and textFields in the columnLayout in order from the top at the blue frame. In the green frame, three buttons and checkBox are placed in one line. When writing in MEL, if you change the indent according to the hierarchy in the GUI layout The code will be easier to read. 2019-12-02_12h46_23.png 2019-12-02_11h13_01.png

** GUI example (maya.cmds) **

The following is an example written by maya.cmds.

# !/usr/bin/env python
# -*- coding: utf-8 -*-
    
import maya.cmds as cmds

def testWindow():
    #Processing to erase the old one when the GUI already exists
    if cmds.window('testWindow', ex=1):
        cmds.deleteUI('testWindow')
    windowName = cmds.window('testWindow',title='testWindow')
    #Create GUI
    tabTest=cmds.tabLayout(scrollable=False, innerMarginHeight=5, innerMarginWidth=1)
    tabTestColumn=cmds.columnLayout(adj=True, rowSpacing=10)
    cmds.button(label="TestButton")
    cmds.button(label="TestButton")
    cmds.textField(w=500, tx="TestTextField")
    cmds.rowLayout(numberOfColumns=4, columnAttach4=('left', 'left', 'left', 'left'), columnWidth4=(100, 150, 150, 150))
    cmds.button(label="TestButton")
    cmds.checkBox(label="TestCheckBox")
    cmds.checkBox(label="TestCheckBox")
    cmds.checkBox(label="TestCheckBox")
    cmds.setParent('..')
    cmds.text(label="TestText")
    cmds.textField(w=500, tx="TestTextField")
    cmds.setParent('..')
    cmds.tabLayout(tabTest, edit=1, tabLabel=(tabTestColumn, "Test"))
    #Show GUI
    cmds.showWindow()

if __name__ == '__main__':  
    testWindow()

In the case of maya.cmds (in the case of python than finally), if the indentation shifts, an error will occur. It is not possible to change the indent according to the hierarchy in the layout. Furthermore, cmds.setParent ('..') appears many times, so I want to delete it. .. .. So here is the main subject of this time! ** **

** Use with syntax and LayoutManager **

Use a combination of the with syntax and the default module LayoutManager in maya. The with syntax is mainly used when opening and closing files. If you do not use with, write as ↓

f = open("test.txt", "r")
print(f.read())
f.close()

You can use with to omit the f.close () part. Fortunately, the line below the with statement is indented.

with open("test.txt", "r") as f:
    print(f.read())

LayoutManager uses this mechanism of with syntax It is a module that allows you to change the indent freely. Since it is included in the library of maya.common.ui, import it as follows.

from maya.common.ui import LayoutManager

The following is a rewrite of the GUI example using maya.cmds using LayoutManager.

# !/usr/bin/env python
# -*- coding: utf-8 -*-
    
import maya.cmds as cmds
from maya.common.ui import LayoutManager

def testWindow():
    #Processing to erase the old one when the GUI already exists
    if cmds.window('testWindow', ex=1):
        cmds.deleteUI('testWindow')
    windowName = cmds.window('testWindow',title='testWindow')
    #Create GUI
    tabTest=cmds.tabLayout(scrollable=False, innerMarginHeight=5, innerMarginWidth=1)
    with LayoutManager(cmds.columnLayout(adj=True, rowSpacing=10)) as tabTestColumn:
        cmds.button(label="TestButton")
        cmds.button(label="TestButton")
        cmds.textField(w=500, tx="TestTextField")
        with LayoutManager(cmds.rowLayout(numberOfColumns=4, columnAttach4=('left', 'left', 'left', 'left'), columnWidth4=(100, 150, 150, 150))):
            cmds.button(label="TestButton")
            cmds.checkBox(label="TestCheckBox")
            cmds.checkBox(label="TestCheckBox")
            cmds.checkBox(label="TestCheckBox")
        cmds.text(label="TestText")
        cmds.textField(w=500, tx="TestTextField")
    cmds.tabLayout(tabTest, edit=1, tabLabel=(tabTestColumn, "Test"))
    #Show GUI
    cmds.showWindow()

if __name__ == '__main__':  
    testWindow()

The indent position has changed and cmds.setParent ('..') It's gone, so it's a little easier to see!

in conclusion

It's hard to see if you don't need it because you use PySide or if you leave the indent. You might think that I did not find LayoutManager when I searched on the net, so I introduced it. It's not a very useful feature, but if you like it, try it!

Recommended Posts

How to write a GUI using the maya command
How to execute a command using subprocess in Python
How to write a Python class
How to generate a query using the IN operator in Django
[sh] How to store the command execution result in a variable
Qiita (1) How to write a code name
How to draw a graph using Matplotlib
How to make a command to read the configuration file with pyramid
[Linux] How to use the echo command
How to use the Linux grep command
How to install a package using a repository
How to output the output result of the Linux man command to a file
How to run a Maya Python script
How to divide and process a data frame using the groupby function
Think about how to write a filter with the Shotgun API-Contact Versions
How to find out the number of CPUs without using the sar command
How to specify a .ui file in the dialog / widget GUI in PySide
How to fix the initial population with a genetic algorithm using DEAP
[Introduction to Python] How to write a character string with the format function
How to calculate the volatility of a brand
How to code a drone using image recognition
Automatically write admin.py using the django-extensions admin_generator command
Write a TCP server using the SocketServer module
How to write a ShellScript Bash for statement
How to create a shortcut command for LINUX
How to write a named tuple document in 2020
[Go] How to write or call a function
How to upload to a shared drive using pydrive
How to uninstall a module installed using setup.py
Create a command to get the work log
How to write a ShellScript bash case statement
Create a GUI on the terminal using curses
[2015/11/19] How to register a service locally using the python SDK on naoqi os
How to pass the execution result of a shell command in a list in Python
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
Read the Python-Markdown source: How to create a parser
How to create an article from the command line
How to delete the specified string with the sed command! !! !!
How to set up a Python environment using pyenv
How to create a submenu with the [Blender] plugin
How to write a list / dictionary type of Python3
How to hold a hands-on seminar using Jupyter using docker
Linux user addition, how to use the useradd command
How to use the grep command and frequent samples
How to make a Python package using VS Code
[Python] How to write a docstring that conforms to PEP8
Write data to KINTONE using the Python requests module
I made a command to markdown the table clipboard
[Introduction to Python] How to stop the loop using break?
I want to automate ssh using the expect command!
How to post a ticket from the Shogun API
(Remember quickly) How to use the LINUX command line
How to write faster when using numpy like deque
[Introduction to Python] How to write repetitive statements using for statements
How to use the grep command to recursively search directories and files to a specified depth
How to unit test a function containing the current time using freezegun in python
XPath Basics (2) -How to write XPath
How to use the generator
How to call a function
Clone using the dd command