FBXExportNode.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2024, 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. #ifndef ASSIMP_BUILD_NO_EXPORT
  34. #ifndef ASSIMP_BUILD_NO_FBX_EXPORTER
  35. #include "FBXExportNode.h"
  36. #include "FBXCommon.h"
  37. #include <assimp/StreamWriter.h> // StreamWriterLE
  38. #include <assimp/Exceptional.h> // DeadlyExportError
  39. #include <assimp/ai_assert.h>
  40. #include <assimp/StringUtils.h> // ai_snprintf
  41. #include <string>
  42. #include <ostream>
  43. #include <sstream> // ostringstream
  44. #include <memory> // shared_ptr
  45. namespace Assimp {
  46. // AddP70<type> helpers... there's no usable pattern here,
  47. // so all are defined as separate functions.
  48. // Even "animatable" properties are often completely different
  49. // from the standard (nonanimated) property definition,
  50. // so they are specified with an 'A' suffix.
  51. void FBX::Node::AddP70int(
  52. const std::string& cur_name, int32_t value
  53. ) {
  54. FBX::Node n("P");
  55. n.AddProperties(cur_name, "int", "Integer", "", value);
  56. AddChild(n);
  57. }
  58. void FBX::Node::AddP70bool(
  59. const std::string& cur_name, bool value
  60. ) {
  61. FBX::Node n("P");
  62. n.AddProperties(cur_name, "bool", "", "", int32_t(value));
  63. AddChild(n);
  64. }
  65. void FBX::Node::AddP70double(
  66. const std::string &cur_name, double value) {
  67. FBX::Node n("P");
  68. n.AddProperties(cur_name, "double", "Number", "", value);
  69. AddChild(n);
  70. }
  71. void FBX::Node::AddP70numberA(
  72. const std::string &cur_name, double value) {
  73. FBX::Node n("P");
  74. n.AddProperties(cur_name, "Number", "", "A", value);
  75. AddChild(n);
  76. }
  77. void FBX::Node::AddP70color(
  78. const std::string &cur_name, double r, double g, double b) {
  79. FBX::Node n("P");
  80. n.AddProperties(cur_name, "ColorRGB", "Color", "", r, g, b);
  81. AddChild(n);
  82. }
  83. void FBX::Node::AddP70colorA(
  84. const std::string &cur_name, double r, double g, double b) {
  85. FBX::Node n("P");
  86. n.AddProperties(cur_name, "Color", "", "A", r, g, b);
  87. AddChild(n);
  88. }
  89. void FBX::Node::AddP70vector(
  90. const std::string &cur_name, double x, double y, double z) {
  91. FBX::Node n("P");
  92. n.AddProperties(cur_name, "Vector3D", "Vector", "", x, y, z);
  93. AddChild(n);
  94. }
  95. void FBX::Node::AddP70vectorA(
  96. const std::string &cur_name, double x, double y, double z) {
  97. FBX::Node n("P");
  98. n.AddProperties(cur_name, "Vector", "", "A", x, y, z);
  99. AddChild(n);
  100. }
  101. void FBX::Node::AddP70string(
  102. const std::string &cur_name, const std::string &value) {
  103. FBX::Node n("P");
  104. n.AddProperties(cur_name, "KString", "", "", value);
  105. AddChild(n);
  106. }
  107. void FBX::Node::AddP70enum(
  108. const std::string &cur_name, int32_t value) {
  109. FBX::Node n("P");
  110. n.AddProperties(cur_name, "enum", "", "", value);
  111. AddChild(n);
  112. }
  113. void FBX::Node::AddP70time(
  114. const std::string &cur_name, int64_t value) {
  115. FBX::Node n("P");
  116. n.AddProperties(cur_name, "KTime", "Time", "", value);
  117. AddChild(n);
  118. }
  119. // public member functions for writing nodes to stream
  120. void FBX::Node::Dump(
  121. const std::shared_ptr<Assimp::IOStream> &outfile,
  122. bool binary, int indent) {
  123. if (binary) {
  124. Assimp::StreamWriterLE outstream(outfile);
  125. DumpBinary(outstream);
  126. } else {
  127. std::ostringstream ss;
  128. DumpAscii(ss, indent);
  129. std::string s = ss.str();
  130. outfile->Write(s.c_str(), s.size(), 1);
  131. }
  132. }
  133. void FBX::Node::Dump(
  134. Assimp::StreamWriterLE &outstream,
  135. bool binary, int indent
  136. ) {
  137. if (binary) {
  138. DumpBinary(outstream);
  139. } else {
  140. std::ostringstream ss;
  141. DumpAscii(ss, indent);
  142. outstream.PutString(ss.str());
  143. }
  144. }
  145. // public member functions for low-level writing
  146. void FBX::Node::Begin(
  147. Assimp::StreamWriterLE &s,
  148. bool binary, int indent
  149. ) {
  150. if (binary) {
  151. BeginBinary(s);
  152. } else {
  153. // assume we're at the correct place to start already
  154. (void)indent;
  155. std::ostringstream ss;
  156. BeginAscii(ss, indent);
  157. s.PutString(ss.str());
  158. }
  159. }
  160. void FBX::Node::DumpProperties(
  161. Assimp::StreamWriterLE& s,
  162. bool binary, int indent
  163. ) {
  164. if (binary) {
  165. DumpPropertiesBinary(s);
  166. } else {
  167. std::ostringstream ss;
  168. DumpPropertiesAscii(ss, indent);
  169. s.PutString(ss.str());
  170. }
  171. }
  172. void FBX::Node::EndProperties(
  173. Assimp::StreamWriterLE &s,
  174. bool binary, int indent
  175. ) {
  176. EndProperties(s, binary, indent, properties.size());
  177. }
  178. void FBX::Node::EndProperties(
  179. Assimp::StreamWriterLE &s,
  180. bool binary, int indent,
  181. size_t num_properties
  182. ) {
  183. if (binary) {
  184. EndPropertiesBinary(s, num_properties);
  185. } else {
  186. // nothing to do
  187. (void)indent;
  188. }
  189. }
  190. void FBX::Node::BeginChildren(
  191. Assimp::StreamWriterLE &s,
  192. bool binary, int indent
  193. ) {
  194. if (binary) {
  195. // nothing to do
  196. } else {
  197. std::ostringstream ss;
  198. BeginChildrenAscii(ss, indent);
  199. s.PutString(ss.str());
  200. }
  201. }
  202. void FBX::Node::DumpChildren(
  203. Assimp::StreamWriterLE& s,
  204. bool binary, int indent
  205. ) {
  206. if (binary) {
  207. DumpChildrenBinary(s);
  208. } else {
  209. std::ostringstream ss;
  210. DumpChildrenAscii(ss, indent);
  211. if (ss.tellp() > 0)
  212. s.PutString(ss.str());
  213. }
  214. }
  215. void FBX::Node::End(
  216. Assimp::StreamWriterLE &s,
  217. bool binary, int indent,
  218. bool has_children
  219. ) {
  220. if (binary) {
  221. EndBinary(s, has_children);
  222. } else {
  223. std::ostringstream ss;
  224. EndAscii(ss, indent, has_children);
  225. if (ss.tellp() > 0)
  226. s.PutString(ss.str());
  227. }
  228. }
  229. // public member functions for writing to binary fbx
  230. void FBX::Node::DumpBinary(Assimp::StreamWriterLE &s)
  231. {
  232. // write header section (with placeholders for some things)
  233. BeginBinary(s);
  234. // write properties
  235. DumpPropertiesBinary(s);
  236. // go back and fill in property related placeholders
  237. EndPropertiesBinary(s, properties.size());
  238. // write children
  239. DumpChildrenBinary(s);
  240. // finish, filling in end offset placeholder
  241. EndBinary(s, force_has_children || !children.empty());
  242. }
  243. // public member functions for writing to ascii fbx
  244. void FBX::Node::DumpAscii(std::ostream &s, int indent)
  245. {
  246. // write name
  247. BeginAscii(s, indent);
  248. // write properties
  249. DumpPropertiesAscii(s, indent);
  250. if (force_has_children || !children.empty()) {
  251. // begin children (with a '{')
  252. BeginChildrenAscii(s, indent + 1);
  253. // write children
  254. DumpChildrenAscii(s, indent + 1);
  255. }
  256. // finish (also closing the children bracket '}')
  257. EndAscii(s, indent, force_has_children || !children.empty());
  258. }
  259. // private member functions for low-level writing to fbx
  260. void FBX::Node::BeginBinary(Assimp::StreamWriterLE &s)
  261. {
  262. // remember start pos so we can come back and write the end pos
  263. this->start_pos = s.Tell();
  264. // placeholders for end pos and property section info
  265. s.PutU8(0); // end pos
  266. s.PutU8(0); // number of properties
  267. s.PutU8(0); // total property section length
  268. // node name
  269. s.PutU1(uint8_t(name.size())); // length of node name
  270. s.PutString(name); // node name as raw bytes
  271. // property data comes after here
  272. this->property_start = s.Tell();
  273. }
  274. void FBX::Node::DumpPropertiesBinary(Assimp::StreamWriterLE& s)
  275. {
  276. for (auto &p : properties) {
  277. p.DumpBinary(s);
  278. }
  279. }
  280. void FBX::Node::EndPropertiesBinary(
  281. Assimp::StreamWriterLE &s,
  282. size_t num_properties
  283. ) {
  284. if (num_properties == 0) { return; }
  285. size_t pos = s.Tell();
  286. ai_assert(pos > property_start);
  287. size_t property_section_size = pos - property_start;
  288. s.Seek(start_pos + 8); // 8 bytes of uint64_t of end_pos
  289. s.PutU8(num_properties);
  290. s.PutU8(property_section_size);
  291. s.Seek(pos);
  292. }
  293. void FBX::Node::DumpChildrenBinary(Assimp::StreamWriterLE& s)
  294. {
  295. for (FBX::Node& child : children) {
  296. child.DumpBinary(s);
  297. }
  298. }
  299. void FBX::Node::EndBinary(
  300. Assimp::StreamWriterLE &s,
  301. bool has_children
  302. ) {
  303. // if there were children, add a null record
  304. if (has_children) { s.PutString(Assimp::FBX::NULL_RECORD_STRING); }
  305. // now go back and write initial pos
  306. this->end_pos = s.Tell();
  307. s.Seek(start_pos);
  308. s.PutU8(end_pos);
  309. s.Seek(end_pos);
  310. }
  311. void FBX::Node::BeginAscii(std::ostream& s, int indent)
  312. {
  313. s << '\n';
  314. for (int i = 0; i < indent; ++i) { s << '\t'; }
  315. s << name << ": ";
  316. }
  317. void FBX::Node::DumpPropertiesAscii(std::ostream &s, int indent)
  318. {
  319. for (size_t i = 0; i < properties.size(); ++i) {
  320. if (i > 0) { s << ", "; }
  321. properties[i].DumpAscii(s, indent);
  322. }
  323. }
  324. void FBX::Node::BeginChildrenAscii(std::ostream& s, int indent)
  325. {
  326. // only call this if there are actually children
  327. s << " {";
  328. (void)indent;
  329. }
  330. void FBX::Node::DumpChildrenAscii(std::ostream& s, int indent)
  331. {
  332. // children will need a lot of padding and corralling
  333. if (children.size() || force_has_children) {
  334. for (size_t i = 0; i < children.size(); ++i) {
  335. // no compression in ascii files, so skip this node if it exists
  336. if (children[i].name == "EncryptionType") { continue; }
  337. // the child can dump itself
  338. children[i].DumpAscii(s, indent);
  339. }
  340. }
  341. }
  342. void FBX::Node::EndAscii(std::ostream& s, int indent, bool has_children)
  343. {
  344. if (!has_children) { return; } // nothing to do
  345. s << '\n';
  346. for (int i = 0; i < indent; ++i) { s << '\t'; }
  347. s << "}";
  348. }
  349. // private helpers for static member functions
  350. // ascii property node from vector of doubles
  351. void FBX::Node::WritePropertyNodeAscii(
  352. const std::string& name,
  353. const std::vector<double>& v,
  354. Assimp::StreamWriterLE& s,
  355. int indent
  356. ){
  357. char buffer[32];
  358. FBX::Node node(name);
  359. node.Begin(s, false, indent);
  360. std::string vsize = ai_to_string(v.size());
  361. // *<size> {
  362. s.PutChar('*'); s.PutString(vsize); s.PutString(" {\n");
  363. // indent + 1
  364. for (int i = 0; i < indent + 1; ++i) { s.PutChar('\t'); }
  365. // a: value,value,value,...
  366. s.PutString("a: ");
  367. int count = 0;
  368. for (size_t i = 0; i < v.size(); ++i) {
  369. if (i > 0) { s.PutChar(','); }
  370. int len = ai_snprintf(buffer, sizeof(buffer), "%f", v[i]);
  371. count += len;
  372. if (count > 2048) { s.PutChar('\n'); count = 0; }
  373. if (len < 0 || len > 31) {
  374. // this should never happen
  375. throw DeadlyExportError("failed to convert double to string");
  376. }
  377. for (int j = 0; j < len; ++j) { s.PutChar(buffer[j]); }
  378. }
  379. // }
  380. s.PutChar('\n');
  381. for (int i = 0; i < indent; ++i) { s.PutChar('\t'); }
  382. s.PutChar('}'); s.PutChar(' ');
  383. node.End(s, false, indent, false);
  384. }
  385. // ascii property node from vector of int32_t
  386. void FBX::Node::WritePropertyNodeAscii(
  387. const std::string& name,
  388. const std::vector<int32_t>& v,
  389. Assimp::StreamWriterLE& s,
  390. int indent
  391. ){
  392. char buffer[32];
  393. FBX::Node node(name);
  394. node.Begin(s, false, indent);
  395. std::string vsize = ai_to_string(v.size());
  396. // *<size> {
  397. s.PutChar('*'); s.PutString(vsize); s.PutString(" {\n");
  398. // indent + 1
  399. for (int i = 0; i < indent + 1; ++i) { s.PutChar('\t'); }
  400. // a: value,value,value,...
  401. s.PutString("a: ");
  402. int count = 0;
  403. for (size_t i = 0; i < v.size(); ++i) {
  404. if (i > 0) { s.PutChar(','); }
  405. int len = ai_snprintf(buffer, sizeof(buffer), "%d", v[i]);
  406. count += len;
  407. if (count > 2048) { s.PutChar('\n'); count = 0; }
  408. if (len < 0 || len > 31) {
  409. // this should never happen
  410. throw DeadlyExportError("failed to convert double to string");
  411. }
  412. for (int j = 0; j < len; ++j) { s.PutChar(buffer[j]); }
  413. }
  414. // }
  415. s.PutChar('\n');
  416. for (int i = 0; i < indent; ++i) { s.PutChar('\t'); }
  417. s.PutChar('}'); s.PutChar(' ');
  418. node.End(s, false, indent, false);
  419. }
  420. // binary property node from vector of doubles
  421. // TODO: optional zip compression!
  422. void FBX::Node::WritePropertyNodeBinary(
  423. const std::string& name,
  424. const std::vector<double>& v,
  425. Assimp::StreamWriterLE& s
  426. ){
  427. FBX::Node node(name);
  428. node.BeginBinary(s);
  429. s.PutU1('d');
  430. s.PutU4(uint32_t(v.size())); // number of elements
  431. s.PutU4(0); // no encoding (1 would be zip-compressed)
  432. s.PutU4(uint32_t(v.size()) * 8); // data size
  433. for (auto it = v.begin(); it != v.end(); ++it) { s.PutF8(*it); }
  434. node.EndPropertiesBinary(s, 1);
  435. node.EndBinary(s, false);
  436. }
  437. // binary property node from vector of int32_t
  438. // TODO: optional zip compression!
  439. void FBX::Node::WritePropertyNodeBinary(
  440. const std::string& name,
  441. const std::vector<int32_t>& v,
  442. Assimp::StreamWriterLE& s
  443. ){
  444. FBX::Node node(name);
  445. node.BeginBinary(s);
  446. s.PutU1('i');
  447. s.PutU4(uint32_t(v.size())); // number of elements
  448. s.PutU4(0); // no encoding (1 would be zip-compressed)
  449. s.PutU4(uint32_t(v.size()) * 4); // data size
  450. for (auto it = v.begin(); it != v.end(); ++it) { s.PutI4(*it); }
  451. node.EndPropertiesBinary(s, 1);
  452. node.EndBinary(s, false);
  453. }
  454. // public static member functions
  455. // convenience function to create and write a property node,
  456. // holding a single property which is an array of values.
  457. // does not copy the data, so is efficient for large arrays.
  458. void FBX::Node::WritePropertyNode(
  459. const std::string& name,
  460. const std::vector<double>& v,
  461. Assimp::StreamWriterLE& s,
  462. bool binary, int indent
  463. ){
  464. if (binary) {
  465. FBX::Node::WritePropertyNodeBinary(name, v, s);
  466. } else {
  467. FBX::Node::WritePropertyNodeAscii(name, v, s, indent);
  468. }
  469. }
  470. // convenience function to create and write a property node,
  471. // holding a single property which is an array of values.
  472. // does not copy the data, so is efficient for large arrays.
  473. void FBX::Node::WritePropertyNode(
  474. const std::string& name,
  475. const std::vector<int32_t>& v,
  476. Assimp::StreamWriterLE& s,
  477. bool binary, int indent
  478. ){
  479. if (binary) {
  480. FBX::Node::WritePropertyNodeBinary(name, v, s);
  481. } else {
  482. FBX::Node::WritePropertyNodeAscii(name, v, s, indent);
  483. }
  484. }
  485. }
  486. #endif // ASSIMP_BUILD_NO_FBX_EXPORTER
  487. #endif // ASSIMP_BUILD_NO_EXPORT