Render Function - rename filename So close yet so far (I think)

A function within an addon am creating is suppose to rename the file ouput using tokens. This is based on the Oscurart tools.

The projects consists of markers on every frame with different binded cameras. The rendered image name needs to consist of a Marker and Camera.

def replaceMarker (dummy):
    global renderpath
#Set Tokens variables
    cameraCURRENT = bpy.context.scene.camera.name
    markerTIMELINE = []
    for marker in bpy.context.scene.timeline_markers:
        name = marker.name
        markerTIMELINE.extend([name])
    markerCURRENT = markerTIMELINE[bpy.context.scene.frame_current-1]

    tokens = {
        "$Marker":markerCURRENT,
    "$Camera":cameraCURRENT
        }
#Replace filename output
    renderpath = bpy.context.scene.render.filepath

    bpy.context.scene.render.filepath = renderpath.replace("$Marker",tokens["$Marker"]).replace("$Camera",tokens["$Camera"])
    print(bpy.context.scene.render.filepath)
#Clear list to repeat sequence
    markerTIMELINE.clear()


The function does not replace the marker and only uses one camera name.

Am I suppose to more this to the init.py?
How can I call the function between every frame?

Thanks

EDIT
Question to circumvent the issue, it is possible to access the Metadata in fileoutput and use it to rename the file?
So sorry for the very late reply. I found the answer in another thread and with some help.
But below is the Render Function I am currently using.
    bl_idname = &quot;render.colorways&quot;
    bl_label = &quot;Render list&quot;
    bl_description = &quot;Render colorways with marker in filename&quot;

    # Define some variables to register
    _timer = None
    shots = None
    stop = None
    rendering = None
    path = &quot;/tmp/&quot;

    # Set up the handler methods
    def pre(self, scene, depsgraph):
        self.rendering = True

    def post(self, scene, depsgraph):
        self.rendering = False

    def cancelled(self, scene, depsgraph):
        self.stop = True

    def execute(self, context):
        # Define the variables during execution. This allows
        # to define when called from a button
        self.stop = False
        self.rendering = False

        # Get all markers in a list and assign them to shots variable
        itemmarkers = bpy.context.scene.timeline_markers.items()
        self.shots = []
        self.shots.clear
        for m in itemmarkers:
            self.shots.append(m[0])
            
        #don't know why but I need to begin 1 frame before the start
        bpy.context.scene.frame_set(bpy.context.scene.frame_start - 1)
        
        self.path = context.scene.render.filepath

        bpy.app.handlers.render_pre.append(self.pre)
        bpy.app.handlers.render_post.append(self.post)
        bpy.app.handlers.render_cancel.append(self.cancelled)

        # The timer gets created and the modal handler
        # is added to the window manager
        self._timer = context.window_manager.event_timer_add(0.5, window=context.window)
        context.window_manager.modal_handler_add(self)

        return {&quot;RUNNING_MODAL&quot;}

    def modal(self, context, event):
        if event.type == 'TIMER':
            scn = context.scene
            
            # filename prefix ans suffix
            firstpart = bpy.context.scene.TextInputProps.nameprefix
            lastpart = bpy.context.scene.TextInputProps.namesuffix
            
            # No frames in the list or user hits ESC
            if True in (scn.frame_current &gt;= scn.frame_end, self.stop is True):

                # We remove the handlers and the modal timer to clean everything
                bpy.app.handlers.render_pre.remove(self.pre)
                bpy.app.handlers.render_post.remove(self.post)
                bpy.app.handlers.render_cancel.remove(self.cancelled)
                context.window_manager.event_timer_remove(self._timer)

                # Reset the output path
                scn.render.filepath = self.path
                return {&quot;FINISHED&quot;}

            elif self.rendering is False: # Nothing is currently rendering.
                                          # Proceed to render.
                # Get the marker from the list
                marker_name  = self.shots[scn.frame_current]

                #Create filename variable
                if firstpart == &quot;&quot; and lastpart == &quot;&quot;:
                    finalfilename = marker_name
                elif firstpart == &quot;&quot; and lastpart != &quot;&quot;:
                    finalfilename = marker_name +&quot;_&quot;+ lastpart
                elif firstpart != &quot;&quot; and lastpart == &quot;&quot;:
                    finalfilename = firstpart +&quot;_&quot;+ marker_name
                else:
                    finalfilename = firstpart +&quot;_&quot;+ marker_name +&quot;_&quot;+ lastpart
    
                # Assemble the file path and filename
                scn.render.filepath = os.path.join(self.path, finalfilename + &quot;.&quot; + scn.render.image_settings.file_format)

                # Call the render operator
                bpy.ops.render.render(&quot;INVOKE_DEFAULT&quot;, write_still=True)
                
                # Set the next frame
                scn.frame_set(scn.frame_current + 1)

        return {&quot;PASS_THROUGH&quot;}

There is one issue, in the render process certain frames don't save. Perhaps optimizing the loop the statement in the def modal would help.
I hope this helps.