1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Original by Fourmadmen
6 from mathutils
import (
11 from bpy
.props
import (
17 from bpy_extras
import object_utils
19 # Create a new mesh (object) from verts/edges/faces.
20 # verts/edges/faces ... List of vertices/edges/faces for the
21 # new mesh (as used in from_pydata)
22 # name ... Name of the new mesh (& object)
24 def create_mesh_object(context
, verts
, edges
, faces
, name
):
27 mesh
= bpy
.data
.meshes
.new(name
)
29 # Make a mesh from a list of verts/edges/faces.
30 mesh
.from_pydata(verts
, edges
, faces
)
32 # Update mesh geometry after adding stuff.
35 from bpy_extras
import object_utils
36 return object_utils
.object_data_add(context
, mesh
, operator
=None)
39 # A very simple "bridge" tool.
41 def createFaces(vertIdx1
, vertIdx2
, closed
=False, flipped
=False):
44 if not vertIdx1
or not vertIdx2
:
47 if len(vertIdx1
) < 2 and len(vertIdx2
) < 2:
51 if (len(vertIdx1
) != len(vertIdx2
)):
52 if (len(vertIdx1
) == 1 and len(vertIdx2
) > 1):
60 # Bridge the start with the end.
67 face
.append(vertIdx1
[total
- 1])
71 face
= [vertIdx2
[0], vertIdx1
[0]]
73 face
.append(vertIdx1
[total
- 1])
74 face
.append(vertIdx2
[total
- 1])
77 # Bridge the rest of the faces.
78 for num
in range(total
- 1):
81 face
= [vertIdx2
[num
], vertIdx1
[0], vertIdx2
[num
+ 1]]
83 face
= [vertIdx2
[num
], vertIdx1
[num
],
84 vertIdx1
[num
+ 1], vertIdx2
[num
+ 1]]
88 face
= [vertIdx1
[0], vertIdx2
[num
], vertIdx2
[num
+ 1]]
90 face
= [vertIdx1
[num
], vertIdx2
[num
],
91 vertIdx2
[num
+ 1], vertIdx1
[num
+ 1]]
97 # @todo Clean up vertex&face creation process a bit.
99 def add_star(points
, outer_radius
, inner_radius
, height
):
106 segments
= points
* 2
108 half_height
= height
/ 2.0
110 vert_idx_top
= len(verts
)
111 verts
.append(Vector((0.0, 0.0, half_height
)))
113 vert_idx_bottom
= len(verts
)
114 verts
.append(Vector((0.0, 0.0, -half_height
)))
119 for index
in range(segments
):
120 quat
= Quaternion(z_axis
, (index
/ segments
) * PI_2
)
124 radius
= outer_radius
127 radius
= inner_radius
129 edgeloop_top
.append(len(verts
))
130 vec
= quat
@ Vector((radius
, 0, half_height
))
133 edgeloop_bottom
.append(len(verts
))
134 vec
= quat
@ Vector((radius
, 0, -half_height
))
137 faces_top
= createFaces([vert_idx_top
], edgeloop_top
, closed
=True)
138 faces_outside
= createFaces(edgeloop_top
, edgeloop_bottom
, closed
=True)
139 faces_bottom
= createFaces([vert_idx_bottom
], edgeloop_bottom
,
140 flipped
=True, closed
=True)
142 faces
.extend(faces_top
)
143 faces
.extend(faces_outside
)
144 faces
.extend(faces_bottom
)
149 class AddStar(bpy
.types
.Operator
, object_utils
.AddObjectHelper
):
150 bl_idname
= "mesh.primitive_star_add"
151 bl_label
= "Simple Star"
152 bl_description
= "Construct a star mesh"
153 bl_options
= {'REGISTER', 'UNDO', 'PRESET'}
155 Star
: BoolProperty(name
= "Star",
157 description
= "Star")
158 change
: BoolProperty(name
= "Change",
160 description
= "change Star")
164 description
="Number of points for the star",
169 outer_radius
: FloatProperty(
171 description
="Outer radius of the star",
176 innter_radius
: FloatProperty(
178 description
="Inner radius of the star",
183 height
: FloatProperty(name
="Height",
184 description
="Height of the star",
190 def draw(self
, context
):
193 layout
.prop(self
, 'points', expand
=True)
194 layout
.prop(self
, 'outer_radius', expand
=True)
195 layout
.prop(self
, 'innter_radius', expand
=True)
196 layout
.prop(self
, 'height', expand
=True)
198 if self
.change
== False:
199 col
= layout
.column(align
=True)
200 col
.prop(self
, 'align', expand
=True)
201 col
= layout
.column(align
=True)
202 col
.prop(self
, 'location', expand
=True)
203 col
= layout
.column(align
=True)
204 col
.prop(self
, 'rotation', expand
=True)
206 def execute(self
, context
):
207 # turn off 'Enter Edit Mode'
208 use_enter_edit_mode
= bpy
.context
.preferences
.edit
.use_enter_edit_mode
209 bpy
.context
.preferences
.edit
.use_enter_edit_mode
= False
211 if bpy
.context
.mode
== "OBJECT":
212 if context
.selected_objects
!= [] and context
.active_object
and \
213 (context
.active_object
.data
is not None) and ('Star' in context
.active_object
.data
.keys()) and \
214 (self
.change
== True):
215 obj
= context
.active_object
217 oldmeshname
= obj
.data
.name
218 verts
, faces
= add_star(
224 mesh
= bpy
.data
.meshes
.new('Star')
225 mesh
.from_pydata(verts
, [], faces
)
227 for material
in oldmesh
.materials
:
228 obj
.data
.materials
.append(material
)
229 bpy
.data
.meshes
.remove(oldmesh
)
230 obj
.data
.name
= oldmeshname
232 verts
, faces
= add_star(
238 mesh
= bpy
.data
.meshes
.new('Star')
239 mesh
.from_pydata(verts
, [], faces
)
240 obj
= object_utils
.object_data_add(context
, mesh
, operator
=self
)
242 obj
.data
["Star"] = True
243 obj
.data
["change"] = False
244 for prm
in StarParameters():
245 obj
.data
[prm
] = getattr(self
, prm
)
247 if bpy
.context
.mode
== "EDIT_MESH":
248 active_object
= context
.active_object
249 name_active_object
= active_object
.name
250 bpy
.ops
.object.mode_set(mode
='OBJECT')
251 verts
, faces
= add_star(
257 mesh
= bpy
.data
.meshes
.new('Star')
258 mesh
.from_pydata(verts
, [], faces
)
259 obj
= object_utils
.object_data_add(context
, mesh
, operator
=self
)
261 active_object
.select_set(True)
262 bpy
.context
.view_layer
.objects
.active
= active_object
263 bpy
.ops
.object.join()
264 context
.active_object
.name
= name_active_object
265 bpy
.ops
.object.mode_set(mode
='EDIT')
267 if use_enter_edit_mode
:
268 bpy
.ops
.object.mode_set(mode
= 'EDIT')
270 # restore pre operator state
271 bpy
.context
.preferences
.edit
.use_enter_edit_mode
= use_enter_edit_mode
275 def StarParameters():
282 return StarParameters