-1

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;  
Green Cell
  • 4,677
  • 2
  • 18
  • 49
pro nerd
  • 13
  • 5
  • SO isn't a script writing service so seeing how this question is about scriptJobs, what have you tried and what is the issue it's giving you? The Maya docs have very clear explanations on how scriptJobs work. – Green Cell Mar 28 '19 at 01:50
  • May a ask why you switched to mel instead of python? With ui, python simplifies your life a lot. – haggi krey Mar 28 '19 at 09:33
  • Thanks guys for the time. First of all Maya docs are good when you know programming, I'm animator and for so long I have ignored programing for the fear of it. I have no programing background. I have lately been learning and asking around a lot and seeing the code in action and wrap my head around it helps my learning curve. So this may seem easy but I will be honest it's not to a beginner like me. I have only head about the scriptjob from my other question when some suggested it. – pro nerd Mar 28 '19 at 11:07
  • I open Maya docs baam I have no idea whats going on. So I know I can use: $Job1 = scriptJob -e "RecentCommandChanged" "colorChange"; $Job2 = scriptJob -e "SelectionChanged" "colorChange"; but plugin this to my code without help it's nightmare. haggi krey I have not switch from python to mel actually I'm kind of learning both languages I believe knowing mel too would help me convent codes to python easily as Maya spit mel on anything. Thanks again. – pro nerd Mar 28 '19 at 11:07
  • @pronerd That's fine that you're a beginner and trying to learn, we've all been there. Just understand that when you ask a question and no attempt has been made then one can only assume that you want the answer spoon fed to you. If you edit your question with what you have tried so far I'm sure you'll get a meaningful answer. – Green Cell Mar 28 '19 at 15:31
  • 1
    Well, to make this task work, you need to know how to work with global variables, global procedures, how to use argments in procedures. So until you know how to deal with these things, any answer will cause frustration because it will not help you very much. Maybe it could help to start with a simpler version of your script. – haggi krey Mar 28 '19 at 15:33
  • Thank you all for advice. I understand all you are saying just few days ago I was having problems to get the button to select an object then from this site I got help on how the *args works and now I can make those buttons in my sleep. I seriously don't expect just answer but explanation on how it works answers a just for reference and I apply the concept to different scenarios that's how I have been learning. In this case I admit scriptjob seems too difficult me to understand that why I asked so I can follow,breakdown the code and apply it to different scenarios> Thanks again to ALL! – pro nerd Mar 28 '19 at 16:54

1 Answers1

1

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.

Green Cell
  • 4,677
  • 2
  • 18
  • 49
  • I could have never asked for a better explanation this just awesome easy to understand and to the point. And yes I will sticking with python like you said python is used in many software, I will focus more on it, Thank you. – pro nerd Mar 29 '19 at 08:15
  • Trust me, something you need to do in 5-6 lines in MEL will be done as a one-liner in Python :P – Green Cell Mar 29 '19 at 09:10