My operator no longer gets region_data after tabs have switched

I’m having problems with an operator I’m desiging - mainly that the context.region_data becomes None whenever I switch tabs and even after I return to my original tab. My operator needs the region_data to do a ray cast, so if the user switches away while it is running, I need a way to get it back. Can I do this?

import bpy


def redraw_all_viewports(context):
    for area in bpy.context.screen.areas: # iterate through areas in current screen
        if area.type == 'VIEW_3D':
            area.tag_redraw()

#--------------------------------------

class TestRegion(bpy.types.Operator):
    bl_idname = "kitfox.test_region"
    bl_label = "Test Region"

    def __init__(self):
        pass

    def modal(self, context, event):
        regView3d = context.region_data
        settings = context.scene.kitfox_easy_fly_props

        if regView3d == None:
            print("regView3d == None")
            return {'PASS_THROUGH'}
        else:
            print("regView3d OK")
            return {'PASS_THROUGH'}
            
    def invoke(self, context, event):
        if context.area.type == 'VIEW_3D':
            print("Invoke view3D ")

            args = (self, context)
            self._context = context
#            self._handle = bpy.types.SpaceView3D.draw_handler_add(draw_callback, args, 'WINDOW', 'POST_PIXEL')
            context.window_manager.modal_handler_add(self)
            
            
            redraw_all_viewports(context)
            return {'RUNNING_MODAL'}
            
        else:
            self.report({'WARNING'}, "View3D not found, cannot run operator")
            return {'CANCELLED'}

#--------------------------------------

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


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

if __name__ == "__main__":
    register()

Hi, Im not sure if this helps, try to store the regView3d in invoke?

 def invoke(self, context, event):
        self.regView3d = context.region_data

Since if you put it in Modal, it will save the current context every time your mouse move (If Im not wrong)

Im not sure if it will work but I hope this helps

That does seem to work. I;m worried that it’s not safe though. In other APIs you’re not always getting the same context every call - ie, it can be deleted and recreated. I don’t know if this applies to Blender.