Addon Operators and Undo support

See 1 part below. It’s just the collection aspect of things, there’s a variety of other operations performed. The usage of the collection API directly was suggested by brecht here: Where to find collection_index for moving an object?

Repro steps:

  • start with default scene
  • select the Cube and go into Edit mode (stay in edit mode for rest of steps)
  • make several edits to the Cube (just move some edges around)
  • Invoke the operator below (notice it does the right thing)
  • Ctrl-Z (it does not do the right thing; you’ll have to Ctrl-Z past the point where you went to edit mode for things to Undo)
class RB_OT_undo_issue(bpy.types.Operator):
    bl_idname = "view3d.rb_undo_issue"
    bl_label = "Test test"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        # Add new collection to the scene
        col_test = bpy.data.collections.new("Test")
        bpy.context.scene.collection.children.link(col_test)

        # Remove default cube from Original and place it into Test...
        col_orig = bpy.data.collections["Collection"]

        cube = bpy.context.active_object
        col_test.objects.link(cube)
        col_orig.objects.unlink(cube)

        return {"FINISHED"}