The Blender API recommends creating custom object properties through the predefined bpy.props classes. For example, the bpy.props.IntProperty class is used to add an integer property. These properties are convenient, work well, and are easy to display on the user interface panels.
However, they are registering in bpy.types and any new property becomes available for all objects of a specific type, not just for the current object. Sometimes, when writing a script, it is much more useful to add the required dynamic property to only one or several objects in the scene.
Adding such a dynamic property to an object is very simple – for example, let’s add an integer property named “dynamic_prop1” to the active object and set it to 35.
1 |
bpy.context.active_object['dynamic_prop1'] = 35 |
Now we can access this object’s property in the script code:
1 2 |
bpy.context.active_object['dynamic_prop1'] # 35 |
But if we try to place it on the user panel in the usual way,
1 2 3 4 5 6 |
class TEST_PT_panel(Panel): def draw(self, context): self.layout.prop( data=context.active_object, property='dynamic_prop1' ) |
Blender will through an error:
RnaUiIremR: property not found: Object.dynamic_prop1
To display such a property on the user panel with no errors, we just need to change the format of the property name in the “property” parameter of the “prop” function. The dynamic property name must be enclosed in square brackets and enclosed in quotation marks:
1 2 3 4 5 6 |
class TEST_PT_panel(Panel): def draw(self, context): self.layout.prop( data=context.active_object, property='["dynamic_prop1"]', ) |
Now the dynamic property will be correctly showed on the UI panel and can be changed through the user interface.
*.blend file with an example of showing a dynamic property of an object on a custom UI panel