Skip to content

Commit 86ecfcd

Browse files
John HackettAlansCodeLog
authored andcommitted
Improved Headless Debugging
Added a parameter to command to allow auto starting the server. This allows easily debugging blender when running headlessly. Closes #26
1 parent d60d8d1 commit 86ecfcd

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,53 @@ At this point you should be able to add a breakpoint and when you trigger it in
106106

107107
Note though that if you make changes to the file, Blender will not detect them. Have open `User Preferences > Addons` so you can toggle your addon on and off when you make changes. If anyone knows any way to improve this I'd love to know.
108108

109+
## Advanced Usage
110+
111+
### Wait for Client
112+
113+
The debugger can be made to wait for a client to connect (this will pause all execution). This can be useful for debugging the connection or when running blender headless / in background mode.
114+
115+
To do so, call the server connect command from the python console or from a script/addon like so:
116+
117+
```python
118+
bpy.ops.debug.connect_debugger_vscode(waitForClient=True)
119+
```
120+
121+
#### Running in Headless Mode
122+
123+
First make sure the addon is installed, enabled, and works when you run blender normally.
124+
125+
Blender can then be run in background mode with the `-b/--background` switch (e.g. `blender --background`, `blender --background --python your_script.py`).
126+
127+
See [Blender Command Line](https://docs.blender.org/manual/en/latest/advanced/command_line/introduction.html).
128+
129+
You can detect when blender is run in background/headless mode and make the debugger pause and wait for a connection in your script/addon:
130+
131+
```python
132+
if bpy.app.background:
133+
bpy.ops.debug.connect_debugger_vscode(waitForClient=True)
134+
```
135+
136+
This will wait for a connection to be made to the debugging server. Once this is established, the script will continue executing and VSCode should pause on breakpoints that have been triggered.
137+
138+
For addons, you will need to do this from a handler:
139+
140+
```python
141+
from bpy.app.handlers import persistent
142+
#...
143+
def register():
144+
bpy.app.handlers.load_post.append(load_handler)
145+
#...
146+
@persistent
147+
def load_handler(dummy):
148+
# remove handler so it only runs once
149+
bpy.app.handlers.load_post.remove(load_handler)
150+
if bpy.app.background:
151+
bpy.ops.debug.connect_debugger_vscode(waitForClient=True)
152+
153+
```
154+
See [Application Handlers](https://docs.blender.org/api/current/bpy.app.handlers.html)
155+
109156
### Debugging/Editing Source Code
110157

111158
It is possible to edit the Blender source code but it can be a bit tricky to get it to detect changes (nevermind live editing is buggy anyways).

__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
bl_info = {
2222
'name': 'Debugger for VS Code',
2323
'author': 'Alan North',
24-
'version': (2, 0, 0),
24+
'version': (2, 1, 0),
2525
'blender': (2, 80, 0), # supports 2.8+
2626
"description": "Starts debugging server for VS Code.",
2727
'location': 'In search (Edit > Operator Search) type "Debug"',
@@ -166,6 +166,8 @@ class DebugServerStart(bpy.types.Operator):
166166
bl_label = "Debug: Start Debug Server for VS Code"
167167
bl_description = "Starts debugpy server for debugger to attach to"
168168

169+
waitForClient: bpy.props.BoolProperty(default=False)
170+
169171
def execute(self, context):
170172
#get debugpy and import if exists
171173
prefs = bpy.context.preferences.addons[__name__].preferences
@@ -193,6 +195,10 @@ def execute(self, context):
193195
except:
194196
print("Server already running.")
195197

198+
if (self.waitForClient):
199+
self.report({"INFO"}, "Blender Debugger for VSCode: Awaiting Connection")
200+
debugpy.wait_for_client()
201+
196202
# call our confirmation listener
197203
bpy.ops.debug.check_for_debugger()
198204
return {"FINISHED"}

0 commit comments

Comments
 (0)