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