0
$\begingroup$

I have this code that triggers a modal operator everytime you hit the R key, i am trying to retain the default behaviour of blender while running this operator, meaning if i am in pose mode and want to rotate a bone i hit the R key and start rotating it. But somehow it doesn't work even with the "PASS_THROUGH". Any solution to this ?

I want to perform a set of operations when the Bone is being rotated, like disable something and enabling it right after the operation is finished. Is this the best solution ?

Thanks

import bpy

class ModalOperator(bpy.types.Operator):
    bl_idname = "object.modal_operator"
    bl_label = "Simple Modal Operator"

    def execute(self, context):
        print('execute function')
        return {'FINISHED'}

    def modal(self, context, event):
       
        if event.type == 'R':  # Confirm
            
            print('bone is being rotated')
          
            return {'PASS_THROUGH'}
     
        elif event.type == 'LEFTMOUSE':  # Confirm
            print('bone has been  rotated')
            return {'FINISHED'}
        elif event.type in {'RIGHTMOUSE', 'ESC'}:  # Cancel
            # Revert all changes that have been made
            print('bone rotation has been  cancelled')
            return {'CANCELLED'}
        
        return {'RUNNING_MODAL'}

    def invoke(self, context, event):

#        self.execute(context)
        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}
    
    
    
addon_keymaps = []

def register():
    bpy.utils.register_class(ModalOperator)
    
    # Add the hotkey
    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    if kc:
        km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')
        kmi = km.keymap_items.new(ModalOperator.bl_idname, type='R', value='PRESS'  )
        addon_keymaps.append((km, kmi))


def unregister():
    bpy.utils.unregister_class(ModalOperator)
    
    # Remove the hotkey
    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()    
    

if __name__ == "__main__":
    register()
$\endgroup$

1 Answer 1

0
$\begingroup$

Just found out that you can call the rotate operator inside the modal so i guess that's the solution

import bpy

class ModalOperator(bpy.types.Operator):
    bl_idname = "object.modal_operator"
    bl_label = "Simple Modal Operator"

    def execute(self, context):
        print('execute function')
        return {'FINISHED'}

    def modal(self, context, event):
       
        if event.type == 'R':  # Confirm
            
            print('bone is being rotated')
            bpy.ops.transform.rotate('INVOKE_DEFAULT')
            return {'PASS_THROUGH'}
     
        elif event.type == 'LEFTMOUSE':  # Confirm
            print('bone has been  rotated')
            return {'FINISHED'}
        elif event.type in {'RIGHTMOUSE', 'ESC'}:  # Cancel
            # Revert all changes that have been made
            print('bone rotation has been  cancelled')
            return {'CANCELLED'}
        
        return {'RUNNING_MODAL'}

    def invoke(self, context, event):

#        self.execute(context)
        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}
    
    
    
addon_keymaps = []

def register():
    bpy.utils.register_class(ModalOperator)
    
    # Add the hotkey
    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    if kc:
        km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')
        kmi = km.keymap_items.new(ModalOperator.bl_idname, type='R', value='PRESS'  )
        addon_keymaps.append((km, kmi))


def unregister():
    bpy.utils.unregister_class(ModalOperator)
    
    # Remove the hotkey
    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()    
    

if __name__ == "__main__":
    register()
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .