Adverti horiz upsell
Writing a procedure that measures the distance in between two transforms
Writing a procedure that measures the distance in between two transforms
kmannens, added 2006-02-20 16:56:05 UTC 29,233 views  Rating:
(2 ratings)
Page 1 of 1


This small procedure measures the distance in between two transforms. In this particular case two individual particles, but with the same workflow can be used for say for joints or locators. You could use the information this procedure provides to make your particles �do� something. Say you want you particles to die when they come within a certain distance from each other; or you want them to explode into more particles once the are 15 units away from each other. The sky is the limit.

Pseudo code:

(Pseudo code is a sort of half English, half MEL language you use to do a drop down design, to order and categorize your thoughts, before you actually start writing the code)

1. Get the world position of each particle using the getParticleAttr command and store the result in a variable (in casu a float array)

2. Get the indivual elements out of the array and substract them from each other

3. Use the mag command to get the length (distance) of those 3 values.
(=The �vector� that results from the subtraction of the 2 float arrays that hold the position of the particles)


1. I know what you are thinking and it is indeed a bit weird that I need use a float array and not a vector to store the position of a particle. But in the command reference it states that the return value of getParticelAttr is a float array.

2. Arrays are zero-based, so always remember the first element in array is [0]. So, also here, with the declaration of $distancePos.

3. Read the docs on mag to find out the math behind it. The normal (Using Maya) docs give a better explanation then the MEL command reference. Basically, mag returns the magnitude of a vector. This is the length of the vector.

4. Obviously this code could be made more dynamics by working with selected items, instead of specific particles.


//First, create two particles with the particle tool

//Declare the variable
// When expecting a return value, you need to
// explicitly declare the data type of the procedure,
// as you do with variables. In this case a float

global proc float getPartDistance()
//Get the disctance of both particles
//and store them in a variable
float $part1Pos[] = `getParticleAttr -at position[0]`;
float $part2Pos[] = `getParticleAttr -at position[1]`;

//Declare a new variable that holds the
// results (xyz) of the substarction of
//the position of both particles.

float $distancePos[];
$distancePos[0] = $part1Pos[0] - $part2Pos[0];
$distancePos[1] = $part1Pos[1] - $part2Pos[1];
$distancePos[2] = $part1Pos[2] - $part2Pos[2];

// Use mag to get the length of those 3 values
// This very conveniently also converts 3 (vector) values in 1 (float).
float $distance = `mag <<$distancePos[0], $distancePos[1], $distancePos[2]>>`;

//return the result
return $distance;