Search code examples
mayamel

Mel Button Color ScriptJob?


New to mel and I'm writing a UI for object selecting in my Maya scene and I need help on how I can use the scriptjob to change the button color to white when the object is selected and back to default color when object is deselected. Button color should remain white as long as the object is selected. Kindly give solutions based on the code below. Thank!

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;  

Solution

  • This answer is all in Python, so you can convert it to MEL if you insist on using it.

    Script jobs can trigger in many different ways when an event occurs. This could be things like when the time has changed, the user does an undo, or in your case when the selection has changed.

    You can get a full list of these event names by running this:

    cmds.scriptJob(listEvents=True)

    The one you're looking for is "SelectionChanged".

    To get this to work you need to define a function that will be called when the script job is triggered (when the selection changes). Here's a simple example of that.

    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)
    

    So in your case when you run the function it can check if the object is selected, and depending on if it is or not you can make it color the button.

    When your tool closes you can remove the script job by using its kill parameter so that it's no longer running when you don't need it.

    And as as side note that you wrote to Haggi unless you have a good reason I would just stick to Python instead of MEL. The syntax is much easier, it has insane amount of libraries, and it's just more powerful to do simple things like manipulating strings. Plus Python is used in many other softwares, MEL is not. It's true that there are some commands that can only be done in MEL but you can easily evaluate a MEL string with Python.