1
$\begingroup$

I'm working on a custom render engine through a Python addon that uses the bgl module for most of its rendering. bl_use_preview is enabled on the engine to let it create material previews, and bl_use_gpu_context is enabled so that it can use the OpenGL context in its render method. It works great for final renders, but not so well for material previews.

Particularly, I've found that bgl function calls create strange effects in the 3D view when previews are generated while in solid mode. This ranges from filling the screen with black to giving it a lighter tint to making parts of the model translucent, etc. There are also bugs in the previews themselves if they're generated in rapid succession, but I haven't tested this as much.

It seems like any bgl calls at all result in this behavior, including functions like glGetError, which should have no effect on rendering as far as I'm aware. The following self-contained code demonstrates this (if you comment out glGetError(), it'll do nothing, but otherwise, generating material previews through the panel will make the 3D view glitchy on the next draw):

import bpy
import bgl


def register():
    bpy.utils.register_class(CustomRenderEngine)
    bpy.types.MATERIAL_PT_preview.COMPAT_ENGINES.add(CustomRenderEngine.bl_idname)


def unregister():
    bpy.types.MATERIAL_PT_preview.COMPAT_ENGINES.remove(CustomRenderEngine.bl_idname)
    bpy.utils.unregister_class(CustomRenderEngine)


class CustomRenderEngine(bpy.types.RenderEngine):
    bl_idname = "CUSTOM"
    bl_label = "Custom"
    bl_use_preview = True
    bl_use_gpu_context = True

    def __init__(self):
        pass

    def __del__(self):
        pass

    def render(self, depsgraph: bpy.types.Depsgraph):
        bgl.glGetError() # i put this in render(), but it has the same effect in __init__, __del__, etc

    def view_update(self, context, depsgraph):
        pass

    def view_draw(self, context, depsgraph):
        pass


if __name__ == "__main__":
    register()

Are bl_use_preview and bl_use_gpu_context simply incompatible? I'm aware that bgl is deprecated (using it because it has some features that gpu still seems to lack), so I understand if that's also part of the issue. Is there anything else I can do here? Any help would be greatly appreciated.

$\endgroup$

0

You must log in to answer this question.

Browse other questions tagged .