-1
$\begingroup$

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).

$\endgroup$
7
  • 2
    $\begingroup$ Possible duplicate of How to create a custom UI? $\endgroup$ Commented Sep 3, 2019 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
    Commented Sep 3, 2019 at 14:24
  • $\begingroup$ That is true but what I need is a way for the bool property to affect the operator first $\endgroup$ Commented Sep 3, 2019 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
    Commented Sep 3, 2019 at 15:02
  • $\begingroup$ can you share an example script of what your're mentioning? $\endgroup$ Commented Sep 3, 2019 at 15:07

1 Answer 1

3
$\begingroup$

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,
                       PointerProperty,
                       )

from bpy.types import (Panel,
                       Operator,
                       PropertyGroup,
                       )


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

class MyProperties(PropertyGroup):

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

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

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):
        scene = context.scene
        mytool = scene.my_tool

        if mytool.my_bool:
            print ("Bool Property enabled")
        else:
            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"   


    @classmethod
    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")
        layout.operator(SimpleOperator.bl_idname)

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

        layout.separator()

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

classes = (
    MyProperties,
    SimpleOperator,
    OBJECT_PT_CustomPanel
)

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

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

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


if __name__ == "__main__":
    register()

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

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .