0
$\begingroup$

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
$\endgroup$
6
  • $\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
    Commented Jul 25, 2019 at 11:56
  • $\begingroup$ @lemon upd: edited $\endgroup$
    – cxnt
    Commented Jul 25, 2019 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
    Commented Jul 25, 2019 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
    Commented Jul 25, 2019 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
    Commented Jul 25, 2019 at 13:00

1 Answer 1

0
$\begingroup$

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)

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .