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>.

Errors: Syntax

There are very basic yet incredibly important considerations to regard when writing MEL scripts and commands; it is very important to notice details. Here are some common syntax errors that a non-programmer might be familiar with:
  • punctuation
  • capitalization
  • misspellings
  • mistypings 

Syntax Errors: An Example
Syntax errors, even a single minor one, will make Maya think you are talking gibberish to it. 
For instance:
  • if we type sphere into the Command Line, 
    • Maya will say,
      • “Aha! That’s the magic word – let me make that sphere you’re asking for!"
  • if we type sphere. (note the period) into the Command Line, 
    • Maya will say,
      • “I have no idea what you’re trying to tell me.”
Even something as benign as a period changes the command entirely.  Although we entered the right command, sphere, the period at the end ruined the syntax of the command. 


Some questions to consider:
  • What is syntax?
  • How come nothing happened when a simple period was added?
  • What do the error messages mean?

What exactly is Syntax? 
Syntax "refers to the ways symbols may be combined to create well-formed
sentences (or programs) in the language."
(Slonneger)
To put simply, syntax refers to rules in a language.  Syntax is intrinsic in all languages...
  • computer languages:
    • MEL
    • C++
    • Python
    • and many more...
  •   spoken languages:
    • English 
    • Russian
    • French
    • ...you get the picture...
All languages follow a syntax that must be followed in order to understand it. If someone speaks or writes without following proper syntax, it may sound odd or, worse, completely incomprehensible. 

Imagine what this entire blog would look like if I failed to capitalize any letters and neglected all punctuation rules. That is an example of improper syntax.

Syntax affects MEL
Syntax affects languages, so it follows that syntax affects MEL - MEL stands for Maya Embedded Language, after all.

When people communicate to each other, we can make at least TRY to make sense of syntax errors because of contextual information.  That’s why we’re able to understand Yoda when he speaks, despite his flagrant disregard for syntax!

However, making syntax errors in any computer language can be the most frustrating thing in the world simply because
  • computers and compilers are so sensitive to syntax errors
  • its so easy to make syntax errors
  • syntax errors are generally minor grammar mistakes 
Computers can't read our minds and don't take what you say into context.  When you enter a  command with even one syntax error, computers don't say, “Oh, you wrote this for me to do, but I'm guessing that this is what you really mean.”  


There's either a right way or a wrong way to communicate with computers; there is very little room for a gray area.  That's why placing a period at the end of the command, sphere, confuses Maya, leading to a syntax error. To Maya, sphere is totally different from sphere. (again, note the period)

There are too many ways to list what not to do; what’s important is:
  • there are correct ways to communicate with Maya
  • there are countless ways to miscommunicate with Maya, with regards to syntax

Next Post:

The next blog post will introduce the Script Editor and write our first MEL command

Bibliography:


Slonneger, K. (n.d.). Syntax and Semantics of Programming Languages. Retrieved October 14, 2011, from The University of Iowa: Department of Computer Science: http://www.divms.uiowa.edu/~slonnegr/plf/Book/Chapter1.pdf

Friday, October 21, 2011

Errors : Commands

Maya doesn’t arbitrarily execute a random “command” that you enter into the Command Line. To illustrate, let’s try to create a cube, instead of a sphere, using the same method we used in the previous post. 

Let’s address the concerns we should be thinking about:
  • Why a cube won't appear when we type cube?
  • Why a cube will appear when we type nurbsCube?
  • What are error messages, and what  do they mean? 
Command Error: An Example
In the last blog post, we used created a sphere in Maya by simply typing sphere into the Command Line. Naturally, you may think that simply typing cube into the Command Line will create a cube into the scene.
 
Let's find out:
    Click inside the Command Line.
    Type cube
    Press Enter

    Nothing happened!

    In fact, the Command Feedback Line turned red and a line of text appeared (see Figure 1 ):
    // Error: Cannot find procedure "cube".

    Figure 1: Error Message





    Valid Commands: An Example

    Let’s make the cube using a valid command: nurbsCube
    Click inside the Command Line.
    Type nurbsCube
    Press Enter



    Figure 2: Cube
    Awesome! 

    A cube appeared! (see Figure 2)

    But what gives? 

    How come it didn’t work when we typed cube?



    "Oohhh… that’s why nothing happened!” 
    In the case of cube, Maya doesn’t recognize it as a command – it simply isn’t in Maya’s library of procedures to execute the command, despite your intentions.  

    On the other hand, sphere is an actual command – it just coincidentally happens to make sense to us in the real world and in this particular example.

    Not all commands are as obvious as sphere. The command, nurbsCube, certainly wouldn't have been my first guess.  After all, if sphere works, why not cube? 

    Frankly, I don't know why the powers that be decided sphere would be a valid command, while cube wouldn't be; and that's not the point.  

    The point is:
    • we can’t simply type in something and expect Maya to do as we wish. 
    • Maya won’t make a cylinder, plane, or torus simply because we type cylinder, plane, or torus into the Command Line.
    The Error Message  
    The error message may seem cryptic, but completely illustrates this point:
    // Error: Cannot find procedure "cube".

    Maya has never heard of cube

    Sure, it may make sense to you – you want Maya to make a cube.  To Maya, though, you may as well be saying wqoe#irnddf?pw.  Arbitrary commands that we write into the Command Line or Script Editor won't always make sense to Maya, despite our intentions.

    We have to give Maya the “magic word(s)”, so to speak.  We have to give Maya proper and valid commands that it understands.

    Next Post:
    While we're on the topic of common problems, we'll address one of the notorious in the entire galaxy: Syntax Errors!

    The Command Line

    If you use Maya for a particular job - such as modeling, rigging, or texturing - there may be tools that are essential in your workflow.  

    In the same way, there are 3 Maya tools/features that we should and will become acquainted with most commonly used for MEL script creation
    • the Command Line 
    • the Command Feedback Line
    • the Script Editor 
    Today, we'll simply introduce the Command Line and the Command Feedback Line, to start. 

    The Command Line
    The Command Line is a simple place to insert your MEL commands.  Although we don't know how to write commands yet, we'll soon fix that.

    The Command Feedback Line 
    The Command Feedback Line is a complimentary component to the Command Line and Its job is to -*drum-roll, please* - give feedback about results from a command.  

    Even if you've never used the Command Line before, you may have noticed that the Command Feedback Line will show some text from time to time, especially when you do major tasks like creating an object or deleting one. 

    Before we go on and write scripts, though, lets find the Command Line and The Command Feedback Line.

    Accessing the Command Line
    If the layout of your Maya settings is in the default mode, you’ll notice at the bottom a piece of text that actually says, “MEL”; immediately to the right is a white text box.  This white text box is the Command Line. The Command Feedback Line is the dark gray box immediately to the right of the Command Line (see Figure 1)

    Figure 1: Command Line
    If for some reason you don’t see the Command Line on your screen, simply go to (see Figure 2):
    • Display > UI Elements > Command Line 
    Figure 2 : Accessing Command Line



    Using the Command Line
    We don’t know any MEL commands yet...let’s change that.


    Click inside the Command Line.
    Type sphere
    Press Enter

    Figure 3: Nurb Sphere
    Awesome!

    A nurbs sphere (should have) just popped up out of nowhere. (see Figure 3)

    By typing sphere in the Command Line, Maya recognized the command and executed it by creating an actual nurbs sphere into the scene.


    The actual sphere is not what's important, though; what's important is you just entered a MEL command and Maya obeyed.  Not to mention, this shows powerful nature of MEL.

    Not convinced? Imagine, for some reason, you wanted 50,000 copies of the same sphere.

    The only way to do that would be to select the sphere and duplicate 50,000 times by hand, right? Wrong. MEL could automate a duplication 50 million times with ease and practically in an instant.

    That's powerful stuff!

    Next Post:
    In the next few blog posts, we’ll touch on what keeps Maya from doing a command due to the two most common types of errors.