Register RNA Subscriber#

Examples#

Listen to a workspace change#

from .addon_utils import Register
from bpy.types import Window, Context, Workspace

@Register.RNA_SUB(Window, 'workspace', persistent=True)
def on_workspace_change(context: Context, window: Window, workspace: Workspace):
    print("WorkSpace changed!", window, workspace)

Listen to a mode change#

from .addon_utils import Register
from bpy.types import Object, Context

@Register.RNA_SUB(Object, 'mode', persistent=True)
def on_mode_change(context: Context, object: Object, mode: str):
    print(f"RNA::on_mode_change -> {mode} ['{object.name}']")

Listen to active object#

from .addon_utils import Register
from bpy.types import LayerObjects, Context, Object

@Register.RNA_SUB(LayerObjects, 'active', persistent=True, data_path='view_layer.objects')
def on_active_object_change(context: Context, view_layer_objects: LayerObjects, active_object: Object):
    # We can filter our active object!
    if active_object.type != 'MESH':
        return

    print(f"RNA::on_active_object_change -> '{active_object.name}'")

Listen to active UV Layer#

from .addon_utils import Register
from bpy.types import Context, UVLoopLayers, Mesh

@Register.RNA_SUB(UVLoopLayers, 'active_index', data_path='object.data.uv_layers', persistent=True)
def on_UVMap_switch(context: Context, uv_layers: UVLoopLayers, active_index: int):
    mesh: Mesh = uv_layers.id_data

    print(f"RNA::on_UVMap_switch -> '{mesh.name}', index: {mesh.uv_layers.active_index}")