diff options
author | Campbell Barton <ideasman42@gmail.com> | 2012-06-27 18:12:06 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2012-06-27 18:12:06 +0400 |
commit | bf1fcf09f184e990dd1b0d590cf303c24a22a394 (patch) | |
tree | 3ed16af95d84abde41bb0af06fd53d79260bae4c | |
parent | 3de74ea572b87635a8bb9f19b315f113031a3535 (diff) |
cell fracture operator is now functional.
-rw-r--r-- | object_fracture_voroni/__init__.py | 38 | ||||
-rw-r--r-- | object_fracture_voroni/fracture_cell_setup.py | 80 |
2 files changed, 83 insertions, 35 deletions
diff --git a/object_fracture_voroni/__init__.py b/object_fracture_voroni/__init__.py index 722e9558..0f78474a 100644 --- a/object_fracture_voroni/__init__.py +++ b/object_fracture_voroni/__init__.py @@ -33,13 +33,31 @@ bl_info = { if "bpy" in locals(): import imp - imp.reload(cell_fracture) -else: - from . import cell_fracture + imp.reload(fracture_cell_setup) import bpy +from bpy.types import Operator +def main(context): + from . import fracture_cell_setup + obj = context.active_object + objects = fracture_cell_setup.cell_fracture_objects(context, obj) + objects = fracture_cell_setup.cell_fracture_boolean(context, obj, objects) + bpy.ops.object.select_all(action='DESELECT') + for obj_cell in objects: + obj_cell.select = True + + +class FractureCell(Operator): + bl_idname = "object.add_fracture_cell_objects" + bl_label = "Cell Fracture Helper Objects" + + def execute(self, context): + main(context) + return {'FINISHED'} + +''' class INFO_MT_add_fracture_objects(bpy.types.Menu): bl_idname = "INFO_MT_add_fracture_objects" bl_label = "Fracture Helper Objects" @@ -54,24 +72,24 @@ class INFO_MT_add_fracture_objects(bpy.types.Menu): text="Projectile") layout.operator("object.import_fracture_recorder", text="Rigidbody Recorder") +''' - -def menu_func(self, context): - self.layout.menu("INFO_MT_add_fracture_objects", icon="PLUGIN") +#def menu_func(self, context): +# self.layout.menu("INFO_MT_add_fracture_objects", icon="PLUGIN") def register(): - bpy.utils.register_module(__name__) + bpy.utils.register_class(FractureCell) # Add the "add fracture objects" menu to the "Add" menu - bpy.types.INFO_MT_add.append(menu_func) + # bpy.types.INFO_MT_add.append(menu_func) def unregister(): - bpy.utils.unregister_module(__name__) + bpy.utils.unregister_class(FractureCell) # Remove "add fracture objects" menu from the "Add" menu. - bpy.types.INFO_MT_add.remove(menu_func) + # bpy.types.INFO_MT_add.remove(menu_func) if __name__ == "__main__": diff --git a/object_fracture_voroni/fracture_cell_setup.py b/object_fracture_voroni/fracture_cell_setup.py index 7e99924c..de3e1210 100644 --- a/object_fracture_voroni/fracture_cell_setup.py +++ b/object_fracture_voroni/fracture_cell_setup.py @@ -20,7 +20,11 @@ # Script copyright (C) Blender Foundation 2012 -def cell_fracture_objects(context, obj, method={'OTHER'}): +import bpy +import bmesh + + +def cell_fracture_objects(context, obj, method={'PARTICLES'}, clean=True): #assert(method in {'OTHER', 'PARTICLES'}) @@ -40,6 +44,8 @@ def cell_fracture_objects(context, obj, method={'OTHER'}): matrix = obj_other.matrix_world.copy() points.extend([matrix * v.co for v in mesh.vertices]) + if not points: + return [] mesh = obj.data matrix = obj.matrix_world.copy() @@ -48,8 +54,6 @@ def cell_fracture_objects(context, obj, method={'OTHER'}): cells = fracture_cell_calc.points_as_bmesh_cells(verts, points) # some hacks here :S - import bmesh - scene = context.scene cell_name = obj.name + "_cell" @@ -73,36 +77,62 @@ def cell_fracture_objects(context, obj, method={'OTHER'}): 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 - - bm.to_mesh(me) + + if clean: + for bm_vert in bm.verts: + bm_vert.tag = True + for bm_edge in bm.edges: + bm_edge.tag = True + bm.normal_update() + bmesh.ops.dissolve_limit(bm, {'TAG'}, {'TAG'}, 0.001) + + bm.to_mesh(mesh) bm.free() objects.append(obj_cell) - return obj_cell - + scene.update() + return objects -def cell_fracture_boolean(context, obj, objects): +def cell_fracture_boolean(context, obj, objects, apply=True, clean=True): scene = context.scene + + objects_boolean = [] + for obj_cell in objects: - mod = obj_cell.modifiers.new(type='BOOLEAN') - mod.object = boolobj + mod = obj_cell.modifiers.new(name="Boolean", type='BOOLEAN') + mod.object = obj mod.operation = 'INTERSECT' - - mesh_new = obj_cell.to_mesh(scene, apply_modifiers=True) - mesh_old = obj_cell.data - obj_cell.data = mesh_new - - # remove if not valid - if not mesh_old.users: - bpy.data.meshes.remove(mesh_old) - if not mesh_new.verts: - scene.objects.unlink(obj_cell) - if not obj_cell.users: - bpy.data.objects.remove(obj_cell) - if not mesh_new.users: - bpy.data.meshes.remove(mesh_old) - obj_cell.select = True + if apply: + mesh_new = obj_cell.to_mesh(scene, apply_modifiers=True, settings='PREVIEW') + mesh_old = obj_cell.data + obj_cell.data = mesh_new + obj_cell.modifiers.remove(mod) + + # remove if not valid + if not mesh_old.users: + bpy.data.meshes.remove(mesh_old) + if not mesh_new.vertices: + scene.objects.unlink(obj_cell) + if not obj_cell.users: + bpy.data.objects.remove(obj_cell) + if not mesh_new.users: + bpy.data.meshes.remove(mesh_new) + + if clean: + bm = bmesh.new() + bm.from_mesh(mesh_new) + for bm_vert in bm.verts: + bm_vert.tag = True + for bm_edge in bm.edges: + bm_edge.tag = True + bm.normal_update() + bmesh.ops.dissolve_limit(bm, {'TAG'}, {'TAG'}, 0.01) + bm.to_mesh(mesh_new) + bm.free() + + objects_boolean.append(obj_cell) + return objects_boolean |