5

So I want to add a checkbox to the UV/Image Editor tools UI

enter image description here

This is currently my code, You can see where I have commented "#Checkbox Here" and later on in the UpdateOperator I want to check whether the box is ticked or not.

import bpy

class UpdatePanel(bpy.types.Panel):
    bl_category = "EasyFX"
    bl_label = "Update"
    bl_idname = "Update"
    bl_space_type = 'IMAGE_EDITOR'
    bl_region_type = 'TOOLS'

    def draw(self, context):
        layout = self.layout
        col = layout.column(align=True)
        row = col.row(align = True)
        row.operator('object.update_operator', text = "Update")


class FilterPanel(bpy.types.Panel):
    bl_category = "EasyFX"
    bl_label = "Filter"
    bl_idname = "Filter"
    bl_space_type = 'IMAGE_EDITOR'
    bl_region_type = 'TOOLS'

    def draw(self, context):
        layout = self.layout
        col = layout.column(align=True)
        row = col.row(align=True)

        #Checkbox Here


class UpdateOperator(bpy.types.Operator):
    '''Update'''
    bl_idname = "object.update_operator"
    bl_label = "Update Nodes Operator"

    def execute(self, context):

        #if checkbox == True: 
        #    bpy.data.scenes['Scene'].use_nodes = True

        pass
        return {'FINISHED'}

def register():
    bpy.utils.register_class(UpdatePanel)
    bpy.utils.register_class(UpdateOperator)
    bpy.utils.register_class(FilterPanel)
def unregister():
    bpy.utils.unregister_class(FilterPanel)
if __name__ == '__main__':
    register()
someonewithpc
  • 12,381
  • 6
  • 55
  • 89
Nils Söderman
  • 586
  • 2
  • 4
  • 11

2 Answers2

16

For a 'checkbox' you have to provide a BoolProperty:

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

I assume you've got more than 1 property at the end, thus I would also suggest create a 'settings class' for your addon by using a PropertyGroup:

class MySettings(PropertyGroup):

    my_bool : BoolProperty()
    my_int : IntProperty()
    my_float : FloatProperty()
    ...

Following example appends a custom panel to the Tool Shelf of the UV/Image Editor (for more options see bl_space_type) and prints the 'current state' of the BoolProperty to the console:

enter image description here

bl_info = {
    "name": "tester",
    "description": "",
    "author": "poor",
    "version": (0, 0, 1),
    "blender": (2, 80, 0),
    "location": "UV/Image Editor > Tool Shelf (T)",
    "warning": "", # used for warning icon and text in addons panel
    "wiki_url": "",
    "tracker_url": "",
    "category": "Test"
}

import bpy

from bpy.props import (StringProperty,
                       BoolProperty,
                       IntProperty,
                       FloatProperty,
                       FloatVectorProperty,
                       EnumProperty,
                       PointerProperty,
                       )
from bpy.types import (Panel,
                       Operator,
                       AddonPreferences,
                       PropertyGroup,
                       )


# ------------------------------------------------------------------------
#    Store properties in the active scene
# ------------------------------------------------------------------------

class MySettings(PropertyGroup):

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

    my_int : IntProperty(
        name = "Set a value",
        description="A integer property",
        default = 23,
        min = 10,
        max = 100
        )

    my_float : FloatProperty(
        name = "Set a value",
        description = "A float property",
        default = 23.7,
        min = 0.01,
        max = 30.0
        )

# ------------------------------------------------------------------------
#    My Tool in the image editor
# ------------------------------------------------------------------------

class UV_PT_my_panel(Panel):
    bl_idname = "UV_PT_my_panel"
    bl_label = "My Tool"
    bl_category = "My Category"
    bl_space_type = 'IMAGE_EDITOR'
    bl_region_type = 'UI'

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

        # display the properties
        layout.prop(mytool, "my_bool", text="Bool Property")
        layout.prop(mytool, "my_int", text="Integer Property")
        layout.prop(mytool, "my_float", text="Float Property")

        # check if bool property is enabled
        if (mytool.my_bool == True):
            print ("Property Enabled")
        else:
            print ("Property Disabled")


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

classes = (
    MySettings,
    UV_PT_my_panel,
)

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

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

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

Note: For demonstration purposes I've also added an Int- and a FloatProperty.

Further reading: How to create a custom UI?

brockmann
  • 12,613
  • 4
  • 50
  • 93
p2or
  • 15,860
  • 10
  • 83
  • 143
5

You just need to map a boolean variable to the row property.

import bpy

class FilterPanel(bpy.types.Panel):
    bl_category = "EasyFX"
    bl_label = "Filter"
    bl_idname = "Filter"
    bl_space_type = 'IMAGE_EDITOR'
    bl_region_type = 'TOOLS'

    def draw(self, context):
        scene = context.scene
        layout = self.layout
        col = layout.column(align=True)
        row = col.row(align=True)

        #Checkbox Here
        #(implied from property type = bool)
        row.prop(scene, "my_prop")

def register():
    # add your custom property to the Scene, but if you
    # have to register many properties consider using a 
    # property group. 
    bpy.types.Scene.my_prop = bpy.props.BoolProperty(
        name="Prop name",
        description="Some tooltip",
        default = True)

    bpy.utils.register_class(FilterPanel)

def unregister():
    bpy.utils.unregister_class(FilterPanel)
    del bpy.types.Scene.my_prop
zeffii
  • 39,634
  • 9
  • 103
  • 186
Todd McIntosh
  • 9,421
  • 25
  • 50