From 4fcc3b8ba20f34ba92f9ad90463502c96426df2e Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 25 Jun 2019 01:06:09 +1000 Subject: WM: add operator to set the tool by it's index Needed for 2.7x brush switching keys. --- .../startup/bl_ui/space_toolsystem_common.py | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'release/scripts/startup/bl_ui/space_toolsystem_common.py') diff --git a/release/scripts/startup/bl_ui/space_toolsystem_common.py b/release/scripts/startup/bl_ui/space_toolsystem_common.py index 1d401ebd2c9..e7e95c26b55 100644 --- a/release/scripts/startup/bl_ui/space_toolsystem_common.py +++ b/release/scripts/startup/bl_ui/space_toolsystem_common.py @@ -295,6 +295,45 @@ class ToolSelectPanelHelper: return (cls, item, index) return None, None, -1 + @staticmethod + def _tool_get_by_flat_index(context, space_type, tool_index): + """ + Return the active Python tool definition and index (if in sub-group, else -1). + + Return the index of the expanded list. + """ + cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type) + if cls is not None: + i = 0 + for item, index in ToolSelectPanelHelper._tools_flatten_with_tool_index(cls.tools_from_context(context)): + if item is not None: + if i == tool_index: + return (cls, item, index) + i += 1 + return None, None, -1 + + @staticmethod + def _tool_get_by_index(context, space_type, tool_index): + """ + Return the active Python tool definition and index (if in sub-group, else -1). + + Return the index of the list without expanding. + """ + cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type) + if cls is not None: + i = 0 + for item in cls.tools_from_context(context): + if item is not None: + if i == tool_index: + if type(item) is tuple: + index = cls._tool_group_active.get(item[0].idname, 0) + item = item[index] + else: + index = -1 + return (cls, item, index) + i += 1 + return None, None, -1 + @staticmethod def _tool_active_from_context(context, space_type, mode=None, create=False): if space_type == 'VIEW_3D': @@ -772,6 +811,16 @@ def item_from_id(context, space_type, idname): return item +def item_from_flat_index(context, space_type, index): + _cls, item, _index = ToolSelectPanelHelper._tool_get_by_flat_index(context, space_type, index) + return item + + +def item_from_index(context, space_type, index): + _cls, item, _index = ToolSelectPanelHelper._tool_get_by_index(context, space_type, index) + return item + + def keymap_from_id(context, space_type, idname): # Used directly for tooltips. _cls, item, _index = ToolSelectPanelHelper._tool_get_by_id(context, space_type, idname) -- cgit v1.2.3