-
Notifications
You must be signed in to change notification settings - Fork 75
/
contour_curves.py
1156 lines (1008 loc) · 42.3 KB
/
contour_curves.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
# SPDX-FileCopyrightText: 2017 Alessandro Zomparelli
#
# SPDX-License-Identifier: GPL-2.0-or-later
import bpy, bmesh, os
import numpy as np
import math, timeit, time
from math import pi
from mathutils import Vector
from numpy import *
from bpy.types import (
Operator,
Panel,
PropertyGroup,
)
from bpy.props import (
BoolProperty,
EnumProperty,
FloatProperty,
IntProperty,
StringProperty,
FloatVectorProperty,
IntVectorProperty,
PointerProperty
)
from .utils import *
def anim_contour_curves(self, context):
ob = context.object
props = ob.tissue_contour_curves
if not (ob.tissue.bool_lock or ob.tissue.bool_hold):
#try:
props.object.name
bpy.ops.object.tissue_update_contour_curves()
#except: pass
class tissue_contour_curves_prop(PropertyGroup):
object : PointerProperty(
type=bpy.types.Object,
name="Object",
description="Source object",
update = anim_contour_curves
)
use_modifiers : BoolProperty(
name="Use Modifiers", default=True,
description="Apply all the modifiers",
update = anim_contour_curves
)
variable_bevel : BoolProperty(
name="Variable Bevel", default=False,
description="Variable Bevel",
update = anim_contour_curves
)
min_value : FloatProperty(
name="Offset Value", default=0., #soft_min=0, soft_max=1,
description="Offset contouring values",
update = anim_contour_curves
)
range_value : FloatProperty(
name="Range Values", default=100, #soft_min=0, soft_max=1,
description="Maximum range of contouring values",
update = anim_contour_curves
)
n_curves : IntProperty(
name="Curves", default=1000, soft_min=1, soft_max=200,
description="Number of Contour Curves",
update = anim_contour_curves
)
in_displace : FloatProperty(
name="Displace A", default=0, soft_min=-10, soft_max=10,
description="Pattern displace strength",
update = anim_contour_curves
)
out_displace : FloatProperty(
name="Displace B", default=2, soft_min=-10, soft_max=10,
description="Pattern displace strength",
update = anim_contour_curves
)
in_steps : IntProperty(
name="Steps A", default=1, min=0, soft_max=10,
description="Number of layers to move inwards",
update = anim_contour_curves
)
out_steps : IntProperty(
name="Steps B", default=1, min=0, soft_max=10,
description="Number of layers to move outwards",
update = anim_contour_curves
)
displace_x : BoolProperty(
name="Use X", default=True,
description="Displace along X axis",
update = anim_contour_curves
)
displace_y : BoolProperty(
name="Use Y", default=True,
description="Displace along Y axis",
update = anim_contour_curves
)
displace_z : BoolProperty(
name="Use Z", default=True,
description="Displace along Z axis",
update = anim_contour_curves
)
merge : BoolProperty(
name="Merge Vertices", default=True,
description="Merge points",
update = anim_contour_curves
)
merge_thres : FloatProperty(
name="Merge Threshold", default=0.01, min=0, soft_max=1,
description="Minimum Curve Radius",
update = anim_contour_curves
)
bevel_depth : FloatProperty(
name="Bevel Depth", default=0, min=0, soft_max=1,
description="",
update = anim_contour_curves
)
min_bevel_depth : FloatProperty(
name="Min Bevel Depth", default=0.05, min=0, soft_max=1,
description="",
update = anim_contour_curves
)
max_bevel_depth : FloatProperty(
name="Max Bevel Depth", default=0.20, min=0, soft_max=1,
description="",
update = anim_contour_curves
)
remove_open_curves : BoolProperty(
name="Remove Open Curves", default=False,
description="Remove Open Curves",
update = anim_contour_curves
)
vertex_group_pattern : StringProperty(
name="Displace", default='',
description="Vertex Group used for pattern displace",
update = anim_contour_curves
)
vertex_group_bevel : StringProperty(
name="Bevel", default='',
description="Variable Bevel depth",
update = anim_contour_curves
)
object_name : StringProperty(
name="Active Object", default='',
description="",
update = anim_contour_curves
)
vertex_group_contour : StringProperty(
name="Contour", default="",
description="Vertex Group used for contouring",
update = anim_contour_curves
)
clean_distance : FloatProperty(
name="Clean Distance", default=0.005, min=0, soft_max=10,
description="Remove short segments",
update = anim_contour_curves
)
spiralized: BoolProperty(
name='Spiralized', default=False,
description='Create a Spiral Contour. Works better with dense meshes.',
update = anim_contour_curves
)
spiral_axis: FloatVectorProperty(
name="Spiral Axis", default=(0,0,1),
description="Axis of the Spiral (in local coordinates)",
update = anim_contour_curves
)
spiral_rotation : FloatProperty(
name="Spiral Rotation", default=0, min=0, max=2*pi,
description="",
update = anim_contour_curves
)
contour_mode : EnumProperty(
items=(
('VECTOR', "Vector", "Orient the Contour to a given vector starting from the origin of the object"),
('OBJECT', "Object", "Orient the Contour to a target object's Z"),
('WEIGHT', "Weight", "Contour based on a Vertex Group"),
('ATTRIBUTE', "Attribute", "Contour based on an Attribute (Vertex > Float)"),
('GEODESIC', "Geodesic Distance", "Contour based on the geodesic distance from the chosen vertices"),
('TOPOLOGY', "Topology Distance", "Contour based on the topology distance from the chosen vertices")
),
default='VECTOR',
name="Mode used for the Contour Curves",
update = anim_contour_curves
)
contour_vector : FloatVectorProperty(
name='Vector', description='Constant Vector', default=(0.0, 0.0, 1.0),
update = anim_contour_curves
)
contour_vector_object : PointerProperty(
type=bpy.types.Object,
name="",
description="Target Object",
update = anim_contour_curves
)
contour_offset : FloatProperty(
name="Offset", default=0.05, min=0.000001, soft_min=0.01, soft_max=10,
description="Contour offset along the Vector",
update = anim_contour_curves
)
seeds_mode : EnumProperty(
items=(
('BOUND', "Boundary Edges", "Compute the distance starting from the boundary edges"),
('SHARP', "Sharp Edges", "Compute the distance starting from the sharp edges"),
('WEIGHT', "Weight", "Compute the distance starting from the selected vertex group")
),
default='BOUND',
name="Seeds used for computing the distance",
update = anim_contour_curves
)
vertex_group_seed : StringProperty(
name="Seeds", default="",
description="Vertex Group used for computing the distance",
update = anim_contour_curves
)
spline_type : EnumProperty(
items=(
('POLY', "Poly", "Generate Poly curves"),
('NURBS', "NURBS", "Generate NURBS curves")
),
default='POLY',
name="Spline type",
update = anim_contour_curves
)
contour_attribute : StringProperty(
name="Contour Attribute", default='',
description="Vertex > Float attribute used for contouring",
update = anim_contour_curves
)
class tissue_weight_contour_curves_pattern(Operator):
bl_idname = "object.tissue_weight_contour_curves_pattern"
bl_label = "Contour Curves"
bl_description = ("")
bl_options = {'REGISTER', 'UNDO'}
object : StringProperty(
name="Object",
description="Source object",
default = ""
)
use_modifiers : BoolProperty(
name="Use Modifiers", default=True,
description="Apply all the modifiers"
)
variable_bevel : BoolProperty(
name="Variable Bevel", default=False,
description="Variable Bevel"
)
min_value : FloatProperty(
name="Offset Value", default=0.,
description="Offset contouring values"
)
range_value : FloatProperty(
name="Range Values", default=100,
description="Maximum range of contouring values"
)
n_curves : IntProperty(
name="Curves", default=1000, soft_min=1, soft_max=200,
description="Number of Contour Curves"
)
min_rad = 1
max_rad = 1
in_displace : FloatProperty(
name="Displace A", default=0, soft_min=-10, soft_max=10,
description="Pattern displace strength"
)
out_displace : FloatProperty(
name="Displace B", default=2, soft_min=-10, soft_max=10,
description="Pattern displace strength"
)
in_steps : IntProperty(
name="Steps A", default=1, min=0, soft_max=10,
description="Number of layers to move inwards"
)
out_steps : IntProperty(
name="Steps B", default=1, min=0, soft_max=10,
description="Number of layers to move outwards"
)
displace_x : BoolProperty(
name="Use X", default=True,
description="Displace along X axis"
)
displace_y : BoolProperty(
name="Use Y", default=True,
description="Displace along Y axis"
)
displace_z : BoolProperty(
name="Use Z", default=True,
description="Displace along Z axis"
)
merge : BoolProperty(
name="Merge Vertices", default=True,
description="Merge points"
)
merge_thres : FloatProperty(
name="Merge Threshold", default=0.01, min=0, soft_max=1,
description="Minimum Curve Radius"
)
bevel_depth : FloatProperty(
name="Bevel Depth", default=0, min=0, soft_max=1,
description=""
)
min_bevel_depth : FloatProperty(
name="Min Bevel Depth", default=0.05, min=0, soft_max=1,
description=""
)
max_bevel_depth : FloatProperty(
name="Max Bevel Depth", default=0.20, min=0, soft_max=1,
description=""
)
remove_open_curves : BoolProperty(
name="Remove Open Curves", default=False,
description="Remove Open Curves"
)
vertex_group_pattern : StringProperty(
name="Displace", default='',
description="Vertex Group used for pattern displace"
)
vertex_group_bevel : StringProperty(
name="Bevel", default='',
description="Variable Bevel depth"
)
object_name : StringProperty(
name="Active Object", default='',
description=""
)
contour_attribute : StringProperty(
name="Contour Attribute", default='',
description="Vertex > Float attribute used for contouring"
)
try: vg_name = bpy.context.object.vertex_groups.active.name
except: vg_name = ''
vertex_group_contour : StringProperty(
name="Contour", default=vg_name,
description="Vertex Group used for contouring"
)
clean_distance : FloatProperty(
name="Clean Distance", default=0.005, min=0, soft_max=10,
description="Remove short segments"
)
spiralized: BoolProperty(
name='Spiralized', default=False,
description='Create a Spiral Contour. Works better with dense meshes.'
)
spiral_axis: FloatVectorProperty(
name="Spiral Axis", default=(0,0,1),
description="Axis of the Spiral (in local coordinates)"
)
spiral_rotation : FloatProperty(
name="Spiral Rotation", default=0, min=0, max=2*pi,
description=""
)
bool_hold : BoolProperty(
name="Hold",
description="Wait...",
default=False
)
contour_mode : EnumProperty(
items=(
('VECTOR', "Vector", "Orient the Contour to a given vector starting from the origin of the object"),
('OBJECT', "Object", "Orient the Contour to a target object's Z"),
('WEIGHT', "Weight", "Contour based on a Vertex Group"),
('ATTRIBUTE', "Attribute", "Contour based on an Attribute (Vertex > Float)"),
('GEODESIC', "Geodesic Distance", "Contour based on the geodesic distance from the chosen vertices"),
('TOPOLOGY', "Topology Distance", "Contour based on the topology distance from the chosen vertices")
),
default='VECTOR',
name="Mode used for the Contour Curves"
)
contour_vector : FloatVectorProperty(
name='Vector', description='Constant Vector', default=(0.0, 0.0, 1.0)
)
contour_vector_object : StringProperty(
name="Object",
description="Target object",
default = ""
)
contour_offset : FloatProperty(
name="Offset", default=0.05, min=0.000001, soft_min=0.01, soft_max=10,
description="Contour offset along the Vector"
)
seeds_mode : EnumProperty(
items=(
('BOUND', "Boundary Edges", "Compute the distance starting from the boundary edges"),
('SHARP', "Sharp Edges", "Compute the distance starting from the sharp edges"),
('WEIGHT', "Weight", "Compute the distance starting from the selected vertex group")
),
default='BOUND',
name="Seeds used for computing the distance"
)
vertex_group_seed : StringProperty(
name="Seeds", default=vg_name,
description="Vertex Group used for computing the distance"
)
spline_type : EnumProperty(
items=(
('POLY', "Poly", "Generate Poly curves"),
('NURBS', "NURBS", "Generate NURBS curves")
),
default='POLY',
name="Spline type"
)
def invoke(self, context, event):
self.object = context.object.name
return context.window_manager.invoke_props_dialog(self, width=250)
def draw(self, context):
ob = context.object
ob0 = bpy.data.objects[self.object]
if self.contour_mode == 'WEIGHT':
try:
if self.vertex_group_contour not in [vg.name for vg in ob.vertex_groups]:
self.vertex_group_contour = ob.vertex_groups.active.name
except:
self.contour_mode == 'VECTOR'
if not self.bool_hold:
self.object = ob.name
self.bool_hold = True
layout = self.layout
col = layout.column(align=True)
col.prop(self, "use_modifiers")
col.label(text="Contour Curves:")
row = col.row()
row.prop(self, "spline_type", icon='NONE', expand=True,
slider=True, toggle=False, icon_only=False, event=False,
full_event=False, emboss=True, index=-1)
col.separator()
col.prop(self, "contour_mode", text="Mode")
if self.contour_mode == 'VECTOR':
row = col.row()
row.prop(self,'contour_vector')
elif self.contour_mode == 'WEIGHT':
col.prop_search(self, 'vertex_group_contour', ob, "vertex_groups", text='Group')
elif self.contour_mode == 'ATTRIBUTE':
col.prop_search(self, 'contour_attribute', ob0.data, "attributes", text='Attribute')
is_attribute = True
if self.contour_attribute in ob0.data.attributes:
attr = ob0.data.attributes[self.contour_attribute]
is_attribute = attr.data_type == 'FLOAT' and attr.domain == 'POINT'
else:
is_attribute = False
if not is_attribute:
col.label(text="Please select a (Vertex > Float) Attribute for contouring.", icon='ERROR')
elif self.contour_mode in ('TOPOLOGY','GEODESIC'):
col.prop(self, "seeds_mode", text="Seeds")
if self.seeds_mode == 'WEIGHT':
col.prop_search(self, 'vertex_group_seed', ob, "vertex_groups", text='Group')
elif self.contour_mode == 'OBJECT':
col.prop_search(self, "contour_vector_object", context.scene, "objects", text='Object')
col.separator()
if self.contour_mode == 'OBJECT':
col.prop(self,'contour_offset')
col.prop(self,'n_curves', text='Max Curves')
elif self.contour_mode in ('VECTOR', 'GEODESIC', 'ATTRIBUTE'):
col.prop(self,'contour_offset')
row = col.row(align=True)
row.prop(self,'min_value')
row.prop(self,'range_value')
col.prop(self,'n_curves', text='Max Curves')
elif self.contour_mode in ('TOPOLOGY', 'WEIGHT'):
row = col.row(align=True)
row.prop(self,'min_value')
row.prop(self,'range_value')
col.prop(self,'n_curves')
col.separator()
col.label(text='Curves Bevel:')
col.prop(self,'variable_bevel')
row = col.row(align=True)
row.prop(self,'min_bevel_depth')
row.prop(self,'max_bevel_depth')
row2 = col.row(align=True)
row2.prop_search(self, 'vertex_group_bevel', ob, "vertex_groups", text='')
if not self.variable_bevel:
row.enabled = row2.enabled = False
col.separator()
col.label(text="Displace Pattern:")
col.prop_search(self, 'vertex_group_pattern', ob, "vertex_groups", text='')
if self.vertex_group_pattern != '':
col.separator()
row = col.row(align=True)
row.prop(self,'in_steps')
row.prop(self,'out_steps')
row = col.row(align=True)
row.prop(self,'in_displace')
row.prop(self,'out_displace')
col.separator()
row = col.row(align=True)
row.label(text="Axis")
row.prop(self,'displace_x', text="X", toggle=1)
row.prop(self,'displace_y', text="Y", toggle=1)
row.prop(self,'displace_z', text="Z", toggle=1)
col.separator()
col.label(text='Clean Curves:')
col.prop(self,'clean_distance')
col.prop(self,'remove_open_curves')
def execute(self, context):
ob0 = bpy.context.object
self.object_name = "Contour Curves"
# Check if existing object with same name
names = [o.name for o in bpy.data.objects]
if self.object_name in names:
count_name = 1
while True:
test_name = self.object_name + '.{:03d}'.format(count_name)
if not (test_name in names):
self.object_name = test_name
break
count_name += 1
if bpy.ops.object.select_all.poll():
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')
bool_update = False
if context.object == ob0:
auto_layer_collection()
curve = bpy.data.curves.new(self.object_name,'CURVE')
new_ob = bpy.data.objects.new(self.object_name,curve)
bpy.context.collection.objects.link(new_ob)
bpy.context.view_layer.objects.active = new_ob
if bpy.ops.object.select_all.poll():
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')
new_ob.select_set(True)
else:
new_ob = context.object
bool_update = True
# Store parameters
props = new_ob.tissue_contour_curves
new_ob.tissue.bool_hold = True
if self.object in bpy.data.objects.keys():
props.object = bpy.data.objects[self.object]
props.use_modifiers = self.use_modifiers
props.variable_bevel = self.variable_bevel
props.min_value = self.min_value
props.range_value = self.range_value
props.n_curves = self.n_curves
props.in_displace = self.in_displace
props.out_displace = self.out_displace
props.in_steps = self.in_steps
props.out_steps = self.out_steps
props.displace_x = self.displace_x
props.displace_y = self.displace_y
props.displace_z = self.displace_z
props.merge = self.merge
props.merge_thres = self.merge_thres
props.bevel_depth = self.bevel_depth
props.min_bevel_depth = self.min_bevel_depth
props.max_bevel_depth = self.max_bevel_depth
props.remove_open_curves = self.remove_open_curves
props.vertex_group_pattern = self.vertex_group_pattern
props.vertex_group_bevel = self.vertex_group_bevel
props.object_name = self.object_name
props.vertex_group_contour = self.vertex_group_contour
props.clean_distance = self.clean_distance
props.spiralized = self.spiralized
props.spiral_axis = self.spiral_axis
props.spiral_rotation = self.spiral_rotation
props.contour_mode = self.contour_mode
if self.contour_vector_object in bpy.data.objects.keys():
props.contour_vector_object = bpy.data.objects[self.contour_vector_object]
props.contour_vector = self.contour_vector
props.contour_offset = self.contour_offset
props.seeds_mode = self.seeds_mode
props.vertex_group_seed = self.vertex_group_seed
props.spline_type = self.spline_type
props.contour_attribute = self.contour_attribute
new_ob.tissue.bool_hold = False
new_ob.tissue.tissue_type = 'CONTOUR_CURVES'
try: bpy.ops.object.tissue_update_contour_curves()
except RuntimeError as e:
print("no update")
bpy.data.objects.remove(new_ob)
remove_temp_objects()
self.report({'ERROR'}, str(e))
return {'CANCELLED'}
if not bool_update:
self.object_name = new_ob.name
#self.working_on = self.object_name
new_ob.location = ob0.location
new_ob.matrix_world = ob0.matrix_world
# Assign collection of the base object
old_coll = new_ob.users_collection
if old_coll != ob0.users_collection:
for c in old_coll:
c.objects.unlink(new_ob)
for c in ob0.users_collection:
c.objects.link(new_ob)
context.view_layer.objects.active = new_ob
return {'FINISHED'}
class tissue_update_contour_curves(Operator):
bl_idname = "object.tissue_update_contour_curves"
bl_label = "Update Contour Curves"
bl_description = ("Update a previously generated Contour Curves object")
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
ob = context.object
props = ob.tissue_contour_curves
_ob0 = props.object
n_curves = props.n_curves
tt0 = time.time()
tt1 = time.time()
tissue_time(None,'Tissue: Contour Curves of "{}"...'.format(ob.name), levels=0)
ob0 = convert_object_to_mesh(_ob0, apply_modifiers=props.use_modifiers)
ob0.name = "_tissue_tmp_ob0"
me0 = ob0.data
# generate new bmesh
bm = bmesh.new()
bm.from_mesh(me0)
n_verts = len(bm.verts)
vertices, normals = get_vertices_and_normals_numpy(me0)
if props.contour_mode == 'OBJECT':
try:
vec_ob = props.contour_vector_object
vec_ob_name = vec_ob.name
except:
bm.free()
bpy.data.objects.remove(ob0)
self.report({'ERROR'}, "Please select an target Object")
return {'CANCELLED'}
tt1 = tissue_time(tt1, "Load objects", levels=1)
# store weight values
if props.contour_mode in ('VECTOR','OBJECT'):
ob0_matrix = np.matrix(ob0.matrix_world.to_3x3().transposed())
global_verts = np.matmul(vertices,ob0_matrix)
global_verts += np.array(ob0.matrix_world.translation)
if props.contour_mode == 'OBJECT' and props.contour_vector_object:
vec_ob = props.contour_vector_object
global_verts -= np.array(vec_ob.matrix_world.translation)
vec_ob_matrix = np.matrix(vec_ob.matrix_world.to_3x3().inverted().transposed())
global_verts = np.matmul(global_verts,vec_ob_matrix)
weight = global_verts[:,2].A1
elif props.contour_mode == 'VECTOR':
vec = np.array(props.contour_vector)
vec_len = np.linalg.norm(vec)
if vec_len == 0:
vec = np.array((0,0,1))
vec_len = 1
else:
vec /= vec_len
vec_len = 1
global_verts = global_verts.A
projected_verts = global_verts * vec
projected_verts = np.sum(projected_verts,axis=1)[:,np.newaxis]
weight = projected_verts.reshape((-1))
elif props.contour_mode == 'WEIGHT':
try:
weight = get_weight_numpy(ob0.vertex_groups[props.vertex_group_contour], len(me0.vertices))
except:
bm.free()
bpy.data.objects.remove(ob0)
self.report({'ERROR'}, "Please select a Vertex Group for contouring")
return {'CANCELLED'}
elif props.contour_mode == 'ATTRIBUTE':
if props.contour_attribute in me0.attributes:
weight = [0]*n_verts
me0.attributes[props.contour_attribute].data.foreach_get('value',weight)
weight = np.array(weight)
else:
bm.free()
bpy.data.objects.remove(ob0)
self.report({'ERROR'}, "Please select a (Vertex > Float) Attribute for contouring")
return {'CANCELLED'}
elif props.contour_mode in ('GEODESIC','TOPOLOGY'):
cancel = False
weight = [None]*n_verts
seed_verts = []
bm.verts.ensure_lookup_table()
if props.seeds_mode == 'BOUND':
for v in bm.verts:
if v.is_boundary:
seed_verts.append(v)
weight[v.index] = 0
if props.seeds_mode == 'SHARP':
for e, bme in zip(me0.edges, bm.edges):
if e.use_edge_sharp:
seed_verts.append(bme.verts[0])
seed_verts.append(bme.verts[1])
seed_verts = list(set(seed_verts))
if len(seed_verts) == 0: cancel = True
for i in [v.index for v in seed_verts]:
weight[i] = 0
if props.seeds_mode == 'WEIGHT':
try:
seeds = get_weight_numpy(ob0.vertex_groups[props.vertex_group_seed], len(me0.vertices))
except:
bm.free()
bpy.data.objects.remove(ob0)
self.report({'ERROR'}, "Please select a Vertex Group as seed")
return {'CANCELLED'}
for i,v in enumerate(bm.verts):
if seeds[i]>0.999999:
seed_verts.append(v)
weight[i] = 0
if cancel or len(seed_verts)==0:
bm.free()
bpy.data.objects.remove(ob0)
self.report({'ERROR'}, "No seed vertices found")
return {'CANCELLED'}
weight = fill_neighbors_attribute(seed_verts, weight, props.contour_mode)
weight = np.array(weight)
print(weight[weight==None])
weight[weight==None] = 0
print(weight[weight==None])
try:
pattern_weight = get_weight_numpy(ob0.vertex_groups[props.vertex_group_pattern], len(me0.vertices))
except:
#self.report({'WARNING'}, "There is no Vertex Group assigned to the pattern displace")
pattern_weight = np.zeros(len(me0.vertices))
weight_bevel = False
if props.variable_bevel:
try:
bevel_weight = get_weight_numpy(ob0.vertex_groups[props.vertex_group_bevel], len(me0.vertices))
weight_bevel = True
except:
bevel_weight = np.ones(len(me0.vertices))
else:
bevel_weight = np.ones(len(me0.vertices))
total_verts = np.zeros((0,3))
total_radii = np.zeros((0,1))
total_edges_index = np.zeros((0)).astype('int')
total_segments = []# np.array([])
radius = []
tt1 = tissue_time(tt1, "Compute values", levels=1)
# start iterate contours levels
filtered_edges = get_edges_id_numpy(me0)
min_value = props.min_value
max_value = props.min_value + props.range_value
if props.contour_mode in ('VECTOR','OBJECT','GEODESIC','ATTRIBUTE'):
delta_iso = props.contour_offset
n_curves = min(int((np.max(weight)-props.min_value)/delta_iso)+1, props.n_curves)
else:
if n_curves == 1:
delta_iso = props.range_value/2
else:
delta_iso = props.range_value/(n_curves-1)
if props.contour_mode == 'TOPOLOGY':
weight = weight/np.max(weight)
if False:
edges_verts = get_attribute_numpy(me0.edges,"vertices",mult=2).astype('int')
edges_vec = vertices[edges_verts[:,0]]-vertices[edges_verts[:,1]]
#edges_vec = global_verts[edges_verts[:,0]]-global_verts[edges_verts[:,1]]
edges_length = np.linalg.norm(edges_vec,axis=1)
edges_vec /= edges_length[:,np.newaxis]
edges_dw = np.abs(weight[edges_verts[:,0]]-weight[edges_verts[:,1]])
edges_bevel = delta_iso*edges_length/edges_dw/2*0 + 1
'''
# numpy method
faces_n_verts = get_attribute_numpy(me0.polygons, attribute='loop_total').astype('int')
faces_verts = get_attribute_numpy(me0.polygons, attribute='vertices', size=np.sum(faces_n_verts)).astype('int')
faces_weight = weight[faces_verts]
faces_weight = np.split(faces_weight, np.cumsum(faces_n_verts)[:-1])
'''
faces_weight = [np.array([weight[v] for v in p.vertices]) for p in me0.polygons]
try:
fw_min = np.min(faces_weight, axis=1)
fw_max = np.max(faces_weight, axis=1)
except:
# necessary for irregular meshes
fw_min = np.array([min(fw) for fw in faces_weight])
fw_max = np.array([max(fw) for fw in faces_weight])
bm_faces = np.array(bm.faces)
tt1 = tissue_time(tt1, "Compute face values", levels=1)
for c in range(n_curves):
if delta_iso:
iso_val = c*delta_iso + min_value
else:
iso_val = min_value + range_value/2
if iso_val > max_value: break
# remove passed faces
bool_mask = iso_val <= fw_max
bm_faces = bm_faces[bool_mask]
fw_min = fw_min[bool_mask]
fw_max = fw_max[bool_mask]
# mask faces
bool_mask = fw_min <= iso_val
faces_mask = bm_faces[bool_mask]
count = len(total_verts)
if not weight_bevel and props.variable_bevel:
bevel_weight = np.full(n_verts, c/n_curves)
new_filtered_edges, edges_index, verts, bevel = contour_edges_pattern(props, c, len(total_verts), iso_val, vertices, normals, filtered_edges, weight, pattern_weight, bevel_weight)
#bevel = edges_bevel[edges_index][:,np.newaxis]
if len(edges_index) > 0:
if props.variable_bevel and props.max_bevel_depth != props.min_bevel_depth and False:
#min_radius = min(props.min_bevel_depth, props.max_bevel_depth)
#max_radius = max(props.min_bevel_depth, props.max_bevel_depth)
min_radius = props.min_bevel_depth
max_radius = props.max_bevel_depth
min_radius = min_radius / max(0.0001,max_radius)
radii = min_radius + bevel*(1 - min_radius)
else:
radii = bevel
else:
continue
if verts[0,0] == None: continue
else: filtered_edges = new_filtered_edges
edges_id = {}
for i, id in enumerate(edges_index): edges_id[id] = i + count
if len(verts) == 0: continue
# finding segments
segments = []
for f in faces_mask:
seg = []
for e in f.edges:
try:
#seg.append(new_ids[np.where(edges_index == e.index)[0][0]])
seg.append(edges_id[e.index])
if len(seg) == 2:
segments.append(seg)
seg = []
except: pass
total_segments = total_segments + segments
total_verts = np.concatenate((total_verts, verts))
total_radii = np.concatenate((total_radii, radii))
total_edges_index = np.concatenate((total_edges_index, edges_index))
tt1 = tissue_time(tt1, "Compute curves", levels=1)
if len(total_segments) > 0:
ordered_points, ordered_points_edge_id = find_curves_attribute(total_segments, len(total_verts), total_edges_index)
total_tangents = np.zeros((len(total_verts),3))
for curve in ordered_points:
np_curve = np.array(curve).astype('int')
curve_pts = np.array(total_verts[np_curve], dtype=np.float64)
tangents = np.roll(curve_pts,1) - np.roll(curve_pts,-1)
tangents /= np.linalg.norm(tangents,axis=1)[:,np.newaxis]
total_tangents[curve] = tangents
step_time = timeit.default_timer()
ob.data.splines.clear()
if props.variable_bevel:# and not weight_bevel:
total_radii = np.interp(total_radii, (total_radii.min(), total_radii.max()), (props.min_bevel_depth, props.max_bevel_depth))
ob.data = curve_from_pydata(total_verts, total_radii, ordered_points, ob0.name + '_ContourCurves', props.remove_open_curves, merge_distance=props.clean_distance, only_data=True, curve=ob.data, spline_type=props.spline_type)
#context.view_layer.objects.active = crv
if props.variable_bevel:
if not weight_bevel:
ob.data.bevel_depth = 1
else:
ob.data.bevel_depth = max(props.max_bevel_depth, props.min_bevel_depth)
tt1 = tissue_time(tt1, "Store curves data", levels=1)
else:
ob.data.splines.clear()
pass
bm.free()
for o in bpy.data.objects:
if '_tissue_tmp_' in o.name:
bpy.data.objects.remove(o)
tt0 = tissue_time(tt0, "Contour Curves", levels=0)
return {'FINISHED'}
class TISSUE_PT_contour_curves(Panel):
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "data"
bl_label = "Tissue Contour Curves"
bl_options = {'DEFAULT_CLOSED'}
@classmethod
def poll(cls, context):
try:
#bool_curve = context.object.tissue_to_curve.object != None
ob = context.object
return ob.type == 'CURVE' and ob.tissue.tissue_type == 'CONTOUR_CURVES'
except:
return False
def draw(self, context):
ob = context.object
props = ob.tissue_contour_curves
ob0 = bpy.data.objects[props.object.name]
layout = self.layout
#layout.use_property_split = True
#layout.use_property_decorate = False
col = layout.column(align=True)
row = col.row(align=True)
#col.operator("object.tissue_update_convert_to_curve", icon='FILE_REFRESH', text='Refresh')
row.operator("object.tissue_update_tessellate_deps", icon='FILE_REFRESH', text='Refresh') ####