Can a property getter function see the name of the property its getting?

Let’s say I define three properties in a property group:

class property_group(bpy.types.PropertyGroup):
    hello_world_1 : bpy.props.BoolProperty(
        default=False,
        get=GetProp,
    )
    hello_world_2 : bpy.props.BoolProperty(
        default=False,
        get=GetProp,
    )
    hello_world_3 : bpy.props.BoolProperty(
        default=False,
        get=GetProp,
    )

They all have the same get function. Can this function tell what property is being got?

Something like this:

def GetProp(self):
    match name_of_property_that_Im_try_to_get:
        case 'hello_world_1':
            return False
        case 'helo_world_2':
            return True
        case 'helo_world_3':
            return True

I tried the self, but that points to the property_group that containts the properties.

I think this should do it. You can use lambdas in property callbacks to add custom arguments. Btw in python by convention classes should be CamelCase and functions should be snake_case (Inverse of what you did).

class property_group(bpy.types.PropertyGroup):
    hello_world_1 : bpy.props.BoolProperty(
        default=False,
        get=lambda self: GetProp(self, "hello_world_1"),
    )
    hello_world_2 : bpy.props.BoolProperty(
        default=False,
        get=lambda self: GetProp(self, "hello_world_2"),
    )
    hello_world_3 : bpy.props.BoolProperty(
        default=False,
        get=lambda self: GetProp(self, "hello_world_3"),
    )
def GetProp(self, prop_name):
    match prop_name:
        case 'hello_world_1':
            return False
        case 'hello_world_2':
            return True
        case 'hello_world_3':
            return True
2 Likes

Thank you, it works! I didn’t even know what a “lambda” was until now.

As for the python naming conventions… well, let’s just say it’s a good thing that I’m the only one who will ever see my code. There’s a lot worse stuff in there than just misplaced camel case :stuck_out_tongue:

1 Like