How to add a button to a window and execute a command through the button

Before adding commands to the window, we need to give the window a layout interface. It should be noted here that nothing can be added to a window without layout. There are many ways of layout. Here we use a columnLayout, which is a column layout with many parameters. Here we simply use -adj, which is used to align the elements on both sides of the window. Our sentence layout should be placed before the showWindow and before the window elements (buttons, etc.). ) to ensure the correct operation of mel.

Now add a button to the window. The mel of the button is the button. There are three suffixes here, namely -label, -h and-c.

-label: the title, that is, the name of the button, followed by the string, and give your button a name. We call it myButton here.

-h: height, which defines the height of the button, followed by a floating-point number.

-c: command, which determines what will be done when this button is pressed, followed by proc (program).

Let's write this button completely:

button-l“my button”-h 20-c”;

I'll leave it blank after -c here, because we haven't defined a proc for him to call. This is what we will finish next. Now let's put this mel in the position behind the layout, and then use//to block the content starting from -c, and run it to see what our window looks like now.

How to add a button to a window and execute a command through the button

We can already see our own windows and buttons. If we think the button is too small, we can add value after -h, but now this button doesn't play any role. We need to define a program for it to use.

Use proc to define a custom program with the following format:

Procedure name (line parameter) {content}.

The line parameter here may not be easy to understand, but fortunately, simple commands are not needed, just leave it blank. Here we customize a program that can complete the alignment function.

The aligned mel is:

string $ ParCons[]= ` parent constraint `;

Delete $ ParCons

These two sentences mel can align the object selected later with the object selected first. In fact, they simply used the parent-child link. There is not much explanation here. We started using proc to define programs.

Procedure ALT(){

string $ ParCons[]= ` parent constraint `;

Delete $ ParCons

}

The program name here can be defined at will and will be used when calling the button in the future. Note that although we don't have line parameters, we still need to write parentheses, just leave them blank. Now let's add the program name ALT to the -c of the button, and pay attention to the need to prompt it as a string with quotation marks.

The final melting process is as follows

if(`window -ex "myWin 1"`){

delete ui-window mywin 1;

}

window-title " myFirstWin "-wh 300 500 mywin 1;

column layout-adj 1;

button-l " my button "-h 20-c " ALT ";

show window mywin 1;

Procedure ALT(){

string $ ParCons[]= ` parent constraint `;

Delete $ ParCons

}

The green part is our window part, and the red part enables us to define the program. This proc actually defines a background instruction, which we call through -c in the button. He automatically executes the command in this instruction, that is, mel with alignment function, and completes the link between the button and the program instruction, which makes this button have its own use.

How to add a button to a window and execute a command through the button

Finally, we can drag this mel to the shelf for future use. Later, we will introduce how to use mel to add the menu bar we want to maya's menu and add sub-elements to it.