How to stop animation at certain frame?

I tried this:

bpy.ops.screen.animation_play()


if bpy.context.scene.frame_current ==30:
    #bpy.context.scene.update()
    bpy.ops.screen.animation_cancel()

But it didnt work. Any solutions?

well from the code side of things i not to sure, from the UI however you simply


You will need to do the checking in a frame change handler or such, otherwise it will only cancel if on frame 30 when you run the script.

@npm1
I’m actually trying to do this using code.
@batFINGER
How would I come about making it work with framechange handlers?

can someone help me with this?

I am not good at scripting at all, but I think I got this to work.

import bpy
bpy.ops.screen.animation_play()

def my_handler(scene):
    
    paus=100
    
    print("Frame Change", scene.frame_current)
    if bpy.context.scene.frame_current ==paus-1:
        
        bpy.ops.screen.animation_cancel()
        bpy.context.scene.frame_current = paus

#remove all handlers
for i in range( len( bpy.app.handlers.frame_change_pre ) ):
        bpy.app.handlers.frame_change_pre.pop()

#append handler
bpy.app.handlers.frame_change_pre.append(my_handler)

I also made a version that paus at markers in the timeline.

import bpy
bpy.ops.screen.animation_play()

def my_handler(scene):

    
    print("Frame Change", scene.frame_current)
    frame_current = bpy.context.scene.frame_current
    markers = bpy.context.scene.timeline_markers

    for m in markers:
        if m.frame == frame_current:
        
            bpy.ops.screen.animation_cancel()
            bpy.context.scene.frame_current = m.frame

#remove all handlers
for i in range( len( bpy.app.handlers.frame_change_pre ) ):
        bpy.app.handlers.frame_change_pre.pop()

#append handler
bpy.app.handlers.frame_change_pre.append(my_handler)

@colorina
Works great! can this be done for an object instead of scene? Everytime I playback the script stays active and stops on the frame.

Great!
Maybe you could put something more in the if statement… like the active object has to be an object named Cube for example. There are probably much nicer ways of doing this :slight_smile:

import bpy
bpy.ops.screen.animation_play()

def my_handler(scene):

    
    print("Frame Change", scene.frame_current)
    frame_current = bpy.context.scene.frame_current
    markers = bpy.context.scene.timeline_markers

    for m in markers:
        if m.frame == frame_current and bpy.context.active_object == bpy.data.objects['Cube']:
        
            bpy.ops.screen.animation_cancel()
            bpy.context.scene.frame_current = m.frame

#remove all handlers
for i in range( len( bpy.app.handlers.frame_change_pre ) ):
        bpy.app.handlers.frame_change_pre.pop()

#append handler
bpy.app.handlers.frame_change_pre.append(my_handler)