-
Notifications
You must be signed in to change notification settings - Fork 1
/
crown.py
1852 lines (1424 loc) · 74 KB
/
crown.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
import bpy
import os
import math
import time
import bmesh
from odcutils import edge_loops_from_bmedges
from mathutils import Vector, Matrix
from bpy_extras import view3d_utils
#from . import odcutils
#from . import crown_methods
#from . import full_arch_methods
#from . import bgl_utils
#from . import classes
import odcutils, crown_methods, full_arch_methods, bgl_utils
from odcutils import get_settings
import odcmenus.menu_utils as menu_utils
import odcmenus.button_data as button_data
from curve import CurveDataManager
from textbox import TextBox
from common_utilities import bversion, space_evenly_on_path
'''
This module handles operators for the crown (and maybe bridge) functionality of ODC
The "meat" of the more complicated operator functions is in crown_methods.py
Some License might be in the source directory. Summary: don't be an asshole, but do whatever you want with this code
Author: Patrick Moore: patrick.moore.bu@gmail.com
'''
#Global variables (should they be?)
global lib_teeth
global lib_teeth_enum
lib_teeth = []
lib_teeth_enum = []
class OPENDENTAL_OT_center_all_objects(bpy.types.Operator):
'''Use With Caution especially if objects are parented.'''
bl_idname = "opendental.center_objects"
bl_label = "Center All Objects"
bl_options = {'REGISTER','UNDO'}
def execute(self,context):
sce = bpy.context.scene
#gather all the objects
objects = [ob for ob in sce.objects] #don't want this to update
#put all their origins at their medianpoint
bpy.ops.object.select_all(action='DESELECT')
for ob in objects:
sce.objects.active = ob
ob.hide = False
ob.select = True
bpy.ops.object.origin_set(type = 'ORIGIN_GEOMETRY', center = 'BOUNDS')
ob.select = False
#calculate the median point of all the objects
Med = Vector((0,0,0))
for ob in objects:
Med += ob.location
Med = 1/len(objects)*Med
print(Med)
#Move everyone
bpy.ops.object.select_all(action = 'SELECT')
bpy.ops.transform.translate(value = (-Med[0], -Med[1], -Med[2]))
#celebrate
return{'FINISHED'}
class OPENDENTAL_OT_set_master(bpy.types.Operator):
''''''
bl_idname='opendental.set_master'
bl_label="Set Master"
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(cls,context):
if not hasattr(context.scene, 'odc_props'): return False
condition_1 = context.object != None
return condition_1
def execute(self, context):
if len(bpy.context.selected_editable_objects) > 1:
bpy.ops.object.join()
ob=bpy.context.object
if ob:
n = 5
if len(ob.name) < 5:
n = len(ob.name) - 1
new_name = "Master_" + ob.name[0:n]
ob.name = new_name
bpy.context.scene.odc_props.master = new_name
odcutils.layer_management(context.scene.odc_teeth, debug = False)
odcutils.material_management(context, [context.scene.odc_props])
else:
self.report({'WARNING'}, "Nothing is active...sometimes you need to right click again :-)")
return{'FINISHED'}
class OPENDENTAL_OT_set_as_prep(bpy.types.Operator):
''''''
bl_idname = 'opendental.set_as_prep'
bl_label = "Set as Prep"
bl_options = {'REGISTER','UNDO'}
abutment = bpy.props.BoolProperty(name = "abutment", default = False)
def execute(self, context):
settings = get_settings()
layers_copy = [layer for layer in context.scene.layers]
context.scene.layers[0] = True
#grab active tooth the old way
sce=bpy.context.scene
tooth = odcutils.active_tooth_from_index(sce)
a = tooth.name
prep_name = str( a + "_Prep")
#get master model from scene properties
master=sce.odc_props.master
Master=bpy.data.objects[master]
#keep track of the current objects so new objects
#are easily identified.
current_objects=list(bpy.data.objects)
act_ob = bpy.context.object
#this means we are selecting an object to assign
if bpy.context.mode == 'OBJECT':
if act_ob.name != master:
act_ob.name = prep_name
Prep = act_ob #need the actual object to parent it
#rare case, one prep work session, just duplicate the prep to make a master model
if act_ob.name == master:
bpy.ops.object.duplicate()
#this will prevent us from messing up any
#abutment/implant relationships. So that we can
#adjust implant placement after consideration of
#restorative solution. Potentially no longer useful
'''
if not self.abutment:
sce.objects.active = Master
Prep.select = True
bpy.ops.object.parent_set(type = 'OBJECT')
'''
#this means we are segmenting part of an object
if bpy.context.mode == 'EDIT_MESH':
bpy.ops.mesh.duplicate()
bpy.ops.mesh.separate(type='SELECTED')
bpy.ops.object.editmode_toggle()
bpy.ops.object.select_all(action = 'DESELECT')
#identify new object as prep and
new_objs = []
for ob in sce.objects:
if ob not in current_objects:
new_objs.append(ob)
if len(new_objs):
Prep = new_objs[0]
Prep.name = prep_name
#put the property of the working tooth
tooth.prep_model = prep_name
#make Prep a child of Master
odcutils.parent_in_place(Prep, Master)
#put the Prep's origin somewhere logical
Prep.select = True
sce.objects.active = Prep
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
#TODO: make this dependent on "Parallel or Linear" work flow
if settings.workflow == '0':
Master.hide = True
#look down on prep
bpy.ops.view3d.viewnumpad(type='TOP', align_active = True)
if settings.workflow in {'1','2'}:
sce.objects.active = act_ob
act_ob.select = True
bpy.ops.object.mode_set(mode='EDIT')
#layers
odcutils.layer_management(sce.odc_teeth) #needs to be a list so it's iterable even if it's one thing.
for i, layer in enumerate(layers_copy):
context.scene.layers[i] = layer
context.scene.layers[1] = True
return {'FINISHED'}
class OPENDENTAL_OT_set_mesial(bpy.types.Operator):
''''''
bl_idname = 'opendental.set_mesial'
bl_label = "Set Mesial"
bl_options = {'REGISTER','UNDO'}
def execute(self, context):
#grab active tooth the old way
sce=bpy.context.scene
tooth = odcutils.active_tooth_from_index(sce)
tooth.mesial = context.object.name
self.report({'INFO'},'You Set The Mesial')
return {'FINISHED'}
class OPENDENTAL_OT_set_distal(bpy.types.Operator):
''''''
bl_idname = 'opendental.set_distal'
bl_label = "Set Distal"
bl_options = {'REGISTER','UNDO'}
def execute(self, context):
#grab active tooth the old way
sce=bpy.context.scene
tooth = odcutils.active_tooth_from_index(sce)
tooth.distal = context.object.name
self.report({'INFO'},'You Set The Distal')
return {'FINISHED'}
class OPENDENTAL_OT_set_opposing(bpy.types.Operator):
''''''
bl_idname = 'opendental.set_opposing'
bl_label = "Set Opposing"
bl_options = {'REGISTER','UNDO'}
for_all = bpy.props.BoolProperty(default = True)
def execute(self, context):
#grab active tooth the old way
if self.for_all:
for tooth in context.scene.odc_teeth:
tooth.opposing = context.object.name
context.scene.odc_props.opposing = context.object.name
else:
sce=bpy.context.scene
tooth = odcutils.active_tooth_from_index(sce)
tooth.opposing = context.object.name
self.report({'INFO'},'You Set The Opposing')
return {'FINISHED'}
class ViewToZ(bpy.types.Operator):
'''Aligns the local coordinates of the acive object with the view'''
bl_idname = "view3d.view_to_z"
bl_label = "View to Z"
bl_options = {'REGISTER','UNDO'}
keep_orientation = bpy.props.BoolProperty(default = False, name = "Keep Orientation")
def execute(self, context):
bpy.ops.object.select_all(action = 'DESELECT')
ob = bpy.context.object
ob.select = True
#necessary because I don't want to have to wory
#about what the transform orientation might be
# bpy.ops.object.transform_apply(rotation = True)
#this is what the view rotation is reported as
#so for convenience I will just make the object
#use it
#ob.rotation_mode = 'QUATERNION'
#gather info
space = bpy.context.space_data
region = space.region_3d
vrot = region.view_rotation
#align = vrot.inverted()
odcutils.reorient_object(ob,vrot)
#rotate the object the inverse of the view rotation
#ob.rotation_quaternion = align
#if we want to keep the rotatio nof the object in
#the scene and essentially just set the object's
#local coordinates to the view...then do this.
if self.keep_orientation:
bpy.ops.object.transform_apply(rotation = True)
ob.rotation_quaternion = vrot
return {'FINISHED'}
def insertion_axis_draw_callback(self, context):
self.help_box.draw()
self.target_box.draw()
bgl_utils.insertion_axis_callback(self,context)
class OPENDENTAL_OT_insertion_axis(bpy.types.Operator):
"""Set the insertion axis of the preps from view"""
bl_idname = "opendental.insertion_axis"
bl_label = "Insertion Axis"
bl_options = {'REGISTER','UNDO'}
def set_axis(self, context, event):
tooth = context.scene.odc_teeth[self.target]
axis = tooth.axis
if axis and axis in bpy.data.objects:
ob = bpy.data.objects[axis]
ob.empty_draw_type = 'SINGLE_ARROW'
ob.empty_draw_size = 10
else:
ob = bpy.data.objects.new(tooth.name + "_Axis", None)
ob.empty_draw_type = 'SINGLE_ARROW'
ob.empty_draw_size = 10
context.scene.objects.link(ob)
tooth.axis = ob.name
master = context.scene.odc_props.master
if master and master in bpy.data.objects:
odcutils.parent_in_place(ob,bpy.data.objects[master])
else:
self.report({'WARNING'}, 'No Master model, be careful when moving things')
coord = (event.mouse_region_x, event.mouse_region_y)
v3d = context.space_data
rv3d = v3d.region_3d
view_vector = view3d_utils.region_2d_to_vector_3d(context.region, rv3d, coord)
ray_origin = view3d_utils.region_2d_to_origin_3d(context.region, rv3d, coord)
ray_target = ray_origin + (view_vector * 1000)
if bversion() < '002.077.000':
res, obj, loc, no, mx = context.scene.ray_cast(ray_origin, ray_target)
else:
res, loc, no, ind, obj, mx = context.scene.ray_cast(ray_origin, view_vector)
if res:
ob.location = loc
else:
vect = context.space_data.region_3d.view_location - ray_origin
Z = rv3d.view_rotation * Vector((0,0,1)) #-1?
delta_view = vect.dot(Z) * Z #this works kind of like orthographic mode even in perspective
ob.location = ray_origin + delta_view
if ob.rotation_mode != 'QUATERNION':
ob.rotation_mode = 'QUATERNION'
vrot = rv3d.view_rotation
self.align = vrot.inverted()
if ob.parent:
mx = ob.parent.matrix_world
imx = mx.inverted()
iq = imx.to_quaternion()
vrot = iq * vrot
ob.rotation_quaternion = vrot
def advance_next_prep(self,context):
self.target_index = int(math.fmod(self.target_index +1, len(self.units)))
self.target = self.units[self.target_index]
self.message = "Set axis for %s" % self.units[self.target_index]
self.target_box.raw_text = self.message
self.target_box.format_and_wrap_text()
self.target_box.fit_box_width_to_text_lines()
tooth = context.scene.odc_teeth[self.target]
for obj in bpy.data.objects:
obj.select = False
if tooth.prep_model in bpy.data.objects:
bpy.data.objects[tooth.prep_model].select = True
context.space_data.region_3d.view_location = bpy.data.objects[tooth.prep_model].location
def select_prev_unit(self,context):
self.target_index = int(math.fmod(self.target_index - 1, len(self.units)))
self.target = self.units[self.target_index]
self.message = "Set axis for %s" % self.units[self.target_index]
self.target_box.raw_text = self.message
self.target_box.format_and_wrap_text()
self.target_box.fit_box_width_to_text_lines()
tooth = context.scene.odc_teeth[self.target]
for obj in bpy.data.objects:
obj.select = False
if tooth.prep_model in bpy.data.objects:
bpy.data.objects[tooth.prep_model].select = True
context.space_data.region_3d.view_location = bpy.data.objects[tooth.prep_model].location
def update_selection(self,context):
selection_targets = odcutils.tooth_selection(context)
self.target_index = self.units.index(selection_targets[0].name)
self.target = self.units[self.target_index]
self.message = "Set axis for %s" % self.units[self.target_index]
self.target_box.raw_text = self.message
self.target_box.format_and_wrap_text()
self.target_box.fit_box_width_to_text_lines()
def modal_main(self, context, event):
# general navigation
nmode = self.modal_nav(event)
if nmode != '':
return nmode #stop here and tell parent modal to 'PASS_THROUGH'
if event.type in {'RIGHTMOUSE'} and event.value == 'PRESS':
self.update_selection(context)
return 'pass'
elif event.type == 'RIGHTMOUSE' and event.value == 'RELEASE':
self.update_selection(context)
if len(context.selected_objects):
context.space_data.region_3d.view_location = context.selected_objects[0].location
return 'main'
elif event.type in {'LEFTMOUSE'} and event.value == 'PRESS':
#raycast to check a hit?
self.set_axis(context, event)
self.advance_next_prep(context)
return 'main'
elif event.type in {'DOWN_ARROW'} and event.value == 'PRESS':
self.select_prev_unit(context)
return 'main'
elif event.type in {'UP_ARROW'} and event.value == 'PRESS':
self.advance_next_prep(context)
return 'main'
elif event.type == 'SPACEBAR' and event.value == 'PRESS':
self.set_axis(context)
return 'main'
elif event.type in {'ESC'}:
#keep track of and delete new objects? reset old transforms?
return'cancel'
elif event.type in {'RET'} and event.value == 'PRESS':
return 'finish'
return 'main'
def modal_nav(self, event):
events_nav = {'MIDDLEMOUSE', 'WHEELINMOUSE','WHEELOUTMOUSE', 'WHEELUPMOUSE','WHEELDOWNMOUSE'} #TODO, better navigation, another tutorial
handle_nav = False
handle_nav |= event.type in events_nav
if handle_nav:
return 'nav'
return ''
def modal(self, context, event):
context.area.tag_redraw()
FSM = {}
FSM['main'] = self.modal_main
FSM['pass'] = self.modal_main
FSM['nav'] = self.modal_nav
nmode = FSM[self.mode](context, event)
if nmode == 'nav':
return {'PASS_THROUGH'}
if nmode in {'finish','cancel'}:
#clean up callbacks
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
return {'FINISHED'} if nmode == 'finish' else {'CANCELLED'}
if nmode == 'pass':
self.mode = 'main'
return {'PASS_THROUGH'}
if nmode: self.mode = nmode
return {'RUNNING_MODAL'}
def invoke(self, context, event):
settings = get_settings()
dbg = settings.debug
odcutils.scene_verification(context.scene, debug = dbg)
context.scene.layers[0] = True
if context.space_data.region_3d.is_perspective:
#context.space_data.region_3d.is_perspective = False
bpy.ops.view3d.view_persportho()
if context.space_data.type != 'VIEW_3D':
self.report({'WARNING'}, "Active space must be a View3d")
return {'CANCELLED'}
else:
#gather all the teeth in the scene
self.units = [tooth.name for tooth in context.scene.odc_teeth]
if not self.units:
self.report({'ERROR'}, "Yikes, there are no working teeth in the sceen!")
return {'CANCELLED'}
selection_targets = odcutils.tooth_selection(context)
if selection_targets:
self.target = selection_targets[0].name
else:
self.target = self.units[0]
self.target_index = self.units.index(self.target)
self.message = "Set axis for %s" %self.units[self.target_index]
help_txt = "Right click to select a prep \n View path of insertion \n Up Arrow and Dn Arrow to select different units \n Left click in middle of prep to set axis \n Enter to finish \n ESC to cancel"
self.help_box = TextBox(context,500,500,300,200,10,20,help_txt)
self.help_box.fit_box_width_to_text_lines()
self.help_box.fit_box_height_to_text_lines()
self.help_box.snap_to_corner(context, corner = [1,1])
aspect, mid = menu_utils.view3d_get_size_and_mid(context)
self.target_box = TextBox(context,mid[0],aspect[1]-20,300,200,10,20,self.message)
self.target_box.format_and_wrap_text()
self.target_box.fit_box_width_to_text_lines()
self.target_box.fit_box_height_to_text_lines()
self.mode = 'main'
context.window_manager.modal_handler_add(self)
self._handle = bpy.types.SpaceView3D.draw_handler_add(insertion_axis_draw_callback, (self, context), 'WINDOW', 'POST_PIXEL')
return {'RUNNING_MODAL'}
class CBGetCrownForm(bpy.types.Operator):
'''Inserts a crown form from the tooth library at the cursor location'''
bl_idname = 'opendental.get_crown_form'
bl_label = "Get Crown Form"
bl_options = {'REGISTER','UNDO'}
bl_property = "ob_list"
def item_cb(self, context):
return [(obj.name, obj.name, '') for obj in self.objs]
objs = bpy.props.CollectionProperty(type=bpy.types.PropertyGroup)
ob_list = bpy.props.EnumProperty(name="Tooth Library Objects", description="A List of the tooth library", items=item_cb)
sculpt = bpy.props.BoolProperty(default = True)
def invoke(self, context, event):
self.objs.clear()
settings = get_settings()
#here we grab the asset library from the addon prefs
libpath = settings.tooth_lib
#a list of all objects in the asset library
assets = odcutils.obj_list_from_lib(libpath, exclude = '_')
for asset_object_name in assets:
self.objs.add().name = asset_object_name
#context.window_manager.invoke_search_popup(self.ob_list)
context.window_manager.invoke_search_popup(self)
return {'FINISHED'}
def execute(self, context):
#TODO:...make this work for multiple teeth
#TODO:...incorporate with teeth on curve
sce = context.scene
settings = get_settings()
tooth = None
tooth_candidates = odcutils.tooth_selection(context)
if tooth_candidates:
tooth = tooth_candidates[0]
if not tooth:
self.report({'WARNING'},"I'm not sure which tooth you want, guessing based on active tooth in list")
tooth = sce.odc_teeth[sce.odc_tooth_index]
if tooth.restoration and tooth.restoration in bpy.data.objects:
old_ob = bpy.data.objects[tooth.restoration]
old_ob.name = 'To Delete'
context.scene.objects.unlink(old_ob)
old_ob.user_clear()
bpy.data.objects.remove(old_ob)
if tooth == None:
self.report({'WARNING'},"No planned teeth, inserting object anyway")
odcutils.obj_from_lib(settings.tooth_lib,self.ob_list)
ob = bpy.data.objects[self.ob_list]
sce.objects.link(ob)
ob.location = sce.cursor_location
if tooth != None:
ob.name = tooth.name + "_FullContour"
tooth.contour = ob.name
ob.rotation_mode = 'QUATERNION'
#align with the insertion axis
axis = tooth.axis
if not axis:
self.report({'WARNING'}, "No insertion axis set, can't align tooth properly, may cause problem later!")
else:
rot = sce.objects[axis].rotation_quaternion #matrix_world.to_quaterion() ? Perhaps so
ob.rotation_quaternion = rot
if tooth.rest_type == '0' or tooth.rest_type == '1':
tooth.restoration = ob.name
if tooth.rest_type == '1':
print('pontic!')
sce.objects.active = ob
ob.select = True
bpy.ops.view3d.view_selected()
bme = bmesh.new()
bme.from_mesh(ob.data)
bme.edges.ensure_lookup_table()
bme.verts.ensure_lookup_table()
eds_non_man = [ed for ed in bme.edges if not ed.is_manifold]
vs_non_man = []
for ed in eds_non_man:
for v in ed.verts:
if v not in vs_non_man:
vs_non_man.append(v)
com = Vector((0,0,0))
min_x, min_y, max_x, max_y, min_z = 0, 0, 0, 0, 1000000
for v in vs_non_man:
com += v.co
if v.co[0] < min_x: min_x = v.co[0]
if v.co[0] > max_x: max_x = v.co[0]
if v.co[1] < min_y: min_y = v.co[1]
if v.co[1] > max_y: max_y = v.co[1]
if v.co[2] < min_z: min_z = v.co[2]
com *= 1/len(vs_non_man)
context.scene.cursor_location = com
width_x = max_x - min_x
center_x = 0.5 * (max_x + min_x)
center_y = 0.5 * (max_y + min_y)
width_y = max_y - min_y
minor = min(width_x, width_y)
fit_no = odcutils.calculate_plane([v.co for v in vs_non_man])
if fit_no[2] > 0: fit_no *= -1 #pointing apically
orientation = odcutils.rot_between_vecs(Vector((0,0,-1)), fit_no)
new_geom = bmesh.ops.create_uvsphere(bme,
u_segments = len(eds_non_man),
v_segments = len(eds_non_man),
diameter = 1)
to_delete = [v for v in new_geom['verts'] if v.co[2] > 0.001]
scale = Matrix()
scale[0][0] = width_x/2
scale[1][1] = width_y/2
scale[2][2] = minor/3
com[0] = center_x
com[1] = center_y
trans = Matrix.Translation(com + .15 * minor/2 * fit_no)
rot = orientation.to_matrix().to_4x4()
for v in new_geom['verts']:
#remember your matrix order or operations!
v.co = trans * rot * scale * v.co
bmesh.ops.delete(bme, geom = to_delete, context = 1)
bme.verts.ensure_lookup_table()
bme.edges.ensure_lookup_table()
eds_non_man = [ed.index for ed in bme.edges if not ed.is_manifold]
loops = edge_loops_from_bmedges(bme, eds_non_man)
stroke_data = []
for loop in loops:
stroke_data += [bme.verts[i].co for i in loop]
new_stroke_data, new_eds = space_evenly_on_path(stroke_data, [(0,1),(1,2)], segments = 200)
bmesh.ops.bridge_loops(bme, edges = [bme.edges[i] for i in eds_non_man])
bme.to_mesh(ob.data) #may need to do the editmesh thing
if not self.sculpt: return {'FINISHED'}
bpy.ops.object.mode_set(mode = 'SCULPT')
screen = bpy.context.window.screen
for area in screen.areas:
if area.type == 'VIEW_3D':
for reg in area.regions:
if reg.type == 'WINDOW':
break
for space in area.spaces:
if space.type == 'VIEW_3D':
break
break
scene = bpy.context.scene
paint_settings = scene.tool_settings.unified_paint_settings
paint_settings.use_locked_size = True
paint_settings.unprojected_radius = 1.5
#paint_settings.size = 50
brush = bpy.data.brushes['Smooth']
scene.tool_settings.sculpt.brush = brush
brush.strength = .8
brush.stroke_method = 'SPACE'
brush.use_frontface = True
override = bpy.context.copy()
override['area'] = area
override['region'] = reg
override['space_data'] = space
override['region_data'] = space.region_3d
bpy_stroke = []
for i, co in enumerate(new_stroke_data):
mouse = view3d_utils.location_3d_to_region_2d(reg, space.region_3d, co)
if mouse[0] < 0 or mouse[1] < 1:
space.region_3d.view_location = co
space.region_3d.update()
mouse = view3d_utils.location_3d_to_region_2d(reg, space.region_3d, co)
ele = {"name": "my_stroke",
"mouse" : (mouse[0], mouse[1]),
"pen_flip" : False,
"is_start": True,
"location": (co[0], co[1], co[2]),
"pressure": 1,
"size" : 50,
"time": 1}
bpy_stroke.append(ele)
print('sculpting')
bpy.ops.sculpt.brush_stroke(override, stroke=bpy_stroke, mode='NORMAL', ignore_background_click=False)
bpy.ops.object.mode_set(mode = 'OBJECT')
#layer management
odcutils.layer_management(sce.odc_teeth)
#odcutils.transform_management(tooth,sce)
return {'FINISHED'}
class OPENDENTAL_OT_seat_to_margin(bpy.types.Operator):
'''
Adapts the open edge of the crown to the marked margin.
'''
bl_idname = 'opendental.seat_to_margin'
bl_label = "Seat to Margin"
bl_options = {'REGISTER','UNDO'}
influence = bpy.props.FloatProperty(name="Nearby Influence", description="", default=1, min=.1, max=2, step=2, precision=1, options={'ANIMATABLE'})
@classmethod
def poll(cls, context):
#restoration exists and is in scene
if not hasattr(context.scene, 'odc_props'): return False
if not len(context.scene.odc_teeth): return False
if not len(odcutils.tooth_selection(context)): return False
tooth = odcutils.tooth_selection(context)[0] #TODO:...make this poll work for all selected teeth...
condition_1 = tooth.contour in bpy.data.objects
condition_2 = tooth.margin in bpy.data.objects
condition_3 = tooth.pmargin in bpy.data.objects
return condition_1 and condition_2 and condition_3
def execute(self,context):
settings = get_settings()
dbg = settings.debug
odcutils.layer_management(context.scene.odc_teeth, debug = False)
layers_copy = [layer for layer in context.scene.layers]
context.scene.layers[0] = True
#TODO: Scene Preservation recording
teeth = odcutils.tooth_selection(context)
not_valid = []
for tooth in teeth:
if tooth.rest_type == 1: continue
condition_1 = tooth.contour in bpy.data.objects
condition_2 = tooth.margin in bpy.data.objects
condition_3 = tooth.pmargin in bpy.data.objects
if not (condition_1 and condition_2 and condition_3):
reason = 'Tooth #' + tooth.name + ' can not seat because...'
if not condition_1:
reason += 'No full contour, please "Get Crown Form." '
if not condition_2:
reason += 'No margin, please "Mark Margin." '
if not condition_3:
reason += 'Margin still provisional, please "Accept Margin."'
not_valid.append(reason)
if len(not_valid):
for reason in not_valid:
self.report({'INFO'}, reason)
for tooth in teeth:
sce = bpy.context.scene
condition_1 = tooth.contour in bpy.data.objects
condition_2 = tooth.margin in bpy.data.objects
condition_3 = tooth.pmargin in bpy.data.objects
if not (condition_1 and condition_2 and condition_3):
continue
crown_methods.seat_to_margin_improved(context, sce, tooth, influence = self.influence, debug = dbg) #TODO: debug stuff
for i, layer in enumerate(layers_copy):
context.scene.layers[i] = layer
return {'FINISHED'}
class OPENDENTAL_OT_calculate_inside(bpy.types.Operator):
''''''
bl_idname = 'opendental.calculate_inside'
bl_label = "Calculate Intaglio"
bl_options = {'REGISTER','UNDO'}
holy_zone = bpy.props.FloatProperty(name="Holy Zone Width", description="", default=.4, min=.2, max=2, step=5, precision=1, options={'ANIMATABLE'})
chamfer = bpy.props.FloatProperty(name="Chamfer", description="0 = shoulder 1 = feather", default=.2, min=0, max=1, step=2, precision=2, options={'ANIMATABLE'})
gap = bpy.props.FloatProperty(name="Gap Thickness", description="thickness required for cement", default=0.07, min=.01, max=.5, step=2, precision=2, options={'ANIMATABLE'})
no_undercuts = bpy.props.BoolProperty(name="No Undercuts", description="Uncheck if there are significant undercuts", default=True)
@classmethod
def poll(cls,context):
#restoration exists and is in scene
if not hasattr(context.scene, 'odc_props'): return False
if not len(context.scene.odc_teeth): return False
if not len(odcutils.tooth_selection(context)): return False
tooth = odcutils.tooth_selection(context)[0] #TODO:...make this poll work for all selected teeth...
condition_1 = tooth.contour in bpy.data.objects
condition_2 = tooth.margin in bpy.data.objects
condition_3 = tooth.pmargin in bpy.data.objects
return condition_1 and condition_2 and condition_3
def invoke(self, context, event):
context.window_manager.invoke_props_dialog(self, width=300)
return {'RUNNING_MODAL'}
def execute(self, context):
settings = get_settings()
dbg = settings.debug
if bpy.context.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
layers_copy = [layer for layer in context.scene.layers]
context.scene.layers[0] = True
v3d = bpy.context.space_data
v3d.pivot_point = 'MEDIAN_POINT'
sce=bpy.context.scene
#master = sce.master_model
#exclude = ["mesial_model","distal_model"]
candidates = odcutils.tooth_selection(context)
for tooth in candidates:
if tooth.rest_type == '1': continue #pontic
if self.no_undercuts:
crown_methods.calc_intaglio(context, sce, tooth, self.chamfer, self.gap, self.holy_zone, debug = dbg) #TODO: institude global debug for addon
else:
crown_methods.calc_intaglio2(context, sce, tooth, self.chamfer, self.gap, self.holy_zone, debug =dbg)
for i, layer in enumerate(layers_copy):
context.scene.layers[i] = layer
context.scene.layers[4] = True
#TODO: good logging print("Finished operationt %s on tooth %s in 3 seconds /n again" % (self.bl_label, tooth.name) )
return {'FINISHED'}
class OPENDENTAL_OT_crown_cervical_convergence(bpy.types.Operator):
'''Changes the angle of cervical convergence for the crown relative to insertion axis /n (unfortunately not to tangent of prep)'''
bl_idname = "opendental.cervical_convergence"
bl_label = "Angle Cervical Convergence"
bl_options = {'REGISTER','UNDO'}
ang = bpy.props.FloatProperty(name="Angle", description="", default=math.pi/12, min=0, options={'ANIMATABLE'}, subtype='ANGLE', unit='ROTATION')
@classmethod
def poll(cls, context):
#restoration exists and is in scene
if not hasattr(context.scene, 'odc_props'): return False
if not len(context.scene.odc_teeth): return False
if not len(odcutils.tooth_selection(context)): return False
tooth = odcutils.tooth_selection(context)[0] #TODO: make this poll work for all selected teeth...
condition_1 = tooth.contour and tooth.contour in bpy.data.objects #TODO: make this restoration when that property implemented
condition_2 = tooth.axis and tooth.axis in bpy.data.objects
return condition_1 and condition_2
#def modal(self,context): #TODO: modal code for cervical convergence
#def draw(self,context): #TODO: draw code for cervical convergence
def execute(self, context):
settings = get_settings()
dbg = settings.debug
tooth = odcutils.tooth_selection(context)[0]
layers_copy = [layer for layer in context.scene.layers]
context.scene.layers[0] = True
angle = self.ang
crown_methods.cervical_convergence_improved(context, tooth, angle, selected = False, debug = dbg)
for i, layer in enumerate(layers_copy):
context.scene.layers[i] = layer
odcutils.layer_management(context.scene.odc_teeth, debug = False)
context.scene.layers[1] = True
return{'FINISHED'}
def draw(self, context):
layout = self.layout
row = layout.row()
row.prop(self, "ang")
class OPENDENTAL_make_solid_restoration(bpy.types.Operator):
'''
joins the shell and intaglio to make a millable/printable watertight mesh
'''
bl_idname = 'opendental.make_solid_restoration'
bl_label = "Make Solid Restoration"
bl_options = {'REGISTER','UNDO'}
method = bpy.props.IntProperty(default = 1)
@classmethod
def poll(cls, context):
#restoration exists and is in scene
if not hasattr(context.scene, 'odc_props'): return False
if not len(context.scene.odc_teeth): return False
if not len(odcutils.tooth_selection(context)): return False
tooth = odcutils.tooth_selection(context)[0] #TODO: make this poll work for all selected teeth...
condition_1 = tooth.contour and tooth.contour in bpy.data.objects #TODO: make this restoration when that property implemented
condition_2 = tooth.intaglio and tooth.intaglio in bpy.data.objects
return condition_1 and condition_2
def execute(self,context):
settings = get_settings()
dbg = settings.debug
teeth = odcutils.tooth_selection(context)
layers_copy = [layer for layer in context.scene.layers]
context.scene.layers[0] = True
for tooth in teeth:
if self.method == 0:
crown_methods.make_solid_restoration(context, tooth, debug = dbg)
else:
crown_methods.make_solid_restoration2(context, tooth, debug = dbg)
for i, layer in enumerate(layers_copy):
context.scene.layers[i] = layer
odcutils.layer_management(context.scene.odc_teeth, debug = False)
context.scene.layers[16] = True
return {'FINISHED'}
class OPENDENTAL_OT_plan_restorations(bpy.types.Operator):
'''Select Multiple Interestingly Shaped Buttons'''
bl_idname = "opendental.plan_restorations"
bl_label = "Plan Restorations"
def modal(self, context, event):
context.area.tag_redraw()
if event.type == 'MOUSEMOVE':
#check to see what button the mouse is over if any
self.mouse = (event.mouse_region_x, event.mouse_region_y)
for i in range(0,len(button_data.tooth_button_data)):
self.tooth_button_hover[i] = bgl_utils.point_inside_loop(button_data.tooth_button_data[i],self.mouse,self.menu_width, self.menu_loc)
elif event.type == 'LEFTMOUSE' and event.value == 'PRESS':
self.mouse = (event.mouse_region_x, event.mouse_region_y)
#determine if we are clicking on a toth
for i in range(0,len(button_data.tooth_button_data)):