5
Get/Set functions can be used for boolean, int, float, string and enum properties.
6
If these callbacks are defined the property will not be stored in the ID properties
7
automatically, instead the get/set functions will be called when the property is
8
read or written from the API.
14
# Simple property reading/writing from ID properties.
15
# This is what the RNA would do internally.
17
return self["testprop"]
20
def set_float(self, value):
21
self["testprop"] = value
23
bpy.types.Scene.test_float = bpy.props.FloatProperty(get=get_float, set=set_float)
26
# Read-only string property, returns the current date
29
return str(datetime.datetime.now())
31
bpy.types.Scene.test_date = bpy.props.StringProperty(get=get_date)
34
# Boolean array. Set function stores a single boolean value, returned as the second component.
35
# Array getters must return a list or tuple
36
# Array size must match the property vector size exactly
38
return (True, self["somebool"])
41
def set_array(self, values):
42
self["somebool"] = values[0] and values[1]
44
bpy.types.Scene.test_array = bpy.props.BoolVectorProperty(size=2, get=get_array, set=set_array)
48
# Note: the getter/setter callback must use integer identifiers!
50
("RED", "Red", "", 1),
51
("GREEN", "Red", "", 2),
52
("BLUE", "Red", "", 3),
53
("YELLOW", "Red", "", 4),
59
return random.randint(1, 4)
62
def set_enum(self, value):
63
print("setting value", value)
65
bpy.types.Scene.test_enum = bpy.props.EnumProperty(items=test_items, get=get_enum, set=set_enum)
70
scene = bpy.context.scene
72
scene.test_float = 12.34
73
print(scene.test_float)
75
scene.test_array = (True, False)
76
print([x for x in scene.test_array])
78
#scene.test_date = "blah" # this would fail, property is read-only
79
print(scene.test_date)
81
scene.test_enum = 'BLUE'
82
print(scene.test_enum)
85
# >>> 12.34000015258789
87
# >>> 2013-01-05 16:33:52.135340