loaderFileTypeAssimp.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file loaderFileTypeAssimp.cxx
  10. * @author rdb
  11. * @date 2011-03-29
  12. */
  13. #include "loaderFileTypeAssimp.h"
  14. #include "config_assimp.h"
  15. #include "assimpLoader.h"
  16. #include <assimp/cimport.h>
  17. using std::string;
  18. TypeHandle LoaderFileTypeAssimp::_type_handle;
  19. /**
  20. *
  21. */
  22. LoaderFileTypeAssimp::
  23. LoaderFileTypeAssimp() {
  24. }
  25. /**
  26. *
  27. */
  28. LoaderFileTypeAssimp::
  29. ~LoaderFileTypeAssimp() {
  30. }
  31. /**
  32. *
  33. */
  34. string LoaderFileTypeAssimp::
  35. get_name() const {
  36. return "Assimp Importer";
  37. }
  38. /**
  39. *
  40. */
  41. string LoaderFileTypeAssimp::
  42. get_extension() const {
  43. return "";
  44. }
  45. /**
  46. * Returns a space-separated list of extension, in addition to the one
  47. * returned by get_extension(), that are recognized by this converter.
  48. */
  49. string LoaderFileTypeAssimp::
  50. get_additional_extensions() const {
  51. // This may be called at static init time, so ensure it is constructed now.
  52. static ConfigVariableString assimp_disable_extensions
  53. ("assimp-disable-extensions", "",
  54. PRC_DESC("A list of extensions (without preceding dot) that should not be "
  55. "loaded via the Assimp loader, even if Assimp supports these "
  56. "formats. It is useful to set this for eg. gltf and glb files "
  57. "to prevent them from being accidentally loaded via the Assimp "
  58. "plug-in instead of via a superior plug-in like panda3d-gltf."));
  59. bool has_disabled_exts = !assimp_disable_extensions.empty();
  60. aiString aexts;
  61. aiGetExtensionList(&aexts);
  62. char *buffer = (char *)alloca(aexts.length + 2);
  63. char *p = buffer;
  64. // The format is like: *.mdc;*.mdl;*.mesh.xml;*.mot
  65. char *sub = strtok(aexts.data, ";");
  66. while (sub != nullptr) {
  67. bool enabled = true;
  68. if (has_disabled_exts) {
  69. for (size_t i = 0; i < assimp_disable_extensions.get_num_words(); ++i) {
  70. std::string disabled_ext = assimp_disable_extensions.get_word(i);
  71. if (strcmp(sub + 2, disabled_ext.c_str()) == 0) {
  72. enabled = false;
  73. break;
  74. }
  75. }
  76. }
  77. if (enabled) {
  78. *(p++) = ' ';
  79. size_t len = strlen(sub + 2);
  80. memcpy(p, sub + 2, len);
  81. p += len;
  82. }
  83. sub = strtok(nullptr, ";");
  84. }
  85. // Strip first space
  86. ++buffer;
  87. return std::string(buffer, p - buffer);
  88. }
  89. /**
  90. * Returns true if this file type can transparently load compressed files
  91. * (with a .pz or .gz extension), false otherwise.
  92. */
  93. bool LoaderFileTypeAssimp::
  94. supports_compressed() const {
  95. return true;
  96. }
  97. /**
  98. *
  99. */
  100. PT(PandaNode) LoaderFileTypeAssimp::
  101. load_file(const Filename &path, const LoaderOptions &options,
  102. BamCacheRecord *record) const {
  103. assimp_cat.info()
  104. << "Reading " << path << "\n";
  105. AssimpLoader loader;
  106. loader.local_object();
  107. if (!loader.read(path)) {
  108. return nullptr;
  109. }
  110. loader.build_graph();
  111. return DCAST(PandaNode, loader._root);
  112. }