1
$\begingroup$

I created an Add-on with lots and lots of min and max values. How can I made them looking at each other for the min and max value?

import bpy 
from bpy.props import FloatProperty

class My_Props(bpy.types.PropertyGroup):


    My_Prop_range_min: FloatProperty(           name="My_Prop_range_min",
                                                default=1,
                                                min=0,
                                                max=100, # <--- This should look at "My_Prop_range_max"
                                                )

    My_Prop_range_max: FloatProperty(           name="My_Prop_range_max",
                                                default=4,
                                                min=0, # <--- This should look at "My_Prop_range_min"
                                                max=100,
                                                )

The minimum property should not be able to have a higher value than the maximum property is set, and the maximum property should not be able to have a lower value than the minimum property is set.

Is that possible? How can I do this?

enter image description here

$\endgroup$
2
  • $\begingroup$ The min and max settings are static absolute values. You could however use an update function to check if the My_Prop_range_min >= My_Prop_range_max and if so force it to be a lower value. docs.blender.org/api/current/bpy.props.html $\endgroup$
    – Ratt
    May 12, 2021 at 13:21
  • $\begingroup$ I can't find any example on the Property Definition page. How can I make them update by changing the value in the UI? $\endgroup$
    – Andi
    May 13, 2021 at 8:13

1 Answer 1

2
$\begingroup$

The following example should point you in the right direction:

Any time the my_prop_range_min is changed in the UI the update function upd_min is called. The function then compares the UI setting to the current value of the my_prop_range_max. If the value of my_prop_range_min >= my_prop_range_max you just force it to be less than the value of my_prop_range_max. Similar when comparing max to min.

import bpy


def upd_min(self, context):
    if self.my_prop_range_min >= self.my_prop_range_max:
        self.my_prop_range_min = self.my_prop_range_max - 1


def upd_max(self, context):
    if self.my_prop_range_max <= self.my_prop_range_min:
        self.my_prop_range_max = self.my_prop_range_min + 1


class TEST_PG_props(bpy.types.PropertyGroup):
    my_prop_range_min: bpy.props.FloatProperty(
        name="My_Prop_range_min",
        default=1,
        min=0,
        max=99,
        update=upd_min,
        )

    my_prop_range_max: bpy.props.FloatProperty(
        name="My_Prop_range_max",
        default=4,
        min=1,
        max=100,
        update=upd_max,
        )


class TEST_PT_test_panel(bpy.types.Panel):
    bl_idname = "TEST_PT_test_panel"
    bl_label = "Test Panel"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "Test Tab"
 
    def draw(self, context):
        my_props = context.scene.my_props
        layout = self.layout
        row = layout.row()
        row.prop(my_props, "my_prop_range_min", text="min")
        row.prop(my_props, "my_prop_range_max", text="max")


classes = [
    TEST_PG_props,
    TEST_PT_test_panel,
    ]


def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.Scene.my_props = bpy.props.PointerProperty(
            type=TEST_PG_props)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    del bpy.types.Scene.my_props

if __name__ == "__main__":
    register()
$\endgroup$
1
  • $\begingroup$ Thank you for the example! That way can I figure it out. $\endgroup$
    – Andi
    May 15, 2021 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.