[SOLVED] Get notified when selected edge(s) are changed

In my addon I’d like to display some information related to currently selected edge(s).
This probably requires get notified when selected edge(s) are changed.
How to do the trick?

Please provide a code snippet.

If I understand correctly, this script will help:


import bpy
import bmesh


def draw_menu(self, context):
    pass


class EdgeIndex(bpy.types.Operator):
    """Display edge index when selected"""
    bl_idname = "view3d.edge_index"
    bl_label = "Display Edge Index"
    
    def modal(self, context, event):
        if context.mode != 'EDIT_MESH':
            return {'FINISHED'}


        if event.type == 'RIGHTMOUSE' and event.value == 'RELEASE':
            geom = self.bm.select_history[0]
            if isinstance(geom, bmesh.types.BMEdge):
                context.window_manager.popup_menu(draw_menu, title="Edge "+str(geom.index), icon='INFO')


        return {'PASS_THROUGH'}


    def invoke(self, context, event):
        if context.mode == 'EDIT_MESH':
            obj = bpy.context.edit_object
            me = obj.data
            self.bm = bmesh.from_edit_mesh(me)


            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}
        else:
            self.report({'WARNING'}, "context.mode must be 'EDIT_MESH'")
            return {'CANCELLED'}


bpy.utils.register_class(EdgeIndex)

I would just create a panel that conditionally shows info. Blender should update the panel behind the scenes.


import bpy
import bmesh


class EdgePropertiesPanel(bpy.types.Panel):
    bl_label = "Edge Info"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    empty = ""

    def draw(self, context):
        if context.mode == 'EDIT_MESH':
            bm = bmesh.from_edit_mesh(context.edit_object.data)
            edge = bm.select_history[0]
            if isinstance(edge, bmesh.types.BMEdge):
                self.layout.label("An Edge is selected: %s" % str(edge.index))


bpy.utils.register_class(EdgePropertiesPanel)

1 Like

@Sazerac

Your short solution solves my problem! Thanks!

@mano-wii
Your solution also solves my problem but via a bit more complicated way. But thanks to your message I’ve finally read about modal operators. I’ve never used the modal operators before. Thank you too!

I’ll mark the thread as solved.