1
$\begingroup$

I'm trying to find out how to add a checkbox to a panel header so panel can be deacivated (as well as storing that checkbox value in some property). It may be quite obious task, but I didn't find any info about it in the docs so far.

Example of what I'm trying to achive is the "Gravity" panel, that can be turned off with the checkbox.

Example of what i'm trying to achive

Any suggestions or leads? I would be grateful for any code examples with it.

$\endgroup$
1
$\begingroup$

Add draw_header(context) function to your Panel class and supply any bool property. Following example is based on ui_panel_simple.py 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_header(self,context):
        self.layout.prop(context.object, "hide_viewport", text="", toggle=False)

    def draw(self, context):
        layout = self.layout
        layout.label(text="Hello world!", icon='WORLD_DATA')


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


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


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

Adding to already registered panels

As an extra to other answers re defining a draw header on a new panel class, can monkey around with existing panel classes.

Here is an example that adds gravity checkbox and "XXX" to audio panel header, and disables that panels layout with scene.use_gravity (silly)

enter image description here

import bpy
from bl_ui.properties_scene import SCENE_PT_audio

dr = SCENE_PT_audio.draw

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

def draw_header(self, context):
    scene = context.scene
    self.layout.prop(scene, "use_gravity", text="XXX")

if SCENE_PT_audio.is_registered:    
    bpy.utils.unregister_class(SCENE_PT_audio) 
SCENE_PT_audio.draw = draw   
SCENE_PT_audio.draw_header = draw_header
bpy.utils.register_class(SCENE_PT_audio)
$\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.