LoginSignup
8
7

More than 3 years have passed since last update.

[UE4]Unreal Python実行時に任意の引数を変数として渡す

Last updated at Posted at 2019-12-24

Unreal Pythonを実行するとき、状況によってPythonスクリプト内の変数を変えたい場合があると思います。
例えばアクタを10個スポーンさせるスクリプトがあったとして、8個にしたり20個にしたり調整したいときに毎回スクリプトを書き換えるのはスマートではありません。
そんな場合はargvを使います。

argv

sysライブラリの関数。
実行時に引数を付けることで変数として取得できる。

qiita.py
import sys
print(sys.argv[1])

一例として、以下のようなスクリプトを作ってみました。

選択アクタのライトマップ解像度を調整

※numpyライブラリのインストールが必要です。
外部ライブラリのインストールについては以下の記事を参考にしてください。
https://qiita.com/EGJ-Hiroyuki_Kobayashi/items/1f8472a77e322704d603

qiita.py
import unreal
import sys
import numpy as np

#選択したアクタを取得
selected_actors = unreal.EditorLevelLibrary.get_selected_level_actors()
selected_static_mesh_actors = unreal.EditorFilterLibrary.by_class(selected_actors,unreal.StaticMeshActor.static_class())

static_meshes = np.array([])

#配列のユニーク化
for sma in selected_static_mesh_actors:
    smc = sma.static_mesh_component
    sm = smc.static_mesh
    static_meshes = np.append(static_meshes,sm)

static_meshes = np.unique(static_meshes)

#引数から値を取得
offset = float(sys.argv[1])

for sm in static_meshes:
    #現在のライトマップ解像度を取得
    current_resolution = sm.get_editor_property("light_map_resolution")
    #新しいライトマップ解像度を設定
    log = unreal.MathLibrary.log(current_resolution,2)
    new_resolution = unreal.MathLibrary.multiply_multiply_float_float(2,log + offset)
    sm.set_editor_property('light_map_resolution',new_resolution)

ライトマップ解像度を1段階あげたい時は引数に1を付ける。(256 → 512など)
1段階下げたい場合は引数に-1を付ける。(512 → 256)
2019-08-07_11h51_47.png
こんな感じで使えます。

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7