trimesh icon indicating copy to clipboard operation
trimesh copied to clipboard

add texture from image to shape

Open pvnieo opened this issue 5 years ago • 6 comments

Hi,

I have a 3D shape loaded from an .off file, and I have an image load as a PIL image.

I want to add this image as a texture to the mesh but I can't figure out how to do it. I did the following manipulation, but when I show the mesh, the texture is not applied.

import trimesh
from PIL import Image

im = Image.open("image.png")
m = trimesh.load("mesh.off", process=False)

tex = trimesh.visual.TextureVisuals(image=im)
m.visual.texture = tex

m.show()

How can I do this? Thank you in advance.

pvnieo avatar Nov 21 '20 22:11 pvnieo

Hi @mikedh I'm getting a step closer!

I succeeded to add the image to the shape, but because my surface coordinates are random, the texture it self is random. This is what I did:

uv = np.random.rand(m.vertices.shape[0], 2)

material = trimesh.visual.texture.SimpleMaterial(image=im)
color_visuals = trimesh.visual.TextureVisuals(uv=uv, image=im, material=material)
mesh=trimesh.Trimesh(vertices=m.vertices, faces=m.faces, visual=color_visuals, validate=True, process=False)
mesh.show

and this gives me the following: image

Can you suggest me a way to compute the uv?

Thank you in advance!

pvnieo avatar Nov 23 '20 15:11 pvnieo

Hi @mikedh I'm getting a step closer!

I succeeded to add the image to the shape, but because my surface coordinates are random, the texture it self is random. This is what I did:

uv = np.random.rand(m.vertices.shape[0], 2)

material = trimesh.visual.texture.SimpleMaterial(image=im)
color_visuals = trimesh.visual.TextureVisuals(uv=uv, image=im, material=material)
mesh=trimesh.Trimesh(vertices=m.vertices, faces=m.faces, visual=color_visuals, validate=True, process=False)
mesh.show

and this gives me the following: image

Can you suggest me a way to compute the uv?

Thank you in advance! I can tell you the way to compute the uv cordinate, but I want to know how apply multiple texture to a Trimesh and show it

Xelawk avatar Jan 20 '21 06:01 Xelawk

image

In my case, the .obj file contains uv information. Note that if the mesh.show() present a dark texture, use pyrender library

robot0321 avatar Oct 21 '21 06:10 robot0321

Just a note for others, I managed to manually calculate and pack my UV coordinates into a .ply, then I read that in by Trimesh, then applied my image as a texture, and boom!

import trimesh
from PIL import Image
mesh = trimesh.load("model.ply", process=False)
uv = mesh.visual.uv
im = Image.open("image.png")
material = trimesh.visual.texture.SimpleMaterial(image=im)
color_visuals = trimesh.visual.TextureVisuals(uv=uv, image=im, material=material)
mesh.visual = color_visuals
mesh.export(file_obj='model.glb')

legel avatar Nov 12 '22 08:11 legel

Hey! Nice solution, how did you manage to manually calculate your UV coords though?

eddjharrison avatar Apr 15 '23 13:04 eddjharrison

Hey! Nice solution, how did you manage to manually calculate your UV coords though?

You can use xatlas-python

rohit7044 avatar Oct 25 '23 06:10 rohit7044