I understood that the problem of incorrect context is a pain, and overriding context is not always a good idea, because there are generally better solutions.
I need to import and render hundreds of meshes with different sizes, and I'm looking for a way to automatically place the camera. For now I just need the three axis view, and I found two useful functions: View -> Align View -> View Selected or bpy.ops.view3d.view_selected() View -> Cameras -> Set Active Oject as Camera or bpt.ops.view3d.object_as_camera()
This is what I thought: I place the 3d view upon one of the three axis, pretty far from the origin; then I call bpy.ops.view3d.view_selected() to have the mesh just insede the 3d view, and finally call bpt.ops.view3d.object_as_camera() to place the camera in that point (I could then slightly move it a bit further).
This is the best way I found to center the mesh inside the camera; if you come up with new ideas they're more than welcomed.
The problem is that I cannot override the active context :/
import bpy
original_type = bpy.context.area.type
bpy.context.area.type = "VIEW_3D"
bpy.ops.view3d.view_selected()
bpy.context.area.type = original_type
Is view3D really overrideable?
Thank you very much
Object.camera_fit_coordsIf you make your code into an operator, running it from the space bar in or from a button in the 3dview space will put it into context for you. – batFINGER Nov 15 '16 at 10:39preview_render_doline 289 in https://github.com/Moguri/blender/blob/master/release/scripts/modules/bl_previews_utils/bl_previews_render.py – batFINGER Nov 15 '16 at 10:58