Main.cpp 17 KB

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