FBXAnimation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**************************************************************************/
  2. /* FBXAnimation.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. /*
  31. Open Asset Import Library (assimp)
  32. ----------------------------------------------------------------------
  33. Copyright (c) 2006-2019, assimp team
  34. All rights reserved.
  35. Redistribution and use of this software in source and binary forms,
  36. with or without modification, are permitted provided that the
  37. following conditions are met:
  38. * Redistributions of source code must retain the above
  39. copyright notice, this list of conditions and the
  40. following disclaimer.
  41. * Redistributions in binary form must reproduce the above
  42. copyright notice, this list of conditions and the
  43. following disclaimer in the documentation and/or other
  44. materials provided with the distribution.
  45. * Neither the name of the assimp team, nor the names of its
  46. contributors may be used to endorse or promote products
  47. derived from this software without specific prior
  48. written permission of the assimp team.
  49. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  50. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  51. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  52. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  53. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  54. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  55. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  56. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  57. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  58. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  59. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. ----------------------------------------------------------------------
  61. */
  62. /** @file FBXAnimation.cpp
  63. * @brief Assimp::FBX::AnimationCurve, Assimp::FBX::AnimationCurveNode,
  64. * Assimp::FBX::AnimationLayer, Assimp::FBX::AnimationStack
  65. */
  66. #include "FBXCommon.h"
  67. #include "FBXDocument.h"
  68. #include "FBXDocumentUtil.h"
  69. #include "FBXParser.h"
  70. namespace FBXDocParser {
  71. using namespace Util;
  72. // ------------------------------------------------------------------------------------------------
  73. AnimationCurve::AnimationCurve(uint64_t id, const ElementPtr element, const std::string &name, const Document & /*doc*/) :
  74. Object(id, element, name) {
  75. const ScopePtr sc = GetRequiredScope(element);
  76. const ElementPtr KeyTime = GetRequiredElement(sc, "KeyTime");
  77. const ElementPtr KeyValueFloat = GetRequiredElement(sc, "KeyValueFloat");
  78. // note preserved keys and values for legacy FBXConverter.cpp
  79. // we can remove this once the animation system is written
  80. // and clean up this code so we do not have copies everywhere.
  81. ParseVectorDataArray(keys, KeyTime);
  82. ParseVectorDataArray(values, KeyValueFloat);
  83. if (keys.size() != values.size()) {
  84. DOMError("the number of key times does not match the number of keyframe values", KeyTime);
  85. }
  86. // put the two lists into the map, underlying container is really just a dictionary
  87. // these will always match, if not an error will throw and the file will not import
  88. // this is useful because we then can report something and fix this later if it becomes an issue
  89. // at this point we do not need a different count of these elements so this makes the
  90. // most sense to do.
  91. for (size_t x = 0; x < keys.size(); x++) {
  92. keyvalues[keys[x]] = values[x];
  93. }
  94. const ElementPtr KeyAttrDataFloat = sc->GetElement("KeyAttrDataFloat");
  95. if (KeyAttrDataFloat) {
  96. ParseVectorDataArray(attributes, KeyAttrDataFloat);
  97. }
  98. const ElementPtr KeyAttrFlags = sc->GetElement("KeyAttrFlags");
  99. if (KeyAttrFlags) {
  100. ParseVectorDataArray(flags, KeyAttrFlags);
  101. }
  102. }
  103. // ------------------------------------------------------------------------------------------------
  104. AnimationCurve::~AnimationCurve() {
  105. // empty
  106. }
  107. // ------------------------------------------------------------------------------------------------
  108. AnimationCurveNode::AnimationCurveNode(uint64_t id, const ElementPtr element, const std::string &name,
  109. const Document &doc, const char *const *target_prop_whitelist /*= NULL*/,
  110. size_t whitelist_size /*= 0*/) :
  111. Object(id, element, name), target(), doc(doc) {
  112. const ScopePtr sc = GetRequiredScope(element);
  113. // find target node
  114. const char *whitelist[] = { "Model", "NodeAttribute", "Deformer" };
  115. const std::vector<const Connection *> &conns = doc.GetConnectionsBySourceSequenced(ID(), whitelist, 3);
  116. for (const Connection *con : conns) {
  117. // link should go for a property
  118. if (!con->PropertyName().length()) {
  119. continue;
  120. }
  121. Object *object = con->DestinationObject();
  122. if (!object) {
  123. DOMWarning("failed to read destination object for AnimationCurveNode->Model link, ignoring", element);
  124. continue;
  125. }
  126. target = object;
  127. prop = con->PropertyName();
  128. break;
  129. }
  130. props = GetPropertyTable(doc, "AnimationCurveNode.FbxAnimCurveNode", element, sc, false);
  131. }
  132. // ------------------------------------------------------------------------------------------------
  133. AnimationCurveNode::~AnimationCurveNode() {
  134. curves.clear();
  135. }
  136. // ------------------------------------------------------------------------------------------------
  137. const AnimationMap &AnimationCurveNode::Curves() const {
  138. /* Lazy loaded animation curves, will only load if required */
  139. if (curves.empty()) {
  140. // resolve attached animation curves
  141. const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "AnimationCurve");
  142. for (const Connection *con : conns) {
  143. // So the advantage of having this STL boilerplate is that it's dead simple once you get it.
  144. // The other advantage is casting is guaranteed to be safe and nullptr will be returned in the last step if it fails.
  145. Object *ob = con->SourceObject();
  146. AnimationCurve *anim_curve = dynamic_cast<AnimationCurve *>(ob);
  147. ERR_CONTINUE_MSG(!anim_curve, "Failed to convert animation curve from object");
  148. curves.insert(std::make_pair(con->PropertyName(), anim_curve));
  149. }
  150. }
  151. return curves;
  152. }
  153. // ------------------------------------------------------------------------------------------------
  154. AnimationLayer::AnimationLayer(uint64_t id, const ElementPtr element, const std::string &name, const Document &doc) :
  155. Object(id, element, name), doc(doc) {
  156. const ScopePtr sc = GetRequiredScope(element);
  157. // note: the props table here bears little importance and is usually absent
  158. props = GetPropertyTable(doc, "AnimationLayer.FbxAnimLayer", element, sc, true);
  159. }
  160. // ------------------------------------------------------------------------------------------------
  161. AnimationLayer::~AnimationLayer() {
  162. // empty
  163. }
  164. // ------------------------------------------------------------------------------------------------
  165. const AnimationCurveNodeList AnimationLayer::Nodes(const char *const *target_prop_whitelist,
  166. size_t whitelist_size /*= 0*/) const {
  167. AnimationCurveNodeList nodes;
  168. // resolve attached animation nodes
  169. const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "AnimationCurveNode");
  170. nodes.reserve(conns.size());
  171. for (const Connection *con : conns) {
  172. // link should not go to a property
  173. if (con->PropertyName().length()) {
  174. continue;
  175. }
  176. Object *ob = con->SourceObject();
  177. if (!ob) {
  178. DOMWarning("failed to read source object for AnimationCurveNode->AnimationLayer link, ignoring", element);
  179. continue;
  180. }
  181. const AnimationCurveNode *anim = dynamic_cast<AnimationCurveNode *>(ob);
  182. if (!anim) {
  183. DOMWarning("source object for ->AnimationLayer link is not an AnimationCurveNode", element);
  184. continue;
  185. }
  186. if (target_prop_whitelist) {
  187. const char *s = anim->TargetProperty().c_str();
  188. bool ok = false;
  189. for (size_t i = 0; i < whitelist_size; ++i) {
  190. if (!strcmp(s, target_prop_whitelist[i])) {
  191. ok = true;
  192. break;
  193. }
  194. }
  195. if (!ok) {
  196. continue;
  197. }
  198. }
  199. nodes.push_back(anim);
  200. }
  201. return nodes;
  202. }
  203. // ------------------------------------------------------------------------------------------------
  204. AnimationStack::AnimationStack(uint64_t id, const ElementPtr element, const std::string &name, const Document &doc) :
  205. Object(id, element, name) {
  206. const ScopePtr sc = GetRequiredScope(element);
  207. // note: we don't currently use any of these properties so we shouldn't bother if it is missing
  208. props = GetPropertyTable(doc, "AnimationStack.FbxAnimStack", element, sc, true);
  209. // resolve attached animation layers
  210. const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "AnimationLayer");
  211. layers.reserve(conns.size());
  212. for (const Connection *con : conns) {
  213. // link should not go to a property
  214. if (con->PropertyName().length()) {
  215. continue;
  216. }
  217. Object *ob = con->SourceObject();
  218. if (!ob) {
  219. DOMWarning("failed to read source object for AnimationLayer->AnimationStack link, ignoring", element);
  220. continue;
  221. }
  222. const AnimationLayer *anim = dynamic_cast<const AnimationLayer *>(ob);
  223. if (!anim) {
  224. DOMWarning("source object for ->AnimationStack link is not an AnimationLayer", element);
  225. continue;
  226. }
  227. layers.push_back(anim);
  228. }
  229. }
  230. // ------------------------------------------------------------------------------------------------
  231. AnimationStack::~AnimationStack() {
  232. if (props != nullptr) {
  233. delete props;
  234. props = nullptr;
  235. }
  236. }
  237. } // namespace FBXDocParser