Main.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Main.cpp
  35. * @brief main() function of assimp_cmd
  36. */
  37. #include "Main.h"
  38. #include <assimp/ProgressHandler.hpp>
  39. #include <iostream>
  40. class ConsoleProgressHandler : public ProgressHandler {
  41. public:
  42. ConsoleProgressHandler() :
  43. ProgressHandler() {
  44. // empty
  45. }
  46. ~ConsoleProgressHandler() override = default;
  47. bool Update(float percentage) override {
  48. std::cout << percentage * 100.0f << " %\n";
  49. return true;
  50. }
  51. };
  52. const char* AICMD_MSG_ABOUT =
  53. "------------------------------------------------------ \n"
  54. "Open Asset Import Library (\"Assimp\", https://github.com/assimp/assimp) \n"
  55. " -- Commandline toolchain --\n"
  56. "------------------------------------------------------ \n\n"
  57. "Version %i.%i %s%s%s%s%s(GIT commit %x)\n\n";
  58. const char* AICMD_MSG_HELP =
  59. "assimp <verb> <parameters>\n\n"
  60. " verbs:\n"
  61. " \tinfo - Quick file stats\n"
  62. " \tlistext - List all known file extensions available for import\n"
  63. " \tknowext - Check whether a file extension is recognized by Assimp\n"
  64. #ifndef ASSIMP_BUILD_NO_EXPORT
  65. " \texport - Export a file to one of the supported output formats\n"
  66. " \tlistexport - List all supported export formats\n"
  67. " \texportinfo - Show basic information on a specific export format\n"
  68. #endif
  69. " \textract - Extract embedded texture images\n"
  70. " \tdump - Convert models to a binary or textual dump (ASSBIN/ASSXML)\n"
  71. " \tcmpdump - Compare dumps created using \'assimp dump <file> -s ...\'\n"
  72. " \tversion - Display Assimp version\n"
  73. "\n Use \'assimp <verb> --help\' for detailed help on a command.\n"
  74. ;
  75. /*extern*/ Assimp::Importer* globalImporter = nullptr;
  76. #ifndef ASSIMP_BUILD_NO_EXPORT
  77. /*extern*/ Assimp::Exporter* globalExporter = nullptr;
  78. #endif
  79. // ------------------------------------------------------------------------------
  80. // Application entry point
  81. int main (int argc, char* argv[])
  82. {
  83. if (argc <= 1) {
  84. printf("assimp: No command specified. Use \'assimp help\' for a detailed command list\n");
  85. return AssimpCmdError::Success;
  86. }
  87. // assimp version
  88. // Display version information
  89. if (! strcmp(argv[1], "version")) {
  90. const unsigned int flags = aiGetCompileFlags();
  91. printf(AICMD_MSG_ABOUT,
  92. aiGetVersionMajor(),
  93. aiGetVersionMinor(),
  94. (flags & ASSIMP_CFLAGS_DEBUG ? "-debug " : ""),
  95. (flags & ASSIMP_CFLAGS_NOBOOST ? "-noboost " : ""),
  96. (flags & ASSIMP_CFLAGS_SHARED ? "-shared " : ""),
  97. (flags & ASSIMP_CFLAGS_SINGLETHREADED ? "-st " : ""),
  98. (flags & ASSIMP_CFLAGS_STLPORT ? "-stlport " : ""),
  99. aiGetVersionRevision());
  100. return AssimpCmdError::Success;
  101. }
  102. // assimp help
  103. // Display some basic help (--help and -h work as well
  104. // because people could try them intuitively)
  105. if (!strcmp(argv[1], "help") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
  106. printf("%s",AICMD_MSG_HELP);
  107. return AssimpCmdError::Success;
  108. }
  109. // assimp cmpdump
  110. // Compare two mini model dumps (regression suite)
  111. if (! strcmp(argv[1], "cmpdump")) {
  112. return Assimp_CompareDump (&argv[2],argc-2);
  113. }
  114. // construct global importer and exporter instances
  115. Assimp::Importer imp;
  116. imp.SetPropertyBool("GLOB_MEASURE_TIME",true);
  117. globalImporter = &imp;
  118. #ifndef ASSIMP_BUILD_NO_EXPORT
  119. //
  120. Assimp::Exporter exp;
  121. globalExporter = &exp;
  122. #endif
  123. // assimp listext
  124. // List all file extensions supported by Assimp
  125. if (! strcmp(argv[1], "listext")) {
  126. aiString s;
  127. imp.GetExtensionList(s);
  128. printf("%s\n",s.data);
  129. return AssimpCmdError::Success;
  130. }
  131. #ifndef ASSIMP_BUILD_NO_EXPORT
  132. // assimp listexport
  133. // List all export file formats supported by Assimp (not the file extensions, just the format identifiers!)
  134. if (! strcmp(argv[1], "listexport")) {
  135. aiString s;
  136. for(size_t i = 0, end = exp.GetExportFormatCount(); i < end; ++i) {
  137. const aiExportFormatDesc* const e = exp.GetExportFormatDescription(i);
  138. s.Append( e->id );
  139. if (i!=end-1) {
  140. s.Append("\n");
  141. }
  142. }
  143. printf("%s\n",s.data);
  144. return AssimpCmdError::Success;
  145. }
  146. // assimp exportinfo
  147. // stat an export format
  148. if (! strcmp(argv[1], "exportinfo")) {
  149. aiString s;
  150. if (argc<3) {
  151. printf("Expected file format id\n");
  152. return AssimpCmdError::NoFileFormatSpecified;
  153. }
  154. for(size_t i = 0, end = exp.GetExportFormatCount(); i < end; ++i) {
  155. const aiExportFormatDesc* const e = exp.GetExportFormatDescription(i);
  156. if (!strcmp(e->id,argv[2])) {
  157. printf("%s\n%s\n%s\n",e->id,e->fileExtension,e->description);
  158. return AssimpCmdError::Success;
  159. }
  160. }
  161. printf("Unknown file format id: \'%s\'\n",argv[2]);
  162. return AssimpCmdError::UnknownFileFormat;
  163. }
  164. // assimp export
  165. // Export a model to a file
  166. if (! strcmp(argv[1], "export")) {
  167. return Assimp_Export (&argv[2],argc-2);
  168. }
  169. #endif
  170. // assimp knowext
  171. // Check whether a particular file extension is known by us, return 0 on success
  172. if (! strcmp(argv[1], "knowext")) {
  173. if (argc<3) {
  174. printf("Expected file extension");
  175. return AssimpCmdError::NoFileExtensionSpecified;
  176. }
  177. const bool b = imp.IsExtensionSupported(argv[2]);
  178. printf("File extension \'%s\' is %sknown\n",argv[2],(b?"":"not "));
  179. return b? AssimpCmdError::Success : AssimpCmdError::UnknownFileExtension;
  180. }
  181. // assimp info
  182. // Print basic model statistics
  183. if (! strcmp(argv[1], "info")) {
  184. return Assimp_Info ((const char**)&argv[2],argc-2);
  185. }
  186. // assimp dump
  187. // Dump a model to a file
  188. if (! strcmp(argv[1], "dump")) {
  189. return Assimp_Dump (&argv[2],argc-2);
  190. }
  191. // assimp extract
  192. // Extract an embedded texture from a file
  193. if (! strcmp(argv[1], "extract")) {
  194. return Assimp_Extract (&argv[2],argc-2);
  195. }
  196. // assimp testbatchload
  197. // Used by /test/other/streamload.py to load a list of files
  198. // using the same importer instance to check for incompatible
  199. // importers.
  200. if (! strcmp(argv[1], "testbatchload")) {
  201. return Assimp_TestBatchLoad (&argv[2],argc-2);
  202. }
  203. printf("Unrecognized command. Use \'assimp help\' for a detailed command list\n");
  204. return AssimpCmdError::UnrecognizedCommand;
  205. }
  206. // ------------------------------------------------------------------------------
  207. void SetLogStreams(const ImportData& imp)
  208. {
  209. printf("\nAttaching log stream ... OK\n");
  210. unsigned int flags = 0;
  211. if (imp.logFile.length()) {
  212. flags |= aiDefaultLogStream_FILE;
  213. }
  214. if (imp.showLog) {
  215. flags |= aiDefaultLogStream_STDERR;
  216. }
  217. DefaultLogger::create(imp.logFile.c_str(),imp.verbose ? Logger::VERBOSE : Logger::NORMAL,flags);
  218. }
  219. // ------------------------------------------------------------------------------
  220. void FreeLogStreams()
  221. {
  222. DefaultLogger::kill();
  223. }
  224. // ------------------------------------------------------------------------------
  225. void PrintHorBar()
  226. {
  227. printf("-----------------------------------------------------------------\n");
  228. }
  229. // ------------------------------------------------------------------------------
  230. // Import a specific file
  231. const aiScene* ImportModel(
  232. const ImportData& imp,
  233. const std::string& path)
  234. {
  235. // Attach log streams
  236. if (imp.log) {
  237. SetLogStreams(imp);
  238. }
  239. printf("Launching asset import ... OK\n");
  240. // Now validate this flag combination
  241. if(!globalImporter->ValidateFlags(imp.ppFlags)) {
  242. printf("ERROR: Unsupported post-processing flags \n");
  243. return NULL;
  244. }
  245. printf("Validating postprocessing flags ... OK\n");
  246. if (imp.showLog) {
  247. PrintHorBar();
  248. }
  249. // do the actual import, measure time
  250. const clock_t first = clock();
  251. ConsoleProgressHandler *ph = new ConsoleProgressHandler;
  252. globalImporter->SetProgressHandler(ph);
  253. const aiScene* scene = globalImporter->ReadFile(path,imp.ppFlags);
  254. if (imp.showLog) {
  255. PrintHorBar();
  256. }
  257. if (!scene) {
  258. printf("ERROR: Failed to load file: %s\n", globalImporter->GetErrorString());
  259. return NULL;
  260. }
  261. const clock_t second = ::clock();
  262. const double seconds = static_cast<double>(second-first) / CLOCKS_PER_SEC;
  263. printf("Importing file ... OK \n import took approx. %.5f seconds\n"
  264. "\n",seconds);
  265. if (imp.log) {
  266. FreeLogStreams();
  267. }
  268. globalImporter->SetProgressHandler(nullptr);
  269. delete ph;
  270. return scene;
  271. }
  272. #ifndef ASSIMP_BUILD_NO_EXPORT
  273. // ------------------------------------------------------------------------------
  274. bool ExportModel(const aiScene* pOut,
  275. const ImportData& imp,
  276. const std::string& path,
  277. const char* pID)
  278. {
  279. // Attach log streams
  280. if (imp.log) {
  281. SetLogStreams(imp);
  282. }
  283. printf("Launching asset export ... OK\n");
  284. if (imp.showLog) {
  285. PrintHorBar();
  286. }
  287. aiMatrix4x4 rx, ry, rz;
  288. aiMatrix4x4::RotationX(imp.rot.x, rx);
  289. aiMatrix4x4::RotationY(imp.rot.y, ry);
  290. aiMatrix4x4::RotationZ(imp.rot.z, rz);
  291. pOut->mRootNode->mTransformation *= rx;
  292. pOut->mRootNode->mTransformation *= ry;
  293. pOut->mRootNode->mTransformation *= rz;
  294. // do the actual export, measure time
  295. const clock_t first = clock();
  296. const aiReturn res = globalExporter->Export(pOut,pID,path);
  297. if (imp.showLog) {
  298. PrintHorBar();
  299. }
  300. if (res != AI_SUCCESS) {
  301. printf("Failed to write file\n");
  302. printf("ERROR: %s\n", globalExporter->GetErrorString());
  303. return false;
  304. }
  305. const clock_t second = ::clock();
  306. const double seconds = static_cast<double>(second-first) / CLOCKS_PER_SEC;
  307. printf("Exporting file ... OK \n export took approx. %.5f seconds\n"
  308. "\n",seconds);
  309. if (imp.log) {
  310. FreeLogStreams();
  311. }
  312. return true;
  313. }
  314. #endif
  315. // ------------------------------------------------------------------------------
  316. // Process standard arguments
  317. int ProcessStandardArguments(
  318. ImportData& fill,
  319. const char* const * params,
  320. unsigned int num)
  321. {
  322. // -ptv --pretransform-vertices
  323. // -gsn --gen-smooth-normals
  324. // -gn --gen-normals
  325. // -cts --calc-tangent-space
  326. // -jiv --join-identical-vertices
  327. // -rrm --remove-redundant-materials
  328. // -fd --find-degenerates
  329. // -slm --split-large-meshes
  330. // -lbw --limit-bone-weights
  331. // -vds --validate-data-structure
  332. // -icl --improve-cache-locality
  333. // -sbpt --sort-by-ptype
  334. // -lh --convert-to-lh
  335. // -fuv --flip-uv
  336. // -fwo --flip-winding-order
  337. // -tuv --transform-uv-coords
  338. // -guv --gen-uvcoords
  339. // -fid --find-invalid-data
  340. // -fixn --fix normals
  341. // -tri --triangulate
  342. // -fi --find-instances
  343. // -og --optimize-graph
  344. // -om --optimize-meshes
  345. // -db --debone
  346. // -sbc --split-by-bone-count
  347. // -gs --global-scale
  348. //
  349. // -c<file> --config-file=<file>
  350. for (unsigned int i = 0; i < num;++i)
  351. {
  352. const char *param = params[ i ];
  353. printf( "param = %s\n", param );
  354. if (! strcmp( param, "-ptv") || ! strcmp( param, "--pretransform-vertices")) {
  355. fill.ppFlags |= aiProcess_PreTransformVertices;
  356. }
  357. else if (! strcmp( param, "-gsn") || ! strcmp( param, "--gen-smooth-normals")) {
  358. fill.ppFlags |= aiProcess_GenSmoothNormals;
  359. }
  360. else if (! strcmp( param, "-dn") || ! strcmp( param, "--drop-normals")) {
  361. fill.ppFlags |= aiProcess_DropNormals;
  362. }
  363. else if (! strcmp( param, "-gn") || ! strcmp( param, "--gen-normals")) {
  364. fill.ppFlags |= aiProcess_GenNormals;
  365. }
  366. else if (! strcmp( param, "-jiv") || ! strcmp( param, "--join-identical-vertices")) {
  367. fill.ppFlags |= aiProcess_JoinIdenticalVertices;
  368. }
  369. else if (! strcmp( param, "-rrm") || ! strcmp( param, "--remove-redundant-materials")) {
  370. fill.ppFlags |= aiProcess_RemoveRedundantMaterials;
  371. }
  372. else if (! strcmp( param, "-fd") || ! strcmp( param, "--find-degenerates")) {
  373. fill.ppFlags |= aiProcess_FindDegenerates;
  374. }
  375. else if (! strcmp( param, "-slm") || ! strcmp( param, "--split-large-meshes")) {
  376. fill.ppFlags |= aiProcess_SplitLargeMeshes;
  377. }
  378. else if (! strcmp( param, "-lbw") || ! strcmp( param, "--limit-bone-weights")) {
  379. fill.ppFlags |= aiProcess_LimitBoneWeights;
  380. }
  381. else if (! strcmp( param, "-vds") || ! strcmp( param, "--validate-data-structure")) {
  382. fill.ppFlags |= aiProcess_ValidateDataStructure;
  383. }
  384. else if (! strcmp( param, "-icl") || ! strcmp( param, "--improve-cache-locality")) {
  385. fill.ppFlags |= aiProcess_ImproveCacheLocality;
  386. }
  387. else if (! strcmp( param, "-sbpt") || ! strcmp( param, "--sort-by-ptype")) {
  388. fill.ppFlags |= aiProcess_SortByPType;
  389. }
  390. else if (! strcmp( param, "-lh") || ! strcmp( param, "--left-handed")) {
  391. fill.ppFlags |= aiProcess_ConvertToLeftHanded;
  392. }
  393. else if (! strcmp( param, "-fuv") || ! strcmp( param, "--flip-uv")) {
  394. fill.ppFlags |= aiProcess_FlipUVs;
  395. }
  396. else if (! strcmp( param, "-fwo") || ! strcmp( param, "--flip-winding-order")) {
  397. fill.ppFlags |= aiProcess_FlipWindingOrder;
  398. }
  399. else if (! strcmp( param, "-tuv") || ! strcmp( param, "--transform-uv-coords")) {
  400. fill.ppFlags |= aiProcess_TransformUVCoords;
  401. }
  402. else if (! strcmp( param, "-guv") || ! strcmp( param, "--gen-uvcoords")) {
  403. fill.ppFlags |= aiProcess_GenUVCoords;
  404. }
  405. else if (! strcmp( param, "-fid") || ! strcmp( param, "--find-invalid-data")) {
  406. fill.ppFlags |= aiProcess_FindInvalidData;
  407. }
  408. else if (! strcmp( param, "-fixn") || ! strcmp( param, "--fix-normals")) {
  409. fill.ppFlags |= aiProcess_FixInfacingNormals;
  410. }
  411. else if (! strcmp( param, "-tri") || ! strcmp( param, "--triangulate")) {
  412. fill.ppFlags |= aiProcess_Triangulate;
  413. }
  414. else if (! strcmp( param, "-cts") || ! strcmp( param, "--calc-tangent-space")) {
  415. fill.ppFlags |= aiProcess_CalcTangentSpace;
  416. }
  417. else if (! strcmp( param, "-fi") || ! strcmp( param, "--find-instances")) {
  418. fill.ppFlags |= aiProcess_FindInstances;
  419. }
  420. else if (! strcmp( param, "-og") || ! strcmp( param, "--optimize-graph")) {
  421. fill.ppFlags |= aiProcess_OptimizeGraph;
  422. }
  423. else if (! strcmp( param, "-om") || ! strcmp( param, "--optimize-meshes")) {
  424. fill.ppFlags |= aiProcess_OptimizeMeshes;
  425. }
  426. else if (! strcmp( param, "-db") || ! strcmp( param, "--debone")) {
  427. fill.ppFlags |= aiProcess_Debone;
  428. }
  429. else if (! strcmp( param, "-sbc") || ! strcmp( param, "--split-by-bone-count")) {
  430. fill.ppFlags |= aiProcess_SplitByBoneCount;
  431. }
  432. else if (!strcmp(param, "-embtex") || ! strcmp(param, "--embed-textures")) {
  433. fill.ppFlags |= aiProcess_EmbedTextures;
  434. }
  435. else if (!strcmp(param, "-gs") || ! strcmp(param, "--global-scale")) {
  436. fill.ppFlags |= aiProcess_GlobalScale;
  437. }
  438. else if (! strncmp( param, "-c",2) || ! strncmp( param, "--config=",9)) {
  439. const unsigned int ofs = (params[i][1] == '-' ? 9 : 2);
  440. // use default configurations
  441. if (!strncmp( param + ofs, "full", 4 )) {
  442. fill.ppFlags |= aiProcessPreset_TargetRealtime_MaxQuality;
  443. } else if (!strncmp( param + ofs, "default", 7 )) {
  444. fill.ppFlags |= aiProcessPreset_TargetRealtime_Quality;
  445. } else if (! strncmp( param +ofs,"fast",4)) {
  446. fill.ppFlags |= aiProcessPreset_TargetRealtime_Fast;
  447. }
  448. } else if (! strcmp( param, "-l") || ! strcmp( param, "--show-log")) {
  449. fill.showLog = true;
  450. }
  451. else if (! strcmp( param, "-v") || ! strcmp( param, "--verbose")) {
  452. fill.verbose = true;
  453. }
  454. else if (!strncmp(params[i], "-rx=", 4) || !strncmp(params[i], "--rotation-x=", 13)) {
  455. std::string value = std::string(params[i] + (params[i][1] == '-' ? 13 : 4));
  456. fill.rot.x = std::stof(value);
  457. }
  458. else if (!strncmp(params[i], "-ry=", 4) || !strncmp(params[i], "--rotation-y=", 13)) {
  459. std::string value = std::string(params[i] + (params[i][1] == '-' ? 13 : 4));
  460. fill.rot.y = std::stof(value);
  461. }
  462. else if (!strncmp(params[i], "-rz=", 4) || !strncmp(params[i], "--rotation-z=", 13)) {
  463. std::string value = std::string(params[i] + (params[i][1] == '-' ? 13 : 4));
  464. fill.rot.z = std::stof(value);
  465. }
  466. else if (! strncmp( param, "--log-out=",10) || ! strncmp( param, "-lo",3)) {
  467. fill.logFile = std::string(params[i]+(params[i][1] == '-' ? 10 : 3));
  468. if (!fill.logFile.length()) {
  469. fill.logFile = "assimp-log.txt";
  470. }
  471. }
  472. }
  473. if (fill.logFile.length() || fill.showLog || fill.verbose) {
  474. fill.log = true;
  475. }
  476. return AssimpCmdError::Success;
  477. }
  478. // ------------------------------------------------------------------------------
  479. int Assimp_TestBatchLoad (
  480. const char* const* params,
  481. unsigned int num)
  482. {
  483. for(unsigned int i = 0; i < num; ++i) {
  484. globalImporter->ReadFile(params[i],aiProcessPreset_TargetRealtime_MaxQuality);
  485. // we're totally silent. scene destructs automatically.
  486. }
  487. return AssimpCmdError::Success;
  488. }