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

Reduce your selection by extracting a single value from an array using an array index.

To reduce the selection to one object

  1. In the tempMEL tab, type the following:
    $first_selected_object=$all_selected_objects[0];
    select $first_selected_object;

    The first element of the array, the roll_Cube, is selected.

    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.

  2. Add the following commands to the top of your user interface script in the MEL2 tab:
    $all_selected_objects=`ls -selection`;
    $first_selected_object=$all_selected_objects[0];
    select $first_selected_object;

    These commands change your selection to the object you selected first.

  3. 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:
    $first_selected_object

    The command now reads:

    $obj_name_text = 	`textField 	-editable 0 	-width 400 	-text $first_selected_object`;

    The text field now displays the currently selected object.

    If you attempt to execute the modified user interface creation script without an object selected, an error occurs, as no elements exist in the string array $all_selected_objects.

    TipDon’t execute the script from the MEL2 tab yet, as it will clear the tab and we want to continue modifying the script. If you do want to execute the script at any interim time, create a new MEL tab, copy the script into it, and execute that.

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 onCommand and offCommand flags 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. Replace the existing check box commands ($box_sim_checkbox and $sphere_sim_checkbox) in the MEL2 tab. This will be approximately lines 38-46 depending on whether you added lines at the beginning of the file. Replace these two commands with the following:
    $box_sim_checkbox = 	`checkBox 	-value 1 	-label "Box Simulation" 	-onCommand "checkBox -edit -value 0 $sphere_sim_checkbox;" 	-offCommand "checkBox -edit -value 1 $sphere_sim_checkbox;"`; $sphere_sim_checkbox = 	`checkBox -value 0 	-label "Sphere Simulation" 	-onCommand "checkBox -edit -value 0 $box_sim_checkbox;" 	-offCommand "checkBox -edit -value 1 $box_sim_checkbox;"`;

    The commands you are replacing are located on lines 35-43 of the original script file.

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

    The query flag allows you to output a value to the Script Editor.

Change the command flag of the button so that it calls makeRoll command with arguments values that are the same as the specified values in the user interface.

To link the button to the makeRoll procedure

  1. Replace the command flag of the execution button block in the MEL2 tab (approximately line 57) with the following:
    	-command "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. The backslash at the end of a line allows the command flag argument to span multiple lines.

Your user interface is now fully functional!

A completed version of the user interface script named Mel_UI_Final.mel is available in the GettingStarted project directory (GettingStarted/mel).