modal timer operator with edittoggle crashes blender

Hi,

I wrote a small script using edittoogle trick to get changes of vertex coordinates, to send them over network. This is done by a modal timer operator. Unfortunatly the script is crashing blender after some seconds if I use the grab mode. Changing vertices from the transform property panel works. Not sure where’s the problem.

blender 2.62-0

!!! Attention: will crash blender


import bpy

class ModalVertexUpdaterOperator(bpy.types.Operator):
    '''Operator which runs its self from a timer.'''
    bl_idname = "wm.modal_vertex_updater"
    bl_label = "Vertex Updater"
    
    _timer = None
    
    def modal(self, context, event):
        print(event.type)
        if event.type == 'ESC':
            return self.cancel(context)

        if event.type == 'TIMER':
            
            if bpy.context.object.mode == 'EDIT':
                bpy.ops.object.mode_set(mode='OBJECT')
           
                #   do something with vertex data (send via osc) 
                print("send: ", [i.co for i in context.object.data.vertices if i.select])
                bpy.ops.object.mode_set(mode='EDIT')
             
                #   uncomment this works better, but blender got stuck
                #   in this operator, all events are caught by the transform
                #   operator
                #bpy.ops.transform.translate('INVOKE_DEFAULT')

        return {'PASS_THROUGH'}

    def execute(self, context):
        bpy.ops.object.mode_set(mode='EDIT')
        context.window_manager.modal_handler_add(self)
        self._timer = context.window_manager.event_timer_add(0.1, context.window)
        return {'RUNNING_MODAL'}

    def cancel(self, context):
        print("CANCELLED")
        context.window_manager.event_timer_remove(self._timer)
        return {'FINISHED'}


def register():
    bpy.utils.register_class(ModalVertexUpdaterOperator)


def unregister():
    bpy.utils.unregister_class(ModalVertexUpdaterOperator)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.wm.modal_vertex_updater('INVOKE_DEFAULT')

offtools

Its solved. Thanks to Bmesh there’s no need to change between edit and object mode anymore.