Skip to content

Commit e29a87a

Browse files
ImLucasBrownLucas Brown
authored andcommitted
* failsafe function calls for UE menu buttons
1 parent cc62292 commit e29a87a

1 file changed

Lines changed: 43 additions & 9 deletions

File tree

nxt_editor/integration/unreal/Content/Python/init_unreal.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@
22
import sys
33
import unreal
44
import subprocess
5+
from functools import wraps
6+
7+
8+
def fail_safe(func):
9+
"""Decorator to wrap functions with exception handling to prevent crashes."""
10+
@wraps(func)
11+
def wrapper(*args, **kwargs):
12+
try:
13+
return func(*args, **kwargs)
14+
except Exception as e:
15+
func_name = func.__name__
16+
error_msg = f"Error in {func_name}: {type(e).__name__}: {e}"
17+
unreal.log_error(error_msg)
18+
# Also show a notification to the user
19+
try:
20+
unreal.EditorDialog.show_message(
21+
title=f"NXT Error: {func_name}",
22+
message=error_msg,
23+
message_type=unreal.AppMsgType.OK
24+
)
25+
except:
26+
# If dialog fails, at least we logged the error
27+
pass
28+
return None
29+
return wrapper
530

631

732
def is_nxt_available():
@@ -19,32 +44,41 @@ def get_python_exc_path():
1944
real_prefix = os.path.realpath(sys.prefix)
2045
return os.path.join(real_prefix, exc_name)
2146

47+
@fail_safe
2248
def install_nxt_to_interpreter():
23-
subprocess.check_call([get_python_exc_path(), '-m', 'pip',
24-
'install', 'nxt-editor'])
25-
unreal.log_warning("Please restart the editor for nxt menu options.")
49+
try:
50+
subprocess.check_call([get_python_exc_path(), '-m', 'pip',
51+
'install', 'nxt-editor'])
52+
unreal.log_warning("Please restart the editor for nxt menu options.")
53+
except Exception as e:
54+
unreal.log_error(f"Failed to install nxt-editor package: {e}")
55+
try:
56+
refresh_nxt_menu()
57+
except Exception as e:
58+
unreal.log_error(f"Failed to refresh nxt menu after installation: {e}")
2659

60+
@fail_safe
2761
def update_installed_nxt():
2862
subprocess.check_call([get_python_exc_path(), '-m', 'pip',
2963
'install', '--upgrade', 'nxt-editor', 'nxt-core'])
3064

65+
@fail_safe
3166
def uninstall_nxt_from_interpreter():
3267
subprocess.check_call([get_python_exc_path(), '-m', 'pip',
3368
'uninstall', '-y', 'nxt-editor', 'nxt-core'])
3469
unreal.log_warning("Nxt menu will refresh next editor launch.")
3570

71+
@fail_safe
3672
def launch_nxt_editor():
37-
try:
38-
from nxt_editor.integration.unreal import launch_nxt_in_ue
39-
launch_nxt_in_ue()
40-
except Exception as e:
41-
unreal.log_error(f'nxt editor crashed: {e}')
73+
"""Safe wrapper for launching the nxt editor."""
74+
from nxt_editor.integration.unreal import launch_nxt_in_ue
75+
launch_nxt_in_ue()
4276

4377
def make_open_editor_entry():
4478
entry = unreal.ToolMenuEntry(name='Open Editor',
4579
type=unreal.MultiBlockType.MENU_ENTRY)
4680
entry.set_label('Open Editor')
47-
launch_command = "import init_unreal; init_unreal.launch_nxt_editor()"
81+
launch_command = "launch_nxt_editor()"
4882
entry.set_string_command(unreal.ToolMenuStringCommandType.PYTHON, 'Python',
4983
string=launch_command)
5084
return entry

0 commit comments

Comments
 (0)