json_exporter.cpp 22 KB

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