Search code examples
pythonuser-interfacemayamel

Python for Maya - Slider color changer


I wrote this little script in Python for Maya, where I have a slider that should change the background color of my iconTextButton. Running the script I don't have any warnings or errors, but it doesn't do what I explained above.

I can't figure out what is the problem, maybe it's that I'm trying to call a function inside another function? ( when I'm calling setColor() inside s_off()). If the problem is this how can I resolve it?

Here is the code:

import maya.cmds as cmds
from functools import partial

class ColorChangeWin(object):
    def __init__(self):
        self.buildWin()

    def buildWin(self):
        self.win = cmds.window(title="ColorChange")
        self.menuLayout = cmds.menuBarLayout()
        self.menu = cmds.menu(label="Window")
        self.menuItem = cmds.menuItem(label = "Close", command = partial(self.closeWin, self.win))
        self.mainlayout = cmds.columnLayout(adj = True)
        color = cmds.intSlider(min = 0, max = 3, value = 0,  step = 1, dc = partial(self.s_off), cc = partial(self.s_on), p = self.mainlayout)
        cmds.iconTextButton(w = 55, bgc = (0.467, 0.467, 0.467), p = self.mainlayout)
        cmds.showWindow(self.win)

    def closeWin(self, window = None, arg = None):
        if cmds.window(self.win, exists = True):
            cmds.deleteUI(self.win, window = True)


    def s_off(*args):
        panel = cmds.getPanel(withFocus=True)
        cmds.modelEditor(panel, e = True,  sel = False, m = False)
        return()
        setColor()

    def s_on(*args):
        panel = cmds.getPanel(withFocus=True)
        cmds.modelEditor(panel, e = True, sel = True, m = True)

    def setColor(*args):
        color_1 = cmds.intSlider(color, q = True, value = True)

        if color_1 == 0: 
            cmds.iconTextButton(e = True, bgc = (1, 1, 1))
        if color_1 == 1:
            cmds.iconTextButton(e = True, bgc = (0, 0, 1))
        if color_1 == 2:
            cmds.iconTextButton(e = True, bgc = (0.608, 0, 0.157))
        return 

ColorChangeWin()

Solution

  • So two things :

    • you are miss using classes, you should use self. when something is used somewhere else inside your ui as ie : window name, slider, colored button
    • you are putting a return before you set the color so it will exit

    /

    from functools import partial
    import maya.cmds as cmds
    
    class ColorChangeWin(object):
        def __init__(self):
            self.buildWin()
    
        def buildWin(self):
            self.win = cmds.window(title="ColorChange")
            cmds.menuBarLayout()
            menu = cmds.menu(label="Window")
            cmds.menuItem(label = "Close", command = partial(self.closeWin, self.win))
            main_layout = cmds.columnLayout(adj = True)
            self.color = cmds.intSlider(min = 0, max = 3, value = 0,  step = 1, dc = partial(self.s_off), cc = partial(self.s_on), p = main_layout)
            self.text_button = cmds.iconTextButton(w = 55, bgc = (0.467, 0.467, 0.467), p = main_layout)
            cmds.showWindow(self.win)
    
        def closeWin(self, window = None, arg = None):
            if cmds.window(self.win, exists = True):
                cmds.deleteUI(self.win, window = True)
    
    
        def s_off(self, *args):
            panel = cmds.getPanel(withFocus=True)
            cmds.modelEditor(panel, e = True,  sel = False, m = False)
            self.setColor()
            return
    
        def s_on(self, *args):
            panel = cmds.getPanel(withFocus=True)
            cmds.modelEditor(panel, e = True, sel = True, m = True)
    
        def setColor(self):
            color_1 = cmds.intSlider(self.color, q = True, value = True)
    
            if color_1 == 0: 
                cmds.iconTextButton(self.text_button, e = True, bgc = (1, 1, 1))
            if color_1 == 1:
                cmds.iconTextButton(self.text_button, e = True, bgc = (0, 0, 1))
            if color_1 == 2:
                cmds.iconTextButton(self.text_button, e = True, bgc = (0.608, 0, 0.157))
            return
    ColorChangeWin()