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’

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:

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”

Where did the code completion in the python console in Blender 2.82 disappear

In Blender 2.81 and earlier, you could see the possible code completion by pressing “ctrl + space” in the python console. This key-shot shows nothing in Blender 2.82.

Code autocompletion in the python console was not removed in 2.82, it was moved to the “tab” key.

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”