Revision: 48012
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=48012
Author:   aramis_acg
Date:     2012-06-17 20:15:39 +0000 (Sun, 17 Jun 2012)
Log Message:
-----------
- bf_assimp: first work on animation importing. 
- bf_assimp: slightly refactor existing code (use refs wherever possible).

Modified Paths:
--------------
    branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.cpp
    branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.h
    branches/soc-2012-bratwurst/source/blender/assimp/ArmatureImporter.cpp
    branches/soc-2012-bratwurst/source/blender/assimp/ArmatureImporter.h
    branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.cpp
    branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.h

Modified: 
branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.cpp
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.cpp     
2012-06-17 16:59:58 UTC (rev 48011)
+++ branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.cpp     
2012-06-17 20:15:39 UTC (rev 48012)
@@ -0,0 +1,292 @@
+/*
+ * ***** 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.
+ *
+ * Contributor(s): Alexander Gessler
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/assimp/MeshImporter.cpp
+ *  \ingroup assimp
+ */
+
+#include <cassert>
+#include <sstream>
+
+#include "SceneImporter.h"
+#include "ArmatureImporter.h"
+#include "AnimationImporter.h"
+#include "bassimp_internal.h"
+
+extern "C" {
+#      include "BKE_global.h"
+#      include "BKE_main.h"
+#      include "BKE_mesh.h"
+#      include "BKE_displist.h"
+#      include "BKE_library.h"
+#      include "BKE_material.h"
+#      include "BKE_texture.h"
+#      include "BKE_armature.h"
+#      include "BKE_depsgraph.h"
+#      include "BKE_action.h"
+
+#      include "MEM_guardedalloc.h"
+
+#      include "BLI_listbase.h"
+#      include "BLI_math.h"
+#      include "BLI_string.h"
+}
+
+namespace bassimp {
+
+AnimationImporter::AnimationImporter(const SceneImporter& scene_imp, const 
aiAnimation& in_anim, 
+       const aiScene& in_scene, 
+       Scene& out_scene, 
+       unsigned int in_anim_index, 
+       const Object& ob_armature)
+: armature(*static_cast<bArmature*>(ob_armature.data)) 
+, ob_armature(ob_armature)
+, out_scene(out_scene)
+, in_anim(in_anim)
+, in_anim_index(in_anim_index)
+, in_scene(in_scene)
+, scene_imp(scene_imp)
+{
+       assert(ob_armature.type == OB_ARMATURE);
+
+       std::stringstream ss;
+       ss << "animation-" << in_anim_index;
+       logname = ss.str();
+}
+
+
+AnimationImporter::~AnimationImporter()
+{
+       
+}
+
+
+void AnimationImporter::error(const char* message)
+{
+       scene_imp.error((message + logname).c_str());
+}
+
+
+void AnimationImporter::verbose(const char* message)
+{
+       scene_imp.verbose((message + logname).c_str());
+}
+
+
+void AnimationImporter::convert()
+{
+       for (unsigned int i = 0; i < in_anim.mNumChannels; ++i) {
+               convert_node_anim(*in_anim.mChannels[i]);
+       }
+}
+
+
+void AnimationImporter::get_rna_path_for_joint(char *joint_path, size_t count, 
const char* name)
+{
+       BLI_snprintf(joint_path, count, "pose.bones[\"%s\"]", name);
+}
+
+
+void AnimationImporter::convert_node_anim(const aiNodeAnim& anim)
+{
+       verbose(("convert node animation: " + 
std::string(anim.mNodeName.C_Str())).c_str());
+
+       // 10 curves: rotation_quaternion, location and scale
+       FCurve *newcu[10] = {0};
+
+       setup_empty_fcurves(newcu,anim);
+       populate_fcurves(newcu,anim);
+}
+
+
+void AnimationImporter::setup_empty_fcurves(FCurve* curves_out[10], const 
aiNodeAnim& anim)
+{
+       char joint_path[200];
+       
get_rna_path_for_joint(joint_path,sizeof(joint_path),anim.mNodeName.C_Str());
+
+       for (int i = 0; i < 10; i++) {
+               int axis = i;
+
+               int vcount = 0;
+               const char* rna_postfix;
+
+               if (i < 4) {
+
+                       if (anim.mNumRotationKeys <= 1) {
+                               curves_out[i] = NULL;
+                               continue;
+                       }
+
+                       rna_postfix = "rotation_quaternion";
+                       axis = i;
+
+                       vcount = anim.mNumRotationKeys;
+               }
+               else if (i < 7) {
+                       if (anim.mNumPositionKeys <= 1) {
+                               curves_out[i] = NULL;
+                               continue;
+                       }
+
+                       rna_postfix = "location";
+                       axis = i - 4;
+
+                       vcount = anim.mNumPositionKeys;
+               }
+               else {
+                       if (anim.mNumScalingKeys <= 1) {
+                               curves_out[i] = NULL;
+                               continue;
+                       }
+
+                       rna_postfix = "scale";
+                       axis = i - 7;
+
+                       vcount = anim.mNumScalingKeys;
+               }
+
+               char rna_path[200];
+               BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, 
rna_postfix);             
+
+               curves_out[i] = create_fcurve(axis, rna_path);
+               curves_out[i]->totvert = vcount;
+       }
+}
+
+
+void AnimationImporter::populate_fcurves(FCurve* const curves_out[10], const 
aiNodeAnim& anim)
+{
+       if (anim.mNumRotationKeys > 1)  {
+               for (unsigned int i = 0; i < anim.mNumRotationKeys; ++i)        
{
+                       const aiQuatKey& q = anim.mRotationKeys[i];
+
+                       const float time = static_cast<float>(q.mTime);
+
+                       add_bezt(curves_out[0], time, q.mValue.w);
+                       add_bezt(curves_out[1], time, q.mValue.x);
+                       add_bezt(curves_out[2], time, q.mValue.y);
+                       add_bezt(curves_out[3], time, q.mValue.z);
+               }
+       }
+
+       if (anim.mNumPositionKeys > 1)  {
+               for (unsigned int i = 0; i < anim.mNumPositionKeys; ++i)        
{
+                       const aiVectorKey& v = anim.mPositionKeys[i];
+
+                       const float time = static_cast<float>(v.mTime);
+
+                       add_bezt(curves_out[4], time, v.mValue.x);
+                       add_bezt(curves_out[5], time, v.mValue.y);
+                       add_bezt(curves_out[6], time, v.mValue.z);
+               }
+       }
+
+       if (anim.mNumScalingKeys > 1)   {
+               for (unsigned int i = 0; i < anim.mNumScalingKeys; ++i) {
+                       const aiVectorKey& v = anim.mScalingKeys[i];
+
+                       const float time = static_cast<float>(v.mTime);
+
+                       add_bezt(curves_out[7], time, v.mValue.x);
+                       add_bezt(curves_out[8], time, v.mValue.y);
+                       add_bezt(curves_out[9], time, v.mValue.z);
+               }
+       }
+
+       verify_adt_action((ID *)&ob_armature.id, 1);
+       ListBase *curves = &ob_armature.adt->action->curves;
+
+       // add curves
+       for (int i = 0; i < 10; i++) {
+               if (!curves_out[i]){
+                       continue;
+               }
+               add_bone_fcurve(anim.mNodeName.C_Str(), curves_out[i]);
+       }
+
+       bPoseChannel* const chan = BKE_pose_channel_find_name(ob_armature.pose, 
anim.mNodeName.C_Str());
+       chan->rotmode = ROT_MODE_QUAT;
+
+       /*
+       if (is_joint) {
+               bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, 
bone_name);
+               chan->rotmode = ROT_MODE_QUAT;
+       }
+       else {
+               ob->rotmode = ROT_MODE_QUAT;
+       }*/
+       
+
+       return;
+}
+
+
+void AnimationImporter::add_bone_fcurve(const char* bone_name, FCurve *fcu)
+{
+       bAction *act = ob_armature.adt->action;
+
+       /* try to find group */
+       bActionGroup *grp = BKE_action_group_find_name(act, bone_name);
+
+       /* no matching groups, so add one */
+       if (grp == NULL) {
+               /* Add a new group, and make it active */
+               grp = (bActionGroup *)MEM_callocN(sizeof(bActionGroup), 
"bActionGroup");
+
+               grp->flag = AGRP_SELECTED;
+               BLI_strncpy(grp->name, bone_name, sizeof(grp->name));
+
+               BLI_addtail(&act->groups, grp);
+               BLI_uniquename(&act->groups, grp, "Group", '.', 
offsetof(bActionGroup, name), 64);
+       }
+
+       /* add F-Curve to group */
+       action_groups_add_channel(act, grp, fcu);
+}
+
+
+void AnimationImporter::add_bezt(FCurve *fcu, float fra, float value)
+{
+       BezTriple bez = {0};
+       
+       bez.vec[1][0] = fra;
+       bez.vec[1][1] = value;
+       bez.ipo = BEZT_IPO_LIN; /* use default interpolation mode here... */
+       bez.f1 = bez.f2 = bez.f3 = SELECT;
+       bez.h1 = bez.h2 = HD_AUTO;
+       insert_bezt_fcurve(fcu, &bez, 0);
+       calchandles_fcurve(fcu);
+}
+
+
+FCurve* AnimationImporter::create_fcurve(int array_index, const char *rna_path)
+{
+       FCurve* const fcu = (FCurve *)MEM_callocN(sizeof(FCurve), "FCurve");
+       fcu->flag = (FCURVE_VISIBLE | FCURVE_AUTO_HANDLES | FCURVE_SELECTED);
+       fcu->rna_path = BLI_strdupn(rna_path, strlen(rna_path));
+       fcu->array_index = array_index;
+       return fcu;
+}
+
+
+
+}

Modified: branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.h
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.h       
2012-06-17 16:59:58 UTC (rev 48011)
+++ branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.h       
2012-06-17 20:15:39 UTC (rev 48012)
@@ -38,28 +38,44 @@
 {
 private:
 
-       bArmature* armature;
-       Scene* out_scene;
-       const aiAnimation* in_anim;
-       unsigned int in_anim_index;
-       const aiScene* in_scene;
+       const bArmature& armature;
+       const Object& ob_armature;
 
+       Scene& out_scene;
+       const aiAnimation& in_anim;
+       const unsigned int in_anim_index;
+       const aiScene& in_scene;
+
        const SceneImporter& scene_imp;
-       
 
+       std::string logname;
+
 private:
 
        void error(const char* message);
        void verbose(const char* message);
 
+       void get_rna_path_for_joint(char *joint_path, size_t count, const char* 
name);
+       void add_bone_fcurve(const char* bone_name, FCurve *fcu);
+       FCurve* create_fcurve(int array_index, const char *rna_path);
+       void add_bezt(FCurve *fcu, float fra, float value);
+
+       void convert_node_anim(const aiNodeAnim& anim);
+       void setup_empty_fcurves(FCurve* curves_out[10], const aiNodeAnim& 
anim);
+       void populate_fcurves(FCurve* const curves_out[10], const aiNodeAnim& 
anim);
+
 public:
 
-       AnimationImporter(const SceneImporter& scene_imp, const aiAnimation* 
in_anim, const aiScene* in_scene, Scene* out_scene, unsigned int in_anim_index);
+       AnimationImporter(const SceneImporter& scene_imp, const aiAnimation& 
in_anim, 
+               const aiScene& in_scene, 
+               Scene& out_scene, 
+               unsigned int in_anim_index, 
+               const Object& ob_armature);
+
        ~AnimationImporter();
 
        // run conversion
        void convert();
-
 };
 
 }

Modified: branches/soc-2012-bratwurst/source/blender/assimp/ArmatureImporter.cpp
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/ArmatureImporter.cpp      
2012-06-17 16:59:58 UTC (rev 48011)
+++ branches/soc-2012-bratwurst/source/blender/assimp/ArmatureImporter.cpp      
2012-06-17 20:15:39 UTC (rev 48012)
@@ -115,9 +115,11 @@
 }
 
 
-void ArmatureImporter::convert_node(const aiNode* node, EditBone* parent, 
const aiNode* parentNode, unsigned int totchild)
+void ArmatureImporter::convert_node(const aiNode& node, EditBone* parent, 
const aiNode* parentNode, unsigned int totchild)
 {
-       EditBone* const bone = ED_armature_edit_bone_add(armature, (char 
*)node->mName.data);
+       verbose(("convert bone: " + std::string(node.mName.C_Str())).c_str());
+
+       EditBone* const bone = ED_armature_edit_bone_add(armature, (char 
*)node.mName.data);
        if(parent != NULL)
        {
                bone->parent = parent;
@@ -128,7 +130,7 @@
        }
 
        // set bone name
-       BLI_strncpy(bone->name,node->mName.C_Str(),sizeof(bone->name));
+       BLI_strncpy(bone->name,node.mName.C_Str(),sizeof(bone->name));
 
        float obmat[4][4];

@@ Diff output truncated at 10240 characters. @@
_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to