Blender Add-on

Author: Daniel Mačura

To preface this post, I’ll assume you are familiar with Python and have a grasp of Blender. We’ll be using Blender 4.1, how ever other versions should work too. We’ll look into the Blender API and create a simple addon with UI.

So, what’s an addon?

Well, simply put, an add-on is a Python module that allows you to extend the functionality of Blender. For Blender to recognize a plugin, we need to define three requirements. Firstly, bl_info, stores add-on metadata, such as the name and minimum supported Blender version. Secondly, we need to implement register() and unregister() methods.

bl_info = {
	"name": "My add-on",
	"blender": (2, 80, 0),
	"category": "Object",
}
def register():
	print("Hello")
def unregister():
	print("Bye")

An astute reader may notice that this add-on doesn’t yet use Blender API functions, so let’s change that!

Your first addon

Let’s start simply, we’ll delete the default cube. To achieve this, we’ll import the Blender API and register a new operator. Operators allow us to define actions that can be executed from a menu, buttons, or a keyboard shortcut.

bl_info = {
	"name": "Remove default cube",
	"blender": (2, 80, 0),
	"category": "Object",
}

import bpy

class RemoveDefaultCube(bpy.types.Operator):
	"""Removes the pesky default cube"""		# The tooltip used in menus and buttons
	bl_idname = "object.remove_cube"		# A unique identifier for buttons and menu items to reference
	bl_label = "Remove default cube"		# The display name in the interface
	bl_options = {'REGISTER', 'UNDO'}		# Allow undo operator

	def execute(self, context):			# Implicitely called when running the operator
		objects = bpy.data.objects		# Get all objects in Blender save file
		objects.remove(objects["Cube"])		# Deletes the cube (assuming it exits)

def menu_function(self, context):
	self.layout.operator(RemoveDefaultCube.bl_idname)

def register():						# Called when the add-on is enabled
	bpy.utils.register_class(RemoveDefaultCube)
	bpy.types.VIEW3D_MT_object.append(menu_function)	# We add the operator to the 

def unregister():					# Called when the add-on is disabled
	bpy.utils.unregister_class(RemoveDefaultCube)

if __name__ == "__main__":				# Entry point for the Belnder script editor to use
	register()

After we press the play button in the text editor window in Blender, the add-on is ready to be tested. It can be found in the Object menu in the 3D viewport, or alternatively, it can be searched by typing the phrase Remove default cube.

Image of a Blender menu with the appended operator.
The add-on is appended to the end of the Object menu, clicking it will run the script.

Go to top File an issue