0
$\begingroup$

Hi Blender/Python users,

I am working on an add-on where I'm trying to make an operator class as a button that runs a different set of commands depending on whether custom boolean properties are checked as true or false(via checkbox in the UI). Unfortunately my code is running incorrectly so I'm wondering what exactly went wrong.

Below is the code I am trying to debug:

    import bpy


    #addon Info
    bl_info = {  
     "name": "System",  
     "author": "Hamilton",  
     "version": (1, 0),  
     "blender": (2, 5, 7),  
     "location": "View3D > Tool Shelf > LightArchitect",  
     "description": "Controls for addon",  
     "warning": "",  
     "wiki_url": "",  
     "tracker_url": "",  
     "category": "Append"} 


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


    '''First we create checkboxes using boolean properties for the user to select the
    custom properties'''


    #This is our CHECKBOX PROPERTY CLASS
    class MySettings(PropertyGroup):

        my_bool = BoolProperty(
            name="Enable or Disable",
            description="Checkbox",
            default = False
            )


    '''Second we define operator presets'''

    '''These operator presets take into account the checkbox properties listed above that
    customize what commands are run'''


    #Preset Operator button here below
    class DirectionalPreset (bpy.types.Operator):
       bl_idname = 'my.direction'
       bl_label = 'Directional burst'

         def execute(self, context):
            if (self.my_bool == True):
        #add plane command
        bpy.ops.mesh.primitive_plane_add(view_align=False, enter_editmode=False, location=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))


        return{'FINISHED'}


    '''In the second half of the code we create the one tab and panels'''


    #Preset Class panel Below
    class CablecamAppendpanel(bpy.types.Panel):
        """Creates a Panel in the Object properties window"""
        bl_label = "CableCam: Cinematic Movement System"
        bl_idname = "Cablecamrig"
        bl_space_type = 'VIEW_3D'
        bl_region_type = 'TOOLS'
        bl_category = "Cable Cam"

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

            obj = context.object
            row = layout.row()
            # display the checkbox properties in the below line
            self.layout.prop(scene.my_tool, "my_bool", text="Checkbox")

            row = layout.row()
            row = layout.row()
            row.operator("my.direction")


    #REGISTRY PANEL BELOW

    def register():
        #Register Preset Panels Below
        bpy.utils.register_class(CablecamAppendpanel)


        #And below the panel registry is the Operator registry
        bpy.utils.register_class(DirectionalPreset)




    def unregister():
        #unregister Preset Panels Below
        bpy.utils.unregister_class(CablecamAppendpanel)

        #And below the panel unregistry is the Operator unregistry
        bpy.utils.unregister_class(DirectionalPreset)

    if __name__ == "__main__":
        register()

    #Boolean checkbox property Script Register panel

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

As you can probably see in this example I am trying to make my directional preset operator button add a plane to the scene ONLY if my_bool is true. The panel that this code adds looks like this...but doesn't add a plane whether or not the checkbox is selected.

enter image description here

$\endgroup$
  • $\begingroup$ This code in operator if (self.my_bool == True): implies that the boolean prop is an operator property. And since it is not defined will throw a syntax error. Change selfto context.scene.my_bool $\endgroup$ – batFINGER Sep 26 '18 at 2:36
  • $\begingroup$ Hi Batfinger. Thank you for the help. I changed the operator to context.scen.my_bool and I am still getting the following error: 'Scene' object has no attribute 'my_bool'. Anything I'm missing? $\endgroup$ – Brad Hamilton Sep 26 '18 at 3:30
  • 2
    $\begingroup$ Oops like in the panel scene.my_tool.my_bool $\endgroup$ – batFINGER Sep 26 '18 at 3:44
  • $\begingroup$ Just going by the code you pasted, you have indentation issues, the plane_add needs to be indented from the if and the return needs to be equal to the if. That would stop the add plane working. You also have two register and unregister functions, or are you messily combining two scripts? $\endgroup$ – sambler Sep 26 '18 at 5:01
  • $\begingroup$ Thank you both so much. I'm not sure how a lot of python works but I am slowly learning. I fixed the indentation issues and then replaced the operator code to --> context.scene.my_tool.my_bool and that solved my issues. Can't thank you enough. I've been going through these forums for days haha. Cheers from LA! And yes I actually am messily combining two scripts in the registry area. I have a lot to clean up. $\endgroup$ – Brad Hamilton Sep 26 '18 at 6:02

Your Answer

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

Browse other questions tagged or ask your own question.