[QUESTION]How to use 'operator_modal_draw.py' NooooB question

Hello everyone here:

I would like to learn from Script Template…
Here is a sample code which name is : operator_modal_draw.py
like:


import bpy
import bgl
import blf

def draw_callback_px(self, context):
    print("mouse points", len(self.mouse_path))

    font_id = 0 # XXX, need to find out how best to get this.

    # draw some text
    blf.position(font_id, 15, 30, 0)
    blf.size(font_id, 20, 72)
    blf.draw(font_id, "Hello Word " + str(len(self.mouse_path)))

    # 50% alpha, 2 pixel width line
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 0.5)
    bgl.glLineWidth(2)

    bgl.glBegin(bgl.GL_LINE_STRIP)
    for x, y in self.mouse_path:
        bgl.glVertex2i(x, y)

    bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)


class ModalDrawOperator(bpy.types.Operator):
    '''Draw a line with the mouse'''
    bl_idname = "view3d.modal_operator"
    bl_label = "Simple Modal View3D Operator"

    def modal(self, context, event):
        context.area.tag_redraw()

        if event.type == 'MOUSEMOVE':
            self.mouse_path.append((event.mouse_region_x, event.mouse_region_y))

        elif event.type == 'LEFTMOUSE':
            context.region.callback_remove(self._handle)
            return {'FINISHED'}

        elif event.type in ('RIGHTMOUSE', 'ESC'):
            context.region.callback_remove(self._handle)
            return {'CANCELLED'}

        return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        if context.area.type == 'VIEW_3D':
            context.window_manager.add_modal_handler(self)

            # Add the region OpenGL drawing callback
            # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
            self._handle = context.region.callback_add(draw_callback_px, (self, context), 'POST_PIXEL')

            self.mouse_path = []

            return {'RUNNING_MODAL'}
        else:
            self.report({'WARNING'}, "View3D not found, cannot run operator")
            return {'CANCELLED'}

I wondering about how to use/trigger it~ when i click Run Script, nothing happens.
anybody can help me?

Thx

The script defines a custom operator. Further down, you’ll see a subclass of bpy.types.Operator:

class ModalDrawOperator(bpy.types.Operator):
    bl_idname = "view3d.modal_operator"
    bl_label = "Simple Modal View3D Operator"

When the script is run, nothing seems to happen, but Blender notices that a class like this has been defined, and automatically registers it as an operator. It doesn’t define any UI, but nevertheless it can still be invoked via the spacebar menu, which brings up a searchable list of all registered operators. Just type “simple modal” into the search box while the mouse is within the 3D View window, and you will see the items narrow down to the name assigned to bl_label above. Select it, and you will see the mouse leave a trail of a line behind it as you move it around; click the left or right buttons to stop the operator executing.

For more details about how scripting works in Blender 2.5x, see my tutorial starting here
http://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/Advanced_Tutorials/Python_Scripting/Introduction_New

Thanks a lot~~~ido…you always answer my questions…thanks again~

tested that in 2.56
and gives error on

File “scriptmodaloperator1.”, line 55, in invoke
AttributeError: ‘WindowManager’ object has no attribute ‘add_modal_handler’

anybody knows how to correct that ?

Thanks happy 2.5

Hi RB,

like a lot of things add_blah_blah it has been renamed to blah_blah_add it is now

modal_handler_add

thanks

i can see like a line that is being drawn in 3D viewport which is an intersting effect!

now is there a way to save this line as an object ?

or is it only to show off !

Thanks happy 2.5

If you look at the source, you will see it saves the stream of mouse X and Y coordinates in an instance variable called mouse_path. Modifying the script to do something with that data is left as an exercise for the reader—this is just a template, after all.:evilgrin:

Or perhaps you could just use this http://www.blendernation.com/2010/04/08/new-script-surface-sketching/