【问题标题】:Maya 2015 Python - textField input, radioButton selection not working in UIMaya 2015 Python - 文本字段输入,单选按钮选择在 UI 中不起作用
【发布时间】:2017-06-04 05:06:40
【问题描述】:

我正在研究 python 为 Maya 开发工具(目前是 2015 年),但我无法准确理解我应该如何定义要在函数内部使用的变量或 ui 对象。当我尝试运行我的脚本时会弹出很多不同的错误(例如对于这个脚本:# Error: NameError: file line 1: name 'inputTextA' is not defined

我一遍又一遍地遇到同样的问题,所以我希望有人向我解释我犯了什么样的错误以及将来要研究什么来解决这种麻烦。 (请仅 Python 和 Maya 回答!:))

下一个脚本保存在maya的脚本路径中,从架子上调用如下:

import uiTestA
reload (uiTestA)
uiTestA

在脚本中,根据您设置的单选按钮(A,B),在按下“执行”按钮后,一个函数应该使用 textField 信息运行。 脚本本身:

# -*- coding: utf-8 -*-

import maya.cmds as cmds

def printStuff (fieldID):
    selObjs=cmds.ls(selection=True)
    print "\n###############################################"
    if ( cmds.radioButtonGrp( radioButtonsA, q=True, select=True )==1 ):
        if len(selObjs)>0:
            typeObj = cmds.textField( fieldID, query=True, text=True )
            if typeObj=="exception":
                print "EXCEPTION [%s] CASE A" % typeObj
            elif typeObj=="":
                cmds.error( "Please input something" )
            else:
                print "NORMAL [%s] CASE A" % typeObj
        else:
            cmds.error( "Please select a group or object in the scene." )
    elif ( cmds.radioButtonGrp( radioButtonsA, q=True, select=True )==2 ):
        typeObj = cmds.textField( fieldID, query=True, text=True )
        if typeObj=="exception":
            print "EXCEPTION [%s] CASE B" % typeObj
        elif typeObj=="":
            cmds.error( "Please input something" )
        else:
            print "NORMAL [%s] CASE B" % typeObj
    print "\n###############################################\n"

winID = "uiTestA"
if cmds.window( winID, exists=True ):
    cmds.deleteUI( winID )
cmds.window ( winID, sizeable=False, width=325, resizeToFitChildren=True )
formLay = cmds.formLayout( numberOfDivisions=100 )

radioButtonsA = cmds.radioButtonGrp( width=325, label='Search in:  ', columnWidth3=(80,158,100), labelArray2=["A", "B"], select=1, numberOfRadioButtons=2 )
explanationA = cmds.text( width=330, label="Input:" )
inputTextA = cmds.textField( width=100 )
selectButton = cmds.button( width=150, label="Execute", command="%s.printStuff(inputTextA)"%__name__)

cmds.formLayout( formLay, edit=True, attachForm=[(radioButtonsA, 'top', 5), (radioButtonsA, 'left', 0), (explanationA, 'top', 30), (inputTextA, 'top', 45), (inputTextA, 'left', 117), (selectButton, 'top', 70), (selectButton, 'left', 92)] )

cmds.showWindow( winID )

我尝试定义一个函数来创建用户界面,例如:

# -*- coding: utf-8 -*-

import maya.cmds as cmds

def printStuff (fieldID):
    selObjs=cmds.ls(selection=True)
    print "\n###############################################"
    if ( cmds.radioButtonGrp( radioButtonsA, q=True, select=True )==1 ):
        if len(selObjs)>0:
            typeObj = cmds.textField( fieldID, query=True, text=True )
            if typeObj=="exception":
                print "EXCEPTION [%s] CASE A" % typeObj
            elif typeObj=="":
                cmds.error( "Please input something" )
            else:
                print "NORMAL [%s] CASE A" % typeObj
        else:
            cmds.error( "Please select a group or object in the scene." )
    elif ( cmds.radioButtonGrp( radioButtonsA, q=True, select=True )==2 ):
        typeObj = cmds.textField( fieldID, query=True, text=True )
        if typeObj=="exception":
            print "EXCEPTION [%s] CASE B" % typeObj
        elif typeObj=="":
            cmds.error( "Please input something" )
        else:
            print "NORMAL [%s] CASE B" % typeObj
    print "\n###############################################\n"

def ui()
    winID = "uiTestA"
    if cmds.window( winID, exists=True ):
        cmds.deleteUI( winID )
    cmds.window ( winID, sizeable=False, width=325, resizeToFitChildren=True )
    formLay = cmds.formLayout( numberOfDivisions=100 )

    radioButtonsA = cmds.radioButtonGrp( width=325, label='Search in:  ', columnWidth3=(80,158,100), labelArray2=["A", "B"], select=1, numberOfRadioButtons=2 )
    explanationA = cmds.text( width=330, label="Input:" )
    inputTextA = cmds.textField( width=100 )
    selectButton = cmds.button( width=150, label="Execute", command="%s.printStuff(inputTextA)"%__name__)

    cmds.formLayout( formLay, edit=True, attachForm=[(radioButtonsA, 'top', 5), (radioButtonsA, 'left', 0), (explanationA, 'top', 30), (inputTextA, 'top', 45), (inputTextA, 'left', 117), (selectButton, 'top', 70), (selectButton, 'left', 92)] )

    cmds.showWindow( winID )

然后从 Maya 的书架上正确调用它;但没有结果。 我对此很陌生,所以请温柔一点! :) 提前感谢您的时间和帮助!祝你今天过得愉快! :)

【问题讨论】:

    标签: python user-interface maya


    【解决方案1】:

    maya ui 总是会出现大问题。这是一个链接,其中包含有关它的所有详细信息: Maya Python - Using data from UI

    总而言之,有多种方法可以走:

    • 创建字典(最简单)
    • 使用 partial 或 lambda 传递变量
    • 创建一个类(更高级,简单的 ui 不需要)

    =============================================

    首先,你不应该写:

    selectButton = cmds.button( width=150, label="Execute", command="%s.printStuff(inputTextA)"%__name__)
    

    命令标志不应该是字符串,你必须使用部分或 lambda,即:

    from functools import partial
    
        selectButton = cmds.button( width=150, label="Execute", command=partial(printStuff, name))
    

    =============================================

    要在 def 之外查询单选按钮,请像这样使用 dic:

    uiDic = {}
    uiDic['radioButtonsA'] = cmds.radioButtonGrp( width=325, label='Search in:  ', columnWidth3=(80,158,100), labelArray2=["A", "B"], select=1, numberOfRadioButtons=2 )
    
    # query ===
    
    if ( cmds.radioButtonGrp(uiDic['radioButtonsA'], q=True, select=True )==1 ):
    

    -------- 编辑 ------------ 这是您的脚本的一个工作示例:

    import maya.cmds as cmds
    from functools import partial
    
    dicUI = {}
    
    def printStuff (fieldID, *args):
        selObjs=cmds.ls(selection=True)
        print "\n###############################################"
        if ( cmds.radioButtonGrp( dicUI['radioButtonsA'], q=True, select=True )==1 ):
            if len(selObjs)>0:
                typeObj = cmds.textField( fieldID, query=True, text=True )
                if typeObj=="exception":
                    print "EXCEPTION [%s] CASE A" % typeObj
                elif typeObj=="":
                    cmds.error( "Please input something" )
                else:
                    print "NORMAL [%s] CASE A" % typeObj
            else:
                cmds.error( "Please select a group or object in the scene." )
        elif ( cmds.radioButtonGrp( dicUI['radioButtonsA'], q=True, select=True )==2 ):
            typeObj = cmds.textField( fieldID, query=True, text=True )
            if typeObj=="exception":
                print "EXCEPTION [%s] CASE B" % typeObj
            elif typeObj=="":
                cmds.error( "Please input something" )
            else:
                print "NORMAL [%s] CASE B" % typeObj
        print "\n###############################################\n"
    
    def ui():
        dicUI['winID'] = "uiTestA"
        if cmds.window( dicUI['winID'], exists=True ):
            cmds.deleteUI( dicUI['winID'] )
        cmds.window ( dicUI['winID'], sizeable=False, width=325, resizeToFitChildren=True )
        formLay = cmds.formLayout( numberOfDivisions=100 )
    
        dicUI['radioButtonsA'] = cmds.radioButtonGrp( width=325, label='Search in:  ', columnWidth3=(80,158,100), labelArray2=["A", "B"], select=1, numberOfRadioButtons=2 )
        explanationA = cmds.text( width=330, label="Input:" )
        inputTextA = cmds.textField( width=100 )
        selectButton = cmds.button( width=150, label="Execute", command=partial(printStuff, inputTextA))
    
        cmds.formLayout( formLay, edit=True, attachForm=[(dicUI['radioButtonsA'], 'top', 5), (dicUI['radioButtonsA'], 'left', 0), (explanationA, 'top', 30), (inputTextA, 'top', 45), (inputTextA, 'left', 117), (selectButton, 'top', 70), (selectButton, 'left', 92)] )
    
        cmds.showWindow( dicUI['winID'] )
    

    【讨论】:

    • 我尝试创建一个字典,将 ui 部分重命名为“ui_dicc['radioButtonsA']”等,但我得到一个无效的语法错误。我正在替换 > 在 ui 函数中带有 > (我在问题上写的第二个示例)。我哪里弄错了?
    • 您在语法中忘记了 def ui(): 前的“:”,我想。我用一个功能示例编辑了我的帖子。
    • 非常感谢! “:”也是一个语法错误,但我也在函数之外定义了 ui 元素,也许这也是一个问题。它作为您编辑的示例运行良好!我会多学习一点字典,从现在开始更多地使用它们:)
    猜你喜欢
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 2021-09-10
    相关资源
    最近更新 更多