Build / debug Blender with Visual Studio Code on Linux

Visual Studio Code is an open source and multiplatform IDE for code editing (and compiling, debugging, etc.) available here : https://code.visualstudio.com/

I wanted to see if I could build, run and debug Blender using this IDE, and the anwser is : yes. Here is how to do it.

Get Blender’s sources and dependencies

Based on : https://wiki.blender.org/index.php/Dev:Doc/Building_Blender/Linux/Generic_Distro/CMake. Here are the commands I used :

mkdir blender-git
cd blender-git
git clone --recursive https://git.blender.org/blender.git

This will get the main Blender sources. It’s quite fast, but depending on your internet connection speed, it can take a few minutes.

The next part is a lot longer : it will retrieve everything needed to build the third party libraries, and then build them. Depending on your computer it can be very long, so be patient. And check regularly, because it might ask for your password more than once !

cd ..
mkdir -p deps/src
mkdir -p deps/install
./blender/build_files/build_environment/install_deps.sh --with-all --source <path_to_blender-git>/deps/src --install <path_to_blender-git>/deps/install

<path_to_blender-git> must be an absolute path to your blender-git folder. Note that you must also create the deps/src and deps/install folders prior to calling install_deps.sh, or else you might have some errors telling you that you don’t have the rights to create folders.

Install Visual Studio Code

Once you’ve installed Visual Studio Code, install the following addons :

  • C/C++ (Microsoft) – C/C++ support. Required
  • CMake (twxs) – CMake syntax coloring, code completion, etc. Optional but useful if you have to modify a CMake script file
  • CMake Tools (vector-of-bool) – Add Configure/Build features based on CMake. Required

Note that CMake Tools plugin requires CMake version 3.7.1 or greater, with server mode enabled (if you installed a recent prebuilt version from the CMake website, it should be enabled by default. But if you build your own, make sure it’s enabled)

Configure Blender’s project

Now, open Visual Studio Code, and open the blender-git/blender folder. If CMake Tools worked correctly, the status bar should look like this:

Before going further, we will setup the project to configure CMake to use the dependencies. Open the Preferences, then select “Workspace Settings” to only modify the Blender’s project settings. In this settings file, add the following options (there are comments in case you’re wondering what they do) :

// Place your settings in this file to overwrite default and user settings.
{
    //
    // This is where the build system will create intermediate files
    // I like to keep this out of my source folder to avoid
    // unintentionally commiting them :)
    //
    "cmake.buildDirectory": "${workspaceRoot}/../build", 

    //
    // This will setup the install path path. This option is needed
    // because Blender needs to deploy all its scripts files, and
    // this is not done during building, but during install.
    // So we need to setup this, and run CMake: Install once
    // before being able to debug. Note that this little complexity
    // is due to the way Blender is built/deployed
    //
    "cmake.installPrefix": "${workspaceRoot}/../build/bin",

    //
    // Options sent to CMake when it's configured. Those are the
    // options found in BUILD_NOTES.txt, converted to JSON format.
    // I didn't put all of them here, so copy from your own file.
    //
    "cmake.configureSettings": {
        "WITH_CODEC_SNDFILE": "ON",
        "PYTHON_VERSION": "3.5",
        "PYTHON_ROOT_DIR": "<blender-git>/deps/install/python-3.5",
        "WITH_OPENCOLORIO": "ON",
        "OPENCOLORIO_ROOT_DIR": "<blender-git>/deps/install/ocio"
.
        //
        // The rest of the options.
        // Don't forget to copy them !!
        //
    }
}

Notice that the options in BUILD_NOTES.txt are not formatted the same way as CMake Tools want, so you need to format them like in the example above.

Also note that even though you can have the -DWITH_OPENCOLLADA=ON option, install_deps.sh seems to forget to add the path to OpenCollada, so you need to add it yourself. Just add the following option:

"OPENCOLLADA_ROOT_DIR": "<blender-git>/deps/install/opencollada",

Now, click on the status bar on the "CMake: No Project: Unconfigured" part. It should pop a dropdown on the upper part of VSCode looking like this:

Just select debug, and when CMake’s done working, go to the Debug panel of VSCode. On the upper left corner, you should see this :

Since you haven’t configured the debug session, it’s perfectly normal :) Click on the little "No Configuration" dropdown, and you should see this :

Select C++ (GDB/LLDB) and it should create and open a launch.json file. The last step is to configure it. In my setup, it looks like this (the parts with a comment are the ones to might want to modify) :

{
    "version": "0.2.0",
    "configurations": [
        {
            //
            // the name of this configuration. Choose what you want
            //
            "name": "Debug",
            "type": "cppdbg",
            "request": "launch",
            //
            // Point to Blender's executable.
            //
            "program": "${workspaceRoot}/../build/bin/blender",
            "args": [],
            "stopAtEntry": false,
            //
            // Execute in the same folder as Blender's executable.
            //
            "cwd": "${workspaceRoot}/../build/bin",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
             {
             "description": "Enable pretty-printing for gdb",
             "text": "-enable-pretty-printing",
             "ignoreFailures": true
             }
            ]
        }
    ]
}

Running / Debugging

And now, building is as simple as using the CMake: Build command (or even better, just F7 by default :p)

Just run a CMake: Install once to install all needed scripts into the folder where Blender’s executable is built, and you’re good to go. You only need to install when you do a clean/rebuild. If you only build Blender’s exe, you don’t need to re-install everytime :)

And debugging is as simple as putting a breakpoint where you want, and hitting F5. Well, provided it’s C/C++ source. To debug the Python scripts I don’t really know how that would work in VSCode since I never tried. But that might be the subject of a next article, if it’s feasible :)

Conclusion

It was my first time using Visual Studio Code, and I must say I’m very impressed by the ease of use and feature set ! Out of the box, it has Git support, and through the embedded extension manager you can enable C/C++ building/debugging/etc., CMake support, etc. with only a few clicks !

And then the setup part is completely straightforward ! You have a .vscode folder in your project containing the various .json setting files, and in each of those files, autocompletion works like a charm so you can easily find what you want, without even having to bother looking online for documentation / help !! (in my first tests, I was on an old computer where I had to specify the path to my custom CMake, and the path to my custom GDB, and I found them in a few seconds without having to leave the IDE)

Anyway, I highly recommend you to give a try to this software, CMake support is almost perfect (I still have to delete the build folder manually whenever I want to change the options CMake was configured with, but other than that, perfect) and building / debugging is fast, intuitive, user friendly and powerful.