Mel Script - Sourcing external mel scripts




mel scripts can easily grow to fairly large sizes. As a general rule, i would advise keeping all of your scripts to less than 1000 lines. Above that size and you will soon get lost trying to perform the simplest task.

Ideally then, we need a way to break our scripts into seperate smaller ones. In addition to simplifying our scripts, this process also promotes much better re-use of code since each small script may be used by a number of other more high level scripts.





The non-source method

Assuming that you have a mel procedure to call defined in an external script. For the sake of argument, the procedure will look like :

global proc foo() { print("foo\n"); }

If we save the file as "foo.mel" into our user scripts directory (~/maya/scripts) then all we have to do to use foo() is to call it from any script we want. When called, if Maya does not recognise the function, it will attempt to search the scripts directories for a mel script with the same name as the called function (case sensitive). If found, it will simply execute the script and then call the function. In effect, if we wished, we could write a single function for every script we write (not as bad an idea as it sounds!)




The Source method

source is a mel command that will explicitly go off and execute the specified script. For example, source("foo.mel") would look for the specified script in all of maya's script directories and then execute it.

Generally it is a good idea to NOT use full path names to scripts, i.e. "C:/my_scripts/foo.mel". The reason is that it makes porting the mel scripts to another platform annoying.

ie, only ever put scripts in your ~/maya/scripts/ directory. Don't even bother thinking about doing anything differently! ;)