Node.sharedCopy(..)

How would I make the sharedCopy method to work for a class which I’ve extended on TransformGroup?
So let’s say there’s a class TransMyGroup which extends TransformGroup. Now what I would like to do is get a clone of this class, if possible with the sharing of geometry and appearance like the sharedCopy does…
But when I do


TransMyGroupe org;
TransMyGroupe clon = (TransMyGroup) org.sharedCopy(org);

a ClassCastException is being thrown. Because the sharedCopy method has some hardcoded node types inside…
Is there a way to get a (shared) copy?

By the way: how do you make a real 1:1 copy of a Node, ie without anything being shared?

Anybody with an extended TransformGroup class would like to clone or sharecopy his extended class…? :slight_smile:

I had the same problem, and just decided to create new instances of my class that extends transformgroup, for now. But, my geometry and appearance objects are static, so (I think) they are automatically shared by all the instances and probably equivalent to using shareCopy. (Correct me if I’m wrong)

But would it be enought to override the sharedCopy method in your subclass to something like?:


TransformGroup tg = super.sharedCopy( (TransformGroup)this);
return new TransMyGroup(tg);

I don’t understand why you got the exception. Trying to cast TransMyGroup to TransformGroup shouldn’t throw one.

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

Here is the code I use:

character is a BranchGroup. Modelstore is a static class containing a hashmap just like you do.


TDSModel model = ModelStore.getModel(modelId);
                  
character = (BranchGroup) model.sharedCopy(model);

Thanks, but thats what I’m currenty doing (casting to a BranchGroup) and it’s all working fine.

But the issue is - I then have a BranchGroup not a TDSModel, so I can not use any of the TDSModel methods (e.g. setCurrentFrame for animation)

I need to cast it to TDSModel not BranchGroup.

I used a workaround to deep copy the TransformGroup. The code is similar to Xith’s sharedCopy but I hardwire the needed TransformGroup extension class. So I end up in just calling sharedCopy on Shape3D objects and it works.
{Edit: not for TDSModel, just for an own TransformGroup extension}

Bombadil,

Would it be possible to post or send me your code?

I thought I had a work around too, but my animations are always animating the first model loaded not the cached copies.

This is driving me nuts, yet again this seems a very common problem that lots of people must have encountered?!

[quote]I thought I had a work around too, but my animations are always animating the first model loaded not the cached copies.
[/quote]
I used my mentioned method with a class extending TransformGroup, not with TDSModel.