Cleanup: remove "Tweak" event type
[blender-addons.git] / add_mesh_extra_objects / add_mesh_round_brilliant.py
blobb1a93012108b9ca1eee4a65f0a568fa1ae445d58
1 # SPDX-License-Identifier: GPL-2.0-or-later
2 # Author: Dominic Kröper, (dommetysk)
4 import bpy
5 from math import (
6 pi, sin,
7 cos, tan,
9 from bpy.types import Operator
10 from mathutils import (
11 Vector,
12 Euler,
14 from bpy.props import (
15 IntProperty,
16 FloatProperty,
17 BoolProperty,
18 StringProperty,
20 from bpy_extras import object_utils
22 # mesh generating function, returns mesh
23 def add_mesh_Brilliant(context, s, table_w, crown_h, girdle_t, pavi_d, bezel_f,
24 pavi_f, culet, girdle_real, keep_lga, g_real_smooth):
26 # # possible user inputs ( output 100% = 2 blender units )
27 # s # no. of girdle facets (steps) default: 16
28 # table_w # table width default: 0.530
29 # crown_h # crown height default: 0.162
30 # girdle_t # girdle thickness default: 0.017
31 # pavi_d # pavilion depth default: 0.431
32 # bezel_f # bezel factor default: 0.250
33 # pavi_f # pavilion factor default: 0.400
34 # culet # culet size default: 0.000
35 # girdle_real # type of girdle flat/real default: True
36 # g_real_smooth # smooth or flat shading default: False
37 # keep_lga # when culet > 0, keep lga default: False
39 # variables / shortcuts
40 if s % 2: # prevent odd number of steps (messes up mesh)
41 s = s - 1
42 if not girdle_real:
43 g_real_smooth = False
44 ang = 2 * pi / s # angle step size
45 Verts = [] # collect all vertices
46 Faces = [] # collect all faces
47 ca = cos(ang)
48 ca2 = cos(ang / 2)
49 sa4 = sin(ang / 4)
50 ta4 = tan(ang / 4)
51 ta8 = tan(ang / 8)
53 def fa(*vs): # shortcut Faces.append
54 v = []
55 for u in vs:
56 v.append(u)
57 Faces.append(v)
59 def va(vx, vz, iang, sang, n): # shortcut Verts.append
60 for i in range(n):
61 v = Vector((vx, 0, vz))
62 ai = sang + iang * i
63 E_rot = Euler((0, 0, ai), 'XYZ')
64 v.rotate(E_rot)
65 Verts.append((v.x, v.y, v.z))
67 # upper girdle angle
68 uga = (1 - bezel_f) * crown_h * 2 / (ca2 -
69 (table_w + (1 - table_w) * bezel_f) * ca2 / ca)
71 # lower girdle angle
72 if keep_lga:
73 if pavi_f > 0 and pavi_f < 1:
74 lga = (1 - pavi_f) * pavi_d * 2 / (ca2 - pavi_f * ca2 / ca)
75 elif pavi_f == 1:
76 lga = 0
77 else:
78 lga = 2 * pavi_d * ca
79 else:
80 lga = (1 - pavi_f) * pavi_d * 2 / (ca2 -
81 (culet + (1 - culet) * pavi_f) * ca2 / ca)
83 # append girdle vertices
84 va(1, 0, ang, 0, s)
85 va(1, 2 * girdle_t, ang, 0, s)
87 # append real girdle vertices
88 if girdle_real:
89 dnu = uga * (1 - ca2)
90 dfu = uga * (ta8 + ta4) * sa4
91 dnl = lga * (1 - ca2)
92 dfl = lga * (ta8 + ta4) * sa4
93 if abs(dnu) + abs(dnl) > 2 * girdle_t or dnu < 0 or dnl < 0:
94 girdle_real = False
95 else:
96 va(1, dnl, ang, ang / 2, s)
97 va(1, 2 * girdle_t - dnu, ang, ang / 2, s)
98 va(1, dfl, ang / 2, ang / 4, 2 * s)
99 va(1, 2 * girdle_t - dfu, ang / 2, ang / 4, 2 * s)
101 # make girdle faces
102 l1 = len(Verts) # 2*s / 8*s
103 for i in range(l1):
104 if girdle_real:
105 if i < s:
106 fa(i, i + s, 2 * i + 6 * s, 2 * i + 4 * s)
107 if i == 0:
108 fa(i, s, l1 - 1, 6 * s - 1)
109 else:
110 fa(i, i + s, 2 * i + 6 * s - 1, 2 * i + 4 * s - 1)
111 elif i > 2 * s - 1 and i < 3 * s:
112 fa(i, i + s, 2 * (i + s), 2 * i)
113 fa(i, i + s, 2 * (i + s) + 1, 2 * i + 1)
114 else:
115 if i < s - 1:
116 fa(i, i + s, i + s + 1, i + 1)
117 elif i == s - 1:
118 fa(i, i + s, s, 0)
120 # append upper girdle facet vertices
121 va((table_w + (1 - table_w) * bezel_f) / ca, (1 - bezel_f) * 2 * crown_h +
122 2 * girdle_t, 2 * ang, ang, int(s / 2))
124 # make upper girdle facet faces
125 l2 = len(Verts) # 2.5*s / 8.5*s
126 for i in range(l2):
127 if i > s and i < 2 * s - 1 and i % 2 != 0:
128 if girdle_real:
129 fa(i, 2 * (i + 2 * s), i + 2 * s, 2 * (i + 2 * s) + 1, i + 1,
130 int(7.5 * s) + int((i - 1) / 2))
131 fa(i, 2 * (i + 2 * s) - 1, i + 2 * s - 1, 2 * (i + 2 * s - 1),
132 i - 1, int(7.5 * s) + int((i - 1) / 2))
133 else:
134 fa(i, i + 1, int((i + 3 * s) / 2))
135 fa(i, i - 1, int((i + 3 * s) / 2))
136 elif i == s:
137 if girdle_real:
138 fa(i, l1 - 1, 4 * s - 1, l1 - 2, 2 * i - 1, l2 - 1)
139 fa(2 * i - 2, l1 - 4, 4 * s - 2, l1 - 3, 2 * i - 1, l2 - 1)
140 else:
141 fa(i, 2 * i - 1, l2 - 1)
142 fa(2 * i - 1, 2 * i - 2, l2 - 1)
144 # append table vertices
145 va(table_w, (crown_h + girdle_t) * 2, 2 * ang, 0, int(s / 2))
147 # make bezel facet faces and star facet faces
148 l3 = len(Verts) # 3*s / 9*s
149 for i in range(l3):
150 if i > l2 - 1 and i < l3 - 1:
151 fa(i, i + 1, i - int(s / 2))
152 fa(i + 1, i - int(s / 2), 2 * (i - l2) + 2 + s, i - int(s / 2) + 1)
153 elif i == l3 - 1:
154 fa(i, l2, l2 - 1)
155 fa(s, l2 - 1, l2, l2 - int(s / 2))
157 # make table facet face
158 tf = []
159 for i in range(l3):
160 if i > l2 - 1:
161 tf.append(i)
162 fa(*tf)
164 # append lower girdle facet vertices
165 if keep_lga:
166 va(pavi_f / ca, (pavi_f - 1) * pavi_d * 2, 2 * ang, ang, int(s / 2))
167 else:
168 va((pavi_f * (1 - culet) + culet) / ca, (pavi_f - 1) * pavi_d * 2, 2 * ang,
169 ang, int(s / 2))
171 # make lower girdle facet faces
172 l4 = len(Verts) # 3.5*s / 9.5*s
173 for i in range(l4):
174 if i > 0 and i < s - 1 and i % 2 == 0:
175 if girdle_real:
176 fa(i, 2 * (i + 2 * s), i + 2 * s, 2 * (i + 2 * s) + 1, i + 1,
177 int(i / 2) + 9 * s)
178 fa(i, 2 * (i + 2 * s) - 1, i + 2 * s - 1, 2 * (i + 2 * s - 1),
179 i - 1, int(i / 2) + 9 * s - 1)
180 else:
181 fa(i, i + 1, int(i / 2) + l4 - int(s / 2))
182 fa(i, i - 1, int(i / 2) + l4 - int(s / 2) - 1)
183 elif i == 0:
184 if girdle_real:
185 fa(0, 4 * s, 2 * s, 4 * s + 1, 1, 9 * s)
186 fa(0, 6 * s - 1, 3 * s - 1, 6 * s - 2, s - 1, l4 - 1)
187 else:
188 fa(0, 1, l4 - int(s / 2))
189 fa(0, s - 1, l4 - 1)
191 # append culet vertice(s)
192 if culet == 0:
193 va(0, pavi_d * (-2), 0, 0, 1)
194 else:
195 if keep_lga:
196 va(culet * pavi_f / ca, pavi_d * (-2) + culet * pavi_f * 2 * pavi_d,
197 2 * ang, ang, int(s / 2))
198 else:
199 va(culet / ca, pavi_d * (-2), 2 * ang, ang, int(s / 2))
201 # make pavilion facet face
202 l5 = len(Verts) # 4*s / 10*s //if !culet: 3.5*s+1 / 9.5*s+1
203 for i in range(l5):
204 if i > 0 and i < s - 1 and i % 2 == 0:
205 if culet:
206 fa(i, l3 + int(i / 2), l3 + int((s + i) / 2),
207 l3 + int((s + i) / 2) - 1, l3 + int(i / 2) - 1)
208 else:
209 fa(i, l3 + int(i / 2), l5 - 1, l3 + int(i / 2) - 1)
210 elif i == 0:
211 if culet:
212 fa(i, l3, l4, l5 - 1, l4 - 1)
213 else:
214 fa(i, l3, l5 - 1, l4 - 1)
216 # make culet facet face
217 if culet:
218 cf = []
219 for i in range(l5):
220 if i > l4 - 1:
221 cf.append(i)
222 fa(*cf)
224 # create actual mesh and object based on Verts and Faces given
225 dmesh = bpy.data.meshes.new("dmesh")
226 dmesh.from_pydata(Verts, [], Faces)
227 dmesh.update()
229 return dmesh
231 # object generating function, returns final object
232 def addBrilliant(context, self, s, table_w, crown_h, girdle_t, pavi_d, bezel_f,
233 pavi_f, culet, girdle_real, keep_lga, g_real_smooth):
235 # deactivate possible active Objects
236 bpy.context.view_layer.objects.active = None
238 # create actual mesh and object based on Verts and Faces given
239 dmesh = add_mesh_Brilliant(context, s, table_w, crown_h, girdle_t, pavi_d, bezel_f,
240 pavi_f, culet, girdle_real, keep_lga, g_real_smooth)
242 # Create object and link it into scene.
243 dobj = object_utils.object_data_add(context, dmesh, operator=self, name="dobj")
245 # activate and select object
246 bpy.context.view_layer.objects.active = dobj
247 dobj.select_set(True)
248 obj = bpy.context.active_object
250 # flip all face normals outside
251 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
252 sel_mode = bpy.context.tool_settings.mesh_select_mode
253 bpy.context.tool_settings.mesh_select_mode = [False, False, True]
254 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
255 for i, face in enumerate(obj.data.polygons):
256 face.select = True
257 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
258 bpy.ops.mesh.normals_make_consistent(inside=False)
259 bpy.context.tool_settings.mesh_select_mode = sel_mode
260 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
262 # make girdle smooth for complex girdle
263 if girdle_real and g_real_smooth:
265 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
267 bpy.ops.mesh.select_all(action='DESELECT') # deselect all mesh data
268 bpy.ops.object.mode_set(mode='OBJECT')
269 pls = []
270 dp = obj.data.polygons[:4 * s] # only consider faces of girdle
271 ov = obj.data.vertices
273 for i, p in enumerate(dp):
274 pls.extend(p.vertices) # list all verts of girdle
276 for i, e in enumerate(obj.data.edges): # select edges to mark sharp
277 if e.vertices[0] in pls and e.vertices[1] in pls and abs(
278 ov[e.vertices[0]].co.x - ov[e.vertices[1]].co.x):
279 obj.data.edges[i].select = True
280 continue
281 obj.data.edges[i].select = False
283 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
284 bpy.ops.mesh.mark_sharp()
286 bpy.context.tool_settings.mesh_select_mode = [False, False, True]
287 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
288 bpy.ops.object.select_all(action='DESELECT')
289 for i, face in enumerate(obj.data.polygons):
290 if i < 4 * s:
291 face.select = True
292 continue
293 face.select = False
294 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
295 bpy.ops.mesh.faces_shade_smooth()
297 bpy.ops.object.modifier_add(type='EDGE_SPLIT')
299 bpy.context.tool_settings.mesh_select_mode = sel_mode
300 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
302 bpy.ops.object.modifier_apply(modifier="EdgeSplit")
304 return dobj
307 # add new operator for object
308 class MESH_OT_primitive_brilliant_add(Operator, object_utils.AddObjectHelper):
309 bl_idname = "mesh.primitive_brilliant_add"
310 bl_label = "Custom Brilliant"
311 bl_description = "Construct a custom brilliant mesh"
312 bl_options = {'REGISTER', 'UNDO', 'PRESET'}
314 Brilliant : BoolProperty(name = "Brilliant",
315 default = True,
316 description = "Brilliant")
317 change : BoolProperty(name = "Change",
318 default = False,
319 description = "change Brilliant")
321 s: IntProperty(
322 name="Segments",
323 description="Longitudial segmentation",
324 step=1,
325 min=6,
326 max=128,
327 default=16,
328 subtype='FACTOR'
330 table_w: FloatProperty(
331 name="Table width",
332 description="Width of table",
333 min=0.001,
334 max=1.0,
335 default=0.53,
336 subtype='PERCENTAGE'
338 crown_h: FloatProperty(
339 name="Crown height",
340 description="Height of crown",
341 min=0.0,
342 max=1.0,
343 default=0.162,
344 subtype='PERCENTAGE'
346 girdle_t: FloatProperty(
347 name="Girdle height",
348 description="Height of girdle",
349 min=0.0,
350 max=0.5,
351 default=0.017,
352 subtype='PERCENTAGE'
354 girdle_real: BoolProperty(
355 name="Real girdle",
356 description="More beautiful girdle; has more polygons",
357 default=True
359 g_real_smooth: BoolProperty(
360 name="Smooth girdle",
361 description="smooth shading for girdle, only available for real girdle",
362 default=False
364 pavi_d: FloatProperty(
365 name="Pavilion depth",
366 description="Height of pavilion",
367 min=0.0,
368 max=1.0,
369 default=0.431,
370 subtype='PERCENTAGE'
372 bezel_f: FloatProperty(
373 name="Upper facet factor",
374 description="Determines the form of bezel and upper girdle facets",
375 min=0.0,
376 max=1.0,
377 default=0.250,
378 subtype='PERCENTAGE'
380 pavi_f: FloatProperty(
381 name="Lower facet factor",
382 description="Determines the form of pavilion and lower girdle facets",
383 min=0.001,
384 max=1.0,
385 default=0.400,
386 subtype='PERCENTAGE'
388 culet: FloatProperty(
389 name="Culet size",
390 description="0: no culet (default)",
391 min=0.0,
392 max=0.999,
393 default=0.0,
394 subtype='PERCENTAGE'
396 keep_lga: BoolProperty(
397 name="Retain lower angle",
398 description="If culet > 0, retains angle of pavilion facets",
399 default=False
402 def draw(self, context):
403 layout = self.layout
404 box = layout.box()
405 box.prop(self, "s")
406 box.prop(self, "table_w")
407 box.prop(self, "crown_h")
408 box.prop(self, "girdle_t")
409 box.prop(self, "girdle_real")
410 box.prop(self, "g_real_smooth")
411 box.prop(self, "pavi_d")
412 box.prop(self, "bezel_f")
413 box.prop(self, "pavi_f")
414 box.prop(self, "culet")
415 box.prop(self, "keep_lga")
417 if self.change == False:
418 # generic transform props
419 box = layout.box()
420 box.prop(self, 'align', expand=True)
421 box.prop(self, 'location', expand=True)
422 box.prop(self, 'rotation', expand=True)
424 # call mesh/object generator function with user inputs
425 def execute(self, context):
426 # turn off 'Enter Edit Mode'
427 use_enter_edit_mode = bpy.context.preferences.edit.use_enter_edit_mode
428 bpy.context.preferences.edit.use_enter_edit_mode = False
430 if bpy.context.mode == "OBJECT":
431 if context.selected_objects != [] and context.active_object and \
432 (context.active_object.data is not None) and ('Brilliant' in context.active_object.data.keys()) and \
433 (self.change == True):
434 obj = context.active_object
435 oldmesh = obj.data
436 oldmeshname = obj.data.name
437 mesh = add_mesh_Brilliant(context, self.s, self.table_w, self.crown_h,
438 self.girdle_t, self.pavi_d, self.bezel_f,
439 self.pavi_f, self.culet, self.girdle_real,
440 self.keep_lga, self.g_real_smooth
442 obj.data = mesh
443 for material in oldmesh.materials:
444 obj.data.materials.append(material)
445 bpy.data.meshes.remove(oldmesh)
446 obj.data.name = oldmeshname
447 else:
448 obj = addBrilliant(context, self, self.s, self.table_w, self.crown_h,
449 self.girdle_t, self.pavi_d, self.bezel_f,
450 self.pavi_f, self.culet, self.girdle_real,
451 self.keep_lga, self.g_real_smooth
454 obj.data["Brilliant"] = True
455 obj.data["change"] = False
456 for prm in BrilliantParameters():
457 obj.data[prm] = getattr(self, prm)
459 if bpy.context.mode == "EDIT_MESH":
460 active_object = context.active_object
461 name_active_object = active_object.name
462 bpy.ops.object.mode_set(mode='OBJECT')
463 obj = addBrilliant(context, self, self.s, self.table_w, self.crown_h,
464 self.girdle_t, self.pavi_d, self.bezel_f,
465 self.pavi_f, self.culet, self.girdle_real,
466 self.keep_lga, self.g_real_smooth
468 obj.select_set(True)
469 active_object.select_set(True)
470 bpy.context.view_layer.objects.active = active_object
471 bpy.ops.object.join()
472 context.active_object.name = name_active_object
473 bpy.ops.object.mode_set(mode='EDIT')
475 if use_enter_edit_mode:
476 bpy.ops.object.mode_set(mode = 'EDIT')
478 # restore pre operator state
479 bpy.context.preferences.edit.use_enter_edit_mode = use_enter_edit_mode
481 return {'FINISHED'}
483 def BrilliantParameters():
484 BrilliantParameters = [
485 "s",
486 "table_w",
487 "crown_h",
488 "girdle_t",
489 "girdle_real",
490 "g_real_smooth",
491 "pavi_d",
492 "bezel_f",
493 "pavi_f",
494 "culet",
495 "keep_lga",
497 return BrilliantParameters