Python

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.

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.

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.

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: