Flow control

if

if(condition) 
{ statements_if_true } 
[ else { statements_if_false } ]

Standard flow control statement: if condition is true then execute statements_if_true, otherwise execute statements_if_false (if exists).

for loop

for(init; condition; step) { statements }

Executes init operations, next executes statements while condition returns true. Performs step at the end of iteration.

It is possible to perform for loop statements controlled by point field values. It means that for given vertex, loop statements execution depends on its field values. Example below presents point position transformation based on per-particle attribute:

vert p = inMesh[0];
int p.x = rand(p.pointID) * 10;
int p.i;
for(p.i=0; p.i<p.x; p.i+=1)
{
    p.position.y += 1;
}

outPoints[0] = p;

forLoop
Random number of for-loop iterations per vertex

& – For each neighbor

&(vert) { statements }

Special flow control statement in MGL. Considered as for each neighbor of every point. Executes statements for every neighbor of points in given vert object.
Use & operator to access neighbor field. Example below averages points position:

vert p = inMesh[0];
int p.n;
int i;

for(i=0; i<20; i+=1) // perform 20 iterations of smoothing
{
    p.n = 1;
    &(p)
    {
        p.n += 1;
        p.position += &.position; // Add neighbor position to current vertex position
    }
    p.position /= p.n; // Divide position vector by neighborCount + 1
}

outMesh[0] = p;

Result:

neighbor

Only points created from inMesh attribute contain neighborhood information, that is defined by mesh edges.

Previous: Communication with Maya | Language reference | Next: Operators