Go to: Synopsis. Return value. MEL examples.

Synopsis

source string

source is NOT undoable, NOT queryable, and NOT editable.

The source directive causes MEL to compile and execute the contents of a script that is stored in a file. The argument passed to the source directive can be a full path or just the name of a script. If only the script name is provided, then MEL will attempt to locate it in the MEL search path. If the name or path has no suffix, then MEL will add a ".mel" suffix before the search. If a full path is provided, then it must be surrounded in quotes. It is important for advanced MEL users to understand that "source" is not a MEL command. It is a directive that tells the interpreter what to compile and execute. If a script has "source" directives in it, then all sourced files will be compiled at the same time as the script. This will happen regardless of where the source directive appears in the file. And, if the script with the "source" directives is re-run, the sourced files will not be recompiled. It is possible to override this behavior by enclosing the source directive in an "eval" statement. Note that the MEL scanner is not reentrant. This means that a sourced script should not "source" any other scripts, otherwise errors may occur. Python scripts have no such limitation. The above only applies to the compilation of the scripts. The actual execution of sourced scripts will occur at the point where the source directive is in the code. Please see the examples below to see how this works.

Return value

None

MEL examples

// There is a script that is shipped with Maya called "dirname.mel".  You
// do not have to source it to use, as that will be done automatically the
// first time you call it.  But, we will source it as an example of how to
// use the source directive on a script that is in the search path.
//

// You can provide the ".mel" extension if you like
//
source "dirname.mel";

// Or, you can leave it out
//
source "dirname";

dirname( "D:/Work/file.txt" );
// Result: D:/Work //

// Here's a utility proc that we'll use for the next examples
//
global proc writeFile( string $filename, string $contents ) {
    $fileId=`fopen $filename "w"`;
    fprint $fileId ( $contents );
    fclose $fileId;
}

// The following examples require you to have write access to the current
// directory.

// Create a file script file
//
writeFile( "./sourceTest.mel", "print \"sourceTest.mel was executed\\n\";");


// Now we can source the local script by giving a relative or full path.
// This MUST be executed separately from the statement above (we will see
// why a bit later).
//
source "./sourceTest.mel";
// sourceTest.mel was executed

// Or, we can leave off the .mel
//
source "./sourceTest";
// sourceTest.mel was executed


// As was explained above, the source statement is an interpreter directive
// and not a command.  This example should illustrate that.
//
// Execute each block below separately in the script editor!
//

// Block 1.  Create a script to source.
//
writeFile( "./sourceTest.mel", "print \"sourceTest.mel was executed\\n\";");

// Block 2. Demonstrate that the script is only compiled once, but is executed
// every time that the source "directive" is reached.  The script will be
// changed between the source calls, but the changes will not be recognised.
//
print( "Before first source\n" );
source "./sourceTest";
// sourceTest.mel was executed
print( "Between sources\n" );
writeFile( "./sourceTest.mel", "print \"sourceTest.mel has been changed\\n\";");
source "./sourceTest.mel";
// sourceTest.mel was executed
print( "After second source\n" );

// Block 3.  Put the first script back
//
writeFile( "./sourceTest.mel", "print \"sourceTest.mel was executed\\n\";");

// Block 4. Demonstrate how make source recompile each time using an "eval"
// statement
//
print( "Before first source\n" );
eval( "source \"./sourceTest\"" );
// sourceTest.mel was executed
print( "Between sources\n" );
writeFile( "./sourceTest.mel", "print \"sourceTest.mel has been changed\\n\";");
eval( "source \"./sourceTest\"");
// sourceTest.mel has been changed
print( "After second source\n" );