Recent Posts

How to Display Custom Property on Properties Editor in Blender Addon

GOAL

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.

bl_info = {
        "name": "Add Target Property",
        "description": "test addon to add target_obj properties to Object",
        "version": (1, 0),
        "blender": (2, 80, 0),
        "category": "3D View",
        }
import bpy

def register():
    bpy.types.Object.target_obj = bpy.props.PointerProperty(name="TargetObj", type=bpy.types.Object)

def unregister():
    del bpy.types.Object.target_obj

if __name__ == '__main__':
    register()

At this time, the property “target_obj” is not displayed in Properties Editor.

Properties Editor
(more…)

How To Detect Object Moving In Blender Addon.

GOAL

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.

Reference: Persistent Handler Example

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”.

(more…)

How to Add Properties In Your Blender Addon UI

GOAL

Today’s goal is to develop 3 types of Blender addons with properties, showing properties in Panel or Properties Editor as below.

Note: Addons in this article are the simplest examples and not practical.

Environment

Blender 2.83(LTS)
Windows10

What is property?

At first, I recommend to read “Properties” in Blender manual and “Property Definitions (bpy.props)” in Blender Python API documentation. And check “Operator Property” for details about how to use properties in Addon.

Properties

The Properties shows and allows editing of many active data, including the active scene and object.

from Blender 2.93 Manual

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.

Example of properties

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()
(more…)

Categories

AfterEffects Algorithm Artificial Intelligence Blender C++ Computer Graphics Computer Science Daily Life DataAnalytics Event Game ImageProcessing JavaScript Kotlin mathematics Maya PHP Python SoftwareEngineering Tips Today's paper Tools TroubleShooting Unity Visual Sudio Web Windows WordPress 未分類