From db6ea00967bae67c16574dcef23997c55e701e6a Mon Sep 17 00:00:00 2001 From: rdb Date: Thu, 3 Nov 2022 13:14:35 +0100 Subject: [PATCH] assimp: Add assimp-collapse-dummy-root-node option This is false for now, but will be true in the future. See #366 --- pandatool/src/assimp/assimpLoader.cxx | 16 ++++++++++++++-- pandatool/src/assimp/config_assimp.cxx | 7 +++++++ pandatool/src/assimp/config_assimp.h | 1 + 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pandatool/src/assimp/assimpLoader.cxx b/pandatool/src/assimp/assimpLoader.cxx index 99407ccabe..377a59b1c5 100644 --- a/pandatool/src/assimp/assimpLoader.cxx +++ b/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 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); } diff --git a/pandatool/src/assimp/config_assimp.cxx b/pandatool/src/assimp/config_assimp.cxx index 78ea989e16..060b480d3a 100644 --- a/pandatool/src/assimp/config_assimp.cxx +++ b/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 diff --git a/pandatool/src/assimp/config_assimp.h b/pandatool/src/assimp/config_assimp.h index e17e2caae6..f999a60030 100644 --- a/pandatool/src/assimp/config_assimp.h +++ b/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();