# How does 'Align Active Camera to Selected' decide where to place the camera?

Thanks to this answer, I'm using bpy.ops.view3d.camera_to_view_selected() to automatically place a camera so that it frames the selected object for rendering. But presumably there are lots of (x,y,z) locations within the scene where the camera could be placed and still frame the object.

Does anyone know how Blender decides which of these locations to move the camera to? For example, does it take the starting position of the camera into consideration?

• From playing with this operator, it appears that: It does not change the angle of the camera. For a given angle (view vector) there is only 1 solution no matter what the starting position of the camera is. The camera aligns with the object so that it is "in front" of it in the camera's local space. It will require either trial and error or the time to delve into the source code to give a mathematical / algorithmic answer. Commented Dec 13, 2016 at 16:31
• Yes, looks like you're right. Rotation is unaffected by bpy.ops.view3d.camera_to_view_selected(), so there's only one possible location for the camera. Thanks! Commented Dec 13, 2016 at 18:19

The bpy.ops.view3d.camera_to_view_selected() (View > Align View > Align Active Camera to Selected) operator allows you to position the camera to view the selected objects, and uses a variety of factors to determine the final location of the camera including the position and size of the selected objects and the current view settings of the 3D viewport.

It uses the following logic to calculate the final position of the camera:

• It calculates the bounding box of the selected objects in the 3D space, which gives the minimum and maximum coordinates of the objects in each dimension.

• It calculates the center point of the bounding box by taking the average of the minimum and maximum coordinates in each dimension.

• It calculates the distance between the center point and the farthest corner of the bounding box.

• It uses this distance and the current view settings, such as the field of view of the camera, to determine the optimal position with the current orientation of the camera to view the selected objects.

• It sets the camera's location to the calculated position, which is only 1 possible point in space since the rotation is not affected by the operator.