按下按钮时,maya python +传递变量

时间:2014-04-03 20:42:09

标签: python maya

我有一个我在python中为maya编写的脚本,并且有3个按钮标记为X Y Z.根据按下的按钮,我想要一个变量将特定值传递给该函数。我怎样才能做到这一点??我已经在关于我想要通过的按钮的评论中写了。它似乎打印'假'我不知道为什么。

import maya.cmds as cmds

class createMyLayoutCls(object):
    def __init__(self):
        pass

    def show(self):
        self.createMyLayout()

    def createMyLayout(self):

        #check to see if our window exists
        if cmds.window('utility', exists = True):
            cmds.deleteUI('utility')

        # create our window
        self.window = cmds.window('utility', widthHeight = (200, 200), title = 'Distribute', resizeToFitChildren=1, sizeable = False)

        cmds.setParent(menu=True)

        # create a main layout
        mainLayout = cmds.gridLayout( numberOfColumns=3, cellWidthHeight=(70, 50) )

        # X Y Z BUTTONS
        btnAlignX = cmds.button(label = 'X', width = 40, height = 40, c = self.TakeAction) # should pass 'axis='X"
        btnAlignY = cmds.button(label = 'Y', width = 40, height = 40, c = self.TakeAction) # should pass 'axis='Y"
        btnAlignZ = cmds.button(label = 'Z', width = 40, height = 40, c = self.TakeAction) # should pass 'axis='Z"

        # show window
        cmds.showWindow(self.window)

    def TakeAction(self, axis=''):
        print axis

        if axis == 'x':
            print 'you selected x'
        if axis == 'y':
            print 'you selected y'
        if axis == 'y':
            print 'you selected z'   

b_cls = createMyLayoutCls()  
b_cls.show()

2 个答案:

答案 0 :(得分:3)

要替换lambda,您可以使用partial:

from functools import partial

btnAlignX = cmds.button(label='X', c=partial(self.TakeAction, 'X'))

lambda和partial都应该有用。

希望它有所帮助。

答案 1 :(得分:2)

使用lambda为每个按钮命令指定其自己的迷你功能:

btnAlignX = cmds.button(label='X', c=lambda *_:self.TakeAction('X'))
btnAlignY = cmds.button(label='Y', c=lambda *_:self.TakeAction('Y'))
btnAlignZ = cmds.button(label='Z', c=lambda *_:self.TakeAction('Z'))