Creating panels that are only available for specific object

Hi

I was going through some examples on how to create panels, menus, popups etc. in Blender 2.8.

However, either I’ve missed the fact, but to me at first glance it only seems possible to create panels that exist always in some location (like N panel tab), and not only when some object is selected?

For example, if I were to create some rigging related addon, I would only like have the tab appear in properties window or N panel tab when a certain rig object is selected.

Is this possible?

I would say that it’s possible but you would have to register and unregister the panel, based on some select event, maybe it’s “too much” for what you want to do.
A much simpler trick would be to add a condition in the draw function.
Here is an example with the default ui_panel_simple template :

import bpy


class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

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

        obj = context.object
        if obj.name=="Cube":
            row = layout.row()
            row.label(text="Hello world!", icon='WORLD_DATA')

            row = layout.row()
            row.label(text="Active object is: " + obj.name)
            row = layout.row()
            row.prop(obj, "name")

            row = layout.row()
            row.operator("mesh.primitive_cube_add")


def register():
    bpy.utils.register_class(HelloWorldPanel)


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


if __name__ == "__main__":
    register()

I only added the line 16 : if obj.name=="Cube":
It means when context object is not the “Cube”, it won’t draw anything.
As you wan see here :
panel_simple

See you :slight_smile: ++
Tricotou

1 Like

@tricotou

Thank you for your reply!

I think I could live with that header still there.

Based on what I’ve read, I think it would also be possible to store object’s name to some custom property / PropertyGroup (not exactly sure of all of these yet) instead of hard coded string comparison… ?

Thanks!

You don’t need to… If you want the whole panel to hide, instead of doing a condition check in the draw method, it’s better to use the poll function (which serves exactly to check if the context is valid for the panel to be called).

@classmethod
def poll(cls, context):
    return context.object.name == "Cube"

This will bypass the whole panel when context.object.name is different than ‘Cube’.

1 Like