# Creating dynamic animation in blender using python scripts

I am new to blender and python scripting in blender, so please forgive me if I am being vague because I have no idea where to start.

Currently I am working on a project whose output I wanted to show using animation in blender. The script would be running continuously and what ever result it generates the o/p will change accordingly. For example consider this python script,

def doCalculations(*args):
...

while(True):
x = doCalculations(...)
print(x)


Say this function generates numbers between 1 to 5. Now say I have two cubes in blender. Then if the generated number is 3 then I will project 3 spheres from cube1 to cube2. The function doCalculations would run indefinitely in background and the animation generated will change accordingly.

Is there a way to achieve this? Thanks in advance.

• Check out Animation Nodes (github.com/JacquesLucke/animation_nodes) which is a free add-on to blender that excels at this sort of procedural animation. Lots of good tutorials out on youtube for it. – zippy Mar 6 at 15:19

If your function is just an infinite loop, you won't be able to do anything in Blender while this script is executing. It doesn't work this way.

For this purpose you can create a new modal operator instead. If you want it to run in the background checking for keyboard input events, you can add a timer event and use it with modal() function in {'PASS_THROUGH'} mode like here.

For some particular tasks it may be easier to use bpy.app.handlers instead. They take your function as argument and execute it on some specific event, e.g. current frame change. This code will execute my_very_complex_function() printing 'Executing algorithm...' in console every time the frame is changed:

import bpy

def my_very_complex_function(context, dummy): # must take 2 arguments
print('Executing algorithm...')

bpy.app.handlers.frame_change_pre.append(my_very_complex_function)


This is just an example, you can put in way more complex functions