Window naming
 
 
 

As user interface scripts get more complex, it is possible that you may make a syntax error. If a user interface script partially executes and halts on an error, the window still exists, but the window is not visible. If you fix the problem and would like to execute the script again, you must either give the window a new name, or delete the hidden window.

Resolving naming conflicts

In the next section, you use conditional statements to resolve window naming conflicts. Conditional statements allow a section of code to execute depending on the value of a variable or the state of an object in the scene.

To resolve conflicting window names

  1. Create a window with a scripting error inserted before the showWindow command by typing the following in a MEL tab of the Script Editor:
    window testwindow; fake_command; showWindow;

    An error is output to the Script Editor.

    // Error: line 2: Cannot find procedure "fake_command;". // 

    The window is not displayed, but still exists.

  2. Create another window with the same name by typing the following in a MEL tab of the Script Editor:
    window testwindow; showWindow;

    An error is output to the Script Editor.

    // Error: line 1: Object's name is not unique: testwindow //

    All objects in Maya and user interface elements must have unique names.

To query a window’s existence

  1. Type the following command in the Script Editor input section, to delete the hidden window:
    deleteUI testwindow;
  2. Check if the window exists by typing the following command:
    window -exists testwindow;

    The following result is output to the Script Editor.

    // Result: 0 // 

    The exists flag of the window command changes the command so that it either returns a one or zero depending on whether or not the window exists.

  3. Type the following in the Script Editor:
    deleteUI non_existing_window; sphere;

    An error is output to the Script Editor.

    // Error: line 1: Object not found: non_existing_window

    The sphere command does not execute as the script has halted on the scripting error.

To be able to delete the existing user interface only if it exists, conditional statements can be used.

To delete a window on condition

  1. Type the following in a MEL tab of the Script Editor:
    window testwindow; fake_command; showWindow;

    The window is invisible as there is a scripting error, and the showWindow command did not execute.

  2. Type the following in a MEL tab of the Script Editor:
    if (`window -exists testwindow`==1) { deleteUI testwindow; } window -resizeToFitChildren 1 testwindow; columnLayout; 	text 		-label "This window deleted the other one"; showWindow;

    Two concepts are being introduced here:

Storing control names

If you refer to a control in your user interface, it must have a name. Instead of manually specifying the name for each user interface element, you can allow the command to generate unique default names and then you can store the name as a variable.

All user interface commands return their full name and path upon creation. You can store the return value as a variable to refer to the control at a later point in the script.

To store a control name as a variable

  1. Type the following in a MEL tab of the Script Editor:
    window -resizeToFitChildren 1 pick_me_window; columnLayout; 	$button_one=`button -label "Click Me!" 		-command "deleteUI $button_two"`; 	$button_two=`button -label "NO!, Click Me!!" 		-command "deleteUI $button_one"`; 	$button_close=`button -label "Close" 		-command "deleteUI pick_me_window"`; showWindow;

    We’re using backticks for evaluation again here, though in this case, we’re creating three variables ($button_one, $button_two, and $button_close) that hold the results of calling the button command each time.

  2. Press one of the “Click Me!” buttons in the window to test the script.

    Clicking one of the “Click Me!” buttons deletes the other button by calling the deleteUI command with the variable that stores the name of the button control as an argument (button_one or button_two).

    (If you click the button a second time, you’ll get an error as the button you’re trying to delete has already been deleted.)

  3. Close the window by pressing the Close button in the window.

    When you created your user interface window, you assigned it a unique name manually. This is a good practice to ensure that you cannot open multiple copies of a user interface. With more complicated user interfaces, multiple copies of the same user interface could interfere with each other or slow down Maya.

    NoteYou can view the full name and path of a user interface elements by using the print command to output the variable.

    print $button_one;

    The following is output to the Script Editor:

    pick_me_window|columnLayout2|button1

    This is the full path of the user interface control. You are using variables to store the name and path of controls and therefore you do not have to type the whole name and path to refer to user interface elements.

In this section, you learned how to create, delete, and check for the existence of named windows. In the next section, you’ll hook up a more complex window to a procedure.