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

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