Mel Button Color ScriptJob?

问题描述 投票:-1回答:1

我正在编写一个用于在我的Maya场景中选择对象的UI的新手,我需要帮助我如何使用scriptjob在选择对象时将按钮颜色更改为白色,并在取消选择对象时返回默认颜色。只要选择了对象,按钮颜色应保持为白色。请根据以下代码提供解决方案。谢谢!

if (`window -exists MyPicker`) deleteUI MySelecter;
window -title "Item Selecter" -widthHeight 170 300 -sizeable false -mxb false MySelecter;
    formLayout -numberOfDivisions 100 MySelecter;{

        button -label "object1" -w 170 -h 44 -enable true -backgroundColor 0.820 0.360 0.161 -command "select object1" object1_Btn;
        button -label "object2" -w 170 -h 44 -enable true -backgroundColor 0.820 0.360 0.161 -command "select object2" object2_Btn;
        button -label "object3" -w 170 -h 44 -enable true -backgroundColor 0.820 0.360 0.161 -command "select object3" object3_Btn;
        button -label "object4" -w 170 -h 44 -enable true -backgroundColor 0.820 0.360 0.161 -command "select object4" object4_Btn;
        button -label "object5" -w 170 -h 44 -enable true -backgroundColor 0.820 0.360 0.161 -command "select object5" object5_Btn;               
    }
    formLayout -edit
        //object button
        -attachForm object1_Btn "top" 14
        -attachForm object1_Btn "left" 0

        -attachForm object2_Btn "top" 71
        -attachForm object2_Btn "left" 0

        -attachForm object3_Btn "top" 128
        -attachForm object3_Btn "left" 0

        -attachForm object4_Btn "top" 185
        -attachForm object4_Btn "left" 0

        -attachForm object5_Btn "top" 242
        -attachForm object5_Btn "left" 0

    MySelecter;
showWindow MySelecter;  
maya mel
1个回答
0
投票

这个答案全部都在Python中,因此如果您坚持使用它,可以将其转换为MEL。

当事件发生时,脚本作业可以以许多不同的方式触发。这可能是时间变化,用户进行撤消或者选择更改时的情况。

您可以通过运行以下命令获取这些事件名称的完整列表:

cmds.scriptJob(listEvents=True)

你要找的那个是"SelectionChanged"

要使其工作,您需要定义一个在触发脚本作业时(当选择更改时)将调用的函数。这是一个简单的例子。

import maya.cmds as cmds


# Create a function that will be called from the script job whenever there's a change to the selection.
def func():
    print "The selection has changed!"

# Create a new script job and save the result to a variable. The result is the script job's id number.
script_job_id = cmds.scriptJob(event=["SelectionChanged", func])

# When it's no longer needed, pass the script job's id with the kill parameter to remove it.
#cmds.scriptJob(kill=script_job_id)

因此,在您运行该功能的情况下,它可以检查对象是否被选中,并且根据是否是您可以使按钮着色。

当您的工具关闭时,您可以使用其kill参数删除脚本作业,以便在您不需要它时不再运行它。

正如你注意到你写给Haggi的旁注,除非你有充分的理由我会坚持使用Python而不是MEL。语法更容易,它有大量的库,并且它可以更强大地执行操作字符串之类的简单操作。加上Python用于许多其他软件,MEL不是。确实有些命令只能在MEL中完成,但您可以使用Python轻松评估MEL字符串。

© www.soinside.com 2019 - 2024. All rights reserved.