1
$\begingroup$

I'm mostly a JavaScript developer and I enjoy to explore objects and quickly write new source code via F12/devtools in browsers like Chrome/Firefox. The browsers even allow to set breakpoints and single-step through the code etc.

I search something like that for Blender, but I only found the Python shell, which is more like a "text only" interface. Is there any powerful Blender/Python devtools like JavaScript devtools in browsers?

$\endgroup$
  • 1
    $\begingroup$ Yes, Blender text editor is not perfect. So, instead, you can use any IDE for that such as Visual Studio Code for example. But that requires a little bit of workaround. Do you want me to explain the setup details? $\endgroup$ – Yann Masoch Oct 15 '18 at 23:10
  • $\begingroup$ @YannMasoch Hi Yann, can you debug Blender via VSCode? That would be interesting. I just googled it, found this: youtube.com/watch?v=UVDf2VSmRvk So debugging seems to be possible, but 15minutes configuration for simple debugging, which is done via one F12 key press in the browser :S $\endgroup$ – lama12345 Oct 16 '18 at 0:14
  • 1
    $\begingroup$ In fact, t's not really possible like you can do in Chrome Dev Tools but you can have the python console for debugging inside VSCode. I'll show you the setup I use for add-ons dev. $\endgroup$ – Yann Masoch Oct 16 '18 at 2:32
3
$\begingroup$

Using an external IDE for Blender dev.

In this example, I am going to show you my setup using VSCode on Win 10. You can use any other IDE you want and it should also work on Mac and Linux too (I never tested).

Install Python and put Blender in your environment variables

I won't explain these steps as you can find great and easy tutorials on internet. Also, you can skip this part if you already did this.

You need to install Python in order code for Blender and you need to put Blender in your environment variables in order to launch it directly from your IDE (we'll see that later).

Create an addons directory

On your computer, wherever you want, simply create a directory called srcipts and a sub-directory inside called addons.

In my case, my directories are D:\Dev\Blender\scripts\addons

enter image description here

Your script or addon folder will be here as shown on the image. Using a folder to organize your addon is a good practice as you will be able to add sub-folders for libraries, icons and so on.

Specify the Scripts folder on Blender

Open the User Preferences on Blender via File menu or using Ctrl Alt U.

Under File tab, complete the Scripts path in order to point to the scripts folder we just created before (do not point to addons, just to scripts).

Save the User Preferences and close blender.

enter image description here

Open VSCode, start to code and launch Blender

Create your code inside VSCode. In this example, I created a simple script my_script.py called My Hello Addon that prints "Hello World!" when activated using the Operator my_script.hello.

enter image description here

bl_info = {
    "name": "My Hello Addon",
    "description": "",
    "author": "Yann Masoch",
    "version": (0, 0, 1),
    "blender": (2, 78, 0),
    "location": "3D View > Tools",
    "warning": "", # used for warning icon and text in addons panel
    "wiki_url": "",
    "tracker_url": "",
    "category": "Object",
}

import bpy

class MyHelloClass(bpy.types.Operator):
    bl_idname = "my_script.hello"
    bl_label = "My Hello Script"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        print("Hello world!") # Replace this line by your own code
        return {'FINISHED'}

def register():
    bpy.utils.register_class(MyHelloClass)

def unregister():
    bpy.utils.unregister_class(MyHelloClass)

if __name__ == "__main__":
    register()

Now, simply type blender or blender.exe in the terminal to launch Blender.

Activate the addon

At the first start, you have to activate the addon. Open the User Preferences on Blender via File menu or using Ctrl Alt U.

Under Add-ons tab, search for our addon My Hello Addon and make it active by checking the box.

Save the User Preferences.

You don't need to do this step every time. Once the addon is registered/activated, this step become optional!

enter image description here

Launch the script

Now, you just have to launch the script we registered as an operator by pressing Space and searching for its name.

enter image description here

On VSCode, Hello World will appear in the terminal section.

enter image description here

The terminal section will show all the outputs from Blender and/or Python to help you to debug your code.

In my code, I decided to register the class as an operator (search via Space) but you can launch it from a button, a menu or more. You will find addon examples or tutorials on internet.

Reload your code with F8

Every time you change your code, save it on VSCode and press F8 on Blender. It will reload all the Blender addons such as when Blender is starting.

That's it!

It looks a bit complicated and long but not really. Most of the steps have to be done only once. If your setup is done, you just need to open Blender from VSCode, update your code and press F8.

$\endgroup$
  • $\begingroup$ Thanks for the answer, I think the biggest win of this answer is to see the print output, but you can have the same by winkey+r, enter cmd and drag blender.exe in there, press enter. But this still is no sane debugging environment like F12/devtools, it is not even possible to enter single python code in the terminal for quick testing and there is no GUI introspection into Python objects $\endgroup$ – lama12345 Oct 16 '18 at 20:38
  • 1
    $\begingroup$ Your welcome. Hope that could help you a bit. Yes, using the terminal inside VSCode or using cmd is almost the same, it's just faster to have everything at the same place. For a quick testing you can also use the Python Console inside Blender Shift+F4 docs.blender.org/manual/en/dev/editors/python_console.html $\endgroup$ – Yann Masoch Oct 16 '18 at 21:07
  • $\begingroup$ I also made a video on using VS Code with the Blender Extension by Jacques Lucke: youtu.be/q06-hER7Y1Q $\endgroup$ – Jayanam Mar 8 '19 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.