Browse Source

assimp: Add assimp-collapse-dummy-root-node option

This is false for now, but will be true in the future.

See #366
rdb 3 years ago
parent
commit
db6ea00967

+ 14 - 2
pandatool/src/assimp/assimpLoader.cxx

@@ -1056,13 +1056,25 @@ load_node(const aiNode &node, PandaNode *parent) {
   if (node.mNumMeshes > 0) {
     pnode = new GeomNode(name);
   } else {
-    pnode = new PandaNode(name);
+    // Many importers create a dummy root node, but they all call it
+    // differently, and some (glTF) create it only conditionally.
+    // It usually has some funny name like <SomethingRoot> or $dummy_root,
+    // except the .obj loader, which assigns it the model base name like we do.
+    if (parent == _root && assimp_collapse_dummy_root_node &&
+        _charmap.find(node.mName.C_Str()) == _charmap.end() &&
+        (name.empty() || name[0] == '$' || name == "RootNode" || name == "ROOT" || name == "Root" || (name.size() > 2 && name[0] == '<' && name[name.size() - 1] == '>') || name == _root->get_name())) {
+      // Collapse root node.
+      pnode = _root;
+    } else {
+      pnode = new PandaNode(name);
+    }
   }
 
   if (_charmap.find(node.mName.C_Str()) != _charmap.end()) {
     character = _charmap[node.mName.C_Str()];
     parent->add_child(character);
-  } else {
+  }
+  else if (parent != pnode) {
     parent->add_child(pnode);
   }
 

+ 7 - 0
pandatool/src/assimp/config_assimp.cxx

@@ -86,6 +86,13 @@ ConfigVariableDouble assimp_smooth_normal_angle
           "normals. Note that you may need to clear the model-cache after "
           "changing this."));
 
+ConfigVariableBool assimp_collapse_dummy_root_node
+("assimp-collapse-dummy-root-node", false,
+ PRC_DESC("If set to true, collapses the root node that Assimp creates, if it "
+          "appears to be a synthetic dummy root node and contains no meshes.  "
+          "This variable is new as of Panda3D 1.10.13 and will become true by "
+          "default as of Panda3D 1.11.0."));
+
 /**
  * Initializes the library.  This must be called at least once before any of
  * the functions or classes in this library can be used.  Normally it will be

+ 1 - 0
pandatool/src/assimp/config_assimp.h

@@ -33,6 +33,7 @@ extern ConfigVariableBool assimp_optimize_graph;
 extern ConfigVariableBool assimp_flip_winding_order;
 extern ConfigVariableBool assimp_gen_normals;
 extern ConfigVariableDouble assimp_smooth_normal_angle;
+extern ConfigVariableBool assimp_collapse_dummy_root_node;
 
 extern EXPCL_ASSIMP void init_libassimp();