编辑器扩展与 Python 脚本
Editor Extension & Python Scripting
扩展 UE 编辑器有三条主要路线,门槛由低到高:Python 脚本(只在编辑器中可用,适合批量处理资产和串联外部 DCC 管线)、Editor Utility Widget(基于 UMG 的蓝图编辑器界面,做成自定义标签页),以及 C++ 编辑器插件(如 Editor Standalone Window 模板,用 Slate 的声明式语法构建窗口)。
三条路线
| 路线 | 本质 | 适合 | 限制 |
|---|---|---|---|
| Python | Python Editor Script Plugin,内嵌 Python 3.11.8 | 资产批处理(如批量生成 LOD)、与其他 3D 软件打通的管线、用 Python 自制界面控制编辑器 | 实验性;只在编辑器中可用,PIE、独立游戏和烘焙后的程序中都不能用,不能作为玩法脚本语言 |
| Editor Utility Widget | 基于 UMG 的 Widget Blueprint | 自定义编辑器标签页(可以从 Windows 菜单打开) | Beta |
| C++ 编辑器插件 + Slate | 编辑器模块中用 Slate 构建界面 | 深度集成的工具窗口 | 需要 C++;Slate 是独立的 UI 框架 |
(Scripting the Unreal Editor Using Python、Editor Utility Widgets in Unreal Engine、Slate Editor Window Quickstart Guide for Unreal Engine)
Python
- 启用 Python Editor Script Plugin,建议同时启用 Editor Scripting Utilities 插件,它为常见编辑器任务提供简化的 API;然后重启。之所以用 Python 3.11.8,是因为它是 VFX Reference Platform 的重要组成部分。(Scripting the Unreal Editor Using Python)
- 运行方式:Output Log 的控制台输入栏可以切换成接受 Python 代码(print 输出也会重定向到 Output Log);命令行参数
-ExecutePythonScript=<脚本路径>;编辑器在 Python 路径中发现init_unreal.py时会立即自动执行它;项目设置中的 Startup Scripts 每次打开项目时运行。(Scripting the Unreal Editor Using Python) - 子系统也可以从 Python 访问,见 子系统。
Editor Utility Widget
基于 UMG,所以和其他 UMG Widget Blueprint 一样在蓝图中搭建;它专用于编辑器界面,可以创建自定义编辑器标签页,并像引擎自带的标签页一样从 Windows 菜单中选择。创建方式:内容浏览器右键 > Editor Utilities > Editor Utility Widget。(Editor Utility Widgets in Unreal Engine)UMG 与 Slate 的关系见 UMG、Slate 与 Common UI。
C++ 与 Slate 编辑器窗口
- 新建插件时选择 Editor Standalone Window 模板,会自动生成插件模块和支持基础编辑器窗口的 C++ 类。(Slate Editor Window Quickstart Guide for Unreal Engine)插件和模块的结构见 模块、插件与构建系统。
- 窗口内容在
OnSpawnPluginTab中用 Slate 的声明式语法构建:SNew(SDockTab)创建停靠标签页,里面嵌套SNew(SBox),再嵌套SNew(STextBlock).Text(...);方括号表示子控件,链式调用设置属性(如.HAlign(HAlign_Center))。(Slate Editor Window Quickstart Guide for Unreal Engine)
flowchart TD M["编辑器模块 StartupModule"] --> T["注册标签页生成器"] T --> O["OnSpawnPluginTab"] O --> D["SNew(SDockTab)"] D --> B["SNew(SBox) 布局"] B --> X["SNew(STextBlock) 内容"]
图中编辑器模块启动时注册标签页,打开时由 OnSpawnPluginTab 用 Slate 逐层构建控件树。(模块启动时注册标签页生成器是模板代码的结构,官方页逐行讲解了 OnSpawnPluginTab。)
怎么选
一次性的批处理或管线工作用 Python;给美术、策划用的可视化工具优先做 Editor Utility Widget;需要深度定制编辑器行为、追求性能或要随插件分发时再写 C++/Slate。这是对上表的归纳;工具本身也应纳入 自动化测试。
出处
- Scripting the Unreal Editor Using Python UE 5.8 文档(Python 为实验性功能)
- Editor Utility Widgets in Unreal Engine UE 5.8 文档(Beta 功能)
- Slate Editor Window Quickstart Guide for Unreal Engine UE 5.8 文档