Disable operator?

Suppose I have created an operator, and I want to disable it at some point, so that if you press its button you won’t be able to call it until it’s enabled again. Is there a way to do that? Thanks

Add a classmethod poll() which returns False

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

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        main(context)
        return {'FINISHED'}

Note that you can’t access any operator properties etc., as the first argument is “cls” (a reference to the class), not an instance of an operator (“self”).

You can also disable buttons in the layout code of your panel:

col = layout.column()
col.disabled = True
col.operator(...)

but it will still be possible to call that operator via PyConsole.

A third option is to return {‘CANCELLED’} in execute() if certain conditions aren’t met to abort the operator, the button will be clickable however.

Thanks a lot!

Just a slight correction to CoDEmanX’s response, you need to use the “enabled” property of the layout.

You can also disable buttons in the layout code of your panel:

Code:
col = layout.column()
col.disabled = True
col.operator(…)
but it will still be possible to call that operator via PyConsole.

I tried to use .disabled and I was getting an attribute error. I found enabled is the correct property to use.

whoops, absolutely correct!