Hi,
Just encountered this same problem with the 3DS loader. I want to load lots of the same model file and have them all over my scene. So I setup a hashmap to cache previously loaded models and I get them from there if they have already been loaded once.
But doing this resulted in an exception:
com.xith3d.scenegraph.IllegalSceneGraphOperation: Illegal attempt to set the parent of this Node blah blah
So I tried to use sharedCopy to get copies of my model, yet I get an exception when casting from the sharedCopy call to a TDSModel. Casting to a ‘plain’ BranchGroup works fine, BUT I can’t use any of the features of the TDSModel class (e.g. animation) that I require.
Here is the working code from my ModelLoader class
public class ModelLoader {
static HashMap cache = new HashMap(20);
static BranchGroup load(String model_file)
{
if(cache.containsKey(model_file)) {
TDSModel mdl = (TDSModel)cache.get(model_file);
return (BranchGroup)mdl.sharedCopy(mdl);
} else {
try {
FileInputStream fis = new FileInputStream("features/"+model_file+".3ds");
TDSModel model = new TDSLoader().load("/features/", fis, true);
cache.put(model_file, model);
return model;
} catch (IOException e) {
System.out.println("Error loading 3DS model '"+model_file+".3ds'");
return null;
}
}
}
}
If I change the line
return (BranchGroup)mdl.sharedCopy(mdl);
to
return (TDSModel)mdl.sharedCopy(mdl);
I get the ClassCastException…
Help! Any ideas as I REALLY need to cache my models like this, it geatly improves load time and memory usage (I noticed at least 16mb was saved in one small test)
Ben