Accessing actual Viewport Render

Hello everyone!

I’m struggling with a weird problem … normally when you’d want to save a render of the viewport to the disk via python you would do something like this:

bpy.ops.render.opengl(write_still = True)

but I want to save an image of the Weight Paint view, which does not show up in the OpenGL renderer…

When I render, it looks like I switched to Object Mode, but I need the Weight Paint mode.

Is there a way to save the Weight Paint viewport as images to the disk? I read something about accessing internal image buffers but that seems to require modifications of the Blender Source Code, which goes way above my head.

Hopefully someone can help me. Have a great day everyone!

Cheers!

You shouldn’t need to touch the source for this.
You can capture the viewport as a texture with the GPU module, then read the pixels of that texture with bgl.

What Blender version are you using, because

bpy.ops.render.opengl(write_still = True)

works fine here.

Also:
View → Viewport Render Animation

works fine on Blender 2.93 Windows.

I am also using 2.93 Windows, and it the OpenGL render works normally as in: it renders the character with the selected viewport material. BUT: I want to render the Weight Paint view. Because I want to save the weights in the form of a 2D projection from the perspective of the camera.
So if I do View > Viewport Render Image, it looks perfect. I get an image with the blue and red values of the Weight Paint mode.
In contrast: bpy.ops.render.opengl(write_still = True) is not giving me the same output.

Interesting. It is giving me the correct weight painted output.

Thanks for the suggestion! I’ve never worked with that module, is it possible to set the output resolution to 256x256? because I’d need that as a requirement.

Really? Could you pls send me an image of how it looks on your end? And thank you so much for your help!

Maybe you have a very small second 3d editor window open and it is using that one.

Okay I can reproduce your view with a blank project but not with my current one … will have to look into that. Thanks anyway!!

Maybe open the file with “Load UI” turned off so you can rule out that it is some UI problem.

I found my issue. I had two windows open, one for coding on one monitor and the other one for the viewport and outliner. This apparently does not work. As soon as I ran the code from a single window with the viewport in the same window, everything worked perfectly.
Guess this is closed then. Thank you for your help and pointing me into the right direction!