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.

For example, when we create a custom integer property “limited_value”, we can define limits from 0 to 25 to it:

Now, if we will try to set this property to a value outside the specified range, for example, “50”, the property will only be assigned the maximum possible value – “25”.

The “max” and “min” values ​​are static. However, sometimes it is necessary to dynamically change the specified limits while the add-on is running.

If, instead of a static value, we will try to define and use a function that dynamically returns the necessary limits, for example, for the maximum value:

Blender will throw an error:

TypeError: an integer is required (got type function)
ValueError: bpy_struct registration error: IntProperty could not register

This occurs because the “max” and “min” parameters only need to be set as numeric values ​​when defining the IntPropert property.

So, we cannot set the dynamic parameters “max” and “min” directly. However, we can still achieve the desired behavior of the property by overriding its “set” and “get” functions.

First, let’s define two additional custom properties in which we will store the maximum and minimum values ​​we need:

They are dynamic, and we can easily change them by placing them on the UI panel, or directly in the code:

Let’s define the “get_limited_value” and “set_limited_value” functions. In the “set_limited_value” function, which is responsible for assigning a value to the property, we will control whether it has gone beyond the specified limits and, if it does – correct it.

The “get_limited_value” function simply returns the value of the property.

Defining the property, let’s override its “get” and “set” functions with ours:

Now, each time a value is assigned to the “limited_value” property, the overridden function “set_limited_value” will be called. In this function, the value will be corrected in accordance with values specified in “dynamic_min” and “dynamic_max” properties.

Let’s show all our properties to the UI panel in the “3D Viewport” window:

Now we can set values ​​for the “limited_value” property, which always will be limited with specified in “dynamic_min” and “dynamic_max”. At the same time, we can dynamically change these limits any way we need.

The full code:

5 2 votes
Article Rating
Notify of

4 Comment
Oldest Most Voted
Inline Feedbacks
View all comments
make this
make this
1 year ago

This code does not seem to work.

1 year ago
Reply to  make this

It was tested and it works.
If something wrong, you can check the errors in the system console (main menu – window – toggle system console)

make this
make this
1 year ago
Reply to  Nikita

Thanks for the reply.
Excuse me. I used it again and it works!

Thank you. Great script.

10 months ago
Reply to  Nikita

Yes thank you so much for supplying this