Search code examples
pythonbuttonmaya

Double callback with option button


When I change between 2 options, it always returns double result (tex,tex/sculpt,sculpt). It was happening it my past projects too but I never got it solved. Restarting maya didn't work, even with rewritten code it kept happening. Any suggestions?

import maya.cmds as cmds

class UI(object):
    def __init__(self):

        a=cmds.window()
        cmds.showWindow(a)
        cmds.columnLayout()
        self.displaceOptions = cmds.radioButtonGrp(la2=['Texture', 'Sculpting'], nrb=2, en=True, cc=self.check)

    def check(self, *args):

        option = cmds.radioButtonGrp(self.displaceOptions, q=True, sl=True)

        if option == 1:
            self.dispTexture()
        elif option == 2:
            self.dispSculpt()

    def dispTexture(*args):
        print('tex')

    def dispSculpt(*args):
        print('sculpt')

UI()

Solution

  • The reason is that the changeCommand reacts to the state change which is changed twice, first one radiobutton is deactivated, then another is activated. The very first call of the UI() has no radio button selected, if you select one, the callback is called only once because the state only changes once. You could use onCommand, or offCommand which should behave a bit more as you expect.