Python スクリプティング を使用して、シーケンサー のさまざまな部分を自動化して制御できます。このドキュメントでは、シーケンサーで Python を使用する主な方法の概要を、汎用的なシーケンサー スクリプトの例とともに説明します。
前提条件
- Unreal Engine で Python スクリプティング を使用した経験があること。
- シーケンサー の使用方法について理解している。
シーケンサーでの Python 用語
シーケンサーでは次の用語が使用されます。
コントロールリグの Python の用語 | 説明 |
---|---|
World (ワールド) | アクタとコンポーネントが存在し、それらをレンダリングすることができるマップを表すオブジェクトで、Level (レベル) とも呼ばれます。 |
LevelSequence (レベル シーケンス) | シネマティックス シーンのコンテナであるアセットで、Sequence (シーケンス) とも呼ばれます。レベル シーケンスには、さまざまなオブジェクトにバインドしてそれらをアニメートできるデータとトラックが含まれます。 |
SequencerBindingProxy (シーケンサー バインディング プロキシ) | レベル シーケンスによってバインドされているアクタまたはコンポーネントを定義する構造体で、Binding (バインディング) とも呼ばれます。 |
Possessable (所有可能) | アニメート可能なあらゆるプロパティをレベル シーケンスで所有することができるレベル内に存在するアクタ、またはコンポーネントを表すバインディングのタイプです。 |
Spawnable (スポーン可能) | シーケンスの再生中のみに 存在するアクタまたはコンポーネントを表すバインディングのタイプです。 |
MovieSceneTrack (ムービー シーン トラック) | バインディング (SequencerBindingProxy) の下位に位置するオブジェクトで、特定タイプのプロパティに対する編集のすべてのセクションが含まれます。例えば、MovieScene3DTransformTrack > Actor / Component Transform です。 |
MovieSceneSection (ムービー シーン セクション) | トラック (MovieSceneTrack) の下位に位置するオブジェクトで、特定タイプのプロパティのすべてのチャンネル、長さ、パラメータが含まれます。例えば、MovieScene3DTransformSection > Pre / Post Roll、When Finished State、Active/Muted、Additive です。 |
MovieSceneScriptingChannel (ムービー シーン スクリプティング チャンネル) | セクション (MovieSceneSection) の下位に位置するオブジェクトで、特定タイプのプロパティまたはサブプロパティをアニメートするすべてのキーフレームが含まれます。例えば、MovieSceneScriptingFloatChannel > Location.X です。 |
MovieSceneScriptingKey (ムービー シーン スクリプティング キー) | 特定タイプのチャンネルでキーフレームを表すオブジェクトです。例えば、MovieSceneScriptingFloatKey です。 |
FrameNumber (フレーム番号) | フレームを表す構造体です。 |
FrameRate (フレーム レート) | フレームと秒を定義する 2 つの整数値の割合を表す構造体です。例えば、1 秒あたり 30 フレームは 30/1 と表現されます。 |
レベル シーケンスにアクセスする
シーケンサーでの Python スクリプティングを行うには、最初に、これから最も頻繁にやり取りする主要なオブジェクトである LevelSequence にアクセスします。状況に応じて、これを行う方法がいくつかあります。
シンプルなアクセス
コンテンツ ブラウザ 内に存在するレベル シーケンスにアクセスするには、次のサンプル スクリプトを使用できます。目的のシーケンスは現在開かれている必要はなく、現在のレベル内に存在している必要もありません。このスクリプトでは、レベル シーケンス アセットがルート コンテンツ フォルダに含まれていることを想定しています。
import unreal
# Get a level sequence asset
level_sequence = unreal.load_asset("/Game/LevelSequenceName")
# Then open it in Sequencer
unreal.LevelSequenceEditorBlueprintLibrary.open_level_sequence(level_sequence)
Access Current Level Sequence
You can also access a currently-opened Level Sequence using the following script:
import unreal
# 現在のレベル シーケンスにアクセスする
現在開かれているレベル シーケンスにアクセスするには、次のスクリプトを使用できます。
レベル シーケンスを作成して開く
新しいレベル シーケンス アセットを作成して開くには、次のスクリプトを使用できます。
import unreal
# Get asset tools
asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
# Create a Level Sequence with name LevelSequenceName in root content folder
level_sequence = unreal.AssetTools.create_asset(asset_tools, asset_name = "LevelSequenceName", package_path = "/Game/", asset_class = unreal.LevelSequence, factory = unreal.LevelSequenceFactoryNew())
レベル シーケンスをクエリして編集する
Python でレベル シーケンスにアクセスしたら、変更を加えることができます。シーケンスに対してはさまざまな方法で影響を及ぼすことができますが、その一部の例を以下に示します。
フレーム レートを変更する
レベル シーケンスは、デフォルトで 30 フレーム/秒 (fps) で再生されます。この再生レートを変更するには、次のコマンドを使用できます。
# Create a frame rate object and set to the desired fps number
frame_rate = unreal.FrameRate(numerator = 60, denominator = 1)
# Set the display rate
level_sequence.set_display_rate(frame_rate)
開始時間と終了時間を変更する
デフォルトでは、シーケンスの再生範囲の開始はフレーム 0 に、終了はフレーム 150 に設定されています (30fps のフレーム レートを想定)。次のコマンドを使用して、開始フレームと終了フレームの両方を調整できます。
# Set the playback range to 20-200
level_sequence.set_playback_start(20)
level_sequence.set_playback_end(200)
アクタを追加する
現在のレベルからアクタを追加して、これをシーケンサーで所有させるには、次のコマンドを使用します。
# Get the Actor subsystem to grab a selected actor
actor_system = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
# Get the selected actor
actor = actor_system.get_selected_level_actors()[0]
# Add actor to level as a possessable
actor_binding = level_sequence.add_possessable(actor)
# Refresh to visually see the new binding added
unreal.LevelSequenceEditorBlueprintLibrary.refresh_current_level_sequence()
シーケンサーでは、レベルからの現在のアクタを所有する代わりに、シーケンス期間中での使用に向けてアクタを新しく スポーン することができます。この場合は、オブジェクト を受け取る add_spawnable_from_instance
か、クラス を必要とする add_spawnable_from_class
のいずれかを使用できます。次の各コマンドを使用して、スポーン可能なものをシーケンスに追加します。
# Get the Actor subsystem to grab a selected actor
actor_system = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
# Get the selected actor
actor = actor_system.get_selected_level_actors()[0]
# Add actor to level as a spawnable
actor_binding = level_sequence.add_spawnable_from_instance(actor)
# Refresh to visually see the new binding added
unreal.LevelSequenceEditorBlueprintLibrary.refresh_current_level_sequence()
トラックとセクションを作成する
Python スクリプティングを使って トラック と セクション を追加することもできます。それぞれのトラック タイプはセクション タイプを示します。次に例を挙げます。
-
トランスフォーム トラック が
unreal.MovieScene3DTransformTrack
として定義され、そのセクションではunreal.MovieScene3DTransformSection
を使用します。 -
スケルタル メッシュ アニメーション トラック が
unreal.MovieSceneSkeletalAnimationTrack
として定義され、そのセクションではunreal.MovieSceneSkeletalAnimationSection
を使用します。
トラックとセクションを追加するには、次のコマンドを使用します。
# Use the binding to add tracks into sequencer - specified by track type
transform_track = actor_binding.add_track(unreal.MovieScene3DTransformTrack)
anim_track = actor_binding.add_track(unreal.MovieSceneSkeletalAnimationTrack)
# Add section to track to be able to manipulate range, parameters, or properties
transform_section = transform_track.add_section()
anim_section = anim_track.add_section()
# Get level sequence start and end frame
start_frame = level_sequence.get_playback_start()
end_frame = level_sequence.get_playback_end()
# Set section range to level sequence start and end frame
transform_section.set_range(start_frame, end_frame)
anim_section.set_range(start_frame, end_frame)
# Refresh to visually see the new tracks and sections added
unreal.LevelSequenceEditorBlueprintLibrary.refresh_current_level_sequence()
一部のセクションでは、セクション内でプロパティを定義する必要があります。例えば、アニメーション トラック セクションではアニメーション アセットを定義する必要があります。そのためには次のコマンドを使用します。
# Get the animation sequence asset
anim_seq = unreal.load_asset("/Game/Mannequin/Animations/ThirdPersonWalk")
# Get the section, get the parameters, set animation to anim sequence asset
anim_section.params.animation = anim_seq
トラックのフィルタリング
トラックのフィルタリング コマンドを使用することもできます。
# Get track filter names and print them
track_filter_names = unreal.LevelSequenceEditorBlueprintLibrary.get_track_filter_names()
for track_filter_name in track_filter_names:
print(track_filter_name)
# Set the track filter for Skeletal Mesh and Selected Control Rig Controls
unreal.LevelSequenceEditorBlueprintLibrary.set_track_filter_enabled("Skeletal Mesh", True)
unreal.LevelSequenceEditorBlueprintLibrary.set_track_filter_enabled("Selected Control Rig Controls", True)
# See the filter enabled status per track
print(unreal.LevelSequenceEditorBlueprintLibrary.is_track_filter_enabled("Event"))
print(unreal.LevelSequenceEditorBlueprintLibrary.is_track_filter_enabled("Skeletal Mesh"))
その他のシーケンサー スクリプティング リソース
全般的なシーケンサー Python スクリプティングのさらなる資料については、次のローカル エンジン パスに含まれるシーケンサー スクリプティングの例を参照してください。
…\Engine\Plugins\MovieScene\SequencerScripting\Content\Python