+ Chapter 08 - MEL - The Style +Your scripts should be neat, elegant, boolet-proof and properly included comments. Many programmers are happy, because they think their script runs. You should always follow some guidelines to make your script more readable. Many times you work in a team, it will cause trouble if your script is not properly aligned and commented; your team members will have problems understanding the script.
+ The Process +
Before writing any script, you have to sit down and do some pre-production. Visualize your target audience. Define your script's definition and vision. Think about the main function of the script. Which category it falls under. Do some research on the topic. Analyze other people scripts, study them and think, what extra functionality can I provide? How my script will execute faster? Once the vision is clear, then focus on who needs this tool? Will it be useful to the end user?
Draw a flowchart. Break your codes in multiple procedures, instead of one long procedure.
+ Comments & Whitespace+
Improve the format of your script by using whitespace. It will make your script more clean and readable. Properly align code inside blocks in if and loop structures. Comments are also very vital, you can use single line or multi-line comments in your script.
// This is a comment, single line.
This is multi line
Commenting is a good habit. Always include comments at the top of the script describing what script does? What are the dependencies? Is the script copyrighted? Which argument it needs?
SAMPLE HEADER FROM Famous GIJoe LIGHT SCRIPT
GI_Joe mel script - version 2.1 - (c) Emmanuel Campin - 2001 / 2002
version 2.1 / date: 12 26 2002
Bugs fixed / improvements: see www.pixho.com for this bugs fixed / improvements list
- clean scene shadows
- clean all shadows (please refer to the doc for more details)
- HDRI fake feature (ray-trace only)
This script creates a spherical based lighting (GI fake) plus a shader which allow
the user to bake the generated shadows.
This spherical lighting is separated in 2: skyLight_group and groundLight_group.
Each group can be controlled in color and intensity independantly to the other.
You can connect/disconnect any texture (environment textures excluded) you want.
Place the GIJoe.lights scene and the GI_Joe.mel into your script directory
IMPORTANT: You'll have to change the directory location in line 57
(or search the string "your_script_directory_path/scripts/GIJoe.lights")
Source the script and execute it.
Go to www.pixho.com for more infos and limitations about the GI_Joe.mel script.
Send your comments to email@example.com
If you find a bug, send a mail at firstname.lastname@example.org with a precise description of the bug
string $lstA, $lstAttributes;
for ($lstA in $lstAttributes)
print ($lstA + "\n");
string $lstA, $lstAttributes; // variable declaration
$lstAttributes =`listAttr nurbsSphere1`; // Fills place holder with attribute names
// Iterate through Array
for ($lstA in $lstAttributes)
print ($lstA + "\n"); //Prints the attribute's name.
+ Avoid Global Variables/Procedures +
Avoid global procedures and variables. Declare global, when it is necessary. Global variables and procedures are visible outside the scope of the script and can be modified by other script. Also, use sensible variable name. For example, if you want to assign listAttr command's output to a variable; $lstAttribute is much better then $la. Also make your code compact. According to maya documentation:
Limit procedures and command scripts to 50 lines.
Limit files to 500 lines.
+ Adding Script Path +
If your script is outside the default script path [ C:\Documents and Settings\user\My Documents\maya\scripts ] and you want maya to automatically search for it, you need to modify the Maya.env file, which you can found in following folder:
[C:\Documents and Settings\User\My Documents\maya\Version]
Open the Maya.ENV file with notepad and add path to this file. For example, if your scripts are in folder c:\mayaScripts add following line in the Maya.env file.
MAYA_SCRIPT_PATH = c:/mayaScripts;
These settings will take effect only when you start a new Maya session.
+ What Next ? +
Now we understand the style of scripting. In next chapter we will learn about a cool feature of MEL, Script Nodes. Script nodes are used to store MEL scripts in a Maya Scene file.
+ Download Project +
Download Project - Downloaded times.