Skip to content

Use HTML Templates & Retained UI ​

While draw(self, ui, context) is the fastest way to build dynamic panels, blendcss also supports two retained-mode approaches: external or inline HTML/CSS templates and Python build(self, ui) + on_sync(self, context).

Not sure which approach fits your panel? Read Choosing an Authoring Mode.

Author with raw HTML & CSS files ​

Place .html and .css files next to your panel's Python file and reference them via html_file and css_file.

html
<!-- panel.html -->
<div class="card">
    <div class="header">
        <span id="obj_name" class="title">No Selection</span>
        <span id="obj_type" class="badge">NONE</span>
    </div>

    <div class="row">
        <span>Scale X</span>
        <div id="scale_x_box" class="num-box">
            <span id="scale_x_val">1.00</span>
        </div>
    </div>

    <button id="dup_btn" class="primary-btn">Duplicate</button>
    <button id="reload_btn" class="ghost-btn">Reload UI</button>
</div>

Connect events with @blendcss.on(elem_id, event), set up bindings in on_mount(self, doc), and update dynamic elements in on_sync(self, context) using live handles (self.doc["elem_id"] or self.doc.el("elem_id")):

python
import bpy
from . import blendcss


class TemplatePanel(blendcss.Panel):
    addon_id = "my_addon"
    category = "My Addon"
    label = "HTML Template"
    html_file = "panel.html"
    css_file = "panel.css"

    def on_mount(self, doc):
        doc.bind_number(
            "scale_x_val",
            lambda: bpy.context.active_object,
            "scale",
            index=0,
            hit_id="scale_x_box",
            step=0.02,
        )
        doc.bind_operator("dup_btn", "object.duplicate", linked=False)

    def on_sync(self, context):
        obj = context.active_object
        self.doc["obj_name"].text = obj.name if obj else "No Selection"
        self.doc["obj_type"].text = obj.type if obj else "NONE"

    @blendcss.on("reload_btn", "click")
    def on_reload(self):
        self.reload()

Hot-reload external files while designing ​

Calling self.reload() re-reads html_file and css_file from disk, re-runs on_mount(self, doc), re-attaches all @blendcss.on handlers, and syncs all bindings. You can edit your HTML/CSS in an external editor and click your reload button without restarting Blender.

Inline HTML strings

For small templates, set html = "..." and css = "..." (or override build_html(self) and build_css(self)) instead of creating separate files.

Author with retained-mode build(self, ui) ​

If you prefer writing UI in Python without rebuilding the tree on every Blender redraw, implement build(self, ui) instead of draw(self, ui, context).

build(self, ui) runs once on mount (and on reload()). Save node handles on self and update them in on_sync(self, context):

python
import bpy
from . import blendcss


class RetainedPanel(blendcss.Panel):
    addon_id = "my_addon"
    category = "My Addon"
    label = "Retained UI"

    def build(self, ui):
        with ui.card():
            header = ui.row(justify="space-between")
            self.title = header.label("No Selection", cls="bl-card-title")
            self.badge = header.badge("NONE")

            self.controls = ui.col()
            with self.controls:
                ui.number(
                    "Scale X",
                    bind=(lambda: bpy.context.active_object, "scale", 0),
                    step=0.02,
                )
                ui.operator("object.duplicate", "Duplicate", variant="primary", linked=False)

    def on_sync(self, context):
        obj = context.active_object
        self.title.text = obj.name if obj else "No Selection"
        self.badge.text = obj.type if obj else "NONE"
        self.controls.visible = obj is not None

Manipulate live node handles ​

Every UINode returned by UIBuilder (and every ElementHandle returned by doc["id"]) supports live updates without reloading the document:

python
self.title.text = "Cube"
self.title.add_class("is-highlighted")
self.title.remove_class("is-muted")
self.title.style(color="#ff8800", opacity="0.9")
self.controls.visible = False   # Hides element and remembers its prior display mode
self.controls.show()            # Restores its previous display mode

See the UICssDocument Reference and UIBuilder Reference for the complete list of handle properties and methods.

Released under the GNU GPL v3.0 License.