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:

Continue reading “Features of using the scale matrix”

Localization of Blender add-ons – with API

The classic way of localizing a Blender add-on (translating it into different languages) is convenient because requires just a single Blender Python API call – to get the currently used locale. This way is maximum universal, but Blender would not be Blender if it did not provide users an ability to localize add-ons through its own API.

The principle of creating multilingual add-ons using the Blender Python API is not much different from the classical one – we need to create a dictionary with all the variants of translations for all text strings from our add-on and use this dictionary in the localization.

Continue reading “Localization of Blender add-ons – with API”

How to get mesh data with modifiers

When referring to the mesh geometry – its vertices, edges, and polygons, Blender operates with the original data without the usage of modifiers added to the object.

For example, the number of vertices of the active object, we can get with the following command:

And it will be returned without considering the object’s Subdivision modifier, which modifies the mesh’s geometry, increasing it.

Continue reading “How to get mesh data with modifiers”

How to make an url button in Blender interface

In order to navigate to an URL-address from Blender – to open the required address in the browser, we can use the system operator “wm.url_open”.

To demonstrate this let’s create a simple tab in the N-panel with a single button, by clicking on which the required page will be opened in the browser.

Continue reading “How to make an url button in Blender interface”

How to get scene data from the bpy.context

To have the access to the current context, for example, to work with operators, and to the scene data, for example, to work with meshes, we use two predefined structures from the Blender Python API: “bpy.context” and “bpy.data”.

For example, we can pass them to a function:

However, in the “bpy.context” structure there is a “blend_data” pointer that links to the scene data. With this pointer, we can access scene data without passing it separately and explicitly.

Localization of Blender add-ons – classical way

If you want your add-on to be available for all Blender users around the world, you need to make the localization – translation of its interface into different languages.

The simplest way to make your add-on multilingual is the classic way, it is used in many other programs and requires the minimal usage of the Blender Python API.

Continue reading “Localization of Blender add-ons – classical way”

Removing collections through the Blender Python API

We can delete collections in Blender through the “collections” list in “bpy.data”.

To remove a collection we need to use the “remove” method from the collections list, specifying the collection to be removed in the parameter:

Continue reading “Removing collections through the Blender Python API”

How to add a constraint to the object with Blender Python API

We can assign constraints to objects through the object “constraints” property.

To add a constraint to the currently active object, we need to create a new constraint in the object’s “constraints” list, specifying its type in the parameters:

Continue reading “How to add a constraint to the object with Blender Python API”

Another way to add code autocomplete when developing Blender add-ons in Visual Studio Code

To improve the speed and convenience of developing Blender add-ons with Microsoft Visual Studio Code, this IDE can show the code auto-completion for common Blender API types.

This way of adding autocomplete is by Zen3515.

Continue reading “Another way to add code autocomplete when developing Blender add-ons in Visual Studio Code”

How to get camera FOV in degrees from focal length in mm

The camera angle ​​in Blender can be set both in millimeters – the Focal Length parameter, and in degrees – the Field of View parameter.

To get the Field of View in degrees from the Focal Length value in millimeters, we can use the following formula:

Continue reading “How to get camera FOV in degrees from focal length in mm”

How to create a Vertex Group and add vertices to it with the Blender Python API

We can access vertex groups for the active mesh through its “vertex_groups” structure.

Continue reading “How to create a Vertex Group and add vertices to it with the Blender Python API”

How to create a new mesh UV with the Blender Python API

To make a new UV with the Python API and set coordinates to its points, we need:

First – create a new UV with the desired name:

Next, to specify the coordinates of its points, we need to cycle through all the “loops” of the mesh: Continue reading “How to create a new mesh UV with the Blender Python API”

How to get a list of UV coordinates

You may need to get a list of coordinates of the mesh’s UV, for example, when exporting a mesh from Blender’s internal format to any external formats.

A list of the mesh UV-s can be got by accessing the “uv_layers” structure:

Continue reading “How to get a list of UV coordinates”

How to check the list of operators added to the area header

To add an operator to the area header, we can use the “append” and “prepend” functions specifying in its parameters the drawing function of the operator call button.

For example, to add an operator of adding the default cube to the header of the viewport (3D View) area, we need to define the operator drawing function:

and add it to the window header:

The operator button will appear in the header of the viewport window.

Now we can view a list of all the functions that add operators to the area header by the “_draw_funcs” property of the “draw” method.

Note that the “_draw_funcs” property would be defined only if custom operators have been added to the area header. If custom operators were not added to the header, an error will be thrown:

AttributeError: ‘function’ object has no attribute ‘_draw_funcs’