API

Operators for globally hiding objects in all project scenes

To hide an object in the 3D Viewport window for the current scene we use the “object.hide_view_set” operator, which calls when we clicking the icon with an “eye” in the outliner. However, to hide the object in the viewport for all scenes of the project at once (clicking on the icon with the “monitor” image) a special operator is not provided. We can make it ourselves.

 

How to apply transformations to a mesh with the Blender Python API

To apply all transformations to an object, all its vertices must be multiplied by its global matrix, the matrix itself must be made identity.

To apply all transformations to the active mesh, we need to execute the following:

Message Bus

In Blender API there is a module that we can use to track the changes of any object’s properties available through the Python API. This module names “Message Bus”.

Let’s look at how we can make an event handler function to track changes to a property. For example – the location of the 3D cursor.

Setting colors for Blender collections with the Python API

Since Blender 2.9, the ability to assign color tags has been added for collections. Collection with such tag is displaying in a specific color in the outliner.

The collection color tag value is stored in its “color_tag” property. To change the color label of a collection, we need to assign a certain value to this property.

For example, to mark the current active collection in green:

How to find which collection contains the desired object

If the object you need is located in a hidden collection, it can be difficult to find it in large scenes.

To find out in which collection the desired object is located by its type, we need to execute the following. For example for cameras:

This code will return a list of cameras and collections in which they are located.

Context override

Some Blender operators can only be called from the workspace for which they are intended.

If you call from the Python console, for example, an operator intended only for working in the 3D Viewport area, the operator will not be executed:

or it will fail with an error message about the incorrect context:

However, we can still execute operators from a non-native working area. To do this, we can pass the first implicit parameter to any operator – a pointer to the context of the area in which this operator should be executed. This parameter commonly named the “overridden context”.

How to change current tool through the Blender Python API

To switch the active tool from the T-panel in the 3D viewport window, we need to call the appropriate operator and pass the “idname” of the required tool in its “name” parameter.

For example, to enable the “Select Circle” selection toll, we need to call:

How to import a Python module by the absolute path

Modules used in Blender scripts and add-ons are located in the same directory as the script or add-on, or in the Blender installation directory by the “blender_version\python\lib\” path. However, it is possible to load a module located elsewhere on the hard drive.

To do this, we need to load the module by its absolute path.

How to align an armature bone to a bone from another armature

To accurately align the bone of one armature with the bone of another armature, we need to correlate the global armature matrices and the local pose-bone matrices.

To do this, we need to execute the following:

After executing this code, the first bone of the “Armature.001” will be moved to the position of the first bone of the “Armature”.

How to calculate the Bounding Sphere for selected objects

Most often, for quick simplified calculations with the object’s geometry, their Bounding Boxes are used – the minimum parallelepiped into which this object is inscribed. But sometimes the Bounding Sphere – the minimum sphere into which an object can be inscribed – can provide greater accuracy and simplify the calculations. While the location and size of the object’s Bounding Box is available in Blender at once, the Bounding Sphere we need to calculate manually

Let’s write a function that, based on the object list, returns the coordinates of the center of their Bounding Sphere and its radius.

Multiline text in Blender interface panels

Unfortunately, Blender’s interface does not support word wrapping and multi-line text blocks. Text lines that are too long to be displayed in the panel are cut off. However, multiline text can still be displayed on the panel using several text elements instead of one.

First, let’s create a simple panel in the N-panel of the 3D Viewport window and place a label element with the desired text on it.

Features of using the scale matrix

To manipulate object transformations with matrices, Blender includes the “mathutils” module in which the “Matrix” class is defined. With this class, you can simply create the necessary transformation matrices – translation, rotation, and scale.

The following command is used to create a scale matrix: