Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.7 kB
12
Indexable
Never
import bpy,gpu,time

def draw():
    framebuffer = gpu.state.active_framebuffer_get()

    viewport_info = gpu.state.viewport_get()
    width = viewport_info[2]
    height = viewport_info[3]

    framebuffer_image.scale(width, height)

    pixelBuffer = framebuffer.read_color(0, 0, width, height, 4, 0, 'FLOAT')
    
    pixelBuffer.dimensions = width * height * 4
    framebuffer_image.pixels.foreach_set(pixelBuffer)
    framebuffer_image.filepath_raw = "/.../MyImage.png" #Enter your path here
    framebuffer_image.save()
    
    end = time.time()
    print(f"Frame Buffer took {end - begin} seconds")
    
    bpy.types.SpaceView3D.draw_handler_remove(draw_handler, 'WINDOW')

if __name__ == "__main__":

    ### Complete Render Testing ###
    begin = time.time()
    bpy.ops.render.render(write_still=True, animation=False)
    end = time.time()
    print(f"Complete Render took {end - begin} seconds")
   
    
    ### Viewport Render Testing ###
    begin = time.time()
    bpy.ops.render.opengl(animation=False, write_still=True, view_context=True)
    end = time.time()
    print(f"Viewport Render took {end - begin} seconds")
   
   
    ### Frame Buffer Test ###
    # note, in this test I'm only taking the time taken to generate image
    
    if "color_buffer_copy" in bpy.data.images:
        framebuffer_image = bpy.data.images["color_buffer_copy"]
    else:
        framebuffer_image = bpy.data.images.new("color_buffer_copy" , 32, 32, float_buffer=True,alpha=True)
    
    draw_handler=bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'PRE_VIEW')
    
    begin = time.time()
    bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)