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.
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
window testwindow; fake_command; showWindow;
An error is output to 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.
deleteUI testwindow;
window -exists testwindow;
The following result is output to the Script Editor.
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.
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
window testwindow; fake_command; showWindow;
The window is invisible as there is a scripting error, and the showWindow command did not execute.
if (`window -exists testwindow`==1) { deleteUI testwindow; } window -resizeToFitChildren 1 testwindow;
columnLayout; text -label "This window deleted the other one"; showWindow;
Together, the if statement and the evaluation mean that only if the result of the window existence query is “1” (the window exists) is the window deleted. Otherwise, the section of code within the curly braces is skipped.
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
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.
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.)
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.
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.