> From: Andreea Francu <[EMAIL PROTECTED]>
> .. when I'm adding, let's say hand.wrl
> file and then flag.wrl file and then again hand.wrl file, then everything
> works fine and I get no error. When I'm trying to add hand.wrl and then
> again hand.wrl I get that error with multiple parents. I was looking for a
> way to avoid this problem. I still don't know how
> I tried to say detach, like you suggested, but then the branchgroup that
> I'm detaching will disappear and I don't want that.
The problem is that you'd like to have hand.wrl appear more than once in your
scene graph, but in J3D an BranchGroup can only appear once (and thus it can
only have one parent).
The simplest solution is to hand.wrl into a SharedGroup and then you can have as
many Links to the SharedGroup as you'd like:
BranchGroup handGroup = scene.getSceneGroup();
SharedGroup sharableHandGroup = new SharedGroup();
sharableHandGroup.addChild(handGroup);
Link firstReference = new Link(sharableHandGroup);
Link secondReference = new Link(shareableHandGroup);
Both firstReference and secondReference can appear a scene graph without getting
a multiple parent exception.
This solution might not work because hand.wrl contains a LOD or Billboard node
(or some other behavior node), so that it can't appear in a SharedGroup.
The more general solution (which works even for graphs with behaviors) is to
clone the tree to produce multiple independent references:
BranchGroup handGroup = scene.getSceneGroup();
BranchGroup handGroup2 = (BranchGroup) handGroup.cloneTree();
By default, this will generate clone of the original graph with references to
the NodeComponents in the original graph. This means the node components (like
the geometry) will be shared between the graphs so that the clone will require
only a fraction of the original memory (most of the scene graph memory is taken
up by the geometry).
Finally, if you create your VRML file with a DEF name for the hand, then you can
use VrmlScene.getNamedObjects() to get a subgraph for the hand. This method
will automatically manage the Link/SharedGroup vs cloneTree() issue and return
either a Link or a clone of the original subgraph.
Doug Gehringer
Sun Microsystems