Blender 2.9 Python Addon Programming Tutorial

This tutorial by Jayanam is a good tutorial to start with. Firstly, it explains how to use the Microsoft Visual Studio Code. Secondly, it shows how to make a simple addon. However, if you are not familiar with Python, then best is to learn the basics on the W3 schools website first. But if you have some experience already but want to dive into Blender API, this is an excellent starting point. So here is the video “Blender 2.9 Python Addon Programming Tutorial”:

Microsoft Visual Studio Code

In case you don’t have the Microsoft Visual Studio Code, you can download that first. This is a freeware code editor by Microsoft. Visit Get started with Visual Studio Code to learn more about the editor. Mind that there is also the Microsoft Visual Studio editor, and that has also a free version. This later one is Visual Studio Community but this is not what most we use here.

The Init file

Jayanam loads first a template which is a so called __init__.py file. This is a file that Blender reads first. Sometimes there are more files for an addon, and then this like an index. In this init file you see a code like this (one of my own addons):

bl_info = {
    "name": "EV Express",
    "category": "3D View",
    "author": "New Media Supply",
    "blender": (2,80,0),
    "location": "View3D > right-side panel > EV EXpress",
    "description":"Addon to speed up the workflow setting up a scene to showcase your model",
    "warning": "",
    "wiki_url":"https://blendermarket.com/products/ev-express",
    "tracker_url": "",
    "version":(10,3,0)
}

The above piece of code is mostly for the Blender preferences. Here how it shows up in the preferences:

Example Preferences init file
BL info in the init file

Registering Classes

In you video you see a smart and modern way to register and unregister classes. Here is a similar explanation by DarkFall:

classes = [YOUR_CLASS_NAME, YOUR_OTHER_CLASS_NAME]

 
def register():
    for cls in classes:
        bpy.utils.register_class(cls)
 
def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)

So, what is registering a Class in Blender exactly? When you enable an addon that has a code as above, the Classes will be accessible elsewhere in Blender. We can now use it in Blender. And when you disable the addon, it will unregister the classes from memory.

What are operators in Blender?

Next, we make a new file that describes an operator? But what is an operator? An operator is a piece of code, an operator Class, that enables us to describe of code an action. And we use that in a seperate file because it’s cleaner or easier to maintain the code later. Though, it is not required: you can also code an operator and a menu etc in one file. Here is an example of an operator class:

class show_OT_indirectcube(bpy.types.Operator):
    """ Show Indirect Cube """
    bl_idname = 'show.indirectcube'
    bl_description = 'Show the indirect cube'
    bl_category = 'NMS'
    bl_label = 'Show'

    def execute(self, context):
        bpy.data.objects['For_Indirect_Baking'].hide_set(False, view_layer=None)
        return {"FINISHED"}

In the code above you see that this is a Class and the parent is an Operator Class. So it looks like this has not really much to do with Python. This is more the Blender API: we are using Blender’s Operator Class, and define our own operator. Behind the screen more is going on, but that is described in what we don’t see. Now, this is a fun part most don’t realize: This class will be instantiated (A copy) once. And it does this as good as continuously: “Every redraw”. And every redraw is like clicking on something in the UI. You can check this with a debug feature in Blender. And that keyword “self” refers to the instance. The keyword Context gives you access to data relative to the operation…pffww. Later more about that. So the above code let you unhides a particular object

Review of the Blender 2.9 Python Addon Programming Tutorial

I love the tutorial by Jayanam which is very clear and concise. Though, what I miss is some more in depth explanation. That’s why I tried to give a bit more info in this article in case you get stuck. I will definitely add some more text later on, because this is a very good starting point.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial
WordPress Cookie Plugin by Real Cookie Banner