# Can the name field of the WindowManager´s “invoke_search_popup” method be reset?

I´m using the "invoke_search_popup" method of the Window Manager to create a search box which enables me to find some items in a predefined collection of objects.

The thing is, that sometimes I switch from some type of collection to another. Let´s say that I first search in a collection of Objects, and then in a collection of Groups. If I just selected an Object the first time, on the next one it´s name will appear as a suggestion or as the "last item used" in the name field of the new Groups search box, filtering all search results.

I understand that this happens globally because the window manager instance is created once, and then on, used again and again. So I want to know if at least there is some way to recreate or reload the instance, so that the name field becomes empty again.

It happens that when working on long sessions it´s annoying to first have to delete the name field everytime to clean the search results. I would like to have it clean everytime I call it, because it makes much more sense for the workflow I´m using. If there´s a way to achieve this, I could even clean it only when switching collections type, which is smarter.

Try this code. Select an item, and then, call the script again.

import bpy

class Operator(bpy.types.Operator):
bl_label = "Search"
bl_description = "Whatever"
bl_options = {'REGISTER', 'INTERNAL'}
bl_property = "enum"

def scene_items(self, context):
return [(ob.name, ob.name, '') for ob in bpy.data.objects]
enum = bpy.props.EnumProperty(items=scene_items)

def execute(self, context):
self.report({'INFO'}, "The item just got selected")
return {"FINISHED"}

def invoke(self, context, event):
context.window_manager.invoke_search_popup(self)
return {"FINISHED"}

def register():
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
register()


This got changed on Blender 2.79 due to a bug report I sent. The popup menu doesn't show the last search anymore. It gets emptied, showing all the list again. It would be cool though, if the scripter could be able to choose if he/she wants to clear the field or not, but maybe it will be necessary to file a feature request for that.

I'm looking for a good answer for this myself. I've found a hacky method that works half the time, and only on Windows:

Import Windows ctypes:

import bpy
import ctypes
cmd = """osascript -e 'tell application "System Events" to keystroke "m" using {command down}' """


And add "ctypes.windll.user32.keybd_event(0x08)" before invoke (0x08 is the delete key in Windows)

def invoke(self, context, event):
ctypes.windll.user32.keybd_event(0x08)
context.window_manager.invoke_search_popup(self)
return {"FINISHED"}


What this hack does, is send the "press" delete key command to Windows, so as soon as the search pops up, the field is cleared with an automatic press of delete. This doesn't always work, but it does sometimes. Also, it only works on Windows (there is something similar for Mac, but I can't test it).

• The default behaviour changed on future release 2.79. Check the answer for details. – Chaos Jun 29 '17 at 5:20