Prop_search with activate_init

In UILayout there is really convenient option to set field as active in popup window. It allows you to support hotkey workflow - it activates some field and user will be able to type into it right away without clicking it first, then user can just hit enter and execute the operator.

It does work flawlessly if you add row.activate_init = True before adding drawing prop with row.prop(self, "dummy_name", text='') (where dummy_name is StringProperty).

blender_PstPQIvJqu

But it doesn’t work at all if you use row.prop_search. Is it possible to somehow make it work?
blender_RHl1aSkcI6

Example code (you can run it from script editor in default blender scene and then search for operators with “test popup” in F3 menu and call them to see the difference).

import bpy

class PopupOperatorText(bpy.types.Operator):
    bl_idname = "test.popup_operator_text"
    bl_label = "Popup Operator Test"
    dummy_name: bpy.props.StringProperty(name="Property", default="test")

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

    def draw(self, context):
        row = self.layout.row()
        row.activate_init = True
        row.prop(self, "dummy_name", text='')

    def execute(self, context):
        return {"FINISHED"}
    
class PopupOperatorPropSearch(bpy.types.Operator):
    bl_idname = "test.popup_operator_prop_search"
    bl_label = "Popup Operator Test Prop Search"
    dummy_name: bpy.props.StringProperty(name="Property")

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

    def draw(self, context):
        row = self.layout.row()
        row.activate_init = True
        row.prop_search(self, "dummy_name", bpy.data, "materials")

    def execute(self, context):
        return {"FINISHED"}


bpy.utils.register_class(PopupOperatorText)
bpy.utils.register_class(PopupOperatorPropSearch)
1 Like

As far as I know, this isn’t possible currently, what I’d suggest is looking into the “search” argument when defining the StringProperty, which would allow you to immitate the prop_search widget by providing your own search function.

There’s some more information about it here:
https://docs.blender.org/api/current/bpy.props.html#bpy.props.StringProperty

1 Like

That’s a great suggestion! It indeed works like a charm.

image

Here’s the example code if anyone is curious:

import bpy

def get_items(self, context, edit_text):
    materials = bpy.data.materials.keys()
    return [(m, str(i)) for i, m in enumerate(materials)]

class PopupOperatorTextSearch(bpy.types.Operator):
    bl_idname = "test.popup_operator_text_search"
    bl_label = "Popup Operator Test Search"
    dummy_name: bpy.props.StringProperty(name="Property", default="test", search=get_items)

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

    def draw(self, context):
        row = self.layout.row()
        row.activate_init = True
        row.prop(self, "dummy_name", text='')

    def execute(self, context):
        return {"FINISHED"}


bpy.utils.register_class(PopupOperatorTextSearch)
2 Likes