Fake-bpy-module

Code autocomplete greatly simplifies writing scripts or developing add-ons for Blender. One of the best autocomplete modules for today is developed by Nutti. Last updated 20190718.

The project is hosted on the author’s GitHub: https://github.com/nutti/fake-bpy-module

The modules are distributed via pip or as a pre-generated-modules. Author also provides a module generator with which you can assemble autocomplete modules yourself.

Decorators for node tree updation delay

When we create a field on the add-on interface panel, the value of which changes something in the node tree, each time the user changes the field value the node tree recompiles. If the user changes the value in that field by holding and moving the mouse, too frequent node tree recompilation will cause Blender to hangs.

This problem can be solved using decorators for deferred updating of the node tree.

Code by Skarn.

Continue reading “Decorators for node tree updation delay”

Class for creating nodes by type

Class for quick node creation by their type.

Code by Skarn.

Continue reading “Class for creating nodes by type”

How to protect your add-on from downloading through aggregators

With the Blender popularity growing, the number of add-ons created for it by third-party developers is growing too. A lot of high-quality professional add-ons are written for Blender now. Over time, the number of add-ons is becoming more and more. And on this wave aggregators appeared – programs and services independently searching for add-ons and allowing Blender users to install add-ons quickly, many at once, and bypassing add-on distribution channels selected by their authors. What caused a negative reaction of add-on developers.

Continue reading “How to protect your add-on from downloading through aggregators”

How to detect if Local View is on in 3D View window

The following command returns the 3D View areas list with enabled Local View mode:

 

Changing objects visibility in the viewport and while rendering

The easiest way to hide and show rendering objects is to assign animation keys to them. To do this, move the cursor over the eye icon (visibility in the viewport) or camera (visibility when rendering) in the Outliner window, press the “i” key and then manage the created condition in the Graph Editor like the ordinary animation keys.

But this method is not always available. For example, we cannot assign visibility animation keys for collections, Blender will generate errors like:

“hide_viewport” property cannot be animated

or

“hide_render” property can not be animated

However, using the Blender Python API, we can control the visibility of such objects.

Continue reading “Changing objects visibility in the viewport and while rendering”

Blender autocomplete modules

Nutti, the author of the “fake-bpy-modules” project, has made the installation of the Blender Python API autocomplete modules through the pip platform. Pip installation is faster and easier, but sometimes we just need to copy the autocomplete modules to our project but now they are not included in the Nutti’s GitHub.

Copies of the autocomplete modules for Blender versions 2.79 and 2.80 can be downloaded directly from here: https://github.com/Korchy/blender_autocomplete

 

How to get global vertex coordinates

To get the vertex coordinates in the scene global coordinate system when the object’s scale was not applied, we need to multiply the local vertex coordinates by the object world transformation matrix:

Class naming conventions in Blender 2.8 Python API

In Blender 2.8 API the requirements for the class and their identifiers naming are becoming tougher. The class name must match the following convention:

Where the {SEPARATOR} is two letters denoting the class belonging to a certain type (from which type the class is inherited):

  • HT – Header
  • MT – Menu
  • OT – Operator
  • PT – Panel
  • UL – UI list

The class identifier “bl_idname” mast match the class name.

Continue reading “Class naming conventions in Blender 2.8 Python API”

Changes in add-ons registration through the API in Blender 2.8

Add-on registration and removing were made with the “Window manager” (wm) in Blender 2.7 Python API:

In Blender 2.8 API add-on operators moved to the “preferences”:

 

3D Cursor location in Blender 2.8 Python API

3D-cursor location property

in Blender 2.8 API moved to “cursor” object

When trying to get the cursor location through the “context.scene.cursor_location” Blender throws an error:

‘Scene’ object has no attribute ‘cursor_location’

Add-on preferences panel

When developing add-ons it is often necessary to give an ability to set a number of parameters that affect the whole add-on work to the user. For example, the user can specify a directory for saving/loading files, set some default variables or switch between add-on modes. Of course, the interface for setting such parameters can be placed in the add-on panel, but it is better to place it in a separate add-on preferences panel, which is located in the “Preferences” window under the add-on installation panel.

The main advantage of the add-on preferences is that they don’t reset when Blender restarts. The user does not need to configure the add-on preferences each time, it’s enough to set the necessary parameters once, personalizing the add-on for convenient work.

Let’s create an add-on and define a parameter, placing it in the add-on preferences panel.

Continue reading “Add-on preferences panel”

Matrix, vector and quaternion multiplication in Blender 2.8 Python API

In Blender 2.7 the “*” (star) operator is used in the matrix, vector, and quaternion multiplication. In Blender 2.8 it is replaced with the “@” (at) operator.

If the “*” operator is used in vector, matrix or quaternion multiplication in Blender 2.8 it throws an error:

Element-wise multiplication: not supported between ‘Matrix’ and ‘Matrix’ types

Proper use of the “@” operator:

 

Snapping elements property in Blender 2.8 Python API

The snapping elements property from Blender 2.7

changed in Blender 2.8 API to

 

Accessing the pivot point type in Blender 2.8 Python API

The “pivot_point” property from Blender 2.7

in Blender 2.8 API moved to:

 

use_drag_immediately property in Blender 2.8 Python API

The “use_drag_immediately” property from Blender 2.7

in Blender 2.8 API moved to

 

Changing the current coordinate system in Blender Python API

In Blender 2.7 the current coordinate system could be changed through the

property. In Blender 2.8 coordinate system access moved to the scene collection of “TransformOrientatiosSlots” objects. To get or set the current coordinate system the “transform_orientation_slots” collection is used.

How to set object (mesh) to active in Blender 2.8 Python API

To set mesh (object) as active in Blender 2.8 Python API the “context.view_layer” is used instead of “context.scene”.

When trying to make object active with “bpy.context.scene.objects.active” Blender throws an error:

AttributeError: bpy_prop_collection: attribute “active” not found

To make object active in Blender 2.8 use the following code:

 

Selecting objects (meshes) in Blender 2.8 Python API

According to Blender 2.8 Python API changes mesh (object) can be selected with using getters and setters.

When trying to check the selected status of the mesh through the “bpy.context.active_object.select” property, Blender throws an error:

AttributeError: ‘Object’ object has no attribute ‘select’

To check whether an object is selected in Blender 2.8 use a getter:

To select an object in Blender 2.8 use a setter:

To unselect an object use the same setter:

 

How to check what version of Python interpreter is used in Blender

To find what Python interpreter version is used in current Blender version type the following commands in Python Console window in Blender:

It means that the version of Python used in Blender is 3.7.0.

To make it more readable type the following command:

or with full info:

Porting add-on from Blender 2.7 to Blender 2.8

In the latest version 2.8 of Blender developers have made many changes in API, so all the scripts and add-ons written for earlier Blender versions (2.7 and below) have stopped working. To run your add-ons in the new Blender 2.8, you need to port them – correct their code to work properly with the new Blender API.

To enable your add-on in Blender 2.80 you have to make the following changes in code:

Continue reading “Porting add-on from Blender 2.7 to Blender 2.8”

Using Microsoft Visual Studio Code as external IDE for writing Blender scripts/add-ons

Blender has its own built-in text editor for writing scripts and add-ons, but it’s much convenient to develop them in external IDEs that provide the user with more features such as autocomplete, syntax highlighting, integration with version control systems and other tools that make development faster and easier.

One of these IDEs is Visual Studio Code from Microsoft. This is a free universal environment that supports development with various programming languages, including the Blender API language – Python.

Continue reading “Using Microsoft Visual Studio Code as external IDE for writing Blender scripts/add-ons”

How to check if Blender object property/attribute is read-only

To check is an attribute/property of any Blender object (mesh, node, modifier, etc.) read-only:

  1. With the is_property_readonly function, execute the following command:

<object> .is_property_readonly (‘<property name>’)

  1. Through the rna structure, execute the following command:

<object> .bl_rna.properties [‘<property name>’]. is_readonly

For example, for the “is_editmode” mesh property (is the mesh in edit mode or not):

How to get the version of the add-on installed in Blender

A complete list of add-ons installed in Blender we can get using the addon_utils:

Having the add-ons list, we can get the version of the desired add-on by its name with the following code:

Where the ADD-ON_NAME is the name of the desired add-on.

If the add-on is missing a version indication, the default result will be returned.

 

How to programmatically switch between vertex, edge and face selection mode

Change the mesh_select_mode property to switch in mesh edit mode between vertex, edge and polygon selection mode in Blender.

For vertex selection mode:

Edge selection mode:

And face selection mode:

The multi-select mode is enabled too. For example for vertex and face selection mode:

In BMesh for vertex, edge and face selection mode:

For multi-select in BMesh: