Skip to content

Build an Interactive Tool Panel ​

In this tutorial, you will build a multi-tab tool panel with local state, segmented tabs, a progress bar, a live GPU preview image generated with NumPy, and a one-click reload button.

This walkthrough follows the same patterns used in the repository's built-in demo/demo_panel.py.

Prerequisites ​

Step 1: Set up panel state and styling ​

Store UI-only state (such as the active tab or shader preview parameters) in a Python dictionary on self.state. blendcss bindings work with plain Python dicts just like Blender objects.

python
import numpy as np
from . import blendcss

STUDIO_CSS = """
.studio-stack {
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.hero-card {
    border-top: 3px var(--bl-accent);
    border-radius: 12px;
    padding: 14px;
}
.hero-title {
    font-size: 16px;
    font-weight: 700;
    color: var(--bl-text-title);
}
.muted-copy {
    font-size: 11px;
    color: var(--bl-text-muted);
}
.preview-img {
    width: 100%;
    height: 76px;
    border-radius: 8px;
    border: 1px var(--bl-border-subtle);
}
"""

GRADIENT_PRESETS = (
    ("Studio Amber", (240, 138, 36), (52, 74, 128)),
    ("Cobalt Dusk", (71, 114, 179), (28, 34, 48)),
    ("Emerald Wire", (58, 167, 109), (24, 42, 40)),
)


class StudioToolPanel(blendcss.Panel):
    addon_id = "studio_tool"
    category = "Studio Tool"
    label = "Studio Hub"
    css = STUDIO_CSS

    def __init__(self):
        super().__init__()
        self.state = {
            "active_tab": "transform",
            "preset_index": 0,
            "roughness": 0.25,
            "metallic": 0.90,
            "raytracing": True,
        }

Step 2: Upload a live GPU image on mount ​

Use the on_mount(self, doc) lifecycle hook to upload an initial in-memory texture before the panel is drawn. doc.register_image(name, array) uploads a NumPy HxWx4 RGBA array directly to the GPU under the blendcss:// scheme — no temporary files on disk.

Add on_mount, cycle_gradient, and _upload_gradient to StudioToolPanel:

python
    def on_mount(self, doc):
        self._upload_gradient()

    def cycle_gradient(self):
        self.state["preset_index"] = (self.state["preset_index"] + 1) % len(GRADIENT_PRESETS)
        self._upload_gradient()

    def _upload_gradient(self):
        if not self.doc:
            return
        _name, c0, c1 = GRADIENT_PRESETS[self.state["preset_index"]]
        w, h = 160, 64
        t = np.linspace(0.0, 1.0, w, dtype=np.float32)[None, :]
        arr = np.zeros((h, w, 4), dtype=np.uint8)
        for ch in range(3):
            arr[:, :, ch] = (c0[ch] * (1.0 - t) + c1[ch] * t).astype(np.uint8)
        arr[:, :, 3] = 255
        self.doc.register_image("studio_gradient", arr)

Step 3: Build the tabbed draw(self, ui, context) layout ​

Use ui.tabs(...) bound to (self.state, "active_tab"). When the user clicks a tab, blendcss updates self.state["active_tab"], detects that the layout structure changed on the next redraw, and renders the newly selected branch cleanly.

Add draw(self, ui, context) to StudioToolPanel:

python
    def draw(self, ui, context):
        obj = context.active_object if context else None
        tab = self.state["active_tab"]
        scene_count = len(context.scene.objects) if (context and context.scene) else 0

        with ui.col(cls="studio-stack"):
            # Header card
            with ui.card(cls="hero-card"):
                header = ui.row(justify="space-between", align="center")
                header.label(obj.name if obj else "No Selection", cls="hero-title")
                header.badge(obj.type if obj else "NONE", variant="accent")

                with ui.row(gap=6):
                    ui.operator("object.duplicate", "Duplicate", icon="layers", variant="primary", linked=False)
                    ui.operator("view3d.view_all", "Frame", icon="eye")

            # Segmented tabs
            ui.tabs(
                {
                    "transform": "Transform",
                    "surface": "Shader",
                    "preview": "Preview",
                },
                bind=(self.state, "active_tab"),
            )

            # Conditional tab content
            if tab == "transform":
                with ui.card(title="Object Scale"):
                    if obj:
                        for axis, idx in (("X", 0), ("Y", 1), ("Z", 2)):
                            ui.number(
                                f"Scale {axis}",
                                bind=(obj, "scale", idx),
                                min_val=0.01,
                                max_val=20.0,
                                step=0.02,
                                precision=3,
                            )
                        ui.toggle("Wireframe", bind=(obj, "show_wire"))
                    else:
                        ui.label("Select an object to adjust scale.", cls="muted-copy")

            elif tab == "surface":
                with ui.card(title="Material Parameters"):
                    ui.number("Roughness", bind=(self.state, "roughness"), min_val=0.0, max_val=1.0, step=0.01)
                    ui.number("Metallic", bind=(self.state, "metallic"), min_val=0.0, max_val=1.0, step=0.01)
                    ui.toggle("Raytracing", subtitle="Local addon state", bind=(self.state, "raytracing"))
                    ui.progress(self.state["roughness"], max_val=1.0)

            elif tab == "preview":
                preset_name = GRADIENT_PRESETS[self.state["preset_index"]][0]
                with ui.card(title="Live GPU Image"):
                    ui.img("blendcss://studio_gradient", cls="preview-img")
                    bar = ui.row(justify="space-between", align="center")
                    bar.label(preset_name, cls="muted-copy")
                    bar.button("Cycle", icon="refresh", on_click=self.cycle_gradient)

            # Footer
            footer = ui.row(justify="space-between", align="center")
            footer.label(f"Scene: {scene_count} objects", cls="muted-copy")
            footer.button("", icon="refresh", on_click=self.reload)

Notice two container styles working side by side:

  • Context managers (with ui.card():) for multi-line nested blocks.
  • Method chaining (header = ui.row(); header.label(...); header.badge(...)) for compact horizontal bars.

Step 4: Register and test ​

Register the panel in your addon's register() / unregister() functions:

python
def register():
    blendcss.register(StudioToolPanel)


def unregister():
    blendcss.unregister(StudioToolPanel)

Reload your addon in Blender and try:

  1. Clicking between Transform, Shader, and Preview.
  2. Dragging Roughness in the Shader tab and watching the progress bar update live.
  3. Clicking Cycle in the Preview tab to regenerate the NumPy texture on the GPU.
  4. Switching Blender between Dark and Light themes to see all colors update automatically.

Next Steps ​

Released under the GNU GPL v3.0 License.