Tools/Debugging/Python Visual Studio

= Debugging blender python with Visual Studio =

Update 2023-01
Visual studio breaks this feature often, it's best to consider this feature non functional and not sink any time into trying to get it to work.

The page below is archived in its original state in case a working version ships in a future Visual Studio version.

Relevant upstream ticket:

https://github.com/microsoft/PTVS/issues/6712

Introduction
Visual Studio 2017 offers integrated debugging of python. This guide will help you set it up for use with blender.

This guide assumes you already know how to build blender, if you have not yet done so, please follow the Building_Blender/Windows guide.

Visual Studio 2017
Version 15.8.7 or later.


 * In the visual studio installer, enable the `Python Development` workload



Visual Studio 2019

 * Enable the Python Development Workload (just like for VS2017)
 * Enable the "Python native development tools" individual component. This is not enabled by default with Python Development Workload, but is required for the "Python/Native Debugger" option to show up later.

Project Creation
You should already have used `make.bat` to build blender before, in this section we'll use it to generate a Visual Studio project with the options enabled we need for debugging python.

Open up a command prompt and navigate to the blender source folder and run make full 2017 x64 nobuild pydebug

After a few seconds, it should tell you where the project files have been written.

Opening the project in visual studio
Navigate to the location and double click blender.sln

Selecting a configuration
You can build blender in both Debug and Release configurations.

You can switch between the builds in the Solution Configuration dropdown menu



Building Blender
First we have to build blender, however just building blender is not enough, the output folder also needs to be populated with additional dll's and startup scripts for blender to run properly, so instead of just clicking build. Expand the CMakePredefinedTargets group, right click on the INSTALL project and select build



after a while the output window should show something along these lines

Selecting the Python Debugger
Select Python/Native Debugger</tt> in the debugger dropdown.



Setting a breakpoint
In the solution explorer, expand scripts/blender_python_system_scripts/addons/io_scene_obj</tt>, double click on __init__.py</tt>, and put a breakpoint on the register function by placing the cursor on the following line and pressing F9</tt> or by clicking in the left margin of the line.

Running blender
Start blender by either pressing the F5</tt> key or clicking the play button.

Blender will now start and after a few moments your breakpoint should hit.

Happy Debugging!