• I have an animation that loops and has two phases: "cube_object" goes to point x, spins - repeat).
  • Now everytime the "spin" animation is finished (or the "cube_object" reaches a certain location...?), I'd like to spawn an object on a random position with Python.
  • If there are 10 objects spawned stop the animation and change camera angle to another scene.

Imagine a worker in a game that chops wood and every time he finishes chopping a new log spawns. After 10 logs - something new happens.


1 Answer 1


Try this example that will spawn a random cube everytime the frame == 10, it will stop after 5 cubes:

import bpy

# this is for the random position, imports the random() function from random library
from random import random

# this holds how many cubes were created and what the cap is
spawned_cubes = 0
max_cubes = 5

def spawn_object(scene):
    """This function will be run everytime before frame changes because it is added in the callback list"""

    global spawned_cubes
    global max_cubes

    # read the current frame number
    # if it is 10 and number of spawned cubes is less than max_cubes place a cube
    # if the frame number is not 10 just end and do nothing
    if scene.frame_current == 10 and spawned_cubes < max_cubes:
        loc = (5-10*random(), 5-10*random(), 5-10*random())
        bpy.ops.mesh.primitive_cube_add(enter_editmode=False, location=loc)
        spawned_cubes += 1

# add spawn_object function if there isnt one in bpy.app.handlers.frame_change_pre callback list
# the function needs to be added there only once
# everything thats in that list will be executed before any frame change
if spawn_object not in bpy.app.handlers.frame_change_pre:

This uses the app handlers callback list. In your spawn_object function you can test any property - for example the location of your cube_object, or some frame value etc.

  • $\begingroup$ thank you, this was very useful! If you have time, could you explain what "from random import random" at the beginning does? And if I get the solution right, your code counts the frames and every X frame calls the function, right? The code spawns 3 cubes now btw... :) And to be honest, I don't get this part at all: # add handler if not in app.handlers if spawn_object not in bpy.app.handlers.frame_change_pre: bpy.app.handlers.frame_change_pre.append(spawn_object) However the script works without it. And how would you stop that loop? After x frames or after x spawns? Thanks! $\endgroup$
    – Sputi
    Dec 16, 2014 at 15:43
  • $\begingroup$ no, the function is not called every X frames, it is called every frame, but only on frame 10 it will do something. You can add counter how many cubes it created for stopping the generation - Ill add that into the example $\endgroup$ Dec 16, 2014 at 15:57
  • $\begingroup$ The from random import random tells python to import the random() function from the random library. More info can be found here. docs.python.org/2/library/random.html#random.random $\endgroup$
    – Vader
    Dec 16, 2014 at 15:58

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.