Share operator properties in WorkSpaceTool

I am writing a custom WorkSpaceTool, getting inspiration from the related Python templates, and in particular I display some tool settings:

tool

Some of these settings come from the operator called when mouse moves, some others from the one called when the user clicks:

props = tool.operator_properties(ops.OperatorCalledOnClick.bl_idname)
layout.prop(props, "freeze_threshold")
layout.prop(props, "solver", text="")
props = tool.operator_properties(ops.OperatorCalledOnMove.bl_idname)
layout.prop(props, "preview_scale")

But how can I have a setting that affects both operators without displaying it several times in the UI?

I think you might’ve answered your own question.

Store common props on one operator, e.g OperatorCalledOnMove, then access them from others using:

any_tool = context.workspace.tools.from_space_view3d_mode(context.mode, create=True)
props = any_tool.operator_properties(ops.OperatorCalledOnMove.bl_idname)

The any_tool part is telling blender to get the current tool. The tool itself doesn’t matter, only its operator_properties lookup method.

Blender keeps an OperatorProperties instance of all operators. That’s what you’re already accessing in your post when drawing props in the ui. You can access these properties from any operator - they are persistent and don’t disappear when an associated operator instance is destroyed.

This, however, means the properties are runtime and never saved with the session. May or may not be desirable in your case.
The other solution is to use a bpy.types.PropertyGroup class for common properties and have them stored using a bpy.props.PointerProperty in bpy.types.WorkSpace. Workspaces are saved with the session.

1 Like

Thanks! This any_tool part feels a bit hacky but it realy is what I was looking for. :slight_smile: