Blender Hack Blog

オープンソースの総合3DCGソフトウェアのBlenderのコード解析や開発を記録していきます。

macOS: Visual Studio CodeでBlenderの開発環境を構築する

はじめに

Blender開発者によるライブコーディングを観ていると、Visual Studio Codeがとても便利そうでした。 そこで、macOSにてVisual Studio Code(以下VSCode)でBlenderの開発環境を構築してみます。

www.youtube.com

手順

こちらのマニュアルを参考にしました。 https://wiki.blender.org/wiki/Developer_Intro/Environment/Portable_CMake_VSCode

VS Codeのインストール

VSCodeを本家からダウンロードしてインストールします。 zipファイルを展開して、アプリケーションにコピーします。

code.visualstudio.com

拡張のインストール

本家のマニュアルに列挙されている拡張をインストールします。

フォーマッタの設定

Code > Preferences > Settingsを開きます。 検索フォームに「format」と入力し、「Editor: Default Formatter」の設定で、「ms-vscode.cpptools 」を選択します。

インテリセンスの設定

本家のマニュアルを読むと少し面倒な工程が必要そうです。 まずは、ビルドとデバッグをできるように整えたいので、ここではこの工程はスキップします。

Blenderのビルド

tasks.jsonに以下を設定し、ビルドを実行します。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build Blender",
            "type": "shell",
            "command": "make",
            "group": "build"
        }
    ]
}

Blenderデバッグ版のビルド

前のステップでBlenderをビルドするとbuild_darwinというディレクトリ以下に様々なファイルが作成されます。 build_darwin/CMakeCache.txtにあるCMAKE_BUILD_TYPEReleaseDebugに変更します。 その後、VSCodeからビルドを実行します。

CMAKE_BUILD_TYPE:STRING=Debug

デバッガの設定

VSCodeからデバッグできるように、launch.jsonを以下のように記述します。

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch Blender",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/../build_darwin/bin/Blender.app/Contents/MacOS/Blender",
            "args": [],               // You could place a .blend file path here to open it by default, or any other Blender args
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false, // This could be set to true if you prefer an external console for debugging
            "MIMode": "lldb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Build Blender" // Optional; you can use if you want it to build before launching
        }
    ]
}

本家はLinux向けなので注意が必要です。 macOS向けに以下の修正を加えます。

ポイント
  • program: build_darwin以下に生成されるBlenderを指定
  • MIMode: lldbを指定

デバッグの実行

VSCodeで適当な場所にブレークポイントを設定します。 ここでは、Blenderを起動したら必ず実行される、void OVERLAY_extra_cache_init(OVERLAY_Data *vedata)の関数の先頭に設定することにします。

Runから「(lldb) Launch Blender」をクリックしてデバッグを実行します。 launch.jsonのpreLaunchTaskに「Build Blender」を指定したので、Blenderのビルド後、デバッガが起動します。

設定したブレークポイントで実行が停止します。 Xcodeのデバッガと遜色なくデバッグができます。

f:id:fixme:20210224122855p:plain
VSCodeデバッグに成功