How to install required packages to the Blender Python with pip

For add-ons developing, sometimes it is necessary to use packages that are not included in the core set of the Blender Python interpreter. Most often, the dependencies you need are included in the Python Package Index (PyPI) and can be installed to Blender through the Pip package management system.

The Pip Package Manager is already installed in Blender. However, Blender does not allow us to install the desired packages in a common way with the “pip install package_name” command.

Continue reading “How to install required packages to the Blender Python with pip”

How to rotate objects through the Blender Python API

For objects rotation, we can use the “rotation_euler” property. This property is a list of three items, each of them corresponds to the rotation angle around the X, Y, and Z-axis. The otation_euler[0] contains the rotation angle around the X-axis, rotation_euler[1] – around the Y-axis, and rotation_euler[2] – around the Z-axis. To rotate an object we must set a rotation angle to the appropriate field of the property.

For example, to rotate an active object around the X-axis to the 90 degrees angle we must execute the following command:

math.radians is used to convert degrees to radians.

How to switch between flat and smooth shading through the Blender Python API

The flat/smooth shading mode is regulated through the “use_smooth” property of each polygon of the mesh.

To enable smooth shading we need to set the “use_smooth” property of each mesh polygon to “True”.

For active object:

To enable flat shading – set the “use_smooth” property of each polygon to “False”.

To make new shading mode visible – force update mesh data:

Switching to the view from camera throug the python API

To switch to the view from the active camera we can execute the following command:

This operator works in the toggle mode, so the next executing of the same command returns the view to the previous state.

Continue reading “Switching to the view from camera throug the python API”

Switching between coordinate systems

To switch from a global coordinate system to a local coordinate system of an object, we need to multiply the global coordinates by the inverted matrix of an object:

To switch from the local coordinate system of the object to the global coordinate system, we need to multiply the local coordinates by the matrix of an object:

Edit Source

For those who want to know “how Blender works” to write their own scripts and add-ons, the developers have provided a very convenient option – the ability to view any element source code in a single click.

To see the source code of any element: open the “Text Editor” window; then simply click on the desired element with the right mouse button and select “Edit Source”.

Continue reading “Edit Source”

Objects pointers brokes if undo/redo operation is used

In Blender 2.8x the undo/redo operation system (ctrl+z / ctrl+shift+z) has been completely rebuilt. One of the results of this API change is when the undo operation is performed, all objects in the scene are fully recreated – the current object is destroyed, and a completely new object is created instead. Pointers to the old objects (before the undo operation) now point to invalid objects that can no longer be used.

Continue reading “Objects pointers brokes if undo/redo operation is used”

User interface elements alignment by columns

Interface elements in custom user panels often do not correspond to each other in size. As a result – the overall panel layout does not look beautiful. As an example, let’s create a custom panel and place an operator button and an input text field on it.

Can we make it more accurate?

Continue reading “User interface elements alignment by columns”

How to get all collectons list

To get a list of collections with all internal collections, we can use the following function:

Continue reading “How to get all collectons list”

Setting operator parameters features

When you make a button in a custom UI to call the operator with passing the necessary parameters to it, pay attention that only the parameters specified explicitly will be sent to the operator. Other operator parameters will remain with default values.

For example, we need to execute the “transform_apply” operator – applying object transformations with applying just the scale.

If we call the operator as follows:

But all transformation (scale, rotation, and position) will still be applied to the object.

Continue reading “Setting operator parameters features”

Render from console only on the specified GPU devices

Sometimes, to effectively distribute the hardware load it is necessary to make a render from Blender only on certain video cards of several of them.

To do this we can use a simple script in which specify the numbers of devices to be used for rendering.

Continue reading “Render from console only on the specified GPU devices”

Updation autocomplete modules fake-bpy-modules for Blender Python API

Blender Python API autocomplete modules by Nutti update – added branch for Blender 2.81a.

Last release: 2020.01.11.

Author GitHub: https://github.com/nutti/fake-bpy-module

Only files (without pip installation) https://github.com/Korchy/blender_autocomplete

Assigning custom properties to vertexes

Working with mesh geometry, it may be necessary to assign each vertex some custom properties or data that must be written to the blend-file when saved, and which must be accessed later.

However, an attempt to assign the necessary data to the vertexes through the creation of custom properties fails. Instead of the custom vertex property, only a tuple with reference to the type of the property is created.

Continue reading “Assigning custom properties to vertexes”

How to split and join Blender interface windows thruough the python API

A set of operators is provided in Blender for manipulating with the location of its interface windows.

To split the current window (using the current context) into two in a specified ratio, we need to execute the following operator:

With:

  • direction – set the splitting direction (‘HORIZONTAL’ or ‘VERTICAL’)
  • factor – percentage ratio of splitting windows

Continue reading “How to split and join Blender interface windows thruough the python API”

Custom property

The Blender API provides a set of simple property types described in bpy.props (IntProperty, BoolProperty, etc.). But the basic types are not always enough, sometimes we need more complex ones. The Blender API allows to group simple properties to make complex properties.

Let’s consider such complex property creation and make the 3×3 matrix property as an example.

Continue reading “Custom property”

How to get current Blender version number through the Python API

To get current Blender version number through the Python API we need to execute the following command:

or

Continue reading “How to get current Blender version number through the Python API”

Calling functions by pressing buttons in Blender custom UI

The button click is basically connected with the operator calling in the Blender user interface. However, some times actions, that need to be performed when a button is pressed, are quite simple and do not require a separate operator for them. And it makes no sense to fill a registered operators stack with a multitude of specific operators designed to perform one highly specialized function. It would be much more convenient to associate a button press with a separate function call but the Blender API allows to associate buttons only with an operator call.

To solve the problem of creating a separate operator for each button we can use the fact that the operator can be called with the input parameters.

Continue reading “Calling functions by pressing buttons in Blender custom UI”

How to get a list with all possible values of an EnumProperty

To correctly set values to an EnumProperty type we need to know all its possible entries.

If we try to set a value to an EnumProperty that is out of its available variants, Blender will throw an error:

TypeError: bpy_struct: item.attr = val: enum “xxx” not found in (‘value_001′, value_002’, …)

where:

xxx – the value we tried to set to an EnumProperty

value_001, value_002, … – possible EnumProperty values

Continue reading “How to get a list with all possible values of an EnumProperty”

How to move cursor to vertex

To translate cursor to the desired mesh vertex execute the following code:

Attention to the multiplication order – world matrix should be left.

How to link a new object to a scene in Blender 2.80 Python API

In Blender 2.79 Python API we can add a new object to a scene with:

If we try to do this in Blender 2.80, it throws an error:

‘bpy_prop_collection’ object has no attribute ‘link’

In Blender 2.80 API new objects mast be linked not to the scene, but to the scene collections:

To link a new object to the active scene collection:

To link a new object to the collection by its name:

 

How to move the cursor to the active strip start/end position in Blender VSE

To move the cursor to the start position of the active strip in Blender VSE:

To move the cursor to the end position of the active strip: