第一次看到一个同事用with写UI的时候,感觉好神奇,原来with还可以这样用啊,顿感自己好无知,今天在看Pymel的文档时,才返现原来那么好的范例就放在那里,自己没有发现啊,今天狠狠地补了一下这方面的知识。同时,如果对UI里的某一类control想做某方面的统一样式,如宽度、高度、边框等等,就可以定义个template,以后所有UI里的control都会自动统一到template,不用每次自定义了,更快捷方便。最后pymel的horizontalLayout和他的redistribute()为pymel自定义的部件和方法,也很好h.redistribute(3, 3, 2)。
from pymel.core import *
template = uiTemplate('ExampleTemplate', force=True)
template.define(button, width=100, height=30, align='right')
template.define(frameLayout, borderVisible=True, labelVisible=False)
if window('firstWithWindow', exists = 1): deleteUI('firstWithWindow', window = True)
with window('firstWithWindow', menuBar=True,menuBarVisible=True, title = 'WithWindow') as win:
# start the template block
with template:
with columnLayout( rowSpacing=5, adj = 1 ):
with frameLayout():
with columnLayout(adj = 1):
button(label='One')
button(label='Two')
button(label='Three')
with frameLayout():
with horizontalLayout() as h5:
button(label = 'Push Me!')
button(label = 'Pull Him!')
h5.redistribute(30, 30)
with frameLayout():
with optionMenu():
menuItem(label='Red')
menuItem(label='Green')
menuItem(label='Blue')
# add a menu to an existing window
with win:
with menu(label = 'File'):
menuItem(label='One')
menuItem(label='Two')
with subMenuItem(label='Sub'):
menuItem(label='A')
menuItem(label='B')
menuItem(label='Three')
pymel在meya里使用起来真是快键方便,而且代码量少,还有些其他功能,自己正在看文档,后面继续更新。
最后上传一张图: