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

点击执行即可。

相关推荐
2501_9418771321 小时前
大规模系统稳定性建设方法论与工程实践分享
java·开发语言
2501_9418204921 小时前
面向零信任安全与最小权限模型的互联网系统防护设计思路与多语言工程实践分享
开发语言·leetcode·rabbitmq
浩瀚地学21 小时前
【Java】面向对象进阶-接口
java·开发语言·经验分享·笔记·学习
2501_9418024821 小时前
面向微服务限流、熔断与降级协同的互联网系统高可用架构与多语言工程实践分享
开发语言·python
2501_9418752821 小时前
分布式系统中的安全权限与审计工程实践方法论经验总结与多语言示例解析分享
开发语言·rabbitmq
海天一色y21 小时前
Pycharm(十八)进程相关内容
python·pycharm
无限进步_21 小时前
【C语言】堆排序:从堆构建到高效排序的完整解析
c语言·开发语言·数据结构·c++·后端·算法·visual studio
haokan_Jia21 小时前
Java 并发编程-ScheduledFuture
java·前端·python
雾岛听蓝21 小时前
STL 容器适配器:stack、queue 与 priority_queue
开发语言·c++
CSDN_RTKLIB21 小时前
【One Definition Rule】多编译单元定义同名全局变量
开发语言·c++