Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
2.0 kB
3
Indexable
Never
import bpy
import gpu,threading

def draw(self):
    if self.modal_redraw == True:
        self.framebuffer = gpu.state.active_framebuffer_get()

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

        self.framebuffer_image.scale(self.width, self.height)
        
        self.pixelBuffer = self.framebuffer.read_color(0, 0, self.width, self.height, 4, 0, 'FLOAT')
        
        self.pixelBuffer.dimensions = self.width * self.height * 4
        self.framebuffer_image.pixels.foreach_set(self.pixelBuffer)

        self.modal_redraw = False

class ModalFramebufferCopy(bpy.types.Operator):
    bl_idname = "view3d.modal_framebuffer_copy"
    bl_label = "Draw 3D View Framebuffer"

    def __init__(self):
        self.modal_redraw = False
        self.framebuffer = None
 
        if not "buffer_img" in bpy.data.images:
            self.framebuffer_image = bpy.data.images.new("buffer_img", 32, 32, float_buffer=True)
        else:
            self.framebuffer_image = bpy.data.images["buffer_img"]

    def modal(self, context, event):
        if event.type in {'ESC'}:
            bpy.types.SpaceView3D.draw_handler_remove(self._handle_3d, 'WINDOW')
            return {'CANCELLED'}
        else:
            self.modal_redraw = True

        return {'PASS_THROUGH'}
    
    def invoke(self, context, event):
        self._handle_3d = bpy.types.SpaceView3D.draw_handler_add(draw, (self,), 'WINDOW', 'PRE_VIEW')

        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}
    
    
def register():
    bpy.utils.register_class(ModalFramebufferCopy) # the main Error seems to be occuring here

def invoke_draw():
    bpy.ops.view3d.modal_framebuffer_copy('INVOKE_DEFAULT')

if __name__ == "__main__":
    register()

    server_thread=threading.Thread(target=invoke_draw) # the Error seems to start from here
    server_thread.start()