Adverti horiz upsell
Conversational MEL Part 1
Conversational MEL Part 1
JamesPiechota, updated 2006-11-29 07:04:17 UTC 47,093 views  Rating:
(7 ratings)
Page 4 of 4

Example Walkthrough


To bring it all together (and take it a little further) you�re going to make your very own �My Settings� window.

My Settings Window

Just like a shelf button, each button in the window will execute a MEL command when pushed. The difference, though, is that you�re going to build the window and buttons from scratch using only MEL and without any drag-and-drop magic. The goal of the window is to save you a few mouse clicks by digging some commonly changed Maya settings out from the nested menus in which they hide, and exposing them on each of the buttons.

The meat of the work will be done by commands that you capture from the History Pane - but there are a few UI commands that you�ll need:

window
columnLayout
button
showWindow

These four commands represent each of the main areas of MEL UI building. We�ll be discussing UI in depth in future articles, for now we�ll briefly go over those four commands. A window looks like this:

Blank Window

Almost all MEL UI you create will be contained in a window. You can think of a window as the foundation of your UI - all other components are built on top of it. The next layers on top of windows are layouts - they organize and position any buttons, fields, checkboxes, etc... (Generically called �controls�) contained in the UI. A
 �columnLayout� organizes the controls in a column from top to bottom:

Column Layout

A button is an example of a MEL control - like text fields, checkboxes, option menus, and any number of other controls, a button allows the user to interact with (i.e. to control) the UI. She can click a button, check a checkbox, select an option from an option menu and that change will trigger any MEL commands attached to that control.

English Conversation Window

Finally there�s the �showWindow� command. By default when you create a window it will be invisible - the �showWindow� command makes it visible.

That�s enough talking let�s do some doing. Here�s some code to create the window and buttons from the previous picture:

window -title "English Conversation";
columnLayout -adjustableColumn true;
button
    -label "Hello, how are you?";
   button
    -label "Fine, thank you. And you?";
   button
     -label "Good. Nice to meet you.";
showWindow;


Let�s walk through the flags:

window -title �English Conversation�;


 �-title� is a flag that sets the words that will be displayed at the top of the window. The flag argument in this case is �English Conversation�  - and you�ll see that indeed the title of the window is �English Conversation�.

A bit of a digression:  the �English Conversation� argument is known in computing circles as a string (short for �string of characters� - basically one or more words or letters). It is characterized by the double-quotes at the beginning and end. These quotes are necessary so that Maya doesn�t treat the space between English and Conversation as an indication that two flag arguments were used.

Next we see:

columnLayout -adjustableColumn true;


The �-adjustableColumn� flag determines whether the width of the columnLayout can be changed (i.e. if you make the window wider or narrower will the columnLayout follow suit). The �true� argument says, yes, the width can be changed. This flag isn�t necessary, but it just makes things look prettier.

button
-label "Hello, how are you?";

You�ll notice that the �-label� flag is on a separate line - since Maya treats all whitespace the same this is okay. The �-label� flag is like the window�s �-title� flag, it determines what words will be displayed on the button �Hello, how are you?� in this case.

There you go, you�ve made your very own (useless) user interface!

Let�s put some use into it. As mentioned before this little bit of UI can be used to expose some commonly changed settings. For the sake of example let�s pretend that I�m frequently changing the joint display size between 1 and 0.5 and toggling between hardware and software rendering. Although, to unwind, I love making spheres and moving them around.

First thing I need to do is find out the MEL command used to change the joint display size. I�ll create a couple joints (for reference), open up the script editor, and hunt down the joint size settings.
 
Joints

After several clicks I find it in Windows > Settings/Preferences > Preferences > Kinematics (phew!).

Joint Size Widget

If all goes well I should be able to set the joint size to 1.0, grab the command from the script editor, stuff it onto a button and never again have to hunt through all those menus! Here goes:

No Output

Uh oh - what happened? The joints got bigger but there�s no output in the History Pane! Since everything you do in Maya passes through MEL, Maya ends up executing a lot of commands - so many that it would be distracting to display them all in the Script Editor all the time. By default only a few are displayed. If you really want to see them all you can enable Echo All Commands in the Script Editor�s Script menu - now you�ll get a lot more text in the history pane.

Note: the Attribute Editor generates a lot of output, it�s best to close this before turning on Echo All Commands. Also, depending on what you�re doing, echoing all commands can slow Maya down - so it�s best to only turn it on when you�re looking for a particular command. Okay, let�s try again - this time change the joint size back to 0.5.

Output

There we go, the last command:

jointDisplayScale 1;


will set the joint size to 1. Let�s stick it on our button.

There�s one more flag on the button command that we haven�t discussed: the �-command� flag. It takes a string argument that will be executed as a MEL command whenever the button is pushed.

Here�s the new code for the My Settings UI:

window -title "My Settings";
columnLayout -adjustableColumn true;
   button
  -label "Joint Size 1"
     -command "jointDisplayScale 1;";
showWindow;


Notice that the �jointDisplayScale� command is enclosed in double-quotes since it is being passed to the �-command� flag as a string argument.

Here�s a picture of the UI - when the �Joint Size 1� button is pressed, the joint size will be set to 1:

My Settings Window with 1 Button

See if you can add another button that sets the joint size to 0.5.

Next we�ll try to add some buttons for changing the current renderer to Maya Hardware or Maya Software.

Leaving Echo All Commands on, open up the Render View window (either click the Render View Button or go to Window > Rendering Editors... > Render View). You�ll notice that a lot of output has scrolled by in the Script Editor. Since we don�t care about it, let�s get rid of it - this will make it easier to figure out what commands are used to change the current renderer.

To clear the History Pane go to: Edit > Clear History in the Script Editor. Watch out, though, that you don�t accidentally clear the Input Pane - if so you may wipe out your work on the My Settings window.

Now go to Options > Render Using > Maya Hardware. Your history pane should look something like this:

Renderer Output

You�ll notice that the output isn�t quite as clear as it was for the joint display size. Most operations in Maya trigger secondary, background operations that you normally don�t care about - unfortunately with Echo All Commands on those operations are recorded in the History Pane as well. What this means is that much of time finding MEL commands in the History Pane requires some detective work. Take a scan through the output and see if you can figure out which command was used to change the current renderer to Maya Hardware. If you need to, change it back to Software to see if that helps. You can also try out a command or two, and see if you current renderer was changed in the menu item.  

... Found it? The command to set the current renderer to Maya Hardware is:

setCurrentRenderer mayaHardware;


Once we add it to our UI, along with a button to set the current renderer to Maya Software, we end up with a script that looks like this:

window -title "My Settings";
columnLayout -adjustableColumn true;
  button
     -label "Joint Size 1"
     -command "jointDisplayScale 1;";
  button
     -label "Joint Size 0.5"
     -command "jointDisplayScale 0.5;";
  button
       -label "HW Render"
     -command "setCurrentRenderer mayaHardware;";
  button
   -label "SW Render"
       -command "setCurrentRenderer mayaSoftware;";
showWindow;


And this swanky piece of utility:
 
My Settings Window With 4 Buttons

Just like Windows has Solitaire and cellphones have Tetris - we need to have a little fun too. And there�s nothing I like more than creating spheres and moving them around. Let�s see if we can add the sphere and move commands from before:

polySphere;
move -r 0 0 -2 ;
move -r 0 1 0 ;


I�ve removed some of the flags and changed the arguments so that the commands fit onto the page nicer - feel free to leave them in their long form.

Okay let�s add it to a new button called �Sphere�:

window -title "My Settings";
columnLayout -adjustableColumn true;
button
     -label "Joint Size 1"
     -command "jointDisplayScale 1;";
   button
     -label "Joint Size 0.5"
     -command "jointDisplayScale 0.5;";
   button
     -label "HW Render"
     -command "setCurrentRenderer mayaHardware;";
   button
      -label "SW Render"
      -command "setCurrentRenderer mayaSoftware;";
   button
       -label "Sphere"
    -command "polySphere;
move -r 0 0 -2 ;
move -r 0 1 0 ; ";
showWindow;

And go! ...


Error!
 
Unterminated String Error

Looking at the History Pane we see a bit more detail:

// Error:   -command "polySphere; //
// Error: Line 17.24: Unterminated string. //
// Error: move -r 0 1 0 ; "; //
// Error: Line 18.19: Unterminated string. //


This is caused by one of the exceptions to the �Maya treats all whitespace the same� rule: you have to be careful when putting carriage returns into the middle of a string. Once Maya encounters one double-quote it expects to see a second one on the same line. One way around this would be to put all the commands together on the same line:

"polySphere; move -r 0 0 -2; move -r 0 1 0 ;"


This is fine if you have a relatively short string, but gets awkward as your string gets longer than the width of the Script Editor. Luckily, if Maya sees a \ (backslash) at the end of line it will ignore the following carriage return. It�s important to note that the carriage return has to come immediately after the backslash - no spaces or anything in between.

With this in mind we can rewrite our window in its final form:

window -title "My Settings";
columnLayout -adjustableColumn true;
   button
     -label "Joint Size 1"
     -command "jointDisplayScale 1;";
  button
     -label "Joint Size 0.5"
     -command "jointDisplayScale 0.5;";
  button
     -label "HW Render"
      -command "setCurrentRenderer mayaHardware;";
  button
      -label "SW Render"
   -command "setCurrentRenderer mayaSoftware;";
  button
   -label "Sphere"
      -command "polySphere;move -r 0 0 -2; \
move -r 0 1 0 ; ";
showWindow;

And in all its graphical glory:
 
My Settings Window

As a final step, select your MEL code, and middle mouse button drag it to the shelf - presto you can now launch your settings window whenever you want!

Conclusion

To recap we�ve covered:  

      • What a command is and how to customize one with flags and arguments
      • How to automate a workflow without writing any MEL by grabbing commands from the History Pane
      • The anatomy of a MEL command and some of the rules you have to abide by when writing MEL
      • The basics of creating your own UI to perform custom tasks

          Thanks for reading this far! If you�d never written any MEL before, I hope this was useful. If none of this was new to you: don�t worry! In future articles we�ll be picking up the speed and going much deeper into the magical world of MEL.

          If you have any questions about anything Maya related, please feel free to shoot us an email at: info@NimbleStudiosInc.com