Teach me how to create marker renamer add-on

So i randomly found this usefull script on the internet (blenderist.com). It’s for marker renaming and i’m going to create add-on based on this script. But i want it to be activated using a button, somewhere near the refresh sequencer button. Can somebody help me how to do it? I hope that i can understand a little bit about logic and scripting in Blender. I really appreciate it. Thanks a ton!


import bpy

i = 1
for Marker in bpy.context.scene.timeline_markers:
    Marker.name=str(i)
    i=i+1

You need to make the rename operation an operator, then append a draw function that adds this operator to the sequencer header (optionally limited to view_type == ‘SEQUENCER’, so it doesn’t show in the preview area header too).

You might wanna sort the timeline markers based on their frame number before renaming, because they are not sorted that way by default.

import bpy

class SCENE_OT_rename_markers(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "scene.rename_markers"
    bl_label = "Rename Markers"

    def execute(self, context):
        for i, marker in enumerate(sorted(context.scene.timeline_markers, key=lambda m: m.frame), 1):
            marker.name = str(i)
        return {'FINISHED'}


def draw_func(self, context):
    layout = self.layout
    if context.space_data.view_type == 'SEQUENCER':
        layout.operator(SCENE_OT_rename_markers.bl_idname)


def register():
    bpy.utils.register_module(__name__)
    bpy.types.SEQUENCER_HT_header.append(draw_func)


def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.types.SEQUENCER_HT_header.remove(draw_func)


if __name__ == "__main__":
    register()


thanks CoDEmanX, i’ll give it a try, your code looks so short compared to mine :smiley: so while i’m waiting for a reply, i tried to “code” it myself, i watch youtube and google a lot with many trials and errors, i think my head is going to explode! i honestly don’t know if this is the correct way to code, but it kind of works!. I also can’t find a way to place the button on the header, so i decided to place it on the sequencer’s properties instead. Can you give me your thought about this?

I also try to add a text filed that align with this marker renamer button. What i’m trying to achieve is to add custom text that will show additional scene information in the marker, and it should looks like this, for example; sc1_1, sc1_2, sc1_3 and so on. But still don’t know how…

import bpy

class SEQUENCER_OT_OHA_marker_renamer_operator(bpy.types.Operator):
bl_idname = ‘oha.marker_renamer’
bl_label = ‘Marker Renamer’
bl_description = ‘Tidy Up Marker Name’

def execute(self, context):
    i = 1
    for Marker in bpy.context.scene.timeline_markers:
        Marker.name='sc_'+str(i)
        i=i+1
        
    return{'FINISHED'}

class SEQUENCER_PT_OHA_marker_renamer_panel(bpy.types.Panel):
bl_label = ‘OHA marker Renamer’
bl_space_type = ‘SEQUENCE_EDITOR’
bl_region_type = ‘UI’

def draw(self, context):
    row = layout.row(align=True)
    row.prop(rd, 'scene_name', text="")
    row.prop('oha.marker_renamer')

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

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

if name == “main”:
register()

My code adds a button to the sequencer header, at the very end. Blender doesn’t allow to place new widgets at arbitrary locations via a Python addon, so this is the only viable way. But in case of the sequencer, it’s not too bad.

Just run my code and click the Rename markers button, it will prompt for a prefix. For instance, type “sc1_” as prefix (without the quote marks) and press OK. The markers will be renamed to sc1_1, sc1_2 etc.

import bpy

class SCENE_OT_rename_markers(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "scene.rename_markers"
    bl_label = "Rename Markers"
    
    prefix = bpy.props.StringProperty(name="Prefix")

    def invoke(self, context, event):
        wm = context.window_manager
        return wm.invoke_props_dialog(self)

    def execute(self, context):
        for i, marker in enumerate(sorted(context.scene.timeline_markers, key=lambda m: m.frame), 1):
            marker.name = "{}{}".format(self.prefix, i)
        return {'FINISHED'}


def draw_func(self, context):
    layout = self.layout
    if context.space_data.view_type == 'SEQUENCER':
        layout.operator(SCENE_OT_rename_markers.bl_idname)


def register():
    bpy.utils.register_module(__name__)
    bpy.types.SEQUENCER_HT_header.append(draw_func)


def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.types.SEQUENCER_HT_header.remove(draw_func)


if __name__ == "__main__":
    register()