Merging Sum Edge Lengths #1

Merged
Nika Kutsniashvili merged 1 commits from Jacob-Beatty/measureit:main into main 2024-09-18 13:23:43 +02:00
2 changed files with 60 additions and 0 deletions
Showing only changes of commit f31b8cb597 - Show all commits

View File

@ -34,6 +34,8 @@ from bpy.props import (
EnumProperty, EnumProperty,
) )
from .measureit_sum_edge_lengths import *
# -------------------------------------------------------------- # --------------------------------------------------------------
# Register all operators and panels # Register all operators and panels
# -------------------------------------------------------------- # --------------------------------------------------------------
@ -389,6 +391,9 @@ def register():
# register internal property # register internal property
wm.measureit_run_opengl = BoolProperty(default=False) wm.measureit_run_opengl = BoolProperty(default=False)
#Register Sum Edge Lengths
bpy.utils.register_class(Sum_Edge_Lengths)
bpy.types.VIEW3D_MT_edit_mesh_edges.append(measureit_sum_edge_lengths.menu_func)
def unregister(): def unregister():
from bpy.utils import unregister_class from bpy.utils import unregister_class
@ -460,6 +465,9 @@ def unregister():
if p in wm: if p in wm:
del wm[p] del wm[p]
#Remove Sum Edge Lengths
bpy.utils.unregister_class(Sum_Edge_Lengths)
bpy.types.VIEW3D_MT_edit_mesh_edges.remove(measureit_sum_edge_lengths.menu_func)
if __name__ == '__main__': if __name__ == '__main__':
register() register()

View File

@ -0,0 +1,52 @@
import bpy
import bmesh
class Sum_Edge_Lengths(bpy.types.Operator):
bl_idname = "mesh.sum_edge_lengths"
bl_label = "Sum Edge Length"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
obj = context.object
if obj is None or obj.type != 'MESH':
self.report({'WARNING'}, "No mesh object selected")
return {'CANCELLED'}
bm = bmesh.from_edit_mesh(obj.data)
total_length = sum(e.calc_length() for e in bm.edges if e.select)
# Adjust for unit scale
unit_settings = context.scene.unit_settings
unit_scale = unit_settings.scale_length
total_length *= unit_scale
# Convert to the appropriate unit
if unit_settings.length_unit == 'METERS':
unit_length = 'm'
elif unit_settings.length_unit == 'CENTIMETERS':
total_length *= 100
unit_length = 'cm'
elif unit_settings.length_unit == 'MILLIMETERS':
total_length *= 1000
unit_length = 'mm'
elif unit_settings.length_unit == 'KILOMETERS':
total_length /= 1000
unit_length = 'km'
elif unit_settings.length_unit == 'INCHES':
total_length *= 39.3701
unit_length = 'in'
elif unit_settings.length_unit == 'FEET':
total_length *= 3.28084
unit_length = 'ft'
elif unit_settings.length_unit == 'MILES':
total_length /= 1609.34
unit_length = 'mi'
else:
unit_length = 'm' # Default to meters if unit is unknown
context.workspace.status_text_set(f"Total Edge Length: {total_length:.3f} {unit_length}")
self.report({'INFO'}, f"Total Edge Length: {total_length:.3f} {unit_length}")
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(Sum_Edge_Lengths.bl_idname)