# How do I write a function that changes current frame to 150 if current frame is 250

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:
bpy.ops.screen.animation_cancel(restore_frame=False)

bpy.app.handlers.frame_change_pre.append(stop_playback)

bpy.ops.screen.animation_play()

def change_frame():
if scene.frame_current == 250:
bpy.data.scenes["Scene"].frame_set(150)

change_frame()


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

• 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. – lemon Jul 25 '19 at 11:56
• @lemon upd: edited – cxnt Jul 25 '19 at 12:01
• 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). – lemon Jul 25 '19 at 12:07
• 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. – lemon Jul 25 '19 at 12:16
• 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):. – Hikariztw Jul 25 '19 at 13:00

Scene.frame_set(...)

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:
scene.frame_set(150)

bpy.app.handlers.frame_change_pre.append(skip_playback)


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)