【问题标题】:PyMEL embedding layoutsPyMEL 嵌入布局
【发布时间】:2021-07-20 10:17:26
【问题描述】:

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

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

  • 第一行对 label+textField 对使用 2 列布局
  • 第二行对具有自己注释标签的菜单项使用 1 列布局
  • 第三行在 1 列布局中添加了一个全角执行按钮
  • 如果用户调整窗口大小,所有控件都会适当缩放

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)

【问题讨论】:

    标签: layout maya pymel


    【解决方案1】:

    由于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,如果将所有内容都包含在一个类中会很有帮助,尤其是在使用 PyMel 时。这样你就可以做到:

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

    如果您有回调,这将非常有用,因为您需要的所有内容都可以从类中访问,并且您根本不需要全局变量。

    【讨论】:

    • 感谢with 语法和课程提示!
    猜你喜欢
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多