UE5中使用Python脚本创建蓝图文件

日常开发中需要编辑器下动态创建一些蓝图文件,例如自动创建成配置好的模板,本文通过一个创建蓝图的简单案例,演示Python进行蓝图文件的创建,动态创建的蓝图如下:

首先编写Python脚本:

python 复制代码
import unreal

def create_blueprint(bp_name, parent_class=unreal.Actor, blueprint_folder="/Game/Blueprints"):

    asset_tools = unreal.AssetToolsHelpers.get_asset_tools()

    # 设置蓝图的父类
    factory = unreal.BlueprintFactory()
    factory.set_editor_property("parent_class", parent_class)

    # 创建蓝图
    blueprint = asset_tools.create_asset(bp_name, blueprint_folder, None, factory)

    
    subsystem = unreal.get_engine_subsystem(unreal.SubobjectDataSubsystem)
    root_data_handle = subsystem.k2_gather_subobject_data_for_blueprint(blueprint)[0]

    sub_handle, fail_reason = subsystem.add_new_subobject(
        unreal.AddNewSubobjectParams(
            parent_handle=root_data_handle,
            new_class=unreal.StaticMeshComponent,
            blueprint_context=blueprint
        )
    )

    if not fail_reason.is_empty():
        print(f"Failed to add sub-object: {fail_reason}")
        return None

    subsystem.rename_subobject(sub_handle, "SimpleCube")

	# 添加一个CubeMesh
    cube_mesh = unreal.load_asset("/Engine/BasicShapes/Cube")
    subobject_data = subsystem.k2_find_subobject_data_from_handle(sub_handle)
    subobject = unreal.SubobjectDataBlueprintFunctionLibrary.get_object(subobject_data)
    subobject.set_editor_property("StaticMesh", cube_mesh)

    return blueprint

# 创建蓝图
blueprint = create_blueprint("SimpleBlueprint")

该脚本实现创建一个空蓝图并且加入StaticMesh组件,blueprint_folder变量是蓝图放置的位置。

然后将该python脚本填入执行python的节点中(较方便的做法):

点击执行即可。

相关推荐
小当家.10510 分钟前
MCP 协议深度解析:AI 领域的 USB-C 接口
开发语言·人工智能·agent·tool·mcp
码云骑士14 分钟前
97-Milvus从零到生产-Standalone-vs-Cluster-索引选择-分片监控扩容
python·milvus
accept 99%19 分钟前
python版提取 PDF 的常用第三方库与工具
开发语言·python·pdf
lzhdim20 分钟前
C# 通过 Windows API 实现进程内存读写操作
开发语言·windows·c#
小猴子爱上树25 分钟前
跨境图片翻译工具,批量处理商品图视频字幕
python·音视频
-银雾鸢尾-28 分钟前
C#中的预处理指令
开发语言·c#
用户2986985301430 分钟前
HTML 转 Word 指南:新手入门教程
人工智能·后端·python
Livia要学习34 分钟前
Python面向对象三大特性:封装、继承、多态
开发语言·python
dalong1038 分钟前
Savitzky-Golay 的C# 实现
开发语言·kotlin·c#
Csvn1 小时前
🐍 Day1 : Python 环境搭建 — 现代 Python 工作流
后端·python