Project 11.1: Implementing Popups


Using layouts that construct user interface elements from separate controls is often necessary to fully exploit the benefits of using popup controls. For example, in Figure 11.10 we see a design for a user interface that integrates two float field groups representing the translation and rotation channels for transforms. Beyond the float fields we have eight buttons to get the values from the selected object, one to get all three, and one to retrieve each individual channel.

click to expand
Figure 11.10: Possible design for the window.

This window is overly cluttered, with the main purpose of the window, the float fields, overpowered by the buttons. If we were to create a window with two float field groups, as seen in Figure 11.11, we could attach a popupMenu to each that has commands to get the x, the y, the z, or all three.

click to expand
Figure 11.11: Alternative design for the proposed window.

If instead we use multiple rowLayouts , each with a text control and three individual float fields, we can attach separate popups to each of the controls, as seen in Figure 11.12.

click to expand
Figure 11.12: The best possible design.

Not only is this design efficient in terms of screen real estate, it also conforms to the behavior of the Attribute Editor.

Now that we have our finalized design, we can see a breakdown of elements in Figure 11.13.

click to expand
Figure 11.13: UI element breakdown.

We can then implement the script, using all the techniques we have learned so far. See Example 11.4.

Example 11.4: We begin our script.

 global proc massSetTransform ()     { 

First, we check for the existence of the window, and if it does not exist, we continue with the UI creation. We again use variables to define widths for UI elements to quickly alter the UI during script creation. See Example 11.5.

Example 11.5: We begin construction of our UI window.

 if( `window -exists massTransUI` != 1 )         {         int $uiLabelWidth = 80;         int $uiInputWidth = 240;         int $uiWidth = ( $uiLabelWidth + $uiInputWidth );             window             -maximizeButton false             -resizeToFitChildren false             -title "Set Transforms"             -iconName "Set Transforms"             -menuBar false             -wh 330 174             massTransUI; 
On the CD  

The text for this script is found on the companion CD-ROM as Chapter_11/project_UI04/v01/massSetTransform.mel.

Note that the window size was determined after the full window was designed, and then added to the file.

Add the main column layout that holds the row layouts. See Example 11.6.

Example 11.6: Adding the column to our UI.

 string $transColumn = `columnLayout             -adjustableColumn true             -rowSpacing 5`; 

Add the row layout that contains the labels at the top of each column. We use columnAlign and columnAttach flags to make sure the labels are properly centered. See Example 11.7.

Example 11.7: ng the row layout.

 rowLayout             -numberOfColumns 4             -columnAlign  2 center             -columnAlign  3 center             -columnAlign  4 center             -columnAttach 1 right 5             -columnAttach 2 both 0             -columnAttach 3 both 0             -columnAttach 4 both 0             -columnWidth 1 $uiLabelWidth             -columnWidth 2 ( $uiInputWidth / 3 )             -columnWidth 3 ( $uiInputWidth / 3 )             -columnWidth 4 ( $uiInputWidth / 3 )             trans_mainRow01; 

Now, add the labels. The first text control is empty to simply fill the far-left column. Again, we use the align flag on the text fields to guarantee centering of labels. See Example 11.8.

Example 11.8: Using text controls as labels.

 text                 -label "";             text                 -label "X"                 -align center;             text                 -label "Y"                 -align center;             text                 -label "Z"                 -align center;             setParent ..;                   rowLayout             -numberOfColumns 4             -columnAttach 1 right 5             -columnWidth 1 $uiLabelWidth             -columnWidth 2 ( $uiInputWidth / 3 )             -columnWidth 3 ( $uiInputWidth / 3 )             -columnWidth 4 ( $uiInputWidth / 3 )             trans_mainRow02;         text             -label "Translate"; 
On the CD  

The text for this script is found on the companion CD-ROM as Chapter_11/project_UI04/v02/massSetTransform.mel.

We begin to add popup menus to the UI. We use the default right mouse button to access the menu. We make a call to the procedure massSetGet , which we will soon write, and pass it the appropriate flag. See Example 11.9.

Example 11.9: The addition of popup menus to the window.

 popupMenu;                 menuItem                     -label "Get All Translate Values"                     -command "massSetGet t";         floatField             -width ( $uiInputWidth / 3 )             msTXField;             popupMenu;                 menuItem                     -label "Get Translate X"                     -command "massSetGet tx";         floatField             -width ( $uiInputWidth / 3 )             msTYField;             popupMenu;                 menuItem                     -label "Get Translate X"                     -command "massSetGet ty";         floatField             -width ( $uiInputWidth / 3 )             msTZField;             popupMenu;                 menuItem                     -label "Get Translate Z"                     -command "massSetGet tz";         setParent ..;         rowLayout             -numberOfColumns 4             -columnAttach 1 right 5             -columnWidth 1 $uiLabelWidth             -columnWidth 2 ( $uiInputWidth / 3 )             -columnWidth 3 ( $uiInputWidth / 3 )             -columnWidth 4 ( $uiInputWidth / 3 )             trans_mainRow03;                        text             -label "Rotate";             popupMenu;                 menuItem                     -label "Get All Rotate Values"                     -command "massSetGet r";         floatField             -width ( $uiInputWidth / 3 )             msRXField;             popupMenu;                 menuItem                     -label "Get Rotate X"                     -command "massSetGet rx";         floatField             -width ( $uiInputWidth / 3 )             msRYField;             popupMenu;                 menuItem                     -label "Get Rotate Y"                     -command "massSetGet ry";         floatField             -width ( $uiInputWidth / 3 )             msRZField;             popupMenu;                 menuItem                     -label "Get Rotate Z"                     -command "massSetGet rz";         setParent ..; 
On the CD  

The text for this script is found on the companion CD-ROM as Chapter_11/project_UI04/v03/massSetTransform.mel.

We can now add a button to execute the set procedure. The UI is simple enough that it doesn t need a formLayout , although we could use one to simplify any expansion of the user interface if we need to. We could even lock the window to prevent it from being resized, if we so desired. See Example 11.10.

Example 11.10: Adding a button to execute the set procedure.

 string $massSetButton = `button                         -height 30                         -label "Set On Selected"                         -command "doMassSet"`;          showWindow massTransUI;          }     } 
On the CD  

The text for this script is found on the companion CD-ROM as Chapter_11/project_UI04/v04/massSetTransform.mel.

Add the procedure that sets the values gathered from the window on all the selected objects. There is actually a lot more error checking we could be doing above and beyond just making sure the attribute exists, We could also check to make sure the channel is not locked, and that the channel is not being controlled by an outside influence, such as an expression. See Example 11.11.

Example 11.11: The procedure to set the transformation values on the selected objects.

 global proc doMassSet ()     {     string $selTrans[] = `ls -selection`;     float $tx = `floatField -query -value msTXField`;     float $ty = `floatField -query -value msTYField`;     float $tz = `floatField -query -value msTZField`;     float $rx = `floatField -query -value msRXField`;     float $ry = `floatField -query -value msRYField`;     float $rz = `floatField -query -value msRZField`;                            for ( $each in $selTrans )          {               if ( `attributeExists "tx" $each` == 1 )                    setAttr ( $each + ".tx" ) $tx;               if ( `attributeExists "ty" $each` == 1 )                    setAttr ( $each + ".ty" ) $ty;               if ( `attributeExists "tz" $each` == 1 )                    setAttr ( $each + ".tz" ) $tz;               if ( `attributeExists "rx" $each` == 1 )                    setAttr ( $each + ".rx" ) $rx;               if ( `attributeExists "ry" $each` == 1 )                    setAttr ( $each + ".ry" ) $ry;               if ( `attributeExists "rz" $each` == 1 )                    setAttr ( $each + ".rz" ) $rz;          }     } 
On the CD  

The text for this script is found on the companion CD-ROM as Chapter_11/project_UI04/doMassSet.mel.

This procedure, massSetGet , is the one called from the popup menus. It accepts a string argument, and depending on which one is requested , updates the appropriate field. See Example 11.12.

Example 11.12: The procedure called from the popup menus.

 global proc massSetGet ( string $attr )     {     string $selTrans[] = `ls -selection`;     if ( `size $selTrans` > 0 )          {           float $tx = `getAttr ( $selTrans[0] + ".tx" )`;          float $ty = `getAttr ( $selTrans[0] + ".ty" )`;          float $tz = `getAttr ( $selTrans[0] + ".tz" )`;          float $rx = `getAttr ( $selTrans[0] + ".rx" )`;          float $ry = `getAttr ( $selTrans[0] + ".ry" )`;          float $rz = `getAttr ( $selTrans[0] + ".rz" )`;          if ( ($attr == "tx")  ($attr == "t" ))               floatField -edit -value $tx msTXField;          if ( ($attr == "ty")  ($attr == "t" ))               floatField -edit -value $ty msTYField;          if ( ($attr == "tz")  ($attr == "t" ))               floatField -edit -value $tz msTZField;          if ( ($attr == "rx")  ($attr == "r" ))               floatField -edit -value $rx msRXField;          if ( ($attr == "ry")  ($attr == "r" ))               floatField -edit -value $ry msRYField;          if ( ($attr == "rz")  ($attr == "r" ))               floatField -edit -value $rz msRZField;         }      } 
On the CD  

The text for this script is found on the companion CD-ROM as Chapter_11/project_UI04/final/massSetTransform.mel.

This gives us the final window, seen in Figure 11.14.

click to expand
Figure 11.14: The finished window, with popups.



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