Maya Expressions 2

One day, a mischievous Alias|Wavefront programmer took the decision to create a wonderfully useful function in Maya. He or she then proceeded to hide it deeply enough inside the interface so that as few people as possible could hope to find it (although I think I saw it somewhere more visible in Maya 6). The function is called the distanceBetween node, and is the object of this tutorial. If you’re new to expressions, you might want to check out my first tutorial on the subject before tackling this one.

What this node basically does is take two vector inputs (such as Translate X, Y and Z), calculate the distance between them, and then output the result into a distance attribute. This allows for some very basic effects to be achieved with little fuss.

The distanceBetween node doesn’t have to have translate values as inputs, but they are the best way to explain how it works. So here goes.

The distanceBetween node

The distanceBetween node

A simple vector

A simple vector

Creating the node

In the Command Line, type the following MEL command:

createNode distanceBetween

and press Enter.

Now isn't that user-friendly

Now isn't that user-friendly

As you probably figured out, this command creates the node. Following the logic which dictates that this node should be hidden from most mortals, it doesn’t appear in the Outliner, Hypershade or Hypergraph. You’ll need to go to the Outliner, and turn off DAG Objects Only from the Display menu.

Still invisible...

Still invisible...

Tada. Since you’ll spend quite some time hunting for it later, you might find it useful to create a button which selects it for you. Just select another node and then select the distanceBetween1 node again. Open the Script Editor, highlight the “select -r distanceBetween1 ;” command which will show up there and middle-drag it to a shelf.

...but not for long

...but not for long

A simple application

Now for the fun part. Create a couple of spheres and assign the same material to both of them. Give it a nice, popular color such as red. Open the Connection Editor (Window > General Editors > Connection Editor…), select one of the spheres and click Reload Left. Select the distanceBetween1 node (either through the Outliner or with your shelf button), and click Reload Right.

Connecting the node

Connecting the node

Towards the bottom of the list on the left, click the Translate attribute. On the right-hand side, click the Point1 attribute. This connects the sphere’s XYZ translate values to the distanceBetween node’s first input.

Select the second sphere and click Reload Left in the Connection Editor. This time, connect the Translate attribute to the Point2 attribute. Check the Hypergraph to see how the nodes are connected.

Two vector inputs

Two vector inputs

The resulting value, which is contained in the distanceBetween1.distance attribute, can be used in an infinite number of ways. Let’s assume that we want the spheres to glow when they get close to one another. You could connect the distance attribute to, say, the incandescence R, G & B values for the material you assigned (remember that distance is a float value, so it goes into each channel separately), but this can be problematic. The distance is measured in world units, and unless your spheres stay within a 0 to 1 distance from each other, you can get funky results. It’s better to write an expression for it.

Mapping the Distance to Incandescence

Mapping the Distance to Incandescence

An expression for more control

Assuming the material assigned to both spheres is called blinn1, create a new expression with the following lines (ignore any line breaks, and you might double check your spelling if you get an error, incandescence is a particularily treacherous attribute to type in):

blinn1.incandescenceR = (10-distanceBetween1.distance)/8 ;

blinn1.incandescenceG = (10-distanceBetween1.distance)/8 ;

blinn1.incandescenceB = (10-distanceBetween1.distance)/8 ;

An expression might be more handy...

An expression might be more handy...

What this does is take the distance and subtract it from 10, so that the spheres glow when they approach each other, instead of when they move away. The 10 value determines at what distance the glow starts. The result is then further divided by 8 so that the transition of the glow is more gradual. Move the spheres around in textured mode to see it in action. Also, try this with different values and see what happens.

There’s still a problem. When the spheres are farther than 10 units apart in this example, the incandescence becomes negative and they go completely black. Remedy this by adding a clamp function, so that the expression reads:

blinn1.incandescenceR = clamp(0,1,(10-distanceBetween1.distance)/8) ;

The same should go for the Green and Blue channels.

...if it's written correctly

...if it's written correctly

What this does is specify Minimum and Maximum values (the 0 and the 1) to be allowed for the third value in the function (the distance value in this case).

For some eye candy, move the spheres apart so that they don’t share any axis, and attach them both to a Newton field positioned between them.

Nicolas Aithadi has created a Cornell Box simulation using the distanceBetween node. I actually wrote this tutorial because I got confused half way about what should be connected where.

Emmanuel Campin, author of the SkyLightControl MEL Script, has an even more involved example which fakes soft shadows with Dmaps.

share this

6 Comments

  1. Posted November 26, 2006 at 17:44 | Permalink

    I did not have this one.
    Thanks.

  2. Nadia
    Posted April 4, 2007 at 22:28 | Permalink

    Hi,

    I would like to know, How show the attributes of the node distanceBetween in addition to connect to the sphere. This attributes is hidden

  3. Tal
    Posted April 5, 2007 at 0:22 | Permalink

    Please read the tutorial carefully. The text clearly says “You’ll need to go to the Outliner, and turn off DAG Objects Only from the Display menu.” I hope this helps.

  4. MatthewGaw
    Posted April 9, 2007 at 12:04 | Permalink

    tnx Tal! Your tutorials are very useful!

  5. vincent
    Posted April 17, 2007 at 16:24 | Permalink

    thanks… this is really useful. I was wondering on another note, do you know how to hook one dynamic field up to another? i.e. I want to have a gravity field effected by a vortex. Both of which have max distances. I tried point constraining the gravity field to an object, and then hooked the object up to the vortex but even though the vortex then moves the object and its child (the gravity field), whatever effects I was getting from the gravity field on other objects disapeared.

  6. Aegis
    Posted June 3, 2008 at 7:09 | Permalink

    thanks
    it is very useful
    but something i want to know is how can i control the distance range that make it glow
    in my example i got two object within a short range so they will always glow
    so how can i control the distance range?
    thanks for your help again

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>