FBXExportNode.cpp 15 KB

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