import bpy
import bmesh
from bpy.props import BoolProperty, FloatVectorProperty


def pyramid_values():
    """
    This function takes inputs and returns vertex and face arrays.
    no actual mesh data creation is done here.
    """
    verts = [(-1, +1, 0),
             (+1, +1, 0),
             (+1, -1, 0),
             (-1, -1, 0),
             (0, 0, +2)]

    faces = [(0, 1, 2, 3),
             (0, 1, 4),
             (1, 2, 4),
             (2, 3, 4),
             (3, 0, 4)]

    return verts, faces


class AddPyramid(bpy.types.Operator):
    """Add a simple pyramid mesh"""

    bl_idname = "mesh.primitive_pyramid_add"
    bl_label = "Add Pyramid"
    bl_options = {'REGISTER', 'UNDO'}

    # generic transform props
    view_align = BoolProperty(name="Align to View",
                              default=False)
    location = FloatVectorProperty(name="Location",
                                   subtype='TRANSLATION')
    rotation = FloatVectorProperty(name="Rotation",
                                   subtype='EULER')
    def __init__(self):
        self.location = (0, 0, 0)
        
    def execute(self, context):

        verts_loc, faces = pyramid_values()
        mesh = bpy.data.meshes.new("Pyramid")
        bm = bmesh.new()

        for v_co in verts_loc:
            bm.verts.new(v_co)

        for f_idx in faces:
            bm.faces.new([bm.verts[i] for i in f_idx])

        bm.to_mesh(mesh)
        mesh.update()
        

        # neues Objekt in die Szene setzen
        from bpy_extras import object_utils
        object_utils.object_data_add(context, mesh, operator=self)

        return {'FINISHED'}


def register():
    bpy.utils.register_class(AddPyramid)


def unregister():
    bpy.utils.unregister_class(AddPyramid)

if __name__ == "__main__":
    register()
    # create a instanz of a pyramid
    bpy.ops.mesh.primitive_pyramid_add()