diff --git a/graphics/src/ColladaLoader.cc b/graphics/src/ColladaLoader.cc index 7812b36cc..7bf473576 100644 --- a/graphics/src/ColladaLoader.cc +++ b/graphics/src/ColladaLoader.cc @@ -477,6 +477,31 @@ void ColladaLoaderPrivate::LoadNode(tinyxml2::XMLElement *_elem, Mesh *_mesh, { this->currentNodeName = _elem->Attribute("name"); } + else + { + // if node does not have a name, then append the mesh in this node + // to the first ancestor node that has a name, i.e. this mesh becomes + // part of the parent / ancestor mesh + tinyxml2::XMLElement *parent = + dynamic_cast(_elem->Parent()); + std::string nodeName; + while (parent && std::string(parent->Value()) == "node") + { + const char *name = parent->Attribute("name"); + if (name) + { + nodeName = name; + break; + } + } + if (nodeName.empty()) + { + // if none of the ancestor node has a name, then create a custom name + static int nodeCounter = 0; + nodeName = "unnamed_submesh_" + std::to_string(nodeCounter++); + } + this->currentNodeName = nodeName; + } if (_elem->FirstChildElement("instance_node")) { diff --git a/graphics/src/ColladaLoader_TEST.cc b/graphics/src/ColladaLoader_TEST.cc index 8c360e80f..45eca5228 100644 --- a/graphics/src/ColladaLoader_TEST.cc +++ b/graphics/src/ColladaLoader_TEST.cc @@ -365,6 +365,32 @@ TEST_F(ColladaLoader, LoadBoxWithMultipleGeoms) EXPECT_EQ(0u, mesh->SubMeshByIndex(1).lock()->NodeAssignmentsCount()); } +///////////////////////////////////////////////// +TEST_F(ColladaLoader, LoadBoxWithHierarchicalNodes) +{ + common::ColladaLoader loader; + common::Mesh *mesh = loader.Load( + std::string(PROJECT_SOURCE_PATH) + + "/test/data/box_with_hierarchical_nodes.dae"); + + ASSERT_EQ(5u, mesh->SubMeshCount()); + + // node by itself + EXPECT_EQ("StaticCube", mesh->SubMeshByIndex(0).lock()->Name()); + + // nested node with no name so it takes the parent's name instead + EXPECT_EQ("StaticCubeParent", mesh->SubMeshByIndex(1).lock()->Name()); + + // parent node containing child node with no name + EXPECT_EQ("StaticCubeParent", mesh->SubMeshByIndex(2).lock()->Name()); + + // nested node with name + EXPECT_EQ("StaticCubeNested", mesh->SubMeshByIndex(3).lock()->Name()); + + // Parent of nested node with name + EXPECT_EQ("StaticCubeParent2", mesh->SubMeshByIndex(4).lock()->Name()); +} + ///////////////////////////////////////////////// TEST_F(ColladaLoader, MergeBoxWithDoubleSkeleton) { diff --git a/test/data/box_with_hierarchical_nodes.dae b/test/data/box_with_hierarchical_nodes.dae new file mode 100644 index 000000000..0f4950ce4 --- /dev/null +++ b/test/data/box_with_hierarchical_nodes.dae @@ -0,0 +1,86 @@ + + + + + Blender User + Blender 2.80.75 commit date:2019-07-29, commit time:14:47, hash:f6cb5f54494e + + 2019-09-13T12:07:34 + 2019-09-13T12:07:34 + + Z_UP + + + + + + + -1 -3.545556 0 -1 -3.545556 2 -1 -1.545556 0 -1 -1.545556 2 1 -3.545556 0 1 -3.545556 2 1 -1.545556 0 1 -1.545556 2 + + + + + + + + + + -1 0 0 0 1 0 1 0 0 0 -1 0 0 0 -1 0 0 1 + + + + + + + + + + 0.375 0 0.625 0 0.625 0.25 0.375 0.25 0.375 0.25 0.625 0.25 0.625 0.5 0.375 0.5 0.375 0.5 0.625 0.5 0.625 0.75 0.375 0.75 0.375 0.75 0.625 0.75 0.625 1 0.375 1 0.125 0.5 0.375 0.5 0.375 0.75 0.125 0.75 0.625 0.5 0.875 0.5 0.875 0.75 0.625 0.75 + + + + + + + + + + + + + + + 4 4 4 4 4 4 +

0 0 0 1 0 1 3 0 2 2 0 3 2 1 4 3 1 5 7 1 6 6 1 7 6 2 8 7 2 9 5 2 10 4 2 11 4 3 12 5 3 13 1 3 14 0 3 15 2 4 16 6 4 17 4 4 18 0 4 19 7 5 20 3 5 21 1 5 22 5 5 23

+
+
+
+
+ + + + 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 + + + + 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 + + + 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 + + + + + 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 + + + 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 + + + + + + + + +