Cleaning a scene

Hi all,



I come back on an existing subject also see in the post below



http://hub.jmonkeyengine.org/groups/general-2/forum/topic/clearing-a-scene/



My issue is that I cannot completly clean my scene using game state detach function.

I clean lights, detach children, remove the main view and clear queue but all objects always appear on render. Any idea on what I missed on my code.







[java]public void stateDetached() {

super.stateDetached();



BBSceneManager.getInstance().destroy();



human.detachAllChildren();

human.removeFromParent();

human.getWorldLightList().clear();

human.getLocalLightList().clear();



humanStalker.detachAllChildren();

humanStalker.removeFromParent();

humanStalker.getWorldLightList().clear();

humanStalker.getLocalLightList().clear();



ndmd.detachAllChildren();

ndmd.removeFromParent();

ndmd.getWorldLightList().clear();

ndmd.getLocalLightList().clear();



physicsModels.detachAllChildren();

physicsModels.removeFromParent();

physicsModels.getWorldLightList().clear();

physicsModels.getLocalLightList().clear();



BBSceneManager.getInstance().getViewPort().clearScenes();



this.engineSystem.getRenderManager().clearQueue(BBSceneManager.getInstance().getViewPort());

this.engineSystem.getRenderManager().removeMainView(“TEST”);

}[/java]

idk if rootNode.detachAllChildren(); will work, cus the physics shapes will be there i think.

But individually detach the object, remove any physics objects attached to them and then set the spatial to null.

[java]BBSceneManager.getInstance().destroy();[/java]



This call detach rootnode object and I also set it to null



[java]

public void destroy(){

rootNode.getWorldLightList().clear();

rootNode.getLocalLightList().clear();

rootNode.detachAllChildren();

rootNode.removeFromParent();

rootNode = null;

}[/java]



I set the spatial to null but no more effect.

I made a basic example with only one model “Sinbad” with light and sky.

But the cleaning the scene seems not to work



Any help !!



[java]sky = SkyFactory.createSky(assetManager, “Textures/sky/skysphere.jpg”, true);

al = new AmbientLight();

al.setColor(ColorRGBA.White.mult(1.3f));

rootNode.addLight(al);

DirectionalLight dl = new DirectionalLight();

dl.setColor(ColorRGBA.White);

dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());



human = new Node(“human”);



player = assetManager.loadModel(“Models/Sinbad/Sinbad.mesh.j3o”);

Spatial sword = assetManager.loadModel(“Models/Sinbad/Sword/Sword.mesh.j3o”);

human.attachChild(player);

human.attachChild(sword);



//Attach to scene root node

rootNode.addLight(dl);

rootNode.attachChild(sky);

rootNode.attachChild(human);





//Try to clean scene



human.getLocalLightList().clear();

human.getWorldLightList().clear();

human.detachAllChildren();

human.removeFromParent();





player.getLocalLightList().clear();

player.getWorldLightList().clear();

player.removeFromParent();

player.getWorldLightList().clear();



sky.getLocalLightList().clear();

sky.getWorldLightList().clear();

sky.removeFromParent();



rootNode.getLocalLightList().clear();

rootNode.getWorldLightList().clear();

rootNode.detachAllChildren();

rootNode.removeFromParent();[/java]

rootNode.getLocalLightList().clear(); ← WTF

This won’t work, what you do is clear a list and not remove every single light. Basically what you do is you don’t detach anything you just make sure the system doesn’t know its there anymore :roll:

Even if I use “detachAllChildren()” and set the nodes or spatials to “null” nothing happen !!



How can I clean or suppress object in scene ?

You definitely do it wrong there, especially with the lights.

Sorry, It is not clear for me.



I set all variable to null. It suppose to be deleted right ?

No, You remove a light from a node with removeLight. When you got a variable, “Node node = new Node();” and then say “rootNode.attachChild(node);” the node doesn’t suddenly get detached from rootNode by itself when you go “node = null;” You only set the variable reference to null.

Even if I use removeLight and set the variable to null. Nothing happen.



Any other suggestions ?

Just do what you did to attach and add things in reverse, so probably:

[java]rootNode.removeLight(dl);

rootNode.detachChild(sky);

rootNode.detachChild(human);

physicsSpace.remove(human);

[/java]