سؤال

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()
هل كانت مفيدة؟

المحلول 2

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

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top