Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trimesh GLTF export how to set pbr material #523

Closed
SimonScholl opened this issue Aug 2, 2019 · 3 comments
Closed

Trimesh GLTF export how to set pbr material #523

SimonScholl opened this issue Aug 2, 2019 · 3 comments

Comments

@SimonScholl
Copy link

Hello community, i have a Trimesh object
trimesh_mesh = trimesh.Trimesh(vertices=vertices, faces=faces, vertex_normals=vertex_normals, validate=True)
Which i export as gltf

trimesh_scene = trimesh.Scene(geometry=trimesh_mesh)
    with open(gltf_path, 'wb') as f:
        f.write(trimesh.exchange.gltf.export_glb(trimesh_scene, include_normals=True))

My question, how can i apply a custom pbr material to the trimesh_mesh before exporting? My first attempt was to define a json

 new_material = {
        'pbrMetallicRoughness': {
            'baseColorFactor': [ 0.5, 0.8, 0.0, 1.0 ],           
            'metallicFactor': 0,
            'roughnessFactor': 0,
        }
    }

And add it like
trimesh_mesh.visual.material = new_material

But that changes nothing to the default material.

@mikedh
Copy link
Owner

mikedh commented Aug 2, 2019

Hey, unfortunately the GLTF exporter doesn't support texture at the moment, although it is on the list of requested features (#199). PR's welcome!

@mikedh
Copy link
Owner

mikedh commented Aug 13, 2019

Hey, I merged GLTF texture exports in #528, now you just need to make sure that trimesh.visual is set up as PBR then that data should be preserved on export:

In [1]: import trimesh

In [2]: m = trimesh.creation.uv_sphere()

In [3]: m
Out[3]: <trimesh.base.Trimesh at 0x7f183475ff28>

In [4]: m.visual
Out[4]: <trimesh.visual.color.ColorVisuals at 0x7f183475f198>

In [5]: m.visual = trimesh.visual.TextureVisuals(material=trimesh.visual.material.PBRMaterial())

In [6]: m.visual
Out[6]: <trimesh.visual.texture.TextureVisuals at 0x7f183468d198>

In [7]: m.visual.material
Out[7]: <trimesh.visual.material.PBRMaterial at 0x7f183468d470>

In [8]: dir(m.visual.material)
Out[8]: 
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'alphaCutoff',
 'baseColorFactor',
 'baseColorTexture',
 'doubleSided',
 'emissiveFactor',
 'emissiveTexture',
 'main_color',
 'metallicFactor',
 'metallicRoughnessTexture',
 'normalTexture',
 'occlusionTexture',
 'roughnessFactor',
 'to_color']

In [11]: m.visual.material.baseColorFactor = [0,0,255,255]

In [12]: m.show()
Out[12]: SceneViewer(width=900, height=675)

In [13]: m.export(file_obj='blue_sphere.glb')

@legel
Copy link

legel commented Mar 29, 2023

This is a great implementation, very useful, thank you.

Is there any support possible for adding specular textures as well, according to the new glTF 2.0 extension?
https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_specular

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants