Article by Satish B, Head Academics, Department of Animation, Jain University

 

The article assumes two things.

1. The user knows what a MEL script is.

2. The user knows how to load and execute a MEL script in Maya.

 

The FOR loop is a very important construct in coding. It lets you execute a group of commands a specific number of times. The general structure of a FOR loop is as shown below.

for(initiate counter; check condition; increment counter)
{
      execute commands;
      execute commands;
      execute commands;
}

The construct is pretty much the same for MEL.

In Maya, we can use the FOR loop to repeat commands or steps that are time consuming to execute manually.

For instance, we can generate a staircase by creating and arranging cubes in a step fashion. Execute the following MEL script from Maya’s script editor to see it in action.

// Simple Staircase generator
// MEL by Satish B

int $i;
int $maxSteps = 10; // Set maximum number of steps required
float $width = 4.0; // Set width of each step
float $height = 1.0; // Set height of each step
float $depth = 1.0; // Set depth of each step
float $cusHt;
float $cusDp;

for ($i=0; $i<$maxSteps; $i++)
{
    polyCube -w $width -h $height -d $depth -ch 0 -n "Step#" ;
    $cusHt = $i * $height; // Calculate the height to move the cube
    $cusDp = $i * $depth; // Calculate the depth to move the cube
    move 0 $cusHt $cusDp;
}

The above MEL script creates the following in Maya’s viewport.
simple staircase

Horizontal-rule-fl-green

The following MEL script uses the FOR loop to construct a hexagonal hive pattern. Follow the steps and see if you can understand how it works.

//Hexagon hive builder
//MEL by Satish B

int $rows = 20; // Set numbers of rows
int $num = 20; // half of this number of objects will appear in each row
int $i;
int $j;

for($i=1; $i<=$rows; $i++)
{
	for($j=1; $j<=$num; $j++)
	{
		if($j % 2 == 0)
		{
		polyCylinder -r 1 -h 0.5 -sx 6 -sy 2 -sz 1 -ch 0 -n "hive#";
		polyBevel -offset 0.1 -segments 2 -fillNgons 1 -ch 0;
		if($i % 2 == 0)
		{
				move ($j * 1.5) 0 ($i * 0.8);			
		}else
		{
				move (($j * 1.5) + 1.5) 0 ($i * 0.8);
		}

		}else
		 	{
			 	//Do nothing	
		 	}

	}
}

Execute the above MEL script to create the following pattern which would otherwise be time consuming to create.

hexagon hive

Horizontal-rule-fl-green

Let us look at another example.

The following MEL script uses FOR loop efficiently to build a brick wall in Maya.

//Wall builder
//MEL by Satish B

int $wallRows = 15; //Set number of rows
int $numBr = 10; //Set number of bricks in each row
int $i;
int $j;
int $offX;
int $offY;

select -cl;

for($i=1; $i<=$wallRows; $i++)
{
	for($j=1; $j<=$numBr; $j++)
	{
		createBrick(); // Call the procedure to create a single brick
		if($i%2==0)
		{
			$offX = $j * 2;			
		}else
			$offX = ($j * 2)+1;

		$offY = $i;
		move $offX $offY 0;
	}
}

// The following procedure creates a brick by beveling a polygon cuboid
proc createBrick()
{
		polyCube -w 2 -h 1 -d 1 -ax 0 1 0  -ch 0 -n "brick#";
		polyBevel -offset 0.1 -segments 2 -fillNgons 1 -ch 0;
}

The result of executing the above script can be seen below.

brick wall

Horizontal-rule-fl-green

We can use the FOR loop construct and create some very useful tools in Maya.

For instance, while rigging, we can prefix joint names by using Modify > Prefix Heirarchy Names

Unfortunately, there is no option to suffix joint names.

In a character rig, often we must suffix

“_jnt” to skinnable joints,

“_fk” to FK joints and

“_ik” to IK joints.

The following MEL script is a simple way to suffix a joint chain with a chosen string.

// Suffixing string 
// MEL by Satish B
// Just select the root joint and execute this script

string $suffix = "_jnt"; //Set the string to be suffixed
select -hierarchy;
string $getObjects[] = `ls -selection`;

int $numObjects = size($getObjects);

int $i;

for ($i=0; $i<$numObjects ; $i++ )
{
select $getObjects[$i];
string $temp = $getObjects[$i] + $suffix;
rename $temp;
}

After execution, the following result can be seen in the outliner.

joint suffix

 

 

The FOR loop can be used in MEL procedures and scripts to carry out custom modeling, rigging, lighting, animation, dynamics, and rendering tasks.

Hope this article has helped you to familiarize with the FOR loop construct.

If you have any questions, do mail me (satishb.iyer@gmail.com).

Have fun coding!!