[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [554] trunk/py/scripts/addons/ space_view3d_panel_measure.py: * Version 0.6. 4 - Fixed unneeded meshdata duplication (sometimes crashes Blender).

Martin Buerbaum martin.buerbaum at gmx.at
Wed Apr 7 11:25:22 CEST 2010


Revision: 554
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=554
Author:   pontiac
Date:     2010-04-07 11:25:19 +0200 (Wed, 07 Apr 2010)

Log Message:
-----------
* Version 0.6.4 - Fixed unneeded meshdata duplication (sometimes crashes Blender).
* Bug http://projects.blender.org/tracker/?func=detail&atid=453&aid=21913&group_id=153
* The script now correctly calculated the surface area (faceAreaGlobal) of scaled meshes.

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	2010-04-06 13:08:51 UTC (rev 553)
+++ trunk/py/scripts/addons/space_view3d_panel_measure.py	2010-04-07 09:25:19 UTC (rev 554)
@@ -18,6 +18,7 @@
 
 import bpy
 from bpy.props import *
+from Mathutils import Vector, Matrix
 
 # Precicion for display of float values.
 PRECISION = 5
@@ -25,12 +26,12 @@
 bl_addon_info = {
     'name': '3D View: Measure panel',
     'author': 'Buerbaum Martin (Pontiac)',
-    'version': '0.6.3',
+    'version': '0.6.4',
     'blender': (2, 5, 3),
     'location': 'View3D > Properties > Measure',
     'description': 'Measure distances between objects',
     'url': 'http://wiki.blender.org/index.php/Extensions:2.5/Py/' \
-	    'Scripts/3D_interaction/Panel_Measure',
+        'Scripts/3D_interaction/Panel_Measure',
     'category': '3D View'}
 
 __bpydoc__ = """
@@ -60,6 +61,11 @@
 "Snap during transform" enabled for fast measurement.
 
 Version history:
+v0.6.4 - Fixed unneeded meshdata duplication (sometimes crashes Blender).
+    The script now correctly calculated the surface area (faceAreaGlobal)
+    of scaled meshes.
+    http://projects.blender.org/tracker/
+    ?func=detail&atid=453&aid=21913&group_id=153
 v0.6.3 - Added register & unregister functions.
 v0.6.2 - Fixed precision of second area property.
     Reduced display precision to 5 (instead of 6).
@@ -119,6 +125,75 @@
 """
 
 
+# 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" parameter):
+# @sa: rna_mesh.c:rna_MeshFace_area_get
+# @sa: math_geom.c:area_quad_v3
+# @sa: math_geom.c:area_tri_v3
+def faceAreaGlobal(face, obj):
+    area = 0.0
+
+    mat = obj.matrix
+
+    if len(face.verts) == 4:
+        # Quad
+
+        # Get vertex indices
+        v1, v2, v3, v4 = face.verts
+
+        # Get vertex data
+        v1 = obj.data.verts[v1]
+        v2 = obj.data.verts[v2]
+        v3 = obj.data.verts[v3]
+        v4 = obj.data.verts[v4]
+
+        # Apply transform matrix to vertex coordinates.
+        v1 = Vector(tuple(v1.co)) * mat
+        v2 = Vector(tuple(v2.co)) * mat
+        v3 = Vector(tuple(v3.co)) * mat
+        v4 = Vector(tuple(v4.co)) * mat
+
+        vec1 = v2 - v1
+        vec2 = v4 - v1
+
+        n = vec1.cross(vec2)
+
+        area = n.length / 2.0
+
+        vec1 = v4 - v3
+        vec2 = v2 - v3
+
+        n = vec1.cross(vec2)
+
+        area += n.length / 2.0
+
+    elif len(face.verts) == 3:
+        # Triangle
+
+        # Get vertex indices
+        v1, v2, v3 = face.verts
+
+        # Get vertex data
+        v1 = obj.data.verts[v1]
+        v2 = obj.data.verts[v2]
+        v3 = obj.data.verts[v3]
+
+        # Apply transform matrix to vertex coordinates.
+        v1 = Vector(tuple(v1.co)) * mat
+        v2 = Vector(tuple(v2.co)) * mat
+        v3 = Vector(tuple(v3.co)) * mat
+
+        vec1 = v3 - v2
+        vec2 = v1 - v2
+
+        n = vec1.cross(vec2)
+
+        area = n.length / 2.0
+
+    return area
+
+
 # Calculate the surface area of a mesh object.
 # *) Set selectedOnly=1 if you only want to count selected faces.
 # *) Set globalSpace=1 if you want to calculate
@@ -126,33 +201,20 @@
 # Note: Be sure you have updated the mesh data before
 #       running this with selectedOnly=1!
 # @todo Support other object types (surfaces, etc...)?
-# @todo Is there a better way to handle
-#       global calculation? (transformed mesh)
-
-
 def objectSurfaceArea(obj, selectedOnly, globalSpace):
     if (obj and obj.type == 'MESH' and obj.data):
         areaTotal = 0
 
-        # Apply transformation if needed.
-#        remove = 0
-        if globalSpace:
-#            remove = 1
-            mesh = obj.data.copy()
-            mesh.transform(obj.matrix)
-        else:
-            mesh = obj.data
+        mesh = obj.data
 
         # Count the area of all the faces.
         for face in mesh.faces:
-            if (not selectedOnly
-                or face.selected):
-                areaTotal += face.area
+            if not selectedOnly or face.selected:
+                if globalSpace:
+                    areaTotal += faceAreaGlobal(face, obj)
+                else:
+                    areaTotal += face.area
 
-#        # Remove temp mesh again.
-#        if remove:
-#            bpy.context.main.meshes.remove(mesh)
-
         return areaTotal
 
     # We can not calculate an area for this object.
@@ -196,8 +258,6 @@
     bl_default_closed = True
 
     def draw(self, context):
-        from Mathutils import Vector, Matrix
-
         layout = self.layout
         scene = context.scene
 




More information about the Bf-extensions-cvs mailing list