Sorry to be back again to this topic. Maybe I am doing something very wrong trying to export a node with more than just one user data array. Maybe I fail to see my mistake in my code.
When I add more arrays as user data to a node, then I am not able to load all the arrays again. It worked with just one array. But when I set two arrays as user data in one node, then loading the node gives me an exception (java.lang.ArrayIndexOutOfBoundsException). One array is loaded correct, the other array is null (I would say because of the exception). Both keys are exported and loaded correct.
I am using the same code from above, just a little bit modified:
public class Main extends SimpleApplication {
private final static Integer[] SOME_INTEGER = {16, 17, 19, 22, 26, 29, 31, 32, 30, 27, 23, 19};
public final static Float[] SOME_FLOAT = {153f, 156f, 97f, 51f, 17f, 3f, 0f, 0f, 7f, 46f, 130f, 186f};
private Node node;
public static void main(String[] args){
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
Box b = new Box(1, 1, 1);
Geometry geom = new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geom.setMaterial(mat);
node = new Node("Node");
node.setUserData("MyIntegerArray", SOME_INTEGER);
node.setUserData("MyFloatArray", SOME_FLOAT);
rootNode.attachChild(node);
node.attachChild(geom);
for (String key : node.getUserDataKeys()) {
Object[] someInteger = node.getUserData(key);
System.out.println("KEY: " + key);
for (Object object : someInteger) {
System.out.println("ARRAY_VALUE: " + object);
}
}
// loadFile();
}
@Override
public void stop() {
String userHome = System.getProperty("user.home");
BinaryExporter exporter = BinaryExporter.getInstance();
File file = new File(userHome+"/Models/"+"MyModel.j3o");
try {
exporter.save(node, file);
} catch (IOException ex) {
ex.printStackTrace();
}
super.stop();
}
private void loadFile() {
String userHome = System.getProperty("user.home");
assetManager.registerLocator(userHome, FileLocator.class);
node = (Node)assetManager.loadModel("Models/MyModel.j3o");
rootNode.attachChild(node);
for (String key : node.getUserDataKeys()) {
Object[] anArray = node.getUserData(key);
System.out.println("KEY: " + key);
for (Object object : anArray) {
System.out.println("ARRAY_VALUE: " + object);
}
}
}
}
After I set the user data in the node I also checked if they are set by printing all the user data. At this moment everything is correct. Just after exporting and then loading the node again, one array is null. The java.lang.ArrayIndexOutOfBoundsException comes from line
node = (Node)assetManager.loadModel("Models/MyModel.j3o");
When I print the values of the array after loading, then one array is there correct and the other array is null.
The exception looks like this (line 64 is the line from above):
Okt. 17, 2024 9:21:30 NACHM. class com.jme3.export.binary.BinaryImporter readObject(int id)
SCHWERWIEGEND: Exception
java.lang.ArrayIndexOutOfBoundsException: Index -18 out of bounds for length 4
at com.jme3.export.binary.ByteUtils.rightAlignBytes(ByteUtils.java:483)
at com.jme3.export.binary.BinaryInputCapsule.readInt(BinaryInputCapsule.java:834)
at com.jme3.export.binary.BinaryInputCapsule.setContent(BinaryInputCapsule.java:154)
at com.jme3.export.binary.BinaryImporter.readObject(BinaryImporter.java:340)
at com.jme3.export.binary.BinaryInputCapsule.resolveIDs(BinaryInputCapsule.java:510)
at com.jme3.export.binary.BinaryInputCapsule.readStringSavableMap(BinaryInputCapsule.java:700)
at com.jme3.scene.Spatial.read(Spatial.java:1665)
at com.jme3.scene.Node.read(Node.java:776)
at com.jme3.export.binary.BinaryImporter.readObject(BinaryImporter.java:345)
at com.jme3.export.binary.BinaryImporter.load(BinaryImporter.java:245)
at com.jme3.export.binary.BinaryImporter.load(BinaryImporter.java:128)
at com.jme3.export.binary.BinaryImporter.load(BinaryImporter.java:112)
at com.jme3.export.binary.BinaryLoader.load(BinaryLoader.java:36)
at com.jme3.asset.DesktopAssetManager.loadLocatedAsset(DesktopAssetManager.java:272)
at com.jme3.asset.DesktopAssetManager.loadAsset(DesktopAssetManager.java:388)
at com.jme3.asset.DesktopAssetManager.loadModel(DesktopAssetManager.java:439)
at com.jme3.asset.DesktopAssetManager.loadModel(DesktopAssetManager.java:444)
at com.neolithicrevolution.main.Main.loadFile(Main.java:64)
at com.neolithicrevolution.main.Main.simpleInitApp(Main.java:45)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:240)
at com.jme3.system.lwjgl.LwjglWindow.initInThread(LwjglWindow.java:607)
at com.jme3.system.lwjgl.LwjglWindow.run(LwjglWindow.java:710)
at java.base/java.lang.Thread.run(Thread.java:829)