View Full Version : Emit particles from mesh through scripting

05 May 2010, 04:12 AM
Hi, how to do go about scripting particles to get them to emit from a mesh?I found a tutorial that shows how it can be done through vertex but have no idea how to get it to spread along the faces of the mesh.


05 May 2010, 08:04 PM
Are you talking about "emitting from the mesh" or "spreading along the mesh"?

The Position Object - Emit From: "Selected Faces" or "Faces" does not work for you?

Maybe a little more info, some specifics on what you are trying to do :)

05 May 2010, 12:27 AM
Hi. what I'm trying to achieve is to control the number of particles emitted from each emitter using 1 PF source. If i were to use position object, it will just randomly spread my particles between the emitters. Thanks.

05 May 2010, 02:29 AM
Well if you can't clone the emitter objects and assign each a position object, and you don't have Birth Texture, or Box#3.

The only thing to do would be to script it, and emitting from a particular face is easy enough it is getting them to spread across that face that would be difficult (at least for me, anyway) and frankly not sure how you would go about that via script. sorry :sad:

Sounds like a job for the maXster of Scripting

05 May 2010, 03:29 AM
Sounds like a job for the maXster of Scripting

Somebody called? :)

If the birth operator creates, say, 1000 particles and 100 of them should be on the surface of mesh 1 and 900 of them on mesh 2, this could be done with a Split Amount Test and two events with separate Position Object operators. But if the number of emitters is high (say, 10), it might make sense to use scripting.

Let's assume we have 10 meshes and we know exactly how many particles we want on each one of them. We can approach this by calculating the total area of each mesh (summing the face areas of all faces of each mesh) to figure out the density of particles per square unit area. Then we can loop through all faces one by one, get again the face area and calculate the number of particles that would be required for that face based on the density we already know. For example, if the density we calculated is something like 0.6 particles per square generic unit and the face has an area of 3.45 units, then 0.6*3.45 = 2.07 or we should distribute 2 particles randomly on the face.

For this, we can use random barycentric coordinates. Barycentric coordinates express a value inside the face based on 3 values which are calculated as the ratio of the face area of the sub-triangle defined by two vertices and the point in the face and the total area of the face. These 3 values always sum up to 1.0 and the area of each triangle divided by the total area expresses the "weight" of the value of the vertex opposite the small triangle. Thus, with a Point3 value, we can express a position based on 3 vertices' positions, a UVW value based on 3 vertices' mapping coordinates and so on. In our case, we will create two random values for the barycentric coordinate and the third will be picked so that the three sum up to 1.0.

A good code for this is

theX = random 0.0 1.0
theY = random 0.0 1.0
if theX+theY > 1.0 do
theX = 1.0 - theX
theY = 1.0 - theY
theZ = 1.0 - theX - theY

In the above case, [theX, theY, theZ] is a valid random barycentric coordinate.
To get the random point on the face, just multiply the position of the first vertex in the face with theX, the position of the second vertex with theY and the third with theZ and add these results together!

Then place the particle at that point and go on...

05 May 2010, 04:23 AM
Here is a simple example with 3 objects - a Sphere, a GeoSphere and a Box.
I have defined to distribute 100 particles on the Sphere, 300 on the GeoSphere and 600 on the Box, out of 1000 particles.

The script performs all calculations to find random points on the faces based on area density and also orients the particles to the face normal by building a complete matrix.

The script only moves the particles on frame 0, exactly the time at which all 1000 particles are created. Creating particles continuously can be tricky since the script assumes all particles are dealt with at once.

05 May 2010, 05:42 AM
Thanks for the reply Bobo! That was very well explained and detailed. Exactly what i was trying out for. Didnt expect just to have particles spread along a mesh surface requires so much work.Thanks again.

05 May 2010, 04:20 PM
Bobo to the rescue! That deserves a Port'o'Call Cheeseburger :)

All I can say is... Wow, that is cool, it would have taken me an eternity to figure that out (if ever):blush:

I looked at the polyOp/meshOp getFaceArea but I had no idea what to do with it once I got the face area, ie how to define the shape of the tri, then distribute across it.

Another fine example to add to the "Pool of Cool"!

Thanks for your help :)

CGTalk Moderation
05 May 2010, 04:20 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.