Correct way to support Operator Redo Panel?

Most blender operators have a redo panel where you can change the operator properties after they run.
What is the correct way to support the same functionalilty in custom python operators ?
I’m trying to get it working in the Modal Operator Template that ships with Blender by adding ‘REGISTER’ and ‘UNDO’ to bl_properties but it doesn’t work.
On 2.79 the properties are greyed out and the panel says “Redo Unsupported”. On 2.8 there’s no panel.

redo_unsupported

For reference, here’s the code:

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"
    bl_options = {"REGISTER","UNDO"}

    first_mouse_x = IntProperty()
    first_value = FloatProperty()

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

        elif event.type == 'LEFTMOUSE':
            return {'FINISHED'}

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

        return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        if context.object:
            self.first_mouse_x = event.mouse_x
            self.first_value = context.object.location.x

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


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


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


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.object.modal_operator('INVOKE_DEFAULT')

1 Like

You need an execute function:
https://docs.blender.org/api/2.79/bpy.types.Operator.html

1 Like

Oops! Thank you @brecht, now its working. :slightly_smiling_face:

Hey @pragma37 could you please post your working script? Would be of tremendous help. By adding the execute function I got to a point where I can hit the Redo Last button but the properties first_mouse_x and first_value are still greyed out.

1 Like

I modified the script a bit since it doesn’t makes too much sense to modify the initial object and mouse position on redo, but here’s a working example :

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"
    bl_options = {"REGISTER","UNDO"}
    
    x_translation = FloatProperty()
    
    def execute(self, context):
        context.object.location.x += self.x_translation
        return {'FINISHED'}

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

        elif event.type == 'LEFTMOUSE':
            return {'FINISHED'}

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

        return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        if context.object:
            self.first_mouse_x = event.mouse_x
            self.first_value = context.object.location.x

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


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


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


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.object.modal_operator('INVOKE_DEFAULT')
1 Like

Great, thanks. The dialog box is greyed out if script is run in the text editor. Used the Search Menu in 3d view and it worked.