Python tool set/unset Handlers #118026

Open
Jaume Bellet wants to merge 5 commits from JaumeBellet/blender:pr-0021-tool-handlers into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
First-time contributor

This patch allows to create a Callback function on python, that is being
executed on operator change (set / unset)

Applies updated diff from https://archive.blender.org/developer/D10635

Adds a handler object on ToolDef, with 'set' 'unset' containing and array of callback function for the tool.

Example code

import bl_ui
import bpy

from bpy.types import WorkSpaceTool


class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    def execute(self, context):
        print ("Simple operator");
        return {'FINISHED'}



class MyTool(WorkSpaceTool):
    bl_space_type = 'VIEW_3D'
    bl_context_mode = 'OBJECT'

    # The prefix of the idname should be your add-on name.
    bl_idname = "me.my_tool"
    bl_label = "My Tool"
    bl_description = "This is a test"
    
    def set_cb():
        print ("set on MyTool Class");
        
    def unset_cb():
        print ("Unset on MyTool Class");

def tool_select_set_cb():
    print("select is set")

def tool_set_another_cb():
    print("another set")

def tool_unset_cb():
    print("select is unset")
    
def my_tool_set_cb():
    print ("my tool is set")
    bpy.ops.object.simple_operator()


mytool = bpy.utils.register_tool(MyTool, after={"builtin.scale_cage"}, separator=True, group = True)
bpy.utils.register_class(SimpleOperator)

# handlers can be also be set on Tool Class.
if not mytool is None:
    mytool.handlers.set.append(my_tool_set_cb) 

# The tool maybe the same tool in diferent modes. 
tool = bl_ui.space_toolsystem_common.item_from_id(bpy.context,'VIEW_3D', 'builtin.select')

# if bpy.context does not have the mode when the function executed, you can try to force it.
# tool = bl_ui.space_toolsystem_common.item_from_id(None,'VIEW_3D', 'builtin.select', 'OBJECT')

tool.handlers.set.append(tool_select_set_cb)
tool.handlers.set.append(tool_set_another_cb)
tool.handlers.unset.append(tool_unset_cb)
tool.handlers.set.remove(tool_set_another_cb)

#changing into select tool should output 
"""
select is set
"""

#going to another tool from select tool should output 
"""
select is unset
"""

#changing into my tool should output 
"""
my tool is set
Simple operator
set on MyTool Class
"""

#going to another tool from my tool should output 
"""
Unset on MyTool Class
"""
This patch allows to create a Callback function on python, that is being executed on operator change (set / unset) Applies updated diff from https://archive.blender.org/developer/D10635 Adds a handler object on ToolDef, with 'set' 'unset' containing and array of callback function for the tool. Example code ```Py import bl_ui import bpy from bpy.types import WorkSpaceTool class SimpleOperator(bpy.types.Operator): """Tooltip""" bl_idname = "object.simple_operator" bl_label = "Simple Object Operator" def execute(self, context): print ("Simple operator"); return {'FINISHED'} class MyTool(WorkSpaceTool): bl_space_type = 'VIEW_3D' bl_context_mode = 'OBJECT' # The prefix of the idname should be your add-on name. bl_idname = "me.my_tool" bl_label = "My Tool" bl_description = "This is a test" def set_cb(): print ("set on MyTool Class"); def unset_cb(): print ("Unset on MyTool Class"); def tool_select_set_cb(): print("select is set") def tool_set_another_cb(): print("another set") def tool_unset_cb(): print("select is unset") def my_tool_set_cb(): print ("my tool is set") bpy.ops.object.simple_operator() mytool = bpy.utils.register_tool(MyTool, after={"builtin.scale_cage"}, separator=True, group = True) bpy.utils.register_class(SimpleOperator) # handlers can be also be set on Tool Class. if not mytool is None: mytool.handlers.set.append(my_tool_set_cb) # The tool maybe the same tool in diferent modes. tool = bl_ui.space_toolsystem_common.item_from_id(bpy.context,'VIEW_3D', 'builtin.select') # if bpy.context does not have the mode when the function executed, you can try to force it. # tool = bl_ui.space_toolsystem_common.item_from_id(None,'VIEW_3D', 'builtin.select', 'OBJECT') tool.handlers.set.append(tool_select_set_cb) tool.handlers.set.append(tool_set_another_cb) tool.handlers.unset.append(tool_unset_cb) tool.handlers.set.remove(tool_set_another_cb) #changing into select tool should output """ select is set """ #going to another tool from select tool should output """ select is unset """ #changing into my tool should output """ my tool is set Simple operator set on MyTool Class """ #going to another tool from my tool should output """ Unset on MyTool Class """ ```
Jaume Bellet added 1 commit 2024-02-09 07:50:31 +01:00
32c5c09284 apply updated diff from https://archive.blender.org/developer/D10635
This patch allows to create a Callback function on python, that is being
executed on operator change (set / unset)
Jaume Bellet changed title from apply updated diff from https://archive.blender.org/developer/D10635 to Python tool set/unset Handlers 2024-02-09 07:51:57 +01:00
Iliya Katushenock added this to the Python API project 2024-02-09 09:32:15 +01:00
First-time contributor

Something like this is exactly what I need for an Edit WorkSpaceTool that I'm developing so I can determine when someone activates or deactivates the tool. Right now, Blender's tool system doesn't appear to have this functionality and I have to use a timer to detect tool changes.

However, this pull request needs some work. Right now you cannot use it to setup the handlers in the addon's register and unregister methods which I think is a requirement for inclusion in Blender.

Something like this is exactly what I need for an Edit WorkSpaceTool that I'm developing so I can determine when someone activates or deactivates the tool. Right now, Blender's tool system doesn't appear to have this functionality and I have to use a timer to detect tool changes. However, this pull request needs some work. Right now you cannot use it to setup the handlers in the addon's `register` and `unregister` methods which I think is a requirement for inclusion in Blender.
Jaume Bellet added 2 commits 2024-02-11 19:38:22 +01:00
3143bab74e bpy.utils.register_tools return the registered tools.
added a check before applying unset cb. May be set None when the tool does no
exists, for example on blender restart and not being resistered on
startup.
67e3940432 added mode on bl_ui.space_toolsystem_common.item_from_id and bypass it
until cls.tools_from_context

This allows getting the tool when the context does not have the
mode set, eg while registiring data.
Author
First-time contributor

I added two commits.

The first one, allows to set the handler on the tool using the data returned by register_tool, so you don't need the context at all.

The second one allows to specify the mode on item_from_id so it does not use the context. It was possible, only bypassed the value, so i don't know if there maybe other some cases when context is needed.

Also fixed issue when the selected tool was saved on file, and not available on blender startup.

I added two commits. The first one, allows to set the handler on the tool using the data returned by register_tool, so you don't need the context at all. The second one allows to specify the mode on `item_from_id` so it does not use the context. It was possible, only bypassed the value, so i don't know if there maybe other some cases when context is needed. Also fixed issue when the selected tool was saved on file, and not available on blender startup.
Jaume Bellet added 1 commit 2024-02-11 20:14:15 +01:00
Author
First-time contributor

Allow to set callback functions in WorkSpaceTool Class . This way the final code is cleaner.

Previous procedures allows you to set callbacks over other tools you have not defined, that is great for expanding some kind of functionality.

Allow to set callback functions in WorkSpaceTool Class . This way the final code is cleaner. Previous procedures allows you to set callbacks over other tools you have not defined, that is great for expanding some kind of functionality.
Jaume Bellet added 1 commit 2024-02-11 21:23:48 +01:00
Merge conflict checking is in progress. Try again in few moments.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u pr-0021-tool-handlers:JaumeBellet-pr-0021-tool-handlers
git checkout JaumeBellet-pr-0021-tool-handlers
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#118026
No description provided.