Browse Source

assimp: don't reuse Importer object when loading multiple models

This works around a crash in v4.1.0 when loading multiple .ply files in sequence.  (I have not reproduced the crash with the latest Assimp master.)
rdb 6 years ago
parent
commit
8ba6a0d844
1 changed files with 25 additions and 10 deletions
  1. 25 10
      pandatool/src/assimp/loaderFileTypeAssimp.cxx

+ 25 - 10
pandatool/src/assimp/loaderFileTypeAssimp.cxx

@@ -15,6 +15,8 @@
 #include "config_assimp.h"
 #include "assimpLoader.h"
 
+#include <assimp/cimport.h>
+
 using std::string;
 
 TypeHandle LoaderFileTypeAssimp::_type_handle;
@@ -23,7 +25,7 @@ TypeHandle LoaderFileTypeAssimp::_type_handle;
  *
  */
 LoaderFileTypeAssimp::
-LoaderFileTypeAssimp() : _loader(new AssimpLoader) {
+LoaderFileTypeAssimp() : _loader(nullptr) {
 }
 
 /**
@@ -31,9 +33,6 @@ LoaderFileTypeAssimp() : _loader(new AssimpLoader) {
  */
 LoaderFileTypeAssimp::
 ~LoaderFileTypeAssimp() {
-  if (_loader != nullptr) {
-    delete _loader;
-  }
 }
 
 /**
@@ -58,9 +57,22 @@ get_extension() const {
  */
 string LoaderFileTypeAssimp::
 get_additional_extensions() const {
-  string exts;
-  _loader->get_extensions(exts);
-  return exts;
+  aiString aexts;
+  aiGetExtensionList(&aexts);
+
+  // The format is like: *.mdc;*.mdl;*.mesh.xml;*.mot
+  std::string ext;
+  char *sub = strtok(aexts.data, ";");
+  while (sub != nullptr) {
+    ext += sub + 2;
+    sub = strtok(nullptr, ";");
+
+    if (sub != nullptr) {
+      ext += ' ';
+    }
+  }
+
+  return ext;
 }
 
 /**
@@ -82,10 +94,13 @@ load_file(const Filename &path, const LoaderOptions &options,
   assimp_cat.info()
     << "Reading " << path << "\n";
 
-  if (!_loader->read(path)) {
+  AssimpLoader loader;
+  loader.local_object();
+
+  if (!loader.read(path)) {
     return nullptr;
   }
 
-  _loader->build_graph();
-  return DCAST(PandaNode, _loader->_root);
+  loader.build_graph();
+  return DCAST(PandaNode, loader._root);
 }