Custom Attributes
Another exciting and very useful feature is the definition of particular attributes, created by the user. There are many cases where it is necessary to add an attribute that is not part of RealFlow’s Python interface by default. To make use of this function, RealFlow offers a couple of new instructions, exclusively valid for emitters and particles:
- Emitters:
createParticlesAttribute, destroyParticlesAttribute, queryParticlesAtrribute
- Particles:
setAttribute, getAttribute, queryAttribute
The first step is to create a particle attribute and this action requires two arguments. The first one is a unique “name” in form of an integer, the other one’s the expected data type.
Allowed types are:
P
ARTICLE_ATTR_TYPE_DOUBLE, PARTICLE_ATTR_TYPE_INT, PARTICLE_ATTR_TYPE_BOOL and PARTICLE_ATTR_TYPE_VECTOR
So, first of all it is necessary to create an attribute for an emitter which will then be applied to a particle:
# Write this part to SimulationPre emitter = scene.get_PB_Emitter("Circle01") emitter.createParticlesAttribute(100, PARTICLE_ATTR_TYPE_DOUBLE) emitter.createParticlesAttribute(101, PARTICLE_ATTR_TYPE_INT) emitter.createParticlesAttribute(102, PARTICLE_ATTR_TYPE_BOOL) emitter.createParticlesAttribute(103, PARTICLE_ATTR_TYPE_VECTOR) # Write this part to FramePre emitter = scene.get_PB_Emitter("Circle01") particleList = emitter.getParticles() for particle in particleList: particle.setAttribute(100, 3.14159) particle.setAttribute(101, 5) particle.setAttribute(102, True) particle.setAttribute(103, Vector.new(1.0,0.0,1.0))
During simulation you will not recognize any effect, but it is possible to call these attributes and use them for further calculations or conditions. The advantage is that you can either leave the attribute’s value untouched or assign a new one with each frame or simulation step – that is up to you.
To request an attribute, simply use the following construction:
for particle in particleList: pi = particle.getAttribute(100) scene.message(str(pi))
You can also remove an emitter’s ability to carry attributes with a destructor. Please note that this is not possible for single particles, but for certain attributes:
frame = scene.getCurrentFrame() if (frame == 50): emitter.destroyParticlesAttribute(102)
Finally, there is the query instruction. With these commands it is possible to check whether a attribute is set or not, and the result is a Boolean value: “True” or “False”. To start a query for an emitter, this syntax is used:
emitter = scene.get_PB_Emitter("Circle01") flag = emitter.queryParticlesAttribute(102) if (flag): scene.message("This attribute exists!")
For a requesting a particle’s attribute, everthing has to be inside a loop again. In this example it is a while-loop:
emitter = scene.get_PB_Emitter("Circle01") particle = emitter.getFirstParticle() while (particle): if (particle.queryAttribute(100)): scene.message("This attribute exists!") particle = particle.getNextParticle()