Main.cpp 18 KB

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