FBXAnimation.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file FBXAnimation.cpp
  34. * @brief Assimp::FBX::AnimationCurve, Assimp::FBX::AnimationCurveNode,
  35. * Assimp::FBX::AnimationLayer, Assimp::FBX::AnimationStack
  36. */
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. #include "FBXDocument.h"
  39. #include "FBXDocumentUtil.h"
  40. #include "FBXImporter.h"
  41. #include "FBXParser.h"
  42. namespace Assimp {
  43. namespace FBX {
  44. using namespace Util;
  45. // ------------------------------------------------------------------------------------------------
  46. AnimationCurve::AnimationCurve(uint64_t id, const Element &element, const std::string &name, const Document & /*doc*/) :
  47. Object(id, element, name) {
  48. const Scope &sc = GetRequiredScope(element);
  49. const Element &KeyTime = GetRequiredElement(sc, "KeyTime");
  50. const Element &KeyValueFloat = GetRequiredElement(sc, "KeyValueFloat");
  51. ParseVectorDataArray(keys, KeyTime);
  52. ParseVectorDataArray(values, KeyValueFloat);
  53. if (keys.size() != values.size()) {
  54. DOMError("the number of key times does not match the number of keyframe values", &KeyTime);
  55. }
  56. // check if the key times are well-ordered
  57. if (!std::equal(keys.begin(), keys.end() - 1, keys.begin() + 1, std::less<KeyTimeList::value_type>())) {
  58. DOMError("the keyframes are not in ascending order", &KeyTime);
  59. }
  60. const Element *KeyAttrDataFloat = sc["KeyAttrDataFloat"];
  61. if (KeyAttrDataFloat) {
  62. ParseVectorDataArray(attributes, *KeyAttrDataFloat);
  63. }
  64. const Element *KeyAttrFlags = sc["KeyAttrFlags"];
  65. if (KeyAttrFlags) {
  66. ParseVectorDataArray(flags, *KeyAttrFlags);
  67. }
  68. }
  69. // ------------------------------------------------------------------------------------------------
  70. AnimationCurve::~AnimationCurve() {
  71. // empty
  72. }
  73. // ------------------------------------------------------------------------------------------------
  74. AnimationCurveNode::AnimationCurveNode(uint64_t id, const Element &element, const std::string &name,
  75. const Document &doc, const char *const *target_prop_whitelist /*= nullptr*/,
  76. size_t whitelist_size /*= 0*/) :
  77. Object(id, element, name), target(), doc(doc) {
  78. const Scope &sc = GetRequiredScope(element);
  79. // find target node
  80. const char *whitelist[] = { "Model", "NodeAttribute", "Deformer" };
  81. const std::vector<const Connection *> &conns = doc.GetConnectionsBySourceSequenced(ID(), whitelist, 3);
  82. for (const Connection *con : conns) {
  83. // link should go for a property
  84. if (!con->PropertyName().length()) {
  85. continue;
  86. }
  87. if (target_prop_whitelist) {
  88. const char *const s = con->PropertyName().c_str();
  89. bool ok = false;
  90. for (size_t i = 0; i < whitelist_size; ++i) {
  91. if (!strcmp(s, target_prop_whitelist[i])) {
  92. ok = true;
  93. break;
  94. }
  95. }
  96. if (!ok) {
  97. throw std::range_error("AnimationCurveNode target property is not in whitelist");
  98. }
  99. }
  100. const Object *const ob = con->DestinationObject();
  101. if (!ob) {
  102. DOMWarning("failed to read destination object for AnimationCurveNode->Model link, ignoring", &element);
  103. continue;
  104. }
  105. target = ob;
  106. if (!target) {
  107. continue;
  108. }
  109. prop = con->PropertyName();
  110. break;
  111. }
  112. if (!target) {
  113. DOMWarning("failed to resolve target Model/NodeAttribute/Constraint for AnimationCurveNode", &element);
  114. }
  115. props = GetPropertyTable(doc, "AnimationCurveNode.FbxAnimCurveNode", element, sc, false);
  116. }
  117. // ------------------------------------------------------------------------------------------------
  118. AnimationCurveNode::~AnimationCurveNode() {
  119. // empty
  120. }
  121. // ------------------------------------------------------------------------------------------------
  122. const AnimationCurveMap &AnimationCurveNode::Curves() const {
  123. if (curves.empty()) {
  124. // resolve attached animation curves
  125. const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "AnimationCurve");
  126. for (const Connection *con : conns) {
  127. // link should go for a property
  128. if (!con->PropertyName().length()) {
  129. continue;
  130. }
  131. const Object *const ob = con->SourceObject();
  132. if (nullptr == ob) {
  133. DOMWarning("failed to read source object for AnimationCurve->AnimationCurveNode link, ignoring", &element);
  134. continue;
  135. }
  136. const AnimationCurve *const anim = dynamic_cast<const AnimationCurve *>(ob);
  137. if (nullptr == anim) {
  138. DOMWarning("source object for ->AnimationCurveNode link is not an AnimationCurve", &element);
  139. continue;
  140. }
  141. curves[con->PropertyName()] = anim;
  142. }
  143. }
  144. return curves;
  145. }
  146. // ------------------------------------------------------------------------------------------------
  147. AnimationLayer::AnimationLayer(uint64_t id, const Element &element, const std::string &name, const Document &doc) :
  148. Object(id, element, name), doc(doc) {
  149. const Scope &sc = GetRequiredScope(element);
  150. // note: the props table here bears little importance and is usually absent
  151. props = GetPropertyTable(doc, "AnimationLayer.FbxAnimLayer", element, sc, true);
  152. }
  153. // ------------------------------------------------------------------------------------------------
  154. AnimationLayer::~AnimationLayer() {
  155. // empty
  156. }
  157. // ------------------------------------------------------------------------------------------------
  158. AnimationCurveNodeList AnimationLayer::Nodes(const char *const *target_prop_whitelist /*= nullptr*/,
  159. size_t whitelist_size /*= 0*/) const {
  160. AnimationCurveNodeList nodes;
  161. // resolve attached animation nodes
  162. const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "AnimationCurveNode");
  163. nodes.reserve(conns.size());
  164. for (const Connection *con : conns) {
  165. // link should not go to a property
  166. if (con->PropertyName().length()) {
  167. continue;
  168. }
  169. const Object *const ob = con->SourceObject();
  170. if (!ob) {
  171. DOMWarning("failed to read source object for AnimationCurveNode->AnimationLayer link, ignoring", &element);
  172. continue;
  173. }
  174. const AnimationCurveNode *const anim = dynamic_cast<const AnimationCurveNode *>(ob);
  175. if (!anim) {
  176. DOMWarning("source object for ->AnimationLayer link is not an AnimationCurveNode", &element);
  177. continue;
  178. }
  179. if (target_prop_whitelist) {
  180. const char *s = anim->TargetProperty().c_str();
  181. bool ok = false;
  182. for (size_t i = 0; i < whitelist_size; ++i) {
  183. if (!strcmp(s, target_prop_whitelist[i])) {
  184. ok = true;
  185. break;
  186. }
  187. }
  188. if (!ok) {
  189. continue;
  190. }
  191. }
  192. nodes.push_back(anim);
  193. }
  194. return nodes; // pray for NRVO
  195. }
  196. // ------------------------------------------------------------------------------------------------
  197. AnimationStack::AnimationStack(uint64_t id, const Element &element, const std::string &name, const Document &doc) :
  198. Object(id, element, name) {
  199. const Scope &sc = GetRequiredScope(element);
  200. // note: we don't currently use any of these properties so we shouldn't bother if it is missing
  201. props = GetPropertyTable(doc, "AnimationStack.FbxAnimStack", element, sc, true);
  202. // resolve attached animation layers
  203. const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "AnimationLayer");
  204. layers.reserve(conns.size());
  205. for (const Connection *con : conns) {
  206. // link should not go to a property
  207. if (con->PropertyName().length()) {
  208. continue;
  209. }
  210. const Object *const ob = con->SourceObject();
  211. if (!ob) {
  212. DOMWarning("failed to read source object for AnimationLayer->AnimationStack link, ignoring", &element);
  213. continue;
  214. }
  215. const AnimationLayer *const anim = dynamic_cast<const AnimationLayer *>(ob);
  216. if (!anim) {
  217. DOMWarning("source object for ->AnimationStack link is not an AnimationLayer", &element);
  218. continue;
  219. }
  220. layers.push_back(anim);
  221. }
  222. }
  223. // ------------------------------------------------------------------------------------------------
  224. AnimationStack::~AnimationStack() {
  225. // empty
  226. }
  227. } // namespace FBX
  228. } // namespace Assimp
  229. #endif // ASSIMP_BUILD_NO_FBX_IMPORTER