I'm using blender to render avatar and avatar accessories on a website, however there is a problem, when a hat, or accessory is made a bit large, the it doesn't fit in the viewport, is there anyway to make the viewport expand or move backwards to fit the entire object automatically with python?
-
I usually get a camera, press 0 on the numpad to view from it, press N and then under View i select "Lock camera to View" i then select my object(s) i wish to zoom in on and press the Dot/period key. – Luka ash Apr 28 '16 at 01:40
2 Answers
There is an operator to do exactly this. it can be called from Python or accessed from a menu.
- Select the objects you wish to put in the camera view.
- View -> Align View -> Align Active Camera to Selected.
Or run the operator: bpy.ops.view3d.camera_to_view_selected()
eg:
import bpy
from bpy import context
Select objects that will be rendered
for obj in context.scene.objects:
obj.select_set(False)
for obj in context.visible_objects:
if not (obj.hide_get() or obj.hide_render):
obj.select_set(True)
bpy.ops.view3d.camera_to_view_selected()
There is also an object method to place a camera given input coordinates: Object.camera_fit_coords()
If you want a margin around the objects, you could temporarily reduce the cameras view angle, then restore it afterwards, or simply move the camera back along its Z axis.
- 15,860
- 10
- 83
- 143
- 47,387
- 10
- 141
- 223
-
1Hey, I've tried your scripts, and the camera_to_view_selected function just returns "{'CANCELLED'}". Any idea why? – Noam Peled Jul 11 '16 at 19:09
I am not at all a programmer, but have you looked into the View All operator?
bpy.ops.view3d.view_all()
or perhaps the View Selected if the one above doesn't work well for you
bpy.ops.view3d.view_selected()
You can find documentation here https://www.blender.org/api/current/bpy.ops.view3d.html
- 15,860
- 10
- 83
- 143
- 59,425
- 39
- 130
- 187
