0

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()
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

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()
DrWeeny
  • 2,487
  • 1
  • 14
  • 17
0

You almost had it, just a few notes:

  1. When you create your gui objects, don't forget to capture them as a variable and using self so that you can reference them later.
  2. In you class, all of the method's first parameter should be self, unless you are purposely making it a static method (then you need to add the decorator). Have a quick Google search if you want to learn more.
  3. No need to query the slider's current value in setColor, as its changeCommand already provides it in its argument.
  4. Not too sure what's being accomplished in s_off and s_on. Since all the interface items have been stored with self. you can already reference them from any other method in the class.

Here's a working example:

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)
        self.color = cmds.intSlider(min=0, max=2, value=0, step=1, cc=partial(self.setColor), p=self.mainlayout)  # Add self.
        self.textButton = cmds.iconTextButton(w=55, bgc=(0.467, 0.467, 0.467), p=self.mainlayout)  # Capture in variable.
        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 setColor(self, color_1):  # Add self as first parameter. No need to query the slider's value, as the 2nd parameter already has it from its changeCommand.
        if color_1 == 0: 
            cmds.iconTextButton(self.textButton, e=True, bgc=(1, 1, 1))
        if color_1 == 1:
            cmds.iconTextButton(self.textButton, e=True, bgc=(0, 0, 1))
        if color_1 == 2:
            cmds.iconTextButton(self.textButton, e=True, bgc=(0.608, 0, 0.157))


ColorChangeWin()
Green Cell
  • 4,677
  • 2
  • 18
  • 49