Issue with handling window events when calling read_homefile 2.83 and 3.0

something is funny with read_homefile operator
and possibly open mainfile in this use case

when the operator is executed from UI Panel in 3D Viewport, and a popup is not invoked, e.g when file is not dirty, or when calling inside another operator with “EXEC_DEFAULT”

the window only catches the mouse press event, and not release, so it invokes for example the box select operator in the newly opened file, or what ever is assigned to left click

here’s code for you to try, execute the button twice in a row, under view3d Misc category

import bpy


class Foo(bpy.types.Panel):
    bl_label = "General"
    bl_region_type = "UI"
    bl_space_type = "VIEW_3D"

    def draw(self, context):
        op = self.layout.operator("wm.read_homefile")
        op.load_ui=False
        op.use_empty=True


bpy.utils.register_class(Foo)

the only way I found to fix this is to simulate mouse-left-release right before calling

....
# simulate mouse left release here
bpy.ops.wm.read_homefile(load_ui=False, use_empty=True)
....

but I cannot use event_simulate without enabling it in command line args Command Line Arguments — Blender Manual

which is not very convenient as it would be used in a user addon
I used a 3rd party module to simulate mouse events, it worked, but that solution is not perfect too, considering support for multiple platforms

can someone help with this?, I suppose this is something that should be edited in Blender source, but still I want to understand the issue more

thanks in advance

screen recording of the issue
panel code in post
and here https://gist.github.com/iyadahmed/af503840cc3b98c00e64e8fc5a14e008

If you add your own operator to the panel and then make a function to run the read home file operator after a delay with a timer set to something like 1 second it should overcome this. I left out the registering and unregistering part. This is copied compiled code from me building it quickly in Serpens Visual Scripting Addon.

The key part to make this all work is the line calling the function with a delay:
bpy.app.timers.register(fn_custom_reload, first_interval=1.0)

def fn_custom_reload():
    try:
        bpy.ops.wm.read_homefile('INVOKE_DEFAULT' if False else 'EXEC_DEFAULT',)
    except Exception as exc:
        print(str(exc) + " | Error in function FN Custom Reload")


###############   EVALUATED CODE
#######   New Addon
class SNA_PT_Read_Homefile_069EF(bpy.types.Panel):
    bl_label = "Read Homefile"
    bl_idname = "SNA_PT_Read_Homefile_069EF"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = 'Test Panel'
    bl_order = 0


    @classmethod
    def poll(cls, context):
        return True

    def draw_header(self, context):
        try:
            layout = self.layout
        except Exception as exc:
            print(str(exc) + " | Error in Read Homefile panel header")

    def draw(self, context):
        try:
            layout = self.layout
            op = layout.operator("sna.custom_reload",text=r"Read Homefile",emboss=True,depress=False,icon_value=0)
        except Exception as exc:
            print(str(exc) + " | Error in Read Homefile panel")


class SNA_OT_Custom_Reload(bpy.types.Operator):
    bl_idname = "sna.custom_reload"
    bl_label = "Custom Reload"
    bl_description = ""
    bl_options = {"REGISTER", "UNDO"}


    @classmethod
    def poll(cls, context):
        return True

    def execute(self, context):
        try:
            bpy.app.timers.register(fn_custom_reload, first_interval=1.0)
        except Exception as exc:
            print(str(exc) + " | Error in execute function of Custom Reload")
        return {"FINISHED"}

    def invoke(self, context, event):
        try:
            pass
        except Exception as exc:
            print(str(exc) + " | Error in invoke function of Custom Reload")
        return self.execute(context)
1 Like