Changing Boids During Animation Crashes Blender

Hello,
I have Python code that runs on each frame during animation, and changes the target which some boid particles are following. It’s fine during development (with Alt+P), but when I render, Blender crashes.

Here’s a simplified form of the code:


def change_boid_target(context):
    if (condition):
         # Get the boids' go-to-goal rule
         particle_system = bpy.data.objects['Emitter'].particle_systems[0];
         goal_rule = particle_system.settings.boids.states['State'].rules['Goal'];

         # Update the goal
         goal_rule.object = bpy.data.objects['Waypoint3'];

# Set change_boid_target() to run on each frame
bpy.app.handlers.frame_change_post.append(change_boid_target);

The crash happens on the line labelled “Update the goal”. Is there a workaround?

MANY thanks,
Peter.

You may not be able to do that just yet with the current API. Meaning without crashing while rendering. But what you could do is instead of re-assigning the goal object simply assign the existing goal object’s location to the location of Waypoint3.


goal_rule.object.location = bpy.data.objects["Waypoint3"].location

Thanks, that solved it!