我用Python为Maya编写了这个小脚本,其中有一个滑块,可以更改iconTextButton的背景颜色。运行脚本我没有任何警告或错误,但是它没有执行我上面解释的内容。
我不知道是什么问题,也许是我要在另一个函数中调用一个函数? (当我在s_off()中调用setColor()时)。如果是此问题,我该如何解决?希望有人能帮助我
这里是代码:
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()
有两件事:
/
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()
您几乎得到了,只有几点注意事项:
self
,以便以后可以引用它们。self
,除非您有意将其设为静态方法(然后需要添加装饰器)。如果您想了解更多信息,请快速进行Google搜索。setColor
中查询滑块的当前值,因为其changeCommand
已经在其参数中提供了它。s_off
和s_on
中要完成什么。由于所有接口项都已用self.
存储,因此您已经可以从该类中的任何其他方法引用它们。这是一个有效的示例:
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()