Coverage Report

Created: 2026-05-04 07:03

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