-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy path__init__.py
238 lines (201 loc) · 7.43 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
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
'''
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': (2, 2, 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 os
import re
import subprocess
import sys
import bpy
# finds path to debugpy if it exists
def check_for_debugpy():
pip_info = None
try:
pip_info = subprocess.Popen(
"pip show debugpy",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
except Exception as e:
print(e)
pass
if pip_info is not None:
pip_info = str(pip_info.communicate()[0], "utf-8")
pip_info = re.sub("\\\\", "/", pip_info)
#extract path up to last slash
match = re.search("Location: (.*)", pip_info)
#normalize slashes
if match is not None:
match = match.group(1).rstrip()
if os.path.exists(match+"/debugpy"):
return match
# commands to check
checks = [
["where", "python"],
["whereis", "python"],
["which", "python"],
]
location = None
for command in checks:
try:
location = subprocess.Popen(
command,
shell=True,
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(1)
if os.path.exists(match+"lib/site-packages/debugpy"):
match = match+"lib/site-packages"
return match
# check in path just in case PYTHONPATH happens to be set
# this is not going to work because Blender's sys.path is different
for path in sys.path:
path = path.rstrip("/")
if os.path.exists(path+"/debugpy"):
return path
if os.path.exists(path+"/site-packages/debugpy"):
return path+"/site-packages"
if os.path.exists(path+"/lib/site-packages/debugpy"):
return path+"lib/site-packages"
return "debugpy not Found"
# Preferences
class DebuggerPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
path: bpy.props.StringProperty(
name="Location of debugpy (site-packages folder)",
subtype="DIR_PATH",
default=check_for_debugpy()
)
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 of debugpy, 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 debugpy.is_client_connected():
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 debugpy server for debugger to attach to"
waitForClient: bpy.props.BoolProperty(default=False)
def execute(self, context):
#get debugpy and import if exists
prefs = bpy.context.preferences.addons[__name__].preferences
debugpy_path = prefs.path.rstrip("/")
debugpy_port = prefs.port
#actually check debugpy is still available
if debugpy_path == "debugpy not found":
self.report({"ERROR"}, "Couldn't detect debugpy, please specify the path manually in the addon preferences or reload the addon if you installed debugpy after enabling it.")
return {"CANCELLED"}
if not os.path.exists(os.path.abspath(debugpy_path+"/debugpy")):
self.report({"ERROR"}, "Can't find debugpy at: %r/debugpy." % debugpy_path)
return {"CANCELLED"}
if not any(debugpy_path in p for p in sys.path):
sys.path.append(debugpy_path)
global debugpy #so we can do check later
import debugpy
# can only be attached once, no way to detach (at least not that I understand?)
try:
debugpy.listen(("localhost", debugpy_port))
except:
print("Server already running.")
if (self.waitForClient):
self.report({"INFO"}, "Blender Debugger for VSCode: Awaiting Connection")
debugpy.wait_for_client()
# 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()