How to keep the same zoom level?

How do I keep the same zoom level such that the image resolution is always the same?
i.e. when I change the viewport size the zoom level should stay the same

for example if this is my view:
enter image description here

when I change the viewport size everything in the view shrinks:
enter image description here

what I need is for it to stay the same size, more like changing the size of the viewport would be like cropping:
enter image description here

so how do I achieve this?

Exit camera view

nope, still the same effect

I don’t know how useful this is, but it was a fun exercise.

import bpy


def scale_view(context, data, flip=False):
    rd = context.region_data
    wmat = rd.window_matrix

    asp_prev = data["aspect"]
    asp_curr = data["aspect"] = wmat[0][0] / wmat[1][1]

    if flip:
        asp_prev = 1 / asp_prev
        asp_curr = 1 / asp_curr

    if asp_curr < 1.0:
        mul = asp_prev / asp_curr
        rd.view_distance *=  mul


def on_redraw(context, cache={}):
    region = context.region
    rw = region.width
    rh = region.height
    try:
        data = cache[region]
    except:
        w = context.region_data.window_matrix
        cache[region] = {"aspect": (w[0][0] / w[1][1]), "rw": rw, "rh": rh}

    else:
        if rw != data["rw"]:
            scale_view(context, data)
            data["rw"] = rw

        elif rh != data["rh"]:
            scale_view(context, data, flip=True)
            data["rh"] = rh

if __name__ == "__main__":
    bpy.types.SpaceView3D.draw_handler_add(
        on_redraw, (bpy.context,), 'WINDOW', 'POST_PIXEL')

2 Likes