Maya 2015 Python - textField 输入、radioButton 选择在 UI 中不起作用

Maya 2015 Python - textField input, radioButton selection not working in UI

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

我运行一遍又一遍地陷入同样的​​问题,所以我希望有人能向我解释我犯了什么样的错误以及将来应该研究什么来解决这种麻烦. (请只 Python 和 Maya 回答!:))

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

import uiTestA
reload (uiTestA)
uiTestA

在脚本中,根据您设置的单选按钮 (A, B),函数应该 运行 在按下按钮 "Execute" 后使用 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 )

我尝试定义一个函数来创建 ui,例如:

# -*- 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的架子上正确调用它;但没有结果。 我对此很陌生,所以请保持温柔! :) 预先感谢您的时间和帮助!祝你今天过得愉快! :)

maya 总是会提出大问题 ui。这是一个 link ,其中包含有关它的所有详细信息:

总而言之,有多种方法可供选择:

  • 创建字典(最简单)
  • 通过部分或 lambda 传递变量
  • 创建一个 class(更高级,简单 ui 不需要)

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

首先,你不应该wright :

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

command flag 不应该是字符串,你必须使用 partial 或 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'] )