Since Blender 3.0 (relevant commit
commit
)
The UI for a custom property is described by a IDPropertyUIManager
that can be retrieved by the id_properties_ui
method.
import bpy
context = bpy.context
obj = context.object
# create the property, set it the initial value
if "scale_factor" not in obj:
obj["scale_factor"] = 1.0
# get or create the UI object for the property
ui = obj.id_properties_ui("scale_factor")
ui.update(description = "Scale factor")
ui.update(default = 1.0)
ui.update(min=0.0, soft_min=0.0)
ui.update(max=10.0, soft_max=10.0)
# test UI in text editor footer
def draw(self, context):
ob = context.object
self.layout.prop(ob, '["scale_factor"]')
bpy.types.TEXT_HT_footer.append(draw)
Before Blender 3.0
The UI for a custom property is described by a dict
object, stored in a special/hidden _RNA_UI
property.
The internal blender addons tend not to access _RNA_UI
directly, but use the rna_prop_ui.rna_idprop_ui_prop_get
function to get or create the relevent "ui" object.
import bpy
context = bpy.context
obj = context.object
from rna_prop_ui import rna_idprop_ui_prop_get
# create the property, set it the initial value
obj["scale_factor"] = 1.0
# get or create the UI object for the property
ui = rna_idprop_ui_prop_get(obj, "scale_factor", create=True)
ui['description'] = "Scale factor"
ui['default'] = 1.0
ui['min'] = ui['soft_min'] = 0.0
ui['max'] = ui['soft_max'] = 10.0
# test UI in text editor footer
def draw(self, context):
ob = context.object
self.layout.prop(ob, '["scale_factor"]')
bpy.types.TEXT_HT_footer.append(draw)
```