1 | /* |
2 | Open Asset Import Library (assimp) |
3 | ---------------------------------------------------------------------- |
4 | |
5 | Copyright (c) 2006-2024, 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.useSkeleton = pImp->GetPropertyBool(AI_CONFIG_FBX_USE_SKELETON_BONE_CONTAINER, bErrorReturn: false); |
121 | } |
122 | |
123 | // ------------------------------------------------------------------------------------------------ |
124 | // Imports the given file into the given scene structure. |
125 | void FBXImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) { |
126 | auto streamCloser = [&](IOStream *pStream) { |
127 | pIOHandler->Close(pFile: pStream); |
128 | }; |
129 | std::unique_ptr<IOStream, decltype(streamCloser)> stream(pIOHandler->Open(pFile, pMode: "rb" ), streamCloser); |
130 | if (!stream) { |
131 | ThrowException(args: "Could not open file for reading" ); |
132 | } |
133 | |
134 | ASSIMP_LOG_DEBUG("Reading FBX file" ); |
135 | |
136 | // read entire file into memory - no streaming for this, fbx |
137 | // files can grow large, but the assimp output data structure |
138 | // then becomes very large, too. Assimp doesn't support |
139 | // streaming for its output data structures so the net win with |
140 | // streaming input data would be very low. |
141 | std::vector<char> contents; |
142 | contents.resize(new_size: stream->FileSize() + 1); |
143 | stream->Read(pvBuffer: &*contents.begin(), pSize: 1, pCount: contents.size() - 1); |
144 | contents[contents.size() - 1] = 0; |
145 | const char *const begin = &*contents.begin(); |
146 | |
147 | // broad-phase tokenized pass in which we identify the core |
148 | // syntax elements of FBX (brackets, commas, key:value mappings) |
149 | TokenList tokens; |
150 | Assimp::StackAllocator tempAllocator; |
151 | try { |
152 | bool is_binary = false; |
153 | if (!strncmp(s1: begin, s2: "Kaydara FBX Binary" , n: 18)) { |
154 | is_binary = true; |
155 | TokenizeBinary(output_tokens&: tokens, input: begin, length: contents.size(), tokenAllocator&: tempAllocator); |
156 | } else { |
157 | Tokenize(output_tokens&: tokens, input: begin, tokenAllocator&: tempAllocator); |
158 | } |
159 | |
160 | // use this information to construct a very rudimentary |
161 | // parse-tree representing the FBX scope structure |
162 | Parser parser(tokens, tempAllocator, is_binary); |
163 | |
164 | // take the raw parse-tree and convert it to a FBX DOM |
165 | Document doc(parser, mSettings); |
166 | |
167 | // convert the FBX DOM to aiScene |
168 | ConvertToAssimpScene(out: pScene, doc, removeEmptyBones: mSettings.removeEmptyBones); |
169 | |
170 | // size relative to cm |
171 | float size_relative_to_cm = doc.GlobalSettings().UnitScaleFactor(); |
172 | if (size_relative_to_cm == 0.0) { |
173 | // BaseImporter later asserts that fileScale is non-zero. |
174 | ThrowException(args: "The UnitScaleFactor must be non-zero" ); |
175 | } |
176 | |
177 | // Set FBX file scale is relative to CM must be converted to M for |
178 | // assimp universal format (M) |
179 | SetFileScale(size_relative_to_cm * 0.01f); |
180 | |
181 | // This collection does not own the memory for the tokens, but we need to call their d'tor |
182 | std::for_each(first: tokens.begin(), last: tokens.end(), f: Util::destructor_fun<Token>()); |
183 | |
184 | } catch (std::exception &) { |
185 | std::for_each(first: tokens.begin(), last: tokens.end(), f: Util::destructor_fun<Token>()); |
186 | throw; |
187 | } |
188 | } |
189 | |
190 | #endif // !ASSIMP_BUILD_NO_FBX_IMPORTER |
191 | |