API

Dynamically setting the Max and Min values ​​for a custom property in the Blender Python API

Custom properties in the Blender Python API can be limited to setting with maximum and minimum values. In this case, the user, entering the desired value to the property field, will not be able to set a value that goes beyond the specified limits.

To set the minimum and maximum limits, we can specify the “min” and “max” parameters in our custom property and assign them the necessary limiting values.

get, set, update

“set” – is something like, you pour whiskey into a glass,
and “update” is like – “Wow, whiskey has appeared in the glass, it needs to drink!”,
and “get” – if your friend asks – “What’s in the glass?”, and you answer – “Whiskey”. Or – “Tea”.

How to apply modifier on selected objects

To apply an object modifier with the Blender Python API we can use the “bpy.ops.object.modifier_apply” operator. However, it processes only the active object.

If we need to apply a modifier, for example – Subdivision Surface, to several selected objects, we need to make each of them active, and then call the “modifier_apply” operator.

How to get all possible EnumProperty values for function parameters

When passing an EnumProperty value to the function parameters, just like with direct assignment, we must always specify the value from the constant list exactly, otherwise, Blender will throw an error.

For example, specifying an icon for a button in the UI we pass the text identifier to the “icon” parameter of the “layout.operator” function.

If it is not correct, Blender throws an error:

TypeError: UILayout.operator(): error with keyword argument “icon” – enum “ICON” not found in (‘NONE’, ‘QUESTION’, ‘ERROR’,…)

Creating the same UI panels in different Blender areas with no code duplication

To create several identical UI panels with the same set of fields and buttons in different areas in Blender, the easiest way is to copy the panel code and replace the parameters that determine in which area the panel will be shown.

The main disadvantage of this method is code duplication, which can make difficulties in the future – when editing panels, changes must be made to each copy of the code, instead of changing it at once for all panels.