Today’s goal is to develop an addon and display custom property on Properties editor. For example, add “target_obj” property to bpy.types.Object for management and edit the value of “target_obj” with Properties editor.
Environment
Blender2.83(LTS) Windows10
Method
1. Register property
The following is the code of addon just add the property “target_obj” to bpy.types.Object.
Today’s goal is to develop addon to detect object’s property changes and call function. In my case, I created an addon to detect object moving and display the message “object <Name> moving” to the system console.
Environment
Blender 1.83(LTS) Windows10
Method
Use handler
Use Application Handlers to call function by triggers. I used “bpy.app.handlers.depsgraph_update_post” for calling the function to check if the current object is moving in each time when the blender scene has changed.
import bpy
from bpy.app.handlers import persistent
@persistent
def obj_init(scene):
# check if the bpy.context.object is moving or not
bpy.app.handlers.depsgraph_update_post.append(obj_init)
Add property to store location before move
I added property “before_loc” to compare current object location to the one before depsgraph updated. If the location changed, store new location to the property “before_loc”.
Property has information about any type of data such as Object, Material, Scene and so on. For example, Objects have properties such as “location”, “scale”, “active_material”, “animation_data”, “bound_box” and more. (Be careful that some of them are internal or non-editable!) You can add custom properties to the existing data type or create property group in Addon.
1. Change Existing property
Change the value stored in Blender data with UI. For example, change the size of the object, change the color of material.
bl_info = {
"name": "Existing Property Test",
"description": "test addon to change existing property",
"version": (1, 0),
"blender": (2, 83, 0),
"category": "3D View",
}
import bpy
class PROPTEST_PT_my_panel(bpy.types.Panel):
bl_label = "Prop Test Addon1"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
# existing property
if "Cube" in bpy.data.objects:
col.prop(bpy.data.objects["Cube"], "scale")
active_material = bpy.data.objects["Cube"].active_material
col.prop(active_material, "diffuse_color")
if "Light" in bpy.data.lights:
col.prop(bpy.data.lights["Light"], "type")
def register():
bpy.utils.register_class(PROPTEST_PT_my_panel)
def unregister():
bpy.utils.unregister_class(PROPTEST_PT_my_panel)
if __name__ == '__main__':
register()