Can you keyframe Python scripts? Not create keyframes, but keyframe scripts.

I am just beginning to program python for Blender and before I start coding I want to know more about what I can do with keyframes. I know the standard list of things you can do when you insert a keyframe. I know that you can keyframe properties.

What I am trying to figure out is if it is possible to keyframe the creation of mesh or other objects. Or is it possible to keyframe the initiation of a python scripts. It seems like one would imply the other.

In more detail, I am wondering if it is possible to create an object in the course of a running animation, so that at the beginning of the animation, the object does not exist and at some frame of the animation, the object is created directly or as the consequence of a triggered script.

I have some experience with Flash, and of course there that’s pretty routine.

import bpy

def MyCreationFunction():
#this function is called per frame
#you can give it inputs like frame number from the my_handler function so that it triggers parts of this function depends on frame number

def my_handler(scene):

well something like this:

import bpy

def frame_change_callback(scene):
    if bpy.context.screen.is_animation_playing:
        if scene.frame_current < 40:
            for ob in scene.objects:
                    if ob['remove_me'] == 1337:
                except KeyError:
        elif scene.frame_current == 40:
            bpy.ops.mesh.primitive_monkey_add() # could create custom mesh here
            bpy.context.object['remove_me'] = 1337

But it doesn’t really make sense, you could just keyframe the hide / hide_render properties

OK, I see the thing I need here which is

and I am looking at the API reference here:

The reason I ask is that if I were to generate components in the animation that were only to show up at different points in the animation, it might be a huge load on my computer for purposes of both editing and rendering. Especially since the different components are basically the same. I could just create the components all at once with the script, but that’s a bigger file than if the components are not created until certain points in the animation. It might be the same render load, but its not the same editing load.

So I’ve got a script running that is monitoring the animation using When do I run this script? Where do I run it? Is it an addon?

Maybe I should do a basic “Hello World” addon first to see what is going on.

it is way cheaper to have a lot of meshes in object mode and put them on different layers / hide them rather than generating meshes while animation is playing. All it takes is a little memory, if you create meshes on frame changes, that will take memory and a lot of cpu power if you do complex calculations in the code and if there are any bpy.ops calls involved, then it will eventually become horribly slow. If it’s just for rending, use render_pre handler.

What do you mean, when?
You need to supply a function reference to the handler you add. That will be called on every frame change.

Also, if you create objects in a frame change you might not get predictable motion blur. Objects that are controlled by scripts that do not have f-curves can not be motion blurred. It is better to have the script generate the animation curves and keyframe the appearance of such objects in the scene over time (as CoDEmanX suggests). If the object has a f-curve, it will be considered for motion blur correctly.

OK, I think I see that I’ll probably be better off generating everything at once. I’ve got more than I started with, it’s time to start coding.

Thanks to all who posted.