Execute script at certain frame

Hi.

Just starting with Python so I’m not sure how to get this: I want an object to do an action defined by a script when the timeline reach certain frame. In instance: A ball should follow a path defined by a function at frame 17.

Thanks

Checking like so every frame,

ran = False

def checkFrame(scene):
    global ran; currFrame = scene.frame_current_final    
    if (18 > currFrame >= 17) and not ran: doSomething()

def doSomething():
    ran = True
    #your code goes here

ran is there in case you want to run the function just once. Else you can take it out.

To run the check on every frame, you could append checkFrame to bpy.app.handlers.frame_change_pre or bpy.app.handlers.frame_change_post.
I haven’t done much of this in Blender outside of game loops, but the API makes it look simple enough: https://docs.blender.org/api/blender_python_api_2_74_0/bpy.app.handlers.html

Thanks! I’ll give a try!!

i wrote it exactly as you did and its not working please help

ran = False

def checkFrame(scene):
global ran; currFrame = scene.frame_current_final
if (18 > currFrame >= 17) and not ran: doSomething()

def doSomething():
ran = True
bpy.ops.object.origin_set(type=‘ORIGIN_CURSOR’, center=‘MEDIAN’)

hello, apart from the subject

use the icone number six when you embed code in your messages. :slight_smile:
or ctrl E

import bpy

ran = False

def checkFrame(scene):
    global ran; currFrame = scene.frame_current_final
    if (18 > currFrame >= 17) and not ran: doSomething()

def doSomething():
    ran = True
    bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')

doSomething()

But anyway you have to execute your function by the end of your code.
typing doSomething() (with args inside if your fonction have some)