File size: 3,755 Bytes
92f480e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Blender startup script (installed by nocode-patch.py into scripts/startup/).
#
# Keeps the coding surfaces closed at run time, complementing the build-time
# removal of the console backend and the Run Script UI:
#   * removes the "Scripting" workspace (from the startup file, and again if
#     it is re-added through the "+" workspace menu);
#   * converts any Python Console / Text Editor area back to the Info editor;
#   * drops the Alt+P "Run Script" keymap entry;
#   * pins the preferences: no Python tooltips, no Developer Extras (F3 search
#     stays menu-only), no auto-run of file-embedded scripts, no splash.
# Runs as a persistent 1 s timer plus a load_post handler. Silent on errors —
# the guard must never break the editor the agent is working in.
import bpy
from bpy.app.handlers import persistent

BANNED_AREAS = {"CONSOLE", "TEXT_EDITOR"}
BANNED_WORKSPACES = ("scripting",)


def _pin_prefs():
    try:
        p = bpy.context.preferences
        p.view.show_splash = False
        p.view.show_tooltips_python = False
        p.view.show_developer_ui = False
        p.filepaths.use_scripts_auto_execute = False
    except Exception:
        pass


def _drop_run_script_keymap():
    try:
        wm = bpy.context.window_manager
        for kc in (wm.keyconfigs.default, wm.keyconfigs.addon, wm.keyconfigs.user, wm.keyconfigs.active):
            if kc is None:
                continue
            km = kc.keymaps.get("Text")
            if km is None:
                continue
            for kmi in list(km.keymap_items):
                if kmi.idname in ("text.run_script", "console.execute"):
                    km.keymap_items.remove(kmi)
    except Exception:
        pass


def _remove_scripting_workspaces():
    try:
        doomed = [ws for ws in bpy.data.workspaces if ws.name.lower().startswith(BANNED_WORKSPACES)]
        if doomed and len(bpy.data.workspaces) > len(doomed):
            # never leave a window on a workspace we are about to delete
            fallback = next(ws for ws in bpy.data.workspaces if ws not in doomed)
            for win in bpy.context.window_manager.windows:
                if win.workspace in doomed:
                    win.workspace = fallback
            bpy.data.batch_remove(ids=doomed)
    except Exception:
        pass


def _convert_banned_areas():
    try:
        for win in bpy.context.window_manager.windows:
            for area in win.screen.areas:
                if area.type in BANNED_AREAS:
                    try:
                        with bpy.context.temp_override(window=win, area=area):
                            area.type = "INFO"
                    except Exception:
                        try:
                            area.type = "INFO"
                        except Exception:
                            pass
    except Exception:
        pass


def _guard():
    _remove_scripting_workspaces()
    _convert_banned_areas()
    return 1.0


@persistent
def _on_load(_dummy=None):
    _pin_prefs()
    _drop_run_script_keymap()
    _remove_scripting_workspaces()
    _convert_banned_areas()


def register():
    if bpy.app.background:
        return  # the verifier / oracle run Blender headless; leave them alone
    _pin_prefs()
    if _on_load not in bpy.app.handlers.load_post:
        bpy.app.handlers.load_post.append(_on_load)
    if not bpy.app.timers.is_registered(_guard):
        bpy.app.timers.register(_guard, first_interval=2.0, persistent=True)
    bpy.app.timers.register(lambda: (_on_load(), None)[1], first_interval=1.0)


def unregister():
    try:
        bpy.app.handlers.load_post.remove(_on_load)
    except ValueError:
        pass
    if bpy.app.timers.is_registered(_guard):
        bpy.app.timers.unregister(_guard)