Skip to content

Instantly share code, notes, and snippets.

@a-nakanosora
Last active September 16, 2023 23:36
Show Gist options
  • Save a-nakanosora/3f8306aabe9c04f831a5087703a63fdf to your computer and use it in GitHub Desktop.
Save a-nakanosora/3f8306aabe9c04f831a5087703a63fdf to your computer and use it in GitHub Desktop.
Blender Script - Save viewport buffer as an image
''' blscript_save_viewport_buffer_as_image.py
'''
import bpy
import bgl
def main():
def get_region_view3d():
'''
context = bpy.context
assert context.space_data.type == 'VIEW_3D'
region = context.region
return region
'''
#for win in bpy.context.window_manager.windows:
for win in [bpy.context.window]:
for area3d in [area for area in win.screen.areas if area.type == 'VIEW_3D']:
for region in [region for region in area3d.regions if region.type == 'WINDOW']:
return region
region = get_region_view3d()
assert region is not None
x = region.x
y = region.y
width = region.width
height = region.height
outputimgname = 'Viewport Render Result'
if outputimgname in bpy.data.images:
bpy.data.images.remove(bpy.data.images[outputimgname])
out = bpy.data.images.new(outputimgname, width, height)
buffer = bgl.Buffer(bgl.GL_FLOAT, width * height * 4)
bgl.glReadPixels(x, y, width, height , bgl.GL_RGBA, bgl.GL_FLOAT, buffer)
out.pixels = buffer[:]
def set_imageeditor_image():
win = bpy.context.window
for area_imeditor in [area for area in win.screen.areas if area.type == 'IMAGE_EDITOR']:
for s in [s for s in area_imeditor.spaces if s.type=='IMAGE_EDITOR']:
s.image = out
return
set_imageeditor_image()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment