json_exporter.cpp 22 KB

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