When I click on the "new" button, some panels that were disabled appear. I read their code, but I didn't understand. How do I get certain panels to appear when I click a button?
-
$\begingroup$ Are you trying to make an addon that lets you add your own custom panels to the materials bar? I'm confused. $\endgroup$– Nascent SpaceCommented Feb 17, 2021 at 2:45
-
$\begingroup$ Yes, but the location is "VIEW 3D" .. I didn't mention that. $\endgroup$– niadhsonCommented Feb 17, 2021 at 16:14
1 Answer
It's a panels poll method
The panels are defined and sitting there polling away, waiting to "show".
The poll classmethod of a panel returns true only when the required context (or otherwise related) conditions are met
Hide Panel With Python ("A Duality Duplicate")
In example displayed in question :
(Speculating haven't checked (...lately), Turn on Developer Extras in settings, so can right click on 99% of UI, view source and see how its done... a common practice is to)
Have one master materials panel, that always polls. In this case the material slots, no panel header, has the option to add a material.
All the others seen are , subpanels ie Panels with their parent set to the master panel, or others in the space, that have poll requirements met.
In the properties space, there is a sort of "polling" option available by setting bl_context
of the panel class to match that of the properties space context, matching the 'OBJECT', 'SCENE', 'RENDER' tabs.
Creating new tabs in properties panel
How do I add a panel to Object Properties?
These subpanels only poll when there is a context.material
(In properties space there is a material context member, most likely context.material == context.object.active_material
When all the panels require the same poll it is common to see a mixin base class
class SomePanelBase:
bl_context = 'MATERIAL'
@classmethod
def poll(cls, context):
return context.material is not None
And for any defined panel
class MaterialFooPanel(SomePanelBase, bpy.types.Panel):
...
making any such defined panels only display if there is an active material.
-
$\begingroup$ Great! This seems to work as I want ... but, I forgot to mention that the location of my addon is "VIEW_3D" .. Here
bl_context
does not work. Would I have to do anything withbl_category
? $\endgroup$– niadhsonCommented Feb 17, 2021 at 18:16 -
$\begingroup$ NP.
bl_context
is for properties area, leave out otherwise.bl_category
is the tab label in other areas. $\endgroup$ Commented Feb 17, 2021 at 23:43