blender/release/scripts/templates_py/operator_mesh_add.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

127 lines
2.7 KiB
Python
Raw Normal View History

2011-01-12 17:29:54 +00:00
import bpy
import bmesh
from bpy_extras.object_utils import AddObjectHelper
2011-01-12 17:29:54 +00:00
2019-10-15 04:20:15 +00:00
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.
"""
2018-06-26 17:41:37 +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
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
return verts, faces
2011-01-12 17:29:54 +00:00
2016-03-21 16:51:48 +00:00
from bpy.props import (
BoolProperty,
BoolVectorProperty,
EnumProperty,
FloatProperty,
FloatVectorProperty,
)
2011-01-12 17:29:54 +00:00
class AddBox(bpy.types.Operator, AddObjectHelper):
"""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'}
width: FloatProperty(
2018-06-26 17:41:37 +00:00
name="Width",
description="Box Width",
min=0.01, max=100.0,
default=1.0,
)
height: FloatProperty(
2018-06-26 17:41:37 +00:00
name="Height",
description="Box Height",
min=0.01, max=100.0,
default=1.0,
)
depth: FloatProperty(
2018-06-26 17:41:37 +00:00
name="Depth",
description="Box Depth",
min=0.01, max=100.0,
default=1.0,
)
layers: BoolVectorProperty(
2018-06-26 17:41:37 +00:00
name="Layers",
description="Object Layers",
size=20,
options={'HIDDEN', 'SKIP_SAVE'},
)
2011-01-12 17:29:54 +00:00
def execute(self, context):
2018-06-26 17:41:37 +00:00
verts_loc, faces = add_box(
self.width,
self.height,
self.depth,
)
2011-01-12 17:29:54 +00:00
mesh = bpy.data.meshes.new("Box")
bm = bmesh.new()
2011-01-12 17:29:54 +00:00
for v_co in verts_loc:
bm.verts.new(v_co)
bm.verts.ensure_lookup_table()
for f_idx in faces:
bm.faces.new([bm.verts[i] for i in f_idx])
bm.to_mesh(mesh)
mesh.update()
2011-01-12 17:29:54 +00:00
# add the mesh as an object into the scene with this utility module
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():
bpy.utils.register_class(AddBox)
bpy.types.VIEW3D_MT_mesh_add.append(menu_func)
2011-01-12 17:29:54 +00:00
def unregister():
bpy.utils.unregister_class(AddBox)
bpy.types.VIEW3D_MT_mesh_add.remove(menu_func)
2011-01-12 17:29:54 +00:00
2018-06-26 17:41:37 +00:00
2011-01-12 17:29:54 +00:00
if __name__ == "__main__":
register()
# test call
2011-01-12 17:29:54 +00:00
bpy.ops.mesh.primitive_box_add()