-
Notifications
You must be signed in to change notification settings - Fork 1
/
crown_methods.py
1821 lines (1381 loc) · 63 KB
/
crown_methods.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Created on Jan 20, 2013
@author: Patrick
'''
#python imports
import time
import math
import random
#blender imports
import bpy
import addon_utils
from mathutils import Vector, Matrix, Quaternion
from bpy_extras.mesh_utils import edge_loops_from_edges
from mathutils.geometry import intersect_point_line
from mathutils.bvhtree import BVHTree
#odc imports
import odcutils
from odcutils import offset_bmesh_edge_loop
from bmesh_fns import join_bmesh_map
from mesh_cut import edge_loops_from_bmedges, space_evenly_on_path
import bmesh
from common_utilities import bversion
def pontificate(context, tooth, shell, p_type, offset):
bpy.ops.object.mode_set(mode= 'OBJECT')
bpy.ops.object.select_all(action = 'DESELECT')
shell.select = True
shell.hide = False
context.scene.objects.active = shell
context.tool_settings.mesh_select_mode = [False, True, True]
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_non_manifold(extend=True)
bpy.ops.object.mode_set(mode = 'OBJECT')
eds = [ed for ed in shell.data.edges if ed.select]
if len(eds) > 4:
odcutils.fill_loop_scale(shell, eds, .3, debug = False)
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.object.mode_set(mode = 'EDIT')
# select the filled_hole group, select more, make a new group
bpy.ops.object.vertex_group_set_active(group = 'filled_hole')
bpy.ops.mesh.select_all(action = 'DESELECT')
bpy.ops.object.vertex_group_select()
bpy.ops.mesh.select_more()
n = len(shell.vertex_groups)
bpy.ops.object.vertex_group_assign_new()
shell.vertex_groups[n].name = "Tissue"
bpy.ops.mesh.select_less()
mx = shell.matrix_world
me = shell.data
if p_type == 'OVATE':
#region to loop and #find the COM of said loop
bpy.ops.mesh.region_to_loop()
bpy.ops.object.mode_set(mode = 'OBJECT') #this updates the selection data...and does some other good stuff
sel_verts = [v.index for v in me.vertices if v.select]
COM = odcutils.get_com(me,sel_verts,mx)
#get dimensions of loop (x,y)
xs = [(mx*me.vertices[i].co)[0] for i in sel_verts]
ys = [(mx*me.vertices[i].co)[1] for i in sel_verts]
scale_x = (max(xs) - min(xs))/1.5
scale_y = (max(ys) - min(ys))/1.5
#add a sphere at COP
context.scene.cursor_location = COM
ov_loc = mx.inverted() * (COM + Vector((0,0,3)))
current_objects=list(bpy.data.objects)
bpy.ops.mesh.primitive_uv_sphere_add(location = tuple(COM + Vector((0,0,3))))
for o in bpy.data.objects:
if o not in current_objects:
#o.parent= Master #conside Master..but then you have to move them both #actually, dependency loop....sphere parent = pontic but pontic shrinkwrapped to sphere...problem.
o.name = tooth.name + '_ovate'
Ovate = o
Ovate.draw_type = 'WIRE'
#pivot point at median point
for A in bpy.context.window.screen.areas:
if A.type == 'VIEW_3D':
for s in A.spaces:
if s.type == 'VIEW_3D':
s.pivot_point = 'MEDIAN_POINT'
#scale X
scale_z = min([scale_x, scale_y])
bpy.ops.transform.resize(value = (scale_x, scale_y, scale_z))
#make pontic active and selected again
bpy.ops.object.select_all(action = 'DESELECT')
shell.select = True
context.scene.objects.active = shell
#shrinkwarp filled hole to this oval
n=len(shell.modifiers)
bpy.ops.object.modifier_add(type='SHRINKWRAP')
mod = shell.modifiers[n]
mod.wrap_method='PROJECT'
mod.vertex_group = "Tissue"
mod.use_negative_direction=True
mod.use_positive_direction=False
mod.use_project_z=True
mod.offset=-0.2 #perhaps negative
mod.target= Ovate
mod.name="Ovate Pontic"
mod.show_expanded=False
for m in range(0,n):
bpy.ops.object.modifier_move_up(modifier = "Ovate Pontic")
bpy.ops.object.mode_set(mode='OBJECT')
if p_type == 'TISSUE':
tissue = tooth.prep_model
if tissue:
Tissue = bpy.data.objects[tissue]
#shrinkwarp filled hole to this oval
n=len(shell.modifiers)
bpy.ops.object.modifier_add(type='SHRINKWRAP')
mod = shell.modifiers[n]
mod.wrap_method='PROJECT'
mod.vertex_group = "Tissue"
mod.use_negative_direction=True
mod.use_positive_direction=True
mod.use_project_z=True
mod.offset=offset #perhaps negative
mod.target= Tissue
mod.name="Tissue Pontic"
mod.show_expanded=False
for m in range(0,n):
print('move modifier up')
bpy.ops.object.modifier_move_up(modifier = "Tissue Pontic")
n=len(shell.modifiers)
bpy.ops.object.modifier_add(type='SMOOTH')
mod = shell.modifiers[n]
mod.vertex_group = "Tissue"
mod.iterations = 10
mod.name="Pontic Smooth"
mod.show_expanded=False
if p_type == 'TISSUE':
n = 2
else:
n = 1
for i in range(0,n):
bpy.ops.object.modifier_move_up(modifier = 'Pontic Smooth')
def prep_from_shell(context, shell, axis_mx, shoulder_width = .75, reduction = 1, base_res = .3, margin_loop = None, debug = False):
'''
shell: blender object representing tooth outer shell
axis_mx = orientation matrix represneing the insertion axis of the prep
shoulder_width = depth of axial inward step from margin perpendicular to insertion axis
chamfer = angle of prep
base_res = base resolution of mesh before multires subdivision
margin_loop = blender bezier closed curve if none, uses nonmanifold edge of shell
'''
if debug:
start = time.time()
if context.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
#if loop: convert to mesh of resolution base_res
current_obs = [ob for ob in bpy.data.objects]
if margin_loop:
if margin_loop.type != 'CURVE':
print('failed, not a curve object')
return {'CANCELLED'}
context.scene.objects.active = margin_loop
margin_loop.select = True
margin_loop.hide = False
bpy.ops.object.convert(target='MESH', keep_original=True)
else:
context.scene.objects.active = shell
shell.select = True
shell.hide = False
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
context.tool_settings.mesh_select_mode = [False,True,False]
bpy.ops.mesh.select_non_manifold()
bpy.ops.mesh.duplicate_move()
bpy.ops.mesh.separate(type = 'SELECTED')
bpy.ops.object.mode_set(mode='OBJECT')
for ob in bpy.data.objects:
if ob not in current_obs:
prep = ob
mx = prep.matrix_world.copy()
for const in prep.constraints:
prep.constraints.remove(const)
prep.matrix_world = mx
odcutils.reorient_object(prep, axis_mx)
#snap in case multires is involved
#hint..this op needs to be bmesh
context.scene.update()
if len(shell.modifiers):
for v in prep.data.vertices:
if bversion() < '002.077.000':
v.co = prep.matrix_world.inverted() * shell.matrix_world * shell.closest_point_on_mesh(shell.matrix_world.inverted() * prep.matrix_world * v.co)[0]
else:
v.co = prep.matrix_world.inverted() * shell.matrix_world * shell.closest_point_on_mesh(shell.matrix_world.inverted() * prep.matrix_world * v.co)[1]
context.scene.update()
bpy.ops.object.select_all(action='DESELECT')
context.scene.objects.active = prep
prep.select = True
#remove modifiers
for mod in prep.modifiers:
if mod.type not in {'MULTIRES', 'SUBSURF'}:
bpy.ops.object.modifier_apply(modifier = mod.name)
else:
bpy.ops.object.modifier_remove(modifier = mod.name)
#clean out the vertex groups
bpy.ops.object.vertex_group_remove(all = True)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.remove_doubles(threshold=base_res)
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.extrude_edges_move()
bpy.context.tool_settings.mesh_select_mode = [True, False, False]
bpy.ops.object.mode_set(mode='OBJECT')
#bpy.ops.object.editmode_toggle()
sel_eds = [ed for ed in prep.data.edges if ed.select]
loc_z = axis_mx.to_quaternion() * Vector((0,0,1))
odcutils.extrude_edges_in(prep.data, sel_eds, prep.matrix_world, loc_z, shoulder_width*.9, debug=debug)
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.extrude_edges_move()
bpy.ops.object.mode_set(mode='OBJECT')
#bpy.ops.object.editmode_toggle()
sel_eds = [ed for ed in prep.data.edges if ed.select]
loc_z = axis_mx.to_quaternion() * Vector((0,0,1))
odcutils.extrude_edges_in(prep.data, sel_eds, prep.matrix_world, loc_z, shoulder_width*.1, debug=debug)
bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()
sel_eds = [ed for ed in prep.data.edges if ed.select]
odcutils.fill_loop_scale(prep, sel_eds, base_res, debug=debug)
bpy.ops.mesh.select_all(action = 'SELECT')
bpy.ops.mesh.normals_make_consistent(inside = False)
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_non_manifold(extend = False)
n = len(prep.vertex_groups)
bpy.ops.object.vertex_group_assign_new()
prep.vertex_groups[n].name = 'Margin'
bpy.ops.object.mode_set(mode='OBJECT')
#project up onto shell
n=len(prep.modifiers)
bpy.ops.object.modifier_add(type='SHRINKWRAP')
mod = prep.modifiers[n]
mod.wrap_method='PROJECT'
mod.vertex_group = 'filled_hole'
mod.use_negative_direction=False
mod.use_positive_direction=True
mod.use_project_z=True
mod.offset = -1*reduction
mod.target=shell
mod.cull_face = 'OFF'
mod.name = "Occlusal Reduction"
#multires?
bpy.ops.object.modifier_add(type='MULTIRES')
for i in range(0,3):
bpy.ops.object.multires_subdivide(modifier="Multires")
if debug:
print("calced prep from shell in %f seconds" % (time.time() - start))
return prep
def calc_intaglio(context, sce, tooth, chamfer, gap, holy_zone, no_undercuts = True, debug = False):
#Get ahold of all relevant tooth items
margin = tooth.margin
prep = tooth.prep_model
axis = tooth.axis
restoration = tooth.contour #TODO make sure this is handled later
pmargin = tooth.pmargin
Prep = bpy.data.objects[prep]
prep_bvh = BVHTree.FromObject(Prep, sce)
prep_imx = Prep.matrix_world.inverted()
prep_mx = Prep.matrix_world
Margin = bpy.data.objects[margin]
margin_mx = Margin.matrix_world
Axis = bpy.data.objects[axis]
#we wil use these to control our translations
axis_quat = Axis.matrix_world.to_quaternion()
axis_mx = Axis.matrix_world
axis_imx = axis_mx.inverted()
axis_z = axis_quat * Vector((0,0,1))
Restoration = bpy.data.objects[restoration]
mx = Restoration.matrix_world
imx = Restoration.matrix_world.inverted()
#rays transform straightforward inverse, unsure on normals
local_z = imx.to_3x3() * axis_z
local_z.normalize()
#get the non manifold edge of the crown and delete all other geometry
intag_bme = bmesh.new()
intag_bme.from_object(Restoration, context.scene)
intag_bme.verts.ensure_lookup_table()
intag_bme.edges.ensure_lookup_table()
non_man_eds = [ed for ed in intag_bme.edges if not ed.is_manifold]
non_man_verts = set()
for ed in non_man_eds:
non_man_verts.add(ed.verts[0])
non_man_verts.add(ed.verts[1])
to_del = [v for v in intag_bme.verts if v not in non_man_verts]
bmesh.ops.delete(intag_bme, geom = to_del, context = 1)
intag_bme.edges.ensure_lookup_table()
intag_bme.verts.ensure_lookup_table()
#space the margin out evenly
loops = edge_loops_from_bmedges(intag_bme, [ed.index for ed in non_man_eds])
if len(loops) > 1:
print('you had another hole in your tooth, go see a dentist')
print('there can not be any holes in the tooth mesh for this step')
return
vs = [intag_bme.verts[i] for i in loops[0][:-1]]
eds = [(0,1),(1,0)] #fake edges...makes them cyclic
vert_path = [v.co for v in vs] + [vs[0].co] #cyclic for space evenly calculation
spaced_coords, eds = space_evenly_on_path(vert_path, eds, len(vert_path)-1 , shift = 0, debug = False)
for i, loc in enumerate(spaced_coords):
vs[i].co = loc
#extrude the edge perpendicular to insertion axis the holy zone width and maintain quads
min_ed = min(non_man_eds, key = lambda ed: ed.calc_length())
offset = min(.03, .75*min_ed.calc_length())
print('offset is %f' % offset)
print('holy zone width is %f' % holy_zone)
print('%i edge loops in holy zone' % math.ceil(holy_zone/offset))
hz_verts = []
new_bmedges = non_man_eds
for i in range(0, math.ceil(holy_zone/offset)):
ret = bmesh.ops.extrude_edge_only(intag_bme, edges = new_bmedges, use_select_history = False)
new_bmverts = [ele for ele in ret['geom'] if isinstance(ele, bmesh.types.BMVert)]
hz_verts += new_bmverts
new_bmedges = [ele for ele in ret['geom'] if isinstance(ele, bmesh.types.BMEdge)]
new_bmfaces = [ele for ele in ret['geom'] if isinstance(ele, bmesh.types.BMFace)]
offset_bmesh_edge_loop(intag_bme, [ed.index for ed in new_bmedges], local_z, offset, debug = False)
loops = edge_loops_from_bmedges(intag_bme, [ed.index for ed in new_bmedges])
vs = [intag_bme.verts[i] for i in loops[0]]
fake_eds = [(0,1),(1,0)]
vert_path = [v.co for v in vs] #cyclic for space evenly calculation
spaced_coords, eds = space_evenly_on_path(vert_path, fake_eds, len(vert_path)-1 , shift = 0, debug = False)
for n, loc in enumerate(spaced_coords):
co = loc + 1.5 * chamfer * offset * local_z
snap, no, ind, d = prep_bvh.find_nearest(prep_imx*mx*co)
vs[n].co = imx * prep_mx * snap
if i == 0:
#get faces oreinted correctly on first pass
print('recalcing normals')
bmesh.ops.recalc_face_normals(intag_bme, faces = new_bmfaces)
bmeds_inds = [ed.index for ed in new_bmedges]
filled_vs = odcutils.fill_bmesh_loop_scale(intag_bme, bmeds_inds, 2 * offset, debug = False)
intag_bme.verts.index_update()
for v in filled_vs:
v.co += 15 * local_z
hz_inds = [v.index for v in hz_verts]
filled_inds = [v.index for v in filled_vs]
print('there are %i HZ inds' % len(hz_inds))
print('there are %i filled inds' % len(filled_inds))
intag_me = bpy.data.meshes.new(tooth.name +'_intaglio')
intag_ob = bpy.data.objects.new(tooth.name + 'intaglio', intag_me)
intag_ob.matrix_world = mx
intag_bme.to_mesh(intag_me)
#change intag local coords so that local z is aligned to
#insertion axis.
intag_ob.data.transform(mx.to_3x3().to_4x4())
intag_ob.data.transform(axis_imx.to_3x3().to_4x4())
intag_ob.matrix_world = axis_mx.to_3x3().to_4x4()
intag_ob.location = mx.to_translation()
context.scene.objects.link(intag_ob)
context.scene.objects.active = intag_ob
intag_ob.select = True
hz_group = intag_ob.vertex_groups.new(name = 'Holy Zone')
hz_group.add(hz_inds, 1, 'ADD')
filled_group = intag_ob.vertex_groups.new(name = 'Filled Zone')
#weird bug where vert group has verts alread in it!
filled_group.remove([v.index for v in intag_ob.data.vertices])
filled_group.add(filled_inds, 1, 'ADD')
mod = intag_ob.modifiers.new('Project Undercuts', 'SHRINKWRAP')
mod.wrap_method = 'PROJECT'
mod.use_negative_direction = True
mod.use_positive_direction = False
mod.use_project_z = True
mod.target = Prep
mod.vertex_group = 'Filled Zone'
if no_undercuts:
mod = intag_ob.modifiers.new('Smooth', 'SMOOTH')
mod.iterations = 20
mod.vertex_group = 'Filled Zone'
mod = intag_ob.modifiers.new('Cement Gap', 'SHRINKWRAP')
mod.wrap_method = 'NEAREST_SURFACEPOINT'
mod.target = Prep
mod.vertex_group = 'Filled Zone'
mod.offset = gap
mod.use_keep_above_surface = True
Restoration.hide = True
tooth.intaglio = intag_ob.name
intag_bme.free()
del prep_bvh
return
def calc_intaglio2(context, sce, tooth, chamfer, gap, holy_zone, debug = False):
if debug:
start = time.time()
#Get ahold of all relevant tooth items
margin = tooth.margin
prep = tooth.prep_model
axis = tooth.axis
restoration = tooth.contour #TODO make sure this is handled later
pmargin = tooth.pmargin
master = sce.odc_props.master
Margin = bpy.data.objects[margin]
Prep = bpy.data.objects[prep]
Axis = bpy.data.objects[axis]
Restoration = bpy.data.objects[restoration]
Psuedomargin = bpy.data.objects[pmargin]
#we wil use these to control our translations
axis_quat = Axis.matrix_world.to_quaternion()
axis_z = axis_quat * Vector((0,0,1))
#take control of the scene TODO:consider overriding context
bpy.ops.object.select_all(action='DESELECT')
current_objects=list(bpy.data.objects)
Restoration.hide = False
sce.objects.active=Restoration
Restoration.select = True
#we want to make a temporary copy of the resoration so that
#we can apply all the dynamic modifiers but still have the option
#to go back and make changes if necessary.
bpy.ops.object.duplicate()
intaglio=str(tooth.name + "_Intaglio")
for o in bpy.data.objects:
if o not in current_objects:
o.name=intaglio
o.parent= sce.objects[master]
bpy.ops.object.select_all(action='DESELECT')
Intaglio = bpy.data.objects[intaglio]
sce.objects.active=Intaglio
Intaglio.select = True
bpy.ops.object.multires_base_apply(modifier="Multires")
#make sure local z is aligned with insertion axis
orientation = Axis.matrix_world
odcutils.reorient_object(Intaglio,orientation)
#save these for later. Currently multires level 3 is th
#all that is needed but I want to keep the option open
#to let the user define the amount of precision.
multires_cuts = Restoration.modifiers['Multires'].levels
bpy.ops.object.modifier_remove(modifier="Multires")
for mod in Intaglio.modifiers:
bpy.ops.object.modifier_apply(modifier=mod.name)
#clean out the vertex groups
bpy.ops.object.vertex_group_remove(all = True)
sce.objects.active=Intaglio
Intaglio.select=True
Restoration.hide=False
#Keep just the free edge of the of resoration to use as a starting ppint
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.context.tool_settings.mesh_select_mode = [False, True, False]
bpy.ops.mesh.select_non_manifold()
bpy.context.tool_settings.mesh_select_mode = [True, False, False]
bpy.ops.mesh.select_all(action="INVERT")
bpy.ops.mesh.delete()
me = Intaglio.data
bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()
sel_edges=[e for e in me.edges if e.select == True]
odcutils.extrude_edges_in(me, sel_edges, Intaglio.matrix_world, axis_z, .02, debug = debug)
bpy.ops.mesh.extrude_edges_move()
bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle() #this is to update the selected vertices
sel_edges=[e for e in me.edges if e.select == True]
sel_verts=[v for v in me.vertices if v.select == True]
odcutils.extrude_edges_in(me, sel_edges, Intaglio.matrix_world, axis_z, holy_zone)
bpy.ops.transform.translate(value = holy_zone*chamfer*2*axis_z)
n = len(Intaglio.vertex_groups)
bpy.ops.object.vertex_group_assign_new()
Intaglio.vertex_groups[n].name = 'Holy Zone'
bpy.ops.object.editmode_toggle() #Back out to object mode
n = len(Intaglio.modifiers)
bpy.ops.object.modifier_add(type = 'SHRINKWRAP')
mod = Intaglio.modifiers[n]
mod.target = Prep
mod.vertex_group = 'Holy Zone'
mod.wrap_method = 'NEAREST_SURFACEPOINT'
bpy.ops.object.modifier_apply(modifier = mod.name)
bpy.ops.object.editmode_toggle() #this is to update the selected vertices
sel_edges=[e for e in me.edges if e.select == True]
odcutils.fill_loop_scale(Intaglio, sel_edges, .3 , debug = debug) #is this a good scale??.3^3 = 1/27mm
bpy.ops.object.vertex_group_select() #active group is 'filled hole'
bpy.ops.transform.translate(value = 10* axis_z)
bpy.ops.mesh.select_all(action="INVERT")
bpy.ops.transform.translate(value = .5 * axis_z)
#Make normals consistent, very important for projections
#and upcoming modifiers
bpy.ops.mesh.select_all(action = 'SELECT')
bpy.ops.mesh.normals_make_consistent()
bpy.ops.object.mode_set(mode='OBJECT')
#Project down and then lift off the intaglio
#Think of this as teasing on/off an acrylic temporary
n=len(Intaglio.modifiers)
bpy.ops.object.modifier_add(type='SHRINKWRAP')
mod = bpy.context.object.modifiers[n]
mod.wrap_method='PROJECT'
mod.vertex_group = 'filled_hole'
mod.use_negative_direction=True
mod.use_positive_direction=False
mod.use_project_z=True
mod.offset = 8
mod.target=Prep
mod.cull_face = 'BACK'
mod.name = "Initial Projection"
#give the margin back it's v.group
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action = 'DESELECT')
bpy.ops.mesh.select_non_manifold()
bpy.context.tool_settings.vertex_group_weight = 1
n = len(Intaglio.vertex_groups)
bpy.ops.object.vertex_group_assign_new()
Intaglio.vertex_groups[n].name = margin
bpy.ops.object.mode_set(mode='OBJECT')
#Subdivide to the same level as the Restoration
bpy.ops.object.modifier_add(type = 'MULTIRES')
for i in range(0,int(multires_cuts)):
bpy.ops.object.multires_subdivide(modifier = 'Multires')
#Apply both modifiers and fix some vertex groups weights
#note. vertex groups 'grow' during multires subdivision
for mod in Intaglio.modifiers:
bpy.ops.object.modifier_apply(modifier = mod.name)
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action = 'DESELECT')
bpy.ops.object.vertex_group_set_active(group = 'filled_hole')
bpy.ops.object.vertex_group_select()
bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()
bpy.ops.object.vertex_group_set_active(group = 'Holy Zone')
bpy.ops.object.vertex_group_remove_from()
bpy.ops.mesh.select_all(action = 'DESELECT')
bpy.ops.object.vertex_group_select()
sce.tool_settings.vertex_group_weight = 1
bpy.ops.object.vertex_group_assign()
bpy.ops.object.mode_set(mode = 'OBJECT')
#poject the high res mesh onto the prep
n=len(Intaglio.modifiers)
bpy.ops.object.modifier_add(type='SHRINKWRAP')
mod = bpy.context.object.modifiers[n]
mod.wrap_method='PROJECT'
mod.use_negative_direction=True
mod.use_positive_direction=False
mod.use_project_z= True
mod.target= Prep
mod.auxiliary_target = Psuedomargin
mod.name = "Final Seat"
#Snap The Edge to the Margin
n=len(Intaglio.modifiers)
bpy.ops.object.modifier_add(type='SHRINKWRAP')
mod = Intaglio.modifiers[n]
mod.name = 'Marginal Seal'
mod.vertex_group = margin
mod.wrap_method='NEAREST_VERTEX'
mod.target=Margin
#Seal the holy zone to the prep
n=len(Intaglio.modifiers)
bpy.ops.object.modifier_add(type='SHRINKWRAP')
mod = Intaglio.modifiers[n]
mod.name = 'HZ Seal'
mod.vertex_group = 'Holy Zone'
mod.wrap_method='NEAREST_SURFACEPOINT'
mod.use_keep_above_surface = True
mod.target=Prep
#Establish the Cement Gap
n = len(Intaglio.modifiers)
bpy.ops.object.modifier_add(type = 'SHRINKWRAP')
mod = Intaglio.modifiers[n]
mod.name = 'Cement Gap'
mod.offset = gap
mod.vertex_group = 'filled_hole'
mod.wrap_method = 'NEAREST_SURFACEPOINT'
mod.use_keep_above_surface = True
mod.target = Prep
#Apply the "final seat" modifier because it is direction dependent
#and we do not want further rotations to affect it.
bpy.ops.object.modifier_apply(modifier="Final Seat")
Restoration.hide = True
tooth.intaglio = intaglio
#for a in bpy.context.window.screen.areas:
# if a.type == 'VIEW_3D':
# for s in a.spaces:
# if s.type == 'VIEW_3D':
# if not s.local_view:
# bpy.ops.view3d.localview()
if tooth.bubble:
Bubble = sce.objects[tooth.bubble]
Bubble.hide = True
if debug:
print("calced intaglio in %f seconds" % (time.time() - start))
def cervical_convergence_improved(context, tooth, angle, selected = False, debug = False):
if debug:
start = time.time()
sce = context.scene
restoration=tooth.contour #TODO:...put this back to restoration after get crown form is all tidied up.
axis = tooth.axis
Axis = bpy.data.objects[axis]
Restoration=bpy.data.objects[restoration]
matrix1 = Restoration.matrix_world
bpy.ops.object.select_all(action='DESELECT')
Restoration.hide = False
Restoration.select = True
sce.objects.active = Restoration
#find the margin.
bpy.ops.object.mode_set(mode = 'EDIT')
context.tool_settings.mesh_select_mode = [True, False, False]
bpy.ops.mesh.select_all(action = 'DESELECT')
bpy.ops.mesh.select_non_manifold()
## ###Toggle for selection and groups to udpate #####
bpy.ops.object.editmode_toggle() #ob
Restoration=bpy.data.objects[restoration]
vs = Restoration.data.vertices
margin_verts = set([v.index for v in vs if v.select]) #unordere set
if debug > 1:
print(margin_verts)
print("#######################")
print("Margin verts selected at %f" % (start - time.time()))
#print(margin_verts)
### Additionally Select the Next Loop (but not connecting edges)
bpy.ops.object.editmode_toggle() #ed
bpy.ops.mesh.select_more()
bpy.ops.object.editmode_toggle() #ob
#make a set of all the keys for the selected edges
#later we will subtract circumfrential edges to
#acvieve just vertical edges
v_edges = {e.key for e in Restoration.data.edges if e.select}
#select just the loops
bpy.ops.object.editmode_toggle() #ed
context.tool_settings.mesh_select_mode = [False, True, False]
bpy.ops.mesh.region_to_loop()
###Toggle for selection and groups to udpate #####
bpy.ops.object.editmode_toggle() #ob
eds = [e for e in Restoration.data.edges if e.select]
loops = edge_loops_from_edges(Restoration.data, eds)
exclude_edges = {e.key for e in eds} #a set of the the circumfrential loops edges
if loops[0][0] in margin_verts:
ring_vert_loop = loops[1]
margin_vert_loop = loops[0]
else:
ring_vert_loop = loops[0]
margin_vert_loop = loops[1]
margin_vert_loop.pop()
ring_vert_loop.pop()
margin_set = set(margin_vert_loop)
ring_set = set(ring_vert_loop)
vertical_edge_set = v_edges - exclude_edges
if debug > 1:
print(vertical_edge_set)
print(margin_set)
print(ring_set)
n = 0
#ed = vertical_edge_set.pop()
for ed in vertical_edge_set: #this should only take one try..perhaps pop one item out and try is.
vert1 = set(ed) & margin_set
vert2 = set(ed) & ring_set
if debug > 1:
print(vert1)
print(vert2)
if vert1 and vert2:
#identify the indices in the loops
#reverse mapping from mesh vert index to edge_loop list index
indx_1 = margin_vert_loop.index(list(vert1)[0]) #index in edge_loop list
indx_2 = ring_vert_loop.index(list(vert2)[0])
#need to test if the lists are reversed relative to each other
#y seeing if the verts one behind in the list also form an
#existing vertical edge in the mesh.
test_edge_key = margin_vert_loop[indx_1 -1], ring_vert_loop[indx_2-1]
test_edge_key1 = test_edge_key[1], test_edge_key[0] #possible that (a,b) isnt in the list but (b,a) is
if not (test_edge_key in vertical_edge_set) or (test_edge_key1 in vertical_edge_set):
ring_vert_loop.reverse()
indx_2 = ring_vert_loop.index(list(vert2)[0]) #need to find the new index...we could do math...but why
ring_vert_loop = odcutils.list_shift(ring_vert_loop, indx_2 - indx_1)
#offsets[i] = offsets[i-1] + indx_2 - indx_1
if debug > 2:
print('found linK in %d iterations' % n)
print(" ")
print(ring_vert_loop)
print(" ")
print(margin_vert_loop)
break
n += 1
### iterate thourhg and translate the top vertex of each vertical edge
### to make the proper angle of cervical convergence.
insertion_z = Axis.matrix_world.to_quaternion() * Vector((0,0,1))
local_z = matrix1.to_quaternion().inverted() * insertion_z
local_z.normalize()
me = Restoration.data
for i in range(0,len(margin_vert_loop)):
bot = margin_vert_loop[i]
tp = ring_vert_loop[i]
v1 = me.vertices[bot].co
v2 = me.vertices[tp].co
normal = me.vertices[tp].normal #lets presume all the normals are right
edge_v = v2 - v1
if debug > 1:
print(bot)
print(tp)
print(edge_v.length)
#A = edge_v.normalized()
# keep in mind, axis is important for direction...rotated z down to a
# meaning positive angles rotate away from z and toward A
#if A is inside of the z axis...it will rotate inside
#may need to do some ccw, cs test on the margin loop or
#use normals....lets just test and see what happens.
axis = local_z.cross(normal)
axis.normalize()
sin = math.sin(angle/2)
cos = math.cos(angle/2)
quat = Quaternion((cos, sin*axis[0], sin*axis[1], sin*axis[2]))
quat.normalize()
vec = edge_v.length * local_z
me.vertices[tp].co = v1 + quat * vec
#print('translating' + str(trans))
context.tool_settings.mesh_select_mode = [True, False, False]
if debug:
finish = time.time() - start
print('completed cerv convergnce in %f seconds' % finish)
def seat_to_margin_improved(context, sce, tooth, influence, debug = False):
if debug:
start = time.time()
contour = tooth.contour
margin = tooth.margin
Restoration = bpy.data.objects[contour]
Margin = bpy.data.objects[margin]
restoration_mx = Restoration.matrix_world
margin_mx = Margin.matrix_world
Restoration.hide = False
Restoration.select = True
sce.objects.active = Restoration
bpy.ops.object.mode_set(mode='EDIT')
# Ensure non manifold edge is the margin of the crown and that it is
#grouped correctly in case the mesh has been altered from its original
#topology.
if margin in bpy.context.object.vertex_groups: #dependent on name being the same
bpy.ops.mesh.select_all(action = 'SELECT')
bpy.ops.object.vertex_group_set_active(group = margin)
bpy.ops.object.vertex_group_remove_from()
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_non_manifold()
## ###Toggle for selection and groups to udpate #####
bpy.ops.object.editmode_toggle() #object
bpy.ops.object.editmode_toggle() #edit
sce.tool_settings.vertex_group_weight = 1
if margin not in Restoration.vertex_groups:
n=len(Restoration.vertex_groups)
bpy.ops.object.vertex_group_assign_new()
Restoration.vertex_groups[n].name = margin
else:
bpy.ops.object.vertex_group_set_active(group = margin)
bpy.ops.object.vertex_group_assign()
#method is as follows
#I..get edge loops for rings from margin to equator
bpy.ops.object.mode_set(mode='OBJECT')
eds = [e for e in Restoration.data.edges if e.select]
margin_verts = edge_loops_from_edges(Restoration.data, eds)[0]
margin_verts.pop() #get rid of the last one
bpy.ops.object.vertex_group_set_active(group = 'Equator')
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.object.vertex_group_select()
#The next operator will leav us with the enture
#bottom half of the tooth selected
bpy.ops.mesh.loop_to_region()
## ###Toggle for selection and groups to udpate #####
bpy.ops.object.mode_set(mode='OBJECT')
seat_region_verts = [v for v in Restoration.data.vertices if v.select]
#we will make a set of all the edge vertex indices...and remove the
#circumfrential edges so we can test vertical edges later
vertical_edge_set = set([e.key for e in Restoration.data.edges if e.select])
n_verts_loop = len(margin_verts)
n_loops = int(len(seat_region_verts)/n_verts_loop)
if debug ==2:
print("there are %d verts in the margin loop" % len(margin_verts))
bpy.ops.object.mode_set(mode = 'EDIT')
region_loops = [None]*n_loops
region_loops[0] = margin_verts
sce.tool_settings.mesh_select_mode = [False, True, False]
for n in range(0,n_loops-1):
bpy.ops.mesh.region_to_loop()
bpy.ops.object.mode_set(mode='OBJECT')
eds = [e for e in Restoration.data.edges if e.select]
#we take out the horzontal edges at each iteration
#leaving us with just the vertical edgse
horizotal_edge_set = set(e.key for e in eds)
vertical_edge_set -= horizotal_edge_set
loops = edge_loops_from_edges(Restoration.data, eds)
if loops[1][0] not in margin_verts:
loop_index = 1
else:
loop_index = 0
loops[loop_index].pop()
region_loops[n_loops-(n+1)] = loops[loop_index]
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.loop_to_region()