Debian Codegraph @

src/bonsai/docs/guides/development/debugging.rst

raw
1 Debugging Bonsai
2 ================
3
4 This is a mini-guide to setting up an IDE and configuring it to debug Bonsai
5 more easily. It is currently specific to this writers own system (Ubuntu) but
6 this can be expanded by others.
7
8 .. warning::
9 Attaching debugger to Blender may significantly decrease performance due to
10 the debugger overhead.
11
12 VSCode Extension
13 --------------
14
15 1. **Install VSCode/VSCodium**: This will be system specific. I used the
16 available snap package.
17
18 .. code-block:: bash
19
20 sudo snap install --classic codium
21
22 I chose VSCodium to avoid Microsoft telemetry and data harvesting, but
23 VSCode is going to be similar, and even a bit easier (i.e. step 3).
24
25 2. **Activate Python language support**: Start VSCode/VSCodium, open the
26 Extensions tab, find the Python language support and activate it.
27
28 3. **Install Blender Addon**: This step depends on which IDE you chose.
29
30 a. VSCode
31
32 Open up the Extensions tab on the left, and search for the *Blender
33 Development* extension. Select it, and click Install.
34
35 I haven't actually tested this, but this should install the
36 *ms-vscode.cpptools* dependancy for you. If not, you may need to search
37 for, and install, this extension too.
38
39 b. VSCodium
40
41 It seems VSCodium is not allowed to direct access the Marketplace, and
42 due to licensing it is not possible to install the *Blender Development*
43 extension because of the *ms-vscode.cpptools* dependancy.
44
45 VSCodium's equivalent Marketplace does not (and could not due to
46 licensing) provide a functioning *Blender Development* extension. (Or I'm
47 an idiot... entirely possible).
48
49 I've created an alternative *Blender Development* extension with the
50 *ms-vscode.cpptools* dependancy removed.
51
52 .. note::
53 This alternative should also work in VSCode, but you may as well save
54 yourself the hassle, and just use the original one.
55
56 Download the *Blender Development* ``.vsix`` file from my GitHub fork
57 using your browser from `this <https://github.com/sboddy/blender_vscode>`_
58 repository. The ``.vsix`` is available on the Releases page.
59
60 In VSCodium's Extensions sidebar install it with "**...**" ->
61 "**Install from VSIX...**"
62
63 If you are squeamish about downloading my ``.vsix``, you can review the
64 single commit, clone the repo and create your own.
65
66 4. **Set the Blender config directory**: In **Settings** -> **Extensions** ->
67 **Blender** -> "**Environment Variables**"
68 edit the json file to set the ``BLENDER_USER_RESOURCE`` to the config folder
69 you installed Bonsai to. *For me*:
70
71 .. code-block:: json
72
73 {
74 "blender.environmentVariables": {
75 "BLENDER_USER_RESOURCES": "/home/steve/.config/blender/4.2bonsai"
76 }
77 }
78
79 .. warning::
80 You `must` change that path to the correct value for your system.
81
82 5. **Unset the Just My Code flag**: In **Settings** -> **Extensions** ->
83 **Blender** -> "**Just My Code**" by unchecking the box.
84
85 Again, this was advice found searching around in a github issue. If not
86 done, the breakpoints do not work for me.
87
88 6. **Open the folder in VSCode/VSCodium**: This is the same folder set in step
89 5. Again, *for me*:
90
91 ``/home/steve/.config/blender/4.2bonsai``
92
93 This was a key point for me. Do `not` go deeper in the folder structure -
94 that just doesn't work. (Thanks @theoryshaw) This feels a little like
95 using a sledgehammer to crack a nut, and there might be a better, "proper"
96 way of setting this up. If there is I'd love to hear it.
97
98 7. **Start Blender**: In VSCode/VSCodium press ``Ctrl+Shift+P``, and
99 search/select **Blender: Start**.
100
101 8. **Set the Blender binary path**: The first time you try to start Blender the
102 addon will ask for the path of the binary. Navigate to your Blender binary
103 and select it. *For me*:
104
105 ``/home/steve/Software/blender-git/build_linux/bin/blender``
106
107 .. warning::
108 Do `not` try to use a binary installed using snap - it will `not` work.
109 Either install a debian package of Blender, or build it from source, and
110 use that.
111
112 All being well, Blender should start and show the usual Bonsai UI.
113
114 9. **Ensure Blender is behaving**: Exercise the interface a bit to be sure
115 Bonsai is working normally. Add a demo project, add some walls, etc.
116
117 One issue I ran in to was opening the wrong folder in step 7. Due to the
118 way VSCode/VSCodium works, I was getting two Bonsai plugins conflicting,
119 and failing in interesting ways. One symptom of this was the modal user
120 interface when adding walls was completely missing. Oh, and the debugger
121 completely failed to work.
122
123 If you are happy, quit Blender, and try setting some breakpoints in
124 VSCode/VSCodium then run Blender again, and try to trigger them. I set one
125 on the ``bim.add_sheet`` operator at:
126 ``extensions/.local/lib/python3.11/site-packages/bonsai/bim/module/drawing/operator.py:1456``
127
128 .. note::
129 I made the mistake of setting the breakpoint on the ``_execute``
130 methods ``def`` line, which did not work. This probably helped confuse
131 me when trying to get the addon to work.
132
133 If you get to this point, congratulations! You will now be 1000% more effective
134 when troubleshooting issues, and able to make many more contributions, fixes
135 and patches.
136
137 Blender Addon + VS Code Debugger
138 ------------------------------
139
140 Setting up debugging with Blender Addon is a bit simpler as it doesn't require Blender to be started in a special way
141 and debugger can be always attached later when it's needed.
142
143 1. Install Hextant Python Debugger Blender addon from `official repository <https://github.com/hextantstudios/hextant_python_debugger#installation>`_.
144
145 2. Open IfcOpenShell repository in VS Code and setup configuration for attaching debugger `per the instructions <https://github.com/hextantstudios/hextant_python_debugger#debug-a-blender-add-on-from-visual-studio-code>`_.
146
147 3. Start Debug Server in Blender (Blender -> System -> Start Debug Server)
148
149 4. VS Code -> Run and Debug -> "Python Debugger: Remote Attach" in dropdown -> Start Debugging
150
151 5. Now debugger is attached to Blender. You can set breakpoints in VS Code and use Debug Console.