0
$\begingroup$

I have a draw_handler_add like this:

import bpy,gpu

def draw():
    global to_draw, begin
    if to_draw:
        
        print("drawing")
        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"
        framebuffer_image.save()
        to_draw=False

if __name__ == "__main__":

    global to_draw,b
    to_draw=False
    
    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)

    bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'PRE_VIEW')
        
    bpy.data.objects['Cube'].rotation_euler[0] = 1.5
    
    bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
        
    to_draw=True

The problem is that the draw() is being invoked after the script it completed
but I want it to be invoked while the script itself is running, so how do I achieve this?

(just for context my ultimate aim is to setup a server)

$\endgroup$

1 Answer 1

0
$\begingroup$

Found the solution thanks to iceythe (you can follow the thread there to see the details)

import bpy,gpu,time

def draw():
    global to_draw
    if to_draw:
        print("drawing...")
        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"
        framebuffer_image.save()
        to_draw=False

def update_draw():
    global to_draw
    for area in bpy.context.screen.areas: 
        if area.type == 'VIEW_3D':
            area.tag_redraw()
    bpy.ops.wm.redraw_timer(type='DRAW_SWAP', iterations=1)
    
    to_draw=True
    
    bpy.ops.wm.redraw_timer(type='DRAW_SWAP', iterations=1)

if __name__ == "__main__":
    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')
    
    
    bpy.data.objects['Cube'].rotation_euler[0] = 1.5
    update_draw()
    
    time.sleep(10)
    
    bpy.data.objects['Cube'].rotation_euler[1] = 3
    update_draw()
    
    bpy.types.SpaceView3D.draw_handler_remove(draw_handler, 'WINDOW')
    print("we're done")

Up vote his answer if you find it helpful too!

Just incase anyone want to know, I am using the 3.3.0 Alpha version of blender & using the compositor branch which you can download here (Blender 3.3.0 - temp-viewport-compositor-merge)

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .