UE4编辑器Python编程6——Actor操作

生成Actor

# coding: utf-8

import unreal

def spawnActor():
    actor_class = unreal.EditorAssetLibrary.load_blueprint_class(
        '/Game/Blueprints/Cube_Blueprint')
    actor_location = unreal.Vector(0.0, 0.0, 0.0)
    actor_rotation = unreal.Rotator(0.0, 0.0, 0.0)
    unreal.EditorLevelLibrary.spawn_actor_from_class(
        actor_class,
        actor_location,
        actor_rotation)

类型转换

# coding: utf-8

import unreal

def cast(object_to_cast, object_class):
    try:
        return object_class.cast(object_to_cast)
    except:
        return None

def tryCast():
    if cast(unreal.load_asset('/Game/Textures/Colours'), unreal.Actor):
        print('Cast Succeeded')
    else:
        print('Cast Failed')
    # 目标类型改为unreal.Texture2D就能cast成功

获取关卡中指定类型的Actor

# coding: utf-8

import unreal

def cast(object_to_cast, object_class):
    try:
        return object_class.cast(object_to_cast)
    except:
        return None

def getAllActors(use_selection=False, actor_class=None, actor_tag=None):
    if use_selection:
        selected_actors = unreal.EditorLevelLibrary.get_selected_level_actors()
        # 从被选中的Actor中过滤出指定类型的Actor
        if actor_class:
            class_actors = [x for x in selected_actors if cast(x, actor_class)]
        # 再过滤出含指定Tag的
        if actor_tag:
            tag_actors = [
                x for x in class_actors if x.actor_has_tag(actor_tag)]
        return tag_actors
    elif actor_class:
        actors = unreal.GameplayStatics.get_all_actors_of_class(
            unreal.EditorLevelLibrary.get_editor_world(),
            actor_class
        )
        # 通过标签过滤
        if actor_tag:
            return [x for x in actors if x.actor_has_tag(actor_tag)]
    elif actor_tag:
        return unreal.GameplayStatics.get_all_actors_with_tag(
            unreal.EditorLevelLibrary.get_editor_world(),
            actor_tag
        )
    else:
        return unreal.GameplayStatics.get_all_actors_of_class(
            unreal.EditorLevelLibrary.get_editor_world(),
            unreal.Actor
        )

选择指定Actor

# coding: utf-8

import unreal

def selectActors(actors_to_select=[]):
    unreal.EditorLevelLibrary.set_selected_level_actors(actors_to_select)

使视口聚焦到特定的Actor

# coding: utf-8

import unreal

import random

def focusViewportOnActor(active_viewport_only=True, actor=None):
    command = 'CAMERA ALIGN'
    if active_viewport_only:
        command += ' ACTIVEVIEWPORTONLY'
    if actor:
        command += ' NAME='+actor.get_name()
    unreal.ZFunctions.execute_console_command(command)

def focusAllViewportsOnSelectedActors_EXAMPLE():
    focusViewportOnActor(False)# 不传入Name,这个命令会默认选择被选中的Actor

def focusActiveViewportOnRandomActor_EXAMPLE():
    world = unreal.EditorLevelLibrary.get_editor_world()
    actors = unreal.GameplayStatics.get_all_actors_of_class(
        world, unreal.Actor)
    random_actor_in_world = actors[random.randrange(len(actors))]
    focusViewportOnActor(False, random_actor_in_world)
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值