Sample MEL scripts

Here are three simple scripts - the first two are for finding the sum of first N natural numbers (ie. 1,2,3...N) and the third, for reading lines from a text file and instantiating spheres based on them.

addNNum_1
// To use this, create a file called addNNum_1.mel with the proc below, and call it
// with something like
// addNNum_1   10  <Ret>

global proc addNNum_1(int \$N)
{
// \$sum will hold the result from the formula/equation
int \$sum =  (\$N * (\$N+1)) * 0.5 ;

print("The sum of first " + \$N + " numbers is " + \$sum + "\n");
}// addNNum_1()

addNNum_2
// To use this, create a file called addNNum_2.mel with the proc below, and call it
// with something like
// addNNum_2   60  <Ret>

global proc addNNum_2(int \$N)
{
int \$sum = 0; // will accumulate our result
int \$i; // loop 'counter' variable

// accumulate the result in a 'for' loop. In other words, we start with 0, then add 1 to that,
// 2 to the result, 3 to that result, and so
for(\$i=1; \$i<=\$N; \$i=\$i+1)
{
\$sum = \$sum + \$i;
}
print("The sum of first " + \$N + " numbers is " + \$sum + "\n");
}// addNNum_2()

createPrims
// Reads a file from disk, creates spheres or cones
// based on contents of the file
// Create a text file (eg. call it 'spheres.dat') and include
// lines in it, where each line is of the form
// sph xlocation ylocation zlocation radius
// A sample line could be
// sph 0 0 5 12.0
// Use the following program [call it createPrims.mel]  by saying
// createPrims spheres.dat <Ret>

global proc createPrims(string \$inFileName)
{

// open the file
int \$fileId=`fopen \$inFileName "r"`;

// for each line
//  read line
//  get position, radius
//  create sphere

string \$nextLine = `fgetline \$fileId`;
while (\$nextLine != "")
{
int \$numTokens;
print ("The ***** next line is " + \$nextLine + "\n");

// Instance a sphere!!

string \$tokens[];
\$numTokens = `tokenize \$nextLine " " \$tokens`;

if(\$tokens[0] == "sph")
{
print("Creating a sphere!\n");
float \$rad = \$tokens[4];

float \$xpos = \$tokens[1];
float \$ypos = \$tokens[2];
float \$zpos = \$tokens[3];

// The following is where the actual sphere creation and translation happens
string \$sph[] = `sphere -r \$rad`;
select \$sph[0];
move -a \$xpos \$ypos \$zpos;
}
\$nextLine = `fgetline \$fileId`;
}// while there is a next line to read

// close the file
fclose \$fileId;

}// createPrims()

Here's a script that will read a text file containing XYZ data, in
the form
1.2 -4.5 0.8
12.6 78.098 1.3425
...
and create a new file with the Y and Z values transposed.

global proc readAFile(string \$fileNm, string \$newFileNm)
{
int \$fileId=`fopen \$fileNm "r"`;

int \$newFileId = `fopen \$newFileNm "w"`;

string \$tokens[];
int \$numTokens;

string \$nextLine = `fgetline \$fileId`;
while ( size( \$nextLine ) > 0 ) {

\$numTokens = `tokenize \$nextLine " " \$tokens`;

float \$newX = \$tokens[0];
float \$newY = \$tokens[2];// SWAP!!
float \$newZ = \$tokens[1]; // the other part of the swap
print ("Old: " + \$nextLine);
print("New: " + \$newX + " " + \$newY + " " + \$newZ + "\n");
fprint \$newFileId (\$newX + " " + \$newY + " " + \$newZ + "\n");
\$nextLine = `fgetline \$fileId`;
}
fclose \$fileId;
fclose \$newFileId;

}// readAFile()