Skip to content

Commit

Permalink
Merge pull request #357 from AlpenAalAlex/issue349-Exporting-URDF-wit…
Browse files Browse the repository at this point in the history
…h-0-mass-causes-divide-by-zero-error

Fix merging inertials with mass 0
  • Loading branch information
hwiedPro authored Jun 13, 2024
2 parents e359008 + ad53079 commit b0d7dc9
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion phobos/blender/model/inertia.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,35 @@ def combine_com_3x3(objects):
for obj in objects:
combined_com = combined_com + obj.matrix_local.translation * obj['mass']
combined_mass += obj['mass']
combined_com = combined_com / combined_mass
if combined_mass != 0:
combined_com = combined_com / combined_mass
else:
combined_com = mathutils.Vector((0.0,) * 3)
combined_volume = 0
for obj in objects:
# 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 b0d7dc9

Please sign in to comment.