1
$\begingroup$

Trying to write a beginner addon in 2.8 that takes an input row and column variable, and then creates a row xcolumn calendar. My code worked, but my addon kept disappearing after I press "x" to delete anything in the scene. I am guessing my addon panel is also part of the 3d viewport scene as well? Full code below, you can just load it into Blender and run it. The panel should pop up if you press "n" from the 3D viewport, and then you can click on tools to see it. Now if you do select all and press "x" you can also delete the panel with the default cubes.

 import bpy




bl_info = {
    "name": "QC AR Calendar",
    "description": "",
    "author": "Luke Chen",
    "version": (0, 0, 2),
    "blender": (2, 80, 0),
    "location": "3D View > Tools",
    "warning": "", # used for warning icon and text in addons panel
    "wiki_url": "",
    "tracker_url": "",
    "category": "Development"
}


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


# ------------------------------------------------------------------------
#    Scene Properties
# ------------------------------------------------------------------------

class Calendar_Properties(PropertyGroup):


    Row: IntProperty(
        name = "Calendar Rows", # We can use this for height and width input.
        description="How long do you want the calendar to be, default is 5.0 rows.",
        default = 5,
        min = 3,
        max = 100
        )

    Column: IntProperty(
        name = "Calendar columns", # We can use this for height and width input.
        description="How wide do you want the calendar to be, default is 7 columns. MON~SUN day",
        default = 7,
        min = 7,
        max = 100
        )


# ------------------------------------------------------------------------
#    Operators
# ------------------------------------------------------------------------

class WM_OT_Create_Calendar_Ops(Operator):
    bl_idname = "wm.create_calendar"
    bl_label = "Creates a 3D calender"

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

        # Takes inputed Row and Column value from Calendar_Properties class
        # Then creates a 3D calendar model

        print(mytool.Row * mytool.Column) # testing

        return {'FINISHED'}


# ------------------------------------------------------------------------
#    Panel in Object Mode
# ------------------------------------------------------------------------

class OBJECT_PT_Calendar_Panel(Panel):
    bl_idname = "object.custom_panel"
    bl_label = "3D Calendar Creator"
    bl_space_type = "VIEW_3D"   
    bl_region_type = "UI"
    bl_category = "Tools"
    bl_context = "objectmode"   


    @classmethod
    def poll(self,context):
        return context.object is not None

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

        layout.prop(mytool, "Row") # creates the row input box
        layout.prop(mytool, "Column") # creates the column input box
        layout.operator("wm.create_calendar") # Calls the operator
        layout.separator()

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

classes = (
    Calendar_Properties,
    WM_OT_Create_Calendar_Ops,
    OBJECT_PT_Calendar_Panel,
)

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

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

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()
$\endgroup$
2
$\begingroup$

The panels poll method determines when the panel is visible, in your example only when there is an active object. By removing this the panel will always be visible, or you can use a different test if you want to hide it under some conditions.

@classmethod
def poll(self,context):
    return context.object is not None
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ @Inkplay_ or with return True will emulate exactly the default poll method. (same result as deleting) Suggest return context.mode == 'OBJECT' $\endgroup$ – batFINGER Apr 15 '19 at 8:46
  • $\begingroup$ Thank you I really appreciate the answer. $\endgroup$ – Inkplay_ Apr 15 '19 at 20:32

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.