json_exporter.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. Assimp2Json
  3. Copyright (c) 2011, Alexander C. Gessler
  4. Licensed under a 3-clause BSD license. See the LICENSE file for more information.
  5. */
  6. #ifndef ASSIMP_BUILD_NO_EXPORT
  7. #ifndef ASSIMP_BUILD_NO_ASSJSON_EXPORTER
  8. #include <assimp/scene.h>
  9. #include <assimp/Exporter.hpp>
  10. #include <assimp/IOStream.hpp>
  11. #include <assimp/IOSystem.hpp>
  12. #include <assimp/Importer.hpp>
  13. #include <assimp/Exceptional.h>
  14. #include <cassert>
  15. #include <limits>
  16. #include <memory>
  17. #include <sstream>
  18. #define CURRENT_FORMAT_VERSION 100
  19. // grab scoped_ptr from assimp to avoid a dependency on boost.
  20. //#include <assimp/../../code/BoostWorkaround/boost/scoped_ptr.hpp>
  21. #include "mesh_splitter.h"
  22. extern "C" {
  23. #include "cencode.h"
  24. }
  25. namespace Assimp {
  26. void ExportAssimp2Json(const char *, Assimp::IOSystem *, const aiScene *, const Assimp::ExportProperties *);
  27. // small utility class to simplify serializing the aiScene to Json
  28. class JSONWriter {
  29. public:
  30. enum {
  31. Flag_DoNotIndent = 0x1,
  32. Flag_WriteSpecialFloats = 0x2,
  33. };
  34. JSONWriter(Assimp::IOStream &out, unsigned int flags = 0u) :
  35. out(out), first(), flags(flags) {
  36. // make sure that all formatting happens using the standard, C locale and not the user's current locale
  37. buff.imbue(std::locale("C"));
  38. }
  39. ~JSONWriter() {
  40. Flush();
  41. }
  42. void Flush() {
  43. const std::string s = buff.str();
  44. out.Write(s.c_str(), s.length(), 1);
  45. buff.clear();
  46. }
  47. void PushIndent() {
  48. indent += '\t';
  49. }
  50. void PopIndent() {
  51. indent.erase(indent.end() - 1);
  52. }
  53. void Key(const std::string &name) {
  54. AddIndentation();
  55. Delimit();
  56. buff << '\"' + name + "\": ";
  57. }
  58. template <typename Literal>
  59. void Element(const Literal &name) {
  60. AddIndentation();
  61. Delimit();
  62. LiteralToString(buff, name) << '\n';
  63. }
  64. template <typename Literal>
  65. void SimpleValue(const Literal &s) {
  66. LiteralToString(buff, s) << '\n';
  67. }
  68. void SimpleValue(const void *buffer, size_t len) {
  69. base64_encodestate s;
  70. base64_init_encodestate(&s);
  71. char *const cur_out = new char[std::max(len * 2, static_cast<size_t>(16u))];
  72. const int n = base64_encode_block(reinterpret_cast<const char *>(buffer), static_cast<int>(len), cur_out, &s);
  73. cur_out[n + base64_encode_blockend(cur_out + n, &s)] = '\0';
  74. // base64 encoding may add newlines, but JSON strings may not contain 'real' newlines
  75. // (only escaped ones). Remove any newlines in out.
  76. for (char *cur = cur_out; *cur; ++cur) {
  77. if (*cur == '\n') {
  78. *cur = ' ';
  79. }
  80. }
  81. buff << '\"' << cur_out << "\"\n";
  82. delete[] cur_out;
  83. }
  84. void StartObj(bool is_element = false) {
  85. // if this appears as a plain array element, we need to insert a delimiter and we should also indent it
  86. if (is_element) {
  87. AddIndentation();
  88. if (!first) {
  89. buff << ',';
  90. }
  91. }
  92. first = true;
  93. buff << "{\n";
  94. PushIndent();
  95. }
  96. void EndObj() {
  97. PopIndent();
  98. AddIndentation();
  99. first = false;
  100. buff << "}\n";
  101. }
  102. void StartArray(bool is_element = false) {
  103. // if this appears as a plain array element, we need to insert a delimiter and we should also indent it
  104. if (is_element) {
  105. AddIndentation();
  106. if (!first) {
  107. buff << ',';
  108. }
  109. }
  110. first = true;
  111. buff << "[\n";
  112. PushIndent();
  113. }
  114. void EndArray() {
  115. PopIndent();
  116. AddIndentation();
  117. buff << "]\n";
  118. first = false;
  119. }
  120. void AddIndentation() {
  121. if (!(flags & Flag_DoNotIndent)) {
  122. buff << indent;
  123. }
  124. }
  125. void Delimit() {
  126. if (!first) {
  127. buff << ',';
  128. } else {
  129. buff << ' ';
  130. first = false;
  131. }
  132. }
  133. private:
  134. template <typename Literal>
  135. std::stringstream &LiteralToString(std::stringstream &stream, const Literal &s) {
  136. stream << s;
  137. return stream;
  138. }
  139. std::stringstream &LiteralToString(std::stringstream &stream, const aiString &s) {
  140. std::string t;
  141. // escape backslashes and single quotes, both would render the JSON invalid if left as is
  142. t.reserve(s.length);
  143. for (size_t i = 0; i < s.length; ++i) {
  144. if (s.data[i] == '\\' || s.data[i] == '\'' || s.data[i] == '\"') {
  145. t.push_back('\\');
  146. }
  147. t.push_back(s.data[i]);
  148. }
  149. stream << "\"";
  150. stream << t;
  151. stream << "\"";
  152. return stream;
  153. }
  154. std::stringstream &LiteralToString(std::stringstream &stream, float f) {
  155. if (!std::numeric_limits<float>::is_iec559) {
  156. // on a non IEEE-754 platform, we make no assumptions about the representation or existence
  157. // of special floating-point numbers.
  158. stream << f;
  159. return stream;
  160. }
  161. // JSON does not support writing Inf/Nan
  162. // [RFC 4672: "Numeric values that cannot be represented as sequences of digits
  163. // (such as Infinity and NaN) are not permitted."]
  164. // Nevertheless, many parsers will accept the special keywords Infinity, -Infinity and NaN
  165. if (std::numeric_limits<float>::infinity() == fabs(f)) {
  166. if (flags & Flag_WriteSpecialFloats) {
  167. stream << (f < 0 ? "\"-" : "\"") + std::string("Infinity\"");
  168. return stream;
  169. }
  170. // we should print this warning, but we can't - this is called from within a generic assimp exporter, we cannot use cerr
  171. // std::cerr << "warning: cannot represent infinite number literal, substituting 0 instead (use -i flag to enforce Infinity/NaN)" << std::endl;
  172. stream << "0.0";
  173. return stream;
  174. }
  175. // f!=f is the most reliable test for NaNs that I know of
  176. else if (f != f) {
  177. if (flags & Flag_WriteSpecialFloats) {
  178. stream << "\"NaN\"";
  179. return stream;
  180. }
  181. // we should print this warning, but we can't - this is called from within a generic assimp exporter, we cannot use cerr
  182. // std::cerr << "warning: cannot represent infinite number literal, substituting 0 instead (use -i flag to enforce Infinity/NaN)" << std::endl;
  183. stream << "0.0";
  184. return stream;
  185. }
  186. stream << f;
  187. return stream;
  188. }
  189. private:
  190. Assimp::IOStream &out;
  191. std::string indent, newline;
  192. std::stringstream buff;
  193. bool first;
  194. unsigned int flags;
  195. };
  196. void Write(JSONWriter &out, const aiVector3D &ai, bool is_elem = true) {
  197. out.StartArray(is_elem);
  198. out.Element(ai.x);
  199. out.Element(ai.y);
  200. out.Element(ai.z);
  201. out.EndArray();
  202. }
  203. void Write(JSONWriter &out, const aiQuaternion &ai, bool is_elem = true) {
  204. out.StartArray(is_elem);
  205. out.Element(ai.w);
  206. out.Element(ai.x);
  207. out.Element(ai.y);
  208. out.Element(ai.z);
  209. out.EndArray();
  210. }
  211. void Write(JSONWriter &out, const aiColor3D &ai, bool is_elem = true) {
  212. out.StartArray(is_elem);
  213. out.Element(ai.r);
  214. out.Element(ai.g);
  215. out.Element(ai.b);
  216. out.EndArray();
  217. }
  218. void Write(JSONWriter &out, const aiMatrix4x4 &ai, bool is_elem = true) {
  219. out.StartArray(is_elem);
  220. for (unsigned int x = 0; x < 4; ++x) {
  221. for (unsigned int y = 0; y < 4; ++y) {
  222. out.Element(ai[x][y]);
  223. }
  224. }
  225. out.EndArray();
  226. }
  227. void Write(JSONWriter &out, const aiBone &ai, bool is_elem = true) {
  228. out.StartObj(is_elem);
  229. out.Key("name");
  230. out.SimpleValue(ai.mName);
  231. out.Key("offsetmatrix");
  232. Write(out, ai.mOffsetMatrix, false);
  233. out.Key("weights");
  234. out.StartArray();
  235. for (unsigned int i = 0; i < ai.mNumWeights; ++i) {
  236. out.StartArray(true);
  237. out.Element(ai.mWeights[i].mVertexId);
  238. out.Element(ai.mWeights[i].mWeight);
  239. out.EndArray();
  240. }
  241. out.EndArray();
  242. out.EndObj();
  243. }
  244. void Write(JSONWriter &out, const aiFace &ai, bool is_elem = true) {
  245. out.StartArray(is_elem);
  246. for (unsigned int i = 0; i < ai.mNumIndices; ++i) {
  247. out.Element(ai.mIndices[i]);
  248. }
  249. out.EndArray();
  250. }
  251. void Write(JSONWriter &out, const aiMesh &ai, bool is_elem = true) {
  252. out.StartObj(is_elem);
  253. out.Key("name");
  254. out.SimpleValue(ai.mName);
  255. out.Key("materialindex");
  256. out.SimpleValue(ai.mMaterialIndex);
  257. out.Key("primitivetypes");
  258. out.SimpleValue(ai.mPrimitiveTypes);
  259. out.Key("vertices");
  260. out.StartArray();
  261. for (unsigned int i = 0; i < ai.mNumVertices; ++i) {
  262. out.Element(ai.mVertices[i].x);
  263. out.Element(ai.mVertices[i].y);
  264. out.Element(ai.mVertices[i].z);
  265. }
  266. out.EndArray();
  267. if (ai.HasNormals()) {
  268. out.Key("normals");
  269. out.StartArray();
  270. for (unsigned int i = 0; i < ai.mNumVertices; ++i) {
  271. out.Element(ai.mNormals[i].x);
  272. out.Element(ai.mNormals[i].y);
  273. out.Element(ai.mNormals[i].z);
  274. }
  275. out.EndArray();
  276. }
  277. if (ai.HasTangentsAndBitangents()) {
  278. out.Key("tangents");
  279. out.StartArray();
  280. for (unsigned int i = 0; i < ai.mNumVertices; ++i) {
  281. out.Element(ai.mTangents[i].x);
  282. out.Element(ai.mTangents[i].y);
  283. out.Element(ai.mTangents[i].z);
  284. }
  285. out.EndArray();
  286. out.Key("bitangents");
  287. out.StartArray();
  288. for (unsigned int i = 0; i < ai.mNumVertices; ++i) {
  289. out.Element(ai.mBitangents[i].x);
  290. out.Element(ai.mBitangents[i].y);
  291. out.Element(ai.mBitangents[i].z);
  292. }
  293. out.EndArray();
  294. }
  295. if (ai.GetNumUVChannels()) {
  296. out.Key("numuvcomponents");
  297. out.StartArray();
  298. for (unsigned int n = 0; n < ai.GetNumUVChannels(); ++n) {
  299. out.Element(ai.mNumUVComponents[n]);
  300. }
  301. out.EndArray();
  302. out.Key("texturecoords");
  303. out.StartArray();
  304. for (unsigned int n = 0; n < ai.GetNumUVChannels(); ++n) {
  305. const unsigned int numc = ai.mNumUVComponents[n] ? ai.mNumUVComponents[n] : 2;
  306. out.StartArray(true);
  307. for (unsigned int i = 0; i < ai.mNumVertices; ++i) {
  308. for (unsigned int c = 0; c < numc; ++c) {
  309. out.Element(ai.mTextureCoords[n][i][c]);
  310. }
  311. }
  312. out.EndArray();
  313. }
  314. out.EndArray();
  315. }
  316. if (ai.GetNumColorChannels()) {
  317. out.Key("colors");
  318. out.StartArray();
  319. for (unsigned int n = 0; n < ai.GetNumColorChannels(); ++n) {
  320. out.StartArray(true);
  321. for (unsigned int i = 0; i < ai.mNumVertices; ++i) {
  322. out.Element(ai.mColors[n][i].r);
  323. out.Element(ai.mColors[n][i].g);
  324. out.Element(ai.mColors[n][i].b);
  325. out.Element(ai.mColors[n][i].a);
  326. }
  327. out.EndArray();
  328. }
  329. out.EndArray();
  330. }
  331. if (ai.mNumBones) {
  332. out.Key("bones");
  333. out.StartArray();
  334. for (unsigned int n = 0; n < ai.mNumBones; ++n) {
  335. Write(out, *ai.mBones[n]);
  336. }
  337. out.EndArray();
  338. }
  339. out.Key("faces");
  340. out.StartArray();
  341. for (unsigned int n = 0; n < ai.mNumFaces; ++n) {
  342. Write(out, ai.mFaces[n]);
  343. }
  344. out.EndArray();
  345. out.EndObj();
  346. }
  347. void Write(JSONWriter &out, const aiNode &ai, bool is_elem = true) {
  348. out.StartObj(is_elem);
  349. out.Key("name");
  350. out.SimpleValue(ai.mName);
  351. out.Key("transformation");
  352. Write(out, ai.mTransformation, false);
  353. if (ai.mNumMeshes) {
  354. out.Key("meshes");
  355. out.StartArray();
  356. for (unsigned int n = 0; n < ai.mNumMeshes; ++n) {
  357. out.Element(ai.mMeshes[n]);
  358. }
  359. out.EndArray();
  360. }
  361. if (ai.mNumChildren) {
  362. out.Key("children");
  363. out.StartArray();
  364. for (unsigned int n = 0; n < ai.mNumChildren; ++n) {
  365. Write(out, *ai.mChildren[n]);
  366. }
  367. out.EndArray();
  368. }
  369. out.EndObj();
  370. }
  371. void Write(JSONWriter &out, const aiMaterial &ai, bool is_elem = true) {
  372. out.StartObj(is_elem);
  373. out.Key("properties");
  374. out.StartArray();
  375. for (unsigned int i = 0; i < ai.mNumProperties; ++i) {
  376. const aiMaterialProperty *const prop = ai.mProperties[i];
  377. out.StartObj(true);
  378. out.Key("key");
  379. out.SimpleValue(prop->mKey);
  380. out.Key("semantic");
  381. out.SimpleValue(prop->mSemantic);
  382. out.Key("index");
  383. out.SimpleValue(prop->mIndex);
  384. out.Key("type");
  385. out.SimpleValue(prop->mType);
  386. out.Key("value");
  387. switch (prop->mType) {
  388. case aiPTI_Float:
  389. if (prop->mDataLength / sizeof(float) > 1) {
  390. out.StartArray();
  391. for (unsigned int ii = 0; ii < prop->mDataLength / sizeof(float); ++ii) {
  392. out.Element(reinterpret_cast<float *>(prop->mData)[ii]);
  393. }
  394. out.EndArray();
  395. } else {
  396. out.SimpleValue(*reinterpret_cast<float *>(prop->mData));
  397. }
  398. break;
  399. case aiPTI_Integer:
  400. if (prop->mDataLength / sizeof(int) > 1) {
  401. out.StartArray();
  402. for (unsigned int ii = 0; ii < prop->mDataLength / sizeof(int); ++ii) {
  403. out.Element(reinterpret_cast<int *>(prop->mData)[ii]);
  404. }
  405. out.EndArray();
  406. } else {
  407. out.SimpleValue(*reinterpret_cast<int *>(prop->mData));
  408. }
  409. break;
  410. case aiPTI_String: {
  411. aiString s;
  412. aiGetMaterialString(&ai, prop->mKey.data, prop->mSemantic, prop->mIndex, &s);
  413. out.SimpleValue(s);
  414. } break;
  415. case aiPTI_Buffer: {
  416. // binary data is written as series of hex-encoded octets
  417. out.SimpleValue(prop->mData, prop->mDataLength);
  418. } break;
  419. default:
  420. assert(false);
  421. }
  422. out.EndObj();
  423. }
  424. out.EndArray();
  425. out.EndObj();
  426. }
  427. void Write(JSONWriter &out, const aiTexture &ai, bool is_elem = true) {
  428. out.StartObj(is_elem);
  429. out.Key("width");
  430. out.SimpleValue(ai.mWidth);
  431. out.Key("height");
  432. out.SimpleValue(ai.mHeight);
  433. out.Key("formathint");
  434. out.SimpleValue(aiString(ai.achFormatHint));
  435. out.Key("data");
  436. if (!ai.mHeight) {
  437. out.SimpleValue(ai.pcData, ai.mWidth);
  438. } else {
  439. out.StartArray();
  440. for (unsigned int y = 0; y < ai.mHeight; ++y) {
  441. out.StartArray(true);
  442. for (unsigned int x = 0; x < ai.mWidth; ++x) {
  443. const aiTexel &tx = ai.pcData[y * ai.mWidth + x];
  444. out.StartArray(true);
  445. out.Element(static_cast<unsigned int>(tx.r));
  446. out.Element(static_cast<unsigned int>(tx.g));
  447. out.Element(static_cast<unsigned int>(tx.b));
  448. out.Element(static_cast<unsigned int>(tx.a));
  449. out.EndArray();
  450. }
  451. out.EndArray();
  452. }
  453. out.EndArray();
  454. }
  455. out.EndObj();
  456. }
  457. void Write(JSONWriter &out, const aiLight &ai, bool is_elem = true) {
  458. out.StartObj(is_elem);
  459. out.Key("name");
  460. out.SimpleValue(ai.mName);
  461. out.Key("type");
  462. out.SimpleValue(ai.mType);
  463. if (ai.mType == aiLightSource_SPOT || ai.mType == aiLightSource_UNDEFINED) {
  464. out.Key("angleinnercone");
  465. out.SimpleValue(ai.mAngleInnerCone);
  466. out.Key("angleoutercone");
  467. out.SimpleValue(ai.mAngleOuterCone);
  468. }
  469. out.Key("attenuationconstant");
  470. out.SimpleValue(ai.mAttenuationConstant);
  471. out.Key("attenuationlinear");
  472. out.SimpleValue(ai.mAttenuationLinear);
  473. out.Key("attenuationquadratic");
  474. out.SimpleValue(ai.mAttenuationQuadratic);
  475. out.Key("diffusecolor");
  476. Write(out, ai.mColorDiffuse, false);
  477. out.Key("specularcolor");
  478. Write(out, ai.mColorSpecular, false);
  479. out.Key("ambientcolor");
  480. Write(out, ai.mColorAmbient, false);
  481. if (ai.mType != aiLightSource_POINT) {
  482. out.Key("direction");
  483. Write(out, ai.mDirection, false);
  484. }
  485. if (ai.mType != aiLightSource_DIRECTIONAL) {
  486. out.Key("position");
  487. Write(out, ai.mPosition, false);
  488. }
  489. out.EndObj();
  490. }
  491. void Write(JSONWriter &out, const aiNodeAnim &ai, bool is_elem = true) {
  492. out.StartObj(is_elem);
  493. out.Key("name");
  494. out.SimpleValue(ai.mNodeName);
  495. out.Key("prestate");
  496. out.SimpleValue(ai.mPreState);
  497. out.Key("poststate");
  498. out.SimpleValue(ai.mPostState);
  499. if (ai.mNumPositionKeys) {
  500. out.Key("positionkeys");
  501. out.StartArray();
  502. for (unsigned int n = 0; n < ai.mNumPositionKeys; ++n) {
  503. const aiVectorKey &pos = ai.mPositionKeys[n];
  504. out.StartArray(true);
  505. out.Element(pos.mTime);
  506. Write(out, pos.mValue);
  507. out.EndArray();
  508. }
  509. out.EndArray();
  510. }
  511. if (ai.mNumRotationKeys) {
  512. out.Key("rotationkeys");
  513. out.StartArray();
  514. for (unsigned int n = 0; n < ai.mNumRotationKeys; ++n) {
  515. const aiQuatKey &rot = ai.mRotationKeys[n];
  516. out.StartArray(true);
  517. out.Element(rot.mTime);
  518. Write(out, rot.mValue);
  519. out.EndArray();
  520. }
  521. out.EndArray();
  522. }
  523. if (ai.mNumScalingKeys) {
  524. out.Key("scalingkeys");
  525. out.StartArray();
  526. for (unsigned int n = 0; n < ai.mNumScalingKeys; ++n) {
  527. const aiVectorKey &scl = ai.mScalingKeys[n];
  528. out.StartArray(true);
  529. out.Element(scl.mTime);
  530. Write(out, scl.mValue);
  531. out.EndArray();
  532. }
  533. out.EndArray();
  534. }
  535. out.EndObj();
  536. }
  537. void Write(JSONWriter &out, const aiAnimation &ai, bool is_elem = true) {
  538. out.StartObj(is_elem);
  539. out.Key("name");
  540. out.SimpleValue(ai.mName);
  541. out.Key("tickspersecond");
  542. out.SimpleValue(ai.mTicksPerSecond);
  543. out.Key("duration");
  544. out.SimpleValue(ai.mDuration);
  545. out.Key("channels");
  546. out.StartArray();
  547. for (unsigned int n = 0; n < ai.mNumChannels; ++n) {
  548. Write(out, *ai.mChannels[n]);
  549. }
  550. out.EndArray();
  551. out.EndObj();
  552. }
  553. void Write(JSONWriter &out, const aiCamera &ai, bool is_elem = true) {
  554. out.StartObj(is_elem);
  555. out.Key("name");
  556. out.SimpleValue(ai.mName);
  557. out.Key("aspect");
  558. out.SimpleValue(ai.mAspect);
  559. out.Key("clipplanefar");
  560. out.SimpleValue(ai.mClipPlaneFar);
  561. out.Key("clipplanenear");
  562. out.SimpleValue(ai.mClipPlaneNear);
  563. out.Key("horizontalfov");
  564. out.SimpleValue(ai.mHorizontalFOV);
  565. out.Key("up");
  566. Write(out, ai.mUp, false);
  567. out.Key("lookat");
  568. Write(out, ai.mLookAt, false);
  569. out.EndObj();
  570. }
  571. void WriteFormatInfo(JSONWriter &out) {
  572. out.StartObj();
  573. out.Key("format");
  574. out.SimpleValue("\"assimp2json\"");
  575. out.Key("version");
  576. out.SimpleValue(CURRENT_FORMAT_VERSION);
  577. out.EndObj();
  578. }
  579. void Write(JSONWriter &out, const aiScene &ai) {
  580. out.StartObj();
  581. out.Key("__metadata__");
  582. WriteFormatInfo(out);
  583. out.Key("rootnode");
  584. Write(out, *ai.mRootNode, false);
  585. out.Key("flags");
  586. out.SimpleValue(ai.mFlags);
  587. if (ai.HasMeshes()) {
  588. out.Key("meshes");
  589. out.StartArray();
  590. for (unsigned int n = 0; n < ai.mNumMeshes; ++n) {
  591. Write(out, *ai.mMeshes[n]);
  592. }
  593. out.EndArray();
  594. }
  595. if (ai.HasMaterials()) {
  596. out.Key("materials");
  597. out.StartArray();
  598. for (unsigned int n = 0; n < ai.mNumMaterials; ++n) {
  599. Write(out, *ai.mMaterials[n]);
  600. }
  601. out.EndArray();
  602. }
  603. if (ai.HasAnimations()) {
  604. out.Key("animations");
  605. out.StartArray();
  606. for (unsigned int n = 0; n < ai.mNumAnimations; ++n) {
  607. Write(out, *ai.mAnimations[n]);
  608. }
  609. out.EndArray();
  610. }
  611. if (ai.HasLights()) {
  612. out.Key("lights");
  613. out.StartArray();
  614. for (unsigned int n = 0; n < ai.mNumLights; ++n) {
  615. Write(out, *ai.mLights[n]);
  616. }
  617. out.EndArray();
  618. }
  619. if (ai.HasCameras()) {
  620. out.Key("cameras");
  621. out.StartArray();
  622. for (unsigned int n = 0; n < ai.mNumCameras; ++n) {
  623. Write(out, *ai.mCameras[n]);
  624. }
  625. out.EndArray();
  626. }
  627. if (ai.HasTextures()) {
  628. out.Key("textures");
  629. out.StartArray();
  630. for (unsigned int n = 0; n < ai.mNumTextures; ++n) {
  631. Write(out, *ai.mTextures[n]);
  632. }
  633. out.EndArray();
  634. }
  635. out.EndObj();
  636. }
  637. void ExportAssimp2Json(const char *file, Assimp::IOSystem *io, const aiScene *scene, const Assimp::ExportProperties *) {
  638. std::unique_ptr<Assimp::IOStream> str(io->Open(file, "wt"));
  639. if (!str) {
  640. throw DeadlyExportError("could not open output file");
  641. }
  642. // get a copy of the scene so we can modify it
  643. aiScene *scenecopy_tmp;
  644. aiCopyScene(scene, &scenecopy_tmp);
  645. try {
  646. // split meshes so they fit into a 16 bit index buffer
  647. MeshSplitter splitter;
  648. splitter.SetLimit(1 << 16);
  649. splitter.Execute(scenecopy_tmp);
  650. // XXX Flag_WriteSpecialFloats is turned on by default, right now we don't have a configuration interface for exporters
  651. JSONWriter s(*str, JSONWriter::Flag_WriteSpecialFloats);
  652. Write(s, *scenecopy_tmp);
  653. } catch (...) {
  654. aiFreeScene(scenecopy_tmp);
  655. throw;
  656. }
  657. aiFreeScene(scenecopy_tmp);
  658. }
  659. } // namespace Assimp
  660. #endif // ASSIMP_BUILD_NO_ASSJSON_EXPORTER
  661. #endif // ASSIMP_BUILD_NO_EXPORT