maya python +按下按钮时传递变量 [英] maya python + Pass variable on button press

查看:107
本文介绍了maya python +按下按钮时传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用python为maya编写的脚本,并且有3个标记为X Y Z的按钮.根据所按下的按钮,我希望变量将特定的值传递给该函数.我怎样才能做到这一点??我已经在按钮的注释中写了关于我要通过的内容的信息.似乎只是打印'false',不确定原因.

I have a script i wrote in python for maya and there are 3 buttons labeled X Y Z. Depending on which button is pressed I want a variable to pass a specific value to the function. How can I do this?? I've written in the comments for the button in regards to what I'm trying to pass. It seems to just print 'false' im not sure why.

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

推荐答案

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

Use a lambda to give each button command its own mini-function:

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

这篇关于maya python +按下按钮时传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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