[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [3546] trunk/py/scripts/addons/ object_fracture_voroni: add functions for scene setup

Campbell Barton ideasman42 at gmail.com
Wed Jun 27 14:39:24 CEST 2012


Revision: 3546
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=3546
Author:   campbellbarton
Date:     2012-06-27 12:39:17 +0000 (Wed, 27 Jun 2012)
Log Message:
-----------
add functions for scene setup

Added Paths:
-----------
    trunk/py/scripts/addons/object_fracture_voroni/fracture_cell_calc.py
    trunk/py/scripts/addons/object_fracture_voroni/fracture_cell_setup.py

Removed Paths:
-------------
    trunk/py/scripts/addons/object_fracture_voroni/cell_fracture.py

Deleted: trunk/py/scripts/addons/object_fracture_voroni/cell_fracture.py
===================================================================
--- trunk/py/scripts/addons/object_fracture_voroni/cell_fracture.py	2012-06-27 10:30:25 UTC (rev 3545)
+++ trunk/py/scripts/addons/object_fracture_voroni/cell_fracture.py	2012-06-27 12:39:17 UTC (rev 3546)
@@ -1,93 +0,0 @@
-# ##### BEGIN GPL LICENSE BLOCK #####
-#
-#  This program is free software; you can redistribute it and/or
-#  modify it under the terms of the GNU General Public License
-#  as published by the Free Software Foundation; either version 2
-#  of the License, or (at your option) any later version.
-#
-#  This program is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY; without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#  GNU General Public License for more details.
-#
-#  You should have received a copy of the GNU General Public License
-#  along with this program; if not, write to the Free Software Foundation,
-#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# ##### END GPL LICENSE BLOCK #####
-
-# <pep8 compliant>
-
-# Script copyright (C) Blender Foundation 2012
-
-
-def points_as_bmesh_cells(verts, points, margin=0.01):
-    import mathutils
-    from mathutils import Vector
-
-    cells = []
-
-    sortedVoronoiPoints = [p for p in points]
-    plane_indices = []
-    vertices = []
-
-    # there are many ways we could get planes - convex hull for eg
-    # but it ends up fastest if we just use bounding box
-    if 1:
-        xa = [v[0] for v in verts]
-        ya = [v[1] for v in verts]
-        za = [v[2] for v in verts]
-        
-        xmin, xmax = min(xa) - margin, max(xa) + margin
-        ymin, ymax = min(ya) - margin, max(ya) + margin
-        zmin, zmax = min(za) - margin, max(za) + margin
-        convexPlanes = [
-            Vector((+1.0, 0.0, 0.0, -abs(xmax))),
-            Vector((-1.0, 0.0, 0.0, -abs(xmin))),
-            Vector((0.0, +1.0, 0.0, -abs(ymax))),
-            Vector((0.0, -1.0, 0.0, -abs(ymin))),
-            Vector((0.0, 0.0, +1.0, -abs(zmax))),
-            Vector((0.0, 0.0, -1.0, -abs(zmin))),
-            ]
-
-    for i, curVoronoiPoint in enumerate(points):
-        planes = [None] * len(convexPlanes)
-        for j in range(len(convexPlanes)):
-            planes[j] = convexPlanes[j].copy()
-            planes[j][3] += planes[j].xyz.dot(curVoronoiPoint)
-        maxDistance = 10000000000.0  # a big value!
-
-        sortedVoronoiPoints.sort(key=lambda p: (p - curVoronoiPoint).length_squared)
-
-        for j in range(1, len(points)):
-            normal = sortedVoronoiPoints[j] - curVoronoiPoint
-            nlength = normal.length
-            if nlength > maxDistance:
-                break
-
-            plane = normal.normalized()
-            plane.resize_4d()
-            plane[3] = -nlength / 2.0
-            planes.append(plane)
-            
-            vertices[:], plane_indices[:] = mathutils.geometry.points_in_planes(planes)
-            if len(vertices) == 0:
-                break
-
-            if len(plane_indices) != len(planes):
-                planes[:] = [planes[k] for k in plane_indices]
-
-            maxDistance = vertices[0].length
-            for k in range(1, len(vertices)):
-                distance = vertices[k].length
-                if maxDistance < distance:
-                    maxDistance = distance
-            maxDistance *= 2.0
-
-        if len(vertices) == 0:
-            continue
-
-        cells.append((curVoronoiPoint, vertices[:]))
-        vertices[:] = []
-
-    return cells

Copied: trunk/py/scripts/addons/object_fracture_voroni/fracture_cell_calc.py (from rev 3545, trunk/py/scripts/addons/object_fracture_voroni/cell_fracture.py)
===================================================================
--- trunk/py/scripts/addons/object_fracture_voroni/fracture_cell_calc.py	                        (rev 0)
+++ trunk/py/scripts/addons/object_fracture_voroni/fracture_cell_calc.py	2012-06-27 12:39:17 UTC (rev 3546)
@@ -0,0 +1,93 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License
+#  as published by the Free Software Foundation; either version 2
+#  of the License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8 compliant>
+
+# Script copyright (C) Blender Foundation 2012
+
+
+def points_as_bmesh_cells(verts, points, margin=0.01):
+    import mathutils
+    from mathutils import Vector
+
+    cells = []
+
+    sortedVoronoiPoints = [p for p in points]
+    plane_indices = []
+    vertices = []
+
+    # there are many ways we could get planes - convex hull for eg
+    # but it ends up fastest if we just use bounding box
+    if 1:
+        xa = [v[0] for v in verts]
+        ya = [v[1] for v in verts]
+        za = [v[2] for v in verts]
+        
+        xmin, xmax = min(xa) - margin, max(xa) + margin
+        ymin, ymax = min(ya) - margin, max(ya) + margin
+        zmin, zmax = min(za) - margin, max(za) + margin
+        convexPlanes = [
+            Vector((+1.0, 0.0, 0.0, -abs(xmax))),
+            Vector((-1.0, 0.0, 0.0, -abs(xmin))),
+            Vector((0.0, +1.0, 0.0, -abs(ymax))),
+            Vector((0.0, -1.0, 0.0, -abs(ymin))),
+            Vector((0.0, 0.0, +1.0, -abs(zmax))),
+            Vector((0.0, 0.0, -1.0, -abs(zmin))),
+            ]
+
+    for i, curVoronoiPoint in enumerate(points):
+        planes = [None] * len(convexPlanes)
+        for j in range(len(convexPlanes)):
+            planes[j] = convexPlanes[j].copy()
+            planes[j][3] += planes[j].xyz.dot(curVoronoiPoint)
+        maxDistance = 10000000000.0  # a big value!
+
+        sortedVoronoiPoints.sort(key=lambda p: (p - curVoronoiPoint).length_squared)
+
+        for j in range(1, len(points)):
+            normal = sortedVoronoiPoints[j] - curVoronoiPoint
+            nlength = normal.length
+            if nlength > maxDistance:
+                break
+
+            plane = normal.normalized()
+            plane.resize_4d()
+            plane[3] = -nlength / 2.0
+            planes.append(plane)
+            
+            vertices[:], plane_indices[:] = mathutils.geometry.points_in_planes(planes)
+            if len(vertices) == 0:
+                break
+
+            if len(plane_indices) != len(planes):
+                planes[:] = [planes[k] for k in plane_indices]
+
+            maxDistance = vertices[0].length
+            for k in range(1, len(vertices)):
+                distance = vertices[k].length
+                if maxDistance < distance:
+                    maxDistance = distance
+            maxDistance *= 2.0
+
+        if len(vertices) == 0:
+            continue
+
+        cells.append((curVoronoiPoint, vertices[:]))
+        vertices[:] = []
+
+    return cells

Added: trunk/py/scripts/addons/object_fracture_voroni/fracture_cell_setup.py
===================================================================
--- trunk/py/scripts/addons/object_fracture_voroni/fracture_cell_setup.py	                        (rev 0)
+++ trunk/py/scripts/addons/object_fracture_voroni/fracture_cell_setup.py	2012-06-27 12:39:17 UTC (rev 3546)
@@ -0,0 +1,108 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License
+#  as published by the Free Software Foundation; either version 2
+#  of the License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8 compliant>
+
+# Script copyright (C) Blender Foundation 2012
+
+def cell_fracture_objects(context, obj, method={'OTHER'}):
+
+    #assert(method in {'OTHER', 'PARTICLES'})
+    
+    from . import fracture_cell_calc
+
+    points = []
+
+    if 'PARTICLES' in method:
+        points.extend([p.location.copy()
+                         for psys in obj.particle_systems
+                         for p in psys.particles])
+
+    if 'OTHER' in method:
+        for obj_other in context.selected_objects:
+            if obj_other.type == 'MESH':
+                mesh = obj_other.data
+                matrix = obj_other.matrix_world.copy()
+                points.extend([matrix * v.co for v in mesh.vertices])
+
+
+    mesh = obj.data
+    matrix = obj.matrix_world.copy()
+    verts = [matrix * v.co for v in mesh.vertices]
+
+    cells = fracture_cell_calc.points_as_bmesh_cells(verts, points)
+    
+    # some hacks here :S
+    import bmesh
+
+    scene = context.scene
+    cell_name = obj.name + "_cell"
+    
+    objects = []
+    
+    for center_point, cell_points in cells:
+        mesh = bpy.data.meshes.new(name=cell_name)
+        obj_cell = bpy.data.objects.new(name=cell_name, object_data=mesh)
+        scene.objects.link(obj_cell)
+        # scene.objects.active = obj_cell
+        obj_cell.location = center_point
+
+        # create the convex hulls
+        bm = bmesh.new()
+        for i, co in enumerate(cell_points):
+            bm_vert = bm.verts.new(co)
+            bm_vert.tag = True
+
+        import mathutils
+        bm.transform(mathutils.Matrix.Translation((+100.0, +100.0, +100.0))) # BUG IN BLENDER
+        bmesh.ops.remove_doubles(bm, {'TAG'}, 0.0001)
+        bmesh.ops.convex_hull(bm, {'TAG'})
+        bm.transform(mathutils.Matrix.Translation((-100.0, -100.0, -100.0))) # BUG IN BLENDER

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list