1
$\begingroup$

Is there a way to force Blender to generate a custom property ahead of time? It looks like custom properties added to objects are only generated when the user changes them from the default in the UI, which makes sense. But I've defined a setter handler in a custom property that spreads it to other objects (object_b.custom_prop = object_a.custom_prop), and Blender halts execution of that setter when it encounters an object that hasn't had its property "generated" yet.

Edit: After discovering the console output messages, Blender throws the error "KeyError: 'bpy_struct[key]: key "PropertyName" not found' when it encounters one of these objects before I manually prime it by manipulating the UI control for it. Still have not found an answer to this.

So I'm wondering if there is something I can do in this situation that will force the property to be generated? This is essentially the code in my setter that spreads it to other objects:

for obj in selected_list:
   value = obj["custom_prop"]
   value = math( value, active_object["custom_prop"] )
   obj["custom_prop"] = value

It works fine as long as I "prime" the involved objects by manipulating their property first. But otherwise, it just stops when it encounters one that hasn't been primed.

$\endgroup$

1 Answer 1

1
$\begingroup$

When a custom property is accessed as obj["custom_prop"], it will throw an error in the console ('bpy_struct[key]: key "PropertyName" not found') if that property has not been assigned yet. To fix this, you can instead use obj.get("custom_prop", DEFAULT_VALUE). This will not throw an error, and will return the default value if the property doesn't yet exist.

I'm assuming we should ALWAYS use the syntax obj.get("custom_prop", DEFAULT_VALUE) instead of obj["custom_prop"]? Can anyone confirm this? Or at least in situations where we may be reading the value before writing to it.

I'm very new at this, so anyone feel free to chime in here.

$\endgroup$

You must log in to answer this question.

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