在Maya UI中回调用户输入值 [英] Calling back user input values inside maya UI

查看:114
本文介绍了在Maya UI中回调用户输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在maya中使用python构建了一个小程序,并且我对打印用户单击应用"时输入的值感兴趣.关于如何实现此目标的任何想法?然后,我想使用另一段代码中的值在maya中创建建筑物.

I've built a small program using python in maya, and im interested in printing the values that the user inputs upon the click of 'Apply'. Any ideas on how I can achieve this? I want to then use the values in another piece of code to create buildings inside maya.

def runGrid():
if mc.window('windowTest9', ex=True):
    mc.deleteUI('windowTest9', window=True)

mc.window('windowTest9', title='BuilGen', sizeable=False, resizeToFitChildren=True)

mc.rowColumnLayout( numberOfColumns = 2, columnWidth = [ (1, 150), (2, 100), (3, 75)])  

#mc.text( label = 'Top Bar')
mc.separator( h = 10, style = 'none')
mc.separator( h = 10, style = 'none')

mc.text(label='Create a New Building', align = 'left')
mc.image(image='F:\Photos\office-building-icon-687152.png')


# insert a blank line space
mc.separator( h = 10, style = 'singleDash')
mc.separator( h = 10, style = 'singleDash')


mc.text( label = 'number of sections wide:', align = 'left')
buildingWidth = mc.intField( value = 4)


mc.text( label = 'number of sections deep:', align = 'left')
buildingDepth = mc.intField( value = 3)    

mc.text( label = 'number of floors:', align = 'left')
numberOfFloors = mc.intField( value = 2)    


mc.text( label = 'roof:', align = 'left')
numberOfFloors = mc.checkBox (label='Y/N')    
mc.separator( h = 10, style = 'none')

# insert another blank line
mc.separator( h = 10, style = 'none')
mc.separator( h = 10, style = 'none')


# create the buttons
mc.separator( h = 10, style = 'none')
mc.button( label = 'Apply', command = '' )


mc.button( label = 'Cancel', command = 'mc.deleteUI("windowTest9", window=True)')

mc.showWindow()

runGrid()

推荐答案

就像DrWeeny所说的那样,最好参考其他类似的主题.

前段时间,我在博客上提醒了我一点,以便能够测试所有不同的本机Maya UI使用命令的方法以及一种快速的测试方法:

I did a little reminder on my blog some time ago, to be able to test all the different native Maya UI way of using commands with a fast way of testing them:

  • 经典玛雅字符串
  • 函数作为参数
  • lambda
  • functools.partial
  • pymel.core.Callback
  • classic maya string
  • function as argument
  • lambda
  • functools.partial
  • pymel.core.Callback

每种情况下都带有将变量作为参数传递给这些函数的示例.因为有时您必须能够. 总的来说,我完全建议您使用 functools.partial ,它仅比其他优点(如果您忘记了PySide).

Each case is also given with examples with passing variables as arguments to those functions. Because sometimes you have to be able to. Overall I totally recommend the use functools.partial, it gives only advantages over the others (if you forget about PySide).

Maya UI类型

def function(*args):
    print args
    cmds.textFieldGrp(text, edit=True, text=str(args))

variable = 'Variable'
width = [1, 250]
align = [1, 'left']
window = cmds.window(title='UI and commands arguments.')
cmds.columnLayout()

cmds.textFieldGrp(label="\"function()\"", changeCommand="function()", columnWidth=width, columnAlign=align)
cmds.textFieldGrp(label="function", changeCommand=function, columnWidth=width, columnAlign=align)
cmds.textFieldGrp(label="\"function(variable)\"", changeCommand="function(variable)", columnWidth=width, columnAlign=align)
cmds.textFieldGrp(label="lambda x: function(variable)", changeCommand=lambda x: function(variable), columnWidth=width, columnAlign=align)
cmds.separator(style="double", height=20)

import functools
cmds.textFieldGrp(changeCommand=functools.partial(function), label='functools.partial(function)', columnWidth=width, columnAlign=align)
cmds.textFieldGrp(changeCommand=functools.partial(function, variable), label='functools.partial(function, variable)', columnWidth=width, columnAlign=align)
cmds.separator(style="single", height=20)

import pymel.core
cmds.textFieldGrp(changeCommand=pymel.core.Callback(function), label='pymel.core.Callback(function)', columnWidth=width, columnAlign=align)
cmds.textFieldGrp(changeCommand=pymel.core.CallbackWithArgs(function), label='pymel.core.CallbackWithArgs(function)', columnWidth=width, columnAlign=align)
cmds.textFieldGrp(changeCommand=pymel.core.CallbackWithArgs(function, variable), label='pymel.core.CallbackWithArgs(function, variable)', columnWidth=width, columnAlign=align)

cmds.separator(style="single", height=20)
text = cmds.textFieldGrp(label='RESULT: ', text='', width=500)
cmds.showWindow()


使用课程时

因为它不是在考虑使用类的情况下完成的,所以当您在课堂上时,某些方法根本不起作用.


When using a class

Because it was not made with the use of class in mind, some ways does not work at all when you are in a class.

class MayaUI():
    def __init__(self):
        self.variable = 'Variable'
        self.width = [1, 250]
        self.align = [1, 'left']
        self.window = cmds.window(title='UI and commands arguments.')
        cmds.columnLayout()

        cmds.textFieldGrp(label="\"self.function()\"", changeCommand="self.function()", columnWidth=self.width, columnAlign=self.align)
        cmds.textFieldGrp(label="self.function", changeCommand=self.function, columnWidth=self.width, columnAlign=self.align)
        cmds.textFieldGrp(label="\"self.function(self.variable)\"", changeCommand="self.function(self.variable)", columnWidth=self.width, columnAlign=self.align)
        cmds.textFieldGrp(label="lambda x: self.function(self.variable)", changeCommand=lambda x: self.function(self.variable), columnWidth=self.width, columnAlign=self.align)
        cmds.separator(style="double", height=20)

        import functools
        cmds.textFieldGrp(changeCommand=functools.partial(self.function), label='functools.partial(self.function)', columnWidth=self.width, columnAlign=self.align)
        cmds.textFieldGrp(changeCommand=functools.partial(self.function, self.variable), label='functools.partial(self.function, self.variable)', columnWidth=self.width, columnAlign=self.align)
        cmds.separator(style="single", height=20)

        import pymel.core
        cmds.textFieldGrp(changeCommand=pymel.core.Callback(self.function), label='pymel.core.Callback(self.function)', columnWidth=self.width, columnAlign=self.align)
        cmds.textFieldGrp(changeCommand=pymel.core.CallbackWithArgs(self.function), label='pymel.core.CallbackWithArgs(self.function)', columnWidth=self.width, columnAlign=self.align)
        cmds.textFieldGrp(changeCommand=pymel.core.CallbackWithArgs(self.function, self.variable), label='pymel.core.CallbackWithArgs(self.function, self.variable)', columnWidth=self.width, columnAlign=self.align)

        # A bit more complicated
        _map = {'textFieldGrp': lambda arg:cmds.textFieldGrp(arg, query=True, text=True)}
        _com = lambda *args:args[0](self.variable, _map[args[1]](args[2]))
        cmds.textFieldGrp('textfieldName', changeCommand=pymel.core.Callback(_com, self.function, 'textFieldGrp', 'textfieldName'), label="pymel.core.Callback(_com, self.function, 'textFieldGrp', 'textfieldName') + lambdas", columnWidth=self.width, columnAlign=self.align)

        cmds.separator(style="single", height=20)
        self.text = cmds.textFieldGrp(label='RESULT: ', text='', width=500)
        cmds.showWindow()

    def function(self, *args):
        print args
        cmds.textFieldGrp(self.text, edit=True, text=str(args))

MayaUI()

这篇关于在Maya UI中回调用户输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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