Linking the user interface

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.

NoteFor more information on the ls command, see ls in the MEL Command Reference.

For more information on arrays, see Arrays.

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

  1. Create a new MEL tab (Command > New Tab in the Script Editor).
  2. Rename it tempMEL (Command > Rename Tab in the Script Editor)
  3. Type the following in the tempMEL tab:
    select -allDagObjects;

    This command selects all scene objects.

  4. Type the following in the tempMEL tab:
    $all_selected_objects =`ls -selection`;

    This command lists currently-selected objects and outputs their names to a variable.

  5. Type the following in the tempMEL tab:
    print $all_selected_objects;

    This command outputs the list of stored objects to the Script Editor.

  6. Execute the commands in the tempMEL tab.

    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.

NoteArrays are used frequently in programming to manage large sets of data. If arrays were not used to store large sets of data, you would need to create a variable for each element of data, making scripts hard to maintain and taking up lots of memory. For more information on arrays, see Arrays.

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

  1. Add the following commands to the top of your user interface script in the MEL2 tab:
    select -allDagObjects;
    string $all_selected_objects[]=`ls -selection`;
  2. In the MEL2 tab, change the argument of the text flag located on line 14 of the original script (you can use the MEL tab for reference) from “name_of_object” to the following:

    The command now reads:

    $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.

Linking the check boxes

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

  1. In the tempMEL tab, change the state of the make roll window’s check box by executing the following:
    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.

  2. Add the following under the existing check box declarations (string $box_sim_checkbox and string $sphere_sim_checkbox) in the MEL2 tab. This will be approximately lines 34-35 depending on whether you added lines at the beginning of the file.
    checkBox -edit -changeCommand ("checkBox -edit -value (!#1) "+ $sphere_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 (!).

    Finally, add the following.

    checkBox -edit -changeCommand ("checkBox -edit -value (!#1) "+ $box_sim_checkbox) 

    This is the same as the previous lines except it changes the “Box Simulation” checkbox when “Sphere Simulation” is changed.

Executing the procedure

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

  1. Change the value of the diameter slider to 10.1 by dragging the slider to the right, or by typing a value in the diameter slider’s text field.

  2. Print the value of the diameter slider by typing the following in the tempMEL tab:
    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

  1. Add the following to the top of the MEL2 tab:
    global proc makeRoll_calback (string $obj_name_text, string $ground_int, string $box_sim_checkbox, string $diameter_float)
     `textField -query -text $obj_name_text` 
     `intSliderGrp -query -value $ground_int` 
     `checkBox -query -value $box_sim_checkbox` 
     `floatSliderGrp -query -value $diameter_float`;
  2. 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

  1. In the MEL2 tab, replace the -command "print (\"something\");"; line with the following.
    -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).