The 'mag' command |
As simple as this command really is I have found that many people new to MEL, and with an average knowledge of math, have a hard time wrapping their heads around what this command does exactly. All that the MEL documentation tells you is this:
"This command returns the magnitude (or length) of its argument."
Underneath that is this example:
mag <<7, 8, 9>>; // Result: 13.9284 //
Come again?
I think the confusion comes from the fact that when people look at a vector value, like the one in the example, they don't see a distance but a position vector. A position vector is a distance, it is the three-dimensional distance from <<0, 0, 0>>. To demonstrate this create a locator and position it (xform -t) at <<7, 8, 9>> in world space (-ws). Then create a distance tool (distanceDimension) with its starting point (-sp) at the locator position and the end point (-ep) at the center of the grid.
// Create a locator and position it at <<7, 8, 9>> string $loc[] = `spaceLocator`; xform -ws -t 7 8 9 $loc[0]; // Create a distance tool starting at <<7, 8, 9>> // and ending at <<0, 0, 0>> distanceDimension -sp 7 8 9 -ep 0 0 0;
The distance tool has an annotation visible in the viewport which is displaying the distance between the two points; which is 13.928388. The mag command example rounded off the last two decimals places so this is why the value from the example in the MEL docs is not exactly the same as what the distance tool tells us (floating point accuracy), but trust me it is correct.

So knowing this you can now query the exact distance from one point to another. The example below creates two locators in randomized positions and a distance tool connecting the two positions. The second position is subtracted from the first position before being evaluated by the mag command.
// First locator string $loc1[] = `spaceLocator`; float $x1 = rand(-10,10); float $y1 = rand(-10,10); float $z1 = rand(-10,10); xform -ws -t $x1 $y1 $z1 $loc1[0]; // Second locator string $loc2[] = `spaceLocator`; float $x2 = rand(-10,10); float $y2 = rand(-10,10); float $z2 = rand(-10,10); xform -ws -t $x2 $y2 $z2 $loc2[0]; distanceDimension -sp $x1 $y1 $z1 -ep $x2 $y2 $z2; mag (<<$x1, $y1, $z1>> - <<$x2, $y2, $z2>>);
The result returned from the mag command:
// Result: 13.929988 //
And the result of the distance tool.

Because the positions are randomized you will get different results from mine.
You can perform simple math function on vector values, when doing this inside of the mag command be sure to enclose them in parentheses. If you forget you will get an error like the one below.
// Error: mag <<$x1, $y1, $z1>> - <<$x2, $y2, $z2>>; // // Error: Syntax error //
Bringing out the nerd in all of us
The magnitude of a vector is derived from the formula for OP, or |p|, which is a three-dimensional model of Pythagorean theorem. The Pythagorean theorem is a relation of the three sides of a right triangle. A three-dimensional vector is made up of two triangles, we solve for one and then use the result to solve for the second. In the example below the first triangle is flat on the grid (XZ plane) which has a vector equivalent of <<7,0,9>>. The formula is:
• OQ = the square root of (X^2 + Z^2)

Using MEL to calculate OQ:
float $x = 7; float $z = 9; float $OQ = pow($x,2) + pow($z,2);
If we were to query the square root of OQ:
sqrt($OQ); // Result: 11.401754 //
It would be identical to:
mag <<7,0,9>>; // Result: 11.401754 //
Even a closet-geek will recognize that the result is actually a hypotenuse which would make Z the adjacent and X the opposite.
Now we get the angled triangle, that makes up <<7,8,9>>, with the following formula:
• OP = the square root of (OQ^2 + Y^2)

Using MEL to calculate OP:
float $y = 8; float $OP = $OQ + pow($y,2); sqrt($OP); // Result: 13.928388 //
This is the same result we get from:
mag <<7,8,9>>;
Understanding all this you could put it all together and solve like this:
sqrt(pow($x,2) + pow($y,2) + pow($z,2));
// Result: 13.928388 //
I find it easier to demonstrate solving for one triangle at a time. Earlier I said the mag command is the distance from <<0,0,0>>. Technically speaking the mag command returns the hypotenuse of the three-dimensional model of Pythagorean theorem. I have found that until you understand the actual math that this rarely makes sense, hence my earlier explanation which is a little less confusing to the non-math types.
I hope these small examples clears up any confusion about the mag command.
- Ed
References: