[HOME] [MEL] [API]

Mel Script - User Interface Controls

Text
Button
Symbol Button
Checkbox
Radio Button

Float Field
Int Field
Text Field
Scroll Field

Float Slider

Int Slider
Float Slider Grp
Int Slider Grp

Channel Box Control

 

 

All user interface controls work on a similar premise, you can assign commands to specific events that the item generates. For example, clicking of a button, state change of a checkbox etc.

 

 

 

 

 

 

Example 1 : Simple Text

The mel command text allows us to specify some simple text within the user interface.

 

 


	// create a window
	window;

		// define the layout of controls added 
		// to the window.
		columnLayout;

			// create some text
			text -label "hello world";

	// show the window we last created
	showWindow;

 

 

 

 

Example 2 : Simple Button

buttons are the most basic of all user interface controls. This example simply attachs a command to the button.

 

 


	// a function to be called when the button gets clicked.
	proc func() {
		print("button clicked\n");
	}

	// create a window
	window;

		// define the layout of controls added 
		// to the window.
		columnLayout;

			// create a button
			button -label "click me" -command "func";

	// show the window we last created
	showWindow;

 

 

 

 

Example 3 : Symbol Button

Symbol buttons work in the same way as normal buttons except that they display an image instead of a text label. Use the -image flag to specify the image to use; you may use either bmp or xpm images (gimp can create xpm images).

 

 


			// create window
			window;
			columnLayout;
			
				// create three symbol buttons with related mel command
				symbolButton -image "circle.xpm" -command "circle";
				symbolButton -image "sphere.xpm" -command "sphere";
				symbolButton -image "cube.xpm" -command "polyCube";
			showWindow;

 

 

 

 

Example 4 : Simple Checkbox

Checkboxes simply store an on/off value. You can either set up commands to run when the state changes, or you can manually query the value from the checkbox.

 

 


	// a function to be called when the checkbox gets checked.
	proc on_func() {
		print("checkbox on!\n");
	}

	// a function to be called when the checkbox gets unchecked.
	proc off_func() {
		print("checkbox on!\n");
	}

	{
		// create a window
		window;
	
			// define the layout of controls added 
			// to the window.
			columnLayout;
	
				// create a checkbox
				$c = `checkBox -label "thingy" 
						 -onCommand "on_func" 
						 -offCommand "off_func"`;
	
		// show the window we last created
		showWindow;
		
		
		// to get the current value of the checkBox, use the -query flag
		$value = `checkBox -query -value $c`;
	
		print("check_box value = "+ $value +"\n");
	}

 

 

 

 

Example 5 : Radio Button

Radio Buttons require the use of both the radioButton and radioCollection mel commands. First we create a radioCollection and then create the radioButtons that need to be part of the collection. The following example creates two radio collections.....

 

 

			
		{
			window;
				columnLayout;
		
					// create the first radio collection
					$radio1 = `radioCollection`;
		
						// add some radio buttons to the collection
						$on  = `radioButton -label "On"`;
						$off = `radioButton -label "Off"`;
		
					separator -w 50 -style "single";
		
					// create the second radio collection
					$radio2 = `radioCollection`;
		
						// add some radio buttons to the collection
						$X = `radioButton -label "X"`;
						$Y = `radioButton -label "Y"`;
						$Z = `radioButton -label "Z"`;
		
			// edit the radio collections to set the required radio button
			radioCollection -edit -select $on $radio1;
			radioCollection -edit -select $X $radio2;
		
			// now show the window
			showWindow;
		
			// If you need to query the selected radio button, use...
			$selected = `radioCollection -query -select`;
		}

 

 

 

 

Example 6 : Float Field's

Float fields can be used to provide numerical text input into your user interfaces. For integers you will need an intField, for strings you can use the textField control.

 

 



			// Global Data and Functions. The callbacks need
			// to be global for the user interface command to be 
			// able to call them. Any user interface event will occur
			// AFTER the script has finished executing. Any commands
			// you call must therefore be available within any scope,
			// ie they must be global.
			
			// the name of the float field 
			global string $floatFieldName=""; 
			
			global proc floatValueChanged() {
				global string $floatFieldName;
				
				// query the value from the float field
				$value = `floatField -query -value $floatFieldName`;
				
				// print value
				print("newValue="+ $value +"\n");
			}

			// window creation scoped to prevent unesseccary
			// globals being defined
			{
				string $window = `window`;
				columnLayout;
					floatField;
					floatField -editable false;
					floatField -minValue -10 -maxValue 10 -value 3;
					floatField -precision 4 -step .01;
					$floatFieldName =
						`floatField -changeCommand "floatValueChanged();"`;
				showWindow $window;
			}

 

 

 

Example 7 : Int Field's

Int fields can be used to provide numerical text input into your user interfaces. For floats you will need a floatField, for strings you can use the textField control.

 

 

			
			// Global Data and Functions. The callbacks need
			// to be global for the user interface command to be 
			// able to call them. Any user interface event will occur
			// AFTER the script has finished executing. Any commands
			// you call must therefore be available within any scope,
			// ie they must be global. 
			// 
			//	There are a couple of ways we can get around having 
			// a global variable, but more on that later...
			global string $intFieldName=""; 
			
			global proc intValueChanged() {
				global string $intFieldName;
				
				// query the value from the float field
				$value = `intField -query -value $intFieldName`;
				
				// print value
				print("newValue="+ $value +"\n");
			}
			
			// window creation scoped to prevent unesseccary
			// globals being defined
			{
				string $window = `window`;
				columnLayout;
					intField;
					intField -editable false;
					intField -minValue -10 -maxValue 10 -value 3;
					intField -changeCommand "intValueChanged();";
				showWindow $window;
			}


 

 

 

Example 8 : Text Field's

Text fields can be used to provide text input into your user interfaces. For floats you will need a floatField, for int's you can use the intField control. If you require larger multi-line text input then look at the Scroll Field example.

 

 



		// a procedure to be called when the text 
		// value changes in the textField
		global proc textValueChanged(string $control) {

			// query the value from the float field
			$value = `textField -query -value $control`;
				
			// print value
			print("newValue="+ $value +"\n");
		}

		{	
			window;
				columnLayout;
	
					// create the text field
					$field = `textField`;
	
					// sets the command to call when the text changes.
					// Note that we add the name of the control directly
					// into the command string. This negates the need 
					// for a global variable to store the textField name
					textField -edit 
					    -changeCommand ("textValueChanged("+$field+")")
					    $field;
			showWindow;
		}

 

 

 

Example 9 : Scroll Field's

The scroll field defines a large mulit-line text edit control. For example, Maya's script editor is composed out of two scroll fields; one for the results, one for the code being edited.

 

 


	{		
		// create the window
		window;
		
			// create a pane layout to hold the 4 scroll fields
			paneLayout -configuration "horizontal4";

				// create 4 scroll fields
				scrollField -wordWrap true 
					-text "Non editable with word wrap" -editable false;
				scrollField -wordWrap false 
					-text "Non editable with no word wrap" -editable false;
				scrollField -wordWrap true 
					-text "Editable with word wrap";
				scrollField -wordWrap false 
					-text "Editable with no word wrap";
		showWindow;
	}

 

 

 

Example 10 : Float Sliders

Float Sliders tend to be used less than the float slider group controls available in mel. This is mainly because a simple slider with no label has *limited* uses.

 

 

			
	{
		window;
			columnLayout -adjustableColumn true;
				floatSlider;
				floatSlider -min -100 -max 100 -value 0 -step 1;
		showWindow;
	}

 

 

 

 

Example 11 : Int Sliders

Int Sliders tend to be used less than the int slider group controls available in mel. This is mainly because a simple slider with no label has *limited* uses.

 

 

			
	{
		window;
			columnLayout -adjustableColumn true;
				intSlider;
				intSlider -min -100 -max 100 -value 0 -step 1;
		showWindow;
	}

 

 

 

 

Example 12 : Float Slider Group

The floatSliderGrp mel command creates a float slider with a text label and a float field input box. This tends to be more useful than the floatSlider on it's own.

 

 


		{
			window;
				columnLayout;
					floatSliderGrp -label "Slide Me" -field true;
					floatSliderGrp -label "Limits" -field true
						-fieldMinValue -50 -fieldMaxValue 50
						-minValue -10 -maxValue 10 -value 0;
			showWindow;
		}

 

 

 

 

Example 13 : Int Slider Group

The intSliderGrp mel command creates an int slider with a text label and an int field input box. This tends to be more useful than the intSlider on it's own.

 

 

			
		{
			window;
				columnLayout;
					intSliderGrp -label "Slide Me" -field true;
					intSliderGrp -label "Limits" -field true
						-fieldMinValue -50 -fieldMaxValue 50
						-minValue -10 -maxValue 10 -value 0;
			showWindow;
		}
		

 

 

 

 

 

Example 14 : Channel Box Control

mel also contains some of the more high level Maya controls. This example demonstrates a channel box, other high level controls include 3D views, outliner panels etc.

 

 

			
	{
		// create the window
		window;
		
		// create a re-sizing form layout
		formLayout form;
		
		// create a channel box widget 
		channelBox ch;
		
		// attach the form layout and the channel box
		formLayout -e
			-af ch"top" 0
			-af ch "left" 0
			-af ch "right" 0
			-af ch "bottom" 0
			form;
		showWindow;
	}

 

 

 

Related Documents

Windows & A quick GUI overview

Layouts

Menus

Attribute Controls

Common Dialogs

 

 

[HOME] [MEL] [API]