OBJ export mirrored?

Hello,

I ran into this issue today, please let me know if this is a potential bug or a misusage of Babylon.js API :wink: .

Consider this very simple demo playground (forked it from another issue):

https://www.babylonjs-playground.com/#MCJYB5#20

From this playground, Iā€™m exporting a OBJ file with BABYLON.OBJExport.OBJ and also a GLB file using the inspector and ā€œexport glbā€ button

This gives me those two files:

Then I import those files into Blender I get this

GLB file (correct)

Screenshot 2020-05-14 at 09.30.22

OBJ file ( mirrored)

Screenshot 2020-05-14 at 09.29.58

Both on the same canvas

It looks like some magic transform settings I need to setā€¦ tried to play around with things but couldnā€™t make the GLB export match the OBJ export.

Any ideas ?

Thanks

Hey there.
I stumbled accross a related issure when importing .obj into the sandbox:


Maybe they are related?
Lets wait for some input from the devs.

@tdurand @mamu

OBJ is a right handed format. I believe the inversion youā€™re seeing is due to our mesh being exported from babylonā€™s left handed system to OBJ, then imported into blenderā€™s right handed system. even if we align the proper forward, and up vector, then our right vector is inverted. thatā€™s not rightā€¦ Iā€™ll try to fix that.

I imagine if the OBJ is exported from a right handed babylon scene, then we would not see this inversionā€¦

Actually after further investigation, iā€™m not sure if thatā€™s the case. see:
https://playground.babylonjs.com/#HDUI2D#4

imported into Blender with default settings we see:


Which is incorrect, but thatā€™s because weā€™re using the wrong settings.

Babylon, like Unity is a left handed coordinate system with +Z forward, +Y up and +X right. changing the import settings to:


We should instead get:

This seems to work regardless of export from babylon using global or local coordinates and scene handednessā€¦Iā€™m not sure if any fixes need to be made hereā€¦

Hi

Thanks for looking into this. I tried to play with the -Z + Z options when importing in blender but Iā€™m not able to have the same models. Seems the obj is Mirrored on the X axis

Iā€™ve made up a ā€œnot symetricalā€ scene so you can reproduce what I mean.

Consider this playground with a Terrain mesh generated from height map

https://www.babylonjs-playground.com/#IFYDRS#103

Then this is exported as OBJ and GLB:

Then in blender I get his

For GLB file with +Z forward:

For OBJ file import with +Z forward:

Thanks !

Ah, I see what you mean.

Using this PG:
https://www.babylonjs-playground.com/#IFYDRS#104

If you change the scene handedness to right handed, the OBJ and GLB export are identical, as would be expected. Otherwise, if we use a left handed system, the OBJ and GLB export are mirrored along the X axis. When imported to Blender with default settingsā€¦not great.

Would you be interested in delivering a fix to the OBJ exporter? I expect we would just need to add an additional parameter to the OBJSerializer.OBJ that represents whether or not to convert our model geometry to right handed coordinates, and invert the geometry X position as a result.

Hi

Yes I would be interested in trying to work on this over the week-end, could you give two pointers (sorry I didnā€™t looked up the documentation as Iā€™m on my mobile and also Iā€™m not an expert with Babylon.js and 3D)

Many Thanks

Thibault

Ok I was able to do what you said by using

scene.useRightHandedSystem = true;

Now Iā€™m looking into fixing the OBJ Exporter depending on whether this flag is true or false

I would need some help with thisā€¦ Iā€™m trying to figure out how to flip the geometry along the X axis ā€¦

Iā€™ve tried to do this:

// invert along X axis
var inversionAlongXAxisMatrix = Matrix.FromValues(
                -1, 0, 0, 0,
                0, 1, 0, 0,
                0, 0, 1, 0,
                0, 0, 0, 1)
mesh[j].bakeTransformIntoVertices(inversionAlongXAxisMatrix);

Inside the OBJ serializerā€¦ but the export is still incorrectā€¦ Any idea ?

Thanks and have a nice week-end

Try modifying the vertices immediately before exporting to our OBJ:

Where we may want to invert trunkVerts[i] (@L70) depending on the scene handedness

Ok, it does work !

Iā€™ve started a pull request: Fix OBJ serializing mirrored by tdurand Ā· Pull Request #8216 Ā· BabylonJS/Babylon.js Ā· GitHub :wink:

1 Like

Thanks, Iā€™ll test your fix before signing off

1 Like

Marking this as solved, the PR was merged : Fix OBJ serializing mirrored by tdurand Ā· Pull Request #8216 Ā· BabylonJS/Babylon.js Ā· GitHub

1 Like