|
| 1 | +bl_info = { |
| 2 | + 'name': 'Debugger for VS Code', |
| 3 | + 'author': 'Alan North', |
| 4 | + 'version': (0, 0, 1), |
| 5 | + 'blender': (2, 79, 0), |
| 6 | + "description": "Starts debugging server for VS Code.", |
| 7 | + 'location': 'In search (default shortcut:space) type "Debug"', |
| 8 | + "warning": "", |
| 9 | + "wiki_url": "https://github.com/AlansCodeLog/blender-debugger-for-vscode", |
| 10 | + "tracker_url": "https://github.com/AlansCodeLog/blender-debugger-for-vscode/issues", |
| 11 | + 'category': 'Development', |
| 12 | +} |
| 13 | + |
| 14 | +import bpy |
| 15 | +import sys |
| 16 | +import os |
| 17 | +import subprocess |
| 18 | +import re |
| 19 | + |
| 20 | +# check path for ptvsd |
| 21 | +def check_for_ptvsd(): |
| 22 | + #check for python with where/whereis |
| 23 | + python_exe = None |
| 24 | + try: |
| 25 | + python_exe = subprocess.Popen( |
| 26 | + ["whereis", "python"], |
| 27 | + shell=False, |
| 28 | + stdout=subprocess.PIPE, |
| 29 | + stderr=subprocess.PIPE |
| 30 | + ) |
| 31 | + except Exception: |
| 32 | + pass |
| 33 | + if python_exe is None: |
| 34 | + try: |
| 35 | + python_exe = subprocess.Popen( |
| 36 | + ["where", "python"], |
| 37 | + shell=False, |
| 38 | + stdout=subprocess.PIPE, |
| 39 | + stderr=subprocess.PIPE |
| 40 | + ) |
| 41 | + except Exception: |
| 42 | + pass |
| 43 | + if python_exe is None: |
| 44 | + try: |
| 45 | + python_exe = subprocess.Popen( |
| 46 | + ["which", "python"], |
| 47 | + shell=False, |
| 48 | + stdout=subprocess.PIPE, |
| 49 | + stderr=subprocess.PIPE |
| 50 | + ) |
| 51 | + except Exception: |
| 52 | + pass |
| 53 | + if python_exe is not None: |
| 54 | + python_exe = str(python_exe.communicate()[0], "utf-8") |
| 55 | + match = re.search(".*(\\\\|/)", python_exe).group() |
| 56 | + if os.path.exists(match+"\lib\site-packages\ptvsd"): |
| 57 | + return match+"\lib\site-packages" |
| 58 | + |
| 59 | + #check in our path |
| 60 | + for path in sys.path: |
| 61 | + if os.path.exists(path+"\ptvsd"): |
| 62 | + return path |
| 63 | + if os.path.exists(path+"\site-packages\ptvsd"): |
| 64 | + return path+"\site-packages" |
| 65 | + if os.path.exists(path+"\lib\site-packages\ptvsd"): |
| 66 | + return path+"lib\site-packages" |
| 67 | + return "PTVSD not Found" |
| 68 | + |
| 69 | +# Preferences |
| 70 | +class DebuggerPreferences(bpy.types.AddonPreferences): |
| 71 | + bl_idname = __name__ |
| 72 | + |
| 73 | + path = bpy.props.StringProperty( |
| 74 | + name="Location of PTVSD", |
| 75 | + subtype="DIR_PATH", |
| 76 | + default=check_for_ptvsd() |
| 77 | + ) |
| 78 | + |
| 79 | + timeout = bpy.props.IntProperty( |
| 80 | + name="Timeout", |
| 81 | + default=20 |
| 82 | + ) |
| 83 | + def draw(self, context): |
| 84 | + layout = self.layout |
| 85 | + layout.prop(self, "path") |
| 86 | + layout.label(text="Pluging will try to auto-find it, if no path found, or you would like to use a different path, set it here.") |
| 87 | + row = layout.split() |
| 88 | + row.label(text="Timeout in seconds for attach confirmation listener.") |
| 89 | + row.prop(self, "timeout") |
| 90 | + |
| 91 | +# check if debugger has attached |
| 92 | +def check_done(i, modal_limit): |
| 93 | + if i % 100 == 0: |
| 94 | + print("Waiting") |
| 95 | + if i > modal_limit: |
| 96 | + print("Attach Confirmation Listener Timed Out") |
| 97 | + return {"CANCELLED"} |
| 98 | + if not ptvsd.is_attached(): |
| 99 | + return {"PASS_THROUGH"} |
| 100 | + print('Debugger is Attached') |
| 101 | + return {"FINISHED"} |
| 102 | + |
| 103 | +class DebuggerCheck(bpy.types.Operator): |
| 104 | + bl_idname = "debug.check_for_debugger" |
| 105 | + bl_label = "Debug: Check if VS Code is Attached" |
| 106 | + bl_description = "Starts modal timer that checks if debugger attached until attached or until timeout." |
| 107 | + |
| 108 | + _timer = None |
| 109 | + count = 0 |
| 110 | + modal_limit = 20000 |
| 111 | + |
| 112 | + # call check_done |
| 113 | + def modal(self, context, event): |
| 114 | + self.count = self.count + 1 |
| 115 | + if event.type == "TIMER": |
| 116 | + return check_done(self.count, self.modal_limit) |
| 117 | + return {"PASS_THROUGH"} |
| 118 | + |
| 119 | + def execute(self, context): |
| 120 | + # set initial variables |
| 121 | + self.count = 0 |
| 122 | + prefs = bpy.context.user_preferences.addons[__name__].preferences |
| 123 | + self.modal_limit = prefs.timeout*100 |
| 124 | + |
| 125 | + wm = context.window_manager |
| 126 | + self._timer = wm.event_timer_add(0.1, context.window) |
| 127 | + wm.modal_handler_add(self) |
| 128 | + return {"RUNNING_MODAL"} |
| 129 | + |
| 130 | + def cancel(self, context): |
| 131 | + print("Debugger Confirmation Cancelled") |
| 132 | + wm = context.window_manager |
| 133 | + wm.event_timer_remove(self._timer) |
| 134 | + |
| 135 | +class DebugServerStart(bpy.types.Operator): |
| 136 | + bl_idname = "debug.connect_debugger_vscode" |
| 137 | + bl_label = "Debug: Start Debug Server for VS Code" |
| 138 | + bl_description = "Starts ptvsd server for debugger to attach to." |
| 139 | + |
| 140 | + def execute(self, context): |
| 141 | + #get ptvsd and import if exists |
| 142 | + prefs = bpy.context.user_preferences.addons[__name__].preferences |
| 143 | + ptvsd_path = os.path.abspath(prefs.path) |
| 144 | + |
| 145 | + #actually check ptvsd is still available |
| 146 | + if ptvsd_path == "PTVSD not Found": |
| 147 | + self.report({"ERROR"}, "Couldn't detect ptvsd, please specify the path manually in the addon preferences or reload the addon if you installed ptvsd after enabling it.") |
| 148 | + return {"CANCELLED"} |
| 149 | + |
| 150 | + if not os.path.exists(ptvsd_path+"/ptvsd"): |
| 151 | + self.report({"ERROR"}, "Can't find ptvsd at: %r/ptvsd." % ptvsd_path) |
| 152 | + return {"CANCELLED"} |
| 153 | + |
| 154 | + if not any(ptvsd_path in p for p in sys.path): |
| 155 | + sys.path.append(ptvsd_path) |
| 156 | + |
| 157 | + global ptvsd #so we can do check later |
| 158 | + import ptvsd |
| 159 | + |
| 160 | + # can only be attached once, no way to detach (at least not that I understand?) |
| 161 | + try: |
| 162 | + ptvsd.enable_attach("my_secret", address = ("0.0.0.0", 3000)) |
| 163 | + except: |
| 164 | + print("Server already running.") |
| 165 | + |
| 166 | + # call our confirmation listener |
| 167 | + bpy.ops.debug.check_for_debugger() |
| 168 | + return {"FINISHED"} |
| 169 | + |
| 170 | +def register(): |
| 171 | + bpy.utils.register_module(__name__) |
| 172 | + |
| 173 | +def unregister(): |
| 174 | + bpy.utils.unregister_module(__name__) |
| 175 | + |
| 176 | +if __name__ == "__main__": |
| 177 | + register() |
0 commit comments