1
$\begingroup$

I'm trying to get my camera to fit the entire viewport (home button) in fullscreen mode
and this is what I've come up with so far:

import bpy

context = bpy.context.copy()
for area in bpy.context.window.screen.areas:
    if area.type == 'VIEW_3D':
        
        context['area'] = area
        bpy.ops.screen.screen_full_area(context, use_hide_panels=True)
        
        area.spaces[0].region_3d.view_perspective = 'CAMERA'
        bpy.ops.view3d.view_center_camera({'area': area})

but it keeps giving the error

RuntimeError: Operator bpy.ops.view3d.view_center_camera.poll() failed, context is incorrect

How do I fix this?

$\endgroup$

1 Answer 1

1
$\begingroup$

I guess I forgot the proper region, this should do the trick:

import bpy

context = bpy.context.copy()

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        
        #sets into camera view
        area.spaces[0].region_3d.view_perspective = 'CAMERA'
        
        #sets to fullscreen
        context['area'] = area
        bpy.ops.screen.screen_full_area(context, use_hide_panels=True)
        
        #sets camera to fit viewoprt
        bpy.ops.view3d.view_center_camera({
            'area': bpy.context.screen.areas[0],
            'region': bpy.context.screen.areas[0].regions[0],
        })
        break

reference answer (please upvote their answer!)

$\endgroup$

You must log in to answer this question.

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