-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathelements.py
executable file
·341 lines (286 loc) · 11.8 KB
/
elements.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
# --------------------------------------------------------------------------
# Blendyn -- file elements.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
from bpy.types import Operator, Panel
from bpy.props import *
import logging
from mathutils import *
from math import *
import ntpath, os, csv
from .elementlib import *
class BLENDYN_PG_elem_pos_offset(bpy.types.PropertyGroup):
value: FloatVectorProperty(
name = "Offset value",
description = "The offset vector, with respect to the node",
size = 3,
precision = 6
)
# -----------------------------------------------------------
# end of BLENDYN_PG_elem_pos_offset class
bpy.utils.register_class(BLENDYN_PG_elem_pos_offset)
class BLENDYN_PG_elem_rot_offset(bpy.types.PropertyGroup):
value: FloatVectorProperty(
name = "Offset value",
description = "The rotational offset quaternion, with respect to the node",
size = 4,
precision = 6
)
# -----------------------------------------------------------
# end of BLENDYN_PG_elem_rot_offset class
bpy.utils.register_class(BLENDYN_PG_elem_rot_offset)
class BLENDYN_PG_nodes_collection(bpy.types.PropertyGroup):
int_label: IntProperty(
name = "MBDyn node ID",
description = "",
)
# -----------------------------------------------------------
# end of BLENDYN_PG_nodes_collection class
bpy.utils.register_class(BLENDYN_PG_nodes_collection)
class BLENDYN_PG_elem_to_be_updated(bpy.types.PropertyGroup):
dkey: StringProperty(
name = "Key of element to be updated",
description = "",
)
# -----------------------------------------------------------
# end of BLENDYN_PG_elem_to_be_updated class
bpy.utils.register_class(BLENDYN_PG_elem_to_be_updated)
## Modes Dictionary: contains modes information for each node
class BLENDYN_PG_modal_modes_dictionary(bpy.types.PropertyGroup):
mode_shape: FloatVectorProperty(
name="Mode shape value",
description="Mode boundary in three directions",
size=3,
precision=6
)
# -----------------------------------------------------------
# end of BLENDYN_PG_modal_modes_dictionary class
bpy.utils.register_class(BLENDYN_PG_modal_modes_dictionary)
## Nodes Dictionary: contains nodes informations
class BLENDYN_PG_modal_nodes_dictionary(bpy.types.PropertyGroup):
mbclass: StringProperty(
name = "Class of MBDyn element",
description = ""
)
modal_int_label: IntProperty(
name = "Modal node integer label",
description = "Modal node integer label",
)
string_label: StringProperty(
name = "node string label",
description = "Node string label",
default = "none"
)
blender_object: StringProperty(
name = "blender object label",
description = "Blender object",
default = "none"
)
relative_pos: FloatVectorProperty(
name = "modal node relative position",
description = "Modal node relative position with reference node",
size = 3,
precision = 6
)
is_imported: BoolProperty(
name = "Is imported flag",
description = "Flag set to true at the end of the import process"
)
mode: CollectionProperty(
name = "Modal mode",
description = "Modal mode",
type = BLENDYN_PG_modal_modes_dictionary
)
# -----------------------------------------------------------
# end of BLENDYN_PG_modal_nodes_dictionary class
bpy.utils.register_class(BLENDYN_PG_modal_nodes_dictionary)
class BLENDYN_PG_elems_dictionary(bpy.types.PropertyGroup):
mbclass: StringProperty(
name = "Class of MBDyn element",
description = ""
)
fem_path: StringProperty(
name="FEM File Path",
description="Path of Modal FEM file",
default="not yet selected"
)
modal_node: CollectionProperty(
name = "Modal nodes",
type = BLENDYN_PG_modal_nodes_dictionary
)
nb_modal_node: IntProperty(
name="Number of FEM nodes",
description= "Number of current FEM nodes if the element is modal joints"
)
nb_modal_mode: IntProperty(
name = "Number of Modal modes",
description = "Number of modal modes"
)
type: StringProperty(
name = "Type of MBDyn element",
description = ""
)
int_label: IntProperty(
name = "Integer label of element",
description = ""
)
string_label: StringProperty(
name = "String label of element",
description = "",
default = "none"
)
nodes: CollectionProperty(
type = BLENDYN_PG_nodes_collection,
name = "Connected nodes",
description = "MBDyn nodes that the element connects"
)
magnitude: FloatProperty(
name = "Magnitude of element",
description = "Magnitude of element if present (e.g. force, couple...)",
default = 0.
)
offsets: CollectionProperty(
type = BLENDYN_PG_elem_pos_offset,
name = "Offsets of attach points",
description = "Collector of offsets of element attaching points"
)
rotoffsets: CollectionProperty(
type = BLENDYN_PG_elem_rot_offset,
name = "Rotational offsets of attach R.Fs. of joint",
description = "Collector of rotational offsets of element attach R.Fs."
)
beam_number: IntProperty(
name="Number beam3",
description="Number of beam3 connected with beam slider"
)
import_function: StringProperty(
name = "Import operator",
description = "Id name of the class defining the import operator for the element"
)
draw_function: StringProperty(
name = "Draw function",
description = "Id of the function that is called when the element configuration is updated",
)
update_info_operator: StringProperty(
name = "Update operator",
description = "Id name of the operator that updates the element info using \
the current position of the blender objects representing it",
default = 'none'
)
write_operator: StringProperty(
name = "Input write operator",
description = "Id name of the operator that writes the element input contribute",
default = 'none'
)
info_draw: StringProperty(
name = "Element Info Function",
description = "Name of the function used to display element data in the View3D panel",
default = "elem_info_draw"
)
blender_object: StringProperty(
name = "Blender Object",
description = "Name of the Blender Object associated with this element"
)
is_imported: BoolProperty(
name = "Is imported flag",
description = "Flag set to true at the end of the import process"
)
update: StringProperty(
name = "Update function",
description = "Function that updates the visualization of the element",
default = 'none'
)
# -----------------------------------------------------------
# end of BLENDYN_PG_elems_dictionary class
bpy.utils.register_class(BLENDYN_PG_elems_dictionary)
# Override delete operator to remove element from elements dictionary
# when the related object is deleted
class BLENDYN_OT_import_elements_as_mesh(bpy.types.Operator):
""" Imports all the elements selected (by type and label range) into a single
mesh Blender Object. Currently useful only for shell4 elements. """
bl_label = "Import MBDyn elements as single mesh"
bl_idname = "blendyn.import_elments_asmesh"
def execute(self, context):
mbs = context.scene.mbdyn
ed = mbs.elems
nd = mbs.nodes
verts = list()
faces = list()
nodes = list()
elems = list()
for elem in ed:
if (elem.type == mbs.elem_type_import) \
and (elem.int_label >= mbs.min_elem_import) \
and (elem.int_label <= mbs.max_elem_import):
elems.append(elem.name)
n1 = 'node_' + str(elem.nodes[0].int_label)
n2 = 'node_' + str(elem.nodes[1].int_label)
n3 = 'node_' + str(elem.nodes[2].int_label)
n4 = 'node_' + str(elem.nodes[3].int_label)
nodes.extend((n1, n2, n3, n4))
node_set = set(nodes)
for node in node_set:
try:
verts.append(bpy.data.objects[nd[node].blender_object].location)
except KeyError:
message = type(self).__name__ + "::execute(): "\
+ "Could not find Blender Objects for " \
+ elem.name + " import"
self.report({'ERROR'}, message)
logging.error(message)
node_to_vert = dict(zip((node_set), range(len(verts))))
for ekey in elems:
elem = ed[ekey]
faces.append(\
(node_to_vert['node_' + str(elem.nodes[0].int_label)],\
node_to_vert['node_' + str(elem.nodes[1].int_label)],\
node_to_vert['node_' + str(elem.nodes[2].int_label)],\
node_to_vert['node_' + str(elem.nodes[3].int_label)]),\
)
if len(verts) and len(faces):
objname = mbs.elem_type_import + '_' + \
str(mbs.min_elem_import) + '_' + \
str(mbs.max_elem_import)
mesh = bpy.data.meshes.new(objname + '_mesh')
mesh.from_pydata(verts, [], faces)
mesh.update()
obj = bpy.data.objects.new(objname, mesh)
context.scene.collection.objects.link(obj)
obj.select_set(state = True)
context.view_layer.objects.active = obj
for node in node_set:
vg = obj.vertex_groups.new('v-' + str(node_to_vert[node]))
vg.add([node_to_vert[node]], 1.0, 'ADD')
hook_mod = obj.modifiers.new('Hook' + vg.name, 'HOOK')
hook_mod.object = bpy.data.objects[nd[node].blender_object]
hook_mod.vertex_group = vg.name
hook_mod.show_in_editmode = True
return {'FINISHED'}
else:
message = type(self).__name__ + "::execute(): "\
+ "No mesh data was created"
self.report({'WARNING'}, message)
logging.warning(message)
return {'CANCELLED'}
# -----------------------------------------------------------
# end of BLENDYN_OT_import_elements_as_mesh class