从 Python 调用 UE5 蓝图类函数

step1:创建一个蓝图类

创建一个蓝图类,设置一个公共的函数,定义参数
在这里插入图片描述

step2:用Python把这个蓝图类放入关卡中

这一步,你也可以手动拖进关卡中

import unreal


# 获取当前编辑器中的世界
editor = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
world = unreal.EditorLevelLibrary.get_editor_world()

location = unreal.Vector(0, 0, 0)
rot = unreal.Rotator(0, 0, 0)

mybp = unreal.EditorAssetLibrary.load_blueprint_class(
    '/Game/SP_LINE/BP_TEST_INVOKE01')
mesh_actor = editor.spawn_actor_from_class(
    mybp, location, rot)
mesh_actor.set_actor_label("BP_Route01_" + str(0))
mesh_actor.set_folder_path("/Route")

放入后的效果
在这里插入图片描述

step3:在 python 中调用它

在python中获取该actor的实例,并且传递参数和调用其方法

import unreal
editor = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
actors = editor.get_all_level_actors()

myactor = None
# 遍历所有 actor
for actor in actors:
    name = actor.get_name()

    if 'BP_TEST_INVOKE01' in name: 
        myactor = actor
        break
 
location = unreal.Vector(0.0, 0.0, 100.0)

# 调用参数
myactor.call_method("MoveTo1", tuple([location]))

执行方法后的效果。
在这里插入图片描述

参照官方文档调用函数1


  1. unreal._ObjectBase call_method ↩︎

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐