Cameras and lights wrapped into nodes

Hi,



this post is just to inform blender loader users (heard rumours that they really exist :smiley: ) that Camera and Light had been wrapped into CameraNode and LightNode. So you might get some errors in your code. And do not be surprised to get a ClassCastException if you were expecting Light or Camera so far :wink:



This change gives us the following features:

  • camera nodes have name
  • camera and light nodes receive the properties if they are set in blender
  • the nodes will allow to add light and camera constraints in the nearby future



    Hope the change will not cause many troubles.



    Cheers,

    Kaelthas
5 Likes
@Kaelthas said:
- camera nodes have name

ha yes. That's another benefit i didn't foresee.
Nice work Marcin ;)

Thanks, this is exactly how it should work :slight_smile:

@Kaelthas said:
this post is just to inform blender loader users (heard rumours that they really exist :D ) that Camera and Light had been wrapped into CameraNode and LightNode. So you might get some errors in your code.


Looks like lights are never actually added now, the LightNode is added but addLight is never called. So everything is black :)

This patch fixes it.

[patch]Index: src/blender/com/jme3/scene/plugins/blender/AbstractBlenderLoader.java
===================================================================
--- src/blender/com/jme3/scene/plugins/blender/AbstractBlenderLoader.java (revision 9710)
+++ src/blender/com/jme3/scene/plugins/blender/AbstractBlenderLoader.java (working copy)
@@ -83,8 +83,9 @@
Object object = this.toObject(objectStructure);
if(object instanceof Spatial && ((Spatial) object).getParent()==null) {
result.attachChild((Spatial) object);
- } else if(object instanceof Light) {
- result.addLight((Light)object);
+ }
+ if(object instanceof LightNode) {
+ result.addLight( ((LightNode) object).getLight() );
}
}
}
Index: src/blender/com/jme3/scene/plugins/blender/BlenderModelLoader.java
===================================================================
--- src/blender/com/jme3/scene/plugins/blender/BlenderModelLoader.java (revision 9710)
+++ src/blender/com/jme3/scene/plugins/blender/BlenderModelLoader.java (working copy)
@@ -34,6 +34,7 @@
import com.jme3.asset.AssetInfo;
import com.jme3.asset.BlenderKey;
import com.jme3.light.Light;
+import com.jme3.scene.LightNode;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.plugins.blender.exceptions.BlenderFileException;
@@ -66,8 +67,9 @@
if (((Node) object).getParent() == null) {
modelRoot.attachChild( (Node) object );
}
- }else if (object instanceof Light){
- modelRoot.addLight( (Light) object );
+ if (object instanceof LightNode) {
+ modelRoot.addLight( ((LightNode) object).getLight() );
+ }
}
}
}
[/patch]
4 Likes

Yeah I know, I’ve also spotted this yesterday.

I made a fix yesterday so I belive it should work fine with todays nightly build.



If it’s still not working please let me know.

@Kaelthas said:
If it's still not working please let me know.


Thanks, that fixed it.

@Kaelthas I found another issue. When I convert the blend to j3o, the light transformations are all reset to identity and so they are all in the wrong place when the j3o is loaded.



LightNode does not save/load its LightControl, and LightControl does not save/load its Light. So in the SDK “Convert to j3o binary” saves the transformed LightNode and the untransformed (identity transform) Light, and the LightControl is lost so when viewing the j3o the light is never transformed back to its proper position.



I tried adding read/write to LightNode to load/save the LightControl, and also added read/write of the Light to LightControl, but am having problems getting it working.

@rectalogic said:
I tried adding read/write to LightNode to load/save the LightControl, and also added read/write of the Light to LightControl, but am having problems getting it working.


OK, I got this working. I also did the same thing for CameraNode/CameraControl.

[patch]Index: src/core/com/jme3/scene/LightNode.java
===================================================================
--- src/core/com/jme3/scene/LightNode.java (revision 9716)
+++ src/core/com/jme3/scene/LightNode.java (working copy)
@@ -31,9 +31,12 @@
*/
package com.jme3.scene;

+import com.jme3.export.JmeExporter;
+import com.jme3.export.JmeImporter;
import com.jme3.light.Light;
import com.jme3.scene.control.LightControl;
import com.jme3.scene.control.LightControl.ControlDirection;
+import java.io.IOException;

/**
* <code>LightNode</code> is used to link together a {@link Light} object
@@ -90,4 +93,16 @@
public Light getLight() {
return lightControl.getLight();
}
+
+ @Override
+ public void read(JmeImporter im) throws IOException {
+ super.read(im);
+ lightControl = (LightControl)im.getCapsule(this).readSavable("lightControl", null);
+ }
+
+ @Override
+ public void write(JmeExporter ex) throws IOException {
+ super.write(ex);
+ ex.getCapsule(this).write(lightControl, "lightControl", null);
+ }
}
Index: src/core/com/jme3/scene/control/LightControl.java
===================================================================
--- src/core/com/jme3/scene/control/LightControl.java (revision 9716)
+++ src/core/com/jme3/scene/control/LightControl.java (working copy)
@@ -31,8 +31,10 @@
*/
package com.jme3.scene.control;

+import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
+import com.jme3.export.OutputCapsule;
import com.jme3.light.DirectionalLight;
import com.jme3.light.Light;
import com.jme3.light.PointLight;
@@ -173,18 +175,22 @@
return control;
}
private static final String CONTROL_DIR_NAME = "controlDir";
+ private static final String LIGHT_NAME = "light";

@Override
public void read(JmeImporter im) throws IOException {
super.read(im);
- im.getCapsule(this).readEnum(CONTROL_DIR_NAME,
- ControlDirection.class, ControlDirection.SpatialToLight);
+ InputCapsule ic = im.getCapsule(this);
+ controlDir = ic.readEnum(CONTROL_DIR_NAME,
+ ControlDirection.class, ControlDirection.SpatialToLight);
+ light = (Light)ic.readSavable(LIGHT_NAME, null);
}

@Override
public void write(JmeExporter ex) throws IOException {
super.write(ex);
- ex.getCapsule(this).write(controlDir, CONTROL_DIR_NAME,
- ControlDirection.SpatialToLight);
+ OutputCapsule oc = ex.getCapsule(this);
+ oc.write(controlDir, CONTROL_DIR_NAME, ControlDirection.SpatialToLight);
+ oc.write(light, LIGHT_NAME, null);
}
}
No newline at end of file
Index: src/core/com/jme3/scene/CameraNode.java
===================================================================
--- src/core/com/jme3/scene/CameraNode.java (revision 9716)
+++ src/core/com/jme3/scene/CameraNode.java (working copy)
@@ -31,9 +31,12 @@
*/
package com.jme3.scene;

+import com.jme3.export.JmeExporter;
+import com.jme3.export.JmeImporter;
import com.jme3.renderer.Camera;
import com.jme3.scene.control.CameraControl;
import com.jme3.scene.control.CameraControl.ControlDirection;
+import java.io.IOException;

/**
* <code>CameraNode</code> simply uses {@link CameraControl} to implement
@@ -85,6 +88,17 @@
return camControl.getCamera();
}

+ @Override
+ public void read(JmeImporter im) throws IOException {
+ super.read(im);
+ camControl = (CameraControl)im.getCapsule(this).readSavable("camControl", null);
+ }
+
+ @Override
+ public void write(JmeExporter ex) throws IOException {
+ super.write(ex);
+ ex.getCapsule(this).write(camControl, "camControl", null);
+ }
// @Override
// public void lookAt(Vector3f position, Vector3f upVector) {
// this.lookAt(position, upVector);
Index: src/core/com/jme3/scene/control/CameraControl.java
===================================================================
--- src/core/com/jme3/scene/control/CameraControl.java (revision 9716)
+++ src/core/com/jme3/scene/control/CameraControl.java (working copy)
@@ -31,8 +31,10 @@
*/
package com.jme3.scene.control;

+import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
+import com.jme3.export.OutputCapsule;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
@@ -142,18 +144,22 @@
return control;
}
private static final String CONTROL_DIR_NAME = "controlDir";
+ private static final String CAMERA_NAME = "camera";

@Override
public void read(JmeImporter im) throws IOException {
super.read(im);
- im.getCapsule(this).readEnum(CONTROL_DIR_NAME,
- ControlDirection.class, ControlDirection.SpatialToCamera);
+ InputCapsule ic = im.getCapsule(this);
+ controlDir = ic.readEnum(CONTROL_DIR_NAME,
+ ControlDirection.class, ControlDirection.SpatialToCamera);
+ camera = (Camera)ic.readSavable(CAMERA_NAME, null);
}

@Override
public void write(JmeExporter ex) throws IOException {
super.write(ex);
- ex.getCapsule(this).write(controlDir, CONTROL_DIR_NAME,
- ControlDirection.SpatialToCamera);
+ OutputCapsule oc = ex.getCapsule(this);
+ oc.write(controlDir, CONTROL_DIR_NAME, ControlDirection.SpatialToCamera);
+ oc.write(camera, CAMERA_NAME, null);
}
}
No newline at end of file
[/patch]
4 Likes

You keep fixing our bugs @rectalogic, kudos :wink:

this post is just to inform blender loader users (heard rumours that they really exist :D )


I can confirm that. :D
@normen said:
You keep fixing our bugs @rectalogic, kudos ;)


Thanks. What do you guys think about committing this - does it look OK?

@Kaelthas was working on this stuff right now… Did you add it? :slight_smile:

OK I made a commit :slight_smile:

Thanks for reminding me about it @normen