2

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

Frank
  • 21
  • 1
  • You can see how to override the view operator's context in this answer: http://blender.stackexchange.com/questions/30643/how-to-toggle-to-camera-view-via-python – TLousky Nov 15 '16 at 10:39
  • Related http://blender.stackexchange.com/questions/51563/how-to-automatically-fit-the-camera-to-objects-in-the-view?rq=1 If it's the ortho view you are after have a look at Object.camera_fit_coords If 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:39
  • Further to above look at preview_render_do line 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
  • @batFINGER thank you, it was what I needed :) – Frank Nov 15 '16 at 12:10

0 Answers0