Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

RealFlow provides a very easy and reliable method for transferring particles from one emitter to another. The combination of a "Container" emitter and "Filter" daemon can be used for this purpose. "Container" and "Filter" are, of course, a convenient alternative, because you can achieve stunning effects without a single line of Python code. Nevertheless it is sometimes necessary to do this within a Python script. Particle shifting can be considered as one of the core concepts with scripting, as it is a versatile and often used method. Particle shifting can be based on several particle properties, like velocity, pressure, age, collision and many more.

 

Name

GUIParticleShift.rfs

Type

Batch and Simulation Events

Description

This script finds particles meeting a certain given criteria and shifts them to a second emitter. Target and source emitters can be meshed and finally rendered separately. Most of the initial settings are  made via a custom GUI.

...

Code Block
themeEclipse
languagepython
linenumberstrue
emitterList = []

colourList = ["Red","Orange","Purple","White","Yellow"]
rgbList    = ((200,0,25),(255,150,0),(180,0,180),(255,255,255),(255,225,0))
emitters   = scene.get_PB_Emitters()

for emitter in emitters:
    emitterList.append(emitter.getName())

 

Except for the rgbList() statement, everything should already be familiar to you. The notation of the rgbList() list entries is different, because each element consists of three values enclosed in parenthesis. This is an exciting feature, because you can nest lists inside a list. The three values here are also called tuples. They are necessary because the RGB model always needs three values to define a certain colour. Since the tuples are lists, it is possible to call the individual elements through their indices, too. That is the complete set of instructions for assembling a colour:

...

The selection from the GUI is translated into a variable called colour. Since all selections and choices are translated into numbers, you can directly extract the chosen colour from rgbList[]. The result of this operation is again a list object, containing the three values for R, G and B. Each element of this list has a fixed position and can be accessed via the index. The individual entries are then used to create the colour “vector”. The next part is just basic GUI programming:

...

Finally the emitters are equalized with getParameter() and setParameter() to avoid problems. The most important physical settings are read from the source emitter and then transferred to the target:

...

All the variables defined so far are local. This means that they cannot be transferred to other parts of the script, for example from batch to simulation events. Nevertheless some of the values have to be transferred to the simulation events section. For this purpose, global variables must be introduced, and you first have to find out which values are shared. For "threshold" and "tolerance" it is pretty straight forward, but for the emitters it is slightly different. With emitterList[emitter1] and emitterList[emitter2] it is possible to find out the particular names, for example “Circle01” and “Circle01”. These names are stored in global variables and transferred to the simulation events section.

...

This initial part is finished with the assignment of the global variables. The second task is to find the particles meeting the previously entered velocity threshold. “tolerance” is a random value that will be added or subtracted to get a more natural look. Since “tolerance” works in both directions, only half of the original value will be used. Everything is located under simulation events:

Simulation > Steps > StepsPre > Right click > Add script...


For this script a tolerance value has to be determined. With a given "tolerance" value of 0.5, for example, the final number lies between -0.25 and +0.25:

...

The result is a float number that can be added to the entered "threshold" and then compared against the current particle’s velocity:

...

The process of shifting particles between emitters should be familiar to you by now, because this technique has already been introduced and discussed in the chapter “Shifting Particles”. The only new thing here is the call of global values from the batch script section. With these values it is possible to identify the source and target emitter, and make use of "threshold" and "tolerance". Type this part to

Simulation Events > Scene > ScenePre

...

Code Block
themeEclipse
languagepython
linenumberstrue
import random
 

# Get the global values from batch script

source_name = scene.getGlobalVariableValue("source")
target_name = scene.getGlobalVariableValue("target")
threshold   = scene.getGlobalVariableValue("threshold")
tolerance   = scene.getGlobalVariableValue("tolerance")
rndValue    = random.uniform(-tolerance / 2, tolerance / 2)

source      = scene.get_PB_Emitter(source_name)
target      = scene.get_PB_Emitter(target_name)
 

# Loop through the particles, compare velocity and shift them to target

particle = source.getFirstParticle()
while (particle):
    if (particle.getVelocity().module() >= threshold + rndValue):
        pos = particle.getPosition()
        vel = particle.getVelocity()
        pid = particle.getId()

        target.addParticle(pos,vel)
        source.removeParticle(pid)

    particle = particle.getNextParticle()

...