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.

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. Changeselftocontext.scene.my_bool– batFINGER Sep 26 '18 at 02:36scene.my_tool.my_bool– batFINGER Sep 26 '18 at 03:44