[Bf-blender-cvs] [f7fa690] fracture_modifier: initial, basic python API for Fracture Modifier contents: mesh_islands and mesh_constraints are createable, removable externally prior to running the simulation (in special "External" fracturing mode) so you can pack objects into the modifier (as mesh islands) and set constraints to connect them. This can happen in immediate update mode or updating is done via refresh operator. You can also have read access for island data after simulation, to dump the results.

Martin Felke noreply at git.blender.org
Sun Jan 17 16:15:26 CET 2016


Commit: f7fa6906e61cff0d9033cd3c45531fdfcf5fd954
Author: Martin Felke
Date:   Sun Jan 17 16:14:48 2016 +0100
Branches: fracture_modifier
https://developer.blender.org/rBf7fa6906e61cff0d9033cd3c45531fdfcf5fd954

initial, basic python API for Fracture Modifier contents: mesh_islands and mesh_constraints are createable, removable externally prior to running the simulation (in special "External" fracturing mode) so you can pack objects into the modifier (as mesh islands) and set constraints to connect them. This can happen in immediate update mode or updating is done via refresh operator. You can also have read access for island data after simulation, to dump the results.

===================================================================

M	release/scripts/startup/bl_ui/properties_physics_fracture.py
M	source/blender/blenkernel/BKE_fracture.h
M	source/blender/blenkernel/intern/fracture.c
M	source/blender/blenkernel/intern/pointcache.c
M	source/blender/blenkernel/intern/rigidbody.c
M	source/blender/makesdna/DNA_modifier_types.h
M	source/blender/makesdna/DNA_rigidbody_types.h
M	source/blender/makesrna/intern/rna_mesh.c
M	source/blender/makesrna/intern/rna_modifier.c
M	source/blender/makesrna/intern/rna_rigidbody.c
M	source/blender/modifiers/intern/MOD_fracture.c

===================================================================

diff --git a/release/scripts/startup/bl_ui/properties_physics_fracture.py b/release/scripts/startup/bl_ui/properties_physics_fracture.py
index 58dd87a..3ea1084 100644
--- a/release/scripts/startup/bl_ui/properties_physics_fracture.py
+++ b/release/scripts/startup/bl_ui/properties_physics_fracture.py
@@ -73,14 +73,22 @@ class PHYSICS_PT_fracture(PhysicButtonsPanel, Panel):
         md = context.fracture
         ob = context.object
 
-        layout.label(text="Presets:")
-        sub = layout.row(align=True)
-        sub.menu("FRACTURE_MT_presets", text=bpy.types.FRACTURE_MT_presets.bl_label)
-        sub.operator("fracture.preset_add", text="", icon='ZOOMIN')
-        sub.operator("fracture.preset_add", text="", icon='ZOOMOUT').remove_active = True
+        if md.fracture_mode != 'EXTERNAL':
+            layout.label(text="Presets:")
+            sub = layout.row(align=True)
+            sub.menu("FRACTURE_MT_presets", text=bpy.types.FRACTURE_MT_presets.bl_label)
+            sub.operator("fracture.preset_add", text="", icon='ZOOMIN')
+            sub.operator("fracture.preset_add", text="", icon='ZOOMOUT').remove_active = True
+        else:
+            layout.label(text="No UI controls here!")
+            layout.label(text="Control happens via Python")
 
         row = layout.row()
         row.prop(md, "fracture_mode")
+
+        if md.fracture_mode == 'EXTERNAL':
+           return
+
         if md.fracture_mode == 'DYNAMIC':
             layout.prop(md, "dynamic_force")
             layout.prop(md, "limit_impact")
@@ -143,6 +151,11 @@ class PHYSICS_PT_fracture(PhysicButtonsPanel, Panel):
 class PHYSICS_PT_fracture_simulation(PhysicButtonsPanel, Panel):
     bl_label = "Fracture Constraint Settings"
 
+    @classmethod
+    def poll(cls, context):
+        md = context.fracture
+        return PhysicButtonsPanel.poll(context) and md.fracture_mode != 'EXTERNAL'
+
     def draw(self, context):
         layout = self.layout
         md = context.fracture
@@ -202,6 +215,11 @@ class PHYSICS_PT_fracture_simulation(PhysicButtonsPanel, Panel):
 class PHYSICS_PT_fracture_utilities(PhysicButtonsPanel, Panel):
     bl_label = "Fracture Utilities"
 
+    @classmethod
+    def poll(cls, context):
+        md = context.fracture
+        return PhysicButtonsPanel.poll(context) and md.fracture_mode != 'EXTERNAL'
+
     def draw(self, context):
         layout = self.layout
         md = context.fracture
diff --git a/source/blender/blenkernel/BKE_fracture.h b/source/blender/blenkernel/BKE_fracture.h
index cb0077e..d12f7e4 100644
--- a/source/blender/blenkernel/BKE_fracture.h
+++ b/source/blender/blenkernel/BKE_fracture.h
@@ -43,6 +43,7 @@ struct DerivedMesh;
 struct Object;
 struct Group;
 struct MeshIsland;
+struct RigidBodyShardCon;
 
 struct BoundBox;
 struct MVert;
@@ -104,4 +105,14 @@ void BKE_fracture_store_settings(struct FractureModifierData *fs, struct Fractur
 struct Shard* BKE_create_initial_shard(struct DerivedMesh *dm);
 void BKE_copy_customdata_layers(struct CustomData* dest, struct CustomData *src, int type, int count);
 
+struct MeshIsland *BKE_fracture_mesh_island_add(struct FractureModifierData *fmd, struct Object *own, struct Object *target, int index);
+void BKE_fracture_mesh_island_remove(struct FractureModifierData *fmd, struct MeshIsland *mi);
+void BKE_fracture_mesh_island_remove_all(struct FractureModifierData *fmd);
+
+struct RigidBodyShardCon* BKE_fracture_mesh_islands_connect(struct FractureModifierData *fmd, struct MeshIsland *mi1, struct MeshIsland *mi2, short con_type, int index);
+void BKE_fracture_mesh_constraint_remove(struct FractureModifierData* fmd, struct RigidBodyShardCon *con);
+void BKE_fracture_mesh_constraint_remove_all(struct FractureModifierData *fmd);
+
+void BKE_fracture_free_mesh_island(struct FractureModifierData *rmd, struct MeshIsland *mi, bool remove_rigidbody);
+
 #endif /* BKE_FRACTURE_H */
diff --git a/source/blender/blenkernel/intern/fracture.c b/source/blender/blenkernel/intern/fracture.c
index 644b8a5..5c0d9a4 100644
--- a/source/blender/blenkernel/intern/fracture.c
+++ b/source/blender/blenkernel/intern/fracture.c
@@ -43,6 +43,7 @@
 #include "BKE_mesh.h"
 #include "BKE_modifier.h"
 #include "BKE_object.h"
+#include "BKE_pointcache.h"
 #include "BKE_rigidbody.h"
 
 #include "BLI_kdtree.h"
@@ -2149,3 +2150,457 @@ void BKE_fracture_store_settings(FractureModifierData *fs, FractureSetting *fmd)
 	fmd->mass_threshold_factor = fs->mass_threshold_factor;
 	fmd->use_compounds = fs->use_compounds;
 }
+
+static Shard* fracture_object_to_shard( Object *own, Object* target)
+{
+	DerivedMesh *dm;
+	Shard *s = NULL;
+
+	MVert* mvert, *mv;
+	MPoly* mpoly;
+	MLoop* mloop;
+	SpaceTransform trans;
+	//float mat[4][4];
+
+	int totvert, totpoly, totloop, v;
+	bool do_free = false;
+
+	dm = target->derivedFinal;
+	if (!dm)
+	{	//fallback if no derivedFinal available
+		dm = CDDM_from_mesh((Mesh*)target->data);
+		do_free = true;
+	}
+
+	//copy_m4_m4(mat, own->obmat);
+	//take centroid into account ? hmmmm
+	//BLI_space_transform_from_matrices(&trans, target->obmat, mat);
+	BLI_SPACE_TRANSFORM_SETUP(&trans, target, own);
+
+	mvert = dm->getVertArray(dm);
+	mpoly = dm->getPolyArray(dm);
+	mloop = dm->getLoopArray(dm);
+	totvert = dm->getNumVerts(dm);
+	totpoly = dm->getNumPolys(dm);
+	totloop = dm->getNumLoops(dm);
+
+	// create temp shard -> that necessary at all ?
+	s = BKE_create_fracture_shard(mvert, mpoly, mloop, totvert, totpoly, totloop, true);
+
+	for(v = 0, mv = s->mvert; v < s->totvert; v++, mv++)
+	{
+		BLI_space_transform_apply(&trans, mv->co);
+	}
+
+	BLI_space_transform_apply(&trans, s->centroid);
+
+	s = BKE_custom_data_to_shard(s, dm);
+	BKE_shard_calc_minmax(s);
+
+	if (do_free && dm)
+	{
+		dm->needsFree = 1;
+		dm->release(dm);
+	}
+
+	return s;
+}
+
+static void fracture_update_shards(FractureModifierData *fmd, Shard *s, int index)
+{
+	FracMesh* fm;
+
+	if (!fmd->frac_mesh)
+		fmd->frac_mesh = BKE_create_fracture_container();
+
+	fm = fmd->frac_mesh;
+	BLI_addtail(&fm->shard_map, s);
+	s->shard_id = fm->shard_count;
+	fm->shard_count++;
+
+	//set custom index ?
+	if (index > -1)
+		s->shard_id = index;
+
+}
+
+static MeshIsland* fracture_shard_to_island(FractureModifierData *fmd, Shard *s, int vertstart)
+{
+	MeshIsland *mi;
+	int k = 0, j = 0, totvert;
+	MVert *mverts, *verts, *mv;
+
+	//create mesh island and intialize
+	mi = MEM_callocN(sizeof(MeshIsland), "meshIsland");
+	BLI_addtail(&fmd->meshIslands, mi);
+	mi->participating_constraints = NULL;
+	mi->participating_constraint_count = 0;
+	mi->thresh_weight = 0.0f;
+	mi->ground_weight = 0.0f;
+	mi->vertex_count = s->totvert;
+
+	//link up the visual mesh verts
+	mi->vertices_cached = MEM_mallocN(sizeof(MVert *) * s->totvert, "vert_cache");
+	mverts = CDDM_get_verts(fmd->visible_mesh_cached);
+	mi->vertex_indices = MEM_mallocN(sizeof(int) * mi->vertex_count, "mi->vertex_indices");
+
+	for (k = 0; k < s->totvert; k++) {
+		mi->vertices_cached[k] = mverts + vertstart + k;
+		mi->vertex_indices[k] = vertstart + k;
+	}
+
+	//some dummy setup, necessary here ?
+	mi->locs = MEM_mallocN(sizeof(float)*3, "mi->locs");
+	mi->rots = MEM_mallocN(sizeof(float)*4, "mi->rots");
+	mi->frame_count = 0;
+	if (fmd->modifier.scene->rigidbody_world)
+	{
+		mi->start_frame = fmd->modifier.scene->rigidbody_world->pointcache->startframe;
+	}
+	else
+	{
+		mi->start_frame = 1;
+	}
+
+	mi->physics_mesh = BKE_shard_create_dm(s, true);
+	totvert = mi->physics_mesh->getNumVerts(mi->physics_mesh);
+	verts = mi->physics_mesh->getVertArray(mi->physics_mesh);
+
+	mi->vertco = MEM_mallocN(sizeof(float) * 3 * totvert, "vertco");
+	mi->vertno = MEM_mallocN(sizeof(short) * 3 * totvert, "vertno");
+
+	for (mv = verts, j = 0; j < totvert; mv++, j++) {
+		short no[3];
+
+		mi->vertco[j * 3] = mv->co[0];
+		mi->vertco[j * 3 + 1] = mv->co[1];
+		mi->vertco[j * 3 + 2] = mv->co[2];
+
+		mi->vertno[j * 3] = no[0];
+		mi->vertno[j * 3 + 1] = no[1];
+		mi->vertno[j * 3 + 2] = no[2];
+
+		/* then eliminate centroid in vertex coords*/
+		sub_v3_v3(mv->co, s->centroid);
+	}
+
+	copy_v3_v3(mi->centroid, s->centroid);
+	mi->id = s->shard_id;
+	mi->bb = BKE_boundbox_alloc_unit();
+	BKE_boundbox_init_from_minmax(mi->bb, s->min, s->max);
+	mi->particle_index = -1;
+
+	//this info isnt necessary here... constraints will be provided too !
+	mi->neighbor_ids = s->neighbor_ids;
+	mi->neighbor_count = s->neighbor_count;
+
+	return mi;
+}
+
+static int fracture_update_visual_mesh(FractureModifierData *fmd, bool do_custom_data)
+{
+	MeshIsland *mi;
+	DerivedMesh *dm = fmd->visible_mesh_cached;
+	int vertstart = 0, totvert = 0;
+	MVert *mv = NULL;
+
+	if (dm)
+	{
+		vertstart = dm->getNumVerts(dm);
+		dm->needsFree = 1;
+		dm->release(dm);
+		dm = fmd->visible_mesh_cached = NULL;
+	}
+
+	fmd->visible_mesh_cached = create_dm(fmd, do_custom_data);
+	dm = fmd->visible_mesh_cached;
+	mv = dm->getVertArray(dm);
+	totvert = dm->getNumVerts(dm);
+
+	//update existing island's vert refs, if any...should have used indexes instead :S
+	for (mi = fmd->meshIslands.first; mi; mi = mi->next)
+	{
+		int i = 0;
+		for (i = 0; i < mi->vertex_count; i++)
+		{
+			//just update pointers, dont need to reallocate something
+			MVert *v = NULL;
+			int index;
+
+			//also correct indexes
+			if (mi->vertex_indices[i] >= totvert)
+			{
+				index = mi->vertex_indices[i];
+				mi->vertex_indices[i] -= (vertstart - totvert);
+				printf("I: %d, O: %d, N: %d\n", i, index, mi->vertex_indices[i]);
+			}
+
+			index = mi->vertex_indices[i];
+			v = mv + index;
+			mi->vertices_cached[i] = v;
+
+			//printf("%d %d\n", index, dm->getNumVerts(dm));
+
+			//hrm perhaps we need to update rest coordinates, too...
+			mi->vertco[3*i] = v->co[0];
+			mi->vertco[3*i+1] = v->co[1];
+			mi->vertco[3*i+2] = v->co[2];
+
+			mi->vertno[3*i] = v->no[0];
+			mi->vertno[3*i+1] = v->no[1];
+			mi->vertno[3*i+2] = v->no[2];
+		}
+	}
+
+	return vertstart;
+}
+
+MeshIsland* BKE_fracture_mesh_island_add(FractureModifierData *fmd, Object* own, Object *target, int index)
+{
+	MeshIsland *mi;
+	Shard *s;
+	int vertstart;
+	float loc[3], rot[4];
+
+	if (fmd->fracture_mode != MOD_FRACTURE_EXTERNAL || own->type != OB_MESH)
+		return NULL;
+
+	s = fracture_object_

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list