Friday, December 2, 2011

Podcast: Creating a Custom MEL Script for the Shelf

This podcast will synthesize most of the information covered throughout the blog and provide an example of how to create a MEL script and add it to the shelf, from scratch.



Thursday, December 1, 2011

Guest Blog: OpenArtBox's Tips for Maya Users

Here's a couple of tips for Maya users from OpenArtBox.blogspot.com, a blog dedicated to open source art and design software.

An Open Source Solution: Blender 
If you need a 3D modeling program but can't afford Maya, you might want to try Blender, which is a FOSS (free and open source software) alternative. A quick scan of a few forum threads indicates that there are many people who actually prefer Blender to Maya. For example, in one thread entitled “Blender vs. Maya” on GameArtisans.org, there has been a back and forth between Blender proponents and Maya proponents:

Maya Proponent: “...kinda like saying Bicycle vs Porsche. Sure they both get you there but one does it in style.”
 

Blender Proponent:  “...actually Blender's interface is way sexier than Maya's.” 

Maya Proponent: “I don't like Blender...(it's) weird...” 

Some of those who entered into the discussion took more neutral positions. 
“Ultimately, it doesn't matter what you use on a personal basis,” one comment read. “What matters is your understanding of how to model and the core concepts of UV layout, mesh creation and management, etc...A tool is a tool. What matters is how you make use of those tools. Being biased towards Blender because it is free or open source is kind of silly. Thinking Maya is an amazing program right out of the box is silly as well, and also naive.” 

Python Scripting in Maya 
Both Blender and Maya 8.5 and later include built in Python interpreters. Python is a cross-platform programming language that can be used by even commercial products such as Maya because of its open source license.

This is what a simple “Hello World” program looks like in Python:

print “Hello World!”

To call this program in Maya, we name the program helloworld.py and type the following in the MEL Script Editor:

pySource helloworld; 

Calling MEL from a Python Script 
When you run Python scripts in Maya, you can actually call any MEL function from Python. To do this, you need to import MEL into your Python script and place wrappers around the MEL commands. Here is an example:

from maya import mel

mel.confirmDialog(message="Hello World!", button=["OK"]);

This Python program uses a MEL command to place the “Hello World!” method in a dialog box.

More Information on Python Scripting
A full tutorial about Python scripting in Maya, written by Matthias Baas, can be found here: http://cgkit.sourceforge.net/maya_tutorials/intro/    

Carrie Sullivan is a student at UT-Dallas.  
She writes a blog about Linux, a free and open source operating system that thrives from a community who help modify and distribute Linux’s source code to build functionality and customization.

You can read her blog at OpenArtBox.blogspot.com

Resources
Blender vs. Maya. (2010, July 1). Retrieved December 1, 2011 from  GameArtisans: http://www.gameartisans.org/forums/showthread.php?t=15099

Python/Maya Introductory Tutorial. (2006, May 8). Retrieved December 1, 2011 from SourceForge:            http://cgkit.sourceforge.net/maya_tutorials/intro/

Python Programming Language, Official Website. Retrieved December 1, 2011 from Python: http://www.python.org 




Friday, November 11, 2011

The Shelf - Adding MEL Commands

Creating a shelf button for tasks and commands that you do regularly throughout your
work flow can be a huge time saver and incredibly easy to do with a few steps.

Adding a MEL Command to the Shelf

1. Decide which shelf you want to add your custom MEL command to.

    For our purposes, we'll use the Custom shelf we created in an earlier post about the Shelf.

    Click on the tab labeled "Custom" on the Shelf.
 

2. The MEL Command Statement(s)

    If you recently accessed the command, you can find it in the History Panel. 
    Otherwise, you can retype it. In our example, we'll use the following command statement:

    textCurves -t "Hello World!" ;

3. Highlight the Satement (see Figure 1) with your mouse.

Figure 1 -Highlight the Statement

4. Save the Script to the Shelf

    There are 2 options you can choose from here:
   
    Option 1: Click and Drag 
  • With the left mouse button, simply click and drag the statement directly to the Shelf.
  • A dialog box will appear, giving you the option to save the command to the Shelf either in MEL or in Python.
  • Select button that says MEL. 

        Option 2: Saving via the Script Editor Menu
    • In the Script Editor menu (NOT the main Maya menu. See Figure 2), go to:                                           File > Save Script to Shelf...
    Figure 2 - Save Script to Shelf
    • A dialog box will appear, giving you a chance to save the command with any name we'd like to give it. 
    • I decided to give it the name, "hello"
    • A dialog box will appear, giving you the option to save our command to the Shelf as either in MEL or  in Python.
    • Select button that says MEL.
    5. Pat yourself on the back

        You've just turned your script into a custom shelf button for future use! (see Figure 3)

    Figure 3 - Results of Options 1 and 2

         Now you'll never have to write the complete statement, textCurves -t "Hello World!"; again!

         A simple press of the button in the Shelf will execute the command!

    Deleting Items from the Shelf: 
    You may want to delete shelf buttons for a variety reasons. Here's how:

    • With your middle mouse button, click & drag the shelf button to the Trashcan icon, which is located at the far right of the Shelf (see Figure 4).

    Figure 4 - Trashcan Icon for the Shelf
     






    Conclusion:
    Remember, this is just a proof of concept; you probably won't ever be in desperate need
    to automate a "Hello World" text curve into your scene.

    However, this example opens up so many possiblities, especially when considering an extremely complex set of instructions (MEL commands) can be encapsulated into a simple button!

    For instance, what if you had a task that required you to the following steps:
    • center pivot of all objects
    • freeze transformations of all objects
    • delete history of all objects
    • save the file
    Going through all those steps to perform a task would be a real pain, especially if you do it often.

    Wouldn't it be nice if there was just one button to do all of these steps? Using what we just learned as a foundation, it won't be difficult to do.

    Of course, before we can create a complex MEL script, we have to know how to make a simple one! Like so many teachers have told me in the past:
    • Learn to stand before you walk.
    • Learn the ABCs before you write your novel.
    Next Blog Post:
    Creating a MEL Script!
       

    The Shelf and Creating a New Shelf

    Figure 1 - the Shelf
    When you probably first started Maya, you may have noticed that even though you can create a polygonal sphere from Maya's main menu, you could also just click on a button on the Shelf (see Figure 1) to create one, as well.

    But did you know that you can actually add custom buttons to the Shelf?

    Creating a shelf button for tasks and commands that you do regularly throughout your workflow can be a huge timesaver and incredibly easy to do.

    There are 2 parts to this process:
    1. Decide which shelf you want to add your MEL command - or create a new Shelf, altogether.
    2. Adding the MEL command to the Shelf

    The Shelf
    If you don't see the Shelf in Maya's interface, you can activate it in Maya's main menu (see Figure 2 ):

        Display > UI Elements > Shelf
    Figure 2 - Displaying the Shelf

    Creating a new Shelf
    1.  Along the Shelf, at the far left, there's an icon with a downward-pointing arrow (see Figure 3). 

    Figure 3 - Shelf Icon
     
    2.  With your left mouse button, click & hold over the downward-pointing arrow icon.

    3.  A dialog box will pop up, but will disappear if you release your mouse button's hold.

    4.  Click on the option, "New Shelf"

    5.  A dialog box will appear, prompting you to enter the name of the new shelf we are creating (see Figure 4).

    Figure 4 - Dialog box to name the new Shelf
     
    6.  For now, let's give it the name, "Custom", but you can give it any name you wish.

    7.  click "Ok".

    Creating a new shelf means we don't have to clutter the other default Maya shelves. Not to mention, its a great way to keep our shelf buttons organized once you become a MEL guru and start writing all kinds of different custom MEL commands and scripts.

    Next Blog Post:

    Friday, November 4, 2011

    MEL Commands and Arguments

    • A command:
      • is like a magic word for Maya to do your bidding. 
      • says to Maya, "textCurves!!!" Then Maya makes one for you.
    • A command argument 
      • says to Maya, "Hey, I'm letting you know that I want to add a detail to this textCurves command called -t"
    • A parameter 
      • is the specific detail of a command argument. 
      • In our case, "Hello World" was the detail we used but it could be anything, depending on what you want.
    In the last post, we created wrote our first MEL command, 
        textCurves -t "Hello World!" ;

    To be thorough, let me briefly address a big question: What exactly does the cryptic text in our command statement mean?

    textCurves  

    This is a command.  

    Specifically, textCurves performs the particular task of creating "NURBS curves from a text string" (Cite source: maya help) into the scene .

    There are thousands (millions?) of specific commands that are already built into Maya
    that are ready to use.  You can also create commands from scratch, in case there's something Maya doesn't already do, but should!
       
    In an earlier post, Command Errors, I introduced what commands and command errors are and defined them in greater detail. I used the command, sphere, as the example.

    -t 

    In general terms, this is a command argument

    Specifically, -t is a command argument for the textCurves command.

    More often than not, its not enough to simply type in a command without telling Maya
    the details. Using command argument fleshes out such details.

    The -t argument for the textCurves command allows you to enter the specific phrase you want displayed.  the -t argument isn't universal, though - you can't use it for just any command (you can't use it for the sphere command)


    In our case, we wanted to display the text, Hello World!

    There are many other command arguments associated with the textCurves command, such as:

    • -f : allows you to determine the font
    • -n : allows you to give a specific name to the object

    For instance, execute the following code, to see what adding extra arguments does:
        textCurves -f "Arial" -n "helloWorld" -t "Hello World!"

    There will be another post about how you can find what other command arguments are associated with a specific command, to give 

    "Hello World!"  

    This is a parameter.

    Specifically, the phrase, "Hello World!", really means nothing to Maya and isn't important to the command, itself. 

    What's important is "Hello World!" represents a specific detailed parameter you want Maya to act on.


    For instance, Instead of "Hello World!", test the following:
    • your name
    • the name of a friend
    • a city
    • a state
    • your favorite restaurant
    • Anything! 
    Not all parameters need to be enclosed in quotes.  In the case of the -t argument it's data type is a String, so we need to enclose whatever we want for this argument within quotes.

    You're probably thinking, "Data types? A 'String'?"  We'll get to that easy stuff in another post.  For now, just know that:
    • the -t argument's parameters need to be enclosed in quotes
    • not all argument parameters need to be enclosed in quotes. It just depends the type of argument.
    Next Post: 
    In the next post, we'll introduce the Shelf and how to add shelves to your the Shelf!

    MEL Command: Hello World!

    The phrase "Hello World!" is a common exercise to acquaint people with a computer programming language, so its only appropriate that we start our first MEL endeavor with this exercise.

    MEL Commands and MEL Scripts

    Let's be clear; there is a difference between MEL commands and MEL scripts.
    The biggest differences between them are:
    • MEL commands are the parts that make up a MEL script
    • MEL commands constitute a MEL script.

    MEL Commands: Hello World!

    1. Open the Script Editor


    2. In the Scripting Panel:
    Write (or copy & paste) the following command statement:
        textCurves -t "Hello World!" ;
         
        NOTE: Please be aware of the semicolon at the end of this command statement. 
      • It's a good - and often necessary - convention to follow.
      • This one particular command will work fine without it, but once we start writing scripts with multiple statements, you could end up with a syntax error or bugs in your scripts!

    3. Execute:
    You actually have a few options to execute this command, choose any one:
    1. Ctrl + Enter 
    2. Enter (on the numeric keypad)
    3. Highlight the command statement, then click on the Execute icon (Figure 1)
    4. Highlight the line, then hold the right mouse button. A dialog box will open up. Select "Execute".
    Figure 1: The Execute Icon in the Script Editor
      
    4. Bask in the splendor of your creation
    Once you've executed the code, you'll notice a nurbs curve that says "Hello World!" is generated in your scene! (see Figure 2)

    Figure 2 : Hello World!
    I know... it's totally awesome! 
    Time to pat yourself on the back.


    Conclusion:

    Let's assume that, for some reason, you have to call on your "Hello World!" command a lot of times throughout your workflow (I know its preposterous, but bear with me!)

    Having to constantly find the command statement in the History Panel and then execute the command every time we want to execute the command just isn't good enough! 

    We're too laz -*ahem*- our time is too important to us!

    Next Post:

    textCurves -t "Hello World!" ;

    Huh? What did we just do? What does that cryptic text mean, anyways?
    In the next post, MEL Commands and Arguments, I'll briefly break down what we wrote to give a little insight about what this statement technically means to Maya. 

    Friday, October 28, 2011

    The Script Editor

    In an earlier post, we became acquainted with 2 Maya tools/features regarding MEL:

    In this post, we’ll discover the tool that is most responsible for the actual creation of MEL scripts:
    • The Script Editor.

    We’ll take things easy, for now, starting with
    • explaining the function of the Script Editor
    • how to access the Script Editor
    • identifying the panes within the Script Editor

    The Script Editor: What does it do? 
    The Command Line is a fast and easy way to execute commands, but where do we find the guts of a command ?  How do we even make a command out of thin air?

    The answers lie in the Script Editor.

    With the Script Editor, you can:
    • actually see the code and scripts that constitute a command
    • execute commands as an alternative to using the Command Line
    • reference previous actions and tasks as a foundation to create other commands
    • make custom commands out of thin air (with a little bit of brainpower, of course!)

    Before we go on and write scripts, let’s find the Script Editor first!

    Accessing the Script Editor 
    At the far right of the Command Line, you'll notice a little icon (see Figure 1).

    Clicking on this icon opens up the Script Editor (see Figure 3)

    Figure 1: Script Editor Icon
     
    If for some reason you don’t see the Script Editor on your screen, you can easily open it up in Maya's main menu (see Figure 2): 
    • Window > General Editors > Script Editor 
    Figure 2: Open Script Editor in Maya''s Main Menu

    The Script Editor: Overview
    Now that your Script Editor is opened, let’s take a brief overview of what exactly we’re looking at.

    The Script Editor has three major components (see Figure 3): 
    • Toobar
    • History Pane
    • Scripting Pane
    Figure 3: The Script Editor

    The Script Editor: The Toolbar
    The Toolbar offers a quick and visual way to access common tasks and tools instead of having to access these tasks through the File, Edit, History, Command, or Help tabs. 

    In a later post, we’ll cover at greater depth what these icons represent and do.  For now, it’s good to know that these are very helpful icons exist and are similar to the icons in Maya’s Shelf, which also simply are an alternative to accessing common tasks.

    The Script Editor: The History Panel
    According to the Maya Help site, there isn’t an official name for the top pane, although its function is to “show the history of commands and their results.(Autodesk Maya Online Help).  For the sake of identifying the top pane throughout this blog, and to avoid confusion, let’s simply call it the History Pane.

    The History Panel serves two simple purposes:
    • show the history of commands
    • show the results of commands
    You may notice some cryptic text written within the History Pane (see Figure 4).  This text consist is actually MEL commands that represent your actions – command history and command results - in your Maya scene. Practically every action you do to an object within your current Maya scene is recorded and documented within the History Pane. 

    Figure 4: History Pane

    The Script Editor: The Scripting Pane
    According to the Maya Help site, there isn’t an official name for the bottom pane, although its function is to “type MEL or Python commands and scripts in either the MEL or Python tab.(Autodesk Maya Online Help).  For the sake of identifying the top pane throughout this blog, and to avoid confusion, let’s simply call it the Scripting Pane.

    The Scripting Pane is blank, by default. We'll be writing text in this blank space to create MEL scripts in the future.

    Next Post:
    In a later post, we'll delve deeper into the functions and applications of the ScriptEditor.

    Before we do any scripting - and to get rid of any bad habits, beforehand - I'll cover two common errors when scripting:
    •     Command Errors
    •     Syntax Errors 
     Bibliography:
     "Autodesk Maya Online Help." 2008. http://usa.autodesk.com/. 2010 24 October <http://download.autodesk.com/us/maya/2009help/index.html>.