はじめに
UE5.3 から、Python を使う時にデバッグ方法です。
とりあえず、Python 系の Plugin を有効にします
設定
Unreal プロジェクト は、Config/DefaultEngine.ini
Unreal Plugin は、 Config/Engine.ini
に下記の設定を追加します。
Engine.ini
[/Script/PythonScriptPlugin.PythonScriptPluginSettings]
+StartupScripts=EditorStartup.py
bIsolateInterpreterEnvironment=False
bDeveloperMode=True
bRemoteExecution=False
RemoteExecutionMulticastGroupEndpoint=239.0.0.1:6766
RemoteExecutionMulticastBindAddress=127.0.0.1
RemoteExecutionSendBufferSizeBytes=2097152
RemoteExecutionReceiveBufferSizeBytes=2097152
RemoteExecutionMulticastTtl=0
Content/Python/EditorStartup.py
が UnrealEditor 起動時に呼ばれます。
Python デバッグする為に、
bIsolateInterpreterEnvironment=False
を設定する必要があります。
Python ワークスペース
Python のソースコードは、
Content/Python に置きます
EditorStartup.py、Test.py を下記のように記述します。
EditorStartup.py
# Content/Python/EditorStartup.py ― Editor 起動時に 1度だけ走らせる
import os, sys, unreal, pathlib
# Engine に同梱されている “本物” の python.exe を取得
engine_dir = unreal.Paths.engine_dir() # 例: C:/Epic Games/UE_5.3/Engine/
py_exe = os.path.join(
engine_dir, "Binaries", "ThirdParty", "Python3", "Win64", "python.exe"
)
# まだ差し替えていなければ上書き
if sys.executable.lower().endswith("unrealeditor.exe") and os.path.isfile(py_exe):
sys.executable = py_exe
# 以降で debugpy を読み込むと、adapter は python.exe で起動する
import debugpy
if not getattr(debugpy, "_listening", False):
debugpy.listen(("0.0.0.0", 5678))
debugpy._listening = True
unreal.log("debugpy listening on 5678 in main editor process…")
Test.py
import unreal
_conunter = 0
def test_func():
global _conunter
_conunter += 1
unreal.log(f"test_func: {_conunter}")
.vscode/launch.json を下記のように記述します
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Unreal Python",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "127.0.0.1",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "${workspaceFolder}"
}
],
}
]
}
UnrealEditor を起動します
VSCode の実行ボタン(Attach to Unreal Python)を押します
Unreal から、Test.py の test_func() を呼び出します
OutputLog から Cmd => Python に切り替えて、Test.test_func を呼び出す方法
BluePrint から Test.test_func を呼び出す方法
import Test
Test.test_func()
Test.py のブレークポイント止まりました
補足 Test.py の編集が反映されない場合
OutputLog の Python から、または、ブループリントから
下記のコードを呼び出してください
import sys
if "Test" in sys.modules:
del sys.modules["Test"]