I've been trying to achieve this, but have no luck. Here is an existing example in which can be easily modified.

I'd like to have this modal operation work for switching between material slot index. Any way of using integer values 1-10 so everytime you move your mouse, the active slot is changed to the next index, for example: 0 > time.sleep for half of a second and then switch to 1 and so on, but depending on mousemovent distance(delta). Thanks.

import bpy
from bpy.props import IntProperty, FloatProperty

class ModalOperator(bpy.types.Operator):
    """Move an object with the mouse, example"""
    bl_idname = "object.modal_operator"
    bl_label = "Simple Modal Operator"

first_mouse_x = IntProperty()
first_value = FloatProperty()

def modal(self, context, event):
    if event.type == 'MOUSEMOVE':
        if self.lmb:
            delta = self.first_mouse_x - event.mouse_x
            context.object.location.x = self.first_value + delta * 0.01

    elif event.type == 'LEFTMOUSE':
        # we could handle PRESS and RELEASE individually if necessary
        self.lmb = event.value == 'PRESS'

    elif event.type in {'RIGHTMOUSE', 'ESC'}:
        context.object.location.x = self.first_value
        return {'CANCELLED'}

    return {'RUNNING_MODAL'}

def invoke(self, context, event):
    # variable to remember left mouse button state    
    self.lmb = False

    if context.object:
        self.first_mouse_x = event.mouse_x
        self.first_value = context.object.location.x

        return {'RUNNING_MODAL'}
        self.report({'WARNING'}, "No active object, could not finish")
        return {'CANCELLED'}

def register():

def unregister():

if __name__ == "__main__":

# test call
  • $\begingroup$ I'd suggest changing to the modal timer operator. Set up the timer to match the sleep time. $\endgroup$
    – batFINGER
    Aug 24, 2016 at 4:27
  • $\begingroup$ I'm not so sure how to do that. Can you provide an example. Thanks. $\endgroup$ Aug 25, 2016 at 3:55


You must log in to answer this question.

Browse other questions tagged .