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

点击执行即可。

相关推荐
万邦科技Lafite3 小时前
京东按图搜索京东商品(拍立淘) API (.jd.item_search_img)快速抓取数据
开发语言·前端·数据库·python·电商开放平台·京东开放平台
丁浩6664 小时前
Python机器学习---6.集成学习与随机森林
python·随机森林·机器学习
charlie1145141915 小时前
现代 Python 学习笔记:Statements & Syntax
笔记·python·学习·教程·基础·现代python·python3.13
Never_Satisfied5 小时前
在JavaScript / Node.js / 抖音小游戏中,使用tt.request通信
开发语言·javascript·node.js
爱吃小胖橘5 小时前
Unity资源加载模块全解析
开发语言·unity·c#·游戏引擎
千里镜宵烛7 小时前
Lua-迭代器
开发语言·junit·lua
渡我白衣7 小时前
C++ 同名全局变量:当符号在链接器中“相遇”
开发语言·c++·人工智能·深度学习·microsoft·语言模型·人机交互
淮北4947 小时前
html + css +js
开发语言·前端·javascript·css·html
麦麦大数据8 小时前
F036 vue+flask中医热性药知识图谱可视化系统vue+flask+echarts+mysql
vue.js·python·mysql·flask·可视化·中医中药