1
$\begingroup$

I'm working on a feature which needs to be a modal that runs when you're painting a stroke. It then needs to repeat that stroke again.

For this I'm trying to store the coordinates of the mouse when painting and drawing the strokes when the mouse is lifted up again.

My problem is that the modal function doesn't seem to run while the mouse is being held down. Here's the code:

class LP_OT_RepeatStroke(bpy.types.Operator):
    bl_idname = "layer_painter.repeat_stroke"
    bl_label = "Repeat Stroke"
    bl_description = "Repeats the last stroke for all channels that have a value set"
    bl_options = {"REGISTER", "UNDO_GROUPED"}

    drawing: bpy.props.BoolProperty(default=False)

    def __init__(self):
        self.coordinates = []

    def paint(self):
        for coordinate in self.coordinates:
            bpy.ops.paint.image_paint(
                stroke=[{"name":"", 
                "pen_flip":False,
                "is_start":False, 
                "location":(0,0,0), 
                "mouse":coordinate,
                "pressure":1, 
                "time": 0,
                "size":40
                }])
        print(self.coordinates)
        self.coordinates = []
        return {'FINISHED'}

    def modal(self, context, event):
        if event.type == 'LEFTMOUSE':
            if event.value == 'PRESS' and not self.drawing:
                self.drawing = True
        elif event.type == "MOUSEMOVE":
            if event.value == 'RELEASE' and self.drawing:
                self.drawing = False
                self.paint()
        elif event.type == "ESC":
            return {"FINISHED"}
        elif self.drawing:
            self.coordinates.append((event.mouse_x,event.mouse_y))
        return {'PASS_THROUGH'}

    def invoke(self, context, event):
        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.