# Align camera to view precisely and automatically, without manual fiddling

Let's say I have a viewport view that I want to render in Cycles. Pressing ALT CTRL Numpad 0 aligns the camera to this view, but it does not solve the whole problem. The thing is, I want PRECISELY this view on the render, preferably in one click, without manually moving the camera back-and-forth, changing sensor sizes etc. fiddling that will remove the unwanted border that appears and blocks the viewport partially. This whole activity wastes a lot of precious time especially if you do this 10 times a day.

What this dream action should do:

• match the render resolution (proportions) to the resolution of the current view (can the resolution of current view be read by a Python script?)

• make the entire view the camera view, without any borders, passe-partout etc. by automatically maneuvering sensor sizes etc. whatever is necessary to make it exactly the same view, the same camera lens angle.

Is it possible to write such a script that would do it all in one click?

• Valid in context, though off topic. – Leander Apr 28 '18 at 18:42
• "match the render resolution (proportions) to the resolution of the current view" Viewport size is pretty arbitrary and may end up being the random cumulative result of monitor resolution, window size, screen layout, open toolbars etc. Not sure it would be worth trying to match such arbitrary size – Duarte Farrajota Ramos Apr 28 '18 at 18:59
• Are you aware of the camera fly mode that you can use to easily tweak the camera position and rotation? Too many things affect viewport framing in order to there being point in such script, but you can minimize the border by keeping the same lens and aspect ratio in both. – kheetor Apr 28 '18 at 20:13
• I'm aware of all these things, but the problem still exists. There is no one-click way to fully and automatically match the render to the current view. Screen-grabbing the viewport is not a solution either, as you can't multiply the resolution. The proportions can be arbitrary and that's the way it's supposed to be. – jubi Apr 29 '18 at 7:15

I created this script based on operator simple template.

import bpy

def main(context):
if context.area.type == 'VIEW_3D':

#set render resolution and lens data
region=False;
for reg in context.area.regions:
if(reg.type=='WINDOW'): region=reg;

if(region):
context.scene.render.resolution_x=region.width
context.scene.render.resolution_y=region.height
context.scene.camera.data.lens=context.space_data.lens

#set camera position
context.scene.camera.matrix_world=context.space_data.region_3d.view_matrix
context.scene.camera.matrix_world.invert()

#I don't know why sensor width must be 64, but it works:
context.scene.camera.data.sensor_width=64

#set view from camera
context.space_data.region_3d.view_perspective = 'CAMERA'

class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.set_render_size"
bl_label = "set render size"

def execute(self, context):
main(context)
return {'FINISHED'}

def register():
bpy.utils.register_class(SimpleOperator)

def unregister():
bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
register()


You can launch this script, then use set render size (you can set outer name if you want) operator.

• Why are you polling for active object, is this remnant of using the operator template? bl_idname and bl_label could be something more relevant to the actual operator task as well. – kheetor May 9 '18 at 14:41
• Yes, it is in template I will remove this. – Crantisz May 9 '18 at 14:54
• How about context.area.type == 'VIEW_3D' for your poll will also remove the need for the if in main – batFINGER May 9 '18 at 14:58
• Awesome, it works, but not exactly as it should. Please note, how the cubes I placed in the corners are not entirely fitting the camera image, they are cut in half: i.imgur.com/0FleKIN.gif Also it would be nice to make an addon out of it. PS. For a complete one-click solution the final view of the camera could be zoomed-in to fill the entire window, now the passe-partout is visible. – jubi May 10 '18 at 9:22
• @jubi hm, that is because opened side panels, i think. – Crantisz May 10 '18 at 10:15