2
$\begingroup$

I want to draw over the rendered image right after rendering it in Blender 2.80.

I tried creating a new render engine that calls the Cycles renderer and then writes over the rendered image (see the python code below). However, Blender crashes if the render engine calls self.begin_result() after executing the render with Cycles.

Is there a way run a renderer from within another render engine? Or some other way to edit the rendered image after rendering?

Here is my test code:

bl_info = {
    'name': "Test", 
    'category': "SVG Render",
    'blender': (2, 80, 0)
}

import bpy

class TestRenderEngine(bpy.types.RenderEngine):
    bl_idname = "test_renderer"
    bl_label = "Test Renderer"
    bl_use_preview = False

    def __init__(self):
        pass

    def __del__(self):
        pass

    def render(self, depsgraph):
        scene = depsgraph.scene
        scale = scene.render.resolution_percentage / 100.0
        self.size_x = int(scene.render.resolution_x * scale)
        self.size_y = int(scene.render.resolution_y * scale)

        bpy.context.scene.render.engine = 'CYCLES'
        bpy.ops.render.render()
        print("Rendered with Cycles")

        color = [0.0 , 0.0, 1.0, 1.0]
        pixel_count = self.size_x * self.size_y
        rect = [color] * pixel_count

        print("About to call self.begin_result")
        result = self.begin_result(0, 0, self.size_x, self.size_y) #Crash here
        print("Called self.begin_result")
        layer = result.layers[0].passes["Combined"]
        layer.rect = rect
        self.end_result(result)

def register():
    # Register the RenderEngine
    bpy.utils.register_class(TestRenderEngine)

def unregister():
    bpy.utils.unregister_class(TestRenderEngine)

# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
    register()

I stripped out the code where I attempt to read the rendered image to focus only on the part that crashes.

To test the code, run it from the scripting window, then select "Test Renderer" as the render engine and start a render. Launching Blender from a console window will allow you to see the debugging messages and the crash error message from Blender.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.