自定义搜索菜单以添加我自己的操作员?

Custom Search menu to add my own operators?

您好,我想创建一个自定义搜索菜单来添加我自己的运算符。很像 blender F3 命令搜索:

如果可能的话,我只想添加我想要的任何类型的运算符。因此,例如,我会有一个动画搜索菜单或建模搜索菜单。或者甚至只是一个包含我自己的自定义脚本的菜单。

这在搅拌机中可行吗?

我在一些帮助下找到了答案。您几乎需要使用:

invoke_search_popup

您需要在运算符上创建枚举,然后 运行 在运算符上 invoke_search_popup。

这是来自 Gorgious 和 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()