Zoom to selection in Python gives RuntimeError I'm new to Blender Python and writing a start-up script that imports mesh from another application to Blender for rendering.

I have a small problem however - imported mesh objects are too large for default Blender view-port, so I wanted to somehow make the script correct this issue.

I tried something with bpy.ops.view3d.view_all() but I can't get it work. I read the execution-context docs, but I always get this error:

RuntimeError: Operator bpy.ops.view3d.view_all.poll() expected a view3d region

Any suggestions?
Operators need a certain context to run from, so you can override this so it can be run from the text editor or wherever depending on the area you need it executed from:

import bpy

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        for region in area.regions:
            if region.type == 'WINDOW':
                override = {'area': area, 'region': region, 'edit_object': bpy.context.edit_object}
                bpy.ops.view3d.view_all(override)


Read more in the docs and see this related question for more info.