From 108475dc019d0b7f7c1f20acdd528832edc88901 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 18 Oct 2018 16:46:43 +1100 Subject: PyAPI: Support for custom tool registration Added a module bpy.utils.toolsystem which only exposes ToolDef, to avoid scripts referencing bl_ui internals. --- release/scripts/modules/bpy/utils/__init__.py | 42 +++++++++++++++++++++++++ release/scripts/modules/bpy/utils/toolsystem.py | 23 ++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 release/scripts/modules/bpy/utils/toolsystem.py (limited to 'release/scripts/modules/bpy') diff --git a/release/scripts/modules/bpy/utils/__init__.py b/release/scripts/modules/bpy/utils/__init__.py index 80e48697b2f..bb1ba5eb719 100644 --- a/release/scripts/modules/bpy/utils/__init__.py +++ b/release/scripts/modules/bpy/utils/__init__.py @@ -708,6 +708,48 @@ def register_submodule_factory(module_name, submodule_names): return register, unregister +# ----------------------------------------------------------------------------- +# Tool Registraion + +def register_tool(space_type, context_mode, tool_def): + from bl_ui.space_toolsystem_common import ToolSelectPanelHelper + cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type) + if cls is None: + raise Exception(f"Space type {space_type!r} has no toolbar") + tools = cls._tools[context_mode] + + keymap_data = tool_def.keymap + if keymap_data is not None: + if context_mode is None: + context_descr = "All" + else: + context_descr = context_mode.replace("_", " ").title() + from bpy import context + wm = context.window_manager + kc = wm.keyconfigs.default + if callable(keymap_data[0]): + cls._km_action_simple(kc, context_descr, tool_def.text, keymap_data) + + tools.append(tool_def) + + +def unregister_tool(space_type, context_mode, tool_def): + from bl_ui.space_toolsystem_common import ToolSelectPanelHelper + cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type) + if cls is None: + raise Exception(f"Space type {space_type!r} has no toolbar") + tools = cls._tools[context_mode] + tools.remove(tool_def) + + keymap_data = tool_def.keymap + if keymap_data is not None: + from bpy import context + wm = context.window_manager + kc = wm.keyconfigs.default + km = keymap_data[0] + kc.keymaps.remove(km) + + # ----------------------------------------------------------------------------- # Manual lookups, each function has to return a basepath and a sequence # of... diff --git a/release/scripts/modules/bpy/utils/toolsystem.py b/release/scripts/modules/bpy/utils/toolsystem.py new file mode 100644 index 00000000000..e2431536b4f --- /dev/null +++ b/release/scripts/modules/bpy/utils/toolsystem.py @@ -0,0 +1,23 @@ +# ##### BEGIN GPL LICENSE BLOCK ##### +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# ##### END GPL LICENSE BLOCK ##### + +# + +# Until we untangle ToolDef from bl_ui internals, +# use this module to document ToolDef. +from bl_ui.space_toolsystem_common import ToolDef -- cgit v1.2.3