[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [30035] branches/soc-2010-aligorith-2/ source/blender: Bullet SoC - Export to .bullet format

Joshua Leung aligorith at gmail.com
Tue Jul 6 14:35:33 CEST 2010


Revision: 30035
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=30035
Author:   aligorith
Date:     2010-07-06 14:35:32 +0200 (Tue, 06 Jul 2010)

Log Message:
-----------
Bullet SoC - Export to .bullet format

Just a quickly little operator that can be found from the search-menu (for now, since it isn't that exciting/important) that exports the active Rigid Body World to a .bullet file. This is basically a sdna-like fileformat.

I know Erwin was quite keen that I add this, and indeed it's not really anything tricky ;)

Modified Paths:
--------------
    branches/soc-2010-aligorith-2/source/blender/editors/physics/SConscript
    branches/soc-2010-aligorith-2/source/blender/editors/physics/physics_intern.h
    branches/soc-2010-aligorith-2/source/blender/editors/physics/physics_ops.c
    branches/soc-2010-aligorith-2/source/blender/editors/physics/rigidbody_edit.c
    branches/soc-2010-aligorith-2/source/blender/rigidbody/RBI_api.h
    branches/soc-2010-aligorith-2/source/blender/rigidbody/rb_bullet_api.cpp

Modified: branches/soc-2010-aligorith-2/source/blender/editors/physics/SConscript
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/editors/physics/SConscript	2010-07-06 12:32:09 UTC (rev 30034)
+++ branches/soc-2010-aligorith-2/source/blender/editors/physics/SConscript	2010-07-06 12:35:32 UTC (rev 30035)
@@ -7,6 +7,7 @@
 incs += ' ../../windowmanager #/intern/guardedalloc #/extern/glew/include'
 incs += ' ../../gpu'
 incs += ' ../../makesrna ../../render/extern/include  #/intern/elbeem/extern'
+incs += ' ../../rigidbody'
 
 defs = ''
 

Modified: branches/soc-2010-aligorith-2/source/blender/editors/physics/physics_intern.h
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/editors/physics/physics_intern.h	2010-07-06 12:32:09 UTC (rev 30034)
+++ branches/soc-2010-aligorith-2/source/blender/editors/physics/physics_intern.h	2010-07-06 12:35:32 UTC (rev 30035)
@@ -110,6 +110,7 @@
 void RIGIDBODY_OT_world_add(struct wmOperatorType *ot);
 void RIGIDBODY_OT_world_remove(struct wmOperatorType *ot);
 
+void RIGIDBODY_OT_world_export(struct wmOperatorType *ot);
 void RIGIDBODY_OT_world_reset(struct wmOperatorType *ot);
 
 /* rigidbody_object.c */

Modified: branches/soc-2010-aligorith-2/source/blender/editors/physics/physics_ops.c
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/editors/physics/physics_ops.c	2010-07-06 12:32:09 UTC (rev 30034)
+++ branches/soc-2010-aligorith-2/source/blender/editors/physics/physics_ops.c	2010-07-06 12:35:32 UTC (rev 30035)
@@ -87,6 +87,7 @@
 	WM_operatortype_append(RIGIDBODY_OT_world_add);	
 	WM_operatortype_append(RIGIDBODY_OT_world_remove);
 	
+	WM_operatortype_append(RIGIDBODY_OT_world_export);	
 	WM_operatortype_append(RIGIDBODY_OT_world_reset);	
 	
 	WM_operatortype_append(RIGIDBODY_OT_object_buttons_add);	

Modified: branches/soc-2010-aligorith-2/source/blender/editors/physics/rigidbody_edit.c
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/editors/physics/rigidbody_edit.c	2010-07-06 12:32:09 UTC (rev 30034)
+++ branches/soc-2010-aligorith-2/source/blender/editors/physics/rigidbody_edit.c	2010-07-06 12:35:32 UTC (rev 30035)
@@ -40,6 +40,8 @@
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
 
+#include "RBI_api.h"
+
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
 #include "BKE_group.h"
@@ -148,6 +150,67 @@
 /* ********************************************** */
 /* UTILITY OPERATORS */
 
+/* ********** Export RigidBody World ************* */
+
+static int rigidbody_world_export_exec (bContext *C, wmOperator *op)
+{
+	Scene *scene = CTX_data_scene(C);
+	RigidBodyWorld *rbw = BKE_rigidbody_get_active_world(scene, 0);
+	char path[FILE_MAX];
+	
+	/* sanity checks */
+	if ELEM(NULL, scene, rbw) {
+		BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to export");
+		return OPERATOR_CANCELLED;
+	}
+	
+	/* call export callback from API */
+	if (rbw->physics_world == NULL) {
+		BKE_report(op->reports, RPT_ERROR, "Rigid Body World has no associated physics data to export");
+		return OPERATOR_CANCELLED;
+	}
+	
+	RNA_string_get(op->ptr, "filepath", path);
+	rbDWorldExport(rbw->physics_world, path);
+	
+	/* done */
+	return OPERATOR_FINISHED;
+}
+
+static int rigidbody_world_export_invoke (bContext *C, wmOperator *op, wmEvent *evt)
+{
+	if (!RNA_property_is_set(op->ptr, "relative_path"))
+		RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
+	
+	if (RNA_property_is_set(op->ptr, "filepath"))
+		return rigidbody_world_export_exec(C, op);
+	
+	// TODO: use the actual rigidbody world's name + .bullet instead of this temp crap
+	RNA_string_set(op->ptr, "filepath", "rigidbodyworld_export.bullet");
+	WM_event_add_fileselect(C, op); 
+
+	return OPERATOR_RUNNING_MODAL;
+}
+
+void RIGIDBODY_OT_world_export (wmOperatorType *ot)
+{
+	/* identifiers */
+	ot->idname = "RIGIDBODY_OT_world_export";
+	ot->name = "Export Rigid Body World";
+	ot->description = "Export Rigid Body world to simulator's own fileformat (i.e. '.bullet' for Bullet Physics)";
+	
+	/* callbacks */
+	ot->invoke = rigidbody_world_export_invoke;
+	ot->exec = rigidbody_world_export_exec;
+	ot->poll = ED_rigidbody_world_active_poll;
+	
+	/* flags */
+	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
+	
+	/* properties */
+	WM_operator_properties_filesel(ot, FOLDERFILE, FILE_SPECIAL, FILE_SAVE, FILE_RELPATH);
+}
+
 /* ********** Reset RigidBody World ************* */
 
 static int rigidbody_world_reset_exec (bContext *C, wmOperator *op)

Modified: branches/soc-2010-aligorith-2/source/blender/rigidbody/RBI_api.h
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/rigidbody/RBI_api.h	2010-07-06 12:32:09 UTC (rev 30034)
+++ branches/soc-2010-aligorith-2/source/blender/rigidbody/RBI_api.h	2010-07-06 12:35:32 UTC (rev 30035)
@@ -88,6 +88,11 @@
 /* Similar to rbStepSimulation(), with extra controls on substep sizes and maximum substeps */
 extern void rbStepSimulationAdv(rbDynamicsWorld *world, float timeStep, int maxSubSteps, float timeSubStep);
 
+/* Export -------------------------- */
+
+/* Exports the dynamics world to physics simulator's serialisation format */
+void rbDWorldExport(rbDynamicsWorld *world, const char *filename);
+
 /* ********************************** */
 /* Rigid Body Methods */
 

Modified: branches/soc-2010-aligorith-2/source/blender/rigidbody/rb_bullet_api.cpp
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/rigidbody/rb_bullet_api.cpp	2010-07-06 12:32:09 UTC (rev 30034)
+++ branches/soc-2010-aligorith-2/source/blender/rigidbody/rb_bullet_api.cpp	2010-07-06 12:35:32 UTC (rev 30035)
@@ -38,8 +38,10 @@
 // TODO: use MEM_guardedalloc instead of btAlignedAllocator
 // for memory allocation instead? That would be more 'Blender' friendly
 // and mean that we don't get nasty memory leaks...
+
+
+#include <stdio.h>
  
-
 #include "RBI_api.h"
 
 #include "btBulletDynamicsCommon.h"
@@ -51,6 +53,8 @@
 #include "LinearMath/btMatrix3x3.h"
 #include "LinearMath/btTransform.h"
 
+#include "LinearMath/btSerializer.h"
+
 #include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
 #include "BulletCollision/CollisionShapes/btTriangleShape.h"
 
@@ -140,6 +144,28 @@
 	dynamicsWorld->stepSimulation(timeStep, maxSubSteps, timeSubStep);
 }
 
+/* Export -------------------------- */
+
+/* Exports entire dynamics world to Bullet's "*.bullet" binary format
+ * which is similar to Blender's SDNA system...
+ * < rbDynamicsWorld: dynamics world to write to file
+ * < filename: assumed to be a valid filename, with .bullet extension 
+ */
+void rbDWorldExport(rbDynamicsWorld *world, const char *filename)
+{
+	btDynamicsWorld *dynamicsWorld = reinterpret_cast<btDynamicsWorld*>(world);
+	
+	//create a large enough buffer. There is no method to pre-calculate the buffer size yet.
+	int maxSerializeBufferSize = 1024*1024*5;
+	
+	btDefaultSerializer *serializer = new btDefaultSerializer(maxSerializeBufferSize);
+	dynamicsWorld->serialize(serializer);
+	
+	FILE *file = fopen(filename, "wb");
+	fwrite(serializer->getBufferPointer(),serializer->getCurrentBufferSize(),1, file);
+	fclose(file);
+}
+
 /* ********************************** */
 /* Rigid Body Methods */
 





More information about the Bf-blender-cvs mailing list