Particle Expressions

From TOI-Pedia


This tutorial covers Maya expressions in combination with particles.

We're going to create a scene in which particles collide with geometry. For every collision the color of the particle will change, so you can see the amount of collisions per particle by the color.

Preparation: Particle and Collision setup

First, we're going to create an emitter. Make sure the Dynamics Menu set is selected.

Dynamics menuset.jpg

Choose particles > create emitter. The different types of emitters are covered in the Maya Help. In this case, we're using a Directional emitter.

Next we create some basic geometry to let the particles collide with. This can either by Polygons or NURBS.

Particle maze.jpg

Next step is to set the particles to collide with the objects. You need to do that one by one. Make sure you select the particles and not the emitter.

Particle make collide outliner.jpg (Window > Outliner)

Then shift-select a surface and choose Particles > Make Collide. Repeat this for all surfaces.

If you play the animation, the particles should now collide with the surfaces.

In order to keep track of the number of collisions of each particle, we need to add an Collision Event. Select the particles and choose Particles > Particle Collision Events...

Collision event new event.jpg

Enter a name for the event, and click Create Event and Close the window. You don't need to set any other values for this example. It might be interesting to explore the other options in this menu however.

That's all for the preparation. Next, we're going to add attributes to our particles and wirte expressions for them to color them.

Particle Attributes

Select your particles and open the attibute Editor (Window > Attribute Editor, or Ctrl+a). Make sure you select the particleShape tab.

We can map the color of the particle to a color ramp. A ramp is typically driven by a zero to one value which maps onto the ramp. The number of collisions typically isn't. So we need some sort of intermediate calculation. Therefore we're going to add a custom 'per particle' attribute to our particles.

Scroll down and locate the Per particle attributes section. Beneath is the Add dynamic Attributes section.

Per particle attributes.jpg

Click General to add a general attribute.

Add float pp attribute.jpg

Enter a name and set the data type to float (for this example). Make sure you set the attribute type to 'per particle' if you want per particle values. Click OK to add the attribute and close the window. The newly created attribute should now show up in the Per Perticle Attributes overview in the attribute Editor.


Next, we're going to create an expression for our new attribute. Right-click in the input field next to the attribute name and hold your mouse button. Select Runtime Expression Before/After Dynamics and release the mouse button.

Create runtime expression.jpg

Our goal is to translate the number of collisions (events) into a number between 0 end 1. We need to know the maximum number of collisions we can expect (estimate). The event attribute is increased by one for every collision event. By dividing event attribute by the maximum number, we'll end up with a number between 0 to 1. In this example we set the maximum to be 10.

Collision expression simple.jpg

If you want to make sure the result of the expression is never outside the 0 to 1 range, we can use the clamp() function. Please refer to the MEL Command Reference (Help) for information on available MEL function. You can use all MEL functions in expressions.

The resulting expression will look like this:

Collision expression clamped.jpg

Click Create to create the expression. you can then Close the window. If you want to edit the expression, use the Edit button. To open the expression window once you've closed it, right click in the attribute input field and choose Runtime Before/After Dynamics. The expression editor should open and show any existing expressions for the selected attribute.

For this tutorial, the difference between Runtime Before and After Dynamics is irrelevant. It's suggested to use the Runtime Before Dynamics expression.

Color per Particle

In the Add Dynamic Attributes section of the Attribute Editor (for your particleShape), click the Color button.

Particle color PP.jpg

Check Add per particle attribute and click Add attribute. The 'rgbPP attribute will apear in the list.

We're going to use a ramp to convert the 0 to 1 value into a color for the particle. Right click in the input field next to the rgbPP attribute. Choose Create Ramp and select the optionbox:

RgbPP create ramp optionbox.jpg

A new window appears. Select your attribute that has the 0 to 1 value as Input V and click OK.

Ramp mapping options.jpg

To edit the ramp, right-click again and choose Edit Ramp. You'll see a window like this:

Ramp blue red.jpg

You can edit the ramp to your own preference.


You need to be in shaded mode (5) in order to see the particle colors. A screenshot of the resulting animation:

Particle maze colored.jpg

Other options

Other expressions might be:

Add general attribute speed. expression:

particleShape1.speed = clamp(0,1,(mag(particleShape1.velocity)/10));
Personal tools