5
$\begingroup$

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.

Thanks

$\endgroup$
  • $\begingroup$ @cegaton: Thanks for the reply. I want to do the boids behaviour through python scripting. $\endgroup$ – spod Aug 3 '17 at 16:38
2
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Thanks for the reply but I would like to do with the python $\endgroup$ – spod Aug 10 '17 at 17:33
  • $\begingroup$ Hm. Well, maybe you could try animating object locations or something...I have no clue how the actual boid calculations work, though. $\endgroup$ – SilverWolf Aug 11 '17 at 21:10
1
$\begingroup$

The basics are quite simple.

# 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):
    scene.frame_set(frame)

    for obj in boids_objects:
        # TODO: update obj location and rotation with your algorithm
        ...

        # persist the animation
        obj.keyframe_insert(data_path='location')
        obj.keyframe_insert(data_path='rotation_euler')

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.

$\endgroup$
1
$\begingroup$

With the help of this post's answers and this I managed to make this, it works in the BGE also:

YouTube video

.Blend file

Bake_Particles_to_keyframes.py

Insert_Action_Logic_Bricks_To_Selected_Objects.py

Imgur

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.