Versions Compared

Key

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

...

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:
Code Block
themeEclipse
languagepython
linenumberstrue
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:

...

When you load scripts from other sources, for example forums, you have to be especially careful. In 3rd 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.

...