-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcomponents.py
388 lines (325 loc) · 14.3 KB
/
components.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
# --------------------------------------------------------------------------
# Blendyn -- file components.py
# Copyright (C) 2020 Andrea Zanoni -- andrea.zanoni@polimi.it
# --------------------------------------------------------------------------
# ***** 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 os
import bpy
from bpy.props import *
import logging
baseLogger = logging.getLogger()
baseLogger.setLevel(logging.DEBUG)
from mathutils import *
from math import *
from .componentlib import *
class BLENDYN_PG_component_element(bpy.types.PropertyGroup):
""" Element associated to a component """
elem: StringProperty(
name = "name of component element",
description = "Name of component element in elements collection"
)
str_idx: IntProperty(
name = "index of element",
description = "Index of element in component structure",
update = update_elem_str_idx
)
arm_ns: IntProperty(
name = "armature subdivisions",
description = "number of subdivisions in components' element armature",
default = 5,
min = 2
)
# -----------------------------------------------------------
# end of BLENDYN_PG_components_element class
bpy.utils.register_class(BLENDYN_PG_component_element)
class BLENDYN_PG_components_dictionary(bpy.types.PropertyGroup):
""" Data of a component (structural model + geometry) """
elements: CollectionProperty(
type = BLENDYN_PG_component_element,
description = "Pointer to id_data of MBDyn elements"
)
el_index: IntProperty(
name = "Component element index",
default = 0
)
object: EnumProperty(
items = get_comp_mesh_objects,
name = "Mesh Object",
)
mesh_ns: IntProperty(
name = "mesh subdivisions",
description = "number of subdivisions in components' mesh",
default = 20
)
armature: PointerProperty(
type = bpy.types.Armature,
name = "Armature",
description = "Component armature"
)
remove_from_etu: BoolProperty(
name = "Disable element update",
description = "Remove component elements from update list",
default = False
)
# -----------------------------------------------------------
# end of BLENDYN_PG_components_dictionary class
bpy.utils.register_class(BLENDYN_PG_components_dictionary)
class BLENDYN_UL_component_object_list(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
custom_icon = 'MESH_DATA'
if self.layout_type in {'DEFAULT', 'COMPACT'}:
layout.prop(item, "name", icon = custom_icon, emboss = False, text = "")
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text = '', icon = custom_icon)
# -----------------------------------------------------------
# end of BLENDYN_UL_component_object_list class
bpy.utils.register_class(BLENDYN_UL_component_object_list)
class BLENDYN_UL_components_list(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
custom_icon = 'MESH_DATA'
if self.layout_type in {'DEFAULT', 'COMPACT'}:
layout.prop(item, "name", icon = custom_icon, emboss = False, text = "")
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text = '', icon = custom_icon)
# -----------------------------------------------------------
# end of BLENDYN_UL_components_list class
bpy.utils.register_class(BLENDYN_UL_components_list)
class BLENDYN_UL_component_elements_list(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
if self.layout_type in {'DEFAULT', 'COMPACT'}:
row = layout.row()
split = row.split(factor = 0.2)
col = split.column()
col.prop(item, "str_idx", icon = 'CUBE', text = "" )
col = split.column()
split = col.split(factor = .67)
col = split.column()
col.label(text = item.elem)
col = split.column()
col.prop(item, "arm_ns", icon = 'MOD_ARRAY', text = "" )
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text = '', icon = 'CUBE')
# -----------------------------------------------------------
# end of BLENDYN_UL_component_elements_list class
bpy.utils.register_class(BLENDYN_UL_component_elements_list)
class BLENDYN_OT_component_add(bpy.types.Operator):
""" Sets the adding_component flag to True """
bl_idname = "blendyn.add_component"
bl_label = "Add an MBDyn component"
def execute(self, context):
mbs = context.scene.mbdyn
comp = mbs.components.add()
comp.name = 'component_' + str(len(mbs.components))
mbs.cd_index = len(mbs.components) - 1
mbs.adding_component = True
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
# -----------------------------------------------------------
# end of BLENDYN_OT_component_add class
class BLENDYN_OT_component_edit(bpy.types.Operator):
""" Open component for editing """
bl_idname = "blendyn.edit_component"
bl_label = "Edit selected MBDyn component"
def execute(self, context):
mbs = context.scene.mbdyn
component = mbs.components[mbs.cd_index]
mccol = bpy.data.collections['mbdyn.components']
mbs.editing_component = True
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
# -----------------------------------------------------------
# end of BLENDYN_OT_component_edit class
class BLENDYN_OT_component_remove(bpy.types.Operator):
""" Removes component """
bl_idname = "blendyn.remove_component"
bl_label = "Remove selected MBDyn component"
def execute(self, context):
mbs = context.scene.mbdyn
component = mbs.components[mbs.cd_index]
mccol = bpy.data.collections['mbdyn.components']
if component.object:
mccol.objects.unlink(bpy.data.objects[component.object])
bpy.data.armatures.remove(component.armature)
mbs.components.remove(mbs.cd_index)
mbs.cd_index -= 1
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
# -----------------------------------------------------------
# end of BLENDYN_OT_component_remove class
class BLENDYN_OT_component_add_cancel(bpy.types.Operator):
""" Cancels adding the component """
bl_idname = "blendyn.add_component_cancel"
bl_label = "Cancel add of MBDyn component"
def execute(self, context):
selftag = "BLENDYN_OT_component_add_cancel::execute(): "
mbs = context.scene.mbdyn
mbs.components.remove(mbs.cd_index)
mbs.adding_component = False
mbs.cd_index -= 1
message = "component add aborted."
print(message)
baseLogger.info(selftag + message)
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
# -----------------------------------------------------------
# end of BLENDYN_OT_component_add_cancel class
class BLENDYN_OT_component_edit_cancel(bpy.types.Operator):
""" Cancels editing the current component """
bl_idname = "blendyn.edit_component_cancel"
bl_label = "Cancel edit of MBDyn component"
def execute(self, context):
selftag = "BLENDYN_OT_component_edit_cancel::execute(): "
mbs = context.scene.mbdyn
mbs.editing_component = False
message = "component edit aborted."
print(message)
baseLogger.info(selftag + message)
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
# -----------------------------------------------------------
# end of BLENDYN_OT_component_edit_cancel class
class BLENDYN_OT_component_add_confirm(bpy.types.Operator):
""" Adds the component and sets adding_component flag to False """
bl_idname = "blendyn.add_component_confirm"
bl_label = "Confirm add of MBDyn component"
def execute(self, context):
selftag = "BLENDYN_OT_component_add_confirm::execute(): "
mbs = context.scene.mbdyn
component = mbs.components[mbs.cd_index]
retval = add_mesh_component(context, component)
mbs.adding_component = False
if retval == {'ELEM_TYPE_UNSUPPORTED'}:
# should not be reached
message = "unknown element type in component!"
print(message)
baseLogger.error(selftag + message)
return {'CANCELLED'}
elif retval == {'ZERO VOLUME BONE'}:
message = "cannot create bone from one node or two nodes at the same position."
print(message)
baseLogger.error(selftag + message)
elif retval == {'ARMATURE_PARENT_FAILED'}:
message = "unable to parent mesh to armature."
print(message)
baseLogger.warning(selftag + message)
return {'CANCELLED'}
elif retval == {'FINISHED'}:
message = "Added component " + component.name + "."
print(message)
baseLogger.info(selftag + message)
return retval
def invoke(self, context, event):
return self.execute(context)
# -----------------------------------------------------------
# end of BLENDYN_OT_component_add_confirm class
class BLENDYN_OT_component_edit_confirm(bpy.types.Operator):
""" Confirms the modifications to the current component """
bl_idname = "blendyn.edit_component_confirm"
bl_label = "Confirm edit of MBDyn component"
def execute(self, context):
selftag = "BLENDYN_OT_component_edit_confirm::execute(): "
mbs = context.scene.mbdyn
component = mbs.components[mbs.cd_index]
message = "edited component {}".format(component.name)
print(message)
baseLogger.info(selftag + message)
mbs.editing_component = False
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
# -----------------------------------------------------------
# end of BLENDYN_OT_component_edit_confirm class
class BLENDYN_OT_component_add_elem(bpy.types.Operator):
""" Adds and element to the component list, checking
that is not already present """
bl_idname = "blendyn.component_add_elem"
bl_label = "Add an element to a MBDyn component"
def execute(self, context):
mbs = context.scene.mbdyn
component = mbs.components[mbs.cd_index]
if mbs.comp_selected_elem not in component.elements.keys():
celem = component.elements.add()
celem.elem = mbs.elems[mbs.comp_selected_elem].name
celem.str_idx = len(component.elements) - 1
celem.name = mbs.elems[mbs.comp_selected_elem].name
return {'FINISHED'}
else:
return {'CANCELLED'}
def invoke(self, context, event):
return self.execute(context)
# -----------------------------------------------------------
# end of BLENDYN_OT_component_add_elem class
class BLENDYN_OT_component_remove_elem(bpy.types.Operator):
""" Removes and element to the component list, reordering
the others """
bl_idname = "blendyn.component_remove_elem"
bl_label = "Remove an element from a MBDyn component"
def execute(self, context):
mbs = context.scene.mbdyn
component = mbs.components[mbs.cd_index]
component.elements.remove(component.el_index)
for idx in range(len(component.elements)):
component.elements[idx].str_idx = idx
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
# -----------------------------------------------------------
# end of BLENDYN_OT_component_remove_elem class
class BLENDYN_OT_component_remove_all_elems(bpy.types.Operator):
""" Removes all elements to the component list """
bl_idname = "blendyn.component_remove_all_elems"
bl_label = "Remove an element from a MBDyn component"
def execute(self, context):
mbs = context.scene.mbdyn
component = mbs.components[mbs.cd_index]
component.elements.clear()
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
# -----------------------------------------------------------
# end of BLENDYN_OT_component_remove_all_elems class
class BLENDYN_OT_component_add_selected_elems(bpy.types.Operator):
""" Add all the selected elements into the component list """
bl_idname = "blendyn.component_add_selected_elems"
bl_label = "Add selected elements to component"
def execute(self, context):
mbs = context.scene.mbdyn
ed = mbs.elems
component = mbs.components[mbs.cd_index]
sel_elems = [ed[obj.mbdyn.dkey] for obj in bpy.context.selected_objects \
if (obj.mbdyn.type == 'element') and (ed[obj.mbdyn.dkey].type not in {'modal'}) and (ed[obj.mbdyn.dkey].type in DEFORMABLE_ELEMENTS) ]
for idx in range(len(sel_elems)):
celem = component.elements.add()
celem.elem = sel_elems[idx].name
celem.str_idx = idx
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
# -----------------------------------------------------------
# end of BLENDYN_OT_component_add_selected_elems class