Hello World
Programming novices often feel like they do not know where to start. Basically there are two ways to get started. Firstly, you can take pre-existing RealFlow scripts and deconstruct them. With this method it is possible to learn many things about the mode of operation of certain functions and how they work in more or less complex environments. The other method is to start with the fundamentals of a programming language and learn the basic rules. With this method it is much easier to write your own scripts very fast, because you have a deep understanding of the correlations between the various elements of a script.
You will probably already be familiar with the famous “Hello World” script from other languages. This program is often used to give the new programmer a feeling of success. With Python for RealFlow this approach is not really suitable, because it only deals with the output of some text and does not affect any of the special features of fluids or objects. However, it is still worth using the good old “Hello World” tradition anyway, so let’s start by applying this script:
- Open a batch script window with F10 or from the "Layout" menu
- Enter the following lines of code:
text = "Hello World!" scene.message(text)
- Execute the script with Script > Run
- Open the message window and look for the output
The result most probably looks very similar to this image:
Please note that the scene.message()
statement only works inside RealFlow. If you are programming with the standard Python interpreter, you have to use the print()
command.
As you might have noticed, it was not necessary to start a simulation and you did not need any objects. The reason is pretty obvious - you did not manipulate any nodes, but just created a text output and sent it to the message window. Though this example is very simple, it already shows important concepts of a programming language – the usage of variables. It also gives you an important insight: with batch scripts it is not necessary to start a simulation.
Before you start writing your own programs it is recommended to adjust RealFlow’s scripting preferences. You can change preferences here:
Preferences > Script
The most common source of errors with Python script is incorrect tabbing. The Python standard makes it clear: the "Tabstop" size must be always 8, but such a big tab size is virtually impossible to use, so you will most probably end up with "Tabstop" sizes of 2 or 4. The “Expand Tabs” under "Preferences" will let you forget about this mess as long as you do not care about tabs being replaced by blank spaces. Since blanks and indents are an important feature of Python, it makes a great difference whether you are writing your code like this:
emitter = scene.get_PB_Emitter("Circle01") particle = emitter.getFirstParticle() while (particle): mass = particle.getMass() density = particle.getDensity() scene.message("Mass: "+str(mass)+" :: "+"Density: "+str(density)) particle = particle.getNextParticle()
or this way:
emitter = scene.get_PB_Emitter("Circle01") particle = emitter.getFirstParticle() while (particle): mass = particle.getMass() density = particle.getDensity() scene.message("Mass: "+str(mass)+" :: "+"Density: "+str(density) ) particle = particle.getNextParticle()
The second code snippet will not work at all, because the leading indents are not correct. It is also important which type of blank or tab you are using, and you are not allowed to mix spaces and tabs. You should always use the tab key and never the space bar! Only with tabs can you be certain that you are always using the same method and number of blanks.
When you load scripts from other sources, for example forums, you have to be especially careful. In 3rd party scripts some of the tabs are often converted to blanks. This has to be fixed before the program is executed. Another issue that can be come up is an empty line with a tab or a few blanks at the end of scripts. This line must be removed, because it always leads to syntax errors. Following this rule is an easy method to avoid and eliminate many of the common problems.