2
$\begingroup$

I'm currently working on addon for EEVEE. The code below adds new panel to Render Properties window.

import bpy

class MyEeveeAddon(bpy.types.PropertyGroup):

    use_myeevee_addon : bpy.props.BoolProperty(
        name="",
        description="Enable My Eevee Addon",
        default=False
    )
    mea_int_property : bpy.props.IntProperty(
        name="Quality",
        description="Increase My Eevee Addon quality",
        default=8,
        min=0,
        max=128
    )

class RENDER_PT_my_eevee_addon_panel(bpy.types.Panel):
    """Creates a Panel in the render properties window"""
    bl_label = "My Eevee Addon"
    bl_idname = "RENDER_PT_true_mb"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "render"
    bl_category = "My Eevee Addon"
    bl_options = {'DEFAULT_CLOSED'}

    def draw(self, context):
        layout = self.layout
        scene = context.scene

        col = layout.column(align=True)
        row = layout.row()
        col.prop(scene.me_addon, "mea_int_property")
        col.active = scene.me_addon.use_myeevee_addon

    def draw_header(self, context):
        layout = self.layout
        scene = context.scene

        col = layout.column(align=True)
        col.prop(scene.me_addon, "use_myeevee_addon")


bpy.utils.register_class(MyEeveeAddon)
bpy.utils.register_class(RENDER_PT_my_eevee_addon_panel)
bpy.types.Scene.me_addon = bpy.props.PointerProperty(type=MyEeveeAddon)

I can't figure out how to make it appear only for EEVEE render engine (like ambient occlusion tab for example). Tried to assign PointerProperty to bpy.types.SceneEEVEE.me_addon and change context in both drawing functions to context.scene.eevee but it doesn't work this way.

I guess, the right way would be to use correct bl_context in RENDER_PT_... class, something like "eevee_render" instead of just "render", but there's no documentation exist about what strings can be used for bl_context, and I could not pick up exact meaning with selection method and not skilled enough to find it in Blender source code.

Please, can anyone help with that?

$\endgroup$
3
$\begingroup$

Mix in class

Following on from your answer, can also create a mix in class. An example below.

The EeveeBasePanel mix in class will make any panel inheriting it be in properties, render context, default closed and poll when the render engine is eevee.

This comes in handy if you are going to register other panels. For example I have put a test panel in the scene context, that will also be in properties, default hidden and only poll if the render engine is eevee.

Seeing as it seems to be eevee based didn't worry about having compat engines list, or setting the category (tabbed by context rather than category in properties)

import bpy


class MyEeveeAddon(bpy.types.PropertyGroup):

    use_myeevee_addon: bpy.props.BoolProperty(
        name="",
        description="Enable My Eevee Addon",
        default=False
    )
    mea_int_property: bpy.props.IntProperty(
        name="Quality",
        description="Increase My Eevee Addon quality",
        default=8,
        min=0,
        max=128
    )


class EeveeBasePanel:
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "render"
    bl_options = {'DEFAULT_CLOSED'}

    @classmethod
    def poll(cls, context):
        return (context.engine == 'BLENDER_EEVEE')


class RENDER_PT_my_eevee_addon(EeveeBasePanel, bpy.types.Panel):
    """Creates a Panel in the render properties window"""
    bl_label = "My Eevee Addon"
    bl_idname = "RENDER_PT_true_mb"


    def draw(self, context):
        layout = self.layout
        scene = context.scene

        col = layout.column(align=True)
        col.prop(scene.me_addon, "mea_int_property")
        col.active = scene.me_addon.use_myeevee_addon

    def draw_header(self, context):
        layout = self.layout
        scene = context.scene

        col = layout.column(align=True)
        col.prop(scene.me_addon, "use_myeevee_addon")

class SCENE_PT_test(EeveeBasePanel, bpy.types.Panel):
    bl_label = "My Eevee Scene Test"
    bl_context = "scene"

    def draw(self, context):
        self.layout.label(text="Test")


classes = (MyEeveeAddon,
           RENDER_PT_my_eevee_addon,
           SCENE_PT_test)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.Scene.me_addon = bpy.props.PointerProperty(type=MyEeveeAddon)


def unregister():
    for cls in revrersed(classes):
        bpy.utils.register_class(cls)

if __name__ == "__main__":
    register()
$\endgroup$
1
$\begingroup$

In case anyone else needs it: I found the solution based on the code of the panel drawing class RENDER_PT_my_eevee_addon_panel. You have to add one more class variable among bl_idname = '', bl_label = '' and others:

COMPAT_ENGINES = {'BLENDER_EEVEE'}

And add a poll class method:

@classmethod
def poll(cls, context):
    return (context.engine in cls.COMPAT_ENGINES)

Now the panel will appear only for Eevee render engine. Improved code:

import bpy

class MyEeveeAddon(bpy.types.PropertyGroup):

    use_myeevee_addon : bpy.props.BoolProperty(
        name="",
        description="Enable My Eevee Addon",
        default=False
    )
    mea_int_property : bpy.props.IntProperty(
        name="Quality",
        description="Increase My Eevee Addon quality",
        default=8,
        min=0,
        max=128
    )

class RENDER_PT_my_eevee_addon_panel(bpy.types.Panel):
    """Creates a Panel in the render properties window"""
    bl_label = "My Eevee Addon"
    bl_idname = "RENDER_PT_true_mb"
    COMPAT_ENGINES = {'BLENDER_EEVEE'}
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "render"
    bl_category = "My Eevee Addon"
    bl_options = {'DEFAULT_CLOSED'}

    @classmethod
    def poll(cls, context):
        return (context.engine in cls.COMPAT_ENGINES)

    def draw(self, context):
        layout = self.layout
        scene = context.scene

        col = layout.column(align=True)
        row = layout.row()
        col.prop(scene.me_addon, "mea_int_property")
        col.active = scene.me_addon.use_myeevee_addon

    def draw_header(self, context):
        layout = self.layout
        scene = context.scene

        col = layout.column(align=True)
        col.prop(scene.me_addon, "use_myeevee_addon")


bpy.utils.register_class(MyEeveeAddon)
bpy.utils.register_class(RENDER_PT_my_eevee_addon_panel)
bpy.types.Scene.me_addon = bpy.props.PointerProperty(type=MyEeveeAddon)
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.