1
$\begingroup$

Here is my test code and picture:

import bpy


# Simple property reading/writing from ID properties.
# This is what the RNA would do internally.
def get_float(self):
    return self["testprop"]


def set_float(self, value):
    self["testprop"] = value

#bpy.types.Scene.test_float = bpy.props.FloatProperty(get=get_float,set=set_float)
bpy.types.Scene.test_float = bpy.props.FloatProperty()


bpy.context.scene.test_float = 12.34

enter image description here

When I don't explicitly set the "get/set" function for my property, after assigning the value, I can get the property value by 'scene["test_float"]', but if I explicitly set the "get/set" function, then if I try to type in the same commands, I will receive a error "KeyError: 'bpy_struct[key]: key "test_float" not found'" . So what should I do to get the same results by using explicitly "get/set" function?

$\endgroup$
  • $\begingroup$ you could simply retrieve the value using bpy.context.scene.test_float instead of looking it up with the bpy.context.scene["test_float"] syntax. This works in both cases for me. $\endgroup$ – aliasguru Dec 18 '18 at 10:36
1
$\begingroup$

It's named "testprop" not "test_float"

When a property defined via

bpy.types.Scene.test_float = bpy.props.FloatProperty(...)

is assigned a value scene.test_float = value it is stored. "behind the scenes" as a custom property scene["test_float"] ("second time above")

In the get / set example ("first time above") it is instead stored in a custom property named "testprop".

Instead, had the custom property been named "test_float" in the getter and setter, it would emulate the behaviour of the float property without a getter and setter, and be reflected in the value and existence of scene["test_float"] when set.

Perhaps this needs to be an edit in docs re what RNA would do internally.

Note if the getter is defined thus

def get_float(self):
    return self.get("test_float", 0.0)

def set_float(self, value):
    self["test_float"] = value

bpy.types.Scene.test_float = bpy.props.FloatProperty(get=get_float,set=set_float)

it will return a default value, in this case 0.0 even if the property has not been set.

$\endgroup$

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.