How to access a custom propery on another operator?

I’m trying to figure out how to access a property on an operator that is different from the operator that is running. Other threads have answered this by suggesting creating a PropertyGroup object to organize all the plugin’s properties, but that’s not an option for me here since I’m modifying an existing project that’s not set up to have that.

Just playing around in the Blender source editor, I created a class

class OBJECT_OT_bone_list_actions(bpy.types.Operator):
    bl_idname = "object.bone_list_actions"
    bl_label = "Bone List Actions"
    bl_options = {"REGISTER", "UNDO"}

    action: bpy.props.EnumProperty(
        items=(
            ("ADD", "Add", "Add a new bone slot"),
            ("ADD_SELECTED", "Add Selected", "Add currently selected bones"),
            ("REMOVE", "Remove", "Remove active bone slot"),
        )
    )


classes = (
    OBJECT_OT_bone_list_actions,
)


def register():
    for c in classes:
        bpy.utils.register_class(c)

I’m trying to query the action property in the Blender python console, but nothing’s working. bpy.ops.object.bone_list_actions does not seem to have action as a property.

>>> bpy.ops.object.bone_list_actions.action
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
AttributeError: 'BPyOpFunction' object has no attribute 'action'

What can I do to access this property on this operator?

You can access operator’s properties from the operator itself when it’s run. If you want to access properties from multiple operators, they need to be attached to something else that’s why you get recommendations to use some property group or define the properties somewhere else than the operator.

You just have to set it up like that then. You can have a separate operator to set some properties that another operator then uses, but those cannot be saved in an operator in question, they have to be attached to something else for example objects, materials, bones, scene, add-on preferences or whatever else. There could be some sort of operation in some operator where it sets some properties for itself and then continues to do something with them, an operator may call another one and pass some properties to it as well… You could set operator properties when calling it from the UI code or a hotkey…

Yes, MartinZ is correct that a property only exists in an operator whilst it is running. bpy.ops.object.bone_list_actions is a wrapper function, not the operator itself. So .action on it gives AttributeError: 'BPyOpFunction' object has no attribute 'action'. Within an operator it is self.action, and this is only available in execute/invoke/draw.

However, you do not necessarily need a whole PropertyGroup. Depending on what you need:

  1. Share a value between operators (least intrusive): add the property to the scene.
bpy.types.Scene.bone_list_action = bpy.props.EnumProperty(items=...)

Any operator can then read or write context.scene.bone_list_action.

  1. Pass a value when calling it: bpy.ops.object.bone_list_actions(action='REMOVE'). Or from within a panel: op = layout.operator("object.bone_list_actions") then op.action = 'REMOVE'.
  2. Read the property’s definition (such as items and default): bpy.ops.object.bone_list_actions.get_rna_type().properties['action']. .enum_items and .default are on that.

In addition to that it’s worth to consider what makes sense. For example it may(or may not) be more logical to store the property on the bones themselves - maybe it may be more convenient to set it from the sidebar with the bones selected or something like that, or maybe scene makes most sense because you might want to make a list of them, or maybe per armature since you might want to check lists per rig :man_shrugging: . It’s worth considering user experience and how it’s going to work.