Coverage Report

Created: 2026-09-14 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/AssetLib/FBX/FBXImporter.cpp
Line
Count
Source
1
/*
2
Open Asset Import Library (assimp)
3
----------------------------------------------------------------------
4
5
Copyright (c) 2006-2026, 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
11
const char *LogFunctions<FBXImporter>::Prefix() {
65
11
  return "FBX: ";
66
11
}
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
      "Autodesk FBX Importer",
77
      "",
78
      "",
79
      "",
80
      aiImporterFlags_SupportTextFlavour,
81
      0,
82
      0,
83
      0,
84
      0,
85
      "fbx"
86
    };
87
}
88
89
// ------------------------------------------------------------------------------------------------
90
// Returns whether the class can handle the format of the given file.
91
6.01k
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
6.01k
  static const char *tokens[] = { " \n\r\n ", "fbx" };
94
6.01k
  return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens));
95
6.01k
}
96
97
// ------------------------------------------------------------------------------------------------
98
// List all extensions handled by this loader
99
181k
const aiImporterDesc *FBXImporter::GetInfo() const {
100
181k
  return &desc;
101
181k
}
102
103
// ------------------------------------------------------------------------------------------------
104
// Setup configuration properties for the loader
105
20.4k
void FBXImporter::SetupProperties(const Importer *pImp) {
106
20.4k
    mSettings.readAllLayers = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_GEOMETRY_LAYERS, true);
107
20.4k
    mSettings.readAllMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_MATERIALS, false);
108
20.4k
    mSettings.readMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_MATERIALS, true);
109
20.4k
    mSettings.readTextures = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_TEXTURES, true);
110
20.4k
    mSettings.readCameras = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_CAMERAS, true);
111
20.4k
    mSettings.readLights = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_LIGHTS, true);
112
20.4k
    mSettings.readAnimations = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ANIMATIONS, true);
113
20.4k
    mSettings.readWeights = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_WEIGHTS, true);
114
20.4k
    mSettings.strictMode = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_STRICT_MODE, false);
115
20.4k
    mSettings.preservePivots = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_PRESERVE_PIVOTS, true);
116
20.4k
    mSettings.optimizeEmptyAnimationCurves = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_OPTIMIZE_EMPTY_ANIMATION_CURVES, true);
117
20.4k
    mSettings.useLegacyEmbeddedTextureNaming = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_EMBEDDED_TEXTURES_LEGACY_NAMING, false);
118
20.4k
    mSettings.removeEmptyBones = pImp->GetPropertyBool(AI_CONFIG_IMPORT_REMOVE_EMPTY_BONES, true);
119
20.4k
    mSettings.convertToMeters = pImp->GetPropertyBool(AI_CONFIG_FBX_CONVERT_TO_M, false);
120
20.4k
    mSettings.ignoreUpDirection = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_IGNORE_UP_DIRECTION, false);
121
20.4k
    mSettings.useSkeleton = pImp->GetPropertyBool(AI_CONFIG_FBX_USE_SKELETON_BONE_CONTAINER, false);
122
20.4k
}
123
124
// ------------------------------------------------------------------------------------------------
125
// Imports the given file into the given scene structure.
126
20.4k
void FBXImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
127
20.4k
  auto streamCloser = [&](IOStream *pStream) {
128
20.4k
    pIOHandler->Close(pStream);
129
20.4k
  };
130
20.4k
  std::unique_ptr<IOStream, decltype(streamCloser)> stream(pIOHandler->Open(pFile, "rb"), streamCloser);
131
20.4k
  if (!stream) {
132
0
    ThrowException("Could not open file for reading");
133
0
  }
134
135
20.4k
    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
20.4k
  std::vector<char> contents;
143
20.4k
  contents.resize(stream->FileSize() + 1);
144
20.4k
  stream->Read(&*contents.begin(), 1, contents.size() - 1);
145
20.4k
  contents[contents.size() - 1] = 0;
146
20.4k
  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
20.4k
  TokenList tokens;
151
20.4k
    Assimp::StackAllocator tempAllocator;
152
20.4k
    try {
153
20.4k
    bool is_binary = false;
154
20.4k
    if (!strncmp(begin, "Kaydara FBX Binary", 18)) {
155
3.28k
      is_binary = true;
156
3.28k
            TokenizeBinary(tokens, begin, contents.size(), tempAllocator);
157
17.1k
    } else {
158
17.1k
            Tokenize(tokens, begin, tempAllocator);
159
17.1k
    }
160
161
    // use this information to construct a very rudimentary
162
    // parse-tree representing the FBX scope structure
163
20.4k
        Parser parser(tokens, tempAllocator, is_binary);
164
165
    // take the raw parse-tree and convert it to a FBX DOM
166
20.4k
    Document doc(parser, mSettings);
167
168
    // convert the FBX DOM to aiScene
169
20.4k
    ConvertToAssimpScene(pScene, doc, mSettings.removeEmptyBones);
170
171
    // size relative to cm
172
20.4k
    float size_relative_to_cm = doc.GlobalSettings().UnitScaleFactor();
173
20.4k
        if (size_relative_to_cm == 0.0) {
174
      // BaseImporter later asserts that fileScale is non-zero.
175
11
      ThrowException("The UnitScaleFactor must be non-zero");
176
11
        }
177
178
    // Set FBX file scale is relative to CM must be converted to M for
179
    // assimp universal format (M)
180
20.4k
    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
20.4k
        std::for_each(tokens.begin(), tokens.end(), Util::destructor_fun<Token>());
184
185
20.4k
    } catch (std::exception &) {
186
7.77k
        std::for_each(tokens.begin(), tokens.end(), Util::destructor_fun<Token>());
187
7.77k
        throw;
188
7.77k
  }
189
20.4k
}
190
191
#endif // !ASSIMP_BUILD_NO_FBX_IMPORTER