diff --git a/CMakeLists.txt b/CMakeLists.txt index 18a4be1..d656c78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ include(cmake/flatbuffer.cmake) add_subdirectory(examples/EncodeSample) add_subdirectory(examples/DecodeSample) +add_subdirectory(examples/FbxSample) add_subdirectory(src/FbxLibra) add_dependencies(FbxLibra generate_flatbuffers_code) diff --git a/src/FbxLibra/CMakeLists.txt b/src/FbxLibra/CMakeLists.txt index 18de881..3abfad8 100644 --- a/src/FbxLibra/CMakeLists.txt +++ b/src/FbxLibra/CMakeLists.txt @@ -18,6 +18,7 @@ set(SOURCE_FILES main.cpp Status.h FlatBufferLoader.h + FbxClient.h FBXLibra.h FBXLibra.cpp CounterWeight/HierarchyCounterWeight.h diff --git a/src/FbxLibra/CounterWeight/HierarchyCounterWeightFactory.cpp b/src/FbxLibra/CounterWeight/HierarchyCounterWeightFactory.cpp index 434c6d3..92bf91b 100644 --- a/src/FbxLibra/CounterWeight/HierarchyCounterWeightFactory.cpp +++ b/src/FbxLibra/CounterWeight/HierarchyCounterWeightFactory.cpp @@ -6,24 +6,18 @@ #include "HierarchyCounterWeightFactory.h" #include "HierarchyCounterWeight.h" #include "../FlatBufferLoader.h" +#include "../FbxClient.h" #include "flatbuffers/flatbuffers.h" CounterWeight *HierarchyCounterWeightFactory::CreateCounterWeight(const std::filesystem::path &fbx_path) { - FbxManager* manager = FbxManager::Create(); - FbxIOSettings* ios = FbxIOSettings::Create(manager, IOSROOT); - manager->SetIOSettings(ios); - - FbxImporter* importer = FbxImporter::Create(manager, ""); - - if (!importer->Initialize(fbx_path.string().c_str(), -1, manager->GetIOSettings())){ - std::cerr << "Failed to initialize FBX importer: " << importer->GetStatus().GetErrorString() << std::endl; - return nullptr; + FbxClient client; + if (!client.Load(fbx_path)) + { + std::cerr << "Failed to load FBX file: " << fbx_path << std::endl; + return nullptr; } - FbxScene* scene = FbxScene::Create(manager, "Scene"); - importer->Import(scene); - - FbxNode* rootNode = scene->GetRootNode(); + FbxNode* rootNode = client.GetRootNode(); std::vector> nodes; if (rootNode){ @@ -37,11 +31,6 @@ CounterWeight *HierarchyCounterWeightFactory::CreateCounterWeight(const std::fil this->builder->Finish(hierarchy_offset); auto new_hierarchy = flatbuffers::GetRoot(builder->GetBufferPointer()); - importer->Destroy(); - scene->Destroy(); - ios->Destroy(); - manager->Destroy(); - return new HierarchyCounterWeight(new_hierarchy); } diff --git a/src/FbxLibra/CounterWeight/SkinWeightCounterWeightFactory.cpp b/src/FbxLibra/CounterWeight/SkinWeightCounterWeightFactory.cpp index b441620..a8a7ac6 100644 --- a/src/FbxLibra/CounterWeight/SkinWeightCounterWeightFactory.cpp +++ b/src/FbxLibra/CounterWeight/SkinWeightCounterWeightFactory.cpp @@ -9,22 +9,20 @@ #include "../FlatBufferLoader.h" #include "cw_generated.h" #include "SkinWeightCounterWeight.h" +#include "../FbxClient.h" +#include "fbxsdk.h" + CounterWeight *SkinWeightCounterWeightFactory::CreateCounterWeight(const std::filesystem::path &fbx_path) { - FbxManager* manager = FbxManager::Create(); - FbxIOSettings* ios = FbxIOSettings::Create(manager, IOSROOT); - manager->SetIOSettings(ios); - - FbxImporter* importer = FbxImporter::Create(manager, ""); - if (!importer->Initialize(fbx_path.string().c_str(), -1, manager->GetIOSettings())) { - std::cerr << "Failed to initialize FBX importer: " << importer->GetStatus().GetErrorString() << std::endl; - return nullptr; + FbxClient client; + if (!client.Load(fbx_path)) + { + std::cerr << "Failed to load FBX file: " << fbx_path << std::endl; + return nullptr; } - FbxScene* scene = FbxScene::Create(manager, "Scene"); - importer->Import(scene); std::vector> skinWeightList; - FbxNode* rootNode = scene->GetRootNode(); + FbxNode* rootNode = client.GetRootNode(); if (rootNode) { ProcessNode(rootNode, skinWeightList); diff --git a/src/FbxLibra/CounterWeight/VertexCounterWeightFactory.cpp b/src/FbxLibra/CounterWeight/VertexCounterWeightFactory.cpp index ada43ee..2a6d710 100644 --- a/src/FbxLibra/CounterWeight/VertexCounterWeightFactory.cpp +++ b/src/FbxLibra/CounterWeight/VertexCounterWeightFactory.cpp @@ -2,26 +2,20 @@ #include "VertexCounterWeight.h" #include "VertexCounterWeightFactory.h" #include "../FlatBufferLoader.h" +#include "../FbxClient.h" using namespace std; using namespace Weight; CounterWeight* VertexCounterWeightFactory::CreateCounterWeight(const std::filesystem::path& fbx_path) { - FbxManager* manager = FbxManager::Create(); - FbxIOSettings* ios = FbxIOSettings::Create(manager, IOSROOT); - manager->SetIOSettings(ios); - - FbxImporter* importer = FbxImporter::Create(manager, ""); - - if (!importer->Initialize(fbx_path.string().c_str(), -1, manager->GetIOSettings())) { - std::cerr << "Failed to initialize FBX importer: " << importer->GetStatus().GetErrorString() << std::endl; - return nullptr; + FbxClient client; + if (!client.Load(fbx_path)) + { + std::cerr << "Failed to load FBX file: " << fbx_path << std::endl; + return nullptr; } - FbxScene* scene = FbxScene::Create(manager, "Scene"); - importer->Import(scene); - - FbxNode* rootNode = scene->GetRootNode(); + FbxNode* rootNode = client.GetRootNode(); //scene内のMeshノードを取得 std::vector mesh_nodes; @@ -140,11 +134,6 @@ CounterWeight* VertexCounterWeightFactory::CreateCounterWeight(const std::filesy // FinishMeshesBuffer(*builder, meshes_offsets); auto ne = flatbuffers::GetRoot(builder->GetBufferPointer()); - importer->Destroy(); - scene->Destroy(); - ios->Destroy(); - manager->Destroy(); - return new VertexCounterWeight(ne);; } diff --git a/src/FbxLibra/FbxClient.h b/src/FbxLibra/FbxClient.h new file mode 100644 index 0000000..43bce80 --- /dev/null +++ b/src/FbxLibra/FbxClient.h @@ -0,0 +1,51 @@ +#ifndef FBXCLIENT_H +#define FBXCLIENT_H +#include +#include + +#include "fbxsdk.h" + +class FbxClient { +private: + FbxNode* root; + + // RAII(Resource Acquisition Is Initialization)を利用してFbxManager, FbxIOSettings, FbxSceneを破棄する + static void DestroyFbxManager(FbxManager* manager) { manager->Destroy(); } + static void DestroyFbxIOSettings(FbxIOSettings* ios) { ios->Destroy(); } + static void DestroyFbxScene(FbxScene* scene) { scene->Destroy(); } + +public: + FbxClient(): root(nullptr), + manager(FbxManager::Create(), DestroyFbxManager), + ios(FbxIOSettings::Create(manager.get(), IOSROOT), DestroyFbxIOSettings), + scene(FbxScene::Create(manager.get(), "Scene"), DestroyFbxScene){} + + std::unique_ptr manager; + std::unique_ptr ios; + std::unique_ptr scene; + bool Load(const std::filesystem::path& fbx_path) + { + manager->SetIOSettings(ios.get()); + if (!manager || !ios || !scene) { + std::cerr << "Failed to create FbxManager, FbxIOSettings, or FbxScene." << std::endl; + return false; + } + + FbxImporter* importer = FbxImporter::Create(manager.get(), ""); + + if (!importer->Initialize(fbx_path.string().c_str(), -1, manager->GetIOSettings())){ + std::cerr << "Failed to initialize FBX importer: " << importer->GetStatus().GetErrorString() << std::endl; + return false; + } + importer->Import(scene.get()); + importer->Destroy(); + + root = scene->GetRootNode(); + return true; + }; + [[nodiscard]] FbxNode* GetRootNode() const { return root; } +}; + + + +#endif //FBXCLIENT_H diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 09d0b7c..197aa6e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -22,6 +22,7 @@ configure_file( add_executable(${PROJECT_NAME} ../src/FbxLibra/Status.h + ../src/FbxLibra/FbxClient.h ../src/FbxLibra/FlatBufferLoader.h ../src/FbxLibra/FBXLibra.h ../src/FbxLibra/FBXLibra.cpp