forked from sambler/myblendercontrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_debugger.py
181 lines (131 loc) · 5.46 KB
/
remote_debugger.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
"""
Remote debugging support.
This addon allows you to use a remote Python debugger with PyCharm, PyDev and
possibly other IDEs. As it is, without modification, it only supports PyCharm,
but it may work by pointing it at a similar egg file shipped with PyDev.
Before using, point the addon to your pycharm-debug-py3k.egg file in the
addon preferences screen.
For more information on how to use this addon, please read my article at
http://code.blender.org/2015/10/debugging-python-code-with-pycharm/
"""
bl_info = {
'name': 'Remote debugger',
'author': 'Sybren A. Stüvel',
'version': (0, 4),
'blender': (2, 80, 0),
'location': 'Press [Space], search for "debugger"',
'category': 'Development',
}
import bpy
import os.path
from bpy.types import AddonPreferences
from bpy.props import StringProperty
# Get references to all property definition functions in bpy.props,
# so that they can be used to replace 'x = IntProperty()' to 'x: IntProperty()'
# dynamically when working on Blender 2.80+
__all_prop_funcs = {
getattr(bpy.props, propname)
for propname in dir(bpy.props)
if propname.endswith('Property')
}
def convert_properties(class_):
"""Class decorator to avoid warnings in Blender 2.80+
This decorator replaces property definitions like this:
someprop = bpy.props.IntProperty()
to annotations, as introduced in Blender 2.80:
someprop: bpy.props.IntProperty()
No-op if running on Blender 2.79 or older.
"""
if bpy.app.version < (2, 80):
return class_
if not hasattr(class_, '__annotations__'):
class_.__annotations__ = {}
attrs_to_delete = []
for name, value in class_.__dict__.items():
if not isinstance(value, tuple) or len(value) != 2:
continue
prop_func, kwargs = value
if prop_func not in __all_prop_funcs:
continue
# This is a property definition, replace it with annotation.
attrs_to_delete.append(name)
class_.__annotations__[name] = value
for attr_name in attrs_to_delete:
delattr(class_, attr_name)
return class_
def addon_preferences(context):
try:
preferences = context.preferences
except AttributeError:
# Old (<2.80) location of user preferences
preferences = context.user_preferences
return preferences.addons[__name__].preferences
@convert_properties
class DebuggerAddonPreferences(AddonPreferences):
# this must match the addon name, use '__package__'
# when defining this in a submodule of a python package.
bl_idname = __name__
eggpath = StringProperty(
name='Path of the PyCharm egg file',
description='Make sure you select the py3k egg',
subtype='FILE_PATH',
default='pycharm-debug-py3k.egg'
)
pydevpath = StringProperty(
name='Path of the PyDev pydevd.py file',
subtype='FILE_PATH',
default='pydevd.py'
)
def draw(self, context):
layout = self.layout
layout.prop(self, 'pydevpath')
layout.prop(self, 'eggpath')
layout.label(text='Make sure you select the egg for Python 3.x: pycharm-debug-py3k.egg ')
class DEBUG_OT_connect_debugger_pycharm(bpy.types.Operator):
bl_idname = 'debug.connect_debugger_pycharm'
bl_label = 'Connect to remote PyCharm debugger'
bl_description = 'Connects to a PyCharm debugger on localhost:1090'
def execute(self, context):
import sys
addon_prefs = addon_preferences(context)
eggpath = os.path.abspath(addon_prefs.eggpath)
if not os.path.exists(eggpath):
self.report({'ERROR'}, 'Unable to find debug egg at %r. Configure the addon properties '
'in the User Preferences menu.' % eggpath)
return {'CANCELLED'}
if not any('pycharm-debug' in p for p in sys.path):
sys.path.append(eggpath)
import pydevd
pydevd.settrace('localhost', port=1090, stdoutToServer=True, stderrToServer=True,
suspend=False)
return {'FINISHED'}
class DEBUG_OT_connect_debugger_pydev(bpy.types.Operator):
bl_idname = 'debug.connect_debugger_pydev'
bl_label = 'Connect to remote PyDev debugger'
bl_description = 'Connects to a PyDev debugger on localhost:5678'
def execute(self, context):
import sys
addon_prefs = addon_preferences(context)
pydevpath = os.path.abspath(addon_prefs.pydevpath)
if not os.path.exists(pydevpath):
self.report({'ERROR'}, 'Unable to find pydevd.py at %r. Configure the addon properties '
'in the User Preferences menu.' % pydevpath)
return {'CANCELLED'}
dirname = os.path.dirname(pydevpath)
basename = os.path.basename(dirname)
if not any(basename in p for p in sys.path):
sys.path.append(dirname)
import pydevd
pydevd.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True,
suspend=False)
return {'FINISHED'}
def register():
bpy.utils.register_class(DEBUG_OT_connect_debugger_pycharm)
bpy.utils.register_class(DEBUG_OT_connect_debugger_pydev)
bpy.utils.register_class(DebuggerAddonPreferences)
def unregister():
bpy.utils.unregister_class(DEBUG_OT_connect_debugger_pycharm)
bpy.utils.unregister_class(DEBUG_OT_connect_debugger_pydev)
bpy.utils.unregister_class(DebuggerAddonPreferences)
if __name__ == '__main__':
register()