【问题标题】:How do you get user choices from a Pymel LayoutDialog?如何从 Pymel LayoutDialog 中获得用户选择?
【发布时间】:2021-02-07 09:53:07
【问题描述】:

以下代码取自此处的 Pymel 文档:https://help.autodesk.com/cloudhelp/2020/ENU/Maya-Tech-Docs/PyMel/generated/functions/pymel.core.windows/pymel.core.windows.layoutDialog.html

虽然该示例名为checkboxPrompt,但它并未显示如何检索复选框的状态。 pm.layoutDialog 函数仅返回用于关闭对话框的按钮选择。同一命令的 MEL 和 Maya.cmds 文档也没有对此进行解释。那么它是如何实现的呢?

 
import pymel.core as pm

def checkboxPrompt():
    # Get the dialog's formLayout.
    #
    form = pm.setParent(q=True)

    # layoutDialog's are not resizable, so hard code a size here,
    # to make sure all UI elements are visible.
    #
    pm.formLayout(form, e=True, width=300)

    t = pm.text(l='What do you want to do?')

    b1 = pm.button(l='Abort', c='pm.layoutDialog( dismiss="Abort" )' )
    b2 = pm.button(l='Skip', c='pm.layoutDialog( dismiss="Skip" )' )
    b3 = pm.button(l='Continue', c='pm.layoutDialog( dismiss="Continue" )' )

    cb1 = pm.checkBox(label='Remember my choice')

    spacer = 5
    top = 5
    edge = 5

    pm.formLayout(form, edit=True,
                  attachForm=[(t, 'top', top), (t, 'left', edge), (t, 'right', edge), (b1, 'left', edge), (b3, 'right', edge), (cb1, 'left', edge), (cb1, 'bottom', spacer)],
                  attachNone=[(t, 'bottom'), (b1, 'bottom'), (b2, 'bottom'), (b3, 'bottom'), (cb1, 'right')],
                  attachControl=[(b1, 'top', spacer, t), (b2, 'top', spacer, t), (b3, 'top', spacer, t), (cb1, 'top', spacer, b1)],
                  attachPosition=[(b1, 'right', spacer, 33), (b2, 'left', spacer, 33), (b2, 'right', spacer, 66), (b3, 'left', spacer, 66)])

print pm.layoutDialog(ui=checkboxPrompt)

【问题讨论】:

    标签: user-interface maya pymel


    【解决方案1】:

    很多时候,文档会跳过很多元素以保持一定的简单性,但正因为如此,我们经常发现自己没有解决方案和/或非常基本的示例。

    在您的情况下,有多种方法可以获取复选框值,例如:

    • 在带有参数changeCommandonCommand / offCommand 的复选框上添加callback 以更新您之前在代码中创建的python global 变量。
    • 更简单的方法是将lambda 函数设置为command 参数(而不是字符串)以检索复选框值并设置正确的dismiss 值。

    这是我上面所说的第二种方法的代码:

    import pymel.core as pm
    
    def checkboxPrompt():
        # Get the dialog's formLayout.
        form = pm.setParent(q=True)
        
        # Function method to query the value of the checkbox
        def getRememberMyChoice(checkbox_id):
            if pm.checkBox(checkbox_id, query=True, value=True):
                return ':Yes'
            return ':No'
    
        # layoutDialog's are not resizable, so hard code a size here,
        # to make sure all UI elements are visible.
        pm.formLayout(form, e=True, width=300)
    
        t = pm.text(l='What do you want to do?')
    
        b1 = pm.button(l='Abort', c=lambda *args: pm.layoutDialog( dismiss="Abort"+getRememberMyChoice(checkbox1) ) )
        b2 = pm.button(l='Skip', c=lambda *args: pm.layoutDialog( dismiss="Skip"+getRememberMyChoice(checkbox1) ) )
        b3 = pm.button(l='Continue', c=lambda *args: pm.layoutDialog( dismiss="Continue"+getRememberMyChoice(checkbox1) ) )
     
        checkbox1 = pm.checkBox(label='Remember my choice')
    
        spacer = 5
        top = 5
        edge = 5
    
        pm.formLayout(form, edit=True,
                      attachForm=[(t, 'top', top), (t, 'left', edge), (t, 'right', edge), (b1, 'left', edge), (b3, 'right', edge), (checkbox1, 'left', edge), (checkbox1, 'bottom', spacer)],
                      attachNone=[(t, 'bottom'), (b1, 'bottom'), (b2, 'bottom'), (b3, 'bottom'), (checkbox1, 'right')],
                      attachControl=[(b1, 'top', spacer, t), (b2, 'top', spacer, t), (b3, 'top', spacer, t), (checkbox1, 'top', spacer, b1)],
                      attachPosition=[(b1, 'right', spacer, 33), (b2, 'left', spacer, 33), (b2, 'right', spacer, 66), (b3, 'left', spacer, 66)])
    
    print (pm.layoutDialog(ui=checkboxPrompt))
    

    执行代码,(选中或不选中复选框)然后通过单击其中一个按钮关闭将打印您:

    # First word ("Continue") depends on the button you clicked
    # Yes or No (after the ":") is the state of the "Remember my choice" checkbox 
    Continue:Yes
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      相关资源
      最近更新 更多