4 def add_box(width, height, depth):
6 This function takes inputs and returns vertex and face arrays.
7 no actual mesh data creation is done here.
10 vertices = [1.0, 1.0, -1.0,
29 for i in range(0, len(vertices), 3):
31 vertices[i + 1] *= depth
32 vertices[i + 2] *= height
34 return vertices, faces
37 from bpy.props import *
40 class AddBox(bpy.types.Operator):
41 '''Add a simple box mesh'''
42 bl_idname = "mesh.primitive_box_add"
44 bl_options = {'REGISTER', 'UNDO'}
46 width = FloatProperty(name="Width",
47 description="Box Width",
48 default=1.0, min=0.01, max=100.0)
50 height = FloatProperty(name="Height",
51 description="Box Height",
52 default=1.0, min=0.01, max=100.0)
54 depth = FloatProperty(name="Depth",
55 description="Box Depth",
56 default=1.0, min=0.01, max=100.0)
58 # generic transform props
59 view_align = BoolProperty(name="Align to View",
61 location = FloatVectorProperty(name="Location",
62 subtype='TRANSLATION')
63 rotation = FloatVectorProperty(name="Rotation",
66 def execute(self, context):
68 verts_loc, faces = add_box(self.width,
73 mesh = bpy.data.meshes.new("Box")
75 mesh.vertices.add(len(verts_loc) // 3)
76 mesh.faces.add(len(faces) // 4)
78 mesh.vertices.foreach_set("co", verts_loc)
79 mesh.faces.foreach_set("vertices_raw", faces)
82 # add the mesh as an object into the scene with this utility module
83 import add_object_utils
84 add_object_utils.object_data_add(context, mesh, operator=self)
89 def menu_func(self, context):
90 self.layout.operator(AddBox.bl_idname, icon='MESH_CUBE')
94 bpy.utils.register_class(AddBox)
95 bpy.types.INFO_MT_mesh_add.append(menu_func)
99 bpy.utils.unregister_class(AddBox)
100 bpy.types.INFO_MT_mesh_add.remove(menu_func)
102 if __name__ == "__main__":
106 bpy.ops.mesh.primitive_box_add()