I've a script that starts render animation from frame 1 to 250, I want a script that changes the current frame to 150 when it reaches 250

I'm trying to write a function that changes current frame to 150 if current frame is 250

here's my code:

def stop_playback(scene):
    if scene.frame_current == 250:



    def change_frame():
        if scene.frame_current == 250:


here's my console output:

>>> def change_frame():
...     if scene.frame_current == 250:
...         bpy.data.scenes["Scene"].frame_set(150)
>>> change_frame()
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
  File "<blender_console>", line 2, in change_frame
NameError: name 'scene' is not defined
  • $\begingroup$ Should be a Python only question. Python need indentations (tabs) inside a block. def is a block (add a tab under it) If is a block (add a second tab level under it). But see Python docs, it's better. $\endgroup$ – lemon Jul 25 '19 at 11:56
  • $\begingroup$ @lemon upd: edited $\endgroup$ – cxnt Jul 25 '19 at 12:01
  • $\begingroup$ See blender.stackexchange.com/questions/145980/…. Your last edit: 'change_frame' is called inside 'change_frame' and with no parameters (it is called inside due to the indentation). $\endgroup$ – lemon Jul 25 '19 at 12:07
  • $\begingroup$ Also have a look here blender.stackexchange.com/questions/143273/… at the "Scene handler" part. The idea it is done is if you dont remove before adding you will append several times the same handler. $\endgroup$ – lemon Jul 25 '19 at 12:16
  • $\begingroup$ There shouldn't be any indentations in front your def change_frame():. And also, scene is not a references at that function. The scene in other function block should not be called here. You might want to pass that value as a parameter inside the parentheses def change_frame(scene_at_this_function):. $\endgroup$ – HikariTW Jul 25 '19 at 13:00


All handlers take a single argument, scene. In some handlers, load for example, the scene argument has value None in which case is often referred to as "dummy"

import bpy

def skip_playback(scene):
    if scene.frame_current >= 250:


The scene in the handler is the scene being "handled".. Using a data reference like bpy.data.scenes["Scene"] is a slippery slope, when there is no scene named "Scene" or there is another scene's handler will set its frame.

Similar functionality could be achieved by setting the scene frame start and end, or preview start and end.

On your error, it is a basic syntax error in that you have not defined scene in the change_frame method.

I would recommend, when you are new (or not) to python, when you get an error you don't understand make a search for "Python NameError" (based on the error type NameError in this case)


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.