Scripts

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.

Fake-bpy-module

Code autocomplete greatly simplifies writing scripts or developing add-ons for Blender. One of the best autocomplete modules for today is developed by Nutti. Last updated 20190718.

The project is hosted on the author’s GitHub: https://github.com/nutti/fake-bpy-module

The modules are distributed via pip or as a pre-generated-modules. Author also provides a module generator with which you can assemble autocomplete modules yourself.

Decorators for node tree updation delay

When we create a field on the add-on interface panel, the value of which changes something in the node tree, each time the user changes the field value the node tree recompiles. If the user changes the value in that field by holding and moving the mouse, too frequent node tree recompilation will cause Blender to hangs.

This problem can be solved using decorators for deferred updating of the node tree.

Code by Skarn.

Class for creating nodes by type

Class for quick node creation by their type.

Code by Skarn.

Changing objects visibility in the viewport and while rendering

The easiest way to hide and show rendering objects is to assign animation keys to them. To do this, move the cursor over the eye icon (visibility in the viewport) or camera (visibility when rendering) in the Outliner window, press the “i” key and then manage the created condition in the Graph Editor like the ordinary animation keys.

But this method is not always available. For example, we cannot assign visibility animation keys for collections, Blender will generate errors like:

“hide_viewport” property cannot be animated

or

“hide_render” property can not be animated

However, using the Blender Python API, we can control the visibility of such objects.

Blender autocomplete modules

Nutti, the author of the “fake-bpy-modules” project, has made the installation of the Blender Python API autocomplete modules through the pip platform. Pip installation is faster and easier, but sometimes we just need to copy the autocomplete modules to our project but now they are not included in the Nutti’s GitHub.

Copies of the autocomplete modules for Blender versions 2.79 and 2.80 can be downloaded directly from here: https://github.com/Korchy/blender_autocomplete

 

How to get global vertex coordinates

To get the vertex coordinates in the scene global coordinate system when the object’s scale was not applied, we need to multiply the local vertex coordinates by the object world transformation matrix: