Maya 2015 Python-textField输入,radioButton选择在UI中不起作用 [英] Maya 2015 Python - textField input, radioButton selection not working in UI

查看:121
本文介绍了Maya 2015 Python-textField输入,radioButton选择在UI中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究python以开发用于maya的工具(截至2015年),但无法确切了解我应该如何定义在函数内部使用的变量或ui对象.当我尝试运行脚本时会弹出很多其他错误(例如,此脚本:#错误:NameError:文件行1:未定义名称'inputTextA')

I'm studying python for developing tools for maya (at the moment 2015) and can't understand exactly how I should define variables or ui objects to be used inside functions. A lot of different errors pop up when I try running my script (e.g. for this script: # Error: NameError: file line 1: name 'inputTextA' is not defined )

我一遍又一遍地遇到相同的问题,所以我希望有人向我解释我正在犯什么样的错误,以及为将来解决此类问题需要学习什么. (请仅使用Python和Maya回答!:))

I run into the same problem over and over again, so I would like someone to explain to me what kind of mistake I'm making and what to study to solve this kind of troubles in the future. (Please only Python and Maya answers! :) )

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

The next script is saved in the maya's script path, called from the shelf as follows:

import uiTestA
reload (uiTestA)
uiTestA

在脚本中,取决于您设置的radioButton(A,B),应在按下执行"按钮后使用textField信息运行函数. 脚本本身:

In the script, depending on the radioButton you set (A, B) a function should run using the textField information after pressing the button "Execute". The script itself:

# -*- 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,如:

I tried defining a function to create the ui like:

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

And then calling it properly from maya's shelf; but no results. I'm fairly new to this so please be gentle! :) Thank you in advance for your time and help! Have a nice day! :)

推荐答案

maya ui总是提出大问题.这是一个链接,其中包含有关它的所有详细信息: Maya Python-使用来自UI的数据

Big questions come up always with maya ui. Here is a link where there is all details about it : Maya Python - Using data from UI

总结起来,有多种方法可供选择:

To sum up, there is multiple way to go :

  • 创建词典(最简单)
  • 使用部分或lambda传递变量
  • 创建一个类(更高级,对于简单的ui而言不是必需的)

首先,您不应该赖特:

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

命令标志不应为字符串,您必须使用partial或lambda,即:

command flag should not be a string, you have to use partial or lambda, i.e :

from functools import partial

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

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

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

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

To query radiobutton outside the def, use dic like this :

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 ):

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

-------- EDIT ----------- Here is a working example of your script :

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'] )

这篇关于Maya 2015 Python-textField输入,radioButton选择在UI中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆