Tools/Debugging/Python Visual Studio

From Blender Developer Wiki
Jump to: navigation, search

Debugging blender python with Visual Studio

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.

Prerequisites

  • Visual Studio 2017 15.8.7 or later

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

Vs python installer.png

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

1 Microsoft Windows [Version 6.1.7601]
2 Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
3 
4 C:\Windows\system32>cd /d k:\BlenderGit\blender
5 
6 k:\BlenderGit\blender>make 2017 x64 nobuild pydebug


Tip:

Here's what each of the additional parameters mean.

  • 2017 This explicitly select Visual Studio 2017 in case there are multiple versions of Visual Studio installed.
  • x64 This explicitly selects a 64 bit build of blender, while 32 bit builds are still supported (use `x86` for that) it is not recommended unless you really need a 32 bit build.
  • nobuild This prevents `make.bat` from building blender for you, and will only generate a Visual Studio solution for you that you can later open with the IDE.
  • pydebug This sets up the project for python debugging and will cause CMAKE to include all .py scripts from the release folder and all users scripts in sub-projects in the solution.


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

1 -- Configuring done
2 -- Generating done
3 -- Build files have been written to: K:/BlenderGit/build_windows_Full_x64_vc15_Release


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.

Tip:

Debug is a specially optimized build for debugging code, this build of blender will be bigger and slower, but it will be easier to debug C/C++ code.

Release is a highly optimized version of blender, it will be fast, but it will be problematic to diagnose issues in the C/C++ Code.


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

Vs configuration dropdown.png

Tip:

If you are only going to debug python code select the Release configuration, if you also need C/C++ debugging select Debug


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

Vs blender build.png

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

1 ========== Build: 149 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


Selecting the Python Debugger

Select Pyhon/Native Debugger in the debugger dropdown.

Vs debugger selection.png

Tip:

If this option is missing, this is most likely due to the Python Development workload not being installed, see the Prerequisites section on how to install.


Setting a breakpoint

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

329 def register():
330     for cls in classes:
331         bpy.utils.register_class(cls)
332 
333     bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
334     bpy.types.TOPBAR_MT_file_export.append(menu_func_export)

Running blender

Start blender by either pressing the F5 key or clicking the play Vs play.png button.

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

Happy Debugging!

Vs blender breakpoint.png