-
Notifications
You must be signed in to change notification settings - Fork 1
/
geometry.py
1778 lines (1394 loc) · 52.2 KB
/
geometry.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 bmesh
from math import sqrt, radians, pi
from phaenotyp import basics, operators
from mathutils import Color, Vector, Matrix
c = Color()
# variable to pass all stuff that needs to be fixed
to_be_fixed = None
def viz_update(self, context):
'''
Triggers the update of the vizulisation.
This function is used to handle self and context.
:param self: Passed from the panel.
:param context: Passed from the panel.
'''
update_geometry_post()
def fh_update(self, context):
'''
Triggers the update from hull.
:param self: Passed from the panel.
:param context: Passed from the panel.
'''
scene = context.scene
phaenotyp = scene.phaenotyp
fh_methode = phaenotyp.fh_methode
# check if all objects are available
valid_objects = True
hull = scene.get("<Phaenotyp>fh_hull")
if not hull:
valid_objects = False
if fh_methode == "path":
path = scene.get("<Phaenotyp>fh_path")
if not path:
valid_objects = False
# run update
if valid_objects:
operators.from_hull()
def amount_of_loose_parts():
'''
Is returning the amount of loose parts.
:return total_vert_sel: The amount of loose parts as integer.
'''
obj = bpy.context.active_object
bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='VERT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_loose()
return obj.data.total_vert_sel
def amount_of_doubles():
'''
Is returning the amount vertices at same position with threshold of 0.001.
:return total_vert_sel: The amount of loose parts as integer.
'''
obj = bpy.context.active_object
vertices = obj.data.vertices
amount = 0
for vertex in vertices:
for other in vertices:
if vertex != other:
v_0 = vertex.co
v_1 = other.co
v = v_1 - v_0
dist = v.length
if v.length < 0.0001:
amount += 1
return amount
def triangulation():
'''
The function is iterating trough all faces and checks the amount of vertices.
This function is only working for triangulated faces and not edges.
:return triangulated: Bool True of triangulated or False if not.
'''
# get selected faces
obj = bpy.context.active_object
mode = bpy.context.object.mode
bpy.ops.object.mode_set(mode = 'OBJECT')
triangulated = True
for face in obj.data.polygons:
# check triangulation
if len(face.vertices) != 3:
# set False, if one face is not tri
triangulated = False
# go back to previous mode
bpy.ops.object.mode_set(mode = mode)
return triangulated
def amount_of_selected_faces():
'''
Is iterating trough all faces and check if the selction is True.
:return selected_faces: The amount of selected faces as integer.
'''
obj = bpy.context.active_object
bpy.ops.object.mode_set(mode = 'OBJECT')
selected_faces = []
for face in obj.data.polygons:
# collect all selected faces
if face.select == True:
selected_faces.append(face)
return len(selected_faces)
def parts():
'''
Get lists of indices from connected vertices
Is checking for the amount of parts in the mesh. Is based on the answer from BlackCutpoint
https://blender.stackexchange.com/questions/75332/how-to-find-the-number-of-loose-parts-with-blenders-python-api
:return parts: Is returning the parts as list of ids of vertices
'''
scene = bpy.context.scene
data = scene["<Phaenotyp>"]
obj = data["structure"]
parts = data["process"].get("parts")
if parts:
# only create parts if not available
pass
else:
mesh = obj.data
paths = {v.index:set() for v in mesh.vertices}
for e in mesh.edges:
paths[e.vertices[0]].add(e.vertices[1])
paths[e.vertices[1]].add(e.vertices[0])
parts_temp = []
while True:
try:
i=next(iter(paths.keys()))
except StopIteration:
break
part = {i}
cur = {i}
while True:
eligible = {sc for sc in cur if sc in paths}
if not eligible:
break
cur = {ve for sc in eligible for ve in paths[sc]}
part.update(cur)
for key in eligible: paths.pop(key)
parts_temp.append(part)
data["process"]["parts"] = {}
for id, part in enumerate(parts_temp):
entries = []
for entry in part:
entries.append(entry)
data["process"]["parts"][str(id)] = entries
parts = data["process"]["parts"]
return parts
def delete_selected_faces():
'''
Is handling the mode-switch and deletes all selected faces.
'''
obj = bpy.context.active_object
bpy.ops.object.mode_set(mode='OBJECT')
selected_faces = []
for face in obj.data.polygons:
# collect all selected faces
if face.select == True:
selected_faces.append(face)
bpy.ops.object.mode_set(mode = 'EDIT')
#support_ids = selected_faces[0].vertices
bpy.ops.mesh.delete(type='EDGE_FACE')
def volume(mesh):
'''
Volume of the structure via bmesh at current frame.
:param mesh: Mesh of the structure as basis.
:return frame_volume: Volume as float.
'''
bm = bmesh.new()
bm.from_mesh(mesh)
frame_volume = bm.calc_volume()
return frame_volume
def area(faces):
'''
Area of the frame as overall sum of faces. Users can delete faces to
influence this as fitness in ga.
:param faces: Faces of the mesh as basis.
:return frame_area: Area as float.
'''
frame_area = 0
for face in faces:
frame_area += face.area
return frame_area
def area_projected(face, vertices):
'''
Get projected area of a given face. Based on answer from Nikos Athanasiou:
https://stackoverflow.com/questions/24467972/calculate-area-of-polygon-given-x-y-coordinates
:param face: Face to work with.
:param vertices: Vertices of the face to work with.
:return area_projected: Projected area as float.
'''
vertex_ids = face.vertices
vertices_temp = []
for vertex_id in vertex_ids:
vertex = vertices[vertex_id]
vertices_temp.append(vertex.co)
n = len(vertices_temp)
a = 0.0
for i in range(n):
j = (i + 1) % n
v_i = vertices_temp[i]
v_j = vertices_temp[j]
a += v_i[0] * v_j[1]
a -= v_j[0] * v_i[1]
area_projected = abs(a) / 2.0
return area_projected
def perimeter(edge_keys, vertices):
'''
Get distances and perimeter of the given face.
:param edge_keys: Edge_keys from the face as list.
:param vertices: Vertices of the face.
:return distances: Distance as list of floats.
:return perimeter: Perimeter as float.
'''
distances = []
for edge_key in edge_keys:
vertex_0_id = edge_key[0]
vertex_1_id = edge_key[1]
vertex_0_co = vertices[vertex_0_id].co
vertex_1_co = vertices[vertex_1_id].co
dist_vector = vertex_0_co - vertex_1_co
dist = dist_vector.length
distances.append(dist)
perimeter = sum(distances)
return distances, perimeter
def rise(vertices):
'''
Rise of the structure as fitness. The function is iterating through
all vertices and returns the distance between the highest and lowest
point.
:param vertices: Vertices of the given structure.
:return frame_rise: Rise as float
'''
highest = 0
lowest = 0
for vertex in vertices:
z = vertex.co[2]
# find highest
if z > highest:
highest = z
# find lowest
if z < lowest:
lowest = z
frame_rise = highest-lowest
return frame_rise
def span(vertices, supports):
'''
Span of the structure as fitness. The function is iterating through
all vertices and returns the highest distance between all supports.
:param vertices: Vertices of the given structure.
:param supports: Supports of the given structure.
:return frame_span: Span as float
'''
highest = 0
for current in supports:
for other in supports:
if current != other:
current_co = vertices[int(current)].co
other_co = vertices[int(other)].co
dist_v = other_co - current_co
dist = dist_v.length
# find highest
if dist > highest:
highest = dist
frame_span = highest
return frame_span
def cantilever(vertices, supports):
# get cantilever of frame
# (lowest distance from all vertices to all supports)
highest = 0
for vertex in vertices:
to_closest_support = float('inf')
for support in supports:
vertex_co = vertex.co
support_co = vertices[int(support)].co
if vertex_co != support_co:
dist_v = support_co - vertex_co
dist = dist_v.length
# find highest
if dist < to_closest_support:
to_closest_support = dist
# find highest
if to_closest_support > highest:
highest = to_closest_support
frame_cantilever = highest
# return 0 if there is only one support
if frame_cantilever == float('inf'):
frame_cantilever = 0
return frame_cantilever
def set_shape_keys(shape_keys, chromosome):
for id, key in enumerate(shape_keys):
if id > 0: # to exlude basis
key.value = chromosome[id-1]
def create_supports(structure_obj, supports):
scene = bpy.context.scene
data = scene["<Phaenotyp>"]
scene_id = data["scene_id"]
mesh = bpy.data.meshes.new("<Phaenotyp>support_" + str(scene_id))
obj = bpy.data.objects.new(mesh.name, mesh)
col = bpy.data.collections.get("<Phaenotyp>" + str(scene_id))
col.objects.link(obj)
bpy.context.view_layer.objects.active = obj
# like suggested here by Gorgious and CodeManX:
# https://blender.stackexchange.com/questions/6155/how-to-convert-coordinates-from-vertex-to-world-space
mat = structure_obj.matrix_world
verts = []
edges = []
faces = []
structure_obj_vertices = structure_obj.data.vertices
len_verts = 0
for id, support in supports.items():
id = int(id)
# transfer parameters
loc_x = support[0]
loc_y = support[1]
loc_z = support[2]
rot_x = support[3]
rot_y = support[4]
rot_z = support[5]
# like suggested here by Gorgious and CodeManX:
# https://blender.stackexchange.com/questions/6155/how-to-convert-coordinates-from-vertex-to-world-space
v = mat @ structure_obj_vertices[id].co
x = v[0]
y = v[1]
z = v[2]
# to set position
offset = Vector((x, y, z))
# pyramide floor
v_0 = Vector((-0.1, 0.1,-0.1)) + offset
v_1 = Vector(( 0.1, 0.1,-0.1)) + offset
v_2 = Vector(( 0.1,-0.1,-0.1)) + offset
v_3 = Vector((-0.1,-0.1,-0.1)) + offset
v_4 = Vector(( 0.0, 0.0, 0.0)) + offset
verts.append(v_0)
verts.append(v_1)
verts.append(v_2)
verts.append(v_3)
verts.append(v_4)
edges.append([0+len_verts, 1+len_verts])
edges.append([1+len_verts, 2+len_verts])
edges.append([2+len_verts, 3+len_verts])
edges.append([3+len_verts, 0+len_verts])
edges.append([0+len_verts, 4+len_verts])
edges.append([1+len_verts, 4+len_verts])
edges.append([2+len_verts, 4+len_verts])
edges.append([3+len_verts, 4+len_verts])
# loc_x
lx = 1 if loc_x == True else 0
v_5 = Vector(( 0.0*lx, 0.0,-0.2*lx)) + offset
v_6 = Vector(( 0.2*lx, 0.0,-0.2*lx)) + offset
verts.append(v_5)
verts.append(v_6)
edges.append([5+len_verts, 6+len_verts])
# loc_y
ly = 1 if loc_y == True else 0
v_7 = Vector(( 0.0, 0.0*ly,-0.2*ly)) + offset
v_8 = Vector(( 0.0, 0.2*ly,-0.2*ly)) + offset
verts.append(v_7)
verts.append(v_8)
edges.append([7+len_verts, 8+len_verts])
# loc_z
lz = 1 if loc_z == True else 0
v_9 = Vector(( 0.0, 0.0,-0.2*lz)) + offset
v_10 = Vector(( 0.0, 0.0,-0.4*lz)) + offset
verts.append(v_9)
verts.append(v_10)
edges.append([9+len_verts, 10+len_verts])
# rot_x
rx = 1 if rot_x == True else 0
v_11 = Vector(( 0.20*rx, 0.02*rx,-0.22*rx)) + offset
v_12 = Vector(( 0.20*rx,-0.02*rx,-0.22*rx)) + offset
v_13 = Vector(( 0.20*rx,-0.02*rx,-0.18*rx)) + offset
v_14 = Vector(( 0.20*rx, 0.02*rx,-0.18*rx)) + offset
verts.append(v_11)
verts.append(v_12)
verts.append(v_13)
verts.append(v_14)
edges.append([11+len_verts, 12+len_verts])
edges.append([12+len_verts, 13+len_verts])
edges.append([13+len_verts, 14+len_verts])
edges.append([14+len_verts, 11+len_verts])
# rot_y
ry = 1 if rot_y == True else 0
v_15 = Vector(( 0.02*ry, 0.20*ry,-0.22*ry)) + offset
v_16 = Vector((-0.02*ry, 0.20*ry,-0.22*ry)) + offset
v_17 = Vector((-0.02*ry, 0.20*ry,-0.18*ry)) + offset
v_18 = Vector(( 0.02*ry, 0.20*ry,-0.18*ry)) + offset
verts.append(v_15)
verts.append(v_16)
verts.append(v_17)
verts.append(v_18)
edges.append([15+len_verts, 16+len_verts])
edges.append([16+len_verts, 17+len_verts])
edges.append([17+len_verts, 18+len_verts])
edges.append([18+len_verts, 15+len_verts])
# rot_z
rz = 1 if rot_z == True else 0
v_19 = Vector(( 0.02*rz, 0.02*rz,-0.40*rz)) + offset
v_20 = Vector((-0.02*rz, 0.02*rz,-0.40*rz)) + offset
v_21 = Vector((-0.02*rz,-0.02*rz,-0.40*rz)) + offset
v_22 = Vector(( 0.02*rz,-0.02*rz,-0.40*rz)) + offset
verts.append(v_19)
verts.append(v_20)
verts.append(v_21)
verts.append(v_22)
edges.append([19+len_verts, 20+len_verts])
edges.append([20+len_verts, 21+len_verts])
edges.append([21+len_verts, 22+len_verts])
edges.append([22+len_verts, 19+len_verts])
len_verts += 23
mesh.from_pydata(verts, edges, faces)
def create_members(structure_obj, members):
# create mesh and object
scene = bpy.context.scene
data = scene["<Phaenotyp>"]
scene_id = data["scene_id"]
mesh = bpy.data.meshes.new("<Phaenotyp>members_" + str(scene_id))
obj = bpy.data.objects.new(mesh.name, mesh)
col = bpy.data.collections.get("<Phaenotyp>" + str(scene_id))
col.objects.link(obj)
bpy.context.view_layer.objects.active = obj
scene = bpy.context.scene
phaenotyp = scene.phaenotyp
frame = scene.frame_current
verts = []
edges = []
faces = []
# like suggested here by Gorgious and CodeManX:
# https://blender.stackexchange.com/questions/6155/how-to-convert-coordinates-from-vertex-to-world-space
mat = structure_obj.matrix_world
structure_obj_vertices = structure_obj.data.vertices
len_verts = 0
for id, member in members.items():
if phaenotyp.calculation_type != "force_distribution":
member["mesh_vertex_ids"] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
else:
member["mesh_vertex_ids"] = [0, 0]
id = int(id)
vertex_0_id = member["vertex_0_id"]
vertex_1_id = member["vertex_1_id"]
# like suggested here by Gorgious and CodeManX:
# https://blender.stackexchange.com/questions/6155/how-to-convert-coordinates-from-vertex-to-world-space
vertex_0_co = mat @ structure_obj_vertices[vertex_0_id].co
vertex_1_co = mat @ structure_obj_vertices[vertex_1_id].co
# create vertices
if phaenotyp.calculation_type != "force_distribution":
for i in range(11):
pos = (vertex_0_co*(i) + vertex_1_co*(10-i))*0.1
v = Vector(pos)
verts.append(v)
member["mesh_vertex_ids"][i] = len_verts+i
# add edges
edges.append([0 + len_verts, 1 + len_verts])
edges.append([1 + len_verts, 2 + len_verts])
edges.append([2 + len_verts, 3 + len_verts])
edges.append([3 + len_verts, 4 + len_verts])
edges.append([4 + len_verts, 5 + len_verts])
edges.append([5 + len_verts, 6 + len_verts])
edges.append([6 + len_verts, 7 + len_verts])
edges.append([7 + len_verts, 8 + len_verts])
edges.append([8 + len_verts, 9 + len_verts])
edges.append([9 + len_verts, 10 + len_verts])
# update counter
len_verts += 11
else:
v_0 = Vector(vertex_0_co)
verts.append(v_0)
member["mesh_vertex_ids"][0] = len_verts
v_1 = Vector(vertex_1_co)
verts.append(v_1)
member["mesh_vertex_ids"][1] = len_verts+1
# add edges
edges.append([0 + len_verts, 1 + len_verts])
# update counter
len_verts += 2
mesh.from_pydata(verts, edges, faces)
# create vertex_color
attribute = obj.data.attributes.get("force")
if attribute:
text = "existing attribute:" + str(attribute)
else:
bpy.ops.geometry.color_attribute_add(name="force", domain='POINT', data_type='FLOAT_COLOR', color=(255, 0, 255, 1))
# create material
material_name = "<Phaenotyp>members_" + str(scene_id)
member_material = bpy.data.materials.get(material_name)
if member_material == None:
mat = bpy.data.materials.new(material_name)
mat.use_nodes = True
nodetree = mat.node_tree
# add color attribute
ca = nodetree.nodes.new(type="ShaderNodeVertexColor")
# set group
ca.layer_name = "force"
# connect to color attribute to base color
input = mat.node_tree.nodes['Principled BSDF'].inputs['Base Color']
output = ca.outputs['Color']
nodetree.links.new(input, output)
obj.data.materials.append(bpy.data.materials.get(material_name))
obj.active_material_index = len(obj.data.materials) - 1
# create modifiere if not existing
modifier = obj.modifiers.get('<Phaenotyp>')
if modifier:
text = "existing modifier:" + str(modifier)
else:
modifier_nodes = obj.modifiers.new(name="<Phaenotyp>", type='NODES')
bpy.ops.node.new_geometry_node_group_assign()
if "<Phaenotyp>Members_" + str(scene_id) in bpy.data.node_groups:
node_group = bpy.data.node_groups["<Phaenotyp>Members_" + str(scene_id)]
obj.modifiers['<Phaenotyp>'].node_group = node_group
else:
node_group = obj.modifiers['<Phaenotyp>'].node_group
node_group.name = "<Phaenotyp>Members_" + str(scene_id)
# mesh to curve
mtc = node_group.nodes.new(type="GeometryNodeMeshToCurve")
input = mtc.inputs[0] # mesh to curve, mesh
output = node_group.nodes[0].outputs[0] # group input, geometry
node_group.links.new(input, output)
# curve to mesh
ctm = node_group.nodes.new(type="GeometryNodeCurveToMesh")
input = mtc.outputs[0] # mesh to curve, curve
output = ctm.inputs[0] # curve to mesh, curve
node_group.links.new(input, output)
# profile to curve
cc = node_group.nodes.new(type="GeometryNodeCurvePrimitiveCircle")
cc.inputs[0].default_value = 8 # set amount of vertices of circle
cc.inputs[4].default_value = 0.5 # diameter * 0.5
input = ctm.inputs[1] # curve to mesh, profile curve
output = cc.outputs[0] # curve circe, curve
node_group.links.new(input, output)
# set material
gnsm = node_group.nodes.new(type="GeometryNodeSetMaterial")
gnsm.inputs[2].default_value = bpy.data.materials[ "<Phaenotyp>members_" + str(scene_id)]
input = gnsm.inputs[0] # geometry
output = ctm.outputs[0] # curve to mesh, mesh
node_group.links.new(input, output)
# link to output
output = gnsm.outputs[0] # gnsm, geometry
input = node_group.nodes[1].inputs[0] # group output, geometry
node_group.links.new(input, output)
# create vertex_group for radius
# radius is automatically choosen by geometry nodes for radius-input
bpy.ops.object.mode_set(mode = 'OBJECT')
radius_group = obj.vertex_groups.get("radius")
if not radius_group:
radius_group = obj.vertex_groups.new(name="radius")
# assign to group
for id, member in members.items():
id = str(id)
vertex_ids = member["mesh_vertex_ids"]
# copy Do and Di if not set by optimization
# or the user changed the frame during optimization
if str(frame) not in member["Do"]:
member["Do"][str(frame)] = member["Do_first"]
member["Di"][str(frame)] = member["Di_first"]
radius = member["Do"][str(frame)]*0.01
radius_group.add(vertex_ids, radius, 'REPLACE')
def create_quads(structure_obj, quads):
scene = bpy.context.scene
data = scene["<Phaenotyp>"]
scene_id = data["scene_id"]
# create mesh and object
mesh = bpy.data.meshes.new("<Phaenotyp>quads_" + str(scene_id))
obj = bpy.data.objects.new(mesh.name, mesh)
col = bpy.data.collections.get("<Phaenotyp>" + str(scene_id))
col.objects.link(obj)
bpy.context.view_layer.objects.active = obj
scene = bpy.context.scene
phaenotyp = scene.phaenotyp
frame = scene.frame_current
verts = []
edges = []
faces = []
# like suggested here by Gorgious and CodeManX:
# https://blender.stackexchange.com/questions/6155/how-to-convert-coordinates-from-vertex-to-world-space
mat = structure_obj.matrix_world
structure_obj_vertices = structure_obj.data.vertices
# to keep track of the newly created mesh
# the whole mesh is recreated when a new member oder quad is added
used_verts = {}
len_verts = 0
len_faces = 0
for id, quad in quads.items():
vertices_ids_structure = quad["vertices_ids_structure"]
# add or update nodes
# this is necessary because faces can share same vertices
vertices_ids_viz = []
for vertex_id in vertices_ids_structure:
# create entry in temporary dictionary
if str(vertex_id) not in used_verts:
# like suggested here by Gorgious and CodeManX:
# https://blender.stackexchange.com/questions/6155/how-to-convert-coordinates-from-vertex-to-world-space
vertex_co = mat @ structure_obj_vertices[vertex_id].co
# append to verts for the new mesh
verts.append(vertex_co)
# to keep track of used vertices
# key = original id in structure, value = new id in mesh for viz
used_verts[str(vertex_id)] = len_verts
vertices_ids_viz.append(len_verts)
# update id of new vertices
len_verts += 1
else:
existing_id = used_verts[str(vertex_id)]
vertices_ids_viz.append(existing_id)
# add face
faces.append(vertices_ids_viz)
# store for later
quad["face_id_viz"] = len_faces
# update list
len_faces += 1
# save new ids to data for later access
quad["vertices_ids_viz"] = vertices_ids_viz
mesh.from_pydata(verts, edges, faces)
# create vertex_color
attribute_1 = obj.data.attributes.get("force_1")
attribute_2 = obj.data.attributes.get("force_2")
if attribute_1:
text = "existing attribute_1:" + str(attribute_1)
else:
bpy.ops.geometry.color_attribute_add(name="force_1", domain='POINT', data_type='FLOAT_COLOR', color=(255, 0, 255, 1))
if attribute_2:
text = "existing attribute_2:" + str(attribute_2)
else:
bpy.ops.geometry.color_attribute_add(name="force_2", domain='POINT', data_type='FLOAT_COLOR', color=(255, 0, 255, 1))
# create material for side 1
material_name_1 = "<Phaenotyp>quad_1"
quad_material_1 = bpy.data.materials.get(material_name_1)
if quad_material_1 == None:
mat = bpy.data.materials.new(material_name_1)
mat.use_nodes = True
nodetree = mat.node_tree
# add color attribute
ca = nodetree.nodes.new(type="ShaderNodeVertexColor")
# set group
ca.layer_name = "force_1"
# connect to color attribute to base color
input = mat.node_tree.nodes['Principled BSDF'].inputs['Base Color']
output = ca.outputs['Color']
nodetree.links.new(input, output)
obj.data.materials.append(bpy.data.materials.get(material_name_1))
obj.active_material_index = len(obj.data.materials) - 1
# create material for side 2
material_name_2 = "<Phaenotyp>quad_2"
quad_material_2 = bpy.data.materials.get(material_name_2)
if quad_material_2 == None:
mat = bpy.data.materials.new(material_name_2)
mat.use_nodes = True
nodetree = mat.node_tree
# add color attribute
ca = nodetree.nodes.new(type="ShaderNodeVertexColor")
# set group
ca.layer_name = "force_2"
# connect to color attribute to base color
input = mat.node_tree.nodes['Principled BSDF'].inputs['Base Color']
output = ca.outputs['Color']
nodetree.links.new(input, output)
obj.data.materials.append(bpy.data.materials.get(material_name_2))
obj.active_material_index = len(obj.data.materials) - 1
# create vertex_group for thickness
bpy.ops.object.mode_set(mode = 'OBJECT')
thickness_group = obj.vertex_groups.get("thickness")
if not thickness_group:
thickness_group = obj.vertex_groups.new(name="thickness")
# create modifiere if not existing
modifier = obj.modifiers.get('<Phaenotyp>')
if modifier:
text = "existing modifier:" + str(modifier)
else:
modifier_subsurf = obj.modifiers.new(name="<Phaenotyp>", type='SUBSURF')
modifier_subsurf.levels = 2
modifier_subsurf.subdivision_type = 'SIMPLE'
modifier_solidify = obj.modifiers.new(name="<Phaenotyp>", type='SOLIDIFY')
modifier_solidify.thickness = 1
modifier_solidify.vertex_group = "thickness"
modifier_solidify.use_even_offset = True
modifier_solidify.material_offset = 1
modifier_solidify.offset = -1
modifier_solidify.use_rim = False
# set the thickness passed from gui
for id, quad in quads.items():
vertices_ids = quad["vertices_ids_viz"]
thickness = quad["thickness_first"] * 0.01 # to convert from cm to m
thickness_group.add(vertices_ids, thickness, 'REPLACE')
def create_stresslines(structure_obj, quads):
scene = bpy.context.scene
data = scene["<Phaenotyp>"]
scene_id = data["scene_id"]
# create mesh and object
mesh = bpy.data.meshes.new("<Phaenotyp>stresslines_" + str(scene_id))
obj = bpy.data.objects.new(mesh.name, mesh)
col = bpy.data.collections.get("<Phaenotyp>" + str(scene_id))
col.objects.link(obj)
bpy.context.view_layer.objects.active = obj
scene = bpy.context.scene
phaenotyp = scene.phaenotyp
frame = scene.frame_current
data = scene["<Phaenotyp>"]
structure = data["structure"]
# like suggested here by Gorgious and CodeManX:
# https://blender.stackexchange.com/questions/6155/how-to-convert-coordinates-from-vertex-to-world-space
mat = structure_obj.matrix_world
structure_faces = structure.data.polygons
verts = []
edges = []
faces = []
len_verts = 0
for id, quad in quads.items():
face = structure_faces[int(id)]
normal = face.normal
center = face.center
# like suggested here by Gorgious and CodeManX:
# https://blender.stackexchange.com/questions/6155/how-to-convert-coordinates-from-vertex-to-world-space
normal = mat @ normal
center = mat @ center
thickness = quad["thickness_first"] * 0.01
# append vertices for first edge
verts.append(center)
verts.append(center + normal*0.2)
# add first edge
edges.append([len_verts, len_verts+1])
# update id of new vertices
len_verts += 2
# append vertices for second edge
verts.append(center)
verts.append(center + normal*0.2)
# add second edge
edges.append([len_verts, len_verts+1])
# update id of new vertices
len_verts += 2
# append vertices for third edge
verts.append(center)
verts.append(center + normal*0.2)
# add third edge
edges.append([len_verts, len_verts+1])
# update id of new vertices
len_verts += 2
# append vertices for fourth edge
verts.append(center)
verts.append(center + normal*0.2)
# add fourth edge
edges.append([len_verts, len_verts+1])
# update id of new vertices
len_verts += 2
# store for later for ids of start_first, end_first, start_second, end_second ...
quad["stresslines_viz"] = [len_verts-8, len_verts-7, len_verts-6, len_verts-5, len_verts-4, len_verts-3, len_verts-2, len_verts-1]
mesh.from_pydata(verts, edges, faces)
# create vertex_color
attribute = obj.data.attributes.get("stressline")
if attribute:
text = "existing attribute:" + str(attribute)
else:
bpy.ops.geometry.color_attribute_add(name="stressline", domain='POINT', data_type='FLOAT_COLOR', color=(255, 0, 255, 1))
# create material
material_name = "<Phaenotyp>Stresslines_" + str(scene_id)
stressline_material = bpy.data.materials.get(material_name)
if stressline_material == None:
mat = bpy.data.materials.new(material_name)
mat.use_nodes = True
nodetree = mat.node_tree
# add color attribute
ca = nodetree.nodes.new(type="ShaderNodeVertexColor")
# set group
ca.layer_name = "stressline"
# connect to color attribute to base color
input = mat.node_tree.nodes['Principled BSDF'].inputs['Base Color']
output = ca.outputs['Color']
nodetree.links.new(input, output)
obj.data.materials.append(bpy.data.materials.get(material_name))
obj.active_material_index = len(obj.data.materials) - 1
# create modifiere if not existing
modifier = obj.modifiers.get('<Phaenotyp>')
if modifier:
text = "existing modifier:" + str(modifiers)
else:
modifier_nodes = obj.modifiers.new(name="<Phaenotyp>", type='NODES')
bpy.ops.node.new_geometry_node_group_assign()
nodes = obj.modifiers['<Phaenotyp>'].node_group
# set name to group
if nodes.name == "<Phaenotyp>Stresslines_" + str(scene_id):
node_group = bpy.data.node_groups["<Phaenotyp>Stresslines_" + str(scene_id)]
else:
nodes.name = "<Phaenotyp>Stresslines_" + str(scene_id)
node_group = bpy.data.node_groups["<Phaenotyp>Stresslines_" + str(scene_id)]
# mesh to curve
mtc = node_group.nodes.new(type="GeometryNodeMeshToCurve")
input = mtc.inputs[0] # mesh to curve, mesh
output = node_group.nodes[0].outputs[0] # group input, geometry
node_group.links.new(input, output)
# curve to mesh
ctm = node_group.nodes.new(type="GeometryNodeCurveToMesh")
input = mtc.outputs[0] # mesh to curve, curve
output = ctm.inputs[0] # curve to mesh, curve
node_group.links.new(input, output)
# profile to curve
cc = node_group.nodes.new(type="GeometryNodeCurvePrimitiveCircle")
cc.inputs[0].default_value = 8 # set amount of vertices of circle
cc.inputs[4].default_value = 0.05 # diameter
input = ctm.inputs[1] # curve to mesh, profile curve