UE4 Python批量渲染Sequence
參考文獻:https://blog.l0v0.com/posts/2f3a9b95.html
(該功能需要大家對UE4 Python能夠進行簡單的使用,所以不會的同學,請謹慎閱讀)
最近公司的專案,需要批量渲染Sequence,輸出格式為.avi,但截止UE4.26,我沒有找到官方合適的解決插件,所以有了這個檔案,
此檔案主要參考UE4 Python的官方示例,參考文獻幫助我解決了回圈渲染,在此對參考文獻作者表示衷心的感謝,
話不多說,以下是我的代碼
"""
說明:使用UE4 Python批量渲染Sequence,輸出格式為.avi
作者:鐘小二寶
E-mail:1023462838@qq.com
時間:2021年3月1日
"""
import unreal
import json
import os
import subprocess
def render_sequence_to_movie(i,output_directory,output_format):
# instance of unreal classes
system_lib = unreal.SystemLibrary()
editor_util = unreal.EditorUtilityLibrary()
capture_settings = unreal.AutomatedLevelSequenceCapture()
burn_in_options = unreal.LevelSequenceBurnInOptions()
# 獲取選擇的所有assets
selected_assets = editor_util.get_selected_assets()
sequence_list = [asset for asset in selected_assets if isinstance(asset,unreal.LevelSequence)]
# 如果超出陣列則退出執行
if i >= len(sequence_list):
# 輸出完成 打開輸出檔案夾的路徑
subprocess.call(["start","",output_directory], creationflags=0x08000000,shell=True)
return
# 獲取當前渲染序號下的 LevelSequence
sequence = sequence_list[i]
set_capture_settings(sequence, i,output_directory,output_format,system_lib,capture_settings)
set_burn_in_options(output_directory,output_format,burn_in_options)
capture_settings.burn_in_options = burn_in_options
global on_finished_callback
on_finished_callback = unreal.OnRenderMovieStopped(
lambda s:render_sequence_to_movie(i+1,output_directory,output_format))
unreal.SequencerTools.render_movie(capture_settings, on_finished_callback)
def set_capture_settings(sequence,i,output_directory,output_format,system_lib,capture_settings):
# 在UMovieSceneCapture上設定所有POD設定
capture_settings.settings.output_directory = unreal.DirectoryPath(output_directory)
capture_settings.settings.game_mode_override = None
# 輸出視頻的檔案名(我使用了LevelSequence的名字)
capture_settings.settings.output_format = "{}".format(system_lib.get_object_name(sequence))
capture_settings.settings.overwrite_existing = True
capture_settings.settings.use_relative_frame_numbers = False
capture_settings.settings.handle_frames = 0
capture_settings.settings.zero_pad_frame_numbers = 4
# 渲染幀率設定,默認使用幀率為渲染序列中的幀率
# capture_settings.settings.use_custom_frame_rate = True
# capture_settings.settings.custom_frame_rate = unreal.FrameRate(24,1)
# 渲染視頻輸出解析度設定
capture_settings.settings.resolution.res_x = 1280
capture_settings.settings.resolution.res_y = 720
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
# 在UAutomatedLevelSequenceCapture上設定所有POD設定
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.0
capture_settings.delay_before_warm_up = 0
capture_settings.delay_before_shot_warm_up = 0.0
capture_settings.write_edit_decision_list = True
# 選擇需要渲染的LevelSequence
capture_settings.level_sequence_asset = unreal.SoftObjectPath(sequence.get_path_name())
capture_settings.set_image_capture_protocol_type(unreal.load_class(None, "/Script/MovieSceneCapture.VideoCaptureProtocol"))
def set_burn_in_options(output_directory,output_format,burn_in_options):
# 是否啟用視頻刻錄
burn_in_options.use_burn_in = False
burn_in_options.set_burn_in(unreal.SoftClassPath("/Engine/Sequencer/DefaultBurnIn.DefaultBurnIn_C"))
burn_in_options.settings.set_editor_property('TopLeftText', "{FocalLength}mm,{Aperture},{FocusDistance}")
burn_in_options.settings.set_editor_property('TopCenterText', "{MasterName} - {Date} - {EngineVersion}")
burn_in_options.settings.set_editor_property('TopRightText', "{TranslationX} {TranslationY} {TranslationZ}, {RotationX} {RotationY} {RotationZ}")
burn_in_options.settings.set_editor_property('BottomLeftText', "{ShotName}")
burn_in_options.settings.set_editor_property('BottomCenterText', "{hh}:{mm}:{ss}:{ff} ({MasterFrame})")
burn_in_options.settings.set_editor_property('BottomRightText', "{ShotFrame}")
# 加載水印地址
burn_in_options.settings.set_editor_property('Watermark', unreal.load_asset("/Engine/EngineResources/AICON-Green"))
# 創建一個FLinearColor著色水印
burn_in_options.settings.set_editor_property('WatermarkTint', unreal.LinearColor(1.0, 0.5, 0.5, 0.5))
def batch_render_sequence_to_movie():
# 渲染輸出檔案夾
output_directory="C:/render"
output_format="{sequence}"
render_sequence_to_movie(0,output_directory,output_format)
def main():
batch_render_sequence_to_movie()
if __name__ == "__main__":
main()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/265394.html
標籤:python
上一篇:python csdn自動關注
