0
$\begingroup$

I have a checkbox in my addon and I wonder how I can I execute or run some code when checking and unchecking it. I found this link but I tried following the given answer there (as shown below) but it is not working, nothing happens when I check/uncheck the checkbox.

This is my script:

bl_info = {
    "name": "tester",
    "description": "",
}

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

class MySettings(PropertyGroup):

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

class UV_OT_my_panel(Panel):
    bl_idname = "UV_OT_my_panel"
    bl_label = "My Tool"
    bl_space_type = "VIEW_3D"   
    bl_region_type = "TOOLS"    
    bl_category = "Tools"

    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")
#           bpy.data.objects['Cube'].hide = True

        else:
            print ("Property Disabled")
#           bpy.data.objects['Cube'].hide = False

def register():
    bpy.utils.register_module(__name__)
    bpy.types.Scene.my_tool = PointerProperty(type=MySettings)

def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.my_tool

if __name__ == "__main__":
    register()
os.system('cls')
def my_prop_callback(self, context):
    if context.scene.my_prop:
        #pass # do something here if user checks checkbox (but not uncheck, just as an example)
        print('Enteredddddddddddddddddddddddddd')
        if context.scene.mytool.my_bool == True:
            bpy.data.objects['Cube'].hide = True
        else:
            bpy.data.objects['Cube'].hide = False    

bpy.types.Scene.my_prop = bpy.props.BoolProperty(update=my_prop_callback)
$\endgroup$
2
$\begingroup$

actually your code is running... but you must open your blender with the cmd (console)

open blender in console

after that you can see the prints in the cmd

cmd

/**** update

you should check enter link description here

I clean the code....

properties

bl_info = {
    "name": "tester",
    "description": "",
}

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
# ------------------------------------------------------------------------
def update_func(self, context):
    for obj in bpy.data.objects:
        if (self.my_bool == True):
            #print ("Property Enabled")
            obj.hide = True
        else:
            #print ("Property Disabled")
            a = obj.hide=False
            print(a)
def update_func_1(self, context):
    for obj in bpy.data.objects:
        if (self.my_bool1 == True and obj.name=="Cube"):
            print ("Property Enabled")
            obj.hide = True
        else:
            print ("Property Disabled")
            obj.hide = False

class MySettings(PropertyGroup):

    my_bool = BoolProperty(
        name="Enable or Disable",
        description="A bool property",
        default = False,
        update = update_func
        )
    my_bool1 = BoolProperty(
        name="Enable or Disable",
        description="A bool property",
        default = False,
        update = update_func_1
        )
    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
        )

# ------------------------------------------------------------------------
#    myTool in the image editor
# ------------------------------------------------------------------------

class UV_OT_my_panel(Panel):
    bl_idname = "UV_OT_my_panel"
    bl_label = "My Tool_modify"
    bl_space_type = "VIEW_3D"   
    bl_region_type = "TOOLS"    
    bl_category = "Tools"

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

        # display the properties
        layout.prop(mytool, "my_bool", text="hide everything")
        layout.prop(mytool, "my_bool1", text="only hide cube")
        layout.prop(mytool, "my_int", text="Integer Property")
        layout.prop(mytool, "my_float", text="Float Property")



# ------------------------------------------------------------------------
# register and unregister functions
# ------------------------------------------------------------------------

def register():
    bpy.utils.register_module(__name__)
    bpy.types.Scene.my_tool = PointerProperty(type=MySettings)

def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.my_tool

if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ I know that the printing is working in the console, I want to hide/unhide a Cube in my scene when checking/unchecking the checkbox. As you can see in my script I've tried hiding/unhiding it but it didn't work that's why I commented it. I found the link I provided in my question and tried applying it but it is not working for some reason I don't know. $\endgroup$ – Tak Jan 10 '17 at 2:21
  • 1
    $\begingroup$ @Tak I update the answere. I think you can continue after this $\endgroup$ – yhoyo Jan 10 '17 at 4:12
2
$\begingroup$

You have to put a reference to the callback in the update of your property:

bl_info = {
"name": "tester",
"description": "",
}

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

def my_prop_callback(self, context):
    #pass # do something here if user checks checkbox (but not uncheck, just         as an example)
    print("my_prop_callback", repr(self))
    cube = self.id_data.objects.get("Cube")
    if cube:
        cube.hide = not self.my_bool
    return None   

class MySettings(PropertyGroup):

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

class UV_OT_my_panel(Panel):
    bl_idname = "UV_OT_my_panel"
    bl_label = "My Tool"
    bl_space_type = "VIEW_3D"   
    bl_region_type = "TOOLS"    
    bl_category = "Tools"

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

def register():
    bpy.utils.register_module(__name__)
    bpy.types.Scene.my_tool = PointerProperty(type=MySettings)

def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.my_tool

if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ I'm not sure how I can use my_bool property in the callback? $\endgroup$ – Tak Jan 10 '17 at 2:50
  • 1
    $\begingroup$ No, I mean you never use my_prop in the code, at all, ever. I assume it's supposed to be in your panel, but its still the other properties. $\endgroup$ – cmomoney Jan 10 '17 at 3:05
  • 1
    $\begingroup$ It does, his property doesn't work because he never uses it in the code. $\endgroup$ – cmomoney Jan 10 '17 at 3:23
  • $\begingroup$ What you are saying is not answering my question, if you could please provide an answer to fix my problem $\endgroup$ – Tak Jan 10 '17 at 3:33

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.