mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
2.85k stars 567 forks source link

Saving textured GLB file #1296

Open Tzanker opened 2 years ago

Tzanker commented 2 years ago

Hi,

I am trying to save a textured OBJ file as a GLB. The texturing imports fine, i can see it using mesh.show(), but when i export as a GLB no texture appears on the model. I saw another similar issue that makes it seem as if this has been implemented but it doesnt seem to be working here.

Thanks so much!!

mesh = trimesh.load("models/all/odm_texturing/odm_textured_model_geo.obj")  
mesh.show()  
mesh.export(file_type="glb", file_obj="output/landslide.glb")  

mesh.show(): image After export: image

palferenc commented 8 months ago

Had the same issue. I think there is a bug in this line, it's indented too deeply. Linking mesh to material should not depend on UV, at least not in the case of PBR material. Unindented that line and resolved the export problem.

mikedh commented 8 months ago

Hey, is this still happening on the latest versions? This should be unit tested and I just quickly ran

    e = trimesh.load('models/fuze.obj')
    e.export('~/Downloads/textest.glb')

Which loaded fine on https://gltf-viewer.donmccurdy.com/

If it's not working unit testable examples super welcome!

palferenc commented 8 months ago

Hi, my use case is that I load STL files, add material and export it as GLB. Here is runable minimal example (attached the input file):

import trimesh
import trimesh.visual

m = trimesh.load('example.stl')
m.visual = trimesh.visual.TextureVisuals(
                material=trimesh.visual.material.PBRMaterial(
                    baseColorFactor=trimesh.visual.random_color()
                )
            )
m.show()
m.export("example.glb")

The shown object has color, however after exporting the material/color is lost. I must use PBR material because the software reading my output only handles that. example.stl.zip

hambsch commented 3 months ago

the "fix" is presented here: https://github.com/mikedh/trimesh/pull/1895

 sphere.visual.material = primary_color_material
 # material will *not* export without uv coordinates to gltf
 # as GLTF requires TEXCOORD_0 be defined if there is a material
 sphere.visual.uv = g.np.zeros((len(sphere.vertices), 2))

maybe it should give some kind of warning on saving a scene as gltf with materials and empty uv. This used to just work before 1895

Had the same issue. I think there is a bug in this line, it's indented too deeply. Linking mesh to material should not depend on UV, at least not in the case of PBR material. Unindented that line and resolved the export problem.