Currently the user interface in the window doesn’t do anything: moving the sliders and clicking the check boxes has no effect. In this section you learn how to link the user interface to the makeRoll procedure you used earlier:
In the following steps you modify the user interface script.
A completed version of the user interface script named mel_Lesson_4_finished.mel is available in the GettingStarted project directory, within the mel sub-directory.
Displaying the currently selected object
The makeRoll procedure does not act upon the currently selected object. You will modify the script so that the textField control will display the name of the currently selected object.
To return the name of the currently selected object, use the ls command (list) with the selection. Since you can select multiple objects in Maya, the return from the ls command is not a string, it is a string array, even if only one object is selected. An array is an ordered list of values used to store multiple items of the same data type within one variable. The makeRoll procedure cannot accept a string array as an argument, so you must modify the data returned by ls -selection.
In this next section, we’ll also create a new tempMEL tab so you’ll have a place to test commands as you learn them.
To store the names of the currently selected objects as a variable
select -allDagObjects;
$all_selected_objects =`ls -selection`;
This command lists currently-selected objects and outputs their names to a variable.
print $all_selected_objects;
This command outputs the list of stored objects to the Script Editor.
The following is output to the Script Editor:
roll_Cube pPlane1
The variable $all_selected_objects is an array of strings. That is, it stores multiple strings within the variable. Values in an array are called elements.
In this example, you only want to apply the roll_Cube procedure to one object in the scene. You can refer to individual elements of an array by using square brackets ([ ]).
To reduce the selection to one object
select -allDagObjects;
string $all_selected_objects[]=`ls -selection`;
$all_selected_objects[0]
$obj_name_text = `textField -editable 0 -width 400 -text $all_selected_objects[0]`;
Elements of an array are accessed using square brackets containing the index number of the element you want to extract from the array. The index number of arrays start counting from zero.
The text field now displays the first object created in the scene.
Check boxes can also use command flags, similar to a button. A check box has more functionality than a button, so the command flags are different. Check boxes have three possible command flags; changeCommand, onCommand, and offCommand:
You can use the checkBox command with an edit flag to change the state of a check box in the user interface.
To change the values of elements in the user interface
checkBox -edit -value 0 $box_sim_checkbox;
The Box Simulation check box, which was turned on, is now turned off. You can repeat this command with a value of 1 to turn the check box back on.
This shows the use of the edit flag with a command as an argument to a command flag, which allows you to change values.
We’ll now use the changeCommand flag to keep the two check boxes in sync, so that when one check box has its state changed, the other check box is set to the opposite state.
checkBox -edit -changeCommand ("checkBox -edit -value (!#1) "+ $sphere_sim_checkbox)
$box_sim_checkbox;
This block indicates that if the changeCommand flag is triggered on the “Box Simulation” checkbox, Maya edits the “Sphere Simulation” checkbox to the opposite of its current value (thus if you turn “Box Simulation” off, Maya turns “Sphere Simulation” on automatically).
In MEL, #1 is a stand-in character for the value of the checkbox. When Maya runs this line of code, it replaces #1 with the value of the checkbox. In programming, the symbol ! means “not.” So in the above code, Maya is changing the “Sphere Simulation” checkbox to whatever value (#1) it currently is not (!).
checkBox -edit -changeCommand ("checkBox -edit -value (!#1) "+ $box_sim_checkbox)
$sphere_sim_checkbox;
This is the same as the previous lines except it changes the “Box Simulation” checkbox when “Sphere Simulation” is changed.
The makeRoll procedure requires arguments to execute. To link it to the window, you must get the values for the arguments from the controls in the user interface, and execute the makeRoll procedure with these arguments.
To get the values of the controls, you must use the query flag. The query flag allows commands to return values of attributes in the scene.
To query a value in the user interface
print ("The diameter is: " + `floatSliderGrp -query -value $diameter_float`);
The following is output to the Script Editor
The diameter is: 10.1
The query flag allows you to output a value to the Script Editor.
We will add a new procedure in our script that uses the makeRoll command with arguments values that are queried from the user interface. This procedure will then be used by the Execute button.
To create a procedure for the Execute button
global proc makeRoll_calback (string $obj_name_text, string $ground_int, string $box_sim_checkbox, string $diameter_float)
{
makeRoll
`textField -query -text $obj_name_text`
`intSliderGrp -query -value $ground_int`
`checkBox -query -value $box_sim_checkbox`
`floatSliderGrp -query -value $diameter_float`;
}
Every argument of the user interface is defined by querying the user interface elements.
Now you can set the Execute button to call the procedure you just created.
To link the Execute command to a procedure
-command ("makeRoll_calback "+$obj_name_text+" "+$ground_int+" "+$box_sim_checkbox+" "+$diameter_float);
The parameters $obj_name_text, ground_int, box_sim_checkbox, and diameter_float, provide the procedure with the values it needs to execute the makeRoll script.
Your user interface is now fully functional.
A completed version of the user interface script named mel_Lesson_4_finished.mel is available in the GettingStarted project directory (GettingStarted/mel).