-
Notifications
You must be signed in to change notification settings - Fork 8
/
modallib.py
362 lines (306 loc) · 13.1 KB
/
modallib.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
# --------------------------------------------------------------------------
# Blendyn -- file modallib.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 os
import numpy as np
from mathutils import *
from math import *
from .utilslib import *
# helper function which parse information of modal in .log file
def parse_modal(rw, ed):
ret_val = True
try:
el = ed['modal_' + str(rw[1])]
eldbmsg({'PARSE_ELEM'}, "BLENDYN::parse_beamslider()", el)
eldbmsg({'FOUND_DICT'}, "BLENDYN::parse_beamslider()", el)
el.nodes[0].int_label = int(rw[2])
el.mbclass = 'elem.joint'
if el.name in bpy.data.objects.keys():
el.blender_object = el.name
el.is_imported = True
pass
except KeyError:
el = ed.add()
el.mbclass = 'elem.joint'
el.type = 'modal'
el.int_label = int(rw[1])
eldbmsg({'PARSE_ELEM'}, "BLENDYN::parse_beamslider()", el)
eldbmsg({'NOTFOUND_DICT'}, "BLENDYN::parse_beamslider()", el)
el.nodes.add()
el.nodes[0].int_label = int(rw[2])
el.import_function = "blendyn.import_modal"
# el.info_draw = "beamslider_info_draw"
el.name = el.type + "_" + str(el.int_label)
el.is_imported = True
ret_val = False
pass
return ret_val
# --------------------------------------------------------------------------
# end of function parse_modal()
# function that displays modal info in panel -- [ optional ]
def modal_info_draw(elem, layout):
nd = bpy.context.scene.mbdyn.nodes
row = layout.row()
col = layout.column(align=True)
try:
node = nd['node_' + str(elem.nodes[0].int_label)]
# Display modal node info
col.prop(node, "int_label", text="Node ID ")
col.prop(node, "string_label", text="Node label ")
col.prop(node, "blender_object", text="Node Object: ")
col.enabled = False
# Display offset of modal node info
row = layout.row()
row.label(text="offset 1 w.r.t. Node " + node.string_label + " R.F.")
col = layout.column(align=True)
col.prop(elem.offsets[0], "value", text="", slider=False)
node = nd['node_' + str(elem.nodes[1].int_label)]
layout.separator()
return {'FINISHED'}
except KeyError:
return {'NODE_NOTFOUND'}
# -----------------------------------------------------------
# end of modal_info_draw() function
# Create an object representing modal joint element
def spawn_modal_element(elem, context):
""" Draws a modal joint element, loading a wireframe
object from the addon library """
mbs = context.scene.mbdyn
nd = mbs.nodes
if any(obj == elem.blender_object for obj in context.scene.objects.keys()):
return {'OBJECT_EXISTS'}
try:
n = nd['node_' + str(elem.nodes[0].int_label)].blender_object
except KeyError:
return {'NODE_NOTFOUND'}
# nodes' objects
nOBJ = bpy.data.objects[n]
try:
set_active_collection('joints')
elcol = bpy.data.collections.new(name=elem.name)
bpy.data.collections['joints'].children.link(elcol)
set_active_collection(elcol.name)
# load the wireframe beam slider joint object from the library
bpy.ops.wm.append(directory=os.path.join(mbs.addon_path, \
'library', 'joints.blend', 'Object'), filename='modal')
# the append operator leaves just the imported object selected
modaljOBJ = bpy.context.selected_objects[0]
modaljOBJ.name = elem.name
# automatic scaling
s = nOBJ.scale.magnitude*(1./sqrt(3.))
modaljOBJ.scale = Vector((s, s, s))
# project offsets in global frame
pN, q, S = nOBJ.matrix_basis.decompose()
# place the joint object in the position of the node (no offset)
modaljOBJ.location = pN
modaljOBJ.rotation_mode = 'QUATERNION'
modaljOBJ.rotation_quaternion = Quaternion(q)
# set parenting of wireframe obj
parenting(modaljOBJ, nOBJ)
modaljOBJ.mbdyn.dkey = elem.name
modaljOBJ.mbdyn.type = 'element'
elem.blender_object = modaljOBJ.name
# set collections
elcol.objects.link(nOBJ)
set_active_collection('Master Collection')
return {'FINISHED'}
except FileNotFoundError:
return {'LIBRARY_ERROR'}
except KeyError:
return {'COLLECTION_ERROR'}
# -----------------------------------------------------------
# end of spawn_modal_element() function
class BLENDYN_OT_import_modal(bpy.types.Operator):
""" Imports and modal joint element
into the Blender scene """
bl_idname = "blendyn.import_modal"
bl_label = "Imports and modal joint element"
int_label: bpy.props.IntProperty()
def draw(self, context):
layout = self.layout
layout.alignment = 'LEFT'
def execute(self, context):
ed = bpy.context.scene.mbdyn.elems
nd = bpy.context.scene.mbdyn.nodes
try:
elem = ed['modal_' + str(self.int_label)]
retval = spawn_modal_element(elem, context)
if retval == {'OBJECT_EXISTS'}:
eldbmsg(retval, type(self).__name__ + '::execute()', elem)
return {'CANCELLED'}
elif retval == {'NODE_NOTFOUND'}:
eldbmsg(retval, type(self).__name__ + '::execute()', elem)
return {'CANCELLED'}
elif retval == {'LIBRARY_ERROR'}:
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:
# Should nod be reached
return retval
except KeyError:
eldbmsg({'DICT_ERROR'}, type(self).__name__ + '::execute()', None)
return {'CANCELLED'}
# -----------------------------------------------------------
# end of BLENDYN_OT_import_modal class.
def parse_modal_fem_file(elem, context):
mbs = context.scene.mbdyn
fem_file = elem.fem_path
# Debug message to console
print("Blendyn::parse_modal_fem_file(): Trying to read fem nodes from file: " \
+ fem_file)
ret_val = {''}
# Check if nodes and fem nodes collections are already present (not the first import).
# if not, create them
try:
ncol = bpy.data.collections['mbdyn.nodes']
except KeyError:
ncol = bpy.data.collections.new(name='mbdyn.nodes')
bpy.context.scene.collection.children.link(ncol)
try:
fncol = ncol.children['fem_nodes']
except KeyError:
fncol = bpy.data.collections.new(name='fem_nodes')
ncol.children.link(fncol)
try:
with open(fem_file) as ff:
# open the reader, skipping initial whitespaces
reader = csv.reader(ff, delimiter=' ', skipinitialspace=True)
rw = next(reader)
while len(rw) < 4 or rw[1]+" "+rw[2]+" "+rw[3] != "RECORD GROUP 1,":
rw = next(reader)
# Move down two rows
rw = next(reader)
rw = next(reader)
elem.nb_modal_node = int(rw[1])
elem.nb_modal_mode = int(rw[2])
rw = next(reader)
while len(rw)< 4 or rw[1]+" "+rw[2]+" "+rw[3] != "RECORD GROUP 2,":
rw = next(reader)
# Move down one rows
rw = next(reader)
modal_node_list = []
while len(rw)>0 and rw[0] != '**':
for node_label in rw:
try:
node = elem.modal_node['node_' + str(elem.int_label) + '_' + str(node_label)]
node.is_imported = True
node.mbclass = 'node.struct'
except KeyError:
node = elem.modal_node.add()
node.mbclass = 'node.struct'
node.is_imported = True
node.modal_int_label = int(node_label)
node.name = 'node_' + str(elem.int_label) + '_' + str(node_label)
modal_node_list.append(node.name)
rw = next(reader)
while len(rw)< 4 or rw[1]+" "+rw[2]+" "+rw[3] != "RECORD GROUP 5,":
rw = next(reader)
# Parse initial x position of modal nodes
rw = next(reader)
for modal_node_name in modal_node_list:
elem.modal_node[modal_node_name].relative_pos[0] = float(rw[0])
rw = next(reader)
# Parse initial y position of modal nodes
rw = next(reader)
rw = next(reader)
for modal_node_name in modal_node_list:
elem.modal_node[modal_node_name].relative_pos[1] = float(rw[0])
rw = next(reader)
# Parse initial z position of modal nodes
rw = next(reader)
rw = next(reader)
for modal_node_name in modal_node_list:
elem.modal_node[modal_node_name].relative_pos[2] = float(rw[0])
rw = next(reader)
# Parse the mode shape
rw = next(reader)
rw = next(reader)
for _ in range(int(elem.nb_modal_mode)):
mode = rw[-1]
rw = next(reader)
for modal_node_name in modal_node_list:
try:
elem.modal_node[modal_node_name].mode[str(mode)].mode_shape = Vector(( float(rw[0]), float(rw[1]), float(rw[2])))
rw = next(reader)
except KeyError:
new_mode = elem.modal_node[modal_node_name].mode.add()
new_mode.name = str(mode)
new_mode.mode_shape = Vector((float(rw[0]), float(rw[1]), float(rw[2])))
rw = next(reader)
elem.is_ready = True
ret_val = {'FINISHED'}
except IOError:
print("Blendyn::parse_modal_fem_file(): Could not locate the file " + fem_file + ".")
ret_val = {'FEM_NOT_FOUND'}
pass
except StopIteration:
print("Blendyn::parse_modal_fem_file() Reached the end of .log file")
pass
return ret_val
#-----------------------------------------------------
# end of parse_modal_fem_file() function
def spawn_modal_node_obj(context, elem, modal_node):
mbs = context.scene.mbdyn
nd = mbs.nodes
elem_node = nd['node_'+str(elem.nodes[0].int_label)]
try:
set_active_collection(elem.name)
except KeyError:
set_active_collection('joints')
elcol = bpy.data.collections.new(name=elem.name)
bpy.data.collections['joints'].children.link(elcol)
set_active_collection(elem.name)
if (modal_node.string_label in bpy.data.objects) or ("node_" + str(elem.int_label) +'_'+str(modal_node.modal_int_label) in bpy.data.objects):
return False
modal_location = Vector(((elem_node.initial_pos[0] + modal_node.relative_pos[0]),
(elem_node.initial_pos[1] + modal_node.relative_pos[1]),
(elem_node.initial_pos[2] + modal_node.relative_pos[2])))
if mbs.node_object == "ARROWS":
bpy.ops.object.empty_add(type='ARROWS', location=modal_location)
return True
elif mbs.node_object == "AXES":
bpy.ops.object.empty_add(type='PLAIN_AXES', location=modal_location)
return True
elif mbs.node_object == "CUBE":
bpy.ops.mesh.primitive_cube_add(location=modal_location)
return True
elif mbs.node_object == "UVSPHERE":
bpy.ops.mesh.primitive_uv_sphere_add(location=modal_location)
return True
elif mbs.node_object == "NSPHERE":
bpy.ops.surface.primitive_nurbs_surface_sphere_add(location=modal_location)
return True
elif mbs.node_object == "CONE":
bpy.ops.mesh.primitive_cone_add(location=modal_location)
return True
else:
return False
# -----------------------------------------------------------
# end of spawn_modal_node_obj() function