2011-01-12 17:29:54 +00:00
|
|
|
import bpy
|
2012-03-30 05:43:33 +00:00
|
|
|
import bmesh
|
2011-01-12 17:29:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_box(width, height, depth):
|
|
|
|
"""
|
|
|
|
This function takes inputs and returns vertex and face arrays.
|
|
|
|
no actual mesh data creation is done here.
|
|
|
|
"""
|
|
|
|
|
2012-03-30 05:43:33 +00:00
|
|
|
verts = [(+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),
|
2011-01-12 17:29:54 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# apply size
|
2012-03-30 05:43:33 +00:00
|
|
|
for i, v in enumerate(verts):
|
|
|
|
verts[i] = v[0] * width, v[1] * depth, v[2] * height
|
2011-01-12 17:29:54 +00:00
|
|
|
|
2012-03-30 05:43:33 +00:00
|
|
|
return verts, faces
|
2011-01-12 17:29:54 +00:00
|
|
|
|
|
|
|
|
2011-02-27 15:25:24 +00:00
|
|
|
from bpy.props import FloatProperty, BoolProperty, FloatVectorProperty
|
2011-01-12 17:29:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AddBox(bpy.types.Operator):
|
2012-07-01 07:55:44 +00:00
|
|
|
"""Add a simple box mesh"""
|
2011-01-12 17:29:54 +00:00
|
|
|
bl_idname = "mesh.primitive_box_add"
|
|
|
|
bl_label = "Add Box"
|
|
|
|
bl_options = {'REGISTER', 'UNDO'}
|
|
|
|
|
2011-08-19 19:25:20 +00:00
|
|
|
width = FloatProperty(
|
|
|
|
name="Width",
|
2011-01-12 17:29:54 +00:00
|
|
|
description="Box Width",
|
2011-08-19 19:25:20 +00:00
|
|
|
min=0.01, max=100.0,
|
|
|
|
default=1.0,
|
|
|
|
)
|
|
|
|
height = FloatProperty(
|
|
|
|
name="Height",
|
2011-01-12 17:29:54 +00:00
|
|
|
description="Box Height",
|
2011-08-19 19:25:20 +00:00
|
|
|
min=0.01, max=100.0,
|
|
|
|
default=1.0,
|
|
|
|
)
|
|
|
|
depth = FloatProperty(
|
|
|
|
name="Depth",
|
2011-01-12 17:29:54 +00:00
|
|
|
description="Box Depth",
|
2011-08-19 19:25:20 +00:00
|
|
|
min=0.01, max=100.0,
|
|
|
|
default=1.0,
|
|
|
|
)
|
2011-01-12 17:29:54 +00:00
|
|
|
|
|
|
|
# generic transform props
|
2011-08-19 19:25:20 +00:00
|
|
|
view_align = BoolProperty(
|
|
|
|
name="Align to View",
|
|
|
|
default=False,
|
|
|
|
)
|
|
|
|
location = FloatVectorProperty(
|
|
|
|
name="Location",
|
|
|
|
subtype='TRANSLATION',
|
|
|
|
)
|
|
|
|
rotation = FloatVectorProperty(
|
|
|
|
name="Rotation",
|
|
|
|
subtype='EULER',
|
|
|
|
)
|
2011-01-12 17:29:54 +00:00
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
|
|
|
|
verts_loc, faces = add_box(self.width,
|
|
|
|
self.height,
|
|
|
|
self.depth,
|
|
|
|
)
|
|
|
|
|
|
|
|
mesh = bpy.data.meshes.new("Box")
|
|
|
|
|
2012-03-30 05:43:33 +00:00
|
|
|
bm = bmesh.new()
|
2011-01-12 17:29:54 +00:00
|
|
|
|
2012-03-30 05:43:33 +00:00
|
|
|
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)
|
2011-02-08 21:32:26 +00:00
|
|
|
mesh.update()
|
2011-01-12 17:29:54 +00:00
|
|
|
|
|
|
|
# add the mesh as an object into the scene with this utility module
|
2011-05-16 07:51:02 +00:00
|
|
|
from bpy_extras import object_utils
|
|
|
|
object_utils.object_data_add(context, mesh, operator=self)
|
2011-01-12 17:29:54 +00:00
|
|
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
|
|
def menu_func(self, context):
|
|
|
|
self.layout.operator(AddBox.bl_idname, icon='MESH_CUBE')
|
|
|
|
|
|
|
|
|
|
|
|
def register():
|
2011-02-12 08:04:32 +00:00
|
|
|
bpy.utils.register_class(AddBox)
|
2011-01-12 17:29:54 +00:00
|
|
|
bpy.types.INFO_MT_mesh_add.append(menu_func)
|
|
|
|
|
|
|
|
|
|
|
|
def unregister():
|
2011-02-12 08:04:32 +00:00
|
|
|
bpy.utils.unregister_class(AddBox)
|
2011-01-12 17:29:54 +00:00
|
|
|
bpy.types.INFO_MT_mesh_add.remove(menu_func)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2011-02-12 08:04:32 +00:00
|
|
|
register()
|
|
|
|
|
|
|
|
# test call
|
2011-01-12 17:29:54 +00:00
|
|
|
bpy.ops.mesh.primitive_box_add()
|