2

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?

cak3_lover
  • 477
  • 3
  • 11
  • Your question is unclear. What do you want to change and what needs to remain unchanged specifically? Do you want to expand or crop the image around your object and the object to remain the same size in pixels in your render? Why? What are you trying to achieve? Context might help to understand. – Martynas Žiemys Sep 26 '22 at 09:08
  • @MartynasŽiemys I'm trying to get the image from the viewport buffer and for that I want the resolution to stay the same, but resizing the viewport changes the zoom level and hence the resolution – cak3_lover Sep 26 '22 at 09:15
  • Getting image from the viewport buffer is not usually any standard workflow. What are you trying to do? You sure this is not XY problem kind of situation? – Martynas Žiemys Sep 26 '22 at 09:36
  • @cakelover ... if such thing would be accessible, check User Preferences ... but I didnt find an option to handle your issue. So Martynas is right - share with as your end-goal ... if you just want to let GUI behave like that, I don't think it is possible now. – vklidu Sep 26 '22 at 11:24
  • @MartynasŽiemys I'm trying to generate 2D images of my 3D model but rendering takes too much time as compared to viewport buffer, the only problem is that you need to set thee viewport itself in a fixed manner (zoom level, area size, etc etc) so that the images being sent are of the same size and resolution always and that's why I was asking this question & you're right, it might not be a standard method but it is a really powerful tool to just let go to waste – cak3_lover Sep 27 '22 at 08:27
  • @vklidu check the comments I added my end goal – cak3_lover Sep 27 '22 at 08:27

1 Answers1

0

I found the solution here

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')

cak3_lover
  • 477
  • 3
  • 11
  • I don't believe you can save images from GPU frame buffer faster with Python than with Blender's internal rendering. Have you tested it? Have you compared the speeds? How? As far as I understand you are just wasting your time with this. Or am I missing something? – Martynas Žiemys Sep 29 '22 at 19:50
  • @MartynasŽiemys I have tested it & it's significantly faster (at least for the task I need), I've made a little script for comparing the speeds, now combine that with the real time compositor branch & you got something going! but I'll be honest it's like taming a really wild horse but when you do boy does it run! But by all means if I'm wrong about this let me know – cak3_lover Sep 29 '22 at 20:15
  • Who could have thought... But I get different images. I also suspect there is something wrong with png format. When I try EXR, it's faster to save an image that is higher resolution with render.opengl() - that might be of interest to you. – Martynas Žiemys Sep 29 '22 at 21:15
  • @MartynasŽiemys well my requirements are for png so I'm trying to tame that & as for resolution, I believe that can be changed by zooming in (hence this question) and then saving the image from frame buffer (0.0304 seconds), render.opengl() is relatively much slow (1.134 seconds for the same test) – cak3_lover Sep 30 '22 at 06:47