maya python + Pass variable on button press

好久不见. 提交于 2019-12-08 12:31:06

问题


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

回答1:


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



回答2:


To substitute lambda, you can use partial :

from functools import partial

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

Both lambda and partial should work.

Hope it helps.



来源:https://stackoverflow.com/questions/22848582/maya-python-pass-variable-on-button-press

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