Project 10.1: A Model of Modality


In this project, we will use a basic function of MEL, the for loop, to rename all the objects the user has selected. While the actual code for this is exceedingly simple, we will implement the promptDialog , the confirmDialog , and the progressWindow .

Before implementing the windows , we will take a quick look at the code, seen in Example 10.32, simply so we are familiar with it and can find the ideal places to add in our dialogs.

Example 10.32: The procedure to rename the selection list.

 global proc massRename (string $newName)     {  // get selection list  string $selList[] = `ls selection`  // parse the list and rename  int $i = 0;          for ($each in $selList)         {  // rebuild selection list to account for   // renaming of parents and possibly having   // multiple objects with the same short name  string $newList[] = `ls selection long`;         rename $newList[$i] ($newName + $i++);         }     } 
On the CD  

The text for this script is found on the companion CD-ROM as Chapter_10/project_UI01/v01/massRename.mel.

As it stands now, this requires the user to enter the command from either the Command Line, Script Editor, or Command Shell so that we can pass it text to be used as the string variable $newName . Since this command would be much more useful as a simple shelf button or menu item, we will replace the passing of $newName with a prompt dialog, similar to the Prefix Hierarchy command that exists already. In addition, since this could be a rather drastic action, we will enclose the entire body of the procedure with an if statement, which will use a confirmDialog as its condition. Finally, because we are using a for loop as the main functionality of the script, we can easily use a progressWindow to give the end user feedback as the loop parses.

First, we remove the passing of the variable from the procedure declaration, and use a promptDialog to get the information we need. In Example 10.33 we see the addition of the dialog.

Example 10.33: Using a prompt dialog in place of a passed variable.

 global proc massRename ()     {     string $newName;     string $promptResult = `promptDialog                                 -title                                     "Mass Rename Objects"                                 -message "Enter New Name:"                                 -button "OK"                                 -button "Cancel"                                 -defaultButton "OK"                                 -cancelButton "Cancel"                                 -dismissString "Cancel"`                                 ;     if ($promptResult == "OK")         {         $newName = `promptDialog -query`;  // get selection list  string $selList[] = `ls selection` 
On the CD  

The text for this script is found on the companion CD-ROM as Chapter_10/project_UI01/v02/massRename.mel.

Next, we will confirm that the user does, in fact, want to rename the objects. We got the selection list first so that we can use that data in the dialog. While not necessary, it adds some nice detail and class to our script. The code for this addition is seen in Example 10.34.

Example 10.34: Adding a Confirm dialog.

 string $confirm = `confirmDialog                         -title "Confirm Mass Rename"                         -message ("Do you really want                             to rename "                             + `size $selList`                             + " objects?")                         -button "Yes"                         -button "No"                         -defaultButton "Yes"                         -cancelButton "No"                         -dismissString "No"`;     if ($confirm == "Yes")     { 
On the CD  

The text for this script is found on the companion CD-ROM as Chapter_10/project_UI01/v03/massRename.mel.

Now, we go into the for loop. We first find how much each iteration adds to the entire percentage of objects and use that in our Progress window updating. Note that we use 1.0 in our calculation to guarantee that a float value is returned. In Example 10.35 we see how the Progress Window is added. It is often good practice to find the increase amount outside the loop and assign it to a variable to avoid the repeated division operations required if the new amount was calculated in an inline arithmetic statement.

Example 10.35: Addition of a Progress window.

  // Build Progress Window  float $increase = (1.0/`size $selList`);     float $amount = 0;     progressWindow         -title "Renaming Object"         -progress $amount         -status "Renaming: 0%"         -isInterruptable true         ;  // parse the list and rename  int $i = 0;     for ($each in $selList)         {  // Check if the dialog has been cancelled  if (`progressWindow -query -isCancelled`)             break;  // rebuild selection list to account for   // renaming of parents and possibly having   // multiple objects with the same short name  string $newList[] = `ls selection long`;         rename $newList[$i] ($newName + $i++);         $amount+= $increase;         progressWindow             -edit             -progress $amount             -status ("Renaming: "+$amount+"%");         }       progressWindow -endProgress;     } 
On the CD  

The text for this script is found on the companion CD-ROM as Chapter_10/project_UI01/final/massRename.mel.

Now, by simple executing the command massRename , the user is given a dialog to give the new object s name, and then given the opportunity to back out of the operation. Because the user can select any number of objects, this operation could take quite a while; its simple politeness to add the Progress window.

Project Conclusion and Review

Using modal dialogs is a simple, effective way of adding interactivity and polish to a tool. Without much effort, we went from a command-line-only tool to one with all the polish and class of those that come with Maya. Adding ease of use is often the last step of a script, and therefore the most often neglected. Modal dialogs are so easy to implement that there is no excuse not to implement them when appropriate.




The MEL Companion
The MEL Companion: Maya Scripting for 3D Artists (Charles River Media Graphics)
ISBN: 1584502754
EAN: 2147483647
Year: 2003
Pages: 101

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net