-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmooth_edge - Copy.py
5841 lines (4950 loc) · 283 KB
/
smooth_edge - Copy.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
bl_info = {
"name": "Smooth Tooth Edge",
"author": "Rien",
"version": (1, 0),
"blender": (2, 83, 0),
"location": "SpaceBar Search -> Add-on Preferences Example",
"description": "Smooth the edge of a selected single tooth",
"warning": "",
"doc_url": "",
"tracker_url": "",
"category": "Mesh",
}
import collections
from enum import EnumMeta
from types import CodeType, resolve_bases
from typing import Text
from numpy.lib import angle
from numpy.lib.arraypad import _set_pad_area
from numpy.linalg import norm
import bpy
import mathutils
import bmesh
import math
import numpy as np
import sys
from bpy_extras import object_utils
import functools
import re
import os
import json
import getpass
dir = os.path.expanduser('~')+'\\AppData\\Roaming\\Blender Foundation\\Blender\\2.83\\scripts\\addons\\pie_menu_editor\\scripts\\zhangzechu'
if not dir in sys.path:
sys.path.append(dir)
# from fillToothHole import fillSingleToothHole, fill_all_teeth_hide
from movePanel import recover_homogenous_affine_transformation
from movePanel import create_plane_three_point
from movePanel import into_select_faces_mode_jaw
from movePanel import delete_object
from movePanel import create_plane
filepath=os.path.expanduser('~')+'/AppData/Roaming/Blender Foundation/Blender/2.83/parameter.json'
parameterlist=[]
with open(filepath, 'r') as f:
parameterlist = json.load(f)
#read parameters
setnames = locals()
for key, value in parameterlist.items():
setnames[key]=value
def exec_read_global_peremeter(commend,key):
_locals = locals()
exec(commend,globals(),_locals)
if key in _locals:
return _locals[key]
return commend
#read parameters
setnames = locals()
for key, value in parameterlist.items():
setnames[key]=value
def exec_read_global_peremeter(commend,key):
_locals = locals()
exec(commend,globals(),_locals)
if key in _locals:
return _locals[key]
return commend
def remove_extra_vetrices_faces(context_object):
edge_keys = context_object.data.edge_keys
total_index = len(context_object.data.vertices)
vrt_index = []
for edge_key in edge_keys:
vrt_index.append(edge_key[0])
vrt_index.append(edge_key[1])
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode = 'OBJECT')
for index in range(total_index):
b = vrt_index.count(index)
if b == 1:
context_object.data.vertices[index].select = True
elif b == 3:
if len(bpy.context.object.data.polygons) != 0:
for polygon in bpy.context.object.data.polygons:
three_index = []
three_index.append(polygon.edge_keys[0][0])
three_index.append(polygon.edge_keys[0][1])
if polygon.edge_keys[1][0] != three_index[0] and polygon.edge_keys[1][0] != three_index[1]:
three_index.append(polygon.edge_keys[1][0])
else:
three_index.append(polygon.edge_keys[1][1])
print(three_index)
for tri_index in three_index:
if vrt_index.count(tri_index) == 2:
context_object.data.vertices[tri_index].select = True
break
else:
pass
else:
pass
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.delete(type='VERT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode = 'OBJECT')
return {'FINISHED'}
def draw(self, context):
error_message = str(context.object.pass_index) + '\'s knife panel is not in correct position!'
self.layout.label(text=error_message)
def distance(point1, point2):
"""Calculate distance bewteen two points in 3D space"""
disp = point1 - point2
dis = math.sqrt(disp[0]*disp[0] + disp[1]*disp[1] + disp[2]*disp[2])
return dis
def pair_traingles(through_triangle_info):
dis_triangle_info = dict()
for triangle_info1 in through_triangle_info:
hit_loc1 = triangle_info1[0]
index = triangle_info1[1]
min_dis = 100
min_dis_triangle_info = triangle_info1
for triangle_info2 in through_triangle_info:
if index == triangle_info2[1]:
continue
hit_loc2 = triangle_info2[0]
dis = distance(hit_loc1, hit_loc2)
if dis < min_dis:
min_dis = dis
min_dis_triangle_info = triangle_info2
dis_triangle_info[min_dis] = (triangle_info1, min_dis_triangle_info)
sorted_list = sorted(dis_triangle_info.items(), key=lambda item:item[0], reverse=True)
# print("Sort List:", sorted_list)
return sorted_list
def generate_emboss_panel(self, context, object_name):
if len(context.selected_objects) == 1 and context.object.name.endswith('curve'):
scene = context.scene
mytool = scene.my_tool
tooth_object = context.collection.objects[object_name]
curve_object = context.object
context.view_layer.objects.active = curve_object
context.object.select_set(True)
bpy.ops.object.convert(target='MESH')
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.looptools_space(influence=100, input='selected', interpolation='cubic', lock_x=False, lock_y=False, lock_z=False)
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.duplicate(linked=False, mode='TRANSLATION')
bpy.ops.object.modifier_add(type='SHRINKWRAP')
bpy.context.object.modifiers["Shrinkwrap"].target = tooth_object
bpy.context.object.modifiers["Shrinkwrap"].wrap_mode = 'ABOVE_SURFACE'
bpy.context.object.modifiers["Shrinkwrap"].offset = -0.27
bpy.ops.object.modifier_add(type='SMOOTH')
bpy.context.object.modifiers["Smooth"].iterations = 10
bpy.ops.object.convert(target='MESH')
curve_object_copy = bpy.context.object
temp_name = curve_object.name + '.001'
context.view_layer.objects.active = curve_object
curve_object.select_set(True)
curve_object_copy.select_set(True)
bpy.ops.object.join()
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.bridge_edge_loops(type='PAIRS')
bpy.ops.object.mode_set(mode='OBJECT')
context.view_layer.objects.active = tooth_object
tooth_object.select_set(True)
bpy.ops.object.join()
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.object.vertex_group_add()
tooth_object.vertex_groups['Group'].name = 'panel'
bpy.ops.object.vertex_group_assign()
bpy.ops.mesh.intersect()
bpy.ops.object.vertex_group_add()
tooth_object.vertex_groups['Group'].name = 'intersect'
bpy.ops.object.vertex_group_assign()
bpy.ops.mesh.select_all(action='DESELECT')
tooth_object.vertex_groups.active = tooth_object.vertex_groups['panel']
bpy.ops.object.vertex_group_select()
bpy.ops.object.vertex_group_remove()
bpy.ops.mesh.select_linked(delimit=set())
if context.object.data.total_vert_sel > 1000:
print('Selection Error!')
self.report({'ERROR'}, 'Selection Error!')
return {'CANCELED'}
bpy.ops.mesh.separate(type='SELECTED')
bpy.ops.object.mode_set(mode='OBJECT')
tooth_object.select_set(False)
bpy.ops.object.delete(use_global=False, confirm=True)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.object.vertex_group_select()
bpy.ops.mesh.loop_to_region()
if context.object.data.total_vert_sel > 1000:
print('Selection Error!')
self.report({'ERROR'}, 'Selection Error!')
return {'CANCELED'}
bpy.ops.mesh.duplicate(mode=1)
bpy.ops.mesh.separate(type='SELECTED')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
panel_name = object_name + '.001'
panel_object = bpy.data.objects[panel_name]
panel_object.data.name = object_name + 'panel'
panel_object.name = object_name + 'panel'
return panel_object
def conver_gpencil_to_curve(self, context, pencil, type):
newCurve = bpy.data.curves.new(type + '_line', type='CURVE')
newCurve.dimensions = '3D'
CurveObject = object_utils.object_data_add(context, newCurve)
error = False
try:
strokes = bpy.context.annotation_data.layers.active.active_frame.strokes
except:
error = True
CurveObject.location = (0.0, 0.0, 0.0)
CurveObject.rotation_euler = (0.0, 0.0, 0.0)
CurveObject.scale = (1.0, 1.0, 1.0)
if not error:
for i, _stroke in enumerate(strokes):
print(i)
stroke_points = strokes[i].points
data_list = [ (point.co.x, point.co.y, point.co.z)
for point in stroke_points ]
points_to_add = len(data_list)-1
flat_list = []
for point in data_list:
flat_list.extend(point)
spline = newCurve.splines.new(type='BEZIER')
spline.bezier_points.add(points_to_add)
spline.bezier_points.foreach_set("co", flat_list)
for point in spline.bezier_points:
point.handle_left_type="AUTO"
point.handle_right_type="AUTO"
return CurveObject
else:
return None
def get_picked_tooth_name(scene, context):
items = [
]
if bpy.data.collections.get('Pick_Teeth') is None :
pass
else:
if len(bpy.data.collections['Pick_Teeth'].objects) > 0:
for obj in bpy.data.collections['Pick_Teeth'].objects:
items.append((obj.name, obj.name, obj.name))
return items
def get_bound_box_volume(object):
ref_box = object.bound_box
x = ref_box[0][0]
y = ref_box[0][1]
z = ref_box[0][2]
x_ = 0
y_ = 0
z_ = 0
for vertex in ref_box:
if vertex[0] != x:
x_ = vertex[0]
if vertex[1] != y:
y_ = vertex[1]
if vertex[2] != z:
z_ = vertex[2]
len_x = abs(x - x_)
len_y = abs(y - y_)
len_z = abs(z - z_)
volume = len_x * len_y * len_z
print('box_info ', object.name, len_x, len_y, len_z, volume)
return volume
def get_plane_ref_coord_matrix(plane_object, tooth_object, up_down):
loca = plane_object.location
matrix_world = plane_object.matrix_world.copy()
if len(plane_object.data.polygons) == 1:
polygon_normal = plane_object.data.polygons[0].normal.copy()
if up_down == 'UP_':
if polygon_normal[2] < 0:
polygon_normal[2] = -polygon_normal[2]
else:
if polygon_normal[2] > 0:
polygon_normal[2] = -polygon_normal[2]
polygon_normal = (matrix_world @ polygon_normal) - loca
polygon_normal.normalize()
# print('polygon normal', polygon_normal)
else:
print('Jaw polygons has no face normal')
return {'CANCELLED'}
x_axis = mathutils.Vector((tooth_object.matrix_local.row[0][0], tooth_object.matrix_local.row[1][0], tooth_object.matrix_local.row[2][0]))
z_axis = x_axis.cross(polygon_normal)
z_axis.normalize()
x_axis = polygon_normal.cross(z_axis)
x_axis.normalize()
M_orient = mathutils.Matrix([x_axis, polygon_normal, z_axis])
M_orient.transpose()
M_orient.normalize()
M_orient = M_orient.to_4x4()
N = tooth_object.matrix_local.copy()
M_orient.row[0][3] = N.row[0][3]
M_orient.row[1][3] = N.row[1][3]
M_orient.row[2][3] = N.row[2][3]
return M_orient
def get_tip(tooth_object, K_orient, tooth_number):
K_orient = K_orient.to_3x3()
K_orient_inver = K_orient.inverted()
obj_y_axis = mathutils.Vector((tooth_object.matrix_local.row[0][1], tooth_object.matrix_local.row[1][1], tooth_object.matrix_local.row[2][1]))
local_y_axis = (K_orient_inver @ obj_y_axis)
print(tooth_number,' local Y axis:',local_y_axis)
angle_radian = math.atan2(local_y_axis[0], local_y_axis[1])
if ((10 < tooth_number) and (tooth_number < 20)) or ((30 < tooth_number) and (tooth_number < 40)):
if local_y_axis[0] > 0:
angle_radian = abs(angle_radian)
else:
angle_radian = -abs(angle_radian)
elif ((20 < tooth_number) and (tooth_number < 30)) or ((40 < tooth_number) and (tooth_number < 50)):
if local_y_axis[0] < 0:
angle_radian = abs(angle_radian)
else:
angle_radian = -abs(angle_radian)
else:
pass
print('tip angle radian', angle_radian)
return angle_radian
def get_torque(tooth_object, K_orient):
matrix_world = tooth_object.matrix_world.copy()
obj_loca = tooth_object.location.copy()
polygons = tooth_object.data.polygons
vertex_index = []
cusp_points_hight=polygons[0].center[1]
for face in polygons:
if face.center[2]<0 and cusp_points_hight>face.center[1]:
cusp_points_hight = face.center[1]
vertex_index.append(face.index)
print(tooth_object.name, 'min hight:', cusp_points_hight)
dir = mathutils.Vector((0, 0, 1))
dist = 10
origin = mathutils.Vector((0, cusp_points_hight/4, -10))
result = tooth_object.ray_cast(origin, dir, distance=dist)
polygons[result[3]].select = True
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_more()
bpy.ops.mesh.select_more()
bpy.ops.object.mode_set(mode='OBJECT')
normal_vector = mathutils.Vector((0, 0, 0))
num = 0
for face in polygons:
if face.select == True:
normal_vector = normal_vector + face.normal
num = num + 1
normal_vector = normal_vector / num
normal_vector.normalize()
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')
face_normal = (matrix_world @ normal_vector) - obj_loca
face_normal.normalize()
x_axis = mathutils.Vector((matrix_world.row[0][0], matrix_world.row[1][0], matrix_world[2][0]))
y_axis = x_axis.cross(face_normal)
y_axis.normalize()
x_axis = face_normal.cross(y_axis)
x_axis.normalize()
K_orient = K_orient.to_3x3()
K_orient_inver = K_orient.inverted()
y_axis_local = (K_orient_inver @ y_axis)
y_axis_local.normalize()
angle_raidan = math.atan2(y_axis_local[2], y_axis_local[1])
if y_axis_local[2] > 0:
angle_raidan = abs(angle_raidan)
else:
angle_raidan = -abs(angle_raidan)
# print('y_axis_local', y_axis_local)
# print('torque angle', angle)
return angle_raidan
def update(self, context, operate_id):
mytool = context.scene.my_tool
if mytool.up_down == 'UP_':
bpy.context.view_layer.active_layer_collection = bpy.context.view_layer.layer_collection.children['Curves_U']
else:
bpy.context.view_layer.active_layer_collection = bpy.context.view_layer.layer_collection.children['Curves_D']
jawplaneName = 'jawPlane_' + mytool.up_down
plane_object = bpy.data.objects[jawplaneName]
bpy.ops.object.select_all(action='DESELECT')
print('selected_objects:', bpy.context.selected_objects)
if (bpy.context.selected_objects) == 1:
print(bpy.context.selected_objects[0].name)
# M_orient = get_plane_ref_coord_matrix(plane_object, tooth_object, mytool.up_down)
# if operate_id == 'A':
# current_tip = get_tip(tooth_object, M_orient, int(tooth_number))
# prop_name = 'Tip_' + tooth_number
# update_tip = mytool.get(prop_name)
# disp_angle = current_tip - update_tip
# if (20 < int(tooth_number) < 30) or (40 < int(tooth_number) < 50):
# disp_angle = -disp_angle
# print('Changed Tip:', update_tip * (180/math.pi))
# print('Disprity angle:', disp_angle * (180/math.pi))
# a = (M_orient.row[0][0], M_orient.row[1][0], M_orient.row[2][0])
# b = (M_orient.row[0][1], M_orient.row[1][1], M_orient.row[2][1])
# c = (M_orient.row[0][2], M_orient.row[1][2], M_orient.row[2][2])
# bpy.ops.transform.rotate(value=disp_angle, orient_axis='Z', orient_type='LOCAL', orient_matrix=(a, b, c), orient_matrix_type='LOCAL', constraint_axis=(False, False, True), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)
# elif operate_id == 'B':
# current_tor = get_torque(tooth_object, M_orient)
# prop_name = 'Tor_' + tooth_number
# update_tor = mytool.get(prop_name)
# disp_angle = update_tor - current_tor
# print('Current_tor', current_tor * (180/math.pi))
# print('Changed Tor:', update_tor * (180/math.pi))
# print('Disprity angle:', disp_angle * (180/math.pi))
# a = (M_orient.row[0][0], M_orient.row[1][0], M_orient.row[2][0])
# b = (M_orient.row[0][1], M_orient.row[1][1], M_orient.row[2][1])
# c = (M_orient.row[0][2], M_orient.row[1][2], M_orient.row[2][2])
# bpy.ops.transform.rotate(value=disp_angle, orient_axis='X', orient_type='LOCAL', orient_matrix=(a, b, c), orient_matrix_type='LOCAL', constraint_axis=(True, False, False), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)
# else:
# pass
# tooth_object.select_set(False)
def get_embed(obj1, obj2):
obj2_matrix_local = obj2.matrix_local.copy()
obj2_vertices = obj2.data.vertices
M = obj1.matrix_local.copy()
M_inv = M.inverted()
a = obj1.location.copy()
a[2] = 0
b = obj2.location.copy()
b[2] = 0
p1 = M_inv @ a
p2 = M_inv @ b
dir_local = p2 - p1
neg_dir_local = -dir_local
P_test_point = mathutils.Vector((2.5, 0, 0))
N_test_point = mathutils.Vector((-2.5, 0, 0))
P_local = M_inv @ obj2_matrix_local @ P_test_point
N_local = M_inv @ obj2_matrix_local @ N_test_point
dis_P = math.sqrt(P_local[0]*P_local[0] + P_local[1]*P_local[1] + P_local[2]*P_local[2])
dis_N = math.sqrt(N_local[0]*N_local[0] + N_local[1]*N_local[1] + N_local[2]*N_local[2])
max_dis = -20
min_dis = 100
cast_quantity = 0
# print('dis_P, dis_N', dis_P, dis_N)
for vrt in obj2_vertices:
if dis_P > dis_N:
if (vrt.co[0] < 0) and (vrt.select == False):
r_co = M_inv @ obj2_matrix_local @ vrt.co
result = obj1.ray_cast(r_co, dir_local, distance=10)
result_neg = obj1.ray_cast(r_co, neg_dir_local, distance=10)
if result[0] == True:
cast_quantity = cast_quantity + 1
v_disp = result[1] - r_co
dis_ = math.sqrt(v_disp[0]*v_disp[0] + v_disp[1]*v_disp[1] + v_disp[2]*v_disp[2])
if dis_ > max_dis:
max_dis = dis_
if result_neg[0] == True:
v_disp = result_neg[1] - r_co
dis_ = math.sqrt(v_disp[0]*v_disp[0] + v_disp[1]*v_disp[1] + v_disp[2]*v_disp[2])
if dis_ < min_dis:
min_dis = dis_
else:
if (vrt.co[0] > 0) and (vrt.select == False):
r_co = M_inv @ obj2_matrix_local @ vrt.co
result = obj1.ray_cast(r_co, dir_local, distance=10)
result_neg = obj1.ray_cast(r_co, neg_dir_local, distance=10)
if result[0] == True:
cast_quantity = cast_quantity + 1
v_disp = result[1] - r_co
dis_ = math.sqrt(v_disp[0]*v_disp[0] + v_disp[1]*v_disp[1] + v_disp[2]*v_disp[2])
if dis_ > max_dis:
max_dis = dis_
if result_neg[0] == True:
v_disp = result_neg[1] - r_co
dis_ = math.sqrt(v_disp[0]*v_disp[0] + v_disp[1]*v_disp[1] + v_disp[2]*v_disp[2])
if dis_ < min_dis:
min_dis = dis_
# print('max_dis, min_dis', max_dis, min_dis)
# print('--------------------------')
if cast_quantity != 0:
value = -max_dis
else:
value = min_dis
return value
def get_embed_value(obj1, obj2):
value1 = get_embed(obj1, obj2)
value2 = get_embed(obj2, obj1)
if (value1 < 0) and (value2 > 0):
value_embed = value1
elif (value2 < 0) and (value1 > 0):
value_embed = value2
elif (value1 > 0) and (value2 > 0):
if value1 < value2:
value_embed = value1
else:
value_embed = value2
else:
if value1 < value2:
value_embed = value1
else:
value_embed = value2
return value_embed
def move(obj, dir, value):
# print('move object and dir:', obj.name, dir)
# print('object location', obj.location)
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
z_axis = mathutils.Vector((0, 0, 1))
dir.normalize()
y_axis = dir.cross(z_axis)
y_axis.normalize()
z_axis = dir.cross(y_axis)
z_axis.normalize()
a = (dir[0], dir[1], dir[2])
b = (y_axis[0], y_axis[1], y_axis[2])
c = (z_axis[0], z_axis[1], z_axis[2])
bpy.ops.transform.translate(value=(value, 0, 0), orient_type='LOCAL', orient_matrix=(a, b, c), orient_matrix_type='LOCAL', constraint_axis=(True, False, False), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)
obj.select_set(False)
# print('object location', obj.location)
class DictProperty():
t_numb_vrt_index = dict()
class MyProperties(bpy.types.PropertyGroup):
selected_object_name : bpy.props.StringProperty(name="")
y_direction : bpy.props.FloatVectorProperty(name="Y axis direction of tooth in local coordinate", subtype='XYZ', precision=2, size=3, default=(0.0, 0.0,0.0))
z_direction : bpy.props.FloatVectorProperty(name="Z axis direction of tooth in local coordinate", subtype='XYZ', precision=2, size=3, default=(0.0, 0.0,0.0))
up_teeth_object_name : bpy.props.StringProperty(name="")
down_teeth_object_name : bpy.props.StringProperty(name="")
trim_cut_object_name : bpy.props.StringProperty(name="")
tooth_name : bpy.props.StringProperty(name="")
curve_collection_name_U : bpy.props.StringProperty(name="")
curve_collection_name_D : bpy.props.StringProperty(name="")
up_down : bpy.props.EnumProperty(
name="UP/DOWN",
description="Choose up or down side of teeth",
items=[
('UP_', 'UP', 'It is UP side'),
('DOWN_', 'DOWN', 'It is DOWN side'),
]
)
quantity_of_teeth : bpy.props.EnumProperty(
name="Quantity",
description="Set the quantity of teeth",
items=[
('16', '16', '16'),
('15', '15', '15'),
('14', '14', '14'),
('13', '13', '13'),
('12', '12', '12'),
('11', '11', '11'),
('10', '10', '10'),
('9', '9', '9'),
('8', '8', '8'),
('7', '7', '7'),
('6', '6', '6'),
('5', '5', '5'),
]
)
save_as_name:bpy.props.EnumProperty(
name="Save Name",
description="Save blender file with this name",
items=[
('import_', 'import', 'Save import stage'),
('smooth_edge_', 'smooth edges', 'Save smooth edges stage'),
('sort_curves_', 'sort curves', 'Save sort curves stage'),
('adjust_', 'adjust curves', 'Save adjust curves stage'),
('intersection', 'find intersection', 'Save find intersection stage'),
('knife panel', 'knife panel', 'Save generate knife panel stage'),
('cut teeth', 'cut teeth', 'Save cut teeth stage'),
('add coordinate', 'add coordinate', 'Save add coordinate stage'),
('adjust orientation', 'adjust orientation', 'Save adjust orientation stage'),
]
)
picked_tooth_name:bpy.props.EnumProperty(
name="Picked Tooth Name",
description="Stroe the picked tooth name dynamicly",
items=get_picked_tooth_name
)
scale_up_arch : bpy.props.BoolProperty(name="scale up arch or not", default=False)
scale_down_arch : bpy.props.BoolProperty(name="scale down arch or not", default=False)
factor : bpy.props.FloatProperty(name="extrude_factor",
description="The Factor control resize scale",
default=1.0,
min=1.0, max=1.3,
soft_min=1.0, soft_max=1.3,
step=0.01)
U_28: bpy.props.BoolProperty(name="28", description="Lost Tooth 28", default=False)
U_27: bpy.props.BoolProperty(name="27", description="Lost Tooth 27", default=False)
U_26: bpy.props.BoolProperty(name="26", description="Lost Tooth 26", default=False)
U_25: bpy.props.BoolProperty(name="25", description="Lost Tooth 25", default=False)
U_24: bpy.props.BoolProperty(name="24", description="Lost Tooth 24", default=False)
U_23: bpy.props.BoolProperty(name="23", description="Lost Tooth 23", default=False)
U_22: bpy.props.BoolProperty(name="22", description="Lost Tooth 22", default=False)
U_21: bpy.props.BoolProperty(name="21", description="Lost Tooth 21", default=False)
U_18: bpy.props.BoolProperty(name="18", description="Lost Tooth 18", default=False)
U_17: bpy.props.BoolProperty(name="17", description="Lost Tooth 17", default=False)
U_16: bpy.props.BoolProperty(name="16", description="Lost Tooth 16", default=False)
U_15: bpy.props.BoolProperty(name="15", description="Lost Tooth 15", default=False)
U_14: bpy.props.BoolProperty(name="14", description="Lost Tooth 14", default=False)
U_13: bpy.props.BoolProperty(name="13", description="Lost Tooth 13", default=False)
U_13: bpy.props.BoolProperty(name="13", description="Lost Tooth 13", default=False)
U_12: bpy.props.BoolProperty(name="12", description="Lost Tooth 12", default=False)
U_11: bpy.props.BoolProperty(name="11", description="Lost Tooth 11", default=False)
D_38: bpy.props.BoolProperty(name="38", description="Lost Tooth 38", default=False)
D_37: bpy.props.BoolProperty(name="37", description="Lost Tooth 37", default=False)
D_36: bpy.props.BoolProperty(name="36", description="Lost Tooth 36", default=False)
D_35: bpy.props.BoolProperty(name="35", description="Lost Tooth 35", default=False)
D_34: bpy.props.BoolProperty(name="34", description="Lost Tooth 34", default=False)
D_33: bpy.props.BoolProperty(name="33", description="Lost Tooth 33", default=False)
D_32: bpy.props.BoolProperty(name="32", description="Lost Tooth 32", default=False)
D_31: bpy.props.BoolProperty(name="31", description="Lost Tooth 31", default=False)
D_48: bpy.props.BoolProperty(name="48", description="Lost Tooth 48", default=False)
D_47: bpy.props.BoolProperty(name="47", description="Lost Tooth 47", default=False)
D_46: bpy.props.BoolProperty(name="46", description="Lost Tooth 46", default=False)
D_45: bpy.props.BoolProperty(name="45", description="Lost Tooth 45", default=False)
D_44: bpy.props.BoolProperty(name="44", description="Lost Tooth 44", default=False)
D_43: bpy.props.BoolProperty(name="43", description="Lost Tooth 43", default=False)
D_43: bpy.props.BoolProperty(name="43", description="Lost Tooth 43", default=False)
D_42: bpy.props.BoolProperty(name="42", description="Lost Tooth 42", default=False)
D_41: bpy.props.BoolProperty(name="41", description="Lost Tooth 41", default=False)
UP_tipTorExpand: bpy.props.BoolProperty(name="Tip Tor Expand", description="Expand Tip and Torque Panel", default=False)
class MESH_TO_ready_seperate_teeth(bpy.types.Operator):
"""Read for seperating teeth"""
bl_idname = "mesh.ready_seperating"
bl_label = "Read Seperating"
def execute(self, context):
filename = os.path.expanduser('~') + "\\AppData\\Roaming\\Blender Foundation\\Blender\\2.83\\scripts\\addons\\pie_menu_editor\\scripts\\prepare_tooth_seperation.py"
exec(compile(open(filename).read(), filename, 'exec'))
return {'FINISHED'}
class MESH_TO_seperate_Teeth(bpy.types.Operator):
"""Seperate Teeth"""
bl_idname = "mesh.seperate_teeth"
bl_label = "Seperate Teeth"
def execute(self, context):
filename = os.path.expanduser('~') + "\\AppData\\Roaming\\Blender Foundation\\Blender\\2.83\\scripts\\addons\\pie_menu_editor\\scripts\\tooth_seperation.py"
exec(compile(open(filename).read(), filename, 'exec'))
return {'FINISHED'}
class MESH_TO_draw_arch(bpy.types.Operator):
"""Draw Arch Cut Thread"""
bl_idname = "mesh.draw_arch"
bl_label = "Draw Arch"
def execute(self, context):
if len(context.selected_objects) == 1:
scene = context.scene
mytool = scene.my_tool
mytool.trim_cut_object_name = bpy.context.object.name
trim_cut_object_name = bpy.context.object.name
trim_cut_object = bpy.data.objects[trim_cut_object_name]
context.scene.tool_settings.use_snap = True
bpy.ops.wm.tool_set_by_id(name="builtin.select_box")
# set EDIT mode's select mode to "vertex select"
context.tool_settings.mesh_select_mode = (True , False , False)
bpy.ops.wm.tool_set_by_id(name="builtin.select_box")
context.scene.tool_settings.use_snap = True
context.scene.tool_settings.snap_elements = {'FACE'}
context.scene.tool_settings.use_proportional_edit = False
bpy.ops.object.mode_set(mode="EDIT") #Activating Edit mode
bpy.ops.mesh.select_all(action = 'DESELECT') #Deselecting all
bpy.ops.object.mode_set(mode="OBJECT") #Going back to Object mode
bpy.ops.object.select_all(action='INVERT')
bpy.ops.object.hide_view_set(unselected=False)
bpy.ops.object.select_all(action='DESELECT') #deselect all object
context.view_layer.objects.active = trim_cut_object
trim_cut_object.select_set(True)
bpy.ops.ed.undo_push()
bpy.ops.mesh.primitive_vert_add()
bpy.context.object.name = "loop"
for obj in bpy.context.selected_objects:
obj.name = "loop"
obj.data.name = "loop"
bpy.ops.mesh.extrude_region_move(MESH_OT_extrude_region={"use_normal_flip":False, "mirror":False}, TRANSFORM_OT_translate={"value":(5, 0, 0), "orient_type":'VIEW', "orient_matrix_type":'VIEW', "constraint_axis":(True, False, False), "mirror":False, "use_proportional_edit":False, "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "use_proportional_connected":False, "use_proportional_projected":False, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "cursor_transform":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False, "use_accurate":False})
bpy.ops.mesh.select_all(action='INVERT')
bpy.ops.object.vertex_group_add()
bpy.ops.object.vertex_group_select()
bpy.ops.object.vertex_group_assign()
bpy.ops.object.skin_root_mark()
bpy.ops.mesh.select_all(action='INVERT')
bpy.ops.object.editmode_toggle()
for x in bpy.context.object.material_slots: #For all of the materials in the selected object:
bpy.context.object.active_material_index = 0 #select the top material
bpy.ops.object.material_slot_remove() #delete it
#changing colour of tool
activeObject = bpy.context.active_object #Set active object to variable
mat = bpy.data.materials.new(name="MaterialName") #set new material to variable
activeObject.data.materials.append(mat) #add the material to the object
bpy.context.object.active_material.diffuse_color = (0.121583, 0.144091, 0.8, 0.729885)
bpy.ops.object.select_all(action='DESELECT') #deselect all object
bpy.data.objects['loop'].select_set(True)
obj = bpy.context.window.scene.objects['loop']
bpy.context.view_layer.objects.active = obj
bpy.ops.object.editmode_toggle()
#set parameters
bpy.ops.object.modifier_add(type='SUBSURF')
bpy.context.object.modifiers["Subdivision"].levels = 3
bpy.ops.object.modifier_add(type='SHRINKWRAP')
bpy.context.object.modifiers["Shrinkwrap"].target = trim_cut_object
bpy.context.object.modifiers["Shrinkwrap"].wrap_method = 'NEAREST_SURFACEPOINT'
bpy.context.object.modifiers["Shrinkwrap"].wrap_mode = 'ABOVE_SURFACE'
bpy.context.object.modifiers["Shrinkwrap"].offset = 0.2
bpy.context.object.modifiers["Shrinkwrap"].show_on_cage = True
bpy.context.object.modifiers["Subdivision"].show_on_cage = True
bpy.ops.object.modifier_add(type='SMOOTH')
bpy.context.object.modifiers["Smooth"].iterations = 4
bpy.context.object.modifiers["Smooth"].show_in_editmode = True
bpy.context.object.modifiers["Smooth"].show_on_cage = True
bpy.context.object.modifiers["Smooth"].factor = 0.5
bpy.ops.object.modifier_add(type='SKIN')
bpy.ops.transform.skin_resize(value=(0.8, 0.8, 0.8), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)
bpy.ops.wm.tool_set_by_id(name="builtin.select")
bpy.ops.ed.undo_push()
return {'FINISHED'}
class MESH_TO_trim_cut(bpy.types.Operator):
"""Trim Cut By Arch drawing"""
bl_idname = "mesh.trim_cut"
bl_label = "Trim Cut"
def execute(self, context):
bpy.ops.scene.saveblend(filename="preSeperation")
if context.mode == 'EDIT_MESH':
scene = context.scene
mytool = scene.my_tool
trim_cut_object_name = mytool.trim_cut_object_name
trim_cut_object = bpy.data.objects[trim_cut_object_name]
bpy.ops.wm.tool_set_by_id(name="builtin.select_box")
bpy.context.tool_settings.mesh_select_mode = (True , False , False)
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = trim_cut_object
trim_cut_object.select_set(True)
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.editmode_toggle()
bpy.ops.object.select_all(action='DESELECT') #deselect all object
bpy.data.objects['loop'].select_set(True)
obj = bpy.context.window.scene.objects['loop']
bpy.context.view_layer.objects.active = obj
bpy.context.scene.tool_settings.use_mesh_automerge = False
bpy.ops.ed.undo_push()
bpy.ops.object.modifier_remove(modifier="Skin")
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.subdivide(quadcorner='INNERVERT')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.convert(target='MESH')
bpy.ops.object.select_all(action='DESELECT') #deselect all object
bpy.data.objects['loop'].select_set(True)
obj = bpy.context.window.scene.objects['loop']
bpy.context.view_layer.objects.active = obj
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.remove_doubles(threshold=0.5)
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.editmode_toggle()
bpy.ops.object.duplicate_move()
bpy.context.object.name = "inside_loop"
bpy.ops.object.select_all(action='DESELECT') #deselect all object
bpy.data.objects['loop'].select_set(True)
obj = bpy.context.window.scene.objects['loop']
bpy.context.view_layer.objects.active = obj
bpy.ops.object.modifier_add(type='SHRINKWRAP')
bpy.context.object.modifiers["Shrinkwrap"].target = trim_cut_object
bpy.context.object.modifiers["Shrinkwrap"].wrap_mode = 'ABOVE_SURFACE'
bpy.context.object.modifiers["Shrinkwrap"].offset = 0.8
bpy.ops.object.modifier_add(type='SMOOTH')
bpy.context.object.modifiers["Smooth"].show_in_editmode = True
bpy.context.object.modifiers["Smooth"].iterations = 5
bpy.context.object.modifiers["Shrinkwrap"].show_on_cage = True
bpy.context.object.modifiers["Smooth"].show_on_cage = True
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.looptools_space(influence=100, input='selected', interpolation='cubic', lock_x=False, lock_y=False, lock_z=False)
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.editmode_toggle()
bpy.ops.object.convert(target='MESH')
bpy.ops.object.modifier_add(type='SUBSURF')
bpy.ops.object.modifier_add(type='SHRINKWRAP')
bpy.context.object.modifiers["Shrinkwrap"].target = trim_cut_object
bpy.context.object.modifiers["Shrinkwrap"].wrap_mode = 'ABOVE_SURFACE'
bpy.context.object.modifiers["Shrinkwrap"].show_on_cage = True
bpy.context.object.modifiers["Shrinkwrap"].offset = 0.4
bpy.ops.object.modifier_add(type='SMOOTH')
bpy.context.object.modifiers["Smooth"].iterations = 5
bpy.context.object.modifiers["Smooth"].show_in_editmode = True
bpy.context.object.modifiers["Smooth"].show_on_cage = True
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.looptools_space(influence=100, input='selected', interpolation='cubic', lock_x=False, lock_y=False, lock_z=False)
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.editmode_toggle()
bpy.ops.object.convert(target='MESH')
#loop inside step 2
bpy.ops.object.select_all(action='DESELECT') #deselect all object
bpy.data.objects['inside_loop'].select_set(True)
obj = bpy.context.window.scene.objects['inside_loop']
bpy.context.view_layer.objects.active = obj
bpy.ops.object.modifier_add(type='SHRINKWRAP')
bpy.context.object.modifiers["Shrinkwrap"].target = trim_cut_object
bpy.context.object.modifiers["Shrinkwrap"].wrap_mode = 'ABOVE_SURFACE'
bpy.context.object.modifiers["Shrinkwrap"].show_on_cage = True
bpy.context.object.modifiers["Shrinkwrap"].offset = -2.3
bpy.ops.object.modifier_add(type='CORRECTIVE_SMOOTH')
bpy.context.object.modifiers["CorrectiveSmooth"].show_in_editmode = True
bpy.context.object.modifiers["CorrectiveSmooth"].show_on_cage = True
bpy.context.object.modifiers["CorrectiveSmooth"].iterations = 50
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.looptools_space(influence=100, input='selected', interpolation='cubic', lock_x=False, lock_y=False, lock_z=False)
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.editmode_toggle()
bpy.ops.object.convert(target='MESH')
bpy.ops.object.modifier_add(type='SHRINKWRAP')
bpy.context.object.modifiers["Shrinkwrap"].target = trim_cut_object
bpy.context.object.modifiers["Shrinkwrap"].wrap_mode = 'ABOVE_SURFACE'
bpy.context.object.modifiers["Shrinkwrap"].offset = -1.2
bpy.ops.object.modifier_add(type='SMOOTH')
bpy.context.object.modifiers["Smooth"].iterations = 5
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.looptools_space(influence=100, input='selected', interpolation='cubic', lock_x=False, lock_y=False, lock_z=False)
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.editmode_toggle()
bpy.ops.object.convert(target='MESH')
bpy.ops.object.select_all(action='DESELECT') #deselect all object
bpy.data.objects['loop'].select_set(True)
obj = bpy.context.window.scene.objects['loop']
bpy.context.view_layer.objects.active = obj
bpy.data.objects['inside_loop'].select_set(True)
obj = bpy.context.window.scene.objects['inside_loop']