forked from tacyan/UE5-MCP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathue5_integration.py
More file actions
574 lines (473 loc) · 22.9 KB
/
Copy pathue5_integration.py
File metadata and controls
574 lines (473 loc) · 22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
UE5-MCP統合スクリプト
このスクリプトは、Unreal Engine 5.5のPythonインタプリタから実行され、
MCPサーバーと連携してMCPフレームワークを使用できるようにします。
主な機能:
- MCPサーバーへの接続
- UE5でのアセット管理(インポート、配置など)
- AIを活用したゲームロジックの生成
- Blenderからのアセット移行のサポート
使用方法:
1. UE5エディタを起動
2. Python スクリプトエディタでこのファイルを開く
3. 実行ボタンを押してスクリプトを実行
制限事項:
- Unreal Engine 5.1以上が必要です
- Python Editor Script Pluginが有効になっている必要があります
- MCPサーバーが事前に起動している必要があります
"""
import unreal
import json
import os
import sys
import requests
import time
import logging
import tempfile
from pathlib import Path
# ロギングの設定
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler()
]
)
logger = logging.getLogger("ue5_integration")
class UE5MCPIntegration:
"""
UE5とMCP連携のためのクラス
"""
def __init__(self, server_host="127.0.0.1", server_port=8000):
"""
初期化メソッド
引数:
server_host (str): MCPサーバーのホスト
server_port (int): MCPサーバーのポート
"""
self.server_url = f"http://{server_host}:{server_port}"
self.import_dir = os.path.join(tempfile.gettempdir(), "ue5_mcp_imports")
# インポートディレクトリが存在しなければ作成
if not os.path.exists(self.import_dir):
os.makedirs(self.import_dir)
# UE5バージョンを取得
self.ue_version = unreal.SystemLibrary.get_engine_version()
logger.info(f"MCPサーバーURL: {self.server_url}")
logger.info(f"インポートディレクトリ: {self.import_dir}")
logger.info(f"Unreal Engine バージョン: {self.ue_version}")
# 通知スタイルの設定
self.notification_style = unreal.EditorNotificationController().notification_style
self.notification_style.fade_in_duration = 0.5
self.notification_style.fade_out_duration = 0.5
self.notification_style.expire_duration = 5.0
def connect_to_server(self):
"""
MCPサーバーへの接続を確認する
戻り値:
bool: 接続成功したかどうか
"""
try:
logger.info("MCPサーバーへの接続を確認しています...")
self.show_notification("MCPサーバーへの接続を確認中...")
response = requests.get(f"{self.server_url}/status")
if response.status_code == 200:
status_data = response.json()
server_status = status_data.get("status", "unknown")
if server_status == "running":
logger.info("MCPサーバーに正常に接続しました")
self.show_notification("MCPサーバーに正常に接続しました", success=True)
return True
else:
logger.warning(f"MCPサーバーのステータスが異常: {server_status}")
self.show_notification(f"MCPサーバーのステータスが異常: {server_status}", success=False)
return False
else:
logger.error(f"MCPサーバーへの接続に失敗しました: {response.status_code}")
self.show_notification(f"MCPサーバーへの接続に失敗しました: HTTP {response.status_code}", success=False)
return False
except Exception as e:
logger.exception(f"MCPサーバーへの接続中にエラーが発生しました: {str(e)}")
self.show_notification(f"MCPサーバーへの接続中にエラーが発生しました", success=False)
return False
def show_notification(self, message, success=True):
"""
UE5に通知を表示する
引数:
message (str): 表示するメッセージ
success (bool): 成功メッセージかどうか
"""
try:
controller = unreal.EditorNotificationController()
self.notification_style.text_color = unreal.LinearColor(0.0, 1.0, 0.0, 1.0) if success else unreal.LinearColor(1.0, 0.0, 0.0, 1.0)
controller.display_notification(message, self.notification_style)
unreal.log(message)
except Exception as e:
logger.error(f"通知表示中にエラーが発生しました: {str(e)}")
def create_level(self, level_name, template="Empty"):
"""
新しいレベルを作成する
引数:
level_name (str): レベル名
template (str): テンプレート名("Empty", "VR-Basic", "ThirdPerson"など)
戻り値:
dict: 作成結果
"""
try:
self.show_notification(f"レベル '{level_name}' を作成中...")
# テンプレートのマッピング
template_map = {
"Empty": "/Engine/Maps/Templates/Template_Default",
"ThirdPerson": "/Engine/Maps/Templates/ThirdPerson",
"FirstPerson": "/Engine/Maps/Templates/FirstPerson",
"TopDown": "/Engine/Maps/Templates/TopDown",
"Puzzle": "/Engine/Maps/Templates/Puzzle",
"VR-Basic": "/Engine/Maps/Templates/VR-Basic"
}
# テンプレートが存在するか確認
template_path = template_map.get(template)
if not template_path:
error_msg = f"未知のテンプレート: {template}"
logger.error(error_msg)
self.show_notification(error_msg, success=False)
return {"status": "error", "message": error_msg}
# 新しいレベルを作成
level_path = f"/Game/Levels/{level_name}"
# すでに存在する場合は警告
asset_registry = unreal.AssetRegistryHelpers.get_asset_registry()
existing_asset = asset_registry.get_asset_by_object_path(level_path)
if existing_asset:
warning_msg = f"レベル '{level_name}' は既に存在します。"
logger.warning(warning_msg)
self.show_notification(warning_msg, success=False)
return {"status": "warning", "message": warning_msg}
# 実際のレベル作成ロジック
# UE5のPythonではエディタレベルの作成APIが制限されているため、
# 通常はC++側のAPIを呼び出すか、エディタコマンドを実行する
# ここではエディタコマンドレットを使用
unreal.log(f"レベル '{level_name}' をテンプレート '{template}' から作成しています...")
unreal.EditorLevelLibrary.new_level(level_path)
# 成功を通知
success_msg = f"レベル '{level_name}' を作成しました"
logger.info(success_msg)
self.show_notification(success_msg, success=True)
return {
"status": "success",
"message": success_msg,
"level_info": {
"name": level_name,
"path": level_path,
"template": template
}
}
except Exception as e:
error_msg = f"レベル作成中にエラーが発生しました: {str(e)}"
logger.exception(error_msg)
self.show_notification(error_msg, success=False)
return {"status": "error", "message": error_msg}
def import_asset(self, file_path, destination_path="/Game/Assets"):
"""
アセットをインポートする
引数:
file_path (str): インポートするファイルのパス
destination_path (str): インポート先のパス
戻り値:
dict: インポート結果
"""
try:
# ファイルが存在するか確認
if not os.path.exists(file_path):
error_msg = f"インポートするファイルが見つかりません: {file_path}"
logger.error(error_msg)
self.show_notification(error_msg, success=False)
return {"status": "error", "message": error_msg}
# ファイル情報を取得
file_name = os.path.basename(file_path)
file_extension = os.path.splitext(file_name)[1].lower()
base_name = os.path.splitext(file_name)[0]
# サポートされているファイル形式か確認
supported_extensions = [".fbx", ".obj", ".gltf", ".glb", ".uasset"]
if file_extension not in supported_extensions:
error_msg = f"未サポートのファイル形式: {file_extension}"
logger.error(error_msg)
self.show_notification(error_msg, success=False)
return {"status": "error", "message": error_msg}
# インポート先のディレクトリが存在することを確認
# UE5では必要に応じて自動的に作成されるので、ここでのチェックは不要
# インポート処理
self.show_notification(f"アセット '{file_name}' をインポート中...")
import_task = unreal.AssetImportTask()
import_task.filename = file_path
import_task.destination_path = destination_path
import_task.replace_existing = True
import_task.automated = True
import_task.save = True
# インポートを実行
unreal.AssetTools.get_asset_tools().import_asset_tasks([import_task])
# インポートされたアセットへのパス
asset_path = f"{destination_path}/{base_name}"
# 成功を通知
success_msg = f"アセット '{file_name}' をインポートしました"
logger.info(success_msg)
self.show_notification(success_msg, success=True)
return {
"status": "success",
"message": success_msg,
"asset_info": {
"name": base_name,
"original_path": file_path,
"asset_path": asset_path,
"type": file_extension[1:] # 先頭の'.'を除去
}
}
except Exception as e:
error_msg = f"アセットインポート中にエラーが発生しました: {str(e)}"
logger.exception(error_msg)
self.show_notification(error_msg, success=False)
return {"status": "error", "message": error_msg}
def place_asset(self, asset_path, location=(0, 0, 0), rotation=(0, 0, 0), scale=(1, 1, 1)):
"""
アセットをレベルに配置する
引数:
asset_path (str): 配置するアセットのパス
location (tuple): 位置座標 (X, Y, Z)
rotation (tuple): 回転 (Pitch, Yaw, Roll)
scale (tuple): スケール (X, Y, Z)
戻り値:
dict: 配置結果
"""
try:
# アセットが存在するか確認
asset_registry = unreal.AssetRegistryHelpers.get_asset_registry()
asset = asset_registry.get_asset_by_object_path(asset_path)
if not asset:
error_msg = f"アセット '{asset_path}' が見つかりません"
logger.error(error_msg)
self.show_notification(error_msg, success=False)
return {"status": "error", "message": error_msg}
# アセットをロード
self.show_notification(f"アセット '{asset_path}' を配置中...")
# アセットを配置
actor_location = unreal.Vector(location[0], location[1], location[2])
actor_rotation = unreal.Rotator(rotation[0], rotation[1], rotation[2])
actor_scale = unreal.Vector(scale[0], scale[1], scale[2])
# アセットクラスをロード
asset_data = asset_registry.get_asset_data(asset.get_path_name())
asset_class = unreal.load_class(None, asset_data.asset_class_path)
# エディタの現在のレベルに追加
actor = unreal.EditorLevelLibrary.spawn_actor_from_object(
asset,
actor_location,
actor_rotation
)
# スケールを設定
if actor:
actor.set_actor_scale3d(actor_scale)
# 成功を通知
success_msg = f"アセット '{asset_path}' を配置しました"
logger.info(success_msg)
self.show_notification(success_msg, success=True)
return {
"status": "success",
"message": success_msg,
"placement_info": {
"asset_path": asset_path,
"location": location,
"rotation": rotation,
"scale": scale,
"actor_name": actor.get_name() if actor else "Unknown"
}
}
except Exception as e:
error_msg = f"アセット配置中にエラーが発生しました: {str(e)}"
logger.exception(error_msg)
self.show_notification(error_msg, success=False)
return {"status": "error", "message": error_msg}
def generate_terrain(self, size_x=4096, size_y=4096, height_variation="medium", terrain_type="plains"):
"""
地形を生成する
引数:
size_x (int): X方向のサイズ
size_y (int): Y方向のサイズ
height_variation (str): 高さのバリエーション ("low", "medium", "high")
terrain_type (str): 地形タイプ ("plains", "mountainous", "desert", etc.)
戻り値:
dict: 生成結果
"""
try:
self.show_notification(f"{terrain_type}地形を生成中...")
# 高さバリエーションのマッピング
height_map = {
"low": 100,
"medium": 500,
"high": 1000
}
max_height = height_map.get(height_variation.lower(), 500)
# 地形ツールを使用して地形を生成
# UE5のPythonではランドスケープ操作のAPIが制限されているため、
# 簡易的な実装になります
# ランドスケープを作成
unreal.log(f"地形を生成しています: サイズ ({size_x}x{size_y}), 高さ {height_variation}, タイプ {terrain_type}")
# コマンドレットを使用してランドスケープを作成
landscape = unreal.EditorLevelLibrary.spawn_actor_from_class(
unreal.Landscape,
unreal.Vector(0, 0, 0),
unreal.Rotator(0, 0, 0)
)
# 地形タイプに応じたマテリアルを適用
terrain_material_map = {
"plains": "/Engine/EngineMaterials/Landscape/M_Landscape",
"mountainous": "/Engine/EngineMaterials/Landscape/M_Landscape_Mountain",
"desert": "/Engine/EngineMaterials/Landscape/M_Landscape_Desert"
}
material_path = terrain_material_map.get(terrain_type.lower(), "/Engine/EngineMaterials/Landscape/M_Landscape")
material = unreal.load_object(None, material_path)
if material and landscape:
landscape.landscape_material = material
# 成功を通知
success_msg = f"{terrain_type}地形の生成が完了しました"
logger.info(success_msg)
self.show_notification(success_msg, success=True)
return {
"status": "success",
"message": success_msg,
"terrain_info": {
"size_x": size_x,
"size_y": size_y,
"height_variation": height_variation,
"max_height": max_height,
"terrain_type": terrain_type
}
}
except Exception as e:
error_msg = f"地形生成中にエラーが発生しました: {str(e)}"
logger.exception(error_msg)
self.show_notification(error_msg, success=False)
return {"status": "error", "message": error_msg}
def create_blueprint(self, name, parent_class="Actor", description=None):
"""
Blueprintを作成する
引数:
name (str): Blueprint名
parent_class (str): 親クラス名
description (str): Blueprint説明文
戻り値:
dict: 作成結果
"""
try:
self.show_notification(f"Blueprint '{name}' を作成中...")
# Blueprint Factory を作成
factory = unreal.BlueprintFactory()
factory.parent_class = unreal.load_class(None, f"Engine.{parent_class}")
# Blueprint を作成
blueprint_path = f"/Game/Blueprints/{name}"
asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
blueprint = asset_tools.create_asset(
name,
"/Game/Blueprints",
unreal.Blueprint,
factory
)
# 説明文があれば、コメントとして追加
if description and blueprint:
# この部分はUE5 Pythonでは直接サポートされていないため、
# 実際の実装では別の方法が必要
pass
# 保存
if blueprint:
package = unreal.load_package(None, blueprint.get_outer().get_path_name())
unreal.EditorAssetLibrary.save_loaded_asset(blueprint)
# 成功を通知
success_msg = f"Blueprint '{name}' を作成しました"
logger.info(success_msg)
self.show_notification(success_msg, success=True)
return {
"status": "success",
"message": success_msg,
"blueprint_info": {
"name": name,
"path": blueprint_path,
"parent_class": parent_class,
"description": description
}
}
except Exception as e:
error_msg = f"Blueprint作成中にエラーが発生しました: {str(e)}"
logger.exception(error_msg)
self.show_notification(error_msg, success=False)
return {"status": "error", "message": error_msg}
def execute_mcp_command(self, command, params=None):
"""
MCPサーバーにコマンドを送信する
引数:
command (str): コマンド名
params (dict): コマンドパラメータ
戻り値:
dict: 実行結果
"""
try:
self.show_notification(f"MCPコマンド '{command}' を実行中...")
# パラメータのデフォルト値
if params is None:
params = {}
# MCPサーバーにコマンドを送信
response = requests.post(
f"{self.server_url}/command",
json={"command": command, "params": params}
)
if response.status_code == 200:
result = response.json()
status = result.get("status", "unknown")
if status == "success":
success_msg = f"MCPコマンド '{command}' が成功しました"
logger.info(success_msg)
self.show_notification(success_msg, success=True)
return result
else:
error_msg = f"MCPコマンド '{command}' が失敗しました: {result.get('message', '')}"
logger.error(error_msg)
self.show_notification(error_msg, success=False)
return result
else:
error_msg = f"MCPサーバーへのコマンド送信に失敗しました: HTTP {response.status_code}"
logger.error(error_msg)
self.show_notification(error_msg, success=False)
return {
"status": "error",
"message": error_msg
}
except Exception as e:
error_msg = f"MCPコマンド実行中にエラーが発生しました: {str(e)}"
logger.exception(error_msg)
self.show_notification(error_msg, success=False)
return {
"status": "error",
"message": error_msg
}
# UE5のメインスクリプト実行関数
def run():
"""
統合をテストするための実行関数
"""
integration = UE5MCPIntegration()
# MCPサーバーへの接続をテスト
if not integration.connect_to_server():
return
# 新しいレベルを作成
level_result = integration.create_level("MCPTestLevel", "ThirdPerson")
if level_result["status"] != "success":
return
# 地形を生成
terrain_result = integration.generate_terrain(4096, 4096, "medium", "mountainous")
if terrain_result["status"] != "success":
return
# Blueprintを作成
bp_result = integration.create_blueprint("BP_CollectibleItem", "Actor", "プレイヤーが収集できるアイテム")
unreal.log(f"\nUE5-MCP統合テスト結果:")
unreal.log(f"レベル作成: {level_result['status']}")
unreal.log(f"地形生成: {terrain_result['status']}")
unreal.log(f"Blueprint作成: {bp_result['status']}")
# スクリプトが直接実行された場合にのみ実行
if __name__ == "__main__":
run()