1
$\begingroup$

doc


# Do I have to write get and set n times?
def get_float(self):
    return self["pos_1"]

def set_float(self, value):
    # do somthing ...

    self["pos_1"] = value

class Xxx(PropertyGroup):
    pos_1: FloatProperty(
        name="pos1",
        default=0.0,
        step=10,
        description="",
        update=update_location("x"),
        get=get_float, 
        set=set_float
    )
    pos_2: FloatProperty(...)
    ...
    pos_n: FloatProperty(...)

how to simplify the set? Thanks!

$\endgroup$
1

1 Answer 1

3
$\begingroup$

Use callable return:

you can return a just-in-time created method for the job. use the following code to simplify the process of building your functions:

def getter(att_str):
    def get_value(self):
        return self[att_str]
    return get_value

def setter(attr_str):
    def set_value(self, value):
        self[attr_str] = value
    return set_value
    

and anytime you need a get_value or set_value method use:

...,
get = getter("pos_1"),

you can also add extra functionality to your generated method by calling that inside your method definition:

def getter(att_str, extra_func = None, *args):
    def get_value(self):
        if not extra_func is None:
            extra_func(*args)
        return self[att_str]
    return get_value

def setter(attr_str, extra_func = None, *args):
    def set_value(self, value):
        if not extra_func is None:
            extra_func(*args)
        self[attr_str] = value
    return set_value

and call them this way:

# define extra functionality:
def add_cube(size):
    ...

# and use it here:
...,
get = getter("pos_1", add_cube, size),
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .