Main.cpp 18 KB

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