operator-panel to panel


This script creates an operator and a very simple panel (implicit). What i want, is do have a panel which does the same as now but to create it explicitly so i can have it in the toolshelf or properties in the 3d-view. And i want it to be permanent, because the operators panel shows only once, when you call the operator. Any ideas or hints to achieve this?

edit: the important part is not to create a cube! the important part is to change the material of objects (the dropdown-menu).

import bpy

def add_box(width, height, depth):
    This function takes inputs and returns vertex and face arrays.
    no actual mesh data creation is done here.

    vertices = [1.0, 1.0, -1.0,
                1.0, -1.0, -1.0,
                -1.0, -1.0, -1.0,
                -1.0, 1.0, -1.0,
                1.0, 1.0, 1.0,
                1.0, -1.0, 1.0,
                -1.0, -1.0, 1.0,
                -1.0, 1.0, 1.0,

    faces = [0, 1, 2, 3,
             4, 7, 6, 5,
             0, 4, 5, 1,
             1, 5, 6, 2,
             2, 6, 7, 3,
             4, 0, 3, 7,

    # apply size
    for i in range(0, len(vertices), 3):
        vertices[i] *= width
        vertices[i + 1] *= depth
        vertices[i + 2] *= height

    return vertices, faces

from bpy.props import FloatProperty, BoolProperty, FloatVectorProperty, EnumProperty

class AddBox(bpy.types.Operator):
    '''Add a simple box mesh'''
    bl_idname = "mesh.primitive_box_add"
    bl_label = "Add Box"
    bl_options = {'REGISTER', 'UNDO'}

    itemlist = []
    for i in bpy.data.materials:
    itemlist = tuple(itemlist)
    color = EnumProperty(attr='attr', name = 'color', items=itemlist)
    # generic transform props
    view_align = BoolProperty(name="Align to View",
    location = FloatVectorProperty(name="Location",
    rotation = FloatVectorProperty(name="Rotation",

    def execute(self, context):
        verts_loc, faces = add_box(1,1,1)

        mesh = bpy.data.meshes.new("Box")

        mesh.vertices.add(len(verts_loc) // 3)
        mesh.faces.add(len(faces) // 4)

        mesh.vertices.foreach_set("co", verts_loc)
        mesh.faces.foreach_set("vertices_raw", faces)
        # add the mesh as an object into the scene with this utility module
        import add_object_utils
        add_object_utils.object_data_add(context, mesh, operator=self)
        return {'FINISHED'}

def menu_func(self, context):
    self.layout.operator(AddBox.bl_idname, icon='MESH_CUBE')

def register():

def unregister():

if __name__ == "__main__":

have a look at my gui post! the button gui is working and error free (section 3 works by itself) so you can adapt it by changing the function call (create(…)…) and erasing most of the slider buttons in the panel class. I meant the post to get artists using gui’s without problem

you mean the tree-gui post? and you’re talking about 2.49? but i want to code it in 2.5x!


the panel class is pretty much the same. I will be testing 2.5 this week, so you can help me get some guis finished after that if you are interested. (There should be an optimal writing to the panel gui)

i have already achieved what i wanted! i’m not sure if i posted it somewhere…

if you have some unresolved problems, just post and i’ll try to help.