forked from patmo141/odc_public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
denture_base.py
521 lines (388 loc) · 17.8 KB
/
denture_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
'''
Created on Sep 17, 2016
@author: Patrick
'''
'''
Created on Aug 18, 2016
@author: Patrick
'''
import bpy
import bmesh
import math
from mathutils import Vector, Matrix, Color
from mathutils.bvhtree import BVHTree
from bpy_extras import view3d_utils
from common_utilities import bversion
import common_drawing
import bgl_utils
from mesh_cut import edge_loops_from_bmedges, space_evenly_on_path
from textbox import TextBox
from odcutils import get_settings, obj_list_from_lib, obj_from_lib
from bpy.props import IntProperty, FloatProperty, BoolProperty, EnumProperty
class OPENDENTAL_OT_prepare_meta_scaffold(bpy.types.Operator):
"""Decimate Mesh to Target edge length, usualy a cutout from polytrim"""
bl_idname = "opendental.meta_scaffold_create"
bl_label = "Create Meta Scaffold"
bl_options = {'REGISTER', 'UNDO'}
radius = FloatProperty(default = 1.25, description = 'Radius of scafold should be 1/2 radius of planned metaballs')
finalize = BoolProperty(default = False, description = 'Will apply the decimate modifier')
@classmethod
def poll(cls, context):
if context.mode == "OBJECT" and context.object != None:
return True
else:
return False
def execute(self, context):
ob = context.object
bme = bmesh.new()
bme.from_object(ob, context.scene)
bme.edges.ensure_lookup_table()
total_l = 0
for ed in bme.edges:
total_l += ed.calc_length()
total_l *= 1/len(bme.edges)
factor = total_l / self.radius
mod = ob.modifiers.new('Reduce','DECIMATE')
mod.ratio = min(1,.5 * factor)
if self.finalize:
context.scene.update()
me = ob.to_mesh(context.scene, apply_modifiers = True, settings = 'PREVIEW')
new_ob = bpy.data.objects.new('Meta Scaffold', me)
context.scene.objects.link(new_ob)
new_ob.matrix_world = ob.matrix_world
if ob.data.materials:
new_ob.data.materials.append(ob.data.materials[0])
bme.free()
return {'FINISHED'}
def invoke(self, context, event):
self.n_verts = len(context.object.data.vertices)
return context.window_manager.invoke_props_dialog(self)
#def draw(self,context):
#
# layout = self.layout
# row = layout.row()
# row.label(text = "%i metaballs will be added" % self.n_verts)
#
# if self.n_verts > 10000:
# row = layout.row()
# row.label(text = "WARNING, THIS SEEMS LIKE A LOT")
# row = layout.row()
# row.label(text = "Consider CANCEL and decimating more")
#
# row = layout.row()
# row.prop(self, "radius")
# row.prop(self, "finish")
class OPENDENTAL_OT_meta_offset_surface(bpy.types.Operator):
"""Create Meta Offset Surface from mesh"""
bl_idname = "opendental.meta_offset_surface"
bl_label = "Create Meta Surface"
bl_options = {'REGISTER', 'UNDO'}
radius = FloatProperty(default = 2.5, description = 'Radius metaballs to be added')
finalize = BoolProperty(default = False, description = 'Will convert meta to mesh and remove meta object')
resolution = FloatProperty(default = .8, description = 'Mesh resolution. 0.8 for dentures, .3 for cast reduction')
n_verts = IntProperty(default = 1000)
@classmethod
def poll(cls, context):
if context.mode == "OBJECT" and context.object != None:
return True
else:
return False
def execute(self, context):
ob = context.object
mx = ob.matrix_world
meta_data = bpy.data.metaballs.new('Meta Mesh')
meta_obj = bpy.data.objects.new('Meta Surface', meta_data)
meta_data.resolution = self.resolution
meta_data.render_resolution = self.resolution
context.scene.objects.link(meta_obj)
# Copy Material if any
if ob.data.materials:
mat = ob.data.materials[0]
meta_obj.data.materials.append(mat)
for v in self.bme.verts:
mb = meta_data.elements.new(type = 'BALL')
mb.radius = self.radius
mb.co = v.co
meta_obj.matrix_world = mx
if self.finalize:
context.scene.update()
me = meta_obj.to_mesh(context.scene, apply_modifiers = True, settings = 'PREVIEW')
new_ob = bpy.data.objects.new('MetaSurfaceMesh', me)
context.scene.objects.link(new_ob)
new_ob.matrix_world = mx
if meta_obj.data.materials:
new_ob.data.materials.append(mat)
context.scene.objects.unlink(meta_obj)
bpy.data.objects.remove(meta_obj)
bpy.data.metaballs.remove(meta_data)
self.bme.free()
return {'FINISHED'}
def invoke(self, context, event):
self.bme = bmesh.new()
self.bme.from_object(context.object, context.scene)
self.bme.verts.ensure_lookup_table()
self.n_verts = len(self.bme.verts)
return context.window_manager.invoke_props_dialog(self)
def draw(self,context):
layout = self.layout
row = layout.row()
row.label(text = "%i metaballs will be added" % self.n_verts)
if self.n_verts > 10000:
row = layout.row()
row.label(text = "WARNING, THIS SEEMS LIKE A LOT")
row = layout.row()
row.label(text = "Consider CANCEL/decimating more or possible long processing time")
row = layout.row()
row.prop(self, "radius")
row.prop(self,"resolution")
row.prop(self, "finalize")
class OPENDENTAL_OT_meta_rim_from_curve(bpy.types.Operator):
"""Create Meta Wax Rim from double bezier curve"""
bl_idname = "opendental.meta_rim_from_curve"
bl_label = "Create Meta Wax Rim"
bl_options = {'REGISTER', 'UNDO'}
posterior_width = FloatProperty(default = 9, description = 'Width of posterior rim')
anterior_width = FloatProperty(default = 4, description = 'Width of anterior rim')
meta_type = EnumProperty(name = 'Meta Type', items = [('CUBE','CUBE','CUBE'), ('ELLIPSOID', 'ELLIPSOID','ELLIPSOID')], default = 'CUBE')
@classmethod
def poll(cls, context):
if context.mode == "OBJECT" and context.object != None and context.object.type == 'CURVE':
return True
else:
return False
def execute(self, context):
crv_data = context.object.data
if len(crv_data.splines) != 2:
#TODO, make real error
print('ERROR, curve must have 2 splines in one object')
return {'CANCELLED'}
ob = context.object
mx = ob.matrix_world
meta_data = bpy.data.metaballs.new('Meta Wax Rim')
meta_obj = bpy.data.objects.new('Meta Surface', meta_data)
meta_data.resolution = .8
meta_data.render_resolution = .8
context.scene.objects.link(meta_obj)
me = context.object.to_mesh(context.scene, apply_modifiers = True, settings = 'PREVIEW')
bme = bmesh.new()
bme.from_mesh(me)
bme.verts.ensure_lookup_table()
bme.edges.ensure_lookup_table()
loops = edge_loops_from_bmedges(bme, [ed.index for ed in bme.edges])
vs0 = [bme.verts[i].co for i in loops[0]]
vs1 = [bme.verts[i].co for i in loops[1]]
vs_even_0, eds0 = space_evenly_on_path(vs0, [(0,1),(1,2)], 60)
vs_even_1, eds1 = space_evenly_on_path(vs1, [(0,1),(1,2)], 60)
if (vs_even_0[0]-vs_even_1[0]).length > (vs_even_0[0]-vs_even_1[-1]).length:
vs_even_1.reverse()
for i in range(1,len(vs_even_0)-1):
blend = -abs((i-30)/30)+1
v0_0 = vs_even_0[i]
v1_1 = vs_even_1[i]
v0_p1 = vs_even_0[i+1]
v1_p1 = vs_even_1[i+1]
v0_m1 = vs_even_0[i-1]
v1_m1 = vs_even_1[i-1]
mid = .5*v0_0 + .5*v1_1
Z = v1_1 - v0_0
Z.normalize()
x0, x1 = v0_p1 - v0_m1, v1_p1 - v1_m1
x0.normalize()
x1.normalize()
X = .5*x0 + .5*x1
X.normalize()
Y = Z.cross(X)
X_c = Y.cross(Z) #X corrected
T = Matrix.Identity(3)
T.col[0] = X_c
T.col[1] = Y
T.col[2] = Z
quat = T.to_quaternion()
mb = meta_data.elements.new(type = self.meta_type)
mb.size_y = .5 * (blend*self.anterior_width + (1-blend)*self.posterior_width)
mb.size_z = .5 * (v1_1 - v0_0).length
mb.size_x = 2
mb.rotation = quat
mb.stiffness = 2
mb.co = mid
meta_obj.matrix_world = mx
#if self.finalize:
# context.scene.update()
# me = meta_obj.to_mesh(context.scene, apply_modifiers = True, settings = 'PREVIEW')
# new_ob = bpy.data.objects.new('MetaSurfaceMesh', me)
# context.scene.objects.link(new_ob)
# new_ob.matrix_world = mx
# if meta_obj.data.materials:
# new_ob.data.materials.append(meta_obj.data.materials[0])
#
# context.scene.objects.unlink(meta_obj)
# bpy.data.objects.remove(meta_obj)
# bpy.data.metaballs.remove(meta_data)
bme.free()
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
class OPENDENTAL_OT_meta_custom_tray(bpy.types.Operator):
"""Create Meta Offset Solid from mesh"""
bl_idname = "opendental.meta_custom_tray"
bl_label = "Create Meta Custom Tray"
bl_options = {'REGISTER', 'UNDO'}
tray_thickness = FloatProperty(default = 2, description = 'Thickness of tray')
tray_offset = FloatProperty(default = 2.5, description = 'Spacer for impression material')
finalize = BoolProperty(default = False, description = 'Will convert meta to mesh and remove meta object')
n_verts = IntProperty(default = 5000)
@classmethod
def poll(cls, context):
if context.mode == "OBJECT" and context.object != None:
return True
else:
return False
def execute(self, context):
ob = context.object
mx = ob.matrix_world
meta_data = bpy.data.metaballs.new('Meta Mesh')
meta_obj = bpy.data.objects.new('Meta Surface', meta_data)
meta_data.resolution = .8
meta_data.render_resolution = .8
context.scene.objects.link(meta_obj)
# Copy Material if any
if ob.data.materials:
mat = ob.data.materials[0]
meta_obj.data.materials.append(mat)
for v in self.bme.verts:
#outer shell
mb = meta_data.elements.new(type = 'BALL')
mb.radius = self.tray_thickness + self.tray_offset
mb.co = v.co
#inner spacer
#mb2 = meta_data2.elements.new(type = 'BALL')
#mb2.radius = self.tray_offset
#mb2.co = v.co
#bridge them?
meta_obj.matrix_world = mx
if self.finalize:
context.scene.update()
me = meta_obj.to_mesh(context.scene, apply_modifiers = True, settings = 'PREVIEW')
new_ob = bpy.data.objects.new('MetaSurfaceMesh', me)
context.scene.objects.link(new_ob)
new_ob.matrix_world = mx
if meta_obj.data.materials:
new_ob.data.materials.append(mat)
context.scene.objects.unlink(meta_obj)
bpy.data.objects.remove(meta_obj)
bpy.data.metaballs.remove(meta_data)
self.bme.free()
return {'FINISHED'}
def invoke(self, context, event):
self.bme = bmesh.new()
self.bme.from_object(context.object, context.scene)
self.bme.verts.ensure_lookup_table()
self.n_verts = len(self.bme.verts)
return context.window_manager.invoke_props_dialog(self)
def draw(self,context):
layout = self.layout
row = layout.row()
row.label(text = "%i metaballs will be added" % self.n_verts)
if self.n_verts > 10000:
row = layout.row()
row.label(text = "WARNING, THIS SEEMS LIKE A LOT")
row = layout.row()
row.label(text = "Consider CANCEL and decimating more")
row = layout.row()
row.prop(self, "tray_thickness")
row = layout.row()
row.prop(self, "tray_offset")
row = layout.row()
row.prop(self, "finalize")
class OPENDENTAL_OT_simple_offset_surface(bpy.types.Operator):
"""Simple normal offset 0 - 2mm """
bl_idname = "opendental.simple_offset_surface"
bl_label = "Simple Meta Surface"
bl_options = {'REGISTER', 'UNDO'}
offset = FloatProperty(default = 0.3, min = -3, max = 3, description = 'Distance to offset')
duplicate = BoolProperty(default = False , description = 'Will create new object, leaving original in tact')
smooth = BoolProperty(default = True, description = 'Will add smooth modifier to attempt to remove self intersections')
shrink = smooth = BoolProperty(default = False, description = 'Will add shrinkwrap modifier to attempt to reoffset after smoothing')
@classmethod
def poll(cls, context):
if context.mode == "OBJECT" and context.object != None and context.object.type == 'MESH':
return True
else:
return False
def execute(self, context):
ob = context.object
mx = ob.matrix_world
if not self.duplicate:
for v in ob.data.vertices:
v.co += self.offset*v.normal
return {'FINISHED'}
# Copy Material if any
if ob.data.materials:
mat = ob.data.materials[0]
me = context.object.to_mesh(context.scene, apply_modifiers = True, settings = 'PREVIEW')
nos = [v.normal for v in me.vertices]
for i, v in enumerate(me.vertices):
v.co += self.offset * nos[i]
new_ob = bpy.data.objects.new(ob.name + '_offset', me)
context.scene.objects.link(new_ob)
new_ob.matrix_world = mx
if ob.data.materials:
new_ob.data.materials.append(mat)
if self.smooth:
smod = new_ob.modifiers.new('Smooth', type = 'SMOOTH')
smod.iterations = 10
if self.duplicate and self.shrink:
swmod = new_ob.modifiers.new('Shrinkwrap', type = 'SHRINKWRAP')
swmod.wrap_method = 'NEAREST_SURFACEPOINT'
swmod.offset = self.offset
swmod.use_keep_above_surface = True
swmod.target = ob
smod = new_ob.modifiers.new('Smooth', type = 'SMOOTH')
smod.iterations = 10
return {'FINISHED'}
class OPENDENTAL_OT_boolean_intaglio(bpy.types.Operator):
"""Add boolean modifier to remove intaglio surfaec"""
bl_idname = "opendental.denture_boolean_intaglio"
bl_label = "Create Meta Surface"
bl_options = {'REGISTER', 'UNDO'}
def item_cb(self, context):
return [(obj.name, obj.name, '') for obj in self.objs]
objs = bpy.props.CollectionProperty(type=bpy.types.PropertyGroup)
ob = bpy.props.EnumProperty(name="Master Cast",
description="Select obj in scene which is master cast",
items=item_cb)
@classmethod
def poll(cls, context):
if context.mode == "OBJECT" and context.object != None:
return True
else:
return False
def execute(self, context):
ob = context.object
mod = ob.modifiers.new('Intaglio', type = 'BOOLEAN')
mod.operation = 'DIFFERENCE'
if self.ob != None:
mod.object = bpy.data.objects[self.ob]
return {'FINISHED'}
def invoke(self, context, event):
self.objs.clear()
assets = [ob.name for ob in context.scene.objects if ob != context.object]
for asset_object_name in assets:
self.objs.add().name = asset_object_name
return context.window_manager.invoke_props_dialog(self)
def register():
bpy.utils.register_class(OPENDENTAL_OT_meta_offset_surface)
bpy.utils.register_class(OPENDENTAL_OT_meta_custom_tray)
bpy.utils.register_class(OPENDENTAL_OT_prepare_meta_scaffold)
bpy.utils.register_class(OPENDENTAL_OT_meta_rim_from_curve)
bpy.utils.register_class(OPENDENTAL_OT_boolean_intaglio)
bpy.utils.register_class(OPENDENTAL_OT_simple_offset_surface)
def unregister():
bpy.utils.unregister_class(OPENDENTAL_OT_meta_offset_surface)
bpy.utils.unregister_class(OPENDENTAL_OT_meta_custom_tray)
bpy.utils.unregister_class(OPENDENTAL_OT_prepare_meta_scaffold)
bpy.utils.unregister_class(OPENDENTAL_OT_meta_rim_from_curve)
bpy.utils.unregister_class(OPENDENTAL_OT_boolean_intaglio)
bpy.utils.unregister_class(OPENDENTAL_OT_simple_offset_surface)
if __name__ == "__main__":
register()