python脚本实例

https://dev.epicgames.com/documentation/en-us/unreal-engine/python-api/class/Actor?application_version=5.2

# SuraUtils.py
import unreal
editor = unreal.get_editor_subsystem(unreal.UnrealEditorSubsystem)
world = unreal.get_editor_subsystem(unreal.UnrealEditorSubsystem).get_editor_world()
game_world = editor.get_game_world()

# "/Script/HybridChromaKey.HybridChromaKeyActor")
def getAllActorsOfClass(world, actor_class):
    actors = unreal.GameplayStatics.get_all_actors_of_class(world, unreal.load_class(None, actor_class))
    return actors

# in blueprints use [Execute Python Script], 可以添加脚本中的同名input和output参数
import SuraUtils as sura

actors = sura.getAllActorsOfClass(sura.world, '/Script/HybridChromaKey.HybridChromaKeyActor')


### 
jiandao=unreal.get_editor_subsystem(unreal.EditorActorSubsystem).get_selected_level_actors()[0]
comps=n1.get_components_by_class(component_class=unreal.load_class(world, '/Script/Engine.SkeletalMeshComponent'))
body=next((i for i in comps if 'Body' in i.get_name()), None) # 找到序列里第一个满足要求的元素:此处为组件名中包含“Body”字样
jiandao.attach_to_component(body, "socket", unreal.AttachmentRule.SNAP_TO_TARGET, unreal.AttachmentRule.SNAP_TO_TARGET, unreal.AttachmentRule.KEEP_WORLD)
jiandao.set_actor_relative_location(unreal.Vector(4,0,-4), sweep=False, teleport=True)
jiandao.set_actor_relative_rotation(unreal.Rotator(-80,0,-168), False, True)

print(actor1.get_name())


####
unreal.get_editor_subsystem(unreal.EditorActorSubsystem).spawn_actor_from_class(unreal.load_class(world, '/Game/VRTemplate/Blueprints/VRPawn.VRPawn_C'), location=[0,0,0]) # 注意Content里的蓝图class的名称规范