-
Notifications
You must be signed in to change notification settings - Fork 8
/
rodjlib.py
786 lines (709 loc) · 31.2 KB
/
rodjlib.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
# --------------------------------------------------------------------------
# Blendyn -- file rodlib.py
# Copyright (C) 2015 -- 2021 Andrea Zanoni -- [email protected]
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This file is part of Blendyn, add-on script for Blender.
#
# Blendyn is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Blendyn is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Blendyn. If not, see <http://www.gnu.org/licenses/>.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------
import bpy
import logging
from mathutils import *
from math import *
from .utilslib import *
## Parses rod joint entry in the .log file
def parse_rod(rw, ed):
ret_val = True
try:
el = ed['rod_' + str(rw[1])]
eldbmsg({'PARSE_ELEM'}, "BLENDYN::parse_rod()", el)
eldbmsg({'FOUND_DICT'}, "BLENDYN::parse_rod()", el)
el.nodes[0].int_label = int(rw[2])
el.nodes[1].int_label = int(rw[6])
el.offsets[0].value = Vector(( float(rw[3]), float(rw[4]), float(rw[5]) ))
el.offsets[1].value = Vector(( float(rw[7]), float(rw[8]), float(rw[9]) ))
el.is_imported = True
# FIXME: this is here to enhance backwards compatibility.
# Should disappear in future versions
el.mbclass = 'elem.joint'
if el.name in bpy.data.objects.keys():
el.blender_object = el.name
except KeyError:
el = ed.add()
el.mbclass = 'elem.joint'
el.type = 'rod'
el.int_label = int(rw[1])
eldbmsg({'PARSE_ELEM'}, "BLENDYN::parse_rod()", el)
eldbmsg({'NOTFOUND_DICT'}, "BLENDYN::parse_rod()", el)
el.nodes.add()
el.nodes[0].int_label = int(rw[2])
el.offsets.add()
el.offsets[0].value = Vector(( float(rw[3]), float(rw[4]), float(rw[5]) ))
el.nodes.add()
el.nodes[1].int_label = int(rw[6])
el.offsets.add()
el.offsets[1].value = Vector(( float(rw[7]), float(rw[8]), float(rw[9]) ))
el.import_function = "blendyn.import_rod"
el.update_info_operator = "update.rod"
el.info_draw = "rod_info_draw"
el.write_operator = "write.rod"
el.name = el.type + "_" + str(el.int_label)
el.is_imported = True
ret_val = False
pass
return ret_val
# -------------------------------------------------------------------------------
# end of parse_rod(rw, ed) function
### Parses rod bezier joint entry in the .log file
#def parse_rod_bezier(rw, ed):
# ret_val = True
#
# # Debug message
# print("Blendyn::parse_rod_bezier(): Parsing rod bezier " + rw[2])
#
# try:
# el = ed['rod_bezier_' + rw[2]]
#
# print("Blendyn::parse_rod_bezier(): found existing entry in elements dictionary.\
# Updating it.")
#
# el.nodes[0].int_label = int(rw[3])
# el.nodes[1].int_label = int(rw[10])
#
# el.offsets[0].value = Vector(( float(rw[4]), float(rw[5]), float(rw[6]) ))
# el.offsets[1].value = Vector(( float(rw[7]), float(rw[8]), float(rw[9]) ))
# el.offsets[2].value = Vector(( float(rw[11]), float(rw[12]), float(rw[13]) ))
# el.offsets[3].value = Vector(( float(rw[14]), float(rw[15]), float(rw[16]) ))
#
# if el.name in bpy.data.objects.keys():
# el.blender_object = el.name
# el.is_imported = True
# except KeyError:
#
# print("Blendyn::parse_rod_bezier(): didn't find entry in elements dictionary. Creating one.")
#
# el = ed.add()
# el.type = 'rod_bezier'
# el.int_label = int(rw[2])
#
# el.nodes.add()
# el.nodes[0].int_label = int(rw[3])
#
# el.offsets.add()
# el.offsets[0].value = Vector(( float(rw[4]), float(rw[5]), float(rw[6]) ))
#
# el.offsets.add()
# el.offsets[1].value = Vector(( float(rw[7]), float(rw[8]), float(rw[9]) ))
#
# el.nodes.add()
# el.nodes[1].int_label = int(rw[10])
#
# el.offsets.add()
# el.offsets[2].value = Vector(( float(rw[11]), float(rw[12]), float(rw[13]) ))
#
# el.offsets.add()
# el.offsets[3].value = Vector(( float(rw[14]), float(rw[15]), float(rw[16]) ))
#
# el.import_function = "add.mbdyn_elem_rod_bezier"
# el.info_draw = "rod_bezier_info_draw"
# el.update_info_operator = "update.rod_bezier"
# el.write_operator = "write.rod_bezier"
# el.name = "rod_bezier_" + str(el.int_label)
# ret_val = False
# pass
# return ret_val
## -------------------------------------------------------------------------------
## end of parse_rod_bezier(rw, ed) function
# Function that displays Rod Element info in panel
def rod_info_draw(elem, layout):
nd = bpy.context.scene.mbdyn.nodes
row = layout.row()
col = layout.column(align=True)
for node in nd:
if node.int_label == elem.nodes[0].int_label:
# Display node 1 info
col.prop(node, "int_label", text = "Node 1 ID ")
col.prop(node, "string_label", text = "Node 1 label ")
col.prop(node, "blender_object", text = "Node 1 Object: ")
col.enabled = False
# Display offset of node 1 info
row = layout.row()
row.label(text = "offset 1 in Node " + node.string_label + " R.F.")
col = layout.column(align = True)
col.prop(elem.offsets[0], "value", text = "", slider = False)
layout.separator()
elif node.int_label == elem.nodes[1].int_label:
# Display node 2 info
row = layout.row()
col = layout.column(align = True)
col.prop(node, "int_label", text = "Node 2 ID ")
col.prop(node, "string_label", text = "Node 2 label ")
col.prop(node, "blender_object", text = "Node 2 Object: ")
col.enabled = False
# Display first offset of node 2 info
row = layout.row()
row.label(text = "offset 2 in Node " + node.string_label + " R.F.")
col = layout.column(align = True)
col.prop(elem.offsets[1], "value", text = "", slider = False)
layout.separator()
# -----------------------------------------------------------
# end of rod_info_draw() function
## Function that displays Bezier Rod Element info in panel
#def rod_bezier_info_draw(elem, layout):
# nd = bpy.context.scene.mbdyn.nodes
# row = layout.row()
# col = layout.column(align=True)
#
# # curve data for blender object representing the element
# cvdata = bpy.data.curves[elem.blender_object + "_cvdata"].splines[0]
#
# for node in nd:
# if node.int_label == elem.nodes[0].int_label:
#
# # Display node 1 info
# col.prop(node, "int_label", text = "Node 1 ID ")
# col.prop(node, "string_label", text = "Node 1 label ")
# col.prop(node, "blender_object", text = "Node 1 Object: ")
# col.enabled = False
#
# # Display first offset of node 1 info
# row = layout.row()
# row.label(text = "offset 1 in Node " + node.string_label + " R.F.")
# col = layout.column(align = True)
# col.prop(elem.offsets[0], "value", text = "", slider = False)
#
# # Display second offset of node 1 info
# row = layout.row()
# row.label(text = "offset 2 in Node " + node.string_label + " R.F.")
# col = layout.column(align = True)
# col.prop(elem.offsets[1], "value", text = "", slider = False)
# # col.enabled = False
#
# layout.separator()
#
# elif node.int_label == elem.nodes[1].int_label:
#
# # Display node 2 info
# row = layout.row()
# col = layout.column(align = True)
# col.prop(node, "int_label", text = "Node 2 ID ")
# col.prop(node, "string_label", text = "Node 2 label ")
# col.prop(node, "blender_object", text = "Node 2 Object: ")
# col.enabled = False
#
# # Display first offset of node 2 info
# row = layout.row()
# row.label(text = "offset 1 in Node " + node.string_label + " R.F.")
# col = layout.column(align = True)
# col.prop(elem.offsets[2], "value", text = "", slider = False)
#
# # Display first offset of node 2 info
# row = layout.row()
# row.label(text = "offset 2 in Node " + node.string_label + " R.F.")
# col = layout.column(align = True)
# col.prop(elem.offsets[3], "value", text = "", slider = False)
#
# layout.separator()
## -----------------------------------------------------------
## end of rod_bezier_info_draw() function
## Updates info for Rod element
class RodUpdate(Operator):
bl_idname = "update.rod"
bl_label = "MBDyn Rod info updater"
elem_key = bpy.props.StringProperty()
def execute(self, context):
elem = bpy.context.scene.mbdyn.elems[self.elem_key]
nd = bpy.context.scene.mbdyn.nodes
cvdata = bpy.data.curves[elem.blender_object + "_cvdata"].splines[0]
for node in nd:
if node.int_label == elem.nodes[0].int_label:
# Node 1 offset
f1 = cvdata.points[0].co.to_3d()
RN1 = bpy.data.objects[node.blender_object].matrix_world.to_3x3().normalized()
xN1 = bpy.data.objects[node.blender_object].location
elem.offsets[0]['value'] = RN1.transposed()*(f1 - xN1)
elif node.int_label == elem.nodes[1].int_label:
# Node 2 offset
f2 = cvdata.points[1].co.to_3d()
RN2 = bpy.data.objects[node.blender_object].matrix_world.to_3x3().normalized()
xN2 = bpy.data.objects[node.blender_object].location
elem.offsets[1]['value'] = RN2.transposed()*(f2 - xN2)
return {'FINISHED'}
# -----------------------------------------------------------
# end of RodUpdate class
### Updates info for Bezier Rod element
#class RodBezierUpdate(Operator):
# bl_idname = "update.rod_bezier"
# bl_label = "MBDyn Rod Bezier info updater"
# elem_key = bpy.props.StringProperty()
#
# def execute(self, context):
# elem = bpy.context.scene.mbdyn.elems[self.elem_key]
# nd = bpy.context.scene.mbdyn.nodes
# cvdata = bpy.data.curves[elem.blender_object + "_cvdata"].splines[0]
# for node in nd:
# if node.int_label == elem.nodes[0].int_label:
#
# # node 1 offset 1
# fOG = cvdata.bezier_points[0].co
# RN1 = bpy.data.objects[node.blender_object].matrix_world.to_3x3().normalized()
# xN1 = bpy.data.objects[node.blender_object].location
# elem.offsets[0]['value'] = RN1.transposed()*(fOG - xN1)
#
# # node 1 offset 2
# fAG = cvdata.bezier_points[0].handle_right
# elem.offsets[1]['value'] = RN1.transposed()*(fAG - xN1)
#
# elif node.int_label == elem.nodes[1].int_label:
#
# # node 2 offset 1
# fBG = cvdata.bezier_points[1].handle_left
# RN2 = bpy.data.objects[node.blender_object].matrix_world.to_3x3().normalized()
# xN2 = bpy.data.objects[node.blender_object].location
# elem.offsets[2]['value'] = RN2.transposed()*(fBG - xN2)
#
# # offset 2
# fIG = cvdata.bezier_points[1].co
# elem.offsets[3]['value'] = RN2.transposed()*(fIG - xN2)
#
# return {'FINISHED'}
## -----------------------------------------------------------
## end of RodBezierUpdate class
## Imports a Rob element in the scene as a line joining two nodes
class BLENDYN_OT_import_rod(bpy.types.Operator):
""" Imports a rod element into the Blender scene """
bl_idname = "blendyn.import_rod"
bl_label = "Imports a rod element"
int_label: bpy.props.IntProperty()
def draw(self, context):
layout = self.layout
layout.aligment = 'LEFT'
def execute(self, context):
ed = bpy.context.scene.mbdyn.elems
nd = bpy.context.scene.mbdyn.nodes
try:
elem = ed['rod_' + str(self.int_label)]
retval = spawn_rod_element(elem, context)
if retval == {'OBJECT_EXISTS'}:
eldbmsg(retval, type(self).__name__ + '::execute()', elem)
return {'CANCELLED'}
elif retval == {'NODE1_NOTFOUND'}:
eldbmsg(retval, type(self).__name__ + '::execute()', elem)
return {'CANCELLED'}
elif retval == {'NODE2_NOTFOUND'}:
eldbmsg(retval, type(self).__name__ + '::execute()', elem)
return {'CANCELLED'}
elif retval == {'COLLECTION_ERROR'}:
eldbmsg(retval, type(self).__name__ + '::execute()', elem)
return {'CANCELLED'}
elif retval == {'FINISHED'}:
eldbmsg({'IMPORT_SUCCESS'}, type(self).__name__ + '::execute()', elem)
return retval
else:
return retval
except KeyError:
eldbmsg({'DICT_ERROR'}, type(self).__name__ + '::execute()', elem)
return {'CANCELLED'}
return {'FINISHED'}
# -----------------------------------------------------------
# end of BLENDYN_OT_import_rod class
### Helps import a Rob Bezier element in the scene
#class Scene_OT_MBDyn_Import_Rod_Bezier_Joint_Element(bpy.types.Operator):
# bl_idname = "add.mbdyn_elem_rod_bezier"
# bl_label = "MBDyn rod element importer"
# int_label: bpy.props.IntProperty()
#
# def draw(self, context):
# layout = self.layout
# layout.aligment = 'LEFT'
#
# def execute(self, context):
# ed = bpy.context.scene.mbdyn.elems
# nd = bpy.context.scene.mbdyn.nodes
# try:
# elem = ed['rod_bezier_' + str(self.int_label)]
# retval = spawn_rod_bezier_element(elem)
# if retval == {'OBJECT_EXISTS'}:
# message = "Found the Object " + elem.blender_object + \
# " remove or rename it to re-import the element!"
# self.report({'WARNING'}, message)
# print("Blendyn::Element is already imported. Remove it or rename it " +\
# "before re-importing the element.")
# logging.warning(message)
# return {'CANCELLED'}
# elif retval == {'NODE1_NOTFOUND'}:
# message = "Could not import element: Blender object " +\
# "associated to Node " + str(elem.nodes[0].int_label) + " not found"
# self.report({'ERROR'}, message)
# logging.error(message)
# return {'CANCELLED'}
# elif retval == {'NODE2_NOTFOUND'}:
# message = "Could not import element: Blender object " +\
# "associated to Node " + str(elem.nodes[1].int_label) + " not found"
# self.report({'ERROR'}, message)
# logging.error(message)
# return {'CANCELLED'}
# else:
# return retval
# except KeyError:
# message = "Element rod_bezier_" + str(self.int_label) + " not found."
# self.report({'ERROR'}, message)
# logging.error(message)
#
# return {'CANCELLED'}
## -----------------------------------------------------------
## end of Scene_OT_MBDyn_Import_Rod_Bezier_Joint_Element class
class BLENDYN_OT_write_rod_input(Operator):
""" Writes the input for Rod Element in the text editor """
bl_idname = "blendyn.write_rod_input"
bl_label = "Write updated input for rod"
elem_key: bpy.props.StringProperty()
def execute(self, context):
elem = bpy.context.scene.mbdyn.elems[self.elem_key]
f1 = elem.offsets[0].value
f2 = elem.offsets[1].value
nd = bpy.context.scene.mbdyn.nodes
n1 = nd['node_' + str(elem.nodes[0].int_label)]
n2 = nd['node_' + str(elem.nodes[1].int_label)]
if n1.string_label != 'none':
node_1_label = n1.string_label
else:
node_1_label = str(n1.int_label)
if n2.string_label != 'none':
node_2_label = n2.string_label
else:
node_2_label = str(n2.int_label)
if elem.string_label != 'none':
joint_label = elem.string_label
else:
joint_label = str(elem.int_label)
# create new text object
rbtext = bpy.data.texts.new(self.elem_key)
# write rod input
rbtext.write("joint: " + joint_label + ",\n")
rbtext.write("\trod,\n")
rbtext.write("\t" + node_1_label + ",\n")
rbtext.write("\t\tposition, reference, node, " + node_1_label + ",\n")
rbtext.write("\t\t\t" + str(f1[0]) + ",\n")
rbtext.write("\t\t\t" + str(f1[1]) + ",\n")
rbtext.write("\t\t\t" + str(f1[2]) + ",\n")
rbtext.write("\t" + node_2_label + ",\n")
rbtext.write("\t\tposition, reference, node, " + node_2_label + ",\n")
rbtext.write("\t\t\t" + str(f2[0]) + ",\n")
rbtext.write("\t\t\t" + str(f2[1]) + ",\n")
rbtext.write("\t\t\t" + str(f2[2]) + ",\n")
rbtext.write("\tfrom nodes;")
message = "Input file contribute for element written. See " +\
rbtext.name + " in text editor"
self.report({'INFO'}, message)
logging.info(message)
return {'FINISHED'}
# -----------------------------------------------------------
# end of BLENDYN_OT_write_rod_input class
### Writes the input for Bezier Rod Element in text panel
#class RodBezierWrite(Operator):
# bl_idname = "write.rod_bezier"
# bl_label = "MBDyn Rod Bezier input writer"
# elem_key = bpy.props.StringProperty()
#
# def execute(self, context):
# elem = bpy.context.scene.mbdyn.elems[self.elem_key]
# fO = elem.offsets[0].value
# fA = elem.offsets[1].value
# fB = elem.offsets[2].value
# fI = elem.offsets[3].value
#
# nd = bpy.context.scene.mbdyn.nodes
# n1 = nd['node_' + str(elem.nodes[0].int_label)]
# n2 = nd['node_' + str(elem.nodes[1].int_label)]
#
# node_1_label = "Node_" + str(elem.nodes[0].int_label)
# if n1.string_label != node_1_label:
# node_1_label = n1.string_label
# else:
# node_1_label = str(n1.int_label)
#
# node_2_label = "Node_" + str(elem.nodes[1].int_label)
# if n2.string_label != node_2_label:
# node_2_label = n2.string_label
# else:
# node_2_label = str(n2.int_label)
#
# # create new text object
# rbtext = bpy.data.texts.new(self.elem_key)
#
# # write rod bezier input
# rbtext.write("joint: " + self.elem_key + ",\n")
# rbtext.write("\trod bezier,\n")
# rbtext.write("\t" + node_1_label + ",\n")
# rbtext.write("\t\tposition, reference, node, " + node_1_label + ",\n")
# rbtext.write("\t\t\t" + str(fO[0]) + "*msfx" + ",\n")
# rbtext.write("\t\t\t" + str(fO[1]) + "*msfy" + ",\n")
# rbtext.write("\t\t\t" + str(fO[2]) + "*msfz" + ",\n")
# rbtext.write("\t\tposition, reference, node, " + node_1_label + ",\n")
# rbtext.write("\t\t\t" + str(fA[0]) + "*msfx" + ",\n")
# rbtext.write("\t\t\t" + str(fA[1]) + "*msfy" + ",\n")
# rbtext.write("\t\t\t" + str(fA[2]) + "*msfz" + ",\n")
# rbtext.write("\t" + node_2_label + ",\n")
# rbtext.write("\t\tposition, reference, node, " + node_2_label + ",\n")
# rbtext.write("\t\t\t" + str(fB[0]) + "*msfx" + ",\n")
# rbtext.write("\t\t\t" + str(fB[1]) + "*msfy" + ",\n")
# rbtext.write("\t\t\t" + str(fB[2]) + "*msfz" + ",\n")
# rbtext.write("\t\tposition, reference, node, " + node_2_label + ",\n")
# rbtext.write("\t\t\t" + str(fI[0]) + "*msfx" + ",\n")
# rbtext.write("\t\t\t" + str(fI[1]) + "*msfy" + ",\n")
# rbtext.write("\t\t\t" + str(fI[2]) + "*msfz" + ",\n")
# rbtext.write("\tfrom nodes;")
#
# message = "Input file contribute for element written. See " +\
# rbtext.name + " in text editor"
# self.report({'INFO'}, message)
# logging.info(message)
# return {'FINISHED'}
## -----------------------------------------------------------
## end of RodBezierWrite class
# Created the object representing a rod joint element
def spawn_rod_element(elem, context):
""" Draws a rod joint element as a line connecting two points
belonging to two objects """
nd = context.scene.mbdyn.nodes
if any(obj == elem.blender_object for obj in context.scene.objects.keys()):
return {'OBJECT_EXISTS'}
# try to find Blender objects associated with the nodes that
# the element connects
try:
n1 = nd['node_' + str(elem.nodes[0].int_label)].blender_object
except KeyError:
return {'NODE1_NOTFOUND'}
try:
n2 = nd['node_' + str(elem.nodes[1].int_label)].blender_object
except KeyError:
return {'NODE2_NOTFOUND'}
# nodes' objects
n1OBJ = bpy.data.objects[n1]
n2OBJ = bpy.data.objects[n2]
# creation of line representing the rod
rodobj_id = 'rod_' + str(elem.int_label)
rodcv_id = rodobj_id + '_cvdata'
try:
# put it all in the 'joints' collection
set_active_collection('joints')
elcol = bpy.data.collections.new(name = elem.name)
bpy.data.collections['joints'].children.link(elcol)
set_active_collection(elem.name)
except KeyError:
return {'COLLECTION_ERROR'}
# check if the object is already present. If it is, remove it.
if rodobj_id in bpy.data.objects.keys():
bpy.data.objects.remove(bpy.data.objects[rodobj_id])
# check if the curve is already present. If it is, remove it.
if rodcv_id in bpy.data.curves.keys():
bpy.data.curves.remove(bpy.data.curves[rodcv_id])
# create a new curve
cvdata = bpy.data.curves.new(rodcv_id, type = 'CURVE')
cvdata.dimensions = '3D'
polydata = cvdata.splines.new('POLY')
polydata.points.add(1)
# get offsets
f1 = elem.offsets[0].value
f2 = elem.offsets[1].value
# assign coordinates of knots in global frame
R1 = n1OBJ.rotation_quaternion.to_matrix()
R2 = n2OBJ.rotation_quaternion.to_matrix()
p1 = n1OBJ.location + R1@Vector(( f1[0], f1[1], f1[2] ))
p2 = n2OBJ.location + R2@Vector(( f2[0], f2[1], f2[2] ))
polydata.points[0].co = p1.to_4d()
polydata.points[1].co = p2.to_4d()
rodOBJ = bpy.data.objects.new(rodobj_id, cvdata)
rodOBJ.mbdyn.type = 'element'
rodOBJ.mbdyn.dkey = elem.name
elcol.objects.link(rodOBJ)
elem.blender_object = rodOBJ.name
## hooking of the line ends to the Blender objects
objs = [n1OBJ, n2OBJ]
names = ['P1', 'P2']
locs = [p1, p2]
M = Matrix()
for i, (p, obj, name, loc) in enumerate(zip(rodOBJ.data.splines[0].points, objs, names, locs)):
hook = rodOBJ.modifiers.new(name, type = 'HOOK')
hook.object = obj
hook.vertex_indices_set([i])
nobj = bpy.data.objects.new(rodOBJ.name + '_RF' + str(i + 1), None)
nobj.empty_display_type = 'ARROWS'
nobj.location = loc
dim = obj.dimensions.magnitude/sqrt(3) if obj.data else obj.empty_display_size
nobj.empty_display_size = .33*dim
parenting(nobj, obj)
elcol.objects.link(nobj)
nobj.hide_set(state = True)
# lock the object scale
rodOBJ.lock_scale[0] = True
rodOBJ.lock_scale[1] = True
rodOBJ.lock_scale[2] = True
# link objects to the element collection
elcol.objects.link(n1OBJ)
elcol.objects.link(n2OBJ)
set_active_collection('Master Collection')
elem.is_imported = True
return{'FINISHED'}
# -----------------------------------------------------------
# end of spawn_rod_element(elem) function
#def spawn_rod_bezier_element(elem):
# if any(obj == elem.blender_object for obj in context.scene.objects.keys()):
# return {'OBJECT_EXISTS'}
# else:
# elem.is_imported = False
# n1 = "none"
# n2 = "none"
#
# # try to find Blender objects associated with the nodes that
# # the element connects
# for node in nd:
# if elem.nodes[0].int_label == node.int_label:
# n1 = node.blender_object
# elif elem.nodes[1].int_label == node.int_label:
# n2 = node.blender_object
# if n1 == "none":
# print("Blendyn::spawn_rod_bezier_element(): Could not find a Blender object \
# associated to Node " + str(elem.nodes[0].int_label))
# return {'NODE1_NOTFOUND'}
# elif n2 == "none":
# print("Blendyn::spawn_rod_bezier_element(): Could not find a Blender object \
# associated to Node " + str(elem.nodes[1].int_label))
# return {'NODE2_NOTFOUND'}
#
# # creation of line representing the rod
# rodobj_id = "rod_bezier_" + str(elem.int_label)
# rodcv_id = rodobj_id + "_cvdata"
#
# # check if the object is already present
# # If it is, remove it. FIXME: this may be dangerous!
# if rodobj_id in bpy.data.objects.keys():
# bpy.data.objects.remove(bpy.data.objects[rodobj_id])
#
# # check if curve is already present
# # If it is, remove it. FIXME: this may be dangerous!
# if rodcv_id in bpy.data.curves.keys():
# bpy.data.curves.remove(bpy.data.curves[rodcv_id])
#
# # create a new curve and its object
# cvdata = bpy.data.curves.new(rodcv_id, type = 'CURVE')
# cvdata.dimensions = '3D'
# polydata = cvdata.splines.new('BEZIER')
# polydata.bezier_points.add(1)
#
# # Get offsets
# fO = elem.offsets[0].value
# fA = elem.offsets[1].value
# fB = elem.offsets[2].value
# fI = elem.offsets[3].value
#
# # Rotate and translate offsets
# fOG = bpy.data.objects[n1].matrix_world*Vector(( fO[0], fO[1], fO[2], 1.0 ))
# fAG = bpy.data.objects[n1].matrix_world*Vector(( fA[0], fA[1], fA[2], 1.0 ))
# fBG = bpy.data.objects[n2].matrix_world*Vector(( fB[0], fB[1], fB[2], 1.0 ))
# fIG = bpy.data.objects[n2].matrix_world*Vector(( fI[0], fI[1], fI[2], 1.0 ))
#
# # node 1
# polydata.bezier_points[0].co = Vector(( fOG[0], fOG[1], fOG[2] ))
# polydata.bezier_points[0].handle_left_type = 'FREE'
# polydata.bezier_points[0].handle_left = polydata.bezier_points[0].co
# polydata.bezier_points[0].handle_right_type = 'FREE'
# polydata.bezier_points[0].handle_right = Vector(( fAG[0], fAG[1], fAG[2] ))
#
# # node 2
# polydata.bezier_points[1].co = Vector(( fIG[0], fIG[1], fIG[2] ))
# polydata.bezier_points[1].handle_left_type = 'FREE'
# polydata.bezier_points[1].handle_left = Vector(( fBG[0], fBG[1], fBG[2] ))
# polydata.bezier_points[1].handle_right_type = 'FREE'
# polydata.bezier_points[1].handle_right = polydata.bezier_points[1].co
#
# rodOBJ = bpy.data.objects.new("rod_bezier_" + str(elem.int_label), cvdata)
# rodOBJ.mbdyn.type = 'element'
# rodOBJ.mbdyn.dkey = elem.name
# elcol.objects.link(rodOBJ)
# elem.blender_object = rodOBJ.name
# elem.name = rodOBJ.name
# rodOBJ.select_set(state = True)
#
# ## hooking of the line ends to the Blender objects
#
# # deselect all objects (guaranteed by previous the selection of rodOBJ)
# bpy.ops.object.select_all()
#
# # select rod, set it to active and enter edit mode
# rodOBJ.select_set(state = True)
# bpy.context.view_layer.objects.active = rodOBJ
#
# # select first control point and its handles and object 1,
# # then set the hook and deselect object of node 1
# bpy.ops.object.mode_set(mode = 'EDIT', toggle = False)
#
# bpy.data.curves[rodcv_id].splines[0].bezier_points[0].select_control_point = True
# bpy.data.objects[n1].select_set(state = True)
# bpy.ops.object.hook_add_selob()
# bpy.data.curves[rodcv_id].splines[0].bezier_points[0].select_control_point = False
# bpy.data.objects[n1].select_set(state = False)
#
# bpy.data.curves[rodcv_id].splines[0].bezier_points[0].select_left_handle = True
# bpy.data.objects[n1].select_set(state = True)
# bpy.ops.object.hook_add_selob()
# bpy.data.curves[rodcv_id].splines[0].bezier_points[0].select_left_handle = False
# bpy.data.objects[n1].select_set(state = False)
#
# bpy.data.curves[rodcv_id].splines[0].bezier_points[0].select_right_handle = True
# bpy.data.objects[n1].select_set(state = True)
# bpy.ops.object.hook_add_selob()
# bpy.data.curves[rodcv_id].splines[0].bezier_points[0].select_right_handle = False
# bpy.data.objects[n1].select_set(state = False)
#
# # exit edit mode and deselect all
# bpy.ops.object.mode_set(mode = 'OBJECT', toggle = False)
# bpy.ops.object.select_all()
#
# # select first control point and its handles and object 2,
# # then set the hook and deselect object of node 2
#
# rodOBJ.select_set(state = True)
# bpy.context.view_layer.objects.active = rodOBJ
# bpy.ops.object.mode_set(mode = 'EDIT', toggle = False)
#
# bpy.data.curves[rodcv_id].splines[0].bezier_points[1].select_control_point = True
# bpy.data.objects[n2].select_set(state = True)
# bpy.ops.object.hook_add_selob()
# bpy.data.curves[rodcv_id].splines[0].bezier_points[1].select_control_point = False
# bpy.data.objects[n2].select_set(state = False)
#
# bpy.data.curves[rodcv_id].splines[0].bezier_points[1].select_left_handle = True
# bpy.data.objects[n2].select_set(state = True)
# bpy.ops.object.hook_add_selob()
# bpy.data.curves[rodcv_id].splines[0].bezier_points[1].select_left_handle = False
# bpy.data.objects[n2].select_set(state = False)
#
# bpy.data.curves[rodcv_id].splines[0].bezier_points[1].select_right_handle = True
# bpy.data.objects[n2].select_set(state = True)
# bpy.ops.object.hook_add_selob()
# bpy.data.curves[rodcv_id].splines[0].bezier_points[1].select_right_handle = False
# bpy.data.objects[n2].select_set(state = False)
#
# # exit edit mode and deselect all
# bpy.ops.object.mode_set(mode = 'OBJECT', toggle = False)
# elem.is_imported = True
# return{'FINISHED'}
## -----------------------------------------------------------
## end of spawn_rod_bezier_element(elem) function