Calling back user input values inside maya UI

∥☆過路亽.° 提交于 2020-01-20 07:51:55

问题


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

回答1:


Edit: Like DrWeeny said, it would be best to refer to the other similar topics.


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:

  • classic maya string
  • function as argument
  • lambda
  • functools.partial
  • pymel.core.Callback

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 types

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



回答2:


You can go on this other post to see the answers : Printing the label of a button when clicked or the usage of dictionnary here : Maya Python - Using data from UI

from functools import partial

def queryInputs(*args):
    #args0 = label01
    print(cmds.text(args[0], q=True, label = 1))
    #args[1] = int_01
    print(cmds.intField(args[1], q=True, v = 1))
    #args[2] = cb_01
    print(cmds.checkBox(args[2], q=True, v = 1))


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

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

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

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

    cb_01 = numberOfFloors = cmds.checkBox (label='Y/N')    

    cmds.button( label = 'Apply', command = partial(queryInputs, label_01, int_01, cb_01) )

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

    cmds.showWindow()

runGrid()


来源:https://stackoverflow.com/questions/37041184/calling-back-user-input-values-inside-maya-ui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!