Modal Operator with BMesh don't refresh mesh?

If you run the below script, the mesh isn’t update until I go in object mode, but if I want a “real time” update of the mesh what I must doing?


import bpy, bmesh
from bpy.props import IntProperty, FloatProperty
class ModalOperator(bpy.types.Operator):
    bl_idname = "object.modal_operator"
    bl_label = "Simple Modal Operator"
    def modal(self, context, event):
        object = bpy.context.object
        mesh = object.data
        bm = bmesh.from_mesh(mesh)
        if event.type == 'MOUSEMOVE':
            bm.verts[0].co.x +=  0.1
 
        elif event.type == 'LEFTMOUSE':
            return {'FINISHED'}
        elif event.type in {'RIGHTMOUSE', 'ESC'}:
            return {'CANCELLED'}
        return {'RUNNING_MODAL'}
    def invoke(self, context, event):
        print("Invoke")
        if context.object:
            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}
        else:
            self.report({'WARNING'}, "No active object, could not finish")
            return {'CANCELLED'}
def register():
    bpy.utils.register_class(ModalOperator)
def unregister():
    bpy.utils.unregister_class(ModalOperator)
if __name__ == "__main__":
    register()
    # test call
    bpy.ops.object.modal_operator('INVOKE_DEFAULT')

may be on the to do list ?

see wiki page for this list

i cannot even find an SVN with Bmesh API yet for windows 32 bits !

salutations

in the future you won’t have to go into edit mode anymore
this will probably be changed in the weeks to come

Solution by Campbell :slight_smile:


import bpy, bmesh
from bpy.props import IntProperty, FloatProperty
class ModalOperator(bpy.types.Operator):
    bl_idname = "object.modal_operator"
    bl_label = "Simple Modal Operator"
    def modal(self, context, event):
        object = bpy.context.object
        mesh = object.data
        bm = bmesh.from_mesh(mesh)
        if event.type == 'MOUSEMOVE':
            bm.verts[0].co.x +=  0.1
            context.area.tag_redraw()  # The trick
        elif event.type == 'LEFTMOUSE':
            return {'FINISHED'}
        elif event.type in {'RIGHTMOUSE', 'ESC'}:
            return {'CANCELLED'}
        return {'RUNNING_MODAL'}
    def invoke(self, context, event):
        print("Invoke")
        if context.object:
            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}
        else:
            self.report({'WARNING'}, "No active object, could not finish")
            return {'CANCELLED'}
def register():
    bpy.utils.register_class(ModalOperator)
def unregister():
    bpy.utils.unregister_class(ModalOperator)
if __name__ == "__main__":
    register()

The answer:

The issue is that python doesn’t have notifier access yet, so you cant
easily tell all 3D views to redraw from the console.
(though it wouldnt be hard to write a python function to do so.)

if you run a tool from the 3D view you can redraw the active view.
context.area.tag_redraw()

If you have a modifier applied, rna can refresh this via the object.
obj.update_tag(refresh={‘DATA’})

There are still some TODO’s - how to tell the editmeshes ngons to be
re-tesselated, how to sync between the bmesh and the original mesh and
updating as you noticed.