【UE×Python】コンテンツ内のアセットを読み込む方法およびオブジェクトデータを表示する方法について徹底解説(SM_Cube)

Python

「unreal」(Unreal Engine Python API)は、Unreal EngineのPythonライブラリです。このAPIを使用することで、Unreal Engineをより簡単にカスタマイズすることができます。Pythonは、様々なプログラミング言語で使用される汎用的なスクリプト言語であり、「unreal」はPythonを使用して構築されています。本記事では、「unreal」を用いたコンテンツ内のアセットを読み込む方法およびオブジェクトデータを表示する方法を紹介します。

# load_asset関数によるコンテンツ内のアセットの読み込み
unreal.load_asset(アセットのパス)

下記の様な内容で悩んでいる/困っている場合に使える方法を参考までにご共有させて頂きます。

・Unreal Engine上で、どの様にPythonを使うの?
・Unreal Engine上のPythonで、どの様にコンテンツ内のアセットの読み込むの?また、どの様にオブジェクトデータの表示するの?

また、「unreal」を使用する上で基礎的な情報は下記の記事で紹介しております。
他の「unreal」のクラスや関数について気になる方はこちらの記事をご覧ください。

unreal.load_asset関数によるコンテンツ内のアセットの読み込み/print関数によるデータの表示

unreal.load_asset関数の基本構文

unreal.load_asset(
	name: str, 
	type: Union[Class, type] = Object.static_class(), 
	follow_redirectors: bool = True
) 
→ Any

unreal.load_asset関数の使い方

import unreal

cube_asset = unreal.load_asset('/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube')

### Attribute of "cube_asset"
for attribute in dir(cube_asset): print(attribute)

### Help of "cube_asset"
help(cube_asset)

### Information of "cube_asset"
print(cube_asset.get_class())
print(cube_asset.get_fname())
print(cube_asset.get_path_name())
LogPython: __class__
LogPython: __delattr__
LogPython: __dir__
...
LogPython: set_num_source_models
LogPython: static_class
LogPython: static_materials
LogPython: Help on StaticMesh object:
class StaticMesh(StreamableRenderAsset)
 |  A StaticMesh is a piece of geometry that consists of a static set of polygons.
 |  Static Meshes can be translated, rotated, and scaled, but they cannot have their vertices animated in any way. As such, they are more efficient
 |  to render than other types of geometry such as USkeletalMesh, and they are often the basic building block of levels created in the engine.
 |  see: https://docs.unrealengine.com/latest/INT/Engine/Content/Types/StaticMeshes/
 |  see: AStaticMeshActor, UStaticMeshComponent
...
 |  Static methods inherited from _ObjectBase:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
LogPython: <Object '/Script/Engine.StaticMesh' (0x00000B0A93F40300) Class 'Class'>
LogPython: Shape_Cube
LogPython: /Game/StarterContent/Shapes/Shape_Cube.Shape_Cube

特定のオブジェクトデータの内容の変更

変更前

### Information of "cube_asset"
cube_material = cube_asset.get_material(0)
print(cube_material)
print(cube_material.get_class())
print(type(cube_material))

cube_lod_for_collision = cube_asset.get_editor_property("lod_for_collision")
print(cube_lod_for_collision)
print(type(cube_lod_for_collision))
LogPython: <Object '/Game/StarterContent/Materials/M_Basic_Wall.M_Basic_Wall' (0x00000B0B50CD3C00) Class 'Material'>
LogPython: <Object '/Script/Engine.Material' (0x00000B0A93EB1B00) Class 'Class'>
LogPython: <class 'Material'>

LogPython: 0
LogPython: <class 'int'>

変更処理

### Change Information
new_material = unreal.load_asset('/Game/StarterContent/Materials/M_Basic_Floor.M_Basic_Floor')
cube_asset.set_material(0, new_material)

new_lod_for_collision = 1
cube_asset.set_editor_property("lod_for_collision", new_lod_for_collision)

変更後

### Information of "cube_asset"

print(new_material.get_class())
print(type(new_material))
print(cube_asset.get_material(0))

print(cube_asset.get_editor_property("lod_for_collision"))
LogPython: <Object '/Script/Engine.Material' (0x00000B0A93EB1B00) Class 'Class'>
LogPython: <class 'Material'>
LogPython: <Object '/Game/StarterContent/Materials/M_Basic_Floor.M_Basic_Floor' (0x00000B0B5AEB6C00) Class 'Material'>

LogPython: 1

まとめ

「unreal」(Unreal Engine Python API)は、Pythonを使ったUnreal Engineの開発に非常に役立ちます。この記事では、コンテンツ内のアセットを読み込む方法およびオブジェクトデータを表示する方法を紹介しました。ぜひ活用してみてください。

関連検索ワード

How to read asset in content browser?
How to show data of object?

関連キーワード

unreal, python, read, show, 読み込み, 表示