unreal-pcg-python
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUnreal Engine PCG Python Integration Guide
Unreal Engine PCG Python集成指南
Overview
概述
Python interacts with UE5's Procedural Content Generation (PCG) framework at two levels:
- PCGPythonInterop Plugin (UE 5.5+, Beta) -- An editor-only PCG graph node ("Execute Python Script") that runs Python code mid-graph.
- PCG Python API (UE 5.2+) -- Standard module classes (
unreal,PCGComponent, etc.) for editor automation and custom node logic.PCGBlueprintElement
Important: All PCG Python functionality is editor-only. Python cannot run in packaged builds or at game runtime.
Python在两个层面与UE5的程序化内容生成(PCG)框架交互:
- PCGPythonInterop插件(UE 5.5+,测试版)——仅编辑器可用的PCG图节点("Execute Python Script"),可在图执行过程中运行Python代码。
- PCG Python API(UE 5.2+)——标准模块类(
unreal、PCGComponent等),用于编辑器自动化和自定义节点逻辑。PCGBlueprintElement
重要提示:所有PCG Python功能均仅支持编辑器环境。Python无法在打包版本或游戏运行时运行。
Official Documentation
官方文档
Community Resources
社区资源
| Resource | URL |
|---|---|
| Forum: Create PCG Graph with Python | https://forums.unrealengine.com/t/create-pcg-graph-with-python/1714891 |
| Forum: Generate PCG through Python | https://forums.unrealengine.com/t/generate-pcg-through-python-script/2053107 |
| Forum: Change PCG Parameters from Python | https://forums.unrealengine.com/t/how-to-change-pcg-graph-parameters-from-python/2060532 |
| Custom PCG Nodes Guide (Blueshift) | https://blueshift-interactive.com/2025/09/03/how-to-create-custom-pcg-nodes/ |
| PCG Extended Toolkit (community C++ plugin) | https://github.com/PCGEx/PCGExtendedToolkit |
| Houdini to PCG Data Example | https://github.com/cgtoolbox/HoudiniToPCGDataExample |
| 资源 | 链接 |
|---|---|
| 论坛:使用Python创建PCG图 | https://forums.unrealengine.com/t/create-pcg-graph-with-python/1714891 |
| 论坛:通过Python脚本生成PCG | https://forums.unrealengine.com/t/generate-pcg-through-python-script/2053107 |
| 论坛:从Python修改PCG参数 | https://forums.unrealengine.com/t/how-to-change-pcg-graph-parameters-from-python/2060532 |
| 自定义PCG节点指南(Blueshift) | https://blueshift-interactive.com/2025/09/03/how-to-create-custom-pcg-nodes/ |
| PCG扩展工具包(社区C++插件) | https://github.com/PCGEx/PCGExtendedToolkit |
| Houdini转PCG数据示例 | https://github.com/cgtoolbox/HoudiniToPCGDataExample |
1. PCGPythonInterop Plugin ("Execute Python Script" Node)
1. PCGPythonInterop插件("Execute Python Script"节点)
Plugin Details
插件详情
- Location:
Engine/Plugins/PCGInterops/PCGPythonInterop/ - Status: Beta (,
IsBetaVersion: true)EnabledByDefault: false - Module: (Editor-only)
PCGPythonInteropEditor - Dependencies: plugin +
PCGPythonScriptPlugin
- 位置:
Engine/Plugins/PCGInterops/PCGPythonInterop/ - 状态: 测试版(,
IsBetaVersion: true)EnabledByDefault: false - 模块: (仅编辑器可用)
PCGPythonInteropEditor - 依赖项: 插件 +
PCGPythonScriptPlugin
Enabling the Plugin
启用插件
- Enable Python Editor Script Plugin (under Plugins > Scripting)
- Enable PCG Python Interop (under Plugins > Procedural Content Generation)
- Restart the editor
- 启用Python Editor Script Plugin(在插件>脚本分类下)
- 启用PCG Python Interop(在插件>程序化内容生成分类下)
- 重启编辑器
The "Execute Python Script" Node
"Execute Python Script"节点
This is the only node the plugin adds. It runs Python code within a PCG graph.
Two input modes:
| Mode | Description |
|---|---|
| Reads Python source from an FString attribute on the "Source" pin, or uses an inline default script |
| Executes a |
Key characteristics:
- Runs on main thread only (Python GIL constraint)
- Not cacheable -- re-executes every time the graph runs
- No data output -- output pin is dependency-only (for execution ordering)
- Shows an editor toast on execution (suppressible via )
bMuteEditorToast - Default inline script:
print("Hello PCG World!")
Settings (UPROPERTY):
ScriptInputMethod -- EPCGPythonScriptInputMethod (Input or File)
ScriptSource -- FPCGAttributePropertyInputSelector (which attribute holds the script)
ScriptPath -- FFilePath (path to .py file, filtered to *.py)
bMuteEditorToast -- bool (suppress editor notification)All properties marked (can be set via PCG parameter overrides).
PCG_Overridable这是该插件新增的唯一节点,可在PCG图内运行Python代码。
两种输入模式:
| 模式 | 描述 |
|---|---|
| 从"Source"引脚的FString属性读取Python代码,或使用内置默认脚本 |
| 执行磁盘上的 |
核心特性:
- 仅在主线程运行(受Python GIL限制)
- 不可缓存——每次图运行时都会重新执行
- 无数据输出——输出引脚仅用于依赖控制(执行顺序)
- 执行时会显示编辑器提示框(可通过关闭)
bMuteEditorToast - 默认内置脚本:
print("Hello PCG World!")
设置项(UPROPERTY):
ScriptInputMethod -- EPCGPythonScriptInputMethod (Input or File)
ScriptSource -- FPCGAttributePropertyInputSelector (which attribute holds the script)
ScriptPath -- FFilePath (path to .py file, filtered to *.py)
bMuteEditorToast -- bool (suppress editor notification)所有标记为的属性均可通过PCG参数覆盖进行设置。
PCG_OverridablePlanned Future Features (from source TODOs)
计划中的未来功能(来自源码TODO)
- mode for line-by-line feedback
EvaluateStatement - Parameter inputs/outputs (get/set variables from within Python, like Blueprint/HLSL nodes)
- Generalized source editor in PCG for HLSL + Python
- Potential multi-thread support
- 模式,支持逐行反馈
EvaluateStatement - 参数输入/输出(可在Python内部获取/设置变量,类似Blueprint/HLSL节点)
- PCG中支持HLSL+Python的通用代码编辑器
- 潜在的多线程支持
2. PCG Python API (Editor Automation)
2. PCG Python API(编辑器自动化)
These classes are available via in any UE Python script, independent of the PCGPythonInterop plugin.
import unreal这些类可在任何UE Python脚本中通过导入,与PCGPythonInterop插件无关。
import unrealCreate PCG Graph Assets
创建PCG图资源
python
import unreal
asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
graph = asset_tools.create_asset(
'MyPCGGraph', '/Game/PCG',
unreal.PCGGraph, unreal.PCGGraphFactory()
)python
import unreal
asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
graph = asset_tools.create_asset(
'MyPCGGraph', '/Game/PCG',
unreal.PCGGraph, unreal.PCGGraphFactory()
)Trigger PCG Generation
触发PCG生成
python
undefinedpython
undefinedGet PCGComponent from an actor
Get PCGComponent from an actor
pcg_comp = actor.get_component_by_class(unreal.PCGComponent)
pcg_comp.generate(True) # force full regeneration
pcg_comp.generate_local(True) # local only, no replication
pcg_comp.set_graph(my_graph) # swap graph asset
pcg_comp.seed = 42 # set deterministic seed
pcg_comp.cleanup(True, False) # cleanup generated components
undefinedpcg_comp = actor.get_component_by_class(unreal.PCGComponent)
pcg_comp.generate(True) # force full regeneration
pcg_comp.generate_local(True) # local only, no replication
pcg_comp.set_graph(my_graph) # swap graph asset
pcg_comp.seed = 42 # set deterministic seed
pcg_comp.cleanup(True, False) # cleanup generated components
undefinedWork with Spatial Data
处理空间数据
python
undefinedpython
undefinedPCGSpatialData operations
PCGSpatialData operations
spatial_data.to_point_data() # convert to points
spatial_data.intersect_with(other) # boolean intersection
spatial_data.union_with(other) # boolean union
spatial_data.subtract(other) # boolean subtraction
spatial_data.get_bounds() # get spatial bounds
spatial_data.get_density_at_position(pos) # sample density
spatial_data.to_point_data() # convert to points
spatial_data.intersect_with(other) # boolean intersection
spatial_data.union_with(other) # boolean union
spatial_data.subtract(other) # boolean subtraction
spatial_data.get_bounds() # get spatial bounds
spatial_data.get_density_at_position(pos) # sample density
PCGPointData
PCGPointData
point_data = unreal.PCGPointData()
points = point_data.get_points() # -> Array[PCGPoint]
point_data.set_points(modified_points)
undefinedpoint_data = unreal.PCGPointData()
points = point_data.get_points() # -> Array[PCGPoint]
point_data.set_points(modified_points)
undefinedPCGBlueprintHelpers (Utility Functions)
PCGBlueprintHelpers(实用函数)
python
helpers = unreal.PCGBlueprintHelpers
helpers.get_actor_data(context)
helpers.get_component(context)
helpers.get_random_stream_from_point(point, settings, component)
helpers.compute_seed_from_position(position)
helpers.create_pcg_data_from_actor(actor, parse_actor)python
helpers = unreal.PCGBlueprintHelpers
helpers.get_actor_data(context)
helpers.get_component(context)
helpers.get_random_stream_from_point(point, settings, component)
helpers.compute_seed_from_position(position)
helpers.create_pcg_data_from_actor(actor, parse_actor)3. Custom PCG Nodes via PCGBlueprintElement
3. 通过PCGBlueprintElement创建自定义PCG节点
PCGBlueprintElementPCGBlueprintElementKey Overridable Methods
可重写的核心方法
| Method | Purpose |
|---|---|
| Primary execution -- receives and returns |
| Execution with PCG context access |
| Per-point processing |
| Per-point, returns variable number of output points |
| Fixed-count iteration |
| Custom display name |
| Custom node color |
| 方法 | 用途 |
|---|---|
| 主执行方法——接收并返回 |
| 带PCG上下文访问的执行方法 |
| 逐点处理逻辑 |
| 逐点处理,支持返回可变数量的输出点 |
| 固定次数迭代逻辑 |
| 自定义节点显示名称 |
| 自定义节点颜色 |
Configurable Properties
可配置属性
python
element.custom_input_pins # Array[PCGPinProperties]
element.custom_output_pins # Array[PCGPinProperties]
element.has_default_in_pin # bool
element.has_default_out_pin # bool
element.is_cacheable # bool
element.requires_game_thread # boolpython
element.custom_input_pins # Array[PCGPinProperties]
element.custom_output_pins # Array[PCGPinProperties]
element.has_default_in_pin # bool
element.has_default_out_pin # bool
element.is_cacheable # bool
element.requires_game_thread # bool4. Known Limitations
4. 已知限制
| Limitation | Details |
|---|---|
| Editor-only | No Python in packaged builds or runtime. The node explicitly errors: "Editor-only, should not be used at runtime." |
| No programmatic node creation | Python cannot add/connect nodes within a PCG graph programmatically (Epic confirmed, as of 2024) |
| No data output from Execute Python Script | The node only provides execution ordering, not PCG data flow |
| Main thread only | Python execution blocks the main thread |
| API churn | Method names changed between 5.2-5.5 (e.g., |
| Parameter access is finicky | Setting PCG graph parameters from Python via |
| 限制 | 详情 |
|---|---|
| 仅支持编辑器 | 打包版本或运行时无法使用Python。节点会明确报错:"Editor-only, should not be used at runtime." |
| 无法程序化创建节点 | 截至2024年,Epic确认Python无法在PCG图内程序化添加/连接节点 |
| Execute Python Script无数据输出 | 该节点仅提供执行顺序控制,不支持PCG数据流输出 |
| 仅主线程运行 | Python执行会阻塞主线程 |
| API变更频繁 | 5.2-5.5版本间方法名称有变更(例如 |
| 参数访问复杂 | 通过 |
5. Version History
5. 版本历史
| UE Version | PCG Status | Python Notes |
|---|---|---|
| 5.2 | Experimental | |
| 5.3 | Experimental | |
| 5.4 | Beta | |
| 5.5 | Beta | GPU compute path, |
| 5.7 | Production-Ready | |
| UE版本 | PCG状态 | Python相关说明 |
|---|---|---|
| 5.2 | 实验性 | 引入 |
| 5.3 | 实验性 | |
| 5.4 | 测试版 | |
| 5.5 | 测试版 | 新增GPU计算路径、 |
| 5.7 | 生产就绪 | |
6. Best Practices
6. 最佳实践
- Use Python for automation: Batch asset creation, parameter sweeps, CI/CD pipelines
- Use Blueprint for custom nodes: More stable API, designer-friendly, works in editor
- Use C++ for performance: Multi-threaded, GPU HLSL support, full API access
- Python + PCG sweet spot: Triggering generation across many actors, managing seeds, integrating external data (Houdini, numpy), asset migration scripts
- 使用Python实现自动化:批量创建资源、参数扫描、CI/CD流水线
- 使用Blueprint创建自定义节点:API更稳定,便于设计师使用,支持编辑器环境
- 使用C++实现高性能逻辑:支持多线程、GPU HLSL、完整API访问
- Python+PCG的最佳场景:跨多个Actor触发生成、管理随机种子、集成外部数据(Houdini、numpy)、资源迁移脚本