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/FBXAnimation.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
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  FBXAnimation.cpp
43
 *  @brief Assimp::FBX::AnimationCurve, Assimp::FBX::AnimationCurveNode,
44
 *         Assimp::FBX::AnimationLayer, Assimp::FBX::AnimationStack
45
 */
46
47
#ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
48
49
#include "FBXDocument.h"
50
#include "FBXDocumentUtil.h"
51
#include "FBXImporter.h"
52
#include "FBXParser.h"
53
54
namespace Assimp {
55
namespace FBX {
56
57
using namespace Util;
58
59
// ------------------------------------------------------------------------------------------------
60
AnimationCurve::AnimationCurve(uint64_t id, const Element &element, const std::string &name, const Document & /*doc*/) :
61
32.0k
        Object(id, element, name) {
62
32.0k
    const Scope &sc = GetRequiredScope(element);
63
32.0k
    const Element &KeyTime = GetRequiredElement(sc, "KeyTime");
64
32.0k
    const Element &KeyValueFloat = GetRequiredElement(sc, "KeyValueFloat");
65
66
32.0k
    ParseVectorDataArray(keys, KeyTime);
67
32.0k
    ParseVectorDataArray(values, KeyValueFloat);
68
69
32.0k
    if (keys.size() != values.size()) {
70
0
        DOMError("the number of key times does not match the number of keyframe values", &KeyTime);
71
0
    }
72
73
    // check if the key times are well-ordered
74
32.0k
    if (!std::equal(keys.begin(), keys.end() - 1, keys.begin() + 1, std::less<KeyTimeList::value_type>())) {
75
159
        DOMError("the keyframes are not in ascending order", &KeyTime);
76
159
    }
77
78
31.9k
    const Element *KeyAttrDataFloat = sc["KeyAttrDataFloat"];
79
31.9k
    if (KeyAttrDataFloat) {
80
31.6k
        ParseVectorDataArray(attributes, *KeyAttrDataFloat);
81
31.6k
    }
82
83
31.9k
    const Element *KeyAttrFlags = sc["KeyAttrFlags"];
84
31.9k
    if (KeyAttrFlags) {
85
31.6k
        ParseVectorDataArray(flags, *KeyAttrFlags);
86
31.6k
    }
87
31.9k
}
88
89
// ------------------------------------------------------------------------------------------------
90
AnimationCurveNode::AnimationCurveNode(uint64_t id, const Element &element, const std::string &name,
91
        const Document &doc, const char *const *target_prop_whitelist /*= nullptr*/,
92
        size_t whitelist_size /*= 0*/) :
93
11.8k
        Object(id, element, name), target(), doc(doc) {
94
11.8k
    const Scope &sc = GetRequiredScope(element);
95
96
    // find target node
97
11.8k
    const char *whitelist[] = { "Model", "NodeAttribute", "Deformer" };
98
11.8k
    const std::vector<const Connection *> &conns = doc.GetConnectionsBySourceSequenced(ID(), whitelist, 3);
99
100
11.8k
    for (const Connection *con : conns) {
101
102
        // link should go for a property
103
11.4k
        if (!con->PropertyName().length()) {
104
11
            continue;
105
11
        }
106
107
11.4k
        if (target_prop_whitelist) {
108
0
            const char *const s = con->PropertyName().c_str();
109
0
            bool ok = false;
110
0
            for (size_t i = 0; i < whitelist_size; ++i) {
111
0
                if (!strcmp(s, target_prop_whitelist[i])) {
112
0
                    ok = true;
113
0
                    break;
114
0
                }
115
0
            }
116
117
0
            if (!ok) {
118
0
                throw std::range_error("AnimationCurveNode target property is not in whitelist");
119
0
            }
120
0
        }
121
122
11.4k
        const Object *const ob = con->DestinationObject();
123
11.4k
        if (!ob) {
124
6
            DOMWarning("failed to read destination object for AnimationCurveNode->Model link, ignoring", &element);
125
6
            continue;
126
6
        }
127
128
11.4k
        target = ob;
129
11.4k
        if (!target) {
130
0
            continue;
131
0
        }
132
133
11.4k
        prop = con->PropertyName();
134
11.4k
        break;
135
11.4k
    }
136
137
11.8k
    if (!target) {
138
445
        DOMWarning("failed to resolve target Model/NodeAttribute/Constraint for AnimationCurveNode", &element);
139
445
    }
140
141
11.8k
    props = GetPropertyTable(doc, "AnimationCurveNode.FbxAnimCurveNode", element, sc, false);
142
11.8k
}
143
144
// ------------------------------------------------------------------------------------------------
145
32.2k
const AnimationCurveMap &AnimationCurveNode::Curves() const {
146
32.2k
    if (!curves.empty()) {
147
21.4k
        return curves;
148
21.4k
    }
149
150
    // resolve attached animation curves
151
10.8k
    const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "AnimationCurve");
152
153
32.0k
    for (const Connection *con : conns) {
154
155
        // link should go for a property
156
32.0k
        if (!con->PropertyName().length()) {
157
11
            continue;
158
11
        }
159
160
32.0k
        const Object *const ob = con->SourceObject();
161
32.0k
        if (nullptr == ob) {
162
361
            DOMWarning("failed to read source object for AnimationCurve->AnimationCurveNode link, ignoring", &element);
163
361
            continue;
164
361
        }
165
166
31.7k
        const AnimationCurve *const anim = dynamic_cast<const AnimationCurve *>(ob);
167
31.7k
        if (nullptr == anim) {
168
0
            DOMWarning("source object for ->AnimationCurveNode link is not an AnimationCurve", &element);
169
0
            continue;
170
0
        }
171
172
31.7k
        curves[con->PropertyName()] = anim;
173
31.7k
    }
174
175
10.8k
    return curves;
176
32.2k
}
177
178
// ------------------------------------------------------------------------------------------------
179
AnimationLayer::AnimationLayer(uint64_t id, const Element &element, const std::string &name, const Document &doc) :
180
279
        Object(id, element, name), doc(doc) {
181
279
    const Scope &sc = GetRequiredScope(element);
182
183
    // note: the props table here bears little importance and is usually absent
184
279
    props = GetPropertyTable(doc, "AnimationLayer.FbxAnimLayer", element, sc, true);
185
279
}
186
187
// ------------------------------------------------------------------------------------------------
188
AnimationCurveNodeList AnimationLayer::Nodes(const char *const *target_prop_whitelist /*= nullptr*/,
189
279
        size_t whitelist_size /*= 0*/) const {
190
279
    AnimationCurveNodeList nodes;
191
192
    // resolve attached animation nodes
193
279
    const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "AnimationCurveNode");
194
279
    nodes.reserve(conns.size());
195
196
11.8k
    for (const Connection *con : conns) {
197
198
        // link should not go to a property
199
11.8k
        if (con->PropertyName().length()) {
200
0
            continue;
201
0
        }
202
203
11.8k
        const Object *const ob = con->SourceObject();
204
11.8k
        if (!ob) {
205
0
            DOMWarning("failed to read source object for AnimationCurveNode->AnimationLayer link, ignoring", &element);
206
0
            continue;
207
0
        }
208
209
11.8k
        const AnimationCurveNode *const anim = dynamic_cast<const AnimationCurveNode *>(ob);
210
11.8k
        if (!anim) {
211
0
            DOMWarning("source object for ->AnimationLayer link is not an AnimationCurveNode", &element);
212
0
            continue;
213
0
        }
214
215
11.8k
        if (target_prop_whitelist) {
216
11.8k
            const char *s = anim->TargetProperty().c_str();
217
11.8k
            bool ok = false;
218
26.0k
            for (size_t i = 0; i < whitelist_size; ++i) {
219
25.2k
                if (!strcmp(s, target_prop_whitelist[i])) {
220
11.1k
                    ok = true;
221
11.1k
                    break;
222
11.1k
                }
223
25.2k
            }
224
11.8k
            if (!ok) {
225
759
                continue;
226
759
            }
227
11.8k
        }
228
11.1k
        nodes.push_back(anim);
229
11.1k
    }
230
231
279
    return nodes; // pray for NRVO
232
279
}
233
234
// ------------------------------------------------------------------------------------------------
235
AnimationStack::AnimationStack(uint64_t id, const Element &element, const std::string &name, const Document &doc) :
236
303
        Object(id, element, name) {
237
303
    const Scope &sc = GetRequiredScope(element);
238
239
    // note: we don't currently use any of these properties so we shouldn't bother if it is missing
240
303
    props = GetPropertyTable(doc, "AnimationStack.FbxAnimStack", element, sc, true);
241
242
    // resolve attached animation layers
243
303
    const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "AnimationLayer");
244
303
    layers.reserve(conns.size());
245
246
303
    for (const Connection *con : conns) {
247
248
        // link should not go to a property
249
279
        if (con->PropertyName().length()) {
250
0
            continue;
251
0
        }
252
253
279
        const Object *const ob = con->SourceObject();
254
279
        if (!ob) {
255
0
            DOMWarning("failed to read source object for AnimationLayer->AnimationStack link, ignoring", &element);
256
0
            continue;
257
0
        }
258
259
279
        const AnimationLayer *const anim = dynamic_cast<const AnimationLayer *>(ob);
260
279
        if (!anim) {
261
0
            DOMWarning("source object for ->AnimationStack link is not an AnimationLayer", &element);
262
0
            continue;
263
0
        }
264
279
        layers.push_back(anim);
265
279
    }
266
303
}
267
268
} // namespace FBX
269
} // namespace Assimp
270
271
#endif // ASSIMP_BUILD_NO_FBX_IMPORTER