Custom Functions

Custom functions are a very convenient way to enhance the readability of your code and introduce independent code sections for executing certain parts of a script. You may remember the predefined functions from scripted daemons:

def setExternalForce( emitter ):

 

Such a definition of functions is not limited to RealFlow’s internal structures. You can also create your own functions and call them. A function can be seen as closed code segment that will be executed on demand. It consists of four elements:

  • The “def” statement. This tells Python that there is an executable code segment.
  • The functions name, e.g. “force_calculation” or “node_creation” etc.
  • The argument “()”. The argument can consist of one or more variables handed over from other functions, but in many cases it will be empty.
  • The function’s Python code.

 

If you do not want to hand over argument variables then you can also make use of global variables. Without these methods, variables will only work on a local level and will not be stored when jumping to another code section. A “def” statement also acts like a jump label. It is possible, for example, to link the execution of different functions to a GUI window’s input. Just create a list you can choose from and jump to the appropriate function based on the user’s choice.

options = ["Emitter","Camera","RealWave"]
window  = GUIFormDialog.new()
 
window.addListField("Add to scene", options, 0)
your_choice = window.getFieldValue("Add to scene")

if (your_choice == 0): createEmitter()
if (your_choice == 1): createCamera()
if (your_choice == 2): createRealWave()

def createEmitter():
    do something here...

def createCamera():
    do something here...

def createRealWave():
    do something here...

 

Another important field of application is to outsource repeating parts of a program. Just imagine a script where you have to open a file for reading and writing at different positions of your script. Instead of adding the code for opening the file again and again, it is much more efficient to pack everything into a function and call it on demand. With a return() statement at the end of the function you can directly jump back to the last position. Similar to a function's argument, the return statement's brackets can also contain variables you want to hand over to the calling section of the script, but you can also introduce global variables instead.

do something here...

# call the function
openFile()

 go on doing with doing something...

def openFile():
    do something new here...
    return()

 

One of the most often used custom functions can be seen with batch scripts. It is used to execute a batch script properly, especially when it is called from the "Scripts Bar". The main function can be seen as a self-reference to run the batch script and it is aways a good idea to enclose batch scripts within this function, even if it is the only one.

def main():
    execute your code here

if __name__ == "RealFlow":
    main()