Operators do not work after bpy.ops.wm.open_mainfile

It looks like many of the operators dont work after bpy.ops.wm.open_mainfile.
I am trying to open a file from Blender via bpy.ops.wm.open_mainfile and then use the Make Instances Real operator to make all instances in that newly opened file real, but the operator does nothing. I also realised that many other operators dont work after bpy.ops.wm.open_mainfile.

Whats wrong with my code? This seems like a very simple task…

import bpy

bpy.ops.wm.open_mainfile(filepath="file.blend")
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.duplicates_make_real()

I can say that when a new file is opened everything in the current file (all of the instantiated data) will be lost. In this case the text editor will loose all of the text files. Also all of the operators or panels, that exist in the current active context.

However for Addons this is not the case since they can be registered at any given time (reload / enable / startup). So the right approach is to turn the script to a simple Addon.

bl_info = {
    "name": "Simple Object Operator",
    "author": "Author",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "View3D > Simple Object Operator",
    "description": "",
    "warning": "",
    "doc_url": "",
    "category": "Object",
}

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"
    
    def execute(self, context):
        bpy.ops.wm.open_mainfile(filepath=R"D:\programs\graphics\blender_addons_27\myaddons_wip\blender\ofxBlender\example\bin\data\test.blend")
        bpy.ops.object.select_all(action="SELECT")
        bpy.ops.object.duplicates_make_real()
        return {'FINISHED'}

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

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

if __name__ == "__main__":
    try:unregister()
    except: pass
    register()

    # test call
    # bpy.ops.object.simple_operator()


2020-07-02 21_19_54-Blender Preferences
2020-07-02 21_19_33-Blender

There is a way also when you invoke the operator to show a popup window and enter the filepath you want to open.

Did you test this?
The code in my original post was acutally part of an addon, but still had the same problem. The weird thing is, that some operators like select_all work, but others like duplicates_make_real dont.

I managed to solve the problem in a different way:
I wrote a simple python script, that opens Blender and executes a “processor” script which does the bpy.ops.object.duplicates_make_real() among other things.
This is the call to open Blender and run the processor:
subprocess.run("blender -b " + filename + " -P processor.py", cwd=dir_path)