3

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?

Heixue
  • 117
  • 1
  • 11
  • 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. – aliasguru Dec 18 '18 at 10:36

2 Answers2

8

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.

batFINGER
  • 84,216
  • 10
  • 108
  • 233
0

use self.testprop. the property exists this is just if you use self['testprop'] the self is refering to the local context and is not able to update the dict further. I guess the dict should be passed in argument to use self['testprop']

FkNWO
  • 74
  • 1
  • 7