I am very new to the blender python scripting. I am planning to do a
flock (boids) behaviour using python script in blender. With a little research, I find some animation script examples for bge but not for blender python. Can anyone please help me
to start with this. Any links to the tutorials will be appreciated.
Blender actually has a built-in boids implementation. If you don't need to use Python for some reason, you can do this with Particle Systems.
Go to the default cube (or whatever object you want the flock to start from), go to the Particles tab (second from the right in the Properties panel), and click the New button.
Scroll down to the Physics section, and click Boids. There are a lot of options to tweak here.
Now you have a basic flock, but they'll just drift around aimlessly at this point. (You can watch them by pressing Alt+A.) To make them attracted to an object, select the object, click on the Physics tab (all the way to the right), and then click Force Field.
Set it to Force to make them attracted or repelled to the the object.
A negative strength will attract them, while a positive strength will repel them.
In the particle Physics section on the emitter, you can make them interact with other boid flocks in your scene.
# scene should contain the current scene
# boids_objects should be a list of blender objects you want
# to perform your algorithm on
for frame in range(scene.frame_start, scene.frame_end):
for obj in boids_objects:
# TODO: update obj location and rotation with your algorithm
# persist the animation
It gets more advanced when you want to do your calculation based on the previous location, speed and rotation. For these you need to manage this data in your own script.