[Bf-blender-cvs] [4fcc3b8ba20] master: WM: add operator to set the tool by it's index

Campbell Barton noreply at git.blender.org
Mon Jun 24 17:13:23 CEST 2019


Commit: 4fcc3b8ba20f34ba92f9ad90463502c96426df2e
Author: Campbell Barton
Date:   Tue Jun 25 01:06:09 2019 +1000
Branches: master
https://developer.blender.org/rB4fcc3b8ba20f34ba92f9ad90463502c96426df2e

WM: add operator to set the tool by it's index

Needed for 2.7x brush switching keys.

===================================================================

M	release/scripts/startup/bl_operators/wm.py
M	release/scripts/startup/bl_ui/space_toolsystem_common.py

===================================================================

diff --git a/release/scripts/startup/bl_operators/wm.py b/release/scripts/startup/bl_operators/wm.py
index 3920ff95a9e..003c92097eb 100644
--- a/release/scripts/startup/bl_operators/wm.py
+++ b/release/scripts/startup/bl_operators/wm.py
@@ -1542,6 +1542,56 @@ class WM_OT_tool_set_by_id(Operator):
             return {'CANCELLED'}
 
 
+class WM_OT_tool_set_by_index(Operator):
+    """Set the tool by index (for keymaps)"""
+    bl_idname = "wm.tool_set_by_index"
+    bl_label = "Set Tool By Index"
+    index: IntProperty(
+        name="Index in toolbar",
+        default=0,
+    )
+    cycle: BoolProperty(
+        name="Cycle",
+        description="Cycle through tools in this group",
+        default=False,
+        options={'SKIP_SAVE'},
+    )
+
+    expand: BoolProperty(
+        description="Include tool sub-groups",
+        default=False,
+    )
+
+    space_type: rna_space_type_prop
+
+    def execute(self, context):
+        from bl_ui.space_toolsystem_common import (
+            activate_by_id,
+            activate_by_id_or_cycle,
+            item_from_index,
+            item_from_flat_index,
+        )
+
+        if self.properties.is_property_set("space_type"):
+            space_type = self.space_type
+        else:
+            space_type = context.space_data.type
+
+        fn = item_from_flat_index if self.expand else item_from_index
+        item = fn(context, space_type, self.index)
+        if item is None:
+            # Don't report, since the number of tools may change.
+            return {'CANCELLED'}
+
+        # Same as: WM_OT_tool_set_by_id
+        fn = activate_by_id_or_cycle if self.cycle else activate_by_id
+        if fn(context, space_type, item.idname):
+            return {'FINISHED'}
+        else:
+            self.report({'WARNING'}, f"Tool {self.name!r:s} not found for space {space_type!r:s}.")
+            return {'CANCELLED'}
+
+
 class WM_OT_toolbar(Operator):
     bl_idname = "wm.toolbar"
     bl_label = "Toolbar"
@@ -1810,6 +1860,7 @@ classes = (
     WM_OT_owner_enable,
     WM_OT_url_open,
     WM_OT_tool_set_by_id,
+    WM_OT_tool_set_by_index,
     WM_OT_toolbar,
     WM_MT_splash,
 )
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)



More information about the Bf-blender-cvs mailing list