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’

How to create camera through the Blender Python API

To create and add a camera to the scene with the Blender Python API we need to do the following:

1. Create a camera data-block

2. Create an object and link it with the camera date-block, we created

3. Add created camera-object to the scene

A new camera will be created in the current scene in its main collection.

Final code:

How to append an object from another blend file to the scene using the Blender Python API

The Blender Python API provides the ability to append or link objects from another *.blend file to the current scene using the “append” command.

To append an object (for example – a mesh) from another .blend file, we need to do the following:

Continue reading “How to append an object from another blend file to the scene using the Blender Python API”

Using the FileBrowser interface window to opening and saving files

In scripts and add-ons for importing-exporting files with formats that are not supported in Blender by default, we must give the user an ability to select files using the “FileBrowser” interface.

To open the file browser window, and after the user selects the necessary file, to return the path to it, we need to use the “ImportHelper” and the “ExportHelper” classes.

Continue reading “Using the FileBrowser interface window to opening and saving files”

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”