Python Variables

A variable can be seen as container to store a value. You can fill this container with content, for example numbers or strings. Other variable have the ability to store more than one value and it is even possible to mix the different types: strings with numbers, vectors with strings and so on. The content or value of a variable can change during the execution of the script and old values can be overwritten or updated with new elements. Variables can be declared wherever they are needed and their number is not limited.

Global and Local Variables

Modern programming languages differentiate between two fundamental types of variables: global and local.  The difference lies in the way the variables are stored within your script. Local variables, as the name indicates, only actlocally inside a function. A function always starts with a def statement and can be considered as a closed code segment. With simulation events scripts, these functions are not directly visible, because you cannot see any predefined functions in the editor, as with scripted emitters or daemons. The editors for emitters, waves and daemons all show an initializing function, e.g.:

  • def setInternalForce( emitter )
  • def updateWave( vertices, initPositions ) 
  • def setExternalForce( emitter )
  • def setExternalForce( body )

Directly below this statement your code begins. Whenever you are writing a variable to such a section it will be treated as local. This means that it cannot be transferred to other functions. The variable is only stored and used within the actual function. You do not have to mark or tag a variable as local – Python automatically treats a variable as local, independent of it is type. So all the variables from the previous examples and code snippets are local variables. With global variables it is different. They can be used over the entire script, multiple functions and even different script types, such as daemons. 

Since they have to be stored permanently, global variables also allocate more memory. A local variable is used and forgotten, once the function has been executed and the memory freed up. To distinguish global variables from local ones, they have to be introduced with a special form:

scene.setGlobalVariableValue(string, any)

 

The string is substituted for the variables' name and has to be written within quotation marks. The second argument is the variable’s value, which can be any type, e.g. an integer or a vector. When there is a set command then there has to be a get instruction, too. As always with get statements, the value has to be stored with a variable. So a complete constructor for global variables could look like this:

velocity_vector = Vector.new(1.0, 1.0, 1.0)
scene.setGlobalVariableValue("velocity", velocity_vector)
new_velocity = scene.getGlobalVariableValue("velocity")
new_velocity_scale = new_velocity.module()
scene.message(str(new_velocity_scale))

 

Another way to use velocity_vector with a global variable is to write it directly to the definition without introducing a local variable first:

scene.setGlobalVariableValue("velocity", Vector.new(1.0, 1.0, 1.0))

 

Of course, lists are also allowed:

scene.setGlobalVariableValue("nodes", ["Sphere01","Circle01","Camera01"])

 

With longer lists it is better to define them separately: 

node_list = scene.getSelectedNodes()
scene.setGlobalVariableValue("selected_nodes", node_list)

 

The definition and assigning of global might appear a little bit cumbersome, but it is an effective method. The only thing is that you have to do is to keep track of the global values, because it is not allowed to define a local or another global variable with the same name somewhere else. You have already heard that variables must always be unique, except if you want to overwrite the previously assigned value(s). Another important issue is the circumstance in which the value of a global variable turns local, once it is called with scene.getGlobalVariableValue(). In this case the global value is stored as a new variable. Here, new_velocity is local, while velocity is still global and can be used elsewhere:

new_velocity = scene.getGlobalVariableValue("velocity")

 

It is also possible to perform calculations with global variables and store them again globally. In this example, the age value is overwritten with a new value and the result is stored back to global variable again:

scene.setGlobalVariableValue("age", 24)
age = scene.getGlobalVariableValue("age")
age = age + 1
scene.setGlobalVariableValue("age", age)

 

Global variables are a very effective way to share values with other parts of a script and make use of them without needing to assign the variables again and again. With very large lists stored in global variables we recommend that you check whether a global definition is really needed, to reduce the amount of allocated memory. In Python tutorials you will also often find a certain notation:

global rigidbody_mass

 

This way of introducing global variables is also valid, but it is better to stay with RealFlow’s internal method, because it is tailored to its special requirements.

Â