1
$\begingroup$

I followed the recommendation of How to dynamically show/hide panel elements using python? for an addon and it works fine during script development. But when I install the addon and use the panel an EnumPropery which defines the layout seems to have lost the default value. The code is e.g.

class ABCK_Panel_with_sectors(bpy.types.Panel):

    bl_space_type = "VIEW_3D" # window type panel is displayed in
    bl_region_type = "TOOLS" # region of window panel is displayed in
    bl_label = "Create ABCK objects in sectors 03" # heading of panel

    def draw(self, context) :

        layout = self.layout
        scene = context.scene
        col = layout.column()
        #.........................................................
        box = layout.box()
        box.label("General definitions")
        row = box.row()
        row.prop(scene, 'Object_type')
        if scene['Object_type'] == 2: #!!!!!!!!!!!!!!!!!!!here is the problem
            row = box.row()
            row.prop(scene, 'with_bevels')
            row.prop(scene, 'with_colors')
        #.........................................................
        layout.label("")
        box = layout.box()
        box.label("Math only, no Blender objects")
        box.prop(scene, "Generations")
        box.prop(scene, "all_sectors")
        if scene['all_sectors'] == False:
            box.prop(scene, "Selection_of_sectors")
        row = box.row()  
        row.operator("compute.objects", text = "compute")
        row.operator("print_number_of.objects_sectors", \
            text = "print count math")
        #.........................................................
        layout.label("")
        box = layout.box()
        box.label("Final creation")

        row = box.row()
        row.prop(scene, 'Border_type')
        col = box.column()
        if scene['Border_type'] == Border_sphere:
            col.prop(scene, 'Size_max')
            col.prop(scene, 'Size_min')
        else:
            #col.operator('select.object_for_tesselation')
            col.label("select object for tesselation") 
            col.label("and press 'create objects'")
        row = box.row()
        row.operator("display_all.objects_sectors", \
            text = "create objects")
        if scene['Border_type'] == Border_sphere:
            row.operator("print_number_of.objects_sectors", \
                text = "print count tetrahedrons")
        #.........................................................
        layout.label("")
        row = layout.row()
        box = row.box()
        box.label("Other")
        box = box.row()
        box.operator("delete.objects", text = "delete")
        box.operator("toogle_select_all_visible.objects", \
            text = "toggle selected objects")

        layout.label("")

        row = layout.row()
        tets_on_layer_0 = [ob for ob in bpy.context.scene.objects \
            if (ob.layers[0] and ob.name.startswith("T_"))]
        if len(tets_on_layer_0) == 1:
            row.label(text="1 object generated.", icon='OBJECT_DATA')
        else:
            row.label(text=str((len(tets_on_layer_0))) + \
            " objects generated.", icon='OBJECT_DATA')   

and I get a 'KeyError':

'bpy_struct[key]: key "Object_type" not found.'

What can be done here?

$\endgroup$
2
  • 2
    $\begingroup$ More code would be helpful, but I would suggest 'if context.scene.object_type == 2:' or whatever you used when you created the property. $\endgroup$
    – cmomoney
    Commented Feb 3, 2016 at 17:31
  • $\begingroup$ this did not help, now I have shown more code $\endgroup$ Commented Feb 3, 2016 at 19:53

1 Answer 1

3
$\begingroup$

scene['Object_type'] will refer to a custom property you have added to a scene either by using the Custom Properties panel in the scene properties or by blender automatically creating it as you used scene['Object_type'] = 2 in your script or the python console.

The proper way to create a new property for your addon to use is by using bpy.props.

In your addons register() function you define the new property then in unregister() you remove it. Within your panel you can then access it with scene.myprop

import bpy

class LayoutDemoPanel(bpy.types.Panel):
    """Creates a Panel in the 3D views toolbar region"""
    bl_label = 'Layout Demo'
    bl_idname = 'SCENE_PT_layout_demo'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'

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

        row = layout.row()
        row.label('testing')

        if scene.myprop == 2:
            row = layout.row()
            row.label('my property is 2')


def register():
    bpy.types.Scene.myprop = bpy.props.IntProperty(name="My Storage", default=2)
    bpy.utils.register_class(LayoutDemoPanel)

def unregister():
    bpy.utils.unregister_class(LayoutDemoPanel)
    del bpy.types.Scene.myprop

if __name__ == "__main__":
    register()
$\endgroup$

You must log in to answer this question.

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