Blender's UI and Runtime: Much Faster Than GPUI
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Blender’s UI and Runtime: Much Faster Than GPUI
Blender’s user interface, as a library you can use from Python, Rust, JavaScript (React, Svelte), C, C++ and Dear ImGui.
Blender 的用户界面现在作为一个库,可供 Python、Rust、JavaScript (React, Svelte)、C、C++ 和 Dear ImGui 使用。
People spent decades building and optimizing Blender’s GUI. It’s battery and memory efficient, and super duper fast. We decoupled it, to give everyone a chance to build their own products with it. It supports multiple languages and frameworks, so you won’t miss a beat!
人们花费了数十年时间构建和优化 Blender 的图形用户界面。它在电池和内存使用上非常高效,且速度极快。我们将它解耦,让每个人都有机会用它构建自己的产品。它支持多种语言和框架,确保你不会错过任何节奏!
Website and full documentation: bui.preetham.top · Chat: Discord
网站及完整文档:bui.preetham.top · 聊天:Discord
This README is a small book. Read it front to back for the whole story, or jump to the chapter you need. Every chapter links to a longer guide in docs/ or on the website when there’s more to say.
这份 README 就像一本小书。你可以从头到尾阅读以了解完整内容,也可以直接跳转到你需要的章节。当有更多内容需要说明时,每一章都会链接到 docs/ 或网站上的详细指南。
Part II: Getting started
第二部分:入门指南
Part IV: Guides by language
第四部分:各语言指南
Part VII: The project
第七部分:项目介绍
BUI takes the user interface of Blender (its widget drawing, its interaction rules and its look) and turns it into a standalone UI library with a small C core. Your code builds the widget tree and handles callbacks. Layout, input, drawing and presenting all happen in fast native code, on the CPU or the GPU.
BUI 提取了 Blender 的用户界面(其组件绘制、交互规则和外观),并将其转化为一个带有小型 C 核心的独立 UI 库。你的代码负责构建组件树并处理回调。布局、输入、绘制和呈现均在 CPU 或 GPU 上通过快速的本地代码完成。
At the heart of BUI is the kit: a small runtime written in C. The kit holds every widget in one tree, lays them out, handles the mouse and keyboard, and draws them the way Blender does. The drawing code is a C port of Blender’s own widget code, and the interaction follows Blender’s rules. On top of the kit sit bindings: thin layers that let Python, Rust, JavaScript, C++ and Dear ImGui talk to it.
BUI 的核心是 kit:一个用 C 语言编写的小型运行时。kit 将所有组件保存在一棵树中,负责布局、处理鼠标和键盘输入,并以 Blender 的方式绘制它们。绘制代码是 Blender 原生组件代码的 C 语言移植版,交互逻辑也遵循 Blender 的规则。在 kit 之上是绑定层:这些轻量级层允许 Python、Rust、JavaScript、C++ 和 Dear ImGui 与其进行交互。
Blender’s interface is famous for how it feels. You press a number field and drag: the cursor vanishes, the value glides under your hand, and you never hit the edge of the screen. You type 2*pi into a field and it just works. You type “ad cu” into a menu and it finds “Add Cube”. Millions of artists have these habits in their hands.
Blender 的界面以其操作手感而闻名。当你按住数字输入框并拖动时,光标会消失,数值会随着你的手势平滑变化,且永远不会触碰到屏幕边缘。你在输入框中键入 2*pi,它就能直接计算。你在菜单中输入 “ad cu”,它就能找到 “Add Cube”。数以百万计的艺术家已经养成了这些肌肉记忆。
That interface is also fast and frugal. It redraws only what changed, sleeps when nothing does, and starts in milliseconds. BUI exists so that any app, in any language, can have both: Blender’s feel, and Blender’s efficiency.
该界面不仅快速而且节省资源。它只重绘发生变化的部分,在无操作时进入休眠,并能在毫秒级时间内启动。BUI 的存在就是为了让任何语言编写的任何应用程序都能同时拥有 Blender 的手感和效率。
Before you ship something with BUI, read the known limitations: no accessibility support yet, no input methods, and it looks like Blender, not like your operating system.
在使用 BUI 发布产品之前,请阅读已知限制:目前尚不支持辅助功能,没有输入法支持,且外观看起来像 Blender,而不是你的操作系统。
You can drive BUI many ways, and all of them share the same C runtime, so they all get the same widgets and interactions:
你可以通过多种方式驱动 BUI,它们都共享同一个 C 运行时,因此都能获得相同的组件和交互体验:
Not sure which to pick? If you already have code, use the row that matches it. If you’re starting fresh in Python, the Blender components are the friendliest.
不知道该选哪种?如果你已有代码,请选择与之匹配的行。如果你是 Python 新手,Blender 组件是最友好的选择。
Every screenshot here is a real BUI window, rendered off-screen. Where an image comes in a dark and a light version, GitHub shows the one that matches your theme.
这里的每一张截图都是真实的 BUI 窗口,在屏幕外渲染而成。如果图片有深色和浅色版本,GitHub 会显示与你主题匹配的那一个。
The node editor, from Python (examples/site/geometry_nodes.py)
节点编辑器,来自 Python (examples/site/geometry_nodes.py)
A pie menu, and Blender’s menu search (type in any open menu)
饼状菜单,以及 Blender 的菜单搜索(在任何打开的菜单中输入即可)
The color picker, and a tooltip
颜色选择器,以及工具提示
CSS effects in Rust: filters, clip-path, transforms, blend modes, backdrop-filter (site_effects.rs)
Rust 中的 CSS 特效:滤镜、裁剪路径、变换、混合模式、背景滤镜 (site_effects.rs)
CSS grid with repeat(auto-fill, minmax()), spans and grid-auto-flow: dense (site_grid.rs)
CSS 网格,支持 repeat(auto-fill, minmax())、跨度及 grid-auto-flow: dense (site_grid.rs)
CSS @keyframes at 0.25 s, 0.6 s and 1.0 s (site_animation.rs)
CSS @keyframes,分别在 0.25 秒、0.6 秒和 1.0 秒处 (site_animation.rs)
examples/showcase.rs: HTML + CSS in Rust
examples/showcase.rs:Rust 中的 HTML + CSS
Dear ImGui’s demo with its own widgets, in Blender’s theme
使用 Blender 主题的 Dear ImGui 演示及其自带组件
Qt syntax
Qt 语法
tkinter programs: text with tags, canvas, dialogs
tkinter 程序:带标签的文本、画布、对话框
Python. You need CPython 3.11 or newer. Wheels are published for Windows, Linux and macOS, so there’s nothing to compile. One package gives you all four Python APIs (tkinter syntax, Qt syntax, the Blender components and Dear ImGui):
Python。你需要 CPython 3.11 或更高版本。Windows、Linux 和 macOS 均已发布 Wheels 包,因此无需编译。一个包即可提供所有四种 Python API(tkinter 语法、Qt 语法、Blender 组件和 Dear ImGui):
Import it as blenderui. You’ll also see import bui in older examples: it’s the same code under its original name, but an unrelated project owns bui on PyPI, so prefer blenderui in anything you publish.
请将其作为 blenderui 导入。在旧示例中你可能会看到 import bui:这是同一代码的原始名称,但由于 PyPI 上有一个不相关的项目占用了 bui 这个名字,因此在你发布的任何内容中请优先使用 blenderui。
Rust. The crate is blenderui on crates.io:
Rust。Crate 名称为 crates.io 上的 blenderui:
The first build compiles BUI’s C kit for you, so you need Rust 1.88 or newer, CMake and a C compiler (on Windows, the Visual Studio Build Tools for the default MSVC target), and on Linux the X11 headers (libx11-dev or libX11-devel; no GL headers are needed).
首次构建时会为你编译 BUI 的 C 语言 kit,因此你需要 Rust 1.88 或更高版本、CMake 和 C 编译器(在 Windows 上为默认 MSVC 目标使用 Visual Studio 构建工具),在 Linux 上需要 X11 头文件(libx11-dev 或 libX11-devel;无需 GL 头文件)。
JavaScript. You need Node.js 18 or newer, or Bun:
JavaScript。你需要 Node.js 18 或更高版本,或者 Bun:
The npm packages are prebuilt for Windows x64. On Linux, build the addon from a clone (npm install then npm run build in bindings/, with Rust, CMake and a C compiler); windows are native X11 windows there. On macOS, JavaScript runs headless for now (see Platforms).
npm 包已预构建用于 Windows x64。在 Linux 上,请从克隆版本构建插件(在 bindings/ 目录下运行 npm install 然后 npm run build,需要 Rust、CMake 和 C 编译器);那里的窗口是原生的 X11 窗口。在 macOS 上,JavaScript 目前以无头模式运行(请参阅平台说明)。
C and C++. Build from source with CMake 3.20+ and a C11 / C++20 compiler (MSVC, GCC or MinGW-w64, or Clang); on Linux, also the X11 headers:
C 和 C++。使用 CMake 3.20+ 和 C11 / C++20 编译器(MSVC、GCC 或 MinGW-w64,或 Clang)从源码构建;在 Linux 上,还需要 X11 头文件:
In your own CMake project, add BUI as a subdirectory and link bui::kit (or bui::imgui for Dear ImGui):
在你自己的 CMake 项目中,将 BUI 添加为子目录并链接 bui::kit(或用于 Dear ImGui 的 bui::imgui):
Dear ImGui. Python gets it with pip install blenderui (import blenderui.imgui). C++ links bui::imgui, C links bui_cimgui. Rust uses the blenderui-imgui crate:
Dear ImGui。Python 可通过 pip install blenderui 安装(导入 blenderui.imgui)。C++ 链接 bui::imgui,C 链接 bui_cimgui。Rust 使用 blenderui-imgui crate:
If something goes wrong:
如果出现问题:
More: docs/getting-started.md and Install BUI on the website.
更多信息:docs/getting-started.md 以及网站上的“安装 BUI”。
Run any tkinter program on BUI without changing a line:
在 BUI 上运行任何 tkinter 程序,无需修改一行代码:
blenderui-tk makes import tkinter return BUI’s implementation for that run. Or opt in from code:
blenderui-tk 会使 import tkinter 在该次运行中返回 BUI 的实现。或者也可以在代码中选择启用:
That snippet is only a window with an entry and a button. These two are larger, unchanged programs from examples/site/, running on BUI:
该代码片段只是一个包含输入框和按钮的窗口。以下两个是来自 examples/site/ 的更大规模、未经修改的程序,它们正在 BUI 上运行:
Unchanged tkinter and ttk programs (examples/site/tk_export.py, ttk_queue.py)
未经修改的 tkinter 和 ttk 程序 (examples/site/tk_export.py, ttk_queue.py)
Number fields (Spinbox, Scale) scrub like Blender’s, Ctrl+Z / Ctrl+Shift+Z undo edits, and filedialog opens Blender’s File Browser. Guide: docs/tkinter.md.
数字输入框(Spinbox, Scale)的操作方式与 Blender 一样,Ctrl+Z / Ctrl+Shift+Z 可撤销编辑,文件对话框会打开 Blender 的文件浏览器。指南:docs/tkinter.md。
Build with Blender’s own components:
使用 Blender 原生组件进行构建:
That snippet is a small window. This is a bigger program built from the same components, a Render Properties editor:
该代码片段是一个小窗口。这是一个由相同组件构建的更大程序,即渲染属性编辑器:
A Render Properties editor from the components (examples/site/render_props.py)
由组件构建的渲染属性编辑器 (examples/site/render_props.py)
There are 45 components: layout, values, buttons, views, menus and pie menus, the color picker, the color ramp, curve and node editors, areas and templates. Reference: docs/components.md.
共有 45 个组件:布局、数值、按钮、视图、菜单和饼状菜单、颜色选择器、颜色渐变、曲线和节点编辑器、区域和模板。参考:docs/components.md。
Write Qt-style Python. The import is the only line that differs from PySide or PyQt, and neither is needed:
编写 Qt 风格的 Python 代码。除了导入语句外,其余与 PySide 或 PyQt 没有区别,且无需安装它们:
A bigger program in the same syntax, a material editor (not the output of the snippet above):
使用相同语法的更大程序,即材质编辑器(非上述代码片段的输出):
Qt syntax (examples/site/qt_material.py)
Qt 语法 (examples/site/qt_material.py)
Qt syntax is partial (ab
Qt 语法尚不完整 (ab