Revision: 50234
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=50234
Author:   aramis_acg
Date:     2012-08-27 01:33:02 +0000 (Mon, 27 Aug 2012)
Log Message:
-----------
- bf_assimp: strip root node if possible.

Modified Paths:
--------------
    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/SceneImporter.cpp
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.cpp 
2012-08-26 23:57:55 UTC (rev 50233)
+++ branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.cpp 
2012-08-27 01:33:02 UTC (rev 50234)
@@ -67,6 +67,7 @@
 , armature()
 , settings(settings)
 , log_pipe(!!settings.enableAssimpLog ? settings.reports : NULL)
+, root_collapsed()
 {
 }
 
@@ -178,6 +179,8 @@
                return false;
        }
 
+       collapse_root_node();
+
        populate_node_name_map(scene->mRootNode);
 
        // generate the subgraph of all mesh-carrier nodes
@@ -216,6 +219,76 @@
 }
 
 
+void SceneImporter::collapse_root_node()
+{
+       root_collapsed = false;
+       const aiNode* const nd = scene->mRootNode;
+
+       // look for reasons why we couldn't get rid of the root node
+       if(nd->mNumMeshes) {
+               return;
+       }
+
+       if(!nd->mNumChildren) {
+               return;
+       }
+
+       // lights assigned to root
+       for (unsigned int j = 0; j < scene->mNumLights; ++j) {
+               if (scene->mLights[j]->mName == nd->mName) {
+                       return;
+               }
+       }
+
+       // cameras assigned to root
+       for (unsigned int j = 0; j < scene->mNumCameras; ++j) {
+               if (scene->mCameras[j]->mName == nd->mName) {
+                       return;
+               }
+       }
+
+       // animations assigned to root
+       for (unsigned int i = 0; i < scene->mNumAnimations; ++i) {
+               const aiAnimation& anim = *scene->mAnimations[i];
+
+               for (unsigned int j = 0; j < anim.mNumChannels; ++j) {
+                       if (anim.mChannels[j]->mNodeName == nd->mName) {
+                               return;
+                       }
+               }
+       }
+
+       // non-identity root transform and animated children
+       if (!nd->mTransformation.IsIdentity()) {
+               for (unsigned int k = 0; k < nd->mNumChildren; ++k) {
+                       for (unsigned int i = 0; i < scene->mNumAnimations; 
++i) {
+
+                               const aiAnimation& anim = 
*scene->mAnimations[i];
+                               for (unsigned int j = 0; j < anim.mNumChannels; 
++j) {
+
+                                       if (anim.mChannels[j]->mNodeName == 
nd->mChildren[k]->mName) {
+                                               return;
+                                       }
+                               }
+                       }
+               }
+       }
+
+       // ok, drop the root node, apply its trafo to all children
+       for (unsigned int k = 0; k < nd->mNumChildren; ++k) {
+               nd->mChildren[k]->mTransformation = nd->mTransformation * 
nd->mChildren[k]->mTransformation;
+       }
+
+       // ignore const-correctness another time since this is not
+       // dangerous either, but it may prevent nasty conversion errors.
+       const_cast<aiNode*>(nd)->mTransformation = aiMatrix4x4();
+
+       root_collapsed = true;
+
+       verbose("collapse root node");
+}
+
+
 void SceneImporter::handle_scale()
 {
        // XXX make this configurable
@@ -371,7 +444,7 @@
 }
 
 
-void SceneImporter::convert_node(const aiNode& in_node, Object* out_parent) 
+void SceneImporter::convert_node(const aiNode& in_node, Object* out_parent, 
bool is_root) 
 {
        typedef std::vector<Object *> ObjectVector;
        ObjectVector objects_done;
@@ -379,6 +452,8 @@
        unsigned int meshes = 0, cameras = 0, lights = 0;
        verbose(("convert node: " + 
std::string(in_node.mName.C_Str())).c_str());
 
+       const bool root_drop = is_root && root_collapsed;
+
        // attach meshes
        if (in_node.mNumMeshes) {
 
@@ -437,7 +512,8 @@
        // are nodes that carry meshes in the subtree rooted at them. These must
        // be preserved to avoid loosing the trafo info.
 
-       if (meshes + cameras + lights  == 0 &&  (!has_bones || 
has_mesh_descendants(in_node))) {
+       if (meshes + cameras + lights  == 0 &&  (!has_bones || 
has_mesh_descendants(in_node)) && !root_drop) {
+               
                Object* const obj = util_add_object(out_scene, OB_EMPTY, 
in_node.mName.C_Str());
                objects_done.push_back(obj);
        }
@@ -474,30 +550,33 @@
        }
 
 
-       if (objects_done.empty()) {
+       if (objects_done.empty() && !root_drop) {
                return;
        }
 
-       Object& anchor = *objects_done.back();
-       convert_node_transform(in_node, anchor); 
+       Object* anchor = NULL; 
 
-       objects_by_node[&in_node] = &anchor;
+       if(!is_root || !root_collapsed) {
 
-       // set parent (unless this is root)
-       if(out_parent) {
-               util_set_parent(&anchor, out_parent ,&C, true);
-       }
+               anchor = objects_done.back();
+               convert_node_transform(in_node, *anchor); 
 
-       for (ObjectVector::iterator it = objects_done.begin(), end = 
objects_done.end() - 1; it != end; ++it) {
-               Object& obj = **it;
+               objects_by_node[&in_node] = anchor;
+               if(out_parent) {
+                       util_set_parent(anchor, out_parent ,&C, true);
+               }
 
-               set_identity_transform(obj);
-               util_set_parent(&obj,&anchor,&C,true);
+               for (ObjectVector::iterator it = objects_done.begin(), end = 
objects_done.end() - 1; it != end; ++it) {
+                       Object& obj = **it;
+
+                       set_identity_transform(obj);
+                       util_set_parent(&obj,anchor,&C,true);
+               }
        }
 
        // recursively convert child nodes
        for (unsigned int i = 0, c = in_node.mNumChildren; i < c; ++i) {
-               convert_node(*in_node.mChildren[i],&anchor);
+               convert_node(*in_node.mChildren[i],anchor, false);
        }
 }
 

Modified: branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.h
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.h   
2012-08-26 23:57:55 UTC (rev 50233)
+++ branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.h   
2012-08-27 01:33:02 UTC (rev 50234)
@@ -75,6 +75,8 @@
        const bassimp_import_settings settings;
        LogPipe log_pipe;
 
+       bool root_collapsed;
+
 private:
 
        void configure_importer();
@@ -85,7 +87,7 @@
        void convert_armature();
        void convert_skin();
 
-       void convert_node(const aiNode& in_node, Object* out_parent);
+       void convert_node(const aiNode& in_node, Object* out_parent, bool 
is_root = true);
        void convert_node_transform(const aiNode& node_in, Object& out_obj) 
const;
        void set_identity_transform(Object& out_obj) const;
 
@@ -97,6 +99,7 @@
        void handle_coordinate_space();
        void populate_mesh_node_graph(const aiNode* node);
        void populate_node_name_map(const aiNode* node);
+       void collapse_root_node();
 
 public:
 

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to