[Bf-blender-cvs] [bd91d19c8d7] collada: Cleanup: Added Helper functions to get Scene actions

Gaia Clary noreply at git.blender.org
Wed Mar 28 21:30:10 CEST 2018


Commit: bd91d19c8d721aae2d69acd1ff888cda4bae4145
Author: Gaia Clary
Date:   Wed Feb 28 23:43:41 2018 +0100
Branches: collada
https://developer.blender.org/rBbd91d19c8d721aae2d69acd1ff888cda4bae4145

Cleanup: Added Helper functions to get Scene actions

This is mostly for cleaning up the AnimationExporter for
supporting the upcoming feature "export all animations".
I also added a few extra checks for existing animations,
which potentially avoid crashes when the calling functions
do not check for existing animations.

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

M	source/blender/collada/AnimationExporter.cpp
M	source/blender/collada/collada_utils.h

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

diff --git a/source/blender/collada/AnimationExporter.cpp b/source/blender/collada/AnimationExporter.cpp
index 5492fcbb625..e3f4e98eaaf 100644
--- a/source/blender/collada/AnimationExporter.cpp
+++ b/source/blender/collada/AnimationExporter.cpp
@@ -147,9 +147,14 @@ void AnimationExporter::create_sampled_animation(int channel_count,
  */
 void AnimationExporter::export_keyframed_animation_set(Object *ob)
 {
-	FCurve *fcu = (FCurve *)ob->adt->action->curves.first;
+	bAction *action = getSceneObjectAction(ob);
+	if (!action) {
+		return; /* Object has no animation */
+	}
+
+	FCurve *fcu = (FCurve *)action->curves.first;
 	if (!fcu) {
-		return; /* object has no animation */
+		return; /* animation has no fcurves */
 	}
 
 	if (this->export_settings->export_transformation_type == BC_TRANSFORMATION_TYPE_MATRIX) {
@@ -195,6 +200,11 @@ void AnimationExporter::export_keyframed_animation_set(Object *ob)
  */
 void AnimationExporter::export_sampled_animation_set(Object *ob)
 {
+	bAction *action = getSceneObjectAction(ob);
+	if (!action) {
+		return; /* Object has no animation */
+	}
+
 	std::vector<float>ctimes;
 	find_sampleframes(ob, ctimes);
 	if (ctimes.size() > 0) {
@@ -292,7 +302,9 @@ void AnimationExporter::operator()(Object *ob)
 	/* bool isMatAnim = false; */ /* UNUSED */
 
 	//Export transform animations
-	if (ob->adt && ob->adt->action) {
+	bAction *action = getSceneObjectAction(ob);
+
+	if (action) {
 
 		if (ob->type == OB_ARMATURE) {
 			/* Export skeletal animation (if any)*/
@@ -316,8 +328,9 @@ void AnimationExporter::operator()(Object *ob)
 	//export_morph_animation(ob);
 		
 	//Export Lamp parameter animations
-	if ( (ob->type == OB_LAMP) && ((Lamp *)ob->data)->adt && ((Lamp *)ob->data)->adt->action) {
-		FCurve *fcu = (FCurve *)(((Lamp *)ob->data)->adt->action->curves.first);
+	action = getSceneLampAction(ob);
+	if (action) {
+		FCurve *fcu = (FCurve *)action->curves.first;
 		while (fcu) {
 			transformName = extract_transform_name(fcu->rna_path);
 
@@ -331,8 +344,9 @@ void AnimationExporter::operator()(Object *ob)
 	}
 
 	//Export Camera parameter animations
-	if ( (ob->type == OB_CAMERA) && ((Camera *)ob->data)->adt && ((Camera *)ob->data)->adt->action) {
-		FCurve *fcu = (FCurve *)(((Camera *)ob->data)->adt->action->curves.first);
+	action = getSceneCameraAction(ob);
+	if (action) {
+		FCurve *fcu = (FCurve *)action->curves.first;
 		while (fcu) {
 			transformName = extract_transform_name(fcu->rna_path);
 
@@ -350,10 +364,10 @@ void AnimationExporter::operator()(Object *ob)
 	//Export Material parameter animations.
 	for (int a = 0; a < ob->totcol; a++) {
 		Material *ma = give_current_material(ob, a + 1);
-		if (!ma) continue;
-		if (ma->adt && ma->adt->action) {
+		action = getSceneMaterialAction(ma);
+		if (action) {
 			/* isMatAnim = true; */
-			FCurve *fcu = (FCurve *)ma->adt->action->curves.first;
+			FCurve *fcu = (FCurve *)action->curves.first;
 			while (fcu) {
 				transformName = extract_transform_name(fcu->rna_path);
 
@@ -434,10 +448,15 @@ void AnimationExporter::make_anim_frames_from_targets(Object *ob, std::vector<fl
 	}
 }
 
-//euler sources from quternion sources
+/* Euler sources from quternion sources 
+ * Important: We assume the object has a scene action.
+ * If it has not, then Blender will die
+*/
 float *AnimationExporter::get_eul_source_for_quat(Object *ob)
 {
-	FCurve *fcu = (FCurve *)ob->adt->action->curves.first;
+	bAction *action = getSceneObjectAction(ob);
+
+	FCurve *fcu = (FCurve *)action->curves.first;
 	const int keys = fcu->totvert;  
 	float *quat = (float *)MEM_callocN(sizeof(float) * fcu->totvert * 4, "quat output source values");
 	float *eul = (float *)MEM_callocN(sizeof(float) * fcu->totvert * 3, "quat output source values");
@@ -490,7 +509,10 @@ std::string AnimationExporter::getAnimationPathId(const FCurve *fcu)
 	return translate_id(rna_path);
 }
 
-/* convert f-curves to animation curves and write */
+/* convert f-curves to animation curves and write
+ * Important: We assume the object has a scene action.
+ * If it has not, then Blender will die!
+ */
 void AnimationExporter::create_keyframed_animation(Object *ob, FCurve *fcu, char *transformName, bool is_param, Material *ma)
 {
 	const char *axis_name = NULL;
@@ -660,7 +682,8 @@ void AnimationExporter::create_keyframed_animation(Object *ob, FCurve *fcu, char
 //write bone animations in transform matrix sources
 void AnimationExporter::write_bone_animation_matrix(Object *ob_arm, Bone *bone)
 {
-	if (!ob_arm->adt)
+	bAction *action = getSceneObjectAction(ob_arm);
+	if (!action)
 		return;
 
 	//This will only export animations of bones in deform group.
@@ -1688,17 +1711,16 @@ bool AnimationExporter::hasAnimations(Scene *sce)
 
 	for (node=this->export_settings->export_set; node; node=node->next) {
 		Object *ob = (Object *)node->link;
-
+		bAction *action = NULL;
 		FCurve *fcu = 0;
-		//Check for object transform animations
-		if (ob->adt && ob->adt->action)
-			fcu = (FCurve *)ob->adt->action->curves.first;
-		//Check for Lamp parameter animations
-		else if ( (ob->type == OB_LAMP) && ((Lamp *)ob->data)->adt && ((Lamp *)ob->data)->adt->action)
-			fcu = (FCurve *)(((Lamp *)ob->data)->adt->action->curves.first);
-		//Check for Camera parameter animations
-		else if ( (ob->type == OB_CAMERA) && ((Camera *)ob->data)->adt && ((Camera *)ob->data)->adt->action)
-			fcu = (FCurve *)(((Camera *)ob->data)->adt->action->curves.first);
+
+		/* Check for object,lamp and camera transform animations */
+		if (getSceneObjectAction(ob))
+			fcu = (FCurve *)getSceneObjectAction(ob)->curves.first;
+		else if (getSceneLampAction(ob))
+			fcu = (FCurve *)getSceneLampAction(ob)->curves.first;
+		else if (getSceneCameraAction(ob))
+			fcu = (FCurve *)getSceneCameraAction(ob)->curves.first;
 
 		//Check Material Effect parameter animations.
 		for (int a = 0; a < ob->totcol; a++) {
@@ -1876,6 +1898,11 @@ void AnimationExporter::sample_and_write_bone_animation(Object *ob_arm, Bone *bo
 
 void AnimationExporter::sample_animation(float *v, std::vector<float> &frames, int type, Bone *bone, Object *ob_arm, bPoseChannel *pchan)
 {
+	bAction *action = getSceneObjectAction(ob_arm);
+	if (!action) {
+		return;
+	}
+
 	bPoseChannel *parchan = NULL;
 	bPose *pose = ob_arm->pose;
 
diff --git a/source/blender/collada/collada_utils.h b/source/blender/collada/collada_utils.h
index 0069b4d4871..0b937a8aaef 100644
--- a/source/blender/collada/collada_utils.h
+++ b/source/blender/collada/collada_utils.h
@@ -39,7 +39,11 @@
 
 extern "C" {
 #include "DNA_object_types.h"
+#include "DNA_anim_types.h"
 #include "DNA_mesh_types.h"
+#include "DNA_lamp_types.h"
+#include "DNA_camera_types.h"
+
 #include "DNA_customdata_types.h"
 #include "DNA_texture_types.h"
 #include "DNA_scene_types.h"
@@ -67,6 +71,39 @@ extern Main *bc_get_main();
 extern EvaluationContext *bc_get_evaluation_context();
 extern void bc_update_scene(Scene *scene, float ctime);
 
+/* Action helpers */
+
+inline bAction *getSceneObjectAction(Object *ob)
+{
+	return (ob->adt && ob->adt->action) ? ob->adt->action : NULL;
+}
+
+inline bAction *getSceneLampAction(Object *ob)
+{
+	if (ob->type != OB_LAMP)
+		return NULL;
+
+	Lamp *lamp = (Lamp *)ob->data;
+	return (lamp->adt && lamp->adt->action) ? lamp->adt->action : NULL;
+}
+
+inline bAction *getSceneCameraAction(Object *ob)
+{
+	if (ob->type != OB_CAMERA)
+		return NULL;
+
+	Camera *camera = (Camera *)ob->data;
+	return (camera->adt && camera->adt->action) ? camera->adt->action : NULL;
+}
+
+inline bAction *getSceneMaterialAction(Material *ma)
+{
+	if (ma == NULL)
+		return NULL;
+
+	return (ma->adt && ma->adt->action) ? ma->adt->action : NULL;
+}
+
 extern float bc_get_float_value(const COLLADAFW::FloatOrDoubleArray& array, unsigned int index);
 extern int bc_test_parent_loop(Object *par, Object *ob);
 extern int bc_set_parent(Object *ob, Object *par, bContext *C, bool is_parent_space = true);



More information about the Bf-blender-cvs mailing list