1/*
2Open Asset Import Library (assimp)
3----------------------------------------------------------------------
4
5Copyright (c) 2006-2024, assimp team
6
7All rights reserved.
8
9Redistribution and use of this software in source and binary forms,
10with or without modification, are permitted provided that the
11following 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.
16r
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
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37OF 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
61namespace Assimp {
62
63template <>
64const char *LogFunctions<FBXImporter>::Prefix() {
65 return "FBX: ";
66}
67
68} // namespace Assimp
69
70using namespace Assimp;
71using namespace Assimp::Formatter;
72using namespace Assimp::FBX;
73
74namespace {
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.
91bool 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
99const aiImporterDesc *FBXImporter::GetInfo() const {
100 return &desc;
101}
102
103// ------------------------------------------------------------------------------------------------
104// Setup configuration properties for the loader
105void 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.
125void 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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of qtquick3d/src/3rdparty/assimp/src/code/AssetLib/FBX/FBXImporter.cpp