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

点击执行即可。

相关推荐
csbysj202011 分钟前
并查集路径压缩
开发语言
企业对冲系统官23 分钟前
基差风险管理系统集成说明与接口规范
大数据·运维·python·算法·区块链·github
JavaEdge.24 分钟前
java.io.IOException: Previous writer likely failed to write hdfs报错解决方案
java·开发语言·hdfs
花酒锄作田33 分钟前
[python]Flask - Tracking ID的设计
python·flask·pytest
C+++Python40 分钟前
C++类型判断
开发语言·c++
PeterClerk44 分钟前
计算机视觉常用指标(Metrics)速查与解释(持续更新)
人工智能·python·深度学习·计算机视觉·benchmark·评测
黎雁·泠崖1 小时前
整数的N进制字符串表示【递归+循环双版满分实现】
c语言·开发语言
张张努力变强1 小时前
C++类和对象(一):inline函数、nullptr、类的定义深度解析
开发语言·前端·jvm·数据结构·c++·算法
独自破碎E1 小时前
Java的CMS垃圾回收流程
java·开发语言
oioihoii1 小时前
C++线程编程模型演进:从Pthread到jthread的技术革命
java·开发语言·c++