simple python question

hi, ive problem with python:

how i can make a series of command work only if i hold LMB??

Your question is (at least to me) not clear …
Python does for itself nothing with LMB …

You have to tell more: Running Blender (?) and then?

Evtl. you mean this:
Make an operator with an poll function checking if LMB is pressed …???

Thaks for the reply :smiley:

i’ve a script that run in modal operator and i need that the function i wrote run only if a hold left mouse button, just like Gpencil…
for example(take from the python templates of blender):

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()
 
        <b><i>if event.type == LEFTMOUSE and event.value == 'PRESS':</i></b>
            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.modal_handler_add(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'}
 
def register():
    bpy.utils.register_class(ModalDrawOperator)
 
def unregister():
    bpy.utils.unregister_class(ModalDrawOperator)
if __name__ == "__main__":
    register()

the part underscored is the part that i want run only if i HOLD LMB
but what the code do at the moment is only to work only if i “CLICK”…

how i can fix this??

Give it an on off flag, turn it on with press and off with release. Do the mousemove draw when it’s on.



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"
    is_drawing = False
    def modal(self, context, event):
        context.area.tag_redraw()

        if event.type == 'MOUSEMOVE' and self.is_drawing:
            self.mouse_path.append((event.mouse_region_x, event.mouse_region_y))
            
        if event.type == 'LEFTMOUSE':
            if event.value == 'PRESS':
                self.is_drawing = True
            elif event.value == 'RELEASE':
                self.is_drawing = False


        
        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.modal_handler_add(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'}


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


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

if __name__ == "__main__":
    register()

oh finally!! thank you very much, it works greatly now(this afternoon) i can release my new script. :D:D