Merge branch 'blender-v2.81-release'
[blender-addons.git] / add_mesh_extra_objects / add_mesh_star.py
blob265f9a80ae8ee544ba6cf57c28af6c17c06d6141
1 # GPL Original by Fourmadmen
3 import bpy
4 from mathutils import (
5 Vector,
6 Quaternion,
8 from math import pi
9 from bpy.props import (
10 IntProperty,
11 FloatProperty,
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):
22 # Create new mesh
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.
29 mesh.update()
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):
38 faces = []
40 if not vertIdx1 or not vertIdx2:
41 return None
43 if len(vertIdx1) < 2 and len(vertIdx2) < 2:
44 return None
46 fan = False
47 if (len(vertIdx1) != len(vertIdx2)):
48 if (len(vertIdx1) == 1 and len(vertIdx2) > 1):
49 fan = True
50 else:
51 return None
53 total = len(vertIdx2)
55 if closed:
56 # Bridge the start with the end.
57 if flipped:
58 face = [
59 vertIdx1[0],
60 vertIdx2[0],
61 vertIdx2[total - 1]]
62 if not fan:
63 face.append(vertIdx1[total - 1])
64 faces.append(face)
66 else:
67 face = [vertIdx2[0], vertIdx1[0]]
68 if not fan:
69 face.append(vertIdx1[total - 1])
70 face.append(vertIdx2[total - 1])
71 faces.append(face)
73 # Bridge the rest of the faces.
74 for num in range(total - 1):
75 if flipped:
76 if fan:
77 face = [vertIdx2[num], vertIdx1[0], vertIdx2[num + 1]]
78 else:
79 face = [vertIdx2[num], vertIdx1[num],
80 vertIdx1[num + 1], vertIdx2[num + 1]]
81 faces.append(face)
82 else:
83 if fan:
84 face = [vertIdx1[0], vertIdx2[num], vertIdx2[num + 1]]
85 else:
86 face = [vertIdx1[num], vertIdx2[num],
87 vertIdx2[num + 1], vertIdx1[num + 1]]
88 faces.append(face)
90 return faces
93 # @todo Clean up vertex&face creation process a bit.
95 def add_star(points, outer_radius, inner_radius, height):
96 PI_2 = pi * 2
97 z_axis = (0, 0, 1)
99 verts = []
100 faces = []
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)))
112 edgeloop_top = []
113 edgeloop_bottom = []
115 for index in range(segments):
116 quat = Quaternion(z_axis, (index / segments) * PI_2)
118 if index % 2:
119 # Uneven
120 radius = outer_radius
121 else:
122 # Even
123 radius = inner_radius
125 edgeloop_top.append(len(verts))
126 vec = quat @ Vector((radius, 0, half_height))
127 verts.append(vec)
129 edgeloop_bottom.append(len(verts))
130 vec = quat @ Vector((radius, 0, -half_height))
131 verts.append(vec)
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)
142 return verts, faces
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'}
151 points: IntProperty(
152 name="Points",
153 description="Number of points for the star",
154 min=2,
155 max=256,
156 default=5
158 outer_radius: FloatProperty(
159 name="Outer Radius",
160 description="Outer radius of the star",
161 min=0.01,
162 max=9999.0,
163 default=1.0
165 innter_radius: FloatProperty(
166 name="Inner Radius",
167 description="Inner radius of the star",
168 min=0.01,
169 max=9999.0,
170 default=0.5
172 height: FloatProperty(name="Height",
173 description="Height of the star",
174 min=0.01,
175 max=9999.0,
176 default=0.5
179 def execute(self, context):
181 verts, faces = add_star(
182 self.points,
183 self.outer_radius,
184 self.innter_radius,
185 self.height
188 obj = create_mesh_object(context, verts, [], faces, "Star")
190 return {'FINISHED'}