PyMeshLab icon indicating copy to clipboard operation
PyMeshLab copied to clipboard

Exporting a textured mesh to .obj does not include texture filenames in .mtl files

Open jmespadero opened this issue 1 year ago • 3 comments

This bug was revealed by this question in stack overflow.

Exporting a file with textures to .obj file format does not include texture filename in the .mtl files if the file has per_vertex_texture_coordinates.

This bug can be tested with this pymeshlab script:

#!/usr/bin/env python3
import pymeshlab as ml

fn='input.ply'
print(f'Converting input file {fn} to {fn}.obj')
  
ms = ml.MeshSet()
ms.set_verbosity(True)
ms.load_new_mesh(fn)
print(f' * Input textures in this mesh: {list(ms.current_mesh().textures().keys())}')
ms.save_current_mesh(fn + '.obj')

When a input mesh with a texture is included, the output input.ply.obj.mtl file is like this:

#
# Wavefront material file
# Converted by Meshlab Group
#

newmtl material_0
Ka 0.200000 0.200000 0.200000
Kd 1.000000 1.000000 1.000000
Ks 1.000000 1.000000 1.000000
Tr 0.000000
illum 2
Ns 0.000000

and does not include the texture filename that should be in a line like map_Ka texture.filename.png

As a workaround, I have been using a section like this before of saving the mesh:

try:
    ms.apply_filter('compute_texcoord_transfer_vertex_to_wedge')
except:
    pass

jmespadero avatar Jun 02 '23 08:06 jmespadero

@jmespadero could you please upload the input ply file that allows to reproduce the issue?

alemuntoni avatar Jun 07 '23 07:06 alemuntoni

Here one input .ply file with a (per vertex) texture (save with ply extension):

texture.ply

ply
format ascii 1.0
comment VCGLIB generated
comment TextureFile texture.jpg
element vertex 4
property float x
property float y
property float z
property float texture_u
property float texture_v
element face 2
property list uchar int vertex_indices
end_header
0 0 0 0 0
1 1 0 1 1
1 0 0 1 0
0 1 0 0 1
3 0 1 3 
3 1 0 2 

any jpeg file can be used as texture.jpg, but here's mine.

texture

The output .obj file does contains the UV coordinates correctly:

####
#
# OBJ File Generated by Meshlab
#
####
# Object textured.ply.obj
#
# Vertices: 4
# Faces: 2
#
####
mtllib ./textured.ply.obj.mtl

vn 0.000000 0.000000 1.570796
vt 0.000000 0.000000
v 0.000000 0.000000 0.000000
vn 0.000000 0.000000 1.570796
vt 1.000000 1.000000
v 1.000000 1.000000 0.000000
vn 0.000000 0.000000 1.570796
vt 1.000000 0.000000
v 1.000000 0.000000 0.000000
vn 0.000000 0.000000 1.570796
vt 0.000000 1.000000
v 0.000000 1.000000 0.000000
# 4 vertices, 0 vertices normals

usemtl material_0
f 1/1/1 2/2/2 4/4/4
f 2/2/2 1/1/1 3/3/3
# 2 faces, 0 coords texture

# End of File

As said, the output.obj.mtl does not contains the filename of the texture (texture.jpg).

#
# Wavefront material file
# Converted by Meshlab Group
#

newmtl material_0
Ka 0.200000 0.200000 0.200000
Kd 1.000000 1.000000 1.000000
Ks 1.000000 1.000000 1.000000
Tr 1.000000
illum 2
Ns 0.000000

As a workaround... you can execute (on meshlab or pymeshlab) the 'compute_texcoord_transfer_vertex_to_wedge' filter, and then the output .mtl file will be correct, including the map_Kd texture.jpgline.

#
# Wavefront material file
# Converted by Meshlab Group
#

newmtl material_0
Ka 0.200000 0.200000 0.200000
Kd 1.000000 1.000000 1.000000
Ks 1.000000 1.000000 1.000000
Tr 1.000000
illum 2
Ns 0.000000
map_Kd texture.jpg

jmespadero avatar Jun 08 '23 14:06 jmespadero

This issue has been automatically marked as stale because it has not had recent activity. The resources of the VCLab team are limited, and so we are asking for your help. If this is a bug and you can still reproduce this error on the last release of PyMeshLab, please reply with all of the information you have about it in order to keep the issue open. If this is a feature request, and you feel that it is still relevant and valuable, please tell us why. This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

stale[bot] avatar Aug 12 '23 01:08 stale[bot]