FBXProperties.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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 FBXProperties.cpp
  34. * @brief Implementation of the FBX dynamic properties system
  35. */
  36. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  37. #include "FBXTokenizer.h"
  38. #include "FBXParser.h"
  39. #include "FBXDocument.h"
  40. #include "FBXDocumentUtil.h"
  41. #include "FBXProperties.h"
  42. #include <utility>
  43. namespace Assimp {
  44. namespace FBX {
  45. using namespace Util;
  46. // ------------------------------------------------------------------------------------------------
  47. namespace {
  48. void checkTokenCount(const TokenList &tok, unsigned int expectedCount) {
  49. ai_assert(expectedCount >= 2);
  50. if (tok.size() < expectedCount) {
  51. const std::string &s = ParseTokenAsString(*tok[1]);
  52. if (tok[1]->IsBinary()) {
  53. throw DeadlyImportError("Not enough tokens for property of type ", s, " at offset ", tok[1]->Offset());
  54. } else {
  55. throw DeadlyImportError("Not enough tokens for property of type ", s, " at line ", tok[1]->Line());
  56. }
  57. }
  58. }
  59. // ------------------------------------------------------------------------------------------------
  60. // read a typed property out of a FBX element. The return value is nullptr if the property cannot be read.
  61. Property* ReadTypedProperty(const Element& element)
  62. {
  63. ai_assert(element.KeyToken().StringContents() == "P");
  64. const TokenList& tok = element.Tokens();
  65. if (tok.size() < 2) {
  66. return nullptr;
  67. }
  68. const std::string& s = ParseTokenAsString(*tok[1]);
  69. const char* const cs = s.c_str();
  70. if (!strcmp(cs,"KString")) {
  71. checkTokenCount(tok, 5);
  72. return new TypedProperty<std::string>(ParseTokenAsString(*tok[4]));
  73. }
  74. else if (!strcmp(cs,"bool") || !strcmp(cs,"Bool")) {
  75. checkTokenCount(tok, 5);
  76. return new TypedProperty<bool>(ParseTokenAsInt(*tok[4]) != 0);
  77. }
  78. else if (!strcmp(cs, "int") || !strcmp(cs, "Int") || !strcmp(cs, "enum") || !strcmp(cs, "Enum") || !strcmp(cs, "Integer")) {
  79. checkTokenCount(tok, 5);
  80. return new TypedProperty<int>(ParseTokenAsInt(*tok[4]));
  81. }
  82. else if (!strcmp(cs, "ULongLong")) {
  83. checkTokenCount(tok, 5);
  84. return new TypedProperty<uint64_t>(ParseTokenAsID(*tok[4]));
  85. }
  86. else if (!strcmp(cs, "KTime")) {
  87. checkTokenCount(tok, 5);
  88. return new TypedProperty<int64_t>(ParseTokenAsInt64(*tok[4]));
  89. }
  90. else if (!strcmp(cs,"Vector3D") ||
  91. !strcmp(cs,"ColorRGB") ||
  92. !strcmp(cs,"Vector") ||
  93. !strcmp(cs,"Color") ||
  94. !strcmp(cs,"Lcl Translation") ||
  95. !strcmp(cs,"Lcl Rotation") ||
  96. !strcmp(cs,"Lcl Scaling")
  97. ) {
  98. checkTokenCount(tok, 7);
  99. return new TypedProperty<aiVector3D>(aiVector3D(
  100. ParseTokenAsFloat(*tok[4]),
  101. ParseTokenAsFloat(*tok[5]),
  102. ParseTokenAsFloat(*tok[6]))
  103. );
  104. }
  105. else if (!strcmp(cs,"double") || !strcmp(cs,"Number") || !strcmp(cs,"float") || !strcmp(cs,"Float") || !strcmp(cs,"FieldOfView") || !strcmp( cs, "UnitScaleFactor" ) ) {
  106. checkTokenCount(tok, 5);
  107. return new TypedProperty<float>(ParseTokenAsFloat(*tok[4]));
  108. }
  109. else if (!strcmp(cs, "ColorAndAlpha")) {
  110. checkTokenCount(tok, 8);
  111. return new TypedProperty<aiColor4D>(aiColor4D(
  112. ParseTokenAsFloat(*tok[4]),
  113. ParseTokenAsFloat(*tok[5]),
  114. ParseTokenAsFloat(*tok[6]),
  115. ParseTokenAsFloat(*tok[7]))
  116. );
  117. }
  118. return nullptr;
  119. }
  120. // ------------------------------------------------------------------------------------------------
  121. // peek into an element and check if it contains a FBX property, if so return its name.
  122. std::string PeekPropertyName(const Element& element) {
  123. ai_assert(element.KeyToken().StringContents() == "P");
  124. const TokenList& tok = element.Tokens();
  125. if(tok.size() < 4) {
  126. return std::string();
  127. }
  128. return ParseTokenAsString(*tok[0]);
  129. }
  130. } //! anon
  131. // ------------------------------------------------------------------------------------------------
  132. PropertyTable::PropertyTable(const Element &element, std::shared_ptr<const PropertyTable> templateProps) :
  133. templateProps(std::move(templateProps)), element(&element) {
  134. const Scope& scope = GetRequiredScope(element);
  135. for(const ElementMap::value_type& v : scope.Elements()) {
  136. if(v.first != "P") {
  137. DOMWarning("expected only P elements in property table",v.second);
  138. continue;
  139. }
  140. const std::string& name = PeekPropertyName(*v.second);
  141. if(!name.length()) {
  142. DOMWarning("could not read property name",v.second);
  143. continue;
  144. }
  145. LazyPropertyMap::const_iterator it = lazyProps.find(name);
  146. if (it != lazyProps.end()) {
  147. DOMWarning("duplicate property name, will hide previous value: " + name,v.second);
  148. continue;
  149. }
  150. lazyProps[name] = v.second;
  151. }
  152. }
  153. // ------------------------------------------------------------------------------------------------
  154. PropertyTable::~PropertyTable()
  155. {
  156. for(PropertyMap::value_type& v : props) {
  157. delete v.second;
  158. }
  159. }
  160. // ------------------------------------------------------------------------------------------------
  161. const Property* PropertyTable::Get(const std::string& name) const
  162. {
  163. PropertyMap::const_iterator it = props.find(name);
  164. if (it == props.end()) {
  165. // hasn't been parsed yet?
  166. LazyPropertyMap::const_iterator lit = lazyProps.find(name);
  167. if(lit != lazyProps.end()) {
  168. props[name] = ReadTypedProperty(*(*lit).second);
  169. it = props.find(name);
  170. ai_assert(it != props.end());
  171. }
  172. if (it == props.end()) {
  173. // check property template
  174. if(templateProps) {
  175. return templateProps->Get(name);
  176. }
  177. return nullptr;
  178. }
  179. }
  180. return (*it).second;
  181. }
  182. DirectPropertyMap PropertyTable::GetUnparsedProperties() const
  183. {
  184. DirectPropertyMap result;
  185. // Loop through all the lazy properties (which is all the properties)
  186. for(const LazyPropertyMap::value_type& currentElement : lazyProps) {
  187. // Skip parsed properties
  188. if (props.end() != props.find(currentElement.first)) {
  189. continue;
  190. }
  191. // Read the element's value.
  192. // Wrap the naked pointer (since the call site is required to acquire ownership)
  193. std::shared_ptr<Property> prop = std::shared_ptr<Property>(ReadTypedProperty(*currentElement.second));
  194. // Element could not be read. Skip it.
  195. if (!prop) {
  196. continue;
  197. }
  198. // Add to result
  199. result[currentElement.first] = prop;
  200. }
  201. return result;
  202. }
  203. } //! FBX
  204. } //! Assimp
  205. #endif