New mesh Custom Property can be created through the Blender python API by executing the following code:
1 |
bpy.data.objects['object_name']['property_name'] = property_value |
with:
- object_name – name of the mesh
- property_name – new custom property name
- property_value – value of this new property
After executing this command, the new property will be created and will be available in the Properties window – Object panel – Custom Properties sub-panel.
Like any object custom properties, the created property has a number of parameters that can be accessed by clicking the Edit button. These parameters can also be changed through the API.
If you access the custom property directly by name, you can only change its value. However, the RNA interface allows you to access its additional parameters through the API.
The following code creates a new property with the name “myCustomProp” for the active (selected) mesh, sets property value to 11, and sets some values to its additional parameters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import bpy from rna_prop_ui import rna_idprop_ui_prop_get # Выделенный объект obj = bpy.context.active_object # Задание нового свойства объекту obj['myCustomProp'] = 11 # Задание дополнительных параметров prop_ui = rna_idprop_ui_prop_get(obj, 'myCustomProp') prop_ui["min"] = -5.0 prop_ui["max"] = 15.0 prop_ui["soft_min"] = -5.0 prop_ui["soft_max"] = 15.0 prop_ui["description"] = 'myProp' # Обновление экрана for area in bpy.context.screen.areas: area.tag_redraw() |
Force redraw of the scene.areas in two last lines of the script needs to update the Custom Properties sub-panel and make the created property visible and available in Blender interface.
Dude, it seems like this code could really help me, thanks in advance. However, It doesn´t work on blender 3.0. Could you tell me how to get it working? thanks again
Blender 3.0 is still beta now. Something may not work properly. I don’t read about removing this part of API in 3.0 so I think you can report a bug to developers.
Thanks for your answer, I think they actually say something about it in
https://wiki.blender.org/wiki/Reference/Release_Notes/3.0/Python_API
down the title: IDProperty UI Data API
but I’m not sure about what does it mean, I’m really not good at coding.
I’m specially interested in the method
as_dict
but have no idea how to use it to get the max value for a custom property.I’m trying:
scn = bpy.data.scenes[“Scene”]
scn[‘myCustomProp’] = 11
dict = scn.id_properties_ui_as_dict(‘myCustomProp’)
but it doesn’t work
could you please give me any clue?
thanks again
It is difficult to say because I don’t know what you are trying to do.
Maybe it’s better for you to use API inherited custom properties?
For the scene – property with the int value:
import bpy
# registering
bpy.types.Scene.my_custom_prop = bpy.props.IntProperty(
default = 1
)
# calling
print(bpy.context.scene.my_custom_prop)
# setting
bpy.context.scene.my_custom_prop = 2
print(bpy.context.scene.my_custom_prop)
The more complex example you can see here: https://b3d.interplanety.org/en/custom-property/
Thanks, Nikita, I’ll check the example 🙂