0
$\begingroup$

I'm trying to setup a scene in Blender 2.8 and I'm having problems "fitting" the timeline to the first/last frame.

There's no problem to set the frame-range of the scene via python, but the actual timeline still shows frames 1-250 even if my start/end is 50-100.

In the timeline it seems like you can press View/View All to fit the timeline to only show the scenes actual frame-range. But how do I do this with python? In the tooltip I see a command, but if I execute that command it tells me the context is wrong. So do I change the context to the timeline somehow, or is there another way?

scene = bpy.context.scene
scene.frame_start = 50
scene.frame_end = 100

# Does not work
bpy.ops.action.view_all() 
$\endgroup$
0

1 Answer 1

1
$\begingroup$

You have to set a custom context, because the operator requires a specific context in which it can be executed. Otherwise the check implemented in its .poll() function will return false and execution fails.

You can accomplish this by iterating through the areas to find the timeline (which is now called DOPESHEET_EDITOR instead of TIMELINE in Blender 2.8). Then you also need the correct region with the type WINDOW. Once you have both you can create a custom context.

This script requires that you have the timeline open as an area somewhere in the UI.

import bpy

scene = bpy.context.scene
scene.frame_start = 50
scene.frame_end = 100

for area in bpy.context.screen.areas:
    if area.type == 'DOPESHEET_EDITOR':
        for region in area.regions:
            if region.type == 'WINDOW':
                ctx = bpy.context.copy()
                ctx['area'] = area
                ctx['region'] = region
                bpy.ops.action.view_all(ctx)
                break
        break
$\endgroup$
2
  • $\begingroup$ Thanks, works well. Should I also reset the old context after execution? $\endgroup$ Sep 25, 2019 at 8:09
  • $\begingroup$ @SimonBjörk The code creates a copy of the current context and only uses the modified one for the operator. There shouldn't be the need to reset any context. $\endgroup$
    – Robert Gützkow
    Sep 25, 2019 at 9:59

You must log in to answer this question.

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