View Full Version : Help me pls..i want to write MEL scripts to learning it

07 July 2007, 07:28 PM
well to all my friends here...i wanna know how to write MEL scripts
who know ..tell me pls ..or somthing of website can you tell me there

Thanx ^ ^'

07 July 2007, 09:59 AM
I don't know if it's still out for maya 8.5, but the Mel Fundamentals book was how I started out with mel. If you can you should get this book, it teaches the basics very well.

Other resources are right inside maya: the MEL Command Reference found in the Help menu of the maya interface lists all MEL commands and their respective respective information like flags and expected arguements. Also check out the Node and Attribute reference as well, as you will be manipulating and creating nodes using mel sometimes.

Also, once you've grasped the basics I suggest you look into other people's mel scripts from time to time to learn how they function. It'a a great way to learn coding. A lot of great mel scripts reside on and Of course writing and viewing code is easier done on a coding program such as jEdit or nEdit.

07 July 2007, 03:15 PM
hi there,

learning mel can be easy by first watching the script editor whenever u do something - even if you do not understand something, you observation skills might be bring down the time to learn mel.

According to me - Learning something the wrong way by making you own "assumptions" then reasoning them one by one will lead you to the right path quickly. In the end you would be knowing what is the right way and why ? As far as books are concerned - complete maya programming by David Gould is cool ( & maya help will get you started

Think of the below stuff like a crash course which might give you an idea "how to learn mel the wrong way ! ". As you already know - Anything in Maya(in terms of data) is a Node and each node has a "object type". Each node might have many no. of attributes and/or plugs and each attribute has its own type. Example: when u create a poly sphere through maya: u create 3 nodes - 1 is the pSphere1(transform), pSphereShape1(shape) and one more node where u change the radius/divisions etc.

Mel has a bunch of commands, functions, ways to store stuff, query stuff, loop stuff, reason stuff ( i mean if condition) etc. your script is done based on your requirement.

There are around 10 commands in maya which are frequently used to list stuff:
etc ...

There are around 20 commands for UI like:
window and showWindow
all the layout commands (columnLayout, scrollLayout....)
all the input fields (intField, floadField)
checkBox and radioButton
button, HUDbutton, HUDButtonSlider
...etc... its simple to make UI's in mel the minute you understand "parenting layout" and stuff

Generally in a UI u will read a value or edit a value or delete that UI element itself >>
for reading values from UI -- command -q -whateverTypeOFDataYouRead
for writing values to UI -- command -e -whateverTypeOFDataYouRead [value/info]
etc. this applies to most of the UI elements

For storing values use the following: (all variables are global variables unless we specifically put the variables within a function)
string $myString = "myName";
int $myAge = 25;
float $myHeight = 179.22; // etc.
you can also use vector, matrix etc to suit your requirment !

the minute you declare a variable as global - you can use it within a function and modify its values and also use it outside the function(if used within a function - just declare the global variable within the function again and use it - but whatever modification done to it will affect the original global variable) - but in most cases one can avoid global varialbles due to its issues...

a collection of strings can be stored in a sting array, int in intArray and so on...
example : string $hairNodes[] = `ls -type "hairShape"`;
int $ages[] = {21, 12, 14};

Normally you would write a script to speed up manually work - so if you know the algo or the step by step thing of the stuff which you need to do, then 90% of the work is done already... now, we just need to figure out the relavent commands to do it...(at the beginning level try using the script editor echo'd scripts to the max - although they might not be the right way or the fast way)

example 1:
I want to move locator1 to (1, 10, 1), scale it by 10% and rename its name to "myLocator"

// script for ex1 "//" - this is a comment header
/* no need to select the locator as we know its name, if you dont know its name select the locator and we can get its name by typing - ls -sl (or) store it to a string variable like this .... string $mySelection[] = `ls -sl`; // the ` symbol has to be used to equate a variable to a command ---now mySelection[0] will contain the first selected node name and mySelection[1] will contain the 2nd selected node name(if any) and so on */

setAttr "locator1.tx" 1; // set Attribute nodename.attributname value
setAttr "locator1.ty" 10;
setAttr "" 1;

// or you can use //

xform -ws -t 1 10 1 locator1;

// now to scale it by 10% you need to know the original scale //

float $originalScale[] = `getAttr "locator1.scale"`; // getAttribute nodename.attributename

// now $originalScale[0] will have scaleX value of locator1
// now $originalScale[1] will have scaleY value of locator1
// now $originalScale[2] will have scaleZ value of locator1

// To scale it by 10 % use setAttr or xform command to do it

setAttr "locator1.scaleX" ($originalScale[0]*0.1);
setAttr "locator1.scaleY" ($originalScale[1]*0.1);
setAttr "locator1.scaleZ" ($originalScale[2]*0.1);

// now to rename the locator1 to myLocator

rename "locator1" "myLocator";

// All done

Well say if you want to do this for 100 nodes and you might do it frequently - then you can use a for loop ....

example 2: suppose you want to print all the transform (TX) values of selection

for($each in `ls -sl`)
int $value = `getAttr ($each."translateX")`;
print ($each + " - TX - " + $value );

example 3: if you need to create a text file and write somthing into it - ck out the example in maya help of "fprint" command

Other than the the regular stuff you can write commands to add shelf, add custom Marking Menus, add a new menu in the set of maya file menus - etc.

most of the mel cmds is like this : commandName [flags] [correspodingValues]

use functions for reduce code length and other advantages:

global proc myReturnValueType myFunctionName(myVariableType myVariableNameUsedInFunction)
// Your code goes here //

return myReturnVariableName;


global proc int isThisFileNamed()
string $filename = `file -q -sceneName`;
if($filename == "")
return 0;

return 1; // if named the function will return 1 else 0

//you can use this function like this:
print isThisFileNamed();

There are math commands, string functions etc which can be used to do lot of stuff in maya ...
The crash course ends here || HEEHE :) ... i might have made some mistakes above - i dont have maya open here to ck what i m writing but please ask you doubts in mel and we can address them in specific, I would like to share my ideas so that it helps me in learning too ...


07 July 2007, 03:29 PM
boy, I guess everyone uses xform.

You can also use move, rotate, scale as well.
but you need to select your object first -

select pSphere1;

//Below adds pSphere2 to the current selection with the flag -tgl
select -tgl pSphere2;

//-r is Relative, below will move x +4 y +6 z +2
move -r 4 6 2;

//-a is Absolute, move something to point 3 2 7
move -a 3 2 7;

This stuff is the same for rotate and scale, select the objects, use -r for relative and -a for absolute.

This is another solution to using setAttr, but only if the situation is right, move rotate scale is usually good for script editor mel and setAttr is good for script editor and expression mel. You can still use move rotate scale in the expression editor, but well I don't like to, doesn't work great selecting things in expressions and unselecting things in the same expression.

07 July 2007, 02:38 PM
I wrote a tutorial that deals with the basics of mel earlier. You can read it here:

07 July 2007, 05:11 PM
I recomend "mel scripting for maya animator" can be found on amazon I'm sure.

07 July 2007, 09:40 PM
I also recommend that book:
Here it is (

CGTalk Moderation
07 July 2007, 09:40 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.