I'm trying to create an Operator and I was wondering what code to use to specifically toggle the Operator? I'm working on with a checkbox (BoolProperty).

  • 2
    $\begingroup$ Possible duplicate of How to create a custom UI? $\endgroup$ – Robert Gützkow Sep 3 '19 at 13:32
  • 1
    $\begingroup$ The checkbox can be displayed as toggle layout.prop(mytool, "my_bool", toggle=True). In addition to the post rjg linked to, have a look into: Quickest way to create panel buttons with quick functionality $\endgroup$ – brockmann Sep 3 '19 at 14:24
  • $\begingroup$ That is true but what I need is a way for the bool property to affect the operator first $\endgroup$ – Scottie Doria Sep 3 '19 at 14:29
  • $\begingroup$ A bool property that affects the operator? What do you mean by that? @ScottieDoria In other words: You can't toggle an operator (read the post I've linked). However you can use a bool property as part of an operator along with a scene property (synced state) the user can toggle before executing it. Is that what you want? $\endgroup$ – brockmann Sep 3 '19 at 15:02
  • $\begingroup$ can you share an example script of what your're mentioning? $\endgroup$ – Scottie Doria Sep 3 '19 at 15:07

You can access the values of all properties (almost). The following Operator prints the current state (True, False) of a custom scene property called my_bool which is displayed at the top of the panel:

import bpy

from bpy.props import (BoolProperty,

from bpy.types import (Panel,

# ------------------------------------------------------------------------
#    Scene Properties
# ------------------------------------------------------------------------

class MyProperties(PropertyGroup):

    my_bool: BoolProperty(
        name="Enable or Disable",
        description="A bool property",
        default = False

# ------------------------------------------------------------------------
#    Operators
# ------------------------------------------------------------------------

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

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

    def execute(self, context):
        scene = context.scene
        mytool = scene.my_tool

        if mytool.my_bool:
            print ("Bool Property enabled")
            print ("Bool Property disabled")

        return {'FINISHED'}

# ------------------------------------------------------------------------
#    Panel in Object Mode
# ------------------------------------------------------------------------

class OBJECT_PT_CustomPanel(Panel):
    bl_label = "My Panel"
    bl_idname = "OBJECT_PT_custom_panel"
    bl_space_type = "VIEW_3D"   
    bl_region_type = "UI"
    bl_category = "Tools"
    bl_context = "objectmode"   

    def poll(self,context):
        return context.object is not None

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        mytool = scene.my_tool

        layout.prop(mytool, "my_bool")

        state_txt = "enabled" if mytool.my_bool else "disabled"
        layout.label(text="Bool Property {}".format(state_txt))


# ------------------------------------------------------------------------
#    Registration
# ------------------------------------------------------------------------

classes = (

def register():
    from bpy.utils import register_class
    for cls in classes:

    bpy.types.Scene.my_tool = PointerProperty(type=MyProperties)

def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
    del bpy.types.Scene.my_tool

if __name__ == "__main__":

Layout code ripped from: How to create a custom UI?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.