Use expressions to control Particles
shows how to use MEL and Python in Maya to give values to Per Particle attributes based on coding
Syawish A Rehman shows how to use Maya to give values to Per Particle attributes based on coding
in Maya, nparticles are one of the most powerful features in your toolkit. Not only can you do amazing things with them, but you can also create expressions to manipulate every single particle. You create an expression and Maya actually evaluates on every frame what that expression dictates and manipulates each particle accordingly. The Maya expression system in itself is very powerful, so the possibilities of what you can do with expressions in Maya are limitless.
In this tutorial, we’ll create a simple forest scene using nparticles and I’ll show how to use expressions to make the forest look random and realistic, as well as how to create a field to control any attribute of the particles. For example, you can create a field to control the scale of the trees, as we’ll do, and the trees inside that field will be smaller or larger than the others depending on what you write in the expressions. We’ll be using a field in this case because it has a magnitude that can detect what particles are being influenced. You’ll learn the basics of manipulating any attribute of every single particle in a particle array using expressions. You’ll also learn where all the attributes are actually hidden and how to activate them.
01 create an emitter
Select the mesh you want to emit particles from. Under the FX module in Maya, go to the nparticles menu. Click Create Emitter>emit from Object. In my case, I’ll be emitting particles from a polyplane. Once you’ve created the emitter, turn the Gravity and Speed to zero in the Nucleus node and the Emitter node respectively. Key the emission rate to stop emitting.
02 prepare your geometry
For this tutorial, I’m going to create a couple of trees to use as instancing geometry to place with particles over the polyplane. If you want to follow what I’m doing, just go the Windows menu bar and under the General Editors submenu, find Visor. This contains many types of useful objects. Under Trees, select Cherry Tree and then draw it on your surface with a paint stroke. Afterwards, simply convert the mesh from paint stroke to polygons and combine the leaves and the trunk.
03 instancing geometry
First of all, delete the history on your geometry and freeze transformations. Select the instancing geometry first, in this case, the tree mesh, and then select the nparticles node. Under the FX module> nparticles, find Instancer and click it. Now, you should see all the particles you had created replaced with the Tree mesh that you had selected. All the copies of the tree mesh have the same attributes, so we have to add some randomness.
04 randomising rotations
Under the nparticle node, go to the Rotation tab and turn on Compute Rotation. Go to the Per Particle (Array) Attributes tab, right-click the Rotation PP field. Click Create Expression. We only want to add some rotation in the Y axis. A simple expression can be used. Enter“np article shape 1. rotation pp =<<0, rand (0,360),0 >> ;” Check what your nparticleshape node is named.
05 ADD a Field
Select the nparticle node and under FX module, go to Fields/solvers. Create a Volume Axis field. It’s set to push your particles away from its center. Change that under Volume Speed Attributes>away from Center. Set to 0.01, which is neither complete zero nor large enough to do something. The shape of the volume can be whatever you want under Volume Control Attributes tab.
06 ADD radiuspp
Under the nparticle node, find the Add Dynamic Attributes tab. Here you can activate attributes similar to Rotationpp. You should see three buttons; General, Opacity and Color. Click General. A new window opens with three tabs. Click the Particles tab and find radiuspp from the list. Click OK. You’ve activated the attribute to set an expression to manipulate the radius of particles.
07 connect Volume Field to radiuspp
Right-click radiuspp field, click Runtime Expression before Dynamics to add an expression that will be evaluated before simulating a frame. Write “vector $field =np article shape 1. input force [0];” to create a new vector variable and store the inputforce of the nparticle. Add float “$fieldmag = mag($field);” to create a float variable to store the magnitude of the variable just created.
08 offsetting Values
Because the Volume Field’s force on the particles had to be so low, we need to multiple the fieldmag variable with 100 to get a fair value of 1. Simply add “$fieldmag = $fieldmag * 100;” This is only for your calculation, and doesn’t have anything to do with the actual particles. They’re still only taking 0.01 force from the Volume Field. Now, add “float $newscale = 1 - $fieldmag;” to store what the new scale for the particles will be.
09 condition For scale
Now we have to add a condition where Maya evaluates if the particles are inside the Volume Field and if it decides they are, to then scale them down accordingly. Add“if ($ field mag!=0){n particle shape 1. radius pp =<< $new scale ,$ new scale ,$ new scale >>;} else{ np article shape 1. radius pp =<< 1, 1, 1 >>; }” This condition checks if the fieldmag isn’t zero. If it isn’t, it will apply the newscale to all three axes of the particles, otherwise it keeps the scale at 1.
10 attach connections
We have to tell Maya to get its values from the expression we just created. To do that, Under Instancer (Geometry Replacement) tab, check Allow All Data Types. Find the option for Scale and set to Radiuspp. Under the sub-tab called Rotation Option, set Rotation to Rotationpp. This will apply randomisation in the rotation we created earlier. •