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.

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

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: