Babylon .obj export

Hello i want to export my meshes from babylon and then import again to babylon. How can i export this meshes to .obj? I have this code. There is console string but how can i download .obj file.
exportMeshes(): void { console.log('Exporting ' + this.sceneViewer.getAllMeshes()); console.log(OBJExport.OBJ(this.sceneViewer.getAllMeshes())); }

You can use javascript to automatically download it: Automatically save OBJ file | Babylon.js Playground (babylonjs.com)

1 Like

Thank you a lot, and what does mean this 3 parameter false, ‘’,true?

 OBJExport.OBJ(this.sceneViewer.getAllMeshes(), false, '', true)

When you hover over a function on the playground, it shows the related documentation. In this case:

Oh… idk why but my export doesnt work. I have import/export function. Import works well when i tried it on some .obj 3D models from web. But i want export my meshes and the import them to the file. Or export all scene and import load new one

This sphere from your playground i can import but my exports no… Can i import .obj with more then 1 mesh?
image
This is consoled log OBJExport

After OBJExport mtllib mat.mtl
g object0
o object_0
usemtl grid
v -250 0 -250
v 250 0 -250
v -250 0 250
v 250 0 250
vn 0 1 0
vn 0 1 0
vn 0 1 0
vn 0 1 0
vt 0 1
vt 1 1
vt 0 0
vt 1 0
f 1/1/1 2/2/2 4/4/4
f 1/1/1 4/4/4 3/3/3
g object1
o object_1
usemtl colorShader
v -250 0 0
v 250 0 0
f NaN// 6// 5//
g object2
o object_2
usemtl colorShader 1
v 0 0 250
v 0 0 -250
f NaN// 8// 7//
g object3
o object_3
usemtl colorShader 2
v 0 -250 0
v 0 250 0
f NaN// 10// 9//
g object4
o object_4
g object5
o object_5
usemtl __GLTFLoader._default
v -3.184000015258789 1.2111999988555908 -25.607900619506836
v -3.184499979019165 1.170199990272522 -25.584699630737305
v -3.2560999393463135 1.1969000101089478 -25.56920051574707
v -3.184000015258789 1.2111999988555908 -25.607900619506836
v -3.2560999393463135 1.1969000101089478 -25.56920051574707
v -3.2592999935150146 1.2335000038146973 -25.590299606323242
v -3.2592999935150146 1.2335000038146973 -25.590299606323242
v -3.2674999237060547 1.2783000469207764 -25.61479949951172
v -3.1803998947143555 1.2562999725341797 -25.63610076904297
v -3.2592999935150146 1.2335000038146973 -25.590299606323242
v -3.1803998947143555 1.2562999725341797 -25.63610076904297
v -3.184000015258789 1.2111999988555908 -25.607900619506836
v -3.142699956893921 1.2008999586105347 -25.618499755859375
...

here is example of few meshes i want to export and then import

As your consoled OBJ shows, there are face (f) elements with NaN (not a number) on it, which is invalid. What mesh are you trying to export?

How can i get which element is f? :smiley: And then how can i exlude from export some meshes. For example, on the picture the first ground and lines i dont want export

You mean you want to know which mesh has generated the invalid faces? For that, and for your other question, the first argument of the OBJ function is a list of the meshes that you want to export, so to exclude a mesh, simply don’t include it in the list.

okej now i imported just main skeleton with the ground so ground i must exclude and skeleton is lies… he should stand :smiley: But what is f ?
image

f is used to define a triangular face in the mesh, I recommend looking at the obj format specification to learn more about it: OBJ File Format – All You Need to Know | All3DP

Okay so lets say everythink works. But why model lies? :smiley:

Export worked, but models lie. Now i startet project and this is consoled,
image
can you help me?

That’s probably because the model was authored in a right handed coordinate system instead of Babylon’s native left handed system. You can change its rotation to make it stand up again. Getting Started - Chapter 2 - Mesh Placement | Babylon.js Documentation (babylonjs.com)

It’s extremely hard to know what is going on without a working reproduction :slight_smile: Can you try loading your model in the Babylon.js Playground (babylonjs.com) to see if the same error happens there?

1 Like

In babylon sandbox my exported objects works, but they are on default position. How can i export object with changed position? Because i change them position by gizmo manager and export them. If i import them into sandbox they are on default. Can you saw me differences between .obj and .glb? .obj are just meshes and glb are meshes + scene?

You need to change the position via code, like: Getting Started - Chapter 2 - Mesh Placement | Babylon.js Documentation (babylonjs.com)

.obj and .gltf/.glb (glb is just the binary (compressed) version of gltf) are two different formats for transmitting 3D model data. The .obj format is older and simpler, but it takes up more space and it’s not as optimized, while the .gltf/.glb is developed by the same commitee which develops OpenGL, uses less memory and has more features than .obj. Whenever it’s possible, it’s better to use .glb instead of .obj. Searching “obj vs glb” on google lead me to this great article explaining a bit more about them and their differences: Does choosing a glTF over an OBJ file format matter? | by Fia Sutton | Medium

The group who develops GLTF also has a great poster explaining the format: gltfOverview-2.0.0b.png (3778×2500) (raw.githubusercontent.com) (start reading from the top left)

1 Like

Hello, i have the same goal, need to save mesh in .obj format with texture, and use a new v5.1.0 - and BABYLON have not the OBJExport in npm package. O_o can you look please?

for nodejs webpack build install the “babylonjs-serializers” last version and solve the problem

1 Like

I was just going to tell you to check the serializers package :sweat_smile:

2 Likes