Found bug in JME XMLExporter, Vector3f.ZERO abuse!

Hey Jmonkeys,
Our team is working on a model editor based on JME and we are using the XMLExporter to create the project files.
The XMLExporter uses DOMOutputCapsule to write each Savable object, in particular Vector3f instances.
When a Spatial is saved, the “transform” object is saved and if its “translation” (vector3f instance) is (0,0,0), it isn’t written in the XML output (I suppose to save space).
The problem happens when we read the XML file to load the saved project:
The XMLImporter uses DOMInputCapsule to read the Spatial and create the “transform”, but it won’t find any “translation” vector so it will load the default one, which is Vector3f.ZERO. You can see this in com.jme3.math.Transform#read:

translation = (Vector3f)capsule.readSavable("translation", Vector3f.ZERO);

This behaviour is heavily wrong as every Spatial will have the same translation vector (ie. Vector3f.ZERO) and will cause several bugs as Vector3f.ZERO mustn’t be edited at all.

The solution is pretty easy, just replace the previous line with:

translation = (Vector3f)capsule.readSavable("translation", new Vector3f(0,0,0));

or maybe you can also use Vector3f.ZERO.clone().

Until the official fix, our workaround is a field reflection.

Hope to be helpful,
Axel

1 Like
translation = (Vector3f)capsule.readSavable("translation", null);
if( translation == null ) {
    translation = new Vector3rf();
}

…would save creating garbage objects just to throw them away in most cases.

Thanks for the report gonn atake care of it

Continuing the discussion from Found bug in JME XMLExporter, Vector3f.ZERO abuse!:

Yeah even better. It should be done in all places where Vector3f.ZERO is being used :smile:

Wonderful, happy to help the JME project!

That’s fixed on master
Thanks