1 # GPL Original by Fourmadmen
4 from mathutils
import (
9 from bpy
.props
import (
15 # Create a new mesh (object) from verts/edges/faces.
16 # verts/edges/faces ... List of vertices/edges/faces for the
17 # new mesh (as used in from_pydata)
18 # name ... Name of the new mesh (& object)
20 def create_mesh_object(context
, verts
, edges
, faces
, name
):
23 mesh
= bpy
.data
.meshes
.new(name
)
25 # Make a mesh from a list of verts/edges/faces.
26 mesh
.from_pydata(verts
, edges
, faces
)
28 # Update mesh geometry after adding stuff.
31 from bpy_extras
import object_utils
32 return object_utils
.object_data_add(context
, mesh
, operator
=None)
35 # A very simple "bridge" tool.
37 def createFaces(vertIdx1
, vertIdx2
, closed
=False, flipped
=False):
40 if not vertIdx1
or not vertIdx2
:
43 if len(vertIdx1
) < 2 and len(vertIdx2
) < 2:
47 if (len(vertIdx1
) != len(vertIdx2
)):
48 if (len(vertIdx1
) == 1 and len(vertIdx2
) > 1):
56 # Bridge the start with the end.
63 face
.append(vertIdx1
[total
- 1])
67 face
= [vertIdx2
[0], vertIdx1
[0]]
69 face
.append(vertIdx1
[total
- 1])
70 face
.append(vertIdx2
[total
- 1])
73 # Bridge the rest of the faces.
74 for num
in range(total
- 1):
77 face
= [vertIdx2
[num
], vertIdx1
[0], vertIdx2
[num
+ 1]]
79 face
= [vertIdx2
[num
], vertIdx1
[num
],
80 vertIdx1
[num
+ 1], vertIdx2
[num
+ 1]]
84 face
= [vertIdx1
[0], vertIdx2
[num
], vertIdx2
[num
+ 1]]
86 face
= [vertIdx1
[num
], vertIdx2
[num
],
87 vertIdx2
[num
+ 1], vertIdx1
[num
+ 1]]
93 # @todo Clean up vertex&face creation process a bit.
95 def add_star(points
, outer_radius
, inner_radius
, height
):
102 segments
= points
* 2
104 half_height
= height
/ 2.0
106 vert_idx_top
= len(verts
)
107 verts
.append(Vector((0.0, 0.0, half_height
)))
109 vert_idx_bottom
= len(verts
)
110 verts
.append(Vector((0.0, 0.0, -half_height
)))
115 for index
in range(segments
):
116 quat
= Quaternion(z_axis
, (index
/ segments
) * PI_2
)
120 radius
= outer_radius
123 radius
= inner_radius
125 edgeloop_top
.append(len(verts
))
126 vec
= quat
@ Vector((radius
, 0, half_height
))
129 edgeloop_bottom
.append(len(verts
))
130 vec
= quat
@ Vector((radius
, 0, -half_height
))
133 faces_top
= createFaces([vert_idx_top
], edgeloop_top
, closed
=True)
134 faces_outside
= createFaces(edgeloop_top
, edgeloop_bottom
, closed
=True)
135 faces_bottom
= createFaces([vert_idx_bottom
], edgeloop_bottom
,
136 flipped
=True, closed
=True)
138 faces
.extend(faces_top
)
139 faces
.extend(faces_outside
)
140 faces
.extend(faces_bottom
)
145 class AddStar(bpy
.types
.Operator
):
146 bl_idname
= "mesh.primitive_star_add"
147 bl_label
= "Simple Star"
148 bl_description
= "Construct a star mesh"
149 bl_options
= {'REGISTER', 'UNDO', 'PRESET'}
153 description
="Number of points for the star",
158 outer_radius
: FloatProperty(
160 description
="Outer radius of the star",
165 innter_radius
: FloatProperty(
167 description
="Inner radius of the star",
172 height
: FloatProperty(name
="Height",
173 description
="Height of the star",
179 def execute(self
, context
):
181 verts
, faces
= add_star(
188 obj
= create_mesh_object(context
, verts
, [], faces
, "Star")