Mel: Writing a UI

From wikinotes

Finding Maya UI items

lsUI -type 'helpLine';

Windows

Confirm Dialog

Confirm Dialog Boxes are MessageBoxes in MEL. They are primarily to deliver a message to the user. However, you can find get their button choice to execute other commands later. No commands can be executed directly from a button in a Confirm Dialog box to my knowledge.

confirmDialog -title "Warning"              			//standard confirmDialog Box
	-message "Operation Successful"
	-button "OK";


string $returnVal = `confirmDialog				//confirmDialog Box with a variable that remembers button choice
	-title "Perform Operation"
	-message "Are you sure you want to Perform Operation"
	-button "Yes"
	-button "No"
	-defaultButton "Yes"
	-cancelButton "No"
	-dismissString "Operation was Cancelled"`;

Prompt Dialog

Same as a ConfirmDialog box, except with a text form you can use.

string $buttonResponse = `promptDialog				//Create a variable to remember what is typed into the form
	-title "Make New Sphere"
	-message "Enter Name for New Sphere"
	-button "Ok"
	-button "Cancel"
	-defaultButton "Ok"`;

string $textResponse = `promptDialog -q`; 			//What was typed into the form

Window

A totally custom window. Unfortunately there is no standard way of having a defaultButton.

window						//Create and show a standard Window with a Menu
	-title "Window1"
	-menuBar true
	-widthHeight 400 200 windowName;

showWindow windowName;

menu -label "File";				//Edit the contents of the Window Menu
	menuItem -label "save";
	menuItem -label "save as";

menu -label "Help";
	menuItem -label "createSphere" -command "sphere";

Layout Types

Form Layouts (The master Layout)

With the form Layout you can precisely control the window exactly how you want. To ensure proper behavior, it is recommended that you define all sides of each element (top,left,right,bottom)

window exampleWindow;

formLayout -numberOfDivisions 100 exampleForm;
	button -label "I'm Grumpy" grumpyBTN;
	button -label "Create"     createBTN;
setParent..;

formLayout -edit

	-attachPosition grumpyBTN "top"     0 40
	-attachForm     grumpyBTN "right"  30
	-attachForm     grumpyBTN "left"   10
	-attachNone     grumpyBTN "bottom"

	-attachNone     createBTN "top"
	-attachPosition createBTN "right"   2 50
	-attachForm     createBTN "left"    5
	-attachForm     createBTN "bottom"  5

exampleForm;

showWindow exampleWindow;

FormLayout Parameters:

-attachForm button1            "top"   25	               //pins button1's top border 25 px from
							                                    //it's parent form's top border

-attachOppositeForm    button1 "left" -25			         //button1's left side is pinned 25 pixels
						                                       //from the form's right side


-attachControl         button1 "right"  5  button2		   //button1 will always be 5 pixels to the 
							                                    //left of button2


-attachOppositeControl button1 "right" 100 button2 	   //button1 is always 100 pixels to the
							                                    //left of button2's right side

-attachPosition        button1 "left"    5  50	         //if Divisions were set to 100, this
							                                    //will be 5 pixels left of 50% of
							                                    //the frame

-attachNone            button1 "right"				         //right side is not fixed to anything

Automated Layouts

rowLayout;						//puts objects one after another horizontally in a row

columnLayout;					//stacks objects in a column

rowColumnLayout -nc2;		//puts objects in a row until the number of items matches the nc#
									//then moves to the next row and continues

Parent Layouts

These layouts cannot contain objects directly, but have specific features and allow you to put other layouts inside them

/*
FrameLayouts are those collapsable frames you find in your render globals. They are also nice to use
as window blocks  when you want multiple layouts within one window. They can be collapsible if you wish.
NOTE** it is mispelled collapsable
*/

frameLayout -collapsable true			//a basic frameLayout
	-label "FrameLayout 1"
	-width 475
	-borderStyle "etched in";




scrollLayout;					//allows you to scroll when the window is too small for the contained information




tabLayout;						//Tab Layout
	string $tab1 = `columnLayout Tab1`;
		button;
		button;
	setParent..;
	
	string $tab2 = `rowLayout Tab2`;
	setParent..;
setParent..;




gridLayout						//gridLayout
	-numberOfRowsColumns 2 3                        //obects fill 3 rows then progress			
	-cellWidthHeight 75 20;                         //to the next column

setParent..;

paneLayout

This technically belongs under parent Layouts, but it merits being highlighted with it's own entry. A pane layout creates something similar to a window within a window. If you think of the Hypershade menu, and how you can move the middle section to make the graphNetworks section and the Materials list bigger or smaller - that is the effect created by two panes.

you can have up to 4 panes sharing a single window space. other configurations include:

  • horizontal2
  • vertical2
  • quad
  • horizontal3
window;

paneLayout -configuration "horizontal2" myPanels;

	//everything in this section is in pane1
	frameLayout paneFrame;
		button;
		button;
	setParent..;

setParent myPanels;

	//everything in this section is in pane2
	text -label "blahblahblah";

setParent myPanels;

showWindow;




Closing a Layout

to close a layout before adding elements to another, use setParent..;

setParent..;

UI Elements

Checkboxes

checkBox -label "CheckBox1" 					//Create a Checkbox
	-onCommand "sphere"
	-offCommand "pcube" myCheckBox;						



checkBox -q -v myCheckBox;					//Check Value of CheckBox

Buttons

button -label "button_00"				//Standard Button
	-command "sphere" button00BTN;


textFieldButtonGrp -label "Sphere Name:"		//textFieldButtonGrp
	-text "Sphere"
	-buttonLabel "MakeSphere"
	-buttonCommand "sphere" myTextFieldButtonGrp;

Result:
--------------------------------------------------------
|  Sphere Name: | sphere(field) |  Make Sphere(button) |
--------------------------------------------------------

Radio Buttons

A radio button group is a collection of radio buttons. Only one button can be selected at a time. A radio buton group has a maximum of 4 buttons

radioButtonGrp -numberOfRadioButtons 4				//Radio Button Group
	-label "Radio Button Group"
	-labelArray4 "X axis" "Y axis" "Z axis" "No Axis"
	-onCommand1 "sphere"
	-onCommand2 "createPolygonCube";
	-select 2 myRadioButtonGrp;


radioButtonGrp -q -select myRadioButtonGrp;

If 4 Radio buttons are not enough, you can connect them by using shareCollection.

radioButtonGrp
	-numberOfRadioButtons 2
	-label "How do you feel"
	-labelArray "Good" "Bad"
	-select 1 myRadioButtonGrp;

radioButtonGrp
	-numberOfRadioButtons 2
	-label ""
	-labelArray2 "Ok" "Don't Know"
	-shareCollection myRadioButtonGrp
	myRadioButtonGrp2;

Text

text -wordWrap 1 "this is your text. It is not editable";

textFieldGrp -label "Instructions: type in the following box."
	-text "type Here";

**there is also a floatFieldGrp AND a intFieldGrp

Sliders

floatSliderGrp -label
	-field false
	-minValue 0.0 -maxValue 10.0
	-value 1 myFloatSliderGrp;

** There is also an intSliderGrp

textScrollList

textScrollList								//Standard textScrollList
	-append "first list item"
	-append "second list item"
	-append "third list item"
	-allowMultiSelection 1 myTextScrollList;



string $textInScroll[] = `textScrollList -q -ai myTextScrollList`;	//find out what the textScrollList already contains

string $selected[] = `textScrollList -q -selectItem myTextScrollList`;	//query selected objects in textScrollList

optionMenu (dropdown)

optionMenu ...
menuItem ...


optionMenu -q -ill optionMenuName;			## list all child-menuitems

statusLine, messageLine (QStatusBar)

string $window = `window -menuBar true`;
menu -label "File";
menuItem -label "New" -annotation "Help for New";
menuItem -label "Open" -annotation "Help for Open";
menuItem -label "Close" -annotation "Help for Close";

string $form = `formLayout`;
string $column = `rowLayout -numberOfColumns 4
-columnWidth4 32 32 32 32 -columnAttach4 both both both both`;
button -label "A" -height 32 -annotation "Help for A";
button -label "B" -height 32 -annotation "Help for B";
button -label "C" -height 32 -annotation "Help for C";
button -label "D" -height 32 -annotation "Help for D";
setParent ..;

string $frame = `frameLayout -labelVisible false`;
helpLine;
formLayout -edit
-attachForm $column "top" 0
-attachForm $column "left" 0
-attachNone $column "bottom"
-attachForm $column "right" 0

-attachNone $frame "top"
-attachForm $frame "left" 0
-attachForm $frame "bottom" 0
-attachForm $frame "right" 0
$form;

showWindow $window;