forked from AlansCodeLog/blender-debugger-for-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
207 lines (175 loc) · 6.47 KB
/
__init__.py
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
'''
Copyright (C) 2018 Alan North
alannorth@gmail.com
Created by Alan North
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
bl_info = {
'name': 'Debugger for VS Code',
'author': 'Alan North',
'version': (1, 0, 1),
'blender': (2, 80, 0), # supports 2.8+
"description": "Starts debugging server for VS Code.",
'location': 'In search (Edit > Operator Search) type "Debug"',
"warning": "",
"wiki_url": "https://github.com/AlansCodeLog/blender-debugger-for-vscode",
"tracker_url": "https://github.com/AlansCodeLog/blender-debugger-for-vscode/issues",
'category': 'Development',
}
import bpy
import sys
import os
import subprocess
import re
# finds path to ptvsd if it exists
def check_for_ptvsd():
#commands to check
checks = [
["where", "python"],
["whereis", "python"],
["which", "python"],
]
location = None
for command in checks:
try:
location = subprocess.Popen(
command,
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
except Exception:
continue
if location is not None:
location = str(location.communicate()[0], "utf-8")
#normalize slashes
location = re.sub("\\\\", "/", location)
#extract path up to last slash
match = re.search(".*(/)", location)
if match is not None:
match = match.group()
if os.path.exists(match+"lib/site-packages/ptvsd"):
match = match+"lib/site-packages"
return match
#check in path just in case PYTHONPATH happens to be set
for path in sys.path:
path = path.rstrip("/")
if os.path.exists(path+"/ptvsd"):
return path
if os.path.exists(path+"/site-packages/ptvsd"):
return path+"/site-packages"
if os.path.exists(path+"/lib/site-packages/ptvsd"):
return path+"lib/site-packages"
return "PTVSD not Found"
# Preferences
class DebuggerPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
path: bpy.props.StringProperty(
name="Location of PTVSD",
subtype="DIR_PATH",
default=check_for_ptvsd()
)
timeout: bpy.props.IntProperty(
name="Timeout",
default=20
)
port: bpy.props.IntProperty(
name="Port",
min=0,
max=65535,
default=5678
)
def draw(self, context):
layout = self.layout
row_path = layout
row_path.label(text="The addon will try to auto-find the location to ptvsd, if no path is found, or you would like to use a different path, set it here.")
row_path.prop(self, "path")
row_timeout = layout.split()
row_timeout.prop(self, "timeout")
row_timeout.label(text="Timeout in seconds for the attach confirmation listener.")
row_port = layout.split()
row_port.prop(self, "port")
row_port.label(text="Port to use. Should match port in VS Code's launch.json.")
# check if debugger has attached
def check_done(i, modal_limit, prefs):
if i == 0 or i % 60 == 0:
print("Waiting... (on port "+str(prefs.port)+")")
if i > modal_limit:
print("Attach Confirmation Listener Timed Out")
return {"CANCELLED"}
if not ptvsd.is_attached():
return {"PASS_THROUGH"}
print('Debugger is Attached')
return {"FINISHED"}
class DebuggerCheck(bpy.types.Operator):
bl_idname = "debug.check_for_debugger"
bl_label = "Debug: Check if VS Code is Attached"
bl_description = "Starts modal timer that checks if debugger attached until attached or until timeout"
_timer = None
count = 0
modal_limit = 20*60
# call check_done
def modal(self, context, event):
self.count = self.count + 1
if event.type == "TIMER":
prefs = bpy.context.preferences.addons[__name__].preferences
return check_done(self.count, self.modal_limit, prefs)
return {"PASS_THROUGH"}
def execute(self, context):
# set initial variables
self.count = 0
prefs = bpy.context.preferences.addons[__name__].preferences
self.modal_limit = prefs.timeout*60
wm = context.window_manager
self._timer = wm.event_timer_add(0.1, window=context.window)
wm.modal_handler_add(self)
return {"RUNNING_MODAL"}
def cancel(self, context):
print("Debugger Confirmation Cancelled")
wm = context.window_manager
wm.event_timer_remove(self._timer)
class DebugServerStart(bpy.types.Operator):
bl_idname = "debug.connect_debugger_vscode"
bl_label = "Debug: Start Debug Server for VS Code"
bl_description = "Starts ptvsd server for debugger to attach to"
def execute(self, context):
#get ptvsd and import if exists
prefs = bpy.context.preferences.addons[__name__].preferences
ptvsd_path = prefs.path.rstrip("/")
ptvsd_port = prefs.port
#actually check ptvsd is still available
if ptvsd_path == "PTVSD not Found":
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.")
return {"CANCELLED"}
if not os.path.exists(os.path.abspath(ptvsd_path+"/ptvsd")):
self.report({"ERROR"}, "Can't find ptvsd at: %r/ptvsd." % ptvsd_path)
return {"CANCELLED"}
if not any(ptvsd_path in p for p in sys.path):
sys.path.append(ptvsd_path)
global ptvsd #so we can do check later
import ptvsd
# can only be attached once, no way to detach (at least not that I understand?)
try:
ptvsd.enable_attach(("0.0.0.0", ptvsd_port), redirect_output=True)
except:
print("Server already running.")
# call our confirmation listener
bpy.ops.debug.check_for_debugger()
return {"FINISHED"}
classes = (
DebuggerPreferences,
DebuggerCheck,
DebugServerStart,
)
register, unregister = bpy.utils.register_classes_factory(classes)
if __name__ == "__main__":
register()