Execute boolean property at the same time it is pressed?

I’ve created a boolean property to be used in a panel. But I only know how to use it to set an option which can then be later executed in the script somewhere else. How would I use the BoolProperty to execute my chosen functions as soon as it’s pressed?

In the code below, I need the “def execute(self,context):” section to be executed as soon as the bool is pressed or unpressed. Thanks for any help or ideas.

class optionsButtons(bpy.types.Operator):
        bl_idname = "object.options_buttons"
        bl_label = "Button"
        bpy.types.WindowManager.myBool = bpy.props.BoolProperty(name = "My Bool",
        description = "", default = False)

     def execute(self, context):                        
         if bpy.context.window_manager.myBool == True:
             #Do some stuff for if the button is pressed.
         else:
             #Do some stuff when the button is released.



Ok, maybe I worded this a bit confusingly. Can anyone show me how to properly use a BoolProperty such that I can direct it’s true and false states to execute separate functions?

That should happen automatically as you toggle the bool prop in the Redo panel. Your operator needs to have

bl_options = {‘REGISTER’, ‘UNDO’}

for that

It’s not working. The bool is working, the info screen console (not the headline information that would be displaying if it were working) displays the property as true or false when pressed and it looks like it’s being pressed on the screen, it’s just not executing the if statement. Here’s the full code. Am I missing some gears here?


import bpy


class optionsButtons(bpy.types.Operator):
    bl_idname = "object.options_buttons"
    bl_label = "Button"
    bl_options = {'REGISTER', 'UNDO'}

    bpy.types.WindowManager.myBool = bpy.props.BoolProperty(name = "My Bool",
    description = "", default = False)
         
    def execute(self, context):
        if bpy.context.window_manager.myBool == True:
            self.report({'INFO'}, "I am True")
        else:
            self.report({'INFO'}, "I am False")
                   
    
class myPanel(bpy.types.Panel):
   bl_label = "My Panel"
   bl_space_type = "VIEW_3D"
   bl_region_type = "UI"
    
   def draw(self, context):
       layout = self.layout
       row = layout.row()

       row.prop(context.window_manager, "myBool")
       

def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)
    
if __name__ == "__main__":
    register()

You’re using a wrong property:

bpy.types.WindowManager.myBool = bpy.props.BoolProperty(name = "My Bool",
description = "", default = False)

You’re not supposed to register global properties within an operator.

It should be

myBool = bpy.props.BoolProperty(name = "My Bool",
description = "", default = False)

And accessed via

self.myBool in execute()

You can’t access it from the panel, you need another property (global) for that.

import bpy

class PrintSomeString(bpy.types.Operator):
    bl_label = "Print Some String"
    bl_idname = "object.print_some_string"
    bl_options = {'REGISTER', 'UNDO'}
    
    print_this = bpy.props.StringProperty(name="Print this")
    
    def execute(self, context):
        self.report({'INFO'}, self.print_this)
        return {'FINISHED'}
        

class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        layout = self.layout
        layout.prop(context.window_manager, "some_string")
        
        props = layout.operator(PrintSomeString.bl_idname)
        props.print_this = context.window_manager.some_string

        props = layout.operator(PrintSomeString.bl_idname, text="Print welcome message")
        props.print_this = "Welcome to Blender!"

def register():
    bpy.utils.register_module(__name__)
    bpy.types.WindowManager.some_string = bpy.props.StringProperty(name="Some String")


def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.WindowManager.some_string


if __name__ == "__main__":
    register()


You can’t access it from the panel, you need another property (global) for that.

How would I do that? That’s the whole reason I posted this thread. I don’t understand how to make a bool execute as it is checked or unchecked. I came across a tutorial once that explained it but for the life of me I can’t seem to remember what it was called. I have no idea how to apply the code you provided to my problem.

Do you want to execute an operator when a checkbox in a panel is toggled?

Yeah, that’s my goal.

See here how you can do it:

import bpy

class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        layout = self.layout
        wm = context.window_manager

        layout.prop(wm, "my_bool")


def upd(self, context):
    # wm and context
    print(self, context)
    
    action = 'SELECT' if self.my_bool else 'DESELECT'
    bpy.ops.object.select_all(action=action)

def register():
    bpy.utils.register_class(HelloWorldPanel)
    bpy.types.WindowManager.my_bool = bpy.props.BoolProperty(update=upd)


def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)
    del bpy.types.WindowManager.my_bool


if __name__ == "__main__":
    register()


Ahh, yes. Excellent! Thanks a ton!