pythonblenderbpy

Custom Search menu to add my own operators?


Hi I'd like to create a custom search menu to add my own operators. Much like the blender F3 command search:

Blender Command Search

If possible I'd just like to add any type of operators I want to it. So for example I would have an Animation search menu, or a modelling search menu. Or even just a menu with my own custom scripts in it.

Is this possible in blender?


Solution

  • I found the answer with some help. Pretty much you need to use:

    invoke_search_popup
    

    You'll need to create an enum on an operator then run the invoke_search_popup on the operator.

    Here is the example code from Gorgious, and Yilmazz: https://blender.stackexchange.com/questions/247695/invoke-search-popup-for-a-simple-panel

    import bpy
    import re
    import json
    
        
    items = (("Metal", "Metal", ""),
    ("Plastic", "Plastic", ""),
    ("Glass", "Glass", ""),
    ("Shadow", "Shadow", ""),
    )
    
    PROPS = [
        ('material', bpy.props.PointerProperty(type=bpy.types.Material, name='Material')),
    ]
    
    # == OPERATORS
    class MYCAT_OT_search_popup(bpy.types.Operator):
        bl_idname = "object.search_popup"
        bl_label = "Material Renamer"
        bl_property = "my_enum"
    
        my_enum: bpy.props.EnumProperty(items = items, name='New Name', default=None)
        
        @classmethod
        def poll(cls, context):
            return context.scene.material  # This prevents executing the operator if we didn't select a material
    
        def execute(self, context):
            material = context.scene.material
            material.name = self.my_enum
            return {'FINISHED'}
    
        def invoke(self, context, event):
            wm = context.window_manager
            wm.invoke_search_popup(self)
            return {'FINISHED'}
    
    
    # == PANELS
    class ObjectRenamerPanel(bpy.types.Panel):
        
        bl_idname = 'VIEW3D_PT_object_renamer'
        bl_label = 'Material Renamer'
        bl_space_type = 'VIEW_3D'
        bl_region_type = 'UI'
        
        
        def draw(self, context):
            col = self.layout.column()
            row = col.row()
            row.prop(context.scene, "material")
    
            col.operator('object.search_popup', text='Rename')  #Display the search popup operator
    
    # == MAIN ROUTINE
    CLASSES = [
        MYCAT_OT_search_popup,
        ObjectRenamerPanel,
        
    ]
    
    def register():
        for (prop_name, prop_value) in PROPS:
            setattr(bpy.types.Scene, prop_name, prop_value)
        
        for klass in CLASSES:
            bpy.utils.register_class(klass)
    
    def unregister():
        for (prop_name, _) in PROPS:
            delattr(bpy.types.Scene, prop_name)
    
        for klass in CLASSES:
            bpy.utils.unregister_class(klass)
            
    
    if __name__ == '__main__':
        register()
    

    Search Menu