Fix T104270: Error in materials_utils "unregister" function
[blender-addons.git] / add_mesh_extra_objects / add_mesh_gemstones.py
blobf30911c54ad78db6743e59360d668184e5cb6bde
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Author: Pontiac, Fourmadmen, Dreampainter
5 import bpy
6 from bpy.types import Operator
7 from mathutils import (
8 Vector,
9 Quaternion,
11 from math import cos, sin, pi
12 from bpy.props import (
13 FloatProperty,
14 IntProperty,
15 BoolProperty,
16 StringProperty,
18 from bpy_extras import object_utils
20 # Create a new mesh (object) from verts/edges/faces.
21 # verts/edges/faces ... List of vertices/edges/faces for the
22 # new mesh (as used in from_pydata)
23 # name ... Name of the new mesh (& object)
25 def create_mesh_object(context, self, verts, edges, faces, name):
27 # Create new mesh
28 mesh = bpy.data.meshes.new(name)
30 # Make a mesh from a list of verts/edges/faces.
31 mesh.from_pydata(verts, edges, faces)
33 # Update mesh geometry after adding stuff.
34 mesh.update()
36 from bpy_extras import object_utils
37 return object_utils.object_data_add(context, mesh, operator=self)
40 # A very simple "bridge" tool.
42 def createFaces(vertIdx1, vertIdx2, closed=False, flipped=False):
43 faces = []
45 if not vertIdx1 or not vertIdx2:
46 return None
48 if len(vertIdx1) < 2 and len(vertIdx2) < 2:
49 return None
51 fan = False
52 if (len(vertIdx1) != len(vertIdx2)):
53 if (len(vertIdx1) == 1 and len(vertIdx2) > 1):
54 fan = True
55 else:
56 return None
58 total = len(vertIdx2)
60 if closed:
61 # Bridge the start with the end
62 if flipped:
63 face = [
64 vertIdx1[0],
65 vertIdx2[0],
66 vertIdx2[total - 1]]
67 if not fan:
68 face.append(vertIdx1[total - 1])
69 faces.append(face)
71 else:
72 face = [vertIdx2[0], vertIdx1[0]]
73 if not fan:
74 face.append(vertIdx1[total - 1])
75 face.append(vertIdx2[total - 1])
76 faces.append(face)
78 # Bridge the rest of the faces
79 for num in range(total - 1):
80 if flipped:
81 if fan:
82 face = [vertIdx2[num], vertIdx1[0], vertIdx2[num + 1]]
83 else:
84 face = [vertIdx2[num], vertIdx1[num],
85 vertIdx1[num + 1], vertIdx2[num + 1]]
86 faces.append(face)
87 else:
88 if fan:
89 face = [vertIdx1[0], vertIdx2[num], vertIdx2[num + 1]]
90 else:
91 face = [vertIdx1[num], vertIdx2[num],
92 vertIdx2[num + 1], vertIdx1[num + 1]]
93 faces.append(face)
95 return faces
98 # @todo Clean up vertex&face creation process a bit.
99 def add_gem(r1, r2, seg, h1, h2):
101 r1 = pavilion radius
102 r2 = crown radius
103 seg = number of segments
104 h1 = pavilion height
105 h2 = crown height
106 Generates the vertices and faces of the gem
109 verts = []
111 a = 2.0 * pi / seg # Angle between segments
112 offset = a / 2.0 # Middle between segments
114 r3 = ((r1 + r2) / 2.0) / cos(offset) # Middle of crown
115 r4 = (r1 / 2.0) / cos(offset) # Middle of pavilion
116 h3 = h2 / 2.0 # Middle of crown height
117 h4 = -h1 / 2.0 # Middle of pavilion height
119 # Tip
120 vert_tip = len(verts)
121 verts.append(Vector((0.0, 0.0, -h1)))
123 # Middle vertex of the flat side (crown)
124 vert_flat = len(verts)
125 verts.append(Vector((0.0, 0.0, h2)))
127 edgeloop_flat = []
128 for i in range(seg):
129 s1 = sin(i * a)
130 s2 = sin(offset + i * a)
131 c1 = cos(i * a)
132 c2 = cos(offset + i * a)
134 verts.append((r4 * s1, r4 * c1, h4)) # Middle of pavilion
135 verts.append((r1 * s2, r1 * c2, 0.0)) # Pavilion
136 verts.append((r3 * s1, r3 * c1, h3)) # Middle crown
137 edgeloop_flat.append(len(verts))
138 verts.append((r2 * s2, r2 * c2, h2)) # Crown
140 faces = []
142 for index in range(seg):
143 i = index * 4
144 j = ((index + 1) % seg) * 4
146 faces.append([j + 2, vert_tip, i + 2, i + 3]) # Tip -> Middle of pav
147 faces.append([j + 2, i + 3, j + 3]) # Middle of pav -> pav
148 faces.append([j + 3, i + 3, j + 4]) # Pav -> Middle crown
149 faces.append([j + 4, i + 3, i + 4, i + 5]) # Crown quads
150 faces.append([j + 4, i + 5, j + 5]) # Middle crown -> crown
152 faces_flat = createFaces([vert_flat], edgeloop_flat, closed=True, flipped=True)
153 faces.extend(faces_flat)
155 return verts, faces
158 def add_diamond(segments, girdle_radius, table_radius,
159 crown_height, pavilion_height):
161 PI_2 = pi * 2.0
162 z_axis = (0.0, 0.0, -1.0)
164 verts = []
165 faces = []
167 height_flat = crown_height
168 height_middle = 0.0
169 height_tip = -pavilion_height
171 # Middle vertex of the flat side (crown)
172 vert_flat = len(verts)
173 verts.append(Vector((0.0, 0.0, height_flat)))
175 # Tip
176 vert_tip = len(verts)
177 verts.append(Vector((0.0, 0.0, height_tip)))
179 verts_flat = []
180 verts_girdle = []
182 for index in range(segments):
183 quat = Quaternion(z_axis, (index / segments) * PI_2)
185 # angle = PI_2 * index / segments # UNUSED
187 # Row for flat side
188 verts_flat.append(len(verts))
189 vec = quat @ Vector((table_radius, 0.0, height_flat))
190 verts.append(vec)
192 # Row for the middle/girdle
193 verts_girdle.append(len(verts))
194 vec = quat @ Vector((girdle_radius, 0.0, height_middle))
195 verts.append(vec)
197 # Flat face
198 faces_flat = createFaces([vert_flat], verts_flat, closed=True,
199 flipped=True)
200 # Side face
201 faces_side = createFaces(verts_girdle, verts_flat, closed=True)
202 # Tip faces
203 faces_tip = createFaces([vert_tip], verts_girdle, closed=True)
205 faces.extend(faces_tip)
206 faces.extend(faces_side)
207 faces.extend(faces_flat)
209 return verts, faces
212 class AddDiamond(Operator, object_utils.AddObjectHelper):
213 bl_idname = "mesh.primitive_diamond_add"
214 bl_label = "Add Diamond"
215 bl_description = "Construct a diamond mesh"
216 bl_options = {'REGISTER', 'UNDO', 'PRESET'}
218 Diamond : BoolProperty(name = "Diamond",
219 default = True,
220 description = "Diamond")
222 #### change properties
223 name : StringProperty(name = "Name",
224 description = "Name")
226 change : BoolProperty(name = "Change",
227 default = False,
228 description = "change Diamond")
230 segments: IntProperty(
231 name="Segments",
232 description="Number of segments for the diamond",
233 min=3,
234 max=256,
235 default=32
237 girdle_radius: FloatProperty(
238 name="Girdle Radius",
239 description="Girdle radius of the diamond",
240 min=0.01,
241 max=9999.0,
242 default=1.0
244 table_radius: FloatProperty(
245 name="Table Radius",
246 description="Girdle radius of the diamond",
247 min=0.01,
248 max=9999.0,
249 default=0.6
251 crown_height: FloatProperty(
252 name="Crown Height",
253 description="Crown height of the diamond",
254 min=0.01,
255 max=9999.0,
256 default=0.35
258 pavilion_height: FloatProperty(
259 name="Pavilion Height",
260 description="Pavilion height of the diamond",
261 min=0.01,
262 max=9999.0,
263 default=0.8
266 def draw(self, context):
267 layout = self.layout
268 box = layout.box()
269 box.prop(self, "segments")
270 box.prop(self, "girdle_radius")
271 box.prop(self, "table_radius")
272 box.prop(self, "crown_height")
273 box.prop(self, "pavilion_height")
275 if self.change == False:
276 # generic transform props
277 box = layout.box()
278 box.prop(self, 'align', expand=True)
279 box.prop(self, 'location', expand=True)
280 box.prop(self, 'rotation', expand=True)
282 def execute(self, context):
283 # turn off 'Enter Edit Mode'
284 use_enter_edit_mode = bpy.context.preferences.edit.use_enter_edit_mode
285 bpy.context.preferences.edit.use_enter_edit_mode = False
287 if bpy.context.mode == "OBJECT":
288 if context.selected_objects != [] and context.active_object and \
289 (context.active_object.data is not None) and ('Diamond' in context.active_object.data.keys()) and \
290 (self.change == True):
291 obj = context.active_object
292 oldmesh = obj.data
293 oldmeshname = obj.data.name
295 verts, faces = add_diamond(self.segments,
296 self.girdle_radius,
297 self.table_radius,
298 self.crown_height,
299 self.pavilion_height)
300 mesh = bpy.data.meshes.new("TMP")
301 mesh.from_pydata(verts, [], faces)
302 mesh.update()
303 obj.data = mesh
305 for material in oldmesh.materials:
306 obj.data.materials.append(material)
308 bpy.data.meshes.remove(oldmesh)
309 obj.data.name = oldmeshname
310 else:
311 verts, faces = add_diamond(self.segments,
312 self.girdle_radius,
313 self.table_radius,
314 self.crown_height,
315 self.pavilion_height)
317 obj = create_mesh_object(context, self, verts, [], faces, "Diamond")
319 obj.data["Diamond"] = True
320 obj.data["change"] = False
321 for prm in DiamondParameters():
322 obj.data[prm] = getattr(self, prm)
324 if bpy.context.mode == "EDIT_MESH":
325 active_object = context.active_object
326 name_active_object = active_object.name
327 bpy.ops.object.mode_set(mode='OBJECT')
328 verts, faces = add_diamond(self.segments,
329 self.girdle_radius,
330 self.table_radius,
331 self.crown_height,
332 self.pavilion_height)
334 obj = create_mesh_object(context, self, verts, [], faces, "TMP")
336 obj.select_set(True)
337 active_object.select_set(True)
338 bpy.context.view_layer.objects.active = active_object
339 bpy.ops.object.join()
340 context.active_object.name = name_active_object
341 bpy.ops.object.mode_set(mode='EDIT')
343 if use_enter_edit_mode:
344 bpy.ops.object.mode_set(mode = 'EDIT')
346 # restore pre operator state
347 bpy.context.preferences.edit.use_enter_edit_mode = use_enter_edit_mode
349 return {'FINISHED'}
351 def DiamondParameters():
352 DiamondParameters = [
353 "segments",
354 "girdle_radius",
355 "table_radius",
356 "crown_height",
357 "pavilion_height",
359 return DiamondParameters
362 class AddGem(Operator, object_utils.AddObjectHelper):
363 bl_idname = "mesh.primitive_gem_add"
364 bl_label = "Add Gem"
365 bl_description = "Construct an offset faceted gem mesh"
366 bl_options = {'REGISTER', 'UNDO', 'PRESET'}
368 Gem : BoolProperty(name = "Gem",
369 default = True,
370 description = "Gem")
372 #### change properties
373 name : StringProperty(name = "Name",
374 description = "Name")
376 change : BoolProperty(name = "Change",
377 default = False,
378 description = "change Gem")
380 segments: IntProperty(
381 name="Segments",
382 description="Longitudial segmentation",
383 min=3,
384 max=265,
385 default=8
387 pavilion_radius: FloatProperty(
388 name="Radius",
389 description="Radius of the gem",
390 min=0.01,
391 max=9999.0,
392 default=1.0
394 crown_radius: FloatProperty(
395 name="Table Radius",
396 description="Radius of the table(top)",
397 min=0.01,
398 max=9999.0,
399 default=0.6
401 crown_height: FloatProperty(
402 name="Table height",
403 description="Height of the top half",
404 min=0.01,
405 max=9999.0,
406 default=0.35
408 pavilion_height: FloatProperty(
409 name="Pavilion height",
410 description="Height of bottom half",
411 min=0.01,
412 max=9999.0,
413 default=0.8
416 def draw(self, context):
417 layout = self.layout
418 box = layout.box()
419 box.prop(self, "segments")
420 box.prop(self, "pavilion_radius")
421 box.prop(self, "crown_radius")
422 box.prop(self, "crown_height")
423 box.prop(self, "pavilion_height")
425 if self.change == False:
426 # generic transform props
427 box = layout.box()
428 box.prop(self, 'align', expand=True)
429 box.prop(self, 'location', expand=True)
430 box.prop(self, 'rotation', expand=True)
432 def execute(self, context):
433 # turn off 'Enter Edit Mode'
434 use_enter_edit_mode = bpy.context.preferences.edit.use_enter_edit_mode
435 bpy.context.preferences.edit.use_enter_edit_mode = False
437 if bpy.context.mode == "OBJECT":
438 if context.selected_objects != [] and context.active_object and \
439 (context.active_object.data is not None) and ('Gem' in context.active_object.data.keys()) and \
440 (self.change == True):
441 obj = context.active_object
442 oldmesh = obj.data
443 oldmeshname = obj.data.name
444 verts, faces = add_gem(
445 self.pavilion_radius,
446 self.crown_radius,
447 self.segments,
448 self.pavilion_height,
449 self.crown_height)
450 mesh = bpy.data.meshes.new("TMP")
451 mesh.from_pydata(verts, [], faces)
452 mesh.update()
453 obj.data = mesh
454 for material in oldmesh.materials:
455 obj.data.materials.append(material)
456 bpy.data.meshes.remove(oldmesh)
457 obj.data.name = oldmeshname
458 else:
459 verts, faces = add_gem(
460 self.pavilion_radius,
461 self.crown_radius,
462 self.segments,
463 self.pavilion_height,
464 self.crown_height)
466 obj = create_mesh_object(context, self, verts, [], faces, "Gem")
468 obj.data["Gem"] = True
469 obj.data["change"] = False
470 for prm in GemParameters():
471 obj.data[prm] = getattr(self, prm)
473 if bpy.context.mode == "EDIT_MESH":
474 active_object = context.active_object
475 name_active_object = active_object.name
476 bpy.ops.object.mode_set(mode='OBJECT')
477 verts, faces = add_gem(
478 self.pavilion_radius,
479 self.crown_radius,
480 self.segments,
481 self.pavilion_height,
482 self.crown_height)
484 obj = create_mesh_object(context, self, verts, [], faces, "TMP")
486 obj.select_set(True)
487 active_object.select_set(True)
488 bpy.context.view_layer.objects.active = active_object
489 bpy.ops.object.join()
490 context.active_object.name = name_active_object
491 bpy.ops.object.mode_set(mode='EDIT')
493 if use_enter_edit_mode:
494 bpy.ops.object.mode_set(mode = 'EDIT')
496 # restore pre operator state
497 bpy.context.preferences.edit.use_enter_edit_mode = use_enter_edit_mode
499 return {'FINISHED'}
501 def GemParameters():
502 GemParameters = [
503 "segments",
504 "pavilion_radius",
505 "crown_radius",
506 "crown_height",
507 "pavilion_height",
509 return GemParameters