I have been trying to figure this out for a while with no luck. What I am trying to do is to create an operator which selects the next menu item.
For example, (template) ui_menu_simple.py:
(This example is using presets but the question is for menus in general)
If you have ‘123’ selected:
Instead of clicking on the dropdown and then manually selecting ‘456’, I want to create an operator that goes to the next option in the list. How do you control the menu’s selection with an operator?
— ?
ui_menu_simple.py: (with test operator)
import bpy
from bpy.types import Operator, Menu
from bl_operators.presets import AddPresetBase
class OBJECT_MT_display_presets(Menu):
bl_label = "Object Display Presets"
preset_subdir = "object/display"
preset_operator = "script.execute_preset"
draw = Menu.draw_preset
class AddPresetObjectDisplay(AddPresetBase, Operator):
'''Add a Object Display Preset'''
bl_idname = "camera.object_display_preset_add"
bl_label = "Add Object Display Preset"
preset_menu = "OBJECT_MT_display_presets"
# variable used for all preset values
preset_defines = [
"obj = bpy.context.object"
]
# properties to store in the preset
preset_values = [
"obj.display_type",
"obj.show_bounds",
"obj.display_bounds_type",
"obj.show_name",
"obj.show_axis",
"obj.show_wire",
]
# where to store the preset
preset_subdir = "object/display"
# Help!
class SelectNext(Operator):
bl_idname = "select.nextmenuitem"
bl_label = ""
bl_description = ""
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
return{'FINISHED'}
# Display into an existing panel
def panel_func(self, context):
layout = self.layout
row = layout.row(align=True)
row.menu(OBJECT_MT_display_presets.__name__, text=OBJECT_MT_display_presets.bl_label)
row.operator("select.nextmenuitem",text="",icon="TRIA_RIGHT")
row.operator(AddPresetObjectDisplay.bl_idname, text="", icon='ZOOM_IN')
row.operator(AddPresetObjectDisplay.bl_idname, text="", icon='ZOOM_OUT').remove_active = True
classes = (
OBJECT_MT_display_presets,
AddPresetObjectDisplay,
SelectNext,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.OBJECT_PT_display.prepend(panel_func)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
bpy.types.OBJECT_PT_display.remove(panel_func)
if __name__ == "__main__":
register()