Changing Attributes

So far we have been talking about standard data types, how to build loops and call individual particles, and the way you can differentiate between options with if and else. You have also learned how to create a new vector and heard something about the “get” statement. With “get” you are able to directly access certain values and parameters, such as position, rotation, mass, friction or even particle-based properties, like density or velocity. That is an interesting feature, because you can read out the values, store them in lists, vectors or scalars, and perform different kinds of calculations with them. But that is only one half of the story, because there has to be a way to assign the new values back to the relevant object or particle. For this purpose, RealFlow offers the "set" statement. “Get” and “set” often appear together, but they are not necessarily linked. Some of the “get” statements you have seen so far are:

  • string getEmitter()
  • string getObject()
  • string getParameter()
  • vector getPosition()
  • vector getVelocity()

 

The syntax of “set” is pretty much the same as with get statements. The list of get instructions is relatively long, but fortunately the concept behind this function is rather easy and for some statements there is a counterpart with “set”:

float getDensity()   -> setDensity(float)
vector getVelocity() -> setVelocity(vector) 
int getFps()         -> setFps(int)

 

Regarding the getParameter() and setParameter() instructions, the concept is a little bit different, though. The parameters are exactly the ones you can find under "Node Params" and written as an argument – this means that it will be enclosed within the brackets. Just prepare an easy scene with an emitter and an internal object, and open the object’s "Particle Fluid Interaction" panel. There you can find many of the object’s properties and they can easily be translated into get and set statements, because you simply have to use the property’s name. 

An example:
You want to read out an item’s particle friction and increase it with each frame. The best idea is to first read out the initial value. Lets say you have added a vase here. The script you are using is a simulation events type and located under "FramesPost".

increment             = 0.001
vase                  = scene.getObject("Vase01")
particle_friction     = vase.getParameter("Particle friction")
new_particle_friction = particle_friction + increment
vase.setParameter("Particle friction", new_particle_friction)

scene.message(str(new_particle_friction))

 

As you can see it is not necessary to introduce any counters or loops for increasing the parameter. This is done automatically with each new frame. Another thing you will surely have noticed is a fundamental difference between the “get” and “set” statement:

  1. With get you always have to store the result in a variable (new_particle_friction).
  2. With set you do not need this construction, but you have to use another argument:
("Particle friction", new_particle_friction)

The first argument, “Particle friction”, is the affected parameter and “new_particle_friction” is the value the script should apply. The "Particle Fluid Interaction" window is updated with each frame and shows the new rounded value, the exact result is printed to the "Messages" window. 

Of course, the parameters are not restricted to the "Particle Fluid Interaction" panel – you can choose them from any section under "Node Params". The code from above is nothing more than an example. You can change almost any of RealFlow’s parameters with scripting. It is not only possible to write new values as numbers (integers, vectors or floats), you can also use strings or Boolean values. That is mostly necessary for parameters which are not numbers, for example the dynamics feature under "Node". Even this parameter can be influenced with scripting. In the following code segment, “Dynamics” is set from “No” to “Rigid body” at the beginning of the simulation ("SimulationPre"):

obj = scene.getObject("Vase01")
obj.setParameter("Dynamics", "Rigid body")

 

With Boolean values it is a bit different, because they do not need any quotation marks. You can toggle between true and false:

obj.setParameter("Visible", False)

 

A very important issue concerns vector parameters and compatibility with RealFlow 4. Whenever you have to deal with individual components of a vector, a certain notation is required. As already mentioned, a vector always conists of a trio of values:

  • Position = [ 5.0, 2.3, 1.6 ]

In case you want to set the values with an appropriate attribute, you have to use the following syntax: 

node.setParameter("Position.X", 5.0)
node.setParameter("Position.Y", 2.3)
node.setParameter("Position.Z", 1.6)

 

The interesting thing is the dot “.” between attribute and component. If you want to use older scripts (e.g. form version 4), this notation has to be adapted, otherwise you will receive an error message.