From f31b8cb5972e0aaa5fcb663d971e11a6d8c63f0c Mon Sep 17 00:00:00 2001 From: jacobbeatty <54652544+jacobbeatty@users.noreply.github.com> Date: Sat, 14 Sep 2024 09:42:14 +0900 Subject: [PATCH] Merging Sum Edge Lengths Merging Sum Edge Lengths with MeasureIt per discussion at: https://extensions.blender.org/approval-queue/sum-edge-lengths/#activity-3493 --- source/__init__.py | 8 +++++ source/measureit_sum_edge_lengths.py | 52 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 source/measureit_sum_edge_lengths.py diff --git a/source/__init__.py b/source/__init__.py index 8ded119..2c21192 100644 --- a/source/__init__.py +++ b/source/__init__.py @@ -34,6 +34,8 @@ from bpy.props import ( EnumProperty, ) +from .measureit_sum_edge_lengths import * + # -------------------------------------------------------------- # Register all operators and panels # -------------------------------------------------------------- @@ -389,6 +391,9 @@ def register(): # register internal property 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(): from bpy.utils import unregister_class @@ -460,6 +465,9 @@ def unregister(): if p in wm: 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__': register() diff --git a/source/measureit_sum_edge_lengths.py b/source/measureit_sum_edge_lengths.py new file mode 100644 index 0000000..d741524 --- /dev/null +++ b/source/measureit_sum_edge_lengths.py @@ -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) \ No newline at end of file -- 2.30.2