How to save lights?

Sorry for the total nob question: how can lights be stored? When I use the JMEBinaryExporter and save and load again only the primitive spatials & models objects are saved, but every light is gone. This is the save action: BinaryExporter.getInstance().save(scene,fileChooser.getSelectedFile());

In the scene I attached all different lights to the "global" lightState, unfortunately I haven't figured out how this lightState can be saved together with the scene node.



This would have been the way in my understanding (which is likely to be wrong of course): :expressionless:



DirectionalLight sunlight = new DirectionalLight();



lightState = DisplaySystem.getDisplaySystem().getRenderer().createLightState();



lightState.attachChild(sunlight);



LightNode lightNode = new LightNode();



lightNode.attachChild(lightState); // doesn't work



scene.attach(lightNode);

If you're using a global light state, there's no need to use LightNode.


DirectionalLight sunlight = new DirectionalLight();
...
lightState = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
...
lightState.attachChild(sunlight);
...
scene.setRenderState(lightState); // now it works

edit: sorry, if I explained it badly. I'll make a new try:

Currently I save & load my scene that way:



//Select the file you want to load

scene = (Node)BinaryImporter.getInstance().load(fileChooser.getSelectedFile());



//Save scene-Node

BinaryExporter.getInstance().save(scene,fileChooser.getSelectedFile());



The "scene"-Node contains cubes, spheres, and 3D models. Saving and loading works flawlessly. Now I've added some lights in the scene: 1 directional sunlight and 2 pointlights. I implemented the lights the usual way as follows:


lightState = DisplaySystem.getDisplaySystem().getRenderer().createLightState();

DirectionalLight sunlight = new DirectionalLight(); //... also add some values
PointLight pl1=new PointLight(); //... also add some values
PointLight pl2=new PointLight(); //... also add some values

lightState.attachChild(sunlight);
lightState.attachChild(pl1);
lightState.attachChild(pl2);

scene.setRenderState(lightState);
scene.updateRenderState();


Every light shows nicely when running the code the first time. But when I try to save the scene with the BinaryExporter line posted above and then load the scene again - everything is dark. No lighting anymore. So my question is: where have the lights gone? Is my saving action something missing? Do lights need another way to be saved and loaded?

Ehrm… I somehow solved this problem partially. Since the API claimed that LightState implements Savable… it had to work. I found out that the problem was caused by the fact that I had created the variable "lightState" as a global Variable… but I used it in the wrong class or somehow like that… I don't know meself :? but it works now…



… finally I can go to bed

It still didnt't work. Eventually I found the real problem:



I loaded the scene the wrong way. Everytime I loaded the scene with that code only: "scene = (Node)BinaryImporter.getInstance().load(fileChooser.getSelectedFile());" I somehow "added" a new scene-Node which was somehow added to the former existing scene-node. So this explained 1.) why I had Lights not disappearing when cleaning the scene 2.) on other saved scenes everything just being dark



I could clean it out by 'extracting' all spatials and lights from the loaded Node and attaching all them to the existing scene-Node and now it works:


//Clear previous scene first
scene.detachAllChildren();
lightState.detachAll();
                        
//Select the file you want to load
Node loading = (Node) BinaryImporter.getInstance().load(fileChooser.getSelectedFile());

ArrayList<Spatial> allspats = loading.getChildren();
for(int i=0;i<allspats.size();i++)
{
   scene.attachChild(allspats.get(i));               
}

RenderState rs = loading.getRenderState(RenderState.RS_LIGHT);
//System.out.println(rs); // prints: com.jme.scene.state.lwjgl.LWJGLLightState@5dcec6

// cast received RenderState to a LightState
LightState lei = (LightState)rs;
System.out.println(lei.getQuantity());   // prints how many lights

for(int i=0;i<lei.getQuantity();i++)
{
   Light light = lei.get(i);
   lightState.attach(light);
}



I still wonder though if there is another way to apply the loaded sceneNode to an existing sceneNode? (applying in a way that RenderStates and Spatials and everything is attached to it - but without "adding" the Node itself to that scene (which caused my problems)?