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的节点中(较方便的做法):

点击执行即可。

相关推荐
bst@微胖子1 小时前
Python高级语法之selenium
开发语言·python·selenium
王小义笔记1 小时前
Postman如何流畅使用DeepSeek
开发语言·测试工具·lua·postman·deepseek
查理零世2 小时前
【蓝桥杯集训·每日一题2025】 AcWing 6118. 蛋糕游戏 python
python·算法·蓝桥杯
魔尔助理顾问3 小时前
一个简洁高效的Flask用户管理示例
后端·python·flask
java1234_小锋3 小时前
一周学会Flask3 Python Web开发-request请求对象与url传参
开发语言·python·flask·flask3
流星白龙5 小时前
【C++】36.C++IO流
开发语言·c++
诚信爱国敬业友善6 小时前
常见排序方法的总结归类
开发语言·python·算法
nbsaas-boot7 小时前
Go 自动升级依赖版本
开发语言·后端·golang
架构默片7 小时前
【JAVA工程师从0开始学AI】,第五步:Python类的“七十二变“——当Java的铠甲遇见Python的液态金属
java·开发语言·python
不只会拍照的程序猿8 小时前
从插入排序到希尔排序
java·开发语言·数据结构·算法·排序算法