PyMEL 嵌入布局

PyMEL embedding layouts

在一个表单中嵌入多个 PyMEL 布局的最佳方式是什么?

例如,对于表格的每一行,我想指定:

TIA!

import pymel.core as pm

def test(*args):
    print('model name: {}'.format(modelTField.getText()))
    print('model geom: {}'.format(modelMenu.getValue())) 

if pm.window("testUI", ex=1): pm.deleteUI("testUI")
window = pm.window("testUI", t="Test v0.1", w=500, h=200)
mainLayout = pm.verticalLayout() 

# two column layout
col2Layout = pm.horizontalLayout(ratios=[1, 2], spacing=10)
pm.text(label='Model name')

global modelTField
modelTField = pm.textField()

col2Layout.redistribute()

# single column layout
col1Layout = pm.horizontalLayout()

global modelMenu
modelMenuName = "modelMenu"
modelMenuLabel = "Model mesh"
modelMenuAnnotation = "Select which geo corresponds to the model shape"
modelMenu = pm.optionMenu(modelMenuName, l=modelMenuLabel, h=20, ann=modelMenuAnnotation)
pm.menuItem(l="FooShape")
pm.menuItem(l="BarShape") 

# execute
buttonLabel = "[DOIT]"
button = pm.button(l=buttonLabel, c=test)

col2Layout.redistribute()

# display window
pm.showWindow(window)

由于 pm.horizontalLayout()pm.verticalLayout 自动重新分发内容,您不需要自己调用重新分发。但是要使这些函数起作用,您需要这样的 with 语句:

import pymel.core as pm

def test(*args):
    print('model name: {}'.format(modelTField.getText()))
    print('model geom: {}'.format(modelMenu.getValue())) 

if pm.window("testUI", ex=1): pm.deleteUI("testUI")
window = pm.window("testUI", t="Test v0.1", w=500, h=200)

with pm.verticalLayout() as mainLayout:
    with pm.horizontalLayout(ratios=[1, 2], spacing=10) as col2Layout:
        pm.text(label='Model name'
        global modelTField
        modelTField = pm.textField()

    with pm.horizontalLayout() as col1Layout:        
        global modelMenu
        modelMenuName = "modelMenu"
        modelMenuLabel = "Model mesh"
        modelMenuAnnotation = "Select which geo corresponds to the model shape"
        modelMenu = pm.optionMenu(modelMenuName, l=modelMenuLabel, h=20, ann=modelMenuAnnotation)
        pm.menuItem(l="FooShape")
        pm.menuItem(l="BarShape") 
    
        # execute
        buttonLabel = "[DOIT]"
        button = pm.button(l=buttonLabel, c=test)

# display window
pm.showWindow(window)

要用 python 创建 UI 如果您将所有内容都包含在 class 中,尤其是当您使用 PyMel 时,这会很有帮助。这样你可以这样做:

class MyWindow(pm.ui.Window):
    def __init(self):
        self.title = "TestUI"
    def buttonCallback(self, *args):
        do something....

如果您有回调,这将非常有用,因为您需要的一切都可以从 class 中访问,而且您根本不需要全局变量。