Viewport size: width and height

I wonder if there is a way to get the actual viewport size (width and height) in pixels. This will be very interesting to get a proportion number.

Sometimes when you orb in User Perspective mode for modeling or shading, you get a very convincing view of the scene as you view it in the viewport. Then you can create a cemera from the view but you never get the exact view, because the created camera has diferent width and height proportions than the viewport.

Getting the appropiate pixels from viewport will give a proportion to apply to the new created camera and will give the exact view as the viewport. Am I correct?

Then an addon should be created to enable this proportion when creating the new camera.

1 Like
import bpy

for a in bpy.context.screen.areas:
    if a.type == 'VIEW_3D':
        for r in a.regions:
            if r.type == 'WINDOW':
                print(f"Viewport dimensions: {r.width}x{r.height}, approximate aspect rato: {round(r.width/r.height, 2)}")
2 Likes

Wow! Thank you so much. I have absolutely no idea about Python. :frowning:

For now I have been able to execute your code and it seems to work. It gives me what I think is viewport width and height and the proportion.

Now I have to be able to create the camera and apply that resolution and get the exact view from the viewport in the camera. Perhaps is not as I was thinking, but Im experimenting.

Again thank you so much for the code, @testure :slight_smile:

This addon might already do what you want:

2 Likes

Thanks so much for pointing this addon. Exactly what I was lloking for.

Till now, I was using code provided by @testure in the python console and changing the camera to 24 or 25 mm. That was very accurate but the addon is a full complete solution.

EDIT: a combination of both the code and the addon gives the best results. The code is needed for the output resolution to fit in the camera.

1 Like