Custom End a Modal operator Blender Python
<p>I have the following code:</p>

<pre><code>class audio_visualizer_create(bpy.types.Operator):
    bl_idname = "myop.audio_visualizer_create"
    bl_label = "Audio Visualizer Create"
    bl_description = ""
    bl_options = {"REGISTER"}


    @classmethod
    def poll(cls, context):
        return True

    def invoke(self, context, event):
        context.window_manager.modal_handler_add(self)
        scene = bpy.context.scene




        ##VARIABLES
        type = scene.audio_visualizer_type
        subtype = scene.audio_visualizer_subtype
        axis = scene.audio_visualizer_axis
        object = scene.audio_visualizer_other_sample_object
        scalex = scene.audio_visualizer_sample_object_scale[0]
        scaley = scene.audio_visualizer_sample_object_scale[1]
        scalez = scene.audio_visualizer_sample_object_scale[2]
        object = scene.audio_visualizer_other_sample_object
        bars = scene.audio_visualizer_bars_number

        print(bars)
        print(scaley)
        print(scene.audio_visualizer_bars_distance_weight)

        ##GETTING THE OBJECT
        if object == "OTHER":
            object = scene.audio_visualizer_other_sample_object

        ##Setting Up the bars
        total_lenght = (scaley*bars) + (scene.audio_visualizer_bars_distance_weight/100*(bars-1))

        for i in range(0, bars):
            bpy.ops.mesh.primitive_cube_add(radius=1, view_align=False, enter_editmode=False, location=(0, 0, 0), layers=bpy.context.scene.layers)
            bpy.context.object.scale = (scalex,scaley,scalez)
            bpy.context.object.location.y = total_lenght/bars*i


        is_finished = True
</code></pre>

<blockquote>
  <p>At this Point i want to finish the Modal Operator.</p>
</blockquote>

<pre><code>        return {"RUNNING_MODAL"}

    def modal(self, context, event):


        if event.type in {"ESC"}:
            print("You've Cancelled The Operation.")

            return {"CANCELLED"}

        if event.type in {"MIDDLEMOUSE", "RIGHTMOUSE", "LEFTMOUSE"}:
            return {"FINISHED"}

        return {"FINISHED"}
</code></pre>

<p>But If I put return {"FINISHED"} instead of return {"RUNNING_MODAL"} blender Crashes or freezes, is there any way to end the operator?</p>

<p>Firstly the example you show doesn't benefit from being a modal operator. A modal operator is one that allows the 3DView to be updated as the user input alters what the operator does. An example of a modal operator is the knife tool, once started it changes the final result based on user input while it is running.</p>

<p>The issue you have with your example, is you are doing the wrong tasks in invoke and modal. <code>invoke()</code> should call <code>modal_handler_add()</code> and return <code>{"RUNNING_MODAL"}</code> to signify that <code>modal()</code> should be called while the operator is still running. <code>modal()</code> should perform the data alterations, returning <code>{"RUNNING_MODAL"}</code> while it is still working and <code>{"FINISHED"}</code> or <code>{"CANCELLED"}</code> when it is done.</p>

<p>For a modal operator, <code>modal()</code> is like a loop, each call to modal performs part of the task with the viewport being updated and user input collected in between each call. You add properties to the operator class to hold state information between each modal call.</p>

<p>A simple modal example that adds cubes as you move the mouse -</p>

<pre><code>class audio_visualizer_create(bpy.types.Operator):
    bl_idname = "myop.audio_visualizer_create"
    bl_label = "Audio Visualizer Create"
    bl_options = {"REGISTER"}

    first_mouse_x = bpy.props.IntProperty()
    first_value = bpy.props.FloatProperty()

    def modal(self, context, event):
        delta = 0
        if event.type == 'MOUSEMOVE':
            delta = event.mouse_x - self.first_mouse_x
        elif event.type in ['LEFTMOUSE','RIGHTMOUSE','ESC']:
            return {'FINISHED'}

        for i in range(delta//5):
            bpy.ops.mesh.primitive_cube_add(radius=1)
            s = i*0.1
            bpy.context.object.scale = (s,s,s)
            bpy.context.object.location.y = i

        return {"RUNNING_MODAL"}

    def invoke(self, context, event):
        self.first_mouse_x = event.mouse_x
        self.first_value = 0.0
        context.window_manager.modal_handler_add(self)

        return {"RUNNING_MODAL"}
</code></pre>

<p>The flaw in this example - each time <code>modal()</code> is called the for loop creates a cube at each location, which leads to multiple cubes created at each position.</p>

python modal-dialog operator-keyword blender