FBXDocument.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*************************************************************************/
  2. /* FBXDocument.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 FBXDocument.cpp
  63. * @brief Implementation of the FBX DOM classes
  64. */
  65. #include "FBXDocument.h"
  66. #include "FBXDocumentUtil.h"
  67. #include "FBXImportSettings.h"
  68. #include "FBXMeshGeometry.h"
  69. #include "FBXParser.h"
  70. #include "FBXProperties.h"
  71. #include "FBXUtil.h"
  72. #include <algorithm>
  73. #include <functional>
  74. #include <iostream>
  75. #include <map>
  76. #include <memory>
  77. namespace FBXDocParser {
  78. using namespace Util;
  79. // ------------------------------------------------------------------------------------------------
  80. LazyObject::LazyObject(uint64_t id, const ElementPtr element, const Document &doc) :
  81. doc(doc), element(element), id(id), flags() {
  82. // empty
  83. }
  84. // ------------------------------------------------------------------------------------------------
  85. LazyObject::~LazyObject() {
  86. object.reset();
  87. }
  88. ObjectPtr LazyObject::LoadObject() {
  89. if (IsBeingConstructed() || FailedToConstruct()) {
  90. return nullptr;
  91. }
  92. if (object) {
  93. return object.get();
  94. }
  95. TokenPtr key = element->KeyToken();
  96. ERR_FAIL_COND_V(!key, nullptr);
  97. const TokenList &tokens = element->Tokens();
  98. if (tokens.size() < 3) {
  99. //DOMError("expected at least 3 tokens: id, name and class tag",&element);
  100. return nullptr;
  101. }
  102. const char *err = nullptr;
  103. std::string name = ParseTokenAsString(tokens[1], err);
  104. if (err) {
  105. DOMError(err, element);
  106. }
  107. // small fix for binary reading: binary fbx files don't use
  108. // prefixes such as Model:: in front of their names. The
  109. // loading code expects this at many places, though!
  110. // so convert the binary representation (a 0x0001) to the
  111. // double colon notation.
  112. if (tokens[1]->IsBinary()) {
  113. for (size_t i = 0; i < name.length(); ++i) {
  114. if (name[i] == 0x0 && name[i + 1] == 0x1) {
  115. name = name.substr(i + 2) + "::" + name.substr(0, i);
  116. }
  117. }
  118. }
  119. const std::string classtag = ParseTokenAsString(tokens[2], err);
  120. if (err) {
  121. DOMError(err, element);
  122. }
  123. // prevent recursive calls
  124. flags |= BEING_CONSTRUCTED;
  125. // this needs to be relatively fast since it happens a lot,
  126. // so avoid constructing strings all the time.
  127. const char *obtype = key->begin();
  128. const size_t length = static_cast<size_t>(key->end() - key->begin());
  129. if (!strncmp(obtype, "Pose", length)) {
  130. object.reset(new FbxPose(id, element, doc, name));
  131. } else if (!strncmp(obtype, "Geometry", length)) {
  132. if (!strcmp(classtag.c_str(), "Mesh")) {
  133. object.reset(new MeshGeometry(id, element, name, doc));
  134. }
  135. if (!strcmp(classtag.c_str(), "Shape")) {
  136. object.reset(new ShapeGeometry(id, element, name, doc));
  137. }
  138. if (!strcmp(classtag.c_str(), "Line")) {
  139. object.reset(new LineGeometry(id, element, name, doc));
  140. }
  141. } else if (!strncmp(obtype, "NodeAttribute", length)) {
  142. if (!strcmp(classtag.c_str(), "Camera")) {
  143. object.reset(new Camera(id, element, doc, name));
  144. } else if (!strcmp(classtag.c_str(), "CameraSwitcher")) {
  145. object.reset(new CameraSwitcher(id, element, doc, name));
  146. } else if (!strcmp(classtag.c_str(), "Light")) {
  147. object.reset(new Light(id, element, doc, name));
  148. } else if (!strcmp(classtag.c_str(), "Null")) {
  149. object.reset(new Null(id, element, doc, name));
  150. } else if (!strcmp(classtag.c_str(), "LimbNode")) {
  151. // This is an older format for bones
  152. // this is what blender uses I believe
  153. object.reset(new LimbNode(id, element, doc, name));
  154. }
  155. } else if (!strncmp(obtype, "Constraint", length)) {
  156. object.reset(new Constraint(id, element, doc, name));
  157. } else if (!strncmp(obtype, "Deformer", length)) {
  158. if (!strcmp(classtag.c_str(), "Cluster")) {
  159. object.reset(new Cluster(id, element, doc, name));
  160. } else if (!strcmp(classtag.c_str(), "Skin")) {
  161. object.reset(new Skin(id, element, doc, name));
  162. } else if (!strcmp(classtag.c_str(), "BlendShape")) {
  163. object.reset(new BlendShape(id, element, doc, name));
  164. } else if (!strcmp(classtag.c_str(), "BlendShapeChannel")) {
  165. object.reset(new BlendShapeChannel(id, element, doc, name));
  166. }
  167. } else if (!strncmp(obtype, "Model", length)) {
  168. // Model is normal node
  169. // LimbNode model is a 'bone' node.
  170. if (!strcmp(classtag.c_str(), "LimbNode")) {
  171. object.reset(new ModelLimbNode(id, element, doc, name));
  172. } else if (strcmp(classtag.c_str(), "IKEffector") && strcmp(classtag.c_str(), "FKEffector")) {
  173. // FK and IK effectors are not supporte
  174. object.reset(new Model(id, element, doc, name));
  175. }
  176. } else if (!strncmp(obtype, "Material", length)) {
  177. object.reset(new Material(id, element, doc, name));
  178. } else if (!strncmp(obtype, "Texture", length)) {
  179. object.reset(new Texture(id, element, doc, name));
  180. } else if (!strncmp(obtype, "LayeredTexture", length)) {
  181. object.reset(new LayeredTexture(id, element, doc, name));
  182. } else if (!strncmp(obtype, "Video", length)) {
  183. object.reset(new Video(id, element, doc, name));
  184. } else if (!strncmp(obtype, "AnimationStack", length)) {
  185. object.reset(new AnimationStack(id, element, name, doc));
  186. } else if (!strncmp(obtype, "AnimationLayer", length)) {
  187. object.reset(new AnimationLayer(id, element, name, doc));
  188. } else if (!strncmp(obtype, "AnimationCurve", length)) {
  189. object.reset(new AnimationCurve(id, element, name, doc));
  190. } else if (!strncmp(obtype, "AnimationCurveNode", length)) {
  191. object.reset(new AnimationCurveNode(id, element, name, doc));
  192. } else {
  193. ERR_FAIL_V_MSG(nullptr, "FBX contains unsupported object: " + String(obtype));
  194. }
  195. flags &= ~BEING_CONSTRUCTED;
  196. return object.get();
  197. }
  198. // ------------------------------------------------------------------------------------------------
  199. Object::Object(uint64_t id, const ElementPtr element, const std::string &name) :
  200. element(element), name(name), id(id) {
  201. }
  202. // ------------------------------------------------------------------------------------------------
  203. Object::~Object() {
  204. // empty
  205. }
  206. // ------------------------------------------------------------------------------------------------
  207. FileGlobalSettings::FileGlobalSettings(const Document &doc, const PropertyTable *props) :
  208. props(props), doc(doc) {
  209. // empty
  210. }
  211. // ------------------------------------------------------------------------------------------------
  212. FileGlobalSettings::~FileGlobalSettings() {
  213. if (props != nullptr) {
  214. delete props;
  215. props = nullptr;
  216. }
  217. }
  218. // ------------------------------------------------------------------------------------------------
  219. Document::Document(const Parser &parser, const ImportSettings &settings) :
  220. settings(settings), parser(parser), SafeToImport(false) {
  221. // Cannot use array default initialization syntax because vc8 fails on it
  222. for (unsigned int &timeStamp : creationTimeStamp) {
  223. timeStamp = 0;
  224. }
  225. // we must check if we can read the header version safely, if its outdated then drop it.
  226. if (ReadHeader()) {
  227. SafeToImport = true;
  228. ReadPropertyTemplates();
  229. ReadGlobalSettings();
  230. // This order is important, connections need parsed objects to check
  231. // whether connections are ok or not. Objects may not be evaluated yet,
  232. // though, since this may require valid connections.
  233. ReadObjects();
  234. ReadConnections();
  235. }
  236. }
  237. // ------------------------------------------------------------------------------------------------
  238. Document::~Document() {
  239. for (PropertyTemplateMap::value_type v : templates) {
  240. delete v.second;
  241. }
  242. for (ObjectMap::value_type &v : objects) {
  243. delete v.second;
  244. }
  245. for (ConnectionMap::value_type &v : src_connections) {
  246. delete v.second;
  247. }
  248. if (metadata_properties != nullptr) {
  249. delete metadata_properties;
  250. }
  251. // clear globals import pointer
  252. globals.reset();
  253. }
  254. // ------------------------------------------------------------------------------------------------
  255. static const unsigned int LowerSupportedVersion = 7300;
  256. static const unsigned int UpperSupportedVersion = 7700;
  257. bool Document::ReadHeader() {
  258. // Read ID objects from "Objects" section
  259. ScopePtr sc = parser.GetRootScope();
  260. ElementPtr ehead = sc->GetElement("FBXHeaderExtension");
  261. if (!ehead || !ehead->Compound()) {
  262. DOMError("no FBXHeaderExtension dictionary found");
  263. }
  264. const ScopePtr shead = ehead->Compound();
  265. fbxVersion = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(shead, "FBXVersion", ehead), 0));
  266. // While we may have some success with newer files, we don't support
  267. // the older 6.n fbx format
  268. if (fbxVersion < LowerSupportedVersion) {
  269. DOMWarning("unsupported, old format version, FBX 2015-2020, you must re-export in a more modern version of your original modelling application");
  270. return false;
  271. }
  272. if (fbxVersion > UpperSupportedVersion) {
  273. DOMWarning("unsupported, newer format version, supported are only FBX 2015, up to FBX 2020"
  274. " trying to read it nevertheless");
  275. }
  276. const ElementPtr ecreator = shead->GetElement("Creator");
  277. if (ecreator) {
  278. creator = ParseTokenAsString(GetRequiredToken(ecreator, 0));
  279. }
  280. //
  281. // Scene Info
  282. //
  283. const ElementPtr scene_info = shead->GetElement("SceneInfo");
  284. if (scene_info) {
  285. PropertyTable *fileExportProps = const_cast<PropertyTable *>(GetPropertyTable(*this, "", scene_info, scene_info->Compound(), true));
  286. if (fileExportProps) {
  287. metadata_properties = fileExportProps;
  288. }
  289. }
  290. const ElementPtr etimestamp = shead->GetElement("CreationTimeStamp");
  291. if (etimestamp && etimestamp->Compound()) {
  292. const ScopePtr stimestamp = etimestamp->Compound();
  293. creationTimeStamp[0] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp, "Year"), 0));
  294. creationTimeStamp[1] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp, "Month"), 0));
  295. creationTimeStamp[2] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp, "Day"), 0));
  296. creationTimeStamp[3] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp, "Hour"), 0));
  297. creationTimeStamp[4] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp, "Minute"), 0));
  298. creationTimeStamp[5] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp, "Second"), 0));
  299. creationTimeStamp[6] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp, "Millisecond"), 0));
  300. }
  301. return true;
  302. }
  303. // ------------------------------------------------------------------------------------------------
  304. void Document::ReadGlobalSettings() {
  305. ERR_FAIL_COND_MSG(globals != nullptr, "Global settings is already setup this is a serious error and should be reported");
  306. const ScopePtr sc = parser.GetRootScope();
  307. const ElementPtr ehead = sc->GetElement("GlobalSettings");
  308. if (nullptr == ehead || !ehead->Compound()) {
  309. DOMWarning("no GlobalSettings dictionary found");
  310. globals = std::make_shared<FileGlobalSettings>(*this, new PropertyTable());
  311. return;
  312. }
  313. const PropertyTable *props = GetPropertyTable(*this, "", ehead, ehead->Compound(), true);
  314. //double v = PropertyGet<float>( *props, std::string("UnitScaleFactor"), 1.0 );
  315. if (!props) {
  316. DOMError("GlobalSettings dictionary contains no property table");
  317. }
  318. globals = std::make_shared<FileGlobalSettings>(*this, props);
  319. }
  320. // ------------------------------------------------------------------------------------------------
  321. void Document::ReadObjects() {
  322. // read ID objects from "Objects" section
  323. const ScopePtr sc = parser.GetRootScope();
  324. const ElementPtr eobjects = sc->GetElement("Objects");
  325. if (!eobjects || !eobjects->Compound()) {
  326. DOMError("no Objects dictionary found");
  327. }
  328. // add a dummy entry to represent the Model::RootNode object (id 0),
  329. // which is only indirectly defined in the input file
  330. objects[0] = new LazyObject(0L, eobjects, *this);
  331. const ScopePtr sobjects = eobjects->Compound();
  332. for (const ElementMap::value_type &iter : sobjects->Elements()) {
  333. // extract ID
  334. const TokenList &tok = iter.second->Tokens();
  335. if (tok.empty()) {
  336. DOMError("expected ID after object key", iter.second);
  337. }
  338. const char *err;
  339. const uint64_t id = ParseTokenAsID(tok[0], err);
  340. if (err) {
  341. DOMError(err, iter.second);
  342. }
  343. // id=0 is normally implicit
  344. if (id == 0L) {
  345. DOMError("encountered object with implicitly defined id 0", iter.second);
  346. }
  347. if (objects.find(id) != objects.end()) {
  348. DOMWarning("encountered duplicate object id, ignoring first occurrence", iter.second);
  349. }
  350. objects[id] = new LazyObject(id, iter.second, *this);
  351. // grab all animation stacks upfront since there is no listing of them
  352. if (!strcmp(iter.first.c_str(), "AnimationStack")) {
  353. animationStacks.push_back(id);
  354. } else if (!strcmp(iter.first.c_str(), "Constraint")) {
  355. constraints.push_back(id);
  356. } else if (!strcmp(iter.first.c_str(), "Pose")) {
  357. bind_poses.push_back(id);
  358. } else if (!strcmp(iter.first.c_str(), "Material")) {
  359. materials.push_back(id);
  360. } else if (!strcmp(iter.first.c_str(), "Deformer")) {
  361. TokenPtr key = iter.second->KeyToken();
  362. ERR_CONTINUE_MSG(!key, "[parser bug] invalid token key for deformer");
  363. const TokenList &tokens = iter.second->Tokens();
  364. const std::string class_tag = ParseTokenAsString(tokens[2], err);
  365. if (err) {
  366. DOMError(err, iter.second);
  367. }
  368. if (class_tag == "Skin") {
  369. //print_verbose("registered skin:" + itos(id));
  370. skins.push_back(id);
  371. }
  372. }
  373. }
  374. }
  375. // ------------------------------------------------------------------------------------------------
  376. void Document::ReadPropertyTemplates() {
  377. const ScopePtr sc = parser.GetRootScope();
  378. // read property templates from "Definitions" section
  379. const ElementPtr edefs = sc->GetElement("Definitions");
  380. if (!edefs || !edefs->Compound()) {
  381. DOMWarning("no Definitions dictionary found");
  382. return;
  383. }
  384. const ScopePtr sdefs = edefs->Compound();
  385. const ElementCollection otypes = sdefs->GetCollection("ObjectType");
  386. for (ElementMap::const_iterator it = otypes.first; it != otypes.second; ++it) {
  387. const ElementPtr el = (*it).second;
  388. const ScopePtr sc_2 = el->Compound();
  389. if (!sc_2) {
  390. DOMWarning("expected nested scope in ObjectType, ignoring", el);
  391. continue;
  392. }
  393. const TokenList &tok = el->Tokens();
  394. if (tok.empty()) {
  395. DOMWarning("expected name for ObjectType element, ignoring", el);
  396. continue;
  397. }
  398. const std::string &oname = ParseTokenAsString(tok[0]);
  399. const ElementCollection templs = sc_2->GetCollection("PropertyTemplate");
  400. for (ElementMap::const_iterator iter = templs.first; iter != templs.second; ++iter) {
  401. const ElementPtr el_2 = (*iter).second;
  402. const ScopePtr sc_3 = el_2->Compound();
  403. if (!sc_3) {
  404. DOMWarning("expected nested scope in PropertyTemplate, ignoring", el);
  405. continue;
  406. }
  407. const TokenList &tok_2 = el_2->Tokens();
  408. if (tok_2.empty()) {
  409. DOMWarning("expected name for PropertyTemplate element, ignoring", el);
  410. continue;
  411. }
  412. const std::string &pname = ParseTokenAsString(tok_2[0]);
  413. const ElementPtr Properties70 = sc_3->GetElement("Properties70");
  414. if (Properties70) {
  415. // PropertyTable(const ElementPtr element, const PropertyTable* templateProps);
  416. const PropertyTable *props = new PropertyTable(Properties70, nullptr);
  417. templates[oname + "." + pname] = props;
  418. }
  419. }
  420. }
  421. }
  422. // ------------------------------------------------------------------------------------------------
  423. void Document::ReadConnections() {
  424. const ScopePtr sc = parser.GetRootScope();
  425. // read property templates from "Definitions" section
  426. const ElementPtr econns = sc->GetElement("Connections");
  427. if (!econns || !econns->Compound()) {
  428. DOMError("no Connections dictionary found");
  429. }
  430. uint64_t insertionOrder = 0l;
  431. const ScopePtr sconns = econns->Compound();
  432. const ElementCollection conns = sconns->GetCollection("C");
  433. for (ElementMap::const_iterator it = conns.first; it != conns.second; ++it) {
  434. const ElementPtr el = (*it).second;
  435. const std::string &type = ParseTokenAsString(GetRequiredToken(el, 0));
  436. // PP = property-property connection, ignored for now
  437. // (tokens: "PP", ID1, "Property1", ID2, "Property2")
  438. if (type == "PP") {
  439. continue;
  440. }
  441. const uint64_t src = ParseTokenAsID(GetRequiredToken(el, 1));
  442. const uint64_t dest = ParseTokenAsID(GetRequiredToken(el, 2));
  443. // OO = object-object connection
  444. // OP = object-property connection, in which case the destination property follows the object ID
  445. const std::string &prop = (type == "OP" ? ParseTokenAsString(GetRequiredToken(el, 3)) : "");
  446. if (objects.find(src) == objects.end()) {
  447. DOMWarning("source object for connection does not exist", el);
  448. continue;
  449. }
  450. // dest may be 0 (root node) but we added a dummy object before
  451. if (objects.find(dest) == objects.end()) {
  452. DOMWarning("destination object for connection does not exist", el);
  453. continue;
  454. }
  455. // add new connection
  456. const Connection *const c = new Connection(insertionOrder++, src, dest, prop, *this);
  457. src_connections.insert(ConnectionMap::value_type(src, c));
  458. dest_connections.insert(ConnectionMap::value_type(dest, c));
  459. }
  460. }
  461. // ------------------------------------------------------------------------------------------------
  462. const std::vector<const AnimationStack *> &Document::AnimationStacks() const {
  463. if (!animationStacksResolved.empty() || animationStacks.empty()) {
  464. return animationStacksResolved;
  465. }
  466. animationStacksResolved.reserve(animationStacks.size());
  467. for (uint64_t id : animationStacks) {
  468. LazyObject *lazy = GetObject(id);
  469. // Two things happen here:
  470. // We cast internally an Object PTR to an Animation Stack PTR
  471. // We return invalid weak_ptrs for objects which are invalid
  472. const AnimationStack *stack = lazy->Get<AnimationStack>();
  473. ERR_CONTINUE_MSG(!stack, "invalid ptr to AnimationStack - conversion failure");
  474. // We push back the weak reference :) to keep things simple, as ownership is on the parser side so it wont be cleaned up.
  475. animationStacksResolved.push_back(stack);
  476. }
  477. return animationStacksResolved;
  478. }
  479. // ------------------------------------------------------------------------------------------------
  480. LazyObject *Document::GetObject(uint64_t id) const {
  481. ObjectMap::const_iterator it = objects.find(id);
  482. return it == objects.end() ? nullptr : (*it).second;
  483. }
  484. #define MAX_CLASSNAMES 6
  485. // ------------------------------------------------------------------------------------------------
  486. std::vector<const Connection *> Document::GetConnectionsSequenced(uint64_t id, const ConnectionMap &conns) const {
  487. std::vector<const Connection *> temp;
  488. const std::pair<ConnectionMap::const_iterator, ConnectionMap::const_iterator> range =
  489. conns.equal_range(id);
  490. temp.reserve(std::distance(range.first, range.second));
  491. for (ConnectionMap::const_iterator it = range.first; it != range.second; ++it) {
  492. temp.push_back((*it).second);
  493. }
  494. std::sort(temp.begin(), temp.end(), std::mem_fn(&Connection::Compare));
  495. return temp; // NRVO should handle this
  496. }
  497. // ------------------------------------------------------------------------------------------------
  498. std::vector<const Connection *> Document::GetConnectionsSequenced(uint64_t id, bool is_src,
  499. const ConnectionMap &conns,
  500. const char *const *classnames,
  501. size_t count) const
  502. {
  503. size_t lengths[MAX_CLASSNAMES];
  504. const size_t c = count;
  505. for (size_t i = 0; i < c; ++i) {
  506. lengths[i] = strlen(classnames[i]);
  507. }
  508. std::vector<const Connection *> temp;
  509. const std::pair<ConnectionMap::const_iterator, ConnectionMap::const_iterator> range =
  510. conns.equal_range(id);
  511. temp.reserve(std::distance(range.first, range.second));
  512. for (ConnectionMap::const_iterator it = range.first; it != range.second; ++it) {
  513. TokenPtr key = (is_src ? (*it).second->LazyDestinationObject() : (*it).second->LazySourceObject())->GetElement()->KeyToken();
  514. const char *obtype = key->begin();
  515. for (size_t i = 0; i < c; ++i) {
  516. //ai_assert(classnames[i]);
  517. if (static_cast<size_t>(std::distance(key->begin(), key->end())) == lengths[i] && !strncmp(classnames[i], obtype, lengths[i])) {
  518. obtype = nullptr;
  519. break;
  520. }
  521. }
  522. if (obtype) {
  523. continue;
  524. }
  525. temp.push_back((*it).second);
  526. }
  527. std::sort(temp.begin(), temp.end(), std::mem_fn(&Connection::Compare));
  528. return temp; // NRVO should handle this
  529. }
  530. // ------------------------------------------------------------------------------------------------
  531. std::vector<const Connection *> Document::GetConnectionsBySourceSequenced(uint64_t source) const {
  532. return GetConnectionsSequenced(source, ConnectionsBySource());
  533. }
  534. // ------------------------------------------------------------------------------------------------
  535. std::vector<const Connection *> Document::GetConnectionsBySourceSequenced(uint64_t src, const char *classname) const {
  536. const char *arr[] = { classname };
  537. return GetConnectionsBySourceSequenced(src, arr, 1);
  538. }
  539. // ------------------------------------------------------------------------------------------------
  540. std::vector<const Connection *> Document::GetConnectionsBySourceSequenced(uint64_t source,
  541. const char *const *classnames, size_t count) const {
  542. return GetConnectionsSequenced(source, true, ConnectionsBySource(), classnames, count);
  543. }
  544. // ------------------------------------------------------------------------------------------------
  545. std::vector<const Connection *> Document::GetConnectionsByDestinationSequenced(uint64_t dest,
  546. const char *classname) const {
  547. const char *arr[] = { classname };
  548. return GetConnectionsByDestinationSequenced(dest, arr, 1);
  549. }
  550. // ------------------------------------------------------------------------------------------------
  551. std::vector<const Connection *> Document::GetConnectionsByDestinationSequenced(uint64_t dest) const {
  552. return GetConnectionsSequenced(dest, ConnectionsByDestination());
  553. }
  554. // ------------------------------------------------------------------------------------------------
  555. std::vector<const Connection *> Document::GetConnectionsByDestinationSequenced(uint64_t dest,
  556. const char *const *classnames, size_t count) const {
  557. return GetConnectionsSequenced(dest, false, ConnectionsByDestination(), classnames, count);
  558. }
  559. // ------------------------------------------------------------------------------------------------
  560. Connection::Connection(uint64_t insertionOrder, uint64_t src, uint64_t dest, const std::string &prop,
  561. const Document &doc) :
  562. insertionOrder(insertionOrder), prop(prop), src(src), dest(dest), doc(doc) {
  563. }
  564. // ------------------------------------------------------------------------------------------------
  565. Connection::~Connection() {
  566. // empty
  567. }
  568. // ------------------------------------------------------------------------------------------------
  569. LazyObject *Connection::LazySourceObject() const {
  570. LazyObject *const lazy = doc.GetObject(src);
  571. return lazy;
  572. }
  573. // ------------------------------------------------------------------------------------------------
  574. LazyObject *Connection::LazyDestinationObject() const {
  575. LazyObject *const lazy = doc.GetObject(dest);
  576. return lazy;
  577. }
  578. // ------------------------------------------------------------------------------------------------
  579. Object *Connection::SourceObject() const {
  580. LazyObject *lazy = doc.GetObject(src);
  581. //ai_assert(lazy);
  582. return lazy->LoadObject();
  583. }
  584. // ------------------------------------------------------------------------------------------------
  585. Object *Connection::DestinationObject() const {
  586. LazyObject *lazy = doc.GetObject(dest);
  587. //ai_assert(lazy);
  588. return lazy->LoadObject();
  589. }
  590. } // namespace FBXDocParser