Coverage Report

Created: 2026-01-07 06:50

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-2025, 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
0
        Object(id, element, name) {
62
0
    const Scope &sc = GetRequiredScope(element);
63
0
    const Element &KeyTime = GetRequiredElement(sc, "KeyTime");
64
0
    const Element &KeyValueFloat = GetRequiredElement(sc, "KeyValueFloat");
65
66
0
    ParseVectorDataArray(keys, KeyTime);
67
0
    ParseVectorDataArray(values, KeyValueFloat);
68
69
0
    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
0
    if (!std::equal(keys.begin(), keys.end() - 1, keys.begin() + 1, std::less<KeyTimeList::value_type>())) {
75
0
        DOMError("the keyframes are not in ascending order", &KeyTime);
76
0
    }
77
78
0
    const Element *KeyAttrDataFloat = sc["KeyAttrDataFloat"];
79
0
    if (KeyAttrDataFloat) {
80
0
        ParseVectorDataArray(attributes, *KeyAttrDataFloat);
81
0
    }
82
83
0
    const Element *KeyAttrFlags = sc["KeyAttrFlags"];
84
0
    if (KeyAttrFlags) {
85
0
        ParseVectorDataArray(flags, *KeyAttrFlags);
86
0
    }
87
0
}
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
0
        Object(id, element, name), target(), doc(doc) {
94
0
    const Scope &sc = GetRequiredScope(element);
95
96
    // find target node
97
0
    const char *whitelist[] = { "Model", "NodeAttribute", "Deformer" };
98
0
    const std::vector<const Connection *> &conns = doc.GetConnectionsBySourceSequenced(ID(), whitelist, 3);
99
100
0
    for (const Connection *con : conns) {
101
102
        // link should go for a property
103
0
        if (!con->PropertyName().length()) {
104
0
            continue;
105
0
        }
106
107
0
        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
0
        const Object *const ob = con->DestinationObject();
123
0
        if (!ob) {
124
0
            DOMWarning("failed to read destination object for AnimationCurveNode->Model link, ignoring", &element);
125
0
            continue;
126
0
        }
127
128
0
        target = ob;
129
0
        if (!target) {
130
0
            continue;
131
0
        }
132
133
0
        prop = con->PropertyName();
134
0
        break;
135
0
    }
136
137
0
    if (!target) {
138
0
        DOMWarning("failed to resolve target Model/NodeAttribute/Constraint for AnimationCurveNode", &element);
139
0
    }
140
141
0
    props = GetPropertyTable(doc, "AnimationCurveNode.FbxAnimCurveNode", element, sc, false);
142
0
}
143
144
// ------------------------------------------------------------------------------------------------
145
0
const AnimationCurveMap &AnimationCurveNode::Curves() const {
146
0
    if (!curves.empty()) {
147
0
        return curves;
148
0
    }
149
150
    // resolve attached animation curves
151
0
    const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "AnimationCurve");
152
153
0
    for (const Connection *con : conns) {
154
155
        // link should go for a property
156
0
        if (!con->PropertyName().length()) {
157
0
            continue;
158
0
        }
159
160
0
        const Object *const ob = con->SourceObject();
161
0
        if (nullptr == ob) {
162
0
            DOMWarning("failed to read source object for AnimationCurve->AnimationCurveNode link, ignoring", &element);
163
0
            continue;
164
0
        }
165
166
0
        const AnimationCurve *const anim = dynamic_cast<const AnimationCurve *>(ob);
167
0
        if (nullptr == anim) {
168
0
            DOMWarning("source object for ->AnimationCurveNode link is not an AnimationCurve", &element);
169
0
            continue;
170
0
        }
171
172
0
        curves[con->PropertyName()] = anim;
173
0
    }
174
175
0
    return curves;
176
0
}
177
178
// ------------------------------------------------------------------------------------------------
179
AnimationLayer::AnimationLayer(uint64_t id, const Element &element, const std::string &name, const Document &doc) :
180
0
        Object(id, element, name), doc(doc) {
181
0
    const Scope &sc = GetRequiredScope(element);
182
183
    // note: the props table here bears little importance and is usually absent
184
0
    props = GetPropertyTable(doc, "AnimationLayer.FbxAnimLayer", element, sc, true);
185
0
}
186
187
// ------------------------------------------------------------------------------------------------
188
AnimationCurveNodeList AnimationLayer::Nodes(const char *const *target_prop_whitelist /*= nullptr*/,
189
0
        size_t whitelist_size /*= 0*/) const {
190
0
    AnimationCurveNodeList nodes;
191
192
    // resolve attached animation nodes
193
0
    const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "AnimationCurveNode");
194
0
    nodes.reserve(conns.size());
195
196
0
    for (const Connection *con : conns) {
197
198
        // link should not go to a property
199
0
        if (con->PropertyName().length()) {
200
0
            continue;
201
0
        }
202
203
0
        const Object *const ob = con->SourceObject();
204
0
        if (!ob) {
205
0
            DOMWarning("failed to read source object for AnimationCurveNode->AnimationLayer link, ignoring", &element);
206
0
            continue;
207
0
        }
208
209
0
        const AnimationCurveNode *const anim = dynamic_cast<const AnimationCurveNode *>(ob);
210
0
        if (!anim) {
211
0
            DOMWarning("source object for ->AnimationLayer link is not an AnimationCurveNode", &element);
212
0
            continue;
213
0
        }
214
215
0
        if (target_prop_whitelist) {
216
0
            const char *s = anim->TargetProperty().c_str();
217
0
            bool ok = false;
218
0
            for (size_t i = 0; i < whitelist_size; ++i) {
219
0
                if (!strcmp(s, target_prop_whitelist[i])) {
220
0
                    ok = true;
221
0
                    break;
222
0
                }
223
0
            }
224
0
            if (!ok) {
225
0
                continue;
226
0
            }
227
0
        }
228
0
        nodes.push_back(anim);
229
0
    }
230
231
0
    return nodes; // pray for NRVO
232
0
}
233
234
// ------------------------------------------------------------------------------------------------
235
AnimationStack::AnimationStack(uint64_t id, const Element &element, const std::string &name, const Document &doc) :
236
0
        Object(id, element, name) {
237
0
    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
0
    props = GetPropertyTable(doc, "AnimationStack.FbxAnimStack", element, sc, true);
241
242
    // resolve attached animation layers
243
0
    const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "AnimationLayer");
244
0
    layers.reserve(conns.size());
245
246
0
    for (const Connection *con : conns) {
247
248
        // link should not go to a property
249
0
        if (con->PropertyName().length()) {
250
0
            continue;
251
0
        }
252
253
0
        const Object *const ob = con->SourceObject();
254
0
        if (!ob) {
255
0
            DOMWarning("failed to read source object for AnimationLayer->AnimationStack link, ignoring", &element);
256
0
            continue;
257
0
        }
258
259
0
        const AnimationLayer *const anim = dynamic_cast<const AnimationLayer *>(ob);
260
0
        if (!anim) {
261
0
            DOMWarning("source object for ->AnimationStack link is not an AnimationLayer", &element);
262
0
            continue;
263
0
        }
264
0
        layers.push_back(anim);
265
0
    }
266
0
}
267
268
} // namespace FBX
269
} // namespace Assimp
270
271
#endif // ASSIMP_BUILD_NO_FBX_IMPORTER