4
$\begingroup$

lets say i have a panel and i want to collapse a part if a bool is False

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
        # where should i define this bool ? 
        layout.prop(place, "bool")
        if bool is True:
            obj = context.object

            row = layout.row()
            row.label(text="Hello world!", icon='WORLD_DATA')

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



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


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


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

1 Answer 1

7
$\begingroup$

Blender 2.8 has sub panels.

As of blender 2.8 can if bl_parent_id is set in a panel it will be appended to the parent panel. Use the usual 'DEFAULT_CLOSED' in the bl_options set to start with panel closed.

This avoids needing to set up some boolean property to open close UI altogether.

Here is a sample file I've been testing for ordering panels. First panel is defined as a child of the object transform panel. Second panel is a child of first. Can unregister and re-register with different parent ids to sort sub-panels, which is what I was testing

enter image description here The two demo sub panels appended to object transform

import bpy
from bl_ui.properties_object import ObjectButtonsPanel, OBJECT_PT_transform

class LayoutDemoPanel(bpy.types.Panel, ObjectButtonsPanel):
    bl_label = "Layout Demo"
    bl_idname = "SCENE_PT_layout"
    bl_parent_id = 'OBJECT_PT_transform'
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'

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

        scene = context.scene

        # Create a simple row.
        layout.label(text=" Simple Row:")

        row = layout.row()
        row.prop(scene, "frame_start")

class LayoutDemoPanel2(bpy.types.Panel, ObjectButtonsPanel):
    bl_label = "Layout Demo 2"
    bl_idname = "SCENE_PT_layout2"
    bl_parent_id = 'SCENE_PT_layout'
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'

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

        scene = context.scene

        # Create a simple row.
        layout.label(text=" Simple Row:")

        row = layout.row()
        row.prop(scene, "frame_start")


def register():
    bpy.utils.register_class(LayoutDemoPanel)
    bpy.utils.register_class(LayoutDemoPanel2)

def unregister():
    bpy.utils.unregister_class(LayoutDemoPanel2)
    bpy.utils.unregister_class(LayoutDemoPanel1)


if __name__ == "__main__":
    register()

If you have some logical setting that when set needs more UI How to dynamically show/hide panel elements using python?

$\endgroup$
1
  • $\begingroup$ Cheers. Can still do this the ol' fashioned way too, added link. $\endgroup$
    – batFINGER
    Commented Mar 26, 2019 at 16:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .