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