python-ue4-metahuman-nerf:我创造了一个数字人!!

2023-11-17

原文:https://zhuanlan.zhihu.com/p/561152870

目录

收起

1. 准备工作:制作 MetaHuman 角色

1.1 创建 MetaHuman 角色

1.2 Quixel Bridge 下载 MetaHuman

1.3 导出 MetaHuman 到 UE4

2. UE 渲染 MetaHuman 的多视点图片

2.1 如何在 UE 中手动渲染视频?

2.2 Python 自动化渲染

2.3 全部代码

3. 渲染结果

随着 ECCV 2020 NeRF 的出现,在计算机视觉、计算机图形学、三维重建等领域砸开了无数的坑位,一篇 NeRF 养活了多少科研工作者、大厂员工。但是要想训练这样一个 Neural Radiance Filed 却不是一件容易的事情,首先从数据来看,训练一个 NeRF 需要该场景的多视点图片,同时还需要知道每张图片所对应的相机参数,要想得到这样的数据,可谓是耗时耗力。那么,有没有一种方式,可以虚拟的仿真出一组 NeRF 的训练数据?

答案当然是有的,而且不止一种方式。下面本文将分享一种使用 UE4 来渲染带有相机参数的多视点图片的方法。在正式开始之前,首先来介绍一下 MetaHuman,它就是我们要渲染的对象。在计算机视觉、三维重建领域,人脸方向的研究一直是一个热点,而 MetaHuman 则是一个高保真的数字人开源框架,我们可以利用 MetaHuman 来设计出自己想要的数字人形象,然后渲染出其多视点图片,用作进一步的研究。

1. 准备工作:制作 MetaHuman 角色

在 UE4 中渲染 MetaHuman 之前,我们首先需要创造一个 Metahuman 角色,然后并将其导入到 UE4 中进行渲染,整体可分为以下几个步骤

  1. 使用 MetaHuman Creator 创建 MetaHuman 角色;也可以不创建新角色,直接使用自带的即可。
  2. 借助 Quixel Bridge 工具将创建的 MetaHuman 角色下载下来。
  3. 在 Quixel Bridge 中将 MetaHuman 角色导出到 UE4中。

下面分别对这几步进行说明:

1.1 创建 MetaHuman 角色

登入 MetaHuman Creator 官网:https://metahuman.unrealengine.com/,会看到如下界面,左侧的 CREATE METAHUMAN 是系统默认给出的 Metahuman 角色,可以直接在其基础上进行修改,创作一个新的 Metahuman,这会存在左侧的 MY METAHUMAN 目录中。

具体的操作界面如下,整体分为三部分:脸部、毛发、身体。我们可以对这三部分中的各个细节进行操作,大家可以自行尝试~

1.2 Quixel Bridge 下载 MetaHuman

我们刚刚创作好的 MetaHuman 角色是存在于官方的服务器上的,在对它进行操作之前,还需要借助 Quixel Bridge 工具将其下载下来。

Quixel Bridge 下载链接:https://quixel.com/bridge

在下载时候,我们使用 Epic Games 账号,后续用的也一直是这个账号。下载完成并登录之后,我们会看到如下界面

左侧导航栏中的 MetaHuman Presets 和 My MetaHuman 分别表示默认角色和我们前一步创建的角色。点击右上角绿色的下载按钮即可下载到本地。

1.3 导出 MetaHuman 到 UE4

在导出 MetaHuman 角色到 UE4 之前,我们需要配置:导出的目标引擎是什么、引擎的版本号、引擎的位置。如下图所示

之后,点击对应 MetaHuman 角色右上角的 Export 按钮即可将其导出到 UE 引擎中(注意:在导出时,要保证 UE 是打开的)。

2. UE 渲染 MetaHuman 的多视点图片

在利用 Python 自动化渲染符合要求的多视点图片之前,我们首先要搞明白的一件事情是:如何手动的在 UE4 中渲染视频序列(视频和图片本质上是一样的,图片就是视频一帧一帧截出来的)?

2.1 如何在 UE 中手动渲染视频?

step1:将 MetaHuman 导入到 UE4 的场景中,并将其定位到合适的位置。在场景中添加电影摄像机 CineCameraActor, 如下:

step2:创建过场动画,并添加关卡序列,如下

step3:依次点击,轨道 — Actor到Sequencer — CineCameraActor,创建 CineCameraActor 的轨道序列,如下

step4:在创建好的 CineCameraActor 轨道序列中添加关键帧(下图添加了 5 个关键帧,分别指定了相机的位置和旋转),使得相机按照既定方式运动,如下

step5:至此,视频序列创建完毕,点击 将影片渲染为视频序列或图片序列,完成导出

上述就是在 UE4 中渲染 MetaHuman 的整个过程,只需要鼠标点点就行了,比较简单。但是有一个严重的问题是:如果要渲染一个多视点 MetaHuman 数据集用作 NeRF 训练的话,我们是必须要知道相机的姿态的;而在上述过程中,我们只能准确的知道关键帧的相机姿态,其他帧的相机姿态都是被插值出来的。如果要手动定义数百上千个关键帧,又是相当繁琐的一项工作,因此我们下面考虑利用 python 来自动化的执行上述过程,自动化的定义关键帧,指定相机的运动。

2.2 Python 自动化渲染

首先,总结一下前一章节手动渲染的过程,整体上可以分为如下 4 个步骤:

  1. 导入 MetaHuman 角色,并将其定位的合适位置
  2. 创建关卡序列,并创建 CineCameraActor 轨道序列
  3. 设置相机参数,并指定相机的位置和旋转,定义相机的运动方式
  4. 导出渲染的视频、图片序列

上述 4 个步骤中,除了步骤一还需要我们手动操作以外,其余步骤全部都可以利用 python 脚本来实现。在 UE4 中使用 python 接口还需要进行一些简单的配置,具体操作可以参考:https://www.bilibili.com/video/BV1PE411d7z8

步骤一:角色导入以及人脸定位

由于我只想渲染 MetaHuman 的人脸区域,因此我需要将其余身体部分移动到相机视野之外。需要移动的身体部位有:Torso、Legs、Feet。

接着进行 MetaHuman 的定位,为了方便,我们可以将其 Face 位置定位在坐标原点(0,0,0),这是为了方便后面指定相机的位置与旋转。

步骤二:指定相机运动方式:位置和旋转

在这一步,我希望相机能够以 MetaHuman 的人脸(也就是坐标原点)为中心,做球形旋转运动。关于相机的外参,做如下设置:

  • 相机到坐标原点的距离(即:球半径)为35
  • 相机的俯仰角设置为如下角度:[-30, -15, 0, 15, 30]
  • 在每个俯仰角下,偏航角区间设置为:[-180, 180], 每间隔角度 10 定义一个关键点(即:拍摄一张照片)

渲染的相关的设置如下:

  • 相机的视场角设置为:100
  • 所渲染图片的分辨率:512
  • 帧与帧之间的延迟:1.5

相机内参的设置见:步骤三具体的渲染代码。

现在,我们将相机的运动数据(相机位置和旋转)、渲染的相关设置,存为一个 .json 文件,在步骤三渲染的时候直接读取这些数据即可。代码如下:

import json
import numpy as np

# 得到相机的运动数据(位置、旋转)
def get_camera_transform():
    radius = 35
    camara_transform = []

    for y_pitch in range(-30, 31, 15):
        for z_yaw in range(-180, 181, 10):
            location_x = radius * np.cos(z_yaw / 180 * np.pi) * np.cos(y_pitch / 180 * np.pi)
            location_y = radius * np.sin(z_yaw / 180 * np.pi) * np.cos(y_pitch / 180 * np.pi)
            location_z = -radius * np.sin(y_pitch / 180 * np.pi)

            print([-location_x, -location_y, location_z, 0, y_pitch, z_yaw])
            camara_transform.append([-location_x, -location_y, location_z, 0, y_pitch, z_yaw])

    return camara_transform

# 将基础数据存为一个 .json 文件
def make_json():
    camara_transform = get_camera_transform()

    data_dict = {
        f"film_fov": 100,
        "film_resolution": 512,
        "delay_every_frame": 1.5,
        "output_image_path": "F:/projects/Unreal projects/MetaHumanRender/RenderImages/Danielle/face_hair",
        "camera_data": {
            "camera_qRrq9DqY": {
                "type": "Camera",
                "camera_transform": camara_transform
            }
        }
    }

    data_json = json.dumps(data_dict, indent=4)
    with open("camera_data.json", 'w') as json_file:
        json_file.write(data_json)


if __name__ == '__main__':
    make_json()

步骤三:读取相机运动数据,创建关卡序列添加关键帧

首先,读取上一节生成的.json文件,获取相机运动数据

def read_json_file(path):
    file = open(path, "rb")
    file_json = json.load(file)

    export_path = file_json['output_image_path']  # image export path
    film_fov = file_json.get('film_fov', None)  # camera film fov
    film_resolution = file_json.get("film_resolution", 512)  # camera file resolution
    delay_every_frame = file_json.get("delay_every_frame", 3.0)  # sequence run delay every frame
    camera_json = file_json['camera_data']  # camera data

    camera_transform_array = []
    camera_rotation_array = []
    camera_name_array = []
    for camera in camera_json:
        camera_transform = camera_json[camera]["camera_transform"]
        for index in range(len(camera_transform)):

            camera_transform_array.append(unreal.Transform(
                location=[camera_transform[index][0], camera_transform[index][1], camera_transform[index][2]],
                rotation=[camera_transform[index][3], camera_transform[index][4], camera_transform[index][5]],
                scale=[1.0, 1.0, 1.0]
            ))
            camera_rotation_array.append(unreal.Rotator(
                roll=camera_transform[index][3],
                pitch=camera_transform[index][4],
                yaw=camera_transform[index][5]
            ))
        camera_name_array.append(camera)

    return camera_transform_array, camera_rotation_array, camera_name_array, export_path, film_fov, film_resolution, delay_every_frame

接着创建关卡序列,并添加关键帧,每一个相机姿态对应一个关键帧。在这一部分,做要做了如下几个事情,具体细节可参考代码

  • 在场景中创建一个电影摄像机 CineCameraActor,并指定其位置和旋转。
  • 设置胶片或数字传感器的尺寸。
  • 相机的镜头的参数设置:最小焦距、最大焦距等。
  • 聚焦设置:焦距、光圈、视场角等。
  • 相机的绑定,相机的关键帧添加,对每一个相机姿态添加一个关键帧。

代码

def create_sequence(asset_name, camera_transform_array, camera_rotation_array, camera_name_array, film_fov,
                    package_path='/Game/'):
    # create a sequence
    sequence = unreal.AssetToolsHelpers.get_asset_tools().create_asset(asset_name, package_path, unreal.LevelSequence,
                                                                       unreal.LevelSequenceFactoryNew())
    sequence.set_display_rate(unreal.FrameRate(numerator=1, denominator=1))
    sequence.set_playback_start_seconds(0)
    sequence.set_playback_end_seconds(len(camera_transform_array))

    # add a camera cut track
    camera_cut_track = sequence.add_master_track(unreal.MovieSceneCameraCutTrack)
    print(camera_transform_array[0], '=======================================================================')
    print(camera_rotation_array[0].get_editor_property("roll"))

    # 添加关键帧
    for index in range(0, len(camera_name_array)):
        camera_cut_section = camera_cut_track.add_section()
        camera_cut_section.set_start_frame_bounded(6 * index)
        camera_cut_section.set_end_frame_bounded(6 * index + len(camera_transform_array))
        camera_cut_section.set_start_frame_seconds(6 * index)
        camera_cut_section.set_end_frame_seconds(6 * index + len(camera_transform_array))

        # Create a cine camera actor
        # 在世界编辑器中创建一个电影摄像机CineCameraActor,并指定位置和旋转
        camera_actor = unreal.EditorLevelLibrary().spawn_actor_from_class(unreal.CineCameraActor,
                                                                          unreal.Vector(0, 0, 0),
                                                                          unreal.Rotator(0, 0, 0))
        # 指定电影摄像机CineCameraActor的名称
        camera_actor.set_actor_label(camera_name_array[index])
        # 返回CineCamera组件
        camera_component = camera_actor.get_cine_camera_component()
        ratio = math.tan(film_fov / 360.0 * math.pi) * 2

        # step1:胶片背板设置
        filmback = camera_component.get_editor_property("filmback")
        # sensor_height:胶片或数字传感器的垂直尺寸,以毫米为单位
        filmback.set_editor_property("sensor_height", 60)
        # sensor_width:胶片或数字传感器的水平尺寸,以毫米为单位
        filmback.set_editor_property("sensor_width", 60)

        # step2:镜头设置
        lens_settings = camera_component.get_editor_property("lens_settings")
        lens_settings.set_editor_property("min_focal_length", 4.0)
        lens_settings.set_editor_property("max_focal_length", 1000)
        lens_settings.set_editor_property("min_f_stop", 1.2)
        lens_settings.set_editor_property("max_f_stop", 500)
        print(lens_settings)

        # step3:聚焦设置
        focal_length = 45  # 焦距
        current_aperture = 500  # 光圈
        camera_component.set_editor_property("current_focal_length", focal_length)  # 设置当前焦距
        camera_component.set_editor_property("current_aperture", current_aperture)  # 设置当前光圈
        fov = camera_component.get_editor_property("field_of_view")
        print(camera_component.get_editor_property("current_focal_length"))
        print(camera_component.get_editor_property("current_aperture"))
        print(f"fov - {fov}")

        # add a binding for the camera
        camera_binding = sequence.add_possessable(camera_actor)
        transform_track = camera_binding.add_track(unreal.MovieScene3DTransformTrack)
        transform_section = transform_track.add_section()
        transform_section.set_start_frame_bounded(6 * index)
        transform_section.set_end_frame_bounded(6 * index + len(camera_transform_array))
        transform_section.set_start_frame_seconds(6 * index)
        transform_section.set_end_frame_seconds(6 * index + len(camera_transform_array))

        # get channel for location_x location_y location_z
        channel_location_x = transform_section.get_channels()[0]
        channel_location_y = transform_section.get_channels()[1]
        channel_location_z = transform_section.get_channels()[2]

        # get key for rotation_y rotation_z
        channel_rotation_x = transform_section.get_channels()[3]
        channel_rotation_y = transform_section.get_channels()[4]
        channel_rotation_z = transform_section.get_channels()[5]

        # camera_transform = camera_transform_array[index]
        # camera_location_x = camera_transform.get_editor_property("translation").get_editor_property("x")
        # camera_location_y = camera_transform.get_editor_property("translation").get_editor_property("y")
        # camera_location_z = camera_transform.get_editor_property("translation").get_editor_property("z")

        for i in range(len(camera_transform_array)):
            camera_transform = camera_transform_array[i]
            camera_location_x = camera_transform.get_editor_property("translation").get_editor_property("x")
            camera_location_y = camera_transform.get_editor_property("translation").get_editor_property("y")
            camera_location_z = camera_transform.get_editor_property("translation").get_editor_property("z")
            camera_rotation = camera_rotation_array[i]
            camera_rotate_roll = camera_rotation.get_editor_property("roll")
            camera_rotate_pitch = camera_rotation.get_editor_property("pitch")
            camera_rotate_yaw = camera_rotation.get_editor_property("yaw")

            new_time = unreal.FrameNumber(value=i)
            channel_location_x.add_key(new_time, camera_location_x, 0.0)
            channel_location_y.add_key(new_time, camera_location_y, 0.0)
            channel_location_z.add_key(new_time, camera_location_z, 0.0)
            channel_rotation_x.add_key(new_time, camera_rotate_roll, 0.0)
            channel_rotation_y.add_key(new_time, camera_rotate_pitch, 0.0)
            channel_rotation_z.add_key(new_time, camera_rotate_yaw, 0.0)

            # if i % 5 == 0:
            #     print(new_time, [camera_location_x, camera_location_y, camera_location_z, camera_rotate_roll, camera_rotate_pitch, camera_rotate_yaw])


        # add the binding for the camera cut section
        camera_binding_id = sequence.make_binding_id(camera_binding, unreal.MovieSceneObjectBindingSpace.LOCAL)
        camera_cut_section.set_camera_binding_id(camera_binding_id)

    # save sequence asset
    unreal.EditorAssetLibrary.save_loaded_asset(sequence, False)

    return sequence

步骤四:将关卡序列渲染为视频、图片序列

将创建的关卡序列渲染为电影序列,这部分主要就是设置一些渲染参数,对应手动渲染的 step5,具体参数的含义可参考 unreal 官方文档。代码如下:

# sequence movie
def render_sequence_to_movie(export_path, film_resolution, delay_every_frame, on_finished_callback):
    # 1) Create an instance of our UAutomatedLevelSequenceCapture and override all of the settings on it. This class is currently
    # set as a config class so settings will leak between the Unreal Sequencer Render-to-Movie UI and this object. To work around
    # this, we set every setting via the script so that no changes the user has made via the UI will affect the script version.
    # The users UI settings will be reset as an unfortunate side effect of this.
    capture_settings = unreal.AutomatedLevelSequenceCapture()

    # Set all POD settings on the UMovieSceneCapture
    # capture_settings.settings.output_directory = unreal.DirectoryPath("../../../unreal-render-release/Saved/VideoCaptures1/")
    capture_settings.settings.output_directory = unreal.DirectoryPath(export_path)

    # If you game mode is implemented in Blueprint, load_asset(...) is going to return you the C++ type ('Blueprint') and not what the BP says it inherits from.
    # Instead, because game_mode_override is a TSubclassOf<AGameModeBase> we can use unreal.load_class to get the UClass which is implicitly convertable.
    # ie: capture_settings.settings.game_mode_override = unreal.load_class(None, "/Game/AI/TestingSupport/AITestingGameMode.AITestingGameMode_C")
    capture_settings.settings.game_mode_override = None
    capture_settings.settings.output_format = "{camera}" + "_{frame}"
    capture_settings.settings.overwrite_existing = False
    capture_settings.settings.use_relative_frame_numbers = False
    capture_settings.settings.handle_frames = 0
    capture_settings.settings.zero_pad_frame_numbers = 4
    # If you wish to override the output framerate you can use these two lines, otherwise the framerate will be derived from the sequence being rendered
    capture_settings.settings.use_custom_frame_rate = False
    # capture_settings.settings.custom_frame_rate = unreal.FrameRate(24,1)
    capture_settings.settings.resolution.res_x = film_resolution
    capture_settings.settings.resolution.res_y = film_resolution
    capture_settings.settings.enable_texture_streaming = False
    capture_settings.settings.cinematic_engine_scalability = True
    capture_settings.settings.cinematic_mode = True
    capture_settings.settings.allow_movement = False  # Requires cinematic_mode = True
    capture_settings.settings.allow_turning = False  # Requires cinematic_mode = True
    capture_settings.settings.show_player = False  # Requires cinematic_mode = True
    capture_settings.settings.show_hud = False  # Requires cinematic_mode = True
    capture_settings.use_separate_process = False
    capture_settings.close_editor_when_capture_starts = False  # Requires use_separate_process = True
    capture_settings.additional_command_line_arguments = "-NOSCREENMESSAGES"  # Requires use_separate_process = True
    capture_settings.inherited_command_line_arguments = ""  # Requires use_separate_process = True

    # Set all the POD settings on UAutomatedLevelSequenceCapture
    capture_settings.use_custom_start_frame = False  # If False, the system will automatically calculate the start based on sequence content
    capture_settings.use_custom_end_frame = False  # If False, the system will automatically calculate the end based on sequence content
    capture_settings.custom_start_frame = unreal.FrameNumber(0)  # Requires use_custom_start_frame = True
    capture_settings.custom_end_frame = unreal.FrameNumber(0)  # Requires use_custom_end_frame = True
    capture_settings.warm_up_frame_count = 0
    capture_settings.delay_before_warm_up = 0
    capture_settings.delay_before_shot_warm_up = 0
    capture_settings.delay_every_frame = delay_every_frame
    capture_settings.write_edit_decision_list = True

    # Tell the capture settings which level sequence to render with these settings. The asset does not need to be loaded,
    # as we're only capturing the path to it and when the PIE instance is created it will load the specified asset.
    # If you only had a reference to the level sequence, you could use "unreal.SoftObjectPath(mysequence.get_path_name())"
    capture_settings.level_sequence_asset = unreal.SoftObjectPath(sequence_asset_path)

    # To configure the video output we need to tell the capture settings which capture protocol to use. The various supported
    # capture protocols can be found by setting the Unreal Content Browser to "Engine C++ Classes" and filtering for "Protocol"
    # ie: CompositionGraphCaptureProtocol, ImageSequenceProtocol_PNG, etc. Do note that some of the listed protocols are not intended
    # to be used directly.
    # Right click on a Protocol and use "Copy Reference" and then remove the extra formatting around it. ie:
    # Class'/Script/MovieSceneCapture.ImageSequenceProtocol_PNG' gets transformed into "/Script/MovieSceneCapture.ImageSequenceProtocol_PNG"
    capture_settings.set_image_capture_protocol_type(
        unreal.load_class(None, "/Script/MovieSceneCapture.ImageSequenceProtocol_PNG"))

    # After we have set the capture protocol to a soft class path we can start editing the settings for the instance of the protocol that is internallyc reated.
    capture_settings.get_image_capture_protocol().compression_quality = 85

    # Finally invoke Sequencer's Render to Movie functionality. This will examine the specified settings object and either construct a new PIE instance to render in,
    # or create and launch a new process (optionally shutting down your editor).
    unreal.SequencerTools.render_movie(capture_settings, on_finished_callback)

2.3 全部代码

import sys

import unreal, os, json, math

# sequence asset path
sequence_asset_path = '/Game/Render_Sequence.Render_Sequence'


# read json file
def read_json_file(path):
    file = open(path, "rb")
    file_json = json.load(file)

    export_path = file_json['output_image_path']  # image export path
    film_fov = file_json.get('film_fov', None)  # camera film fov
    film_resolution = file_json.get("film_resolution", 512)  # camera file resolution
    delay_every_frame = file_json.get("delay_every_frame", 3.0)  # sequence run delay every frame
    camera_json = file_json['camera_data']  # camera data

    camera_transform_array = []
    camera_rotation_array = []
    camera_name_array = []
    for camera in camera_json:
        camera_transform = camera_json[camera]["camera_transform"]
        for index in range(len(camera_transform)):

            camera_transform_array.append(unreal.Transform(
                location=[camera_transform[index][0], camera_transform[index][1], camera_transform[index][2]],
                rotation=[camera_transform[index][3], camera_transform[index][4], camera_transform[index][5]],
                scale=[1.0, 1.0, 1.0]
            ))
            camera_rotation_array.append(unreal.Rotator(
                roll=camera_transform[index][3],
                pitch=camera_transform[index][4],
                yaw=camera_transform[index][5]
            ))
        camera_name_array.append(camera)

    return camera_transform_array, camera_rotation_array, camera_name_array, export_path, film_fov, film_resolution, delay_every_frame


def create_sequence(asset_name, camera_transform_array, camera_rotation_array, camera_name_array, film_fov,
                    package_path='/Game/'):
    # create a sequence
    sequence = unreal.AssetToolsHelpers.get_asset_tools().create_asset(asset_name, package_path, unreal.LevelSequence,
                                                                       unreal.LevelSequenceFactoryNew())
    sequence.set_display_rate(unreal.FrameRate(numerator=1, denominator=1))
    sequence.set_playback_start_seconds(0)
    sequence.set_playback_end_seconds(len(camera_transform_array))

    # add a camera cut track
    camera_cut_track = sequence.add_master_track(unreal.MovieSceneCameraCutTrack)
    print(camera_transform_array[0], '=======================================================================')
    print(camera_rotation_array[0].get_editor_property("roll"))

    # 添加关键帧
    for index in range(0, len(camera_name_array)):
        camera_cut_section = camera_cut_track.add_section()
        camera_cut_section.set_start_frame_bounded(6 * index)
        camera_cut_section.set_end_frame_bounded(6 * index + len(camera_transform_array))
        camera_cut_section.set_start_frame_seconds(6 * index)
        camera_cut_section.set_end_frame_seconds(6 * index + len(camera_transform_array))

        # Create a cine camera actor
        # 在世界编辑器中创建一个电影摄像机CineCameraActor,并指定位置和旋转
        camera_actor = unreal.EditorLevelLibrary().spawn_actor_from_class(unreal.CineCameraActor,
                                                                          unreal.Vector(0, 0, 0),
                                                                          unreal.Rotator(0, 0, 0))
        # 指定电影摄像机CineCameraActor的名称
        camera_actor.set_actor_label(camera_name_array[index])
        # 返回CineCamera组件
        camera_component = camera_actor.get_cine_camera_component()
        ratio = math.tan(film_fov / 360.0 * math.pi) * 2

        # step1:胶片背板设置
        filmback = camera_component.get_editor_property("filmback")
        # sensor_height:胶片或数字传感器的垂直尺寸,以毫米为单位
        filmback.set_editor_property("sensor_height", 60)
        # sensor_width:胶片或数字传感器的水平尺寸,以毫米为单位
        filmback.set_editor_property("sensor_width", 60)

        # step2:镜头设置
        lens_settings = camera_component.get_editor_property("lens_settings")
        lens_settings.set_editor_property("min_focal_length", 4.0)
        lens_settings.set_editor_property("max_focal_length", 1000)
        lens_settings.set_editor_property("min_f_stop", 1.2)
        lens_settings.set_editor_property("max_f_stop", 500)
        print(lens_settings)

        # step3:聚焦设置
        focal_length = 45  # 焦距
        current_aperture = 500  # 光圈
        camera_component.set_editor_property("current_focal_length", focal_length)  # 设置当前焦距
        camera_component.set_editor_property("current_aperture", current_aperture)  # 设置当前光圈
        fov = camera_component.get_editor_property("field_of_view")
        print(camera_component.get_editor_property("current_focal_length"))
        print(camera_component.get_editor_property("current_aperture"))
        print(f"fov - {fov}")

        # add a binding for the camera
        camera_binding = sequence.add_possessable(camera_actor)
        transform_track = camera_binding.add_track(unreal.MovieScene3DTransformTrack)
        transform_section = transform_track.add_section()
        transform_section.set_start_frame_bounded(6 * index)
        transform_section.set_end_frame_bounded(6 * index + len(camera_transform_array))
        transform_section.set_start_frame_seconds(6 * index)
        transform_section.set_end_frame_seconds(6 * index + len(camera_transform_array))

        # get channel for location_x location_y location_z
        channel_location_x = transform_section.get_channels()[0]
        channel_location_y = transform_section.get_channels()[1]
        channel_location_z = transform_section.get_channels()[2]

        # get key for rotation_y rotation_z
        channel_rotation_x = transform_section.get_channels()[3]
        channel_rotation_y = transform_section.get_channels()[4]
        channel_rotation_z = transform_section.get_channels()[5]

        # camera_transform = camera_transform_array[index]
        # camera_location_x = camera_transform.get_editor_property("translation").get_editor_property("x")
        # camera_location_y = camera_transform.get_editor_property("translation").get_editor_property("y")
        # camera_location_z = camera_transform.get_editor_property("translation").get_editor_property("z")

        for i in range(len(camera_transform_array)):
            camera_transform = camera_transform_array[i]
            camera_location_x = camera_transform.get_editor_property("translation").get_editor_property("x")
            camera_location_y = camera_transform.get_editor_property("translation").get_editor_property("y")
            camera_location_z = camera_transform.get_editor_property("translation").get_editor_property("z")
            camera_rotation = camera_rotation_array[i]
            camera_rotate_roll = camera_rotation.get_editor_property("roll")
            camera_rotate_pitch = camera_rotation.get_editor_property("pitch")
            camera_rotate_yaw = camera_rotation.get_editor_property("yaw")

            new_time = unreal.FrameNumber(value=i)
            channel_location_x.add_key(new_time, camera_location_x, 0.0)
            channel_location_y.add_key(new_time, camera_location_y, 0.0)
            channel_location_z.add_key(new_time, camera_location_z, 0.0)
            channel_rotation_x.add_key(new_time, camera_rotate_roll, 0.0)
            channel_rotation_y.add_key(new_time, camera_rotate_pitch, 0.0)
            channel_rotation_z.add_key(new_time, camera_rotate_yaw, 0.0)

            # if i % 5 == 0:
            #     print(new_time, [camera_location_x, camera_location_y, camera_location_z, camera_rotate_roll, camera_rotate_pitch, camera_rotate_yaw])


        # add the binding for the camera cut section
        camera_binding_id = sequence.make_binding_id(camera_binding, unreal.MovieSceneObjectBindingSpace.LOCAL)
        camera_cut_section.set_camera_binding_id(camera_binding_id)

    # save sequence asset
    unreal.EditorAssetLibrary.save_loaded_asset(sequence, False)

    return sequence


# sequence movie
def render_sequence_to_movie(export_path, film_resolution, delay_every_frame, on_finished_callback):
    # 1) Create an instance of our UAutomatedLevelSequenceCapture and override all of the settings on it. This class is currently
    # set as a config class so settings will leak between the Unreal Sequencer Render-to-Movie UI and this object. To work around
    # this, we set every setting via the script so that no changes the user has made via the UI will affect the script version.
    # The users UI settings will be reset as an unfortunate side effect of this.
    capture_settings = unreal.AutomatedLevelSequenceCapture()

    # Set all POD settings on the UMovieSceneCapture
    # capture_settings.settings.output_directory = unreal.DirectoryPath("../../../unreal-render-release/Saved/VideoCaptures1/")
    capture_settings.settings.output_directory = unreal.DirectoryPath(export_path)

    # If you game mode is implemented in Blueprint, load_asset(...) is going to return you the C++ type ('Blueprint') and not what the BP says it inherits from.
    # Instead, because game_mode_override is a TSubclassOf<AGameModeBase> we can use unreal.load_class to get the UClass which is implicitly convertable.
    # ie: capture_settings.settings.game_mode_override = unreal.load_class(None, "/Game/AI/TestingSupport/AITestingGameMode.AITestingGameMode_C")
    capture_settings.settings.game_mode_override = None
    capture_settings.settings.output_format = "{camera}" + "_{frame}"
    capture_settings.settings.overwrite_existing = False
    capture_settings.settings.use_relative_frame_numbers = False
    capture_settings.settings.handle_frames = 0
    capture_settings.settings.zero_pad_frame_numbers = 4
    # If you wish to override the output framerate you can use these two lines, otherwise the framerate will be derived from the sequence being rendered
    capture_settings.settings.use_custom_frame_rate = False
    # capture_settings.settings.custom_frame_rate = unreal.FrameRate(24,1)
    capture_settings.settings.resolution.res_x = film_resolution
    capture_settings.settings.resolution.res_y = film_resolution
    capture_settings.settings.enable_texture_streaming = False
    capture_settings.settings.cinematic_engine_scalability = True
    capture_settings.settings.cinematic_mode = True
    capture_settings.settings.allow_movement = False  # Requires cinematic_mode = True
    capture_settings.settings.allow_turning = False  # Requires cinematic_mode = True
    capture_settings.settings.show_player = False  # Requires cinematic_mode = True
    capture_settings.settings.show_hud = False  # Requires cinematic_mode = True
    capture_settings.use_separate_process = False
    capture_settings.close_editor_when_capture_starts = False  # Requires use_separate_process = True
    capture_settings.additional_command_line_arguments = "-NOSCREENMESSAGES"  # Requires use_separate_process = True
    capture_settings.inherited_command_line_arguments = ""  # Requires use_separate_process = True

    # Set all the POD settings on UAutomatedLevelSequenceCapture
    capture_settings.use_custom_start_frame = False  # If False, the system will automatically calculate the start based on sequence content
    capture_settings.use_custom_end_frame = False  # If False, the system will automatically calculate the end based on sequence content
    capture_settings.custom_start_frame = unreal.FrameNumber(0)  # Requires use_custom_start_frame = True
    capture_settings.custom_end_frame = unreal.FrameNumber(0)  # Requires use_custom_end_frame = True
    capture_settings.warm_up_frame_count = 0
    capture_settings.delay_before_warm_up = 0
    capture_settings.delay_before_shot_warm_up = 0
    capture_settings.delay_every_frame = delay_every_frame
    capture_settings.write_edit_decision_list = True

    # Tell the capture settings which level sequence to render with these settings. The asset does not need to be loaded,
    # as we're only capturing the path to it and when the PIE instance is created it will load the specified asset.
    # If you only had a reference to the level sequence, you could use "unreal.SoftObjectPath(mysequence.get_path_name())"
    capture_settings.level_sequence_asset = unreal.SoftObjectPath(sequence_asset_path)

    # To configure the video output we need to tell the capture settings which capture protocol to use. The various supported
    # capture protocols can be found by setting the Unreal Content Browser to "Engine C++ Classes" and filtering for "Protocol"
    # ie: CompositionGraphCaptureProtocol, ImageSequenceProtocol_PNG, etc. Do note that some of the listed protocols are not intended
    # to be used directly.
    # Right click on a Protocol and use "Copy Reference" and then remove the extra formatting around it. ie:
    # Class'/Script/MovieSceneCapture.ImageSequenceProtocol_PNG' gets transformed into "/Script/MovieSceneCapture.ImageSequenceProtocol_PNG"
    capture_settings.set_image_capture_protocol_type(
        unreal.load_class(None, "/Script/MovieSceneCapture.ImageSequenceProtocol_PNG"))

    # After we have set the capture protocol to a soft class path we can start editing the settings for the instance of the protocol that is internallyc reated.
    capture_settings.get_image_capture_protocol().compression_quality = 85

    # Finally invoke Sequencer's Render to Movie functionality. This will examine the specified settings object and either construct a new PIE instance to render in,
    # or create and launch a new process (optionally shutting down your editor).
    unreal.SequencerTools.render_movie(capture_settings, on_finished_callback)


# sequence end call back
def on_render_movie_finished(success):
    unreal.log('on_render_movie_finished is called')
    if success:
        unreal.log('---end LevelSequenceTask')


# check asset is exist
def check_sequence_asset_exist(root_dir):
    sequence_path = os.path.join(root_dir, 'Content', 'Render_Sequence.uasset')
    if (os.path.exists(sequence_path)):
        unreal.log('---sequence is exist os path---')
        os.remove(sequence_path)
    else:
        unreal.log('---sequence is not exist os path---')

    if unreal.EditorAssetLibrary.does_asset_exist(sequence_asset_path):
        unreal.log('---sequence dose asset exist---')
        unreal.EditorAssetLibrary.delete_asset(sequence_asset_path)
    else:
        unreal.log('---sequence does not asset exist---')

    sequence_asset_data = unreal.AssetRegistryHelpers.get_asset_registry().get_asset_by_object_path(sequence_asset_path)
    if sequence_asset_data:
        unreal.log('---sequence is exist on content---')
        sequence_asset = unreal.AssetData.get_asset(sequence_asset_data)
        unreal.EditorAssetLibrary.delete_loaded_asset(sequence_asset)
    else:
        unreal.log('---sequence is not exit on content---')


def main():
    # bind event
    on_finished_callback = unreal.OnRenderMovieStopped()
    on_finished_callback.bind_callable(on_render_movie_finished)

    # get json file
    root_dir = unreal.SystemLibrary.get_project_directory()
    json_path = os.path.join(root_dir, 'RawData', 'camera_data.json')

    # get json data
    camera_transform_array, camera_rotation_array, camera_name_array, export_path, film_fov, film_resolution, delay_every_frame = read_json_file(
        json_path)

    # check sequence is asset
    check_sequence_asset_exist(root_dir)

    # create sequence
    create_sequence('Render_Sequence', camera_transform_array, camera_rotation_array, camera_name_array, film_fov)

    # run sequence
    render_sequence_to_movie(export_path, film_resolution, delay_every_frame, on_finished_callback)

3. 渲染结果

上图即为渲染的多视点图片,每张图片对应的相机姿态也是确定的。

至此,在 UE4 中利用 python 实现自动化渲染的整个过程就结束了。在这里留下一个小问题:

UE4 中的相机姿态数据我们是无法直接使用的,如何将其转化为 NeRF 中的相机姿态数据呢?大家可以自行探索一下,后续文章再对这一部分的内容进行讲解。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python-ue4-metahuman-nerf:我创造了一个数字人!! 的相关文章

随机推荐

  • uniapp弹幕滚动到底部

    发布的弹幕至于最底部
  • 【linux】linux 离线安装 curl命令

    文章目录 1 概述 2 curl安装步骤 3 验证 原创不易 且行且珍惜 1 概述 最近在忙一个艰苦的环境 没有yarn界面 没有flink界面 没有es界面 没有kibana界面 条件艰苦 且行且艰险 这个环境发现es日志不入库 然后查看
  • 内网渗透工具-反向代理FRP

    内网渗透工具 反向代理FRP 0x1 简介 FRP是一个比较流行而且成熟的内网渗透工具 支持 TCP UDP HTTP HTTPS 等多种协议 0x2 前期准备 工具准备 可在官方github仓库下载 https github com fa
  • ‘mvn‘不是内部或外部命令

    解决方案有两种 一 1 如果没有安装maven 在IDEA中使用maven 提示mvn不是内部命令 需要在环境变量中的用户变量的Path中添加maven的bin路径 重启下IDEA即可 1 环境变量 用户 2 Path 添加IDEA下的ma
  • Pytorch框架下训练网络的代码结构

    PyTorch 是一个基于 Torch 的 Python 开源机器学习库 用于自然语言处理等应用程序 它主要由 Facebook 的人工智能研究小组开发 PyTorch 提供两个高级功能 1 具有强大的 GPU 加速的张量计算 如 NumP
  • TCP/IP网络编程(6)

    1 IO复用 并发服务器的实现方法 在网络程序中 数据通信时间比CPU运算时间占比更大 因此 采用并发的形式向多个客户端提供服务是一种有效利用CPU的方式 并发服务器的主要实现模型及方法如下所示 多进程服务器 通过常见多个进程提供服务 多路
  • 内存泄漏3____内存泄漏, 内存溢出的区别与关系__内存抖动

    泄漏 memory leak 是指程序在申请内存后 无法释放已申请的内存空间 一次内存泄露危害可以忽略 但内存泄漏堆积后 会变得很严重 无论有多少空间 迟早会被占光 memory leak 最终会导致 OOM out of memory 看
  • web前端三大核心技术

    web前端三大核心技术 根据 W3C 标准 一个网页主要由三部分组成 结构 表现和行为 结构 超文本标记语言 HTML Hyper Text Markup Language HTML用于描述页面的结构 html5 是一门标记型语言 主要由一
  • 列存数据仓库怎样更高效

    很多数据仓库产品都采用了列式存储 如果数据表的总列数很多而计算涉及的列很少 采用列存就只读取需要的列即可 能够减少硬盘访问量 提高性能 特别是数据量非常大时 硬盘扫描和读取的时间占比很大 这时候列存的优势会很明显 那么 是不是只要用了列存就
  • 单链表的建立(C语言):头插法和尾插法建立单链表

    采用头插法建立单链表 该方法从一个空表开始 生成新结点 并将读取到的数据存放到新结点的数据域中 然后将新结点插入到当前链表的表头 即头结点之后 如图2 4所示 图2 4 头插法建立单链表 头插法建立单链表的算法如下 LinkList Cre
  • KMP算法是怎么被设计出来的

    定义 我们假设要在主串中寻找子串出现的所有位置 我们记主串中的开始位置为匹配位置 如在 abc 中匹配 bc 则匹配位置为 2 暴力 我们把匹配过程拆解为 枚举匹配位置 验证主串从匹配位置开始是否一一匹配子串 以此 有显然的 O n m
  • layui table 列覆盖

    layui table 列个数超过10列后 表头发生覆盖现象 临时解决方法 注释掉下面代码 if left div class layui table fixed layui table fixed l div class layui ta
  • DevC++小白程序-一元二次方程

    DevC 小白程序 一元二次方程 include
  • 智能化应用爆发,银行如何打造敏捷响应的智能中枢?

    在宏观经济增速放缓和互联网金融发展的双重压力下 银行已普遍开始了数字化转型 并向智能化方向迈进 不过在智能化转型过程中 银行也普遍面临数字资产建设能力不足 数据治理和AI模型开发自动化程度低等问题 银行需要搭建起适应未来的敏捷响应的智能中枢
  • js textarea焦点事件,CodeMirror焦点事件

    1 效果图 2 功能说明 当我把光标放在 指标对象 的控件框的时候 双击 字段名称 则字段数据写入指标对象控件框 当我把光标放在 指标维度 的控件框的时候 双击 字段名称 则字段数据写入指标维度控件框 当我把光标放在 指标sql 的控件框的
  • ElementUI表格错位问题

    最近一个项目需要用到表格 为了图省事直接用的ElementUI组件 使用默认的参数在表格所在的容器大小变化时会发生错位问题 如下图所示 网上的一些方法基本都试过了 没啥作用 可能是方式不对 最后查询ElementUI文档 在el table
  • 开关稳压电源设计

    文末下载完整资料 摘要 本设计应用隔离型回扫式DC DC电源变换技术完成开关稳压电源的设计及制作 系统主要由整流滤波电路 DC DC变换电路 单片机显示与控制电路三部分组成 开关电源的集成控制由脉宽调制控制芯片UC3843及相关电路完成 利
  • linux编程第一部分总结

    C 多线程安全原则 对象析构很复杂 我们采用shared ptr和weak ptr来做 enable shared from this lt gt 是用来做回调的 因为多线程中可能对象的生命周期比传出去的this指针短 同时为了不延长对象的
  • 碎片笔记

    前言 与传统的AI攻防 后门攻击 对抗样本 投毒攻击等 不同 如今的大模型攻防涉及以下多个方面的内容 目录 一 大模型的可信问题 1 1 虚假内容生成 1 2 隐私泄露 二 大模型的安全问题 2 1 模型窃取攻击 2 2 数据窃取攻击 2
  • python-ue4-metahuman-nerf:我创造了一个数字人!!

    原文 https zhuanlan zhihu com p 561152870 目录 收起 1 准备工作 制作 MetaHuman 角色 1 1 创建 MetaHuman 角色 1 2 Quixel Bridge 下载 MetaHuman