trimesh icon indicating copy to clipboard operation
trimesh copied to clipboard

Extract UV map as .png file

Open rohit7044 opened this issue 1 year ago • 1 comments

Hi, First of all tremendous work on trimesh. Thank you

I am trying to extract the UV map from a mesh. I see the coordinates in visuals but I want the extracted texture map processed in image format. Is there any way to do this?

rohit7044 avatar Oct 25 '23 08:10 rohit7044

I am assuming your mesh only contains vertex colors ? (Else you would already have your texture) If so you can project the vertex colors in the texture space and interpolate between them to recreate a texture.

import numpy as np
import cv2
from scipy.interpolate import griddata
 
def compute_interpolation_map(shape, tcoords, values):
    points = (tcoords * np.asarray(shape)[None, :]).astype(np.int32)
    x = np.arange(shape[0])
    y = np.flip(np.arange(shape[1]))
    X, Y = np.meshgrid(x, y)
    res = griddata(points, values, (X, Y),  method='cubic')
    res[np.isnan(res)] = 0
    return res

tcoords = ... # you already have them
vertex_colors = ... # I assume you already have them
image_size = (512, 512) # create a 512 x 512 texture
texture_map = compute_interpolation_map(image_size, tcoords, vertex_colors) 
cv2.imwrite(image_path, texture_map)

Kiord avatar Oct 25 '23 11:10 Kiord

@Kiord I dont have the tcoords and I dont know what it should be. Do you know how can I retrieve from the mesh?

mesh = trimesh.Trimesh(
                vertices=vertices_numpy,
                faces=faces_numpy,
                vertex_colors=color_numpy,
      )

Also, my goal is to import the mesh inside blender with color. Not sure which image_size would be required to that. Do you have some experience on that?

math-sasso avatar Apr 01 '24 19:04 math-sasso

Hello @math-sasso, they are texture coordinates (or uv coordinates), a numpy array of size number_of_vertices X 2 which indicates, for each vertex, its corresponding position in the two dimensional texture space. (see https://en.wikipedia.org/wiki/Texture_mapping).

If you load a mesh that has texture coordinates, you can access them like this mesh.visual.uv. If your mesh has no texture coordinates, mesh.visual will be a ColorVisuals instead of a TextureVisuals and will not have a .uv member.

Are you sure your mesh is textured in the first place ? (It may have just vertex colors) Also it is not clear what are your inputs (before trimesh) and your outputs (what you want trimesh to do)

Kiord avatar Apr 01 '24 22:04 Kiord

Hello @Kiord

Thanks for your explanation, I am new in this 3D object manipulation world.

Effectivelly it does not have mesh.visual.uv, I believe it is a ColorVisual then. My goal it to be able to load the 3D model inside blender with the colors.

I am trying to deploy this AI model called TripoSR and they use trimesh to generate 3D models from images.

It is doing a bunch of AI stuff before arriving to the code below in the TSR class. Now you explained me the ColorVisuals it makes sense there are no textures because it can take pictures from the trimesh.Trimesh mesh object and it has color.

mesh = trimesh.Trimesh(
                vertices=vertices_numpy,
                faces=faces_numpy,
                vertex_colors=color_numpy,
)

However in the moment I export to obj or glb with mesh.export there is not color information, and Blender does only imports the asset with color.

Do you know if I need to do something special in this case? My hyphoteses was that the library had a bug in the exporting method that removed the texture, but as it does not have a texture, what is the strategy I should follow to preserve the color when exporting?

math-sasso avatar Apr 02 '24 15:04 math-sasso

The issue is on Blender side I'm afraid.

obj is not really a well defined format. Softwares use different conventions and it affects the vertex colors here.

glTF 2.0 works though (tested from TripoSR demo)

  • import the glb mesh as glTF 2.0 in blender
  • select the object by clicking it
  • in "Material" tab, click new
  • click on the Base Color yellow dot to change its source, and click Color Attribute
  • now you should see the colors in material preview mode

Kiord avatar Apr 02 '24 22:04 Kiord

@Kiord thanks for the reply.

I followed the step by step, but when I click Color Attribute that is what happening. image

I unedrstand fom your explanation that colors will come configure inside the .glb file, so we sort of just need to "enable" it.

Did I miss something?

math-sasso avatar Apr 03 '24 12:04 math-sasso

TripoSR also generates a video where I can see the colors exist before the export. https://github.com/mikedh/trimesh/assets/23565626/c019c7bf-b575-4098-9b2a-427fdb49ea0b

math-sasso avatar Apr 03 '24 12:04 math-sasso

Are you in material preview mode ?

Screenshot_20240403_150331.jpg

Kiord avatar Apr 03 '24 13:04 Kiord

I can see it now thanks! However compared with the video gif I shared, the quality is way inferior.

image

math-sasso avatar Apr 03 '24 16:04 math-sasso

Try opening the file on Meshlab or Microsoft 3D and see if it contains the same texture like the video. If it doesn't, then there's issue on the processing pipeline of Tripod and better post the issue on that repository. If it does show the texture right as you expect, then you need to figure out how to replicate that in blender. Either way this situation remains out of scope for Trimesh

rohit7044 avatar Apr 04 '24 03:04 rohit7044