FBXImporter.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. r
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ----------------------------------------------------------------------
  33. */
  34. /** @file FBXImporter.cpp
  35. * @brief Implementation of the FBX importer.
  36. */
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. #include "FBXImporter.h"
  39. #include "FBXConverter.h"
  40. #include "FBXDocument.h"
  41. #include "FBXParser.h"
  42. #include "FBXTokenizer.h"
  43. #include "FBXUtil.h"
  44. #include <assimp/MemoryIOWrapper.h>
  45. #include <assimp/StreamReader.h>
  46. #include <assimp/importerdesc.h>
  47. #include <assimp/Importer.hpp>
  48. namespace Assimp {
  49. template <>
  50. const char *LogFunctions<FBXImporter>::Prefix() {
  51. return "FBX: ";
  52. }
  53. } // namespace Assimp
  54. using namespace Assimp;
  55. using namespace Assimp::Formatter;
  56. using namespace Assimp::FBX;
  57. namespace {
  58. static constexpr aiImporterDesc desc = {
  59. "Autodesk FBX Importer",
  60. "",
  61. "",
  62. "",
  63. aiImporterFlags_SupportTextFlavour,
  64. 0,
  65. 0,
  66. 0,
  67. 0,
  68. "fbx"
  69. };
  70. }
  71. // ------------------------------------------------------------------------------------------------
  72. // Returns whether the class can handle the format of the given file.
  73. bool FBXImporter::CanRead(const std::string & pFile, IOSystem * pIOHandler, bool /*checkSig*/) const {
  74. // at least ASCII-FBX files usually have a 'FBX' somewhere in their head
  75. static const char *tokens[] = { " \n\r\n " };
  76. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens));
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. // List all extensions handled by this loader
  80. const aiImporterDesc *FBXImporter::GetInfo() const {
  81. return &desc;
  82. }
  83. // ------------------------------------------------------------------------------------------------
  84. // Setup configuration properties for the loader
  85. void FBXImporter::SetupProperties(const Importer *pImp) {
  86. mSettings.readAllLayers = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_GEOMETRY_LAYERS, true);
  87. mSettings.readAllMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_MATERIALS, false);
  88. mSettings.readMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_MATERIALS, true);
  89. mSettings.readTextures = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_TEXTURES, true);
  90. mSettings.readCameras = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_CAMERAS, true);
  91. mSettings.readLights = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_LIGHTS, true);
  92. mSettings.readAnimations = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ANIMATIONS, true);
  93. mSettings.readWeights = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_WEIGHTS, true);
  94. mSettings.strictMode = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_STRICT_MODE, false);
  95. mSettings.preservePivots = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_PRESERVE_PIVOTS, true);
  96. mSettings.optimizeEmptyAnimationCurves = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_OPTIMIZE_EMPTY_ANIMATION_CURVES, true);
  97. mSettings.useLegacyEmbeddedTextureNaming = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_EMBEDDED_TEXTURES_LEGACY_NAMING, false);
  98. mSettings.removeEmptyBones = pImp->GetPropertyBool(AI_CONFIG_IMPORT_REMOVE_EMPTY_BONES, true);
  99. mSettings.convertToMeters = pImp->GetPropertyBool(AI_CONFIG_FBX_CONVERT_TO_M, false);
  100. mSettings.ignoreUpDirection = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_IGNORE_UP_DIRECTION, false);
  101. mSettings.useSkeleton = pImp->GetPropertyBool(AI_CONFIG_FBX_USE_SKELETON_BONE_CONTAINER, false);
  102. }
  103. // ------------------------------------------------------------------------------------------------
  104. // Imports the given file into the given scene structure.
  105. void FBXImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
  106. auto streamCloser = [&](IOStream *pStream) {
  107. pIOHandler->Close(pStream);
  108. };
  109. std::unique_ptr<IOStream, decltype(streamCloser)> stream(pIOHandler->Open(pFile, "rb"), streamCloser);
  110. if (!stream) {
  111. ThrowException("Could not open file for reading");
  112. }
  113. ASSIMP_LOG_DEBUG("Reading FBX file");
  114. // read entire file into memory - no streaming for this, fbx
  115. // files can grow large, but the assimp output data structure
  116. // then becomes very large, too. Assimp doesn't support
  117. // streaming for its output data structures so the net win with
  118. // streaming input data would be very low.
  119. std::vector<char> contents;
  120. contents.resize(stream->FileSize() + 1);
  121. stream->Read(&*contents.begin(), 1, contents.size() - 1);
  122. contents[contents.size() - 1] = 0;
  123. const char *const begin = &*contents.begin();
  124. // broad-phase tokenized pass in which we identify the core
  125. // syntax elements of FBX (brackets, commas, key:value mappings)
  126. TokenList tokens;
  127. Assimp::StackAllocator tempAllocator;
  128. try {
  129. bool is_binary = false;
  130. if (!strncmp(begin, "Kaydara FBX Binary", 18)) {
  131. is_binary = true;
  132. TokenizeBinary(tokens, begin, contents.size(), tempAllocator);
  133. } else {
  134. Tokenize(tokens, begin, tempAllocator);
  135. }
  136. // use this information to construct a very rudimentary
  137. // parse-tree representing the FBX scope structure
  138. Parser parser(tokens, tempAllocator, is_binary);
  139. // take the raw parse-tree and convert it to a FBX DOM
  140. Document doc(parser, mSettings);
  141. // convert the FBX DOM to aiScene
  142. ConvertToAssimpScene(pScene, doc, mSettings.removeEmptyBones);
  143. // size relative to cm
  144. float size_relative_to_cm = doc.GlobalSettings().UnitScaleFactor();
  145. if (size_relative_to_cm == 0.0) {
  146. // BaseImporter later asserts that fileScale is non-zero.
  147. ThrowException("The UnitScaleFactor must be non-zero");
  148. }
  149. // Set FBX file scale is relative to CM must be converted to M for
  150. // assimp universal format (M)
  151. SetFileScale(size_relative_to_cm * 0.01f);
  152. // This collection does not own the memory for the tokens, but we need to call their d'tor
  153. std::for_each(tokens.begin(), tokens.end(), Util::destructor_fun<Token>());
  154. } catch (std::exception &) {
  155. std::for_each(tokens.begin(), tokens.end(), Util::destructor_fun<Token>());
  156. throw;
  157. }
  158. }
  159. #endif // !ASSIMP_BUILD_NO_FBX_IMPORTER