Skip to content

Commit

Permalink
Fix - Weighing objects by volume if mass totals 0
Browse files Browse the repository at this point in the history
  • Loading branch information
AlpenAalAlex committed Jun 6, 2024
1 parent a5c80ab commit ad53079
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions phobos/blender/model/inertia.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,31 @@ def combine_com_3x3(objects):
combined_com = combined_com / combined_mass
else:
combined_com = mathutils.Vector((0.0,) * 3)
combined_volume = 0
for obj in objects:
combined_com = combined_com + obj.matrix_local.translation
log(message=f"Inertial {objects[0].name} exported with mass 0")
# calculate volume
mesh = representation.Mesh(
mesh=obj.data.copy(),
meshname=obj.data.name
)
volume, _ = mesh.approx_volume_and_com()
center = obj.matrix_local.translation

combined_volume += volume
combined_com = combined_com + center * volume
combined_com = combined_com / combined_volume

if len(objects) > 1:
all_objects = ""
for i in range(len(objects)):
obj = objects[i]
if i == 0:
all_objects += obj.name
elif i == len(objects)-1:
all_objects += " and "+obj.name
else:
all_objects += ", "+obj.name
log(message=f"Mass of {all_objects} totals 0. Weighing by volume", level='INFO')
log(" Combined center of mass: " + str(combined_com), 'DEBUG')
return combined_mass, combined_com

Expand Down

0 comments on commit ad53079

Please sign in to comment.