1
$\begingroup$

I want to update a layout.label based on a selected manipulator tool. I don't want to have it update on selection of object or change of interaction mode, rather, update when the tool has been changed.

tool: str = str()

@persistent
def tool_updated():
    global tool
    context = bpy.context
    workspace = context.workspace.name
    tools = bpy.data.workspaces[workspace].tools
    active_tool = tools.from_space_view3d_mode(context.mode).idname
    tool = ".".join(str(active_tool).split('.')[1:])

    # check again once every second.
    return 0.5


class InfoPanel(bpy.types.Panel):
    bl_idname = "OBJECT_PT_InfoTools"
    bl_label = "Info Tools"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Info"
    bl_options = {"DEFAULT_CLOSED"}

    def draw(self, context):
        self.layout.label(text="Blender Info Tools")
        self.layout.label(text="Current Tool: {0}".format(tool))  # I want to update this each time the manipulator tool is changed... Move to Rotate to Scale to Crease to Transform, etc.


def register():
    bpy.utils.register_class(InfoPanel)
    bpy.app.timers.register(tool_updated)


def unregister():
    bpy.utils.unregister_class(InfoPanel)
    bpy.app.timers.unregister(tool_updated)


if __name__ == "__main__":
    register()
$\endgroup$

1 Answer 1

1
$\begingroup$

I have updated my code to "refresh" the panel. This is the closest solution I have found and experimented with. It does work pretty well, if I might add.

tool: str = str()

@persistent
def tool_updated():
    global tool
    context = bpy.context
    workspace = context.workspace.name
    tools = bpy.data.workspaces[workspace].tools
    active_tool = tools.from_space_view3d_mode(context.mode).idname
    tool = ".".join(str(active_tool).split('.')[1:])
    
    redraw_panel()
    
    # check again once every second.
    return 0.5


class InfoPanel(bpy.types.Panel):
    bl_idname = "OBJECT_PT_InfoTools"
    bl_label = "Info Tools"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Info"
    bl_options = {"DEFAULT_CLOSED"}

    def draw(self, context):
        self.layout.label(text="Blender Info Tools")
        self.layout.label(text="Current Tool: {0}".format(tool))  # I want to update this each time the manipulator tool is changed... Move to Rotate to Scale to Crease to Transform, etc.

def redraw_panel():
    try:
        bpy.utils.unregister_class(InfoPanel)
    except:
        pass
    bpy.utils.register_class(InfoPanel)

def register():
    bpy.utils.register_class(InfoPanel)
    bpy.app.timers.register(tool_updated)


def unregister():
    bpy.utils.unregister_class(InfoPanel)
    bpy.app.timers.unregister(tool_updated)


if __name__ == "__main__":
    register()
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .