Class: Mesh II¶
Objectives¶
Now we will finish the pyramid class |
Instructions¶
Tasks: |
---|
- Repeat the sub-steps shown here to create a class until you have a working version.
- Create a class of its own »primitive«, eg. a wedge or the base form of a hex key.
- Add a material to the class, which is given to the new object.
Design data for a mesh¶
Define the tip of the pyramid and defining the faces, is the last step.
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
Defining the faces is done clockwise and remember the indes starts with 0.
The result¶
Now you are able to create as much pyramids as you like.
The final version of the class¶
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')
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()
self.location = (0, 0, 0)
# 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()