[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [2752] trunk/py/scripts/addons/ space_view3d_panel_measure.py: * Measure Panel - Version 0.8.2

Martin Buerbaum martin.buerbaum at gmx.at
Thu Dec 8 12:05:33 CET 2011


Revision: 2752
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=2752
Author:   pontiac
Date:     2011-12-08 11:05:32 +0000 (Thu, 08 Dec 2011)
Log Message:
-----------
* Measure Panel - Version 0.8.2
* Added support for edge length measurement
* Updated API revision and Blender version
* Rearranged some things to make code clearer - it's still pretty cumbersome :-(.
* Also moved "Update selection" button to the very top.

Modified Paths:
--------------
    trunk/py/scripts/addons/space_view3d_panel_measure.py

Modified: trunk/py/scripts/addons/space_view3d_panel_measure.py
===================================================================
--- trunk/py/scripts/addons/space_view3d_panel_measure.py	2011-12-07 17:25:18 UTC (rev 2751)
+++ trunk/py/scripts/addons/space_view3d_panel_measure.py	2011-12-08 11:05:32 UTC (rev 2752)
@@ -26,9 +26,9 @@
     "name": "Measure Panel",
     "author": "Buerbaum Martin (Pontiac), TNae (Normal patch)," \
         " Benjamin Lauritzen (Loonsbury; Volume code)",
-    "version": (0, 8, 1),
-    "blender": (2, 5, 7),
-    "api": 40847,
+    "version": (0, 8, 2),
+    "blender": (2, 6, 0),
+    "api": 42508,
     "location": "View3D > Properties > Measure Panel",
     "description": "Measure distances between objects",
     "warning": "Script needs repairs",
@@ -206,6 +206,51 @@
             return None
 
 
+# Return the length of an edge (in global space if "obj" is set).
+# Respects the scaling (via the "obj.matrix_world" parameter).
+def edgeLengthGlobal(edge, obj, globalSpace):
+    v1, v2 = edge.vertices
+
+    # Get vertex data
+    v1 = obj.data.vertices[v1]
+    v2 = obj.data.vertices[v2]
+
+    if globalSpace:
+        mat = obj.matrix_world
+        # Apply transform matrix to vertex coordinates.
+        v1 = mat * v1.co
+        v2 = mat * v2.co
+    else:
+        v1 = v1.co
+        v2 = v2.co
+
+    return (v1 - v2).length
+
+
+# Calculate the edge length of a mesh object.
+# *) Set selectedOnly=1 if you only want to count selected edges.
+# *) Set globalSpace=1 if you want to calculate
+#    the global edge length (object mode).
+# Note: Be sure you have updated the mesh data before
+#       running this with selectedOnly=1!
+# @todo Support other object types (surfaces, etc...)?
+def objectEdgeLength(obj, selectedOnly, globalSpace):
+    if (obj and obj.type == 'MESH' and obj.data):
+        edgeTotal = 0
+
+        mesh = obj.data
+
+        # Count the length of all edges.
+        for ed in mesh.edges:
+            if not selectedOnly or ed.select:
+                edgeTotal += edgeLengthGlobal(ed, obj, globalSpace)
+
+        return edgeTotal
+
+    # We can not calculate a length for this object.
+    return -1
+
+
 # Return the area of a face (in global space).
 # @note Copies the functionality of the following functions,
 # but also respects the scaling (via the "obj.matrix_world" parameter):
@@ -587,6 +632,29 @@
 
             loc_y -= OFFSET_Y
 
+    if (sce.measure_panel_calc_edge_length):
+        if (context.mode == 'EDIT_MESH'):
+            obj = context.active_object
+
+            length_total = objectEdgeLength(obj, True, measureGlobal(sce))
+
+            sce.measure_panel_edge_length = length_total
+
+        elif (context.mode == 'OBJECT'):
+            length_total = -1
+
+            for o in context.selected_objects:
+                if (o.type == 'MESH'):
+                    length = objectEdgeLength(o, False, measureGlobal(sce))
+
+                    if length >= 0:
+                        if length_total < 0:
+                            length_total = 0
+
+                        length_total += length
+
+            sce.measure_panel_edge_length = length_total
+
     # Handle mesh surface area calulations
     if (sce.measure_panel_calc_area):
         # Get a single selected object (or nothing).
@@ -816,7 +884,6 @@
     def draw(self, context):
         layout = self.layout
         sce = context.scene
-        layout.label(text="Distance")
 
         # Get a single selected object (or nothing).
         obj = getSingleObject(context)
@@ -826,6 +893,16 @@
         if (context.mode == 'EDIT_MESH'):
             obj = context.active_object
 
+            row = layout.row()
+            row.operator("view3d.reenter_editmode",
+                text="Update selection")
+#               @todo
+#                description="The calculated values can" \
+#                    " not be updated in mesh edit mode" \
+#                    " automatically. Press this button" \
+#                    " to do this manually, after you changed" \
+#                    " the selection")
+
             if (obj and obj.type == 'MESH' and obj.data):
                 # "Note: a Mesh will return the selection state of the mesh
                 # when EditMode was last exited. A Python script operating
@@ -854,6 +931,7 @@
                     # We measure the distance from...
                     # local  ... the object center to the 3D cursor.
                     # global ... the origin to the 3D cursor.
+                    layout.label(text="Distance")
 
                     box = layout.box()
                     row = box.row()
@@ -867,22 +945,13 @@
                     else:
                         row.label(text="Origin [0,0,0]")
 
-                    row = layout.row()
-                    row.operator("view3d.reenter_editmode",
-                        text="Update selection & distance")
-#                       @todo
-#                        description="The surface area value can" \
-#                            " not be updated in mesh edit mode" \
-#                            " automatically. Press this button" \
-#                            " to do this manually, after you changed" \
-#                            " the selection")
-
                     layout.prop(sce, "measure_panel_draw")
 
                 elif len(verts_selected) == 1:
                     # One vertex selected.
                     # We measure the distance from the
                     # selected vertex object to the 3D cursor.
+                    layout.label(text="Distance")
 
                     box = layout.box()
                     row = box.row()
@@ -893,16 +962,13 @@
                     row.label(text="", icon='ARROW_LEFTRIGHT')
                     row.label(text="", icon='VERTEXSEL')
 
-                    row = layout.row()
-                    row.operator("view3d.reenter_editmode",
-                        text="Update selection & distance")
-
                     layout.prop(sce, "measure_panel_draw")
 
                 elif len(verts_selected) == 2:
                     # Two vertices selected.
                     # We measure the distance between the
                     # two selected vertices.
+                    layout.label(text="Distance")
 
                     box = layout.box()
                     row = box.row()
@@ -913,16 +979,30 @@
                     row.label(text="", icon='ARROW_LEFTRIGHT')
                     row.label(text="", icon='VERTEXSEL')
 
+                    layout.prop(sce, "measure_panel_draw")
+
+                edges_selected = [ed for ed in mesh.edges if ed.select == 1]
+                if len(edges_selected) >= 1:
                     row = layout.row()
-                    row.operator("view3d.reenter_editmode",
-                        text="Update selection & distance")
+                    row.prop(sce, "measure_panel_calc_edge_length",
+                        text="Edge Length (selected edges)")
 
-                    layout.prop(sce, "measure_panel_draw")
+                    if (sce.measure_panel_calc_edge_length):
+                        if (sce.measure_panel_edge_length >= 0):
+                            box = layout.box()
+                            row = box.row()
+                            row.label(
+                                text=str(len(edges_selected)),
+                                icon='EDGESEL')
 
-                else:
+                            row = box.row()
+                            row.label(text="Length")
+                            row.prop(sce, "measure_panel_edge_length")
+
+                if len(verts_selected) > 2:
                     row = layout.row()
                     row.prop(sce, "measure_panel_calc_area",
-                        text="Surface area (selected faces):")
+                        text="Surface area (selected faces)")
 
                     if (sce.measure_panel_calc_area):
                         # Get selected faces
@@ -947,38 +1027,45 @@
                                 row = box.row()
                                 row.prop(sce, "measure_panel_normal1")
 
-                                row = layout.row()
-                                row.operator("view3d.reenter_editmode",
-                                    text="Update selection & area")
-
                         else:
                             row = layout.row()
                             row.label(text="Selection not supported",
                                 icon='INFO')
 
-                            row = layout.row()
-                            row.operator("view3d.reenter_editmode",
-                                text="Update selection")
+                if drawTansformButtons:
+                    row = layout.row()
+                    row.prop(sce,
+                        "measure_panel_transform",
+                        expand=True)
 
-                    else:
-                        row = layout.row()
-                        row.operator("view3d.reenter_editmode",
-                            text="Update selection")
-
         elif (context.mode == 'OBJECT'):
             # We are working in object mode.
 
+            mesh_objects = [o for o in context.selected_objects
+                        if (o.type == 'MESH')]
+
             if len(context.selected_objects) > 2:
                 # We have more that 2 objects selected...
 
+                # EDGES
                 row = layout.row()
+                row.prop(sce, "measure_panel_calc_edge_length",
+                    text="Edge Length")
+
+                if (sce.measure_panel_calc_edge_length):
+                    if (len(mesh_objects) > 0):
+                        box = layout.box()
+
+                        row = box.row()
+                        row.label(text="Total edge length")
+                        row.prop(sce, "measure_panel_edge_length")
+
+                # AREA
+                row = layout.row()
                 row.prop(sce, "measure_panel_calc_area",
-                        text="Surface area (selected faces):")
+                        text="Surface area")
 
                 if (sce.measure_panel_calc_area):
-                    mesh_objects = [o for o in context.selected_objects
-                        if (o.type == 'MESH')]
-
                     if (len(mesh_objects) > 0):
                         # ... and at least one of them is a mesh.
 
@@ -1005,6 +1092,7 @@

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list