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 windowwindow;
// define the layout of controls added
// to the window.columnLayout;
// create some texttext -label "hello world";
// show the window we last createdshowWindow;
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.
procfunc() {
print("button clicked\n");
}
// create a windowwindow;
// define the layout of controls added
// to the window.columnLayout;
// create a buttonbutton -label "click me" -command "func";
// show the window we last createdshowWindow;
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 windowwindow;
columnLayout;
// create three symbol buttons with related mel commandsymbolButton -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.
procon_func() {
print("checkbox on!\n");
}
// a function to be called when the checkbox gets unchecked.
procoff_func() {
print("checkbox on!\n");
} {
// create a windowwindow;
// 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 createdshowWindow;
// 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 buttonradioCollection -edit -select $on $radio1;
radioCollection -edit -select $X $radio2;
// now show the windowshowWindow;
// 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 procfloatValueChanged() {
global string $floatFieldName;
// query the value from the float field
$value = `floatField -query -value $floatFieldName`;
// print valueprint("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 procintValueChanged() {
global string $intFieldName;
// query the value from the float field
$value = `intField -query -value $intFieldName`;
// print valueprint("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 textFieldglobal proctextValueChanged(string $control) {
// query the value from the float field
$value = `textField -query -value $control`;
// print valueprint("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 nametextField -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 windowwindow;
// create a pane layout to hold the 4 scroll fieldspaneLayout -configuration "horizontal4";
// create 4 scroll fieldsscrollField -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.
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.
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.
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.
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 windowwindow;
// create a re-sizing form layoutformLayout form;
// create a channel box widget channelBox ch;
// attach the form layout and the channel boxformLayout -e
-af ch"top" 0
-af ch "left" 0
-af ch "right" 0
-af ch "bottom" 0
form;
showWindow;
}