MPI-IS / mesh

MPI-IS Mesh Processing Library
Other
643 stars 147 forks source link

How to save the textured obj files that generated by using Mesh()? #27

Closed Qingcsai closed 4 years ago

Qingcsai commented 4 years ago

After I borrowed some code from smpl like below, I found it worked for saving the class Mesh() as .obj file.

## Write to an .obj file
outmesh_path = './hello_smpl.obj'
with open( outmesh_path, 'w') as fp:
    for v in m.v:
        fp.write( 'v %f %f %f\n' % ( v[0], v[1], v[2]) )

    for f in m.f+1: # Faces are 1-based, not 0-based in obj files
        fp.write( 'f %d %d %d\n' %  (f[0], f[1], f[2]) )

But when I try to add some textures to the faces, I found it wouldn't work after I added some code below, which m=Mesh(*****) by definition.

## Write to an .obj file
outmesh_path = './hello_smpl.obj'
with open( outmesh_path, 'w') as fp:
    for v in m.v:
        fp.write( 'v %f %f %f\n' % ( v[0], v[1], v[2]) )
    for vt in m.vt:
        fp.write('vt %f %f\n' % (vt[0], vt[1]))
    for i, f in enumerate(m.f): # Faces are 1-based, not 0-based in obj files
        fp.write( 'f %d/%d %d/%d %d%d/\n' %  (f[0]+1,m.ft[i][0], f[1]+1,m.ft[i][0], f[2]+1,m.ft[i][0]) )

I wonder if I have done something wrong? If so, how to save the class Mesh() as the .obj files. Thank you!

jcpassy commented 4 years ago

Hi @Qingcsai, I would need a bit more details to find out what is happening. Could you please try to reproduce your issue with some files from the data folder, post the full code, and explain what is not working?

Thanks!

Qingcsai commented 4 years ago

Hi @jcpassy I have solved this question. I just want to add some texture to .obj files, so I tried to write the ft, vt, f by myself.

But now I think I considered this problem over complicated before when I find two functions in Mesh(), which is .set_texture_image() and .write_obj().

This two functions totally solved my question, and here's the method for helping others who might want to add some texture images to their .obj files:

import os
from os.path import join
from psbody.mesh import Mesh

path = 'PATH_TO_YOUR_OBJ_FILE'
your_mesh = Mesh(filename = join(path, 'your_mesh.obj'))
your_mesh.set_texture_image(join(path, 'sample_texture.jpg'))
your_mesh.write_obj('PATH_TO_SAVE_IT/your_mesh.obj')

Thanks for your great work! I will close this issue.

jcpassy commented 4 years ago

Thanks @Qingcsai , glad you got it to work. If you are interested, I think it would be great to add your little example to the documentation. If you issue a PR, I would gladly review it and add your contribution to the next release. Cheers!

PRAKHAR-bit commented 4 years ago

@Qingcsai How do you do it. I did but still did not get any texture. and in the mesh repo the .mtl file is constant for all SMPL wont it affect the result What I did in the code of MultiGarmentNet is that I saved the new_garment as an obj and tried to view it but no benifit. Fitting is their but no texture

Qingcsai commented 4 years ago

@PRAKHAR-bit Hi, see https://github.com/Qingcsai/3DVirtualTryOn/issues/1#issuecomment-629921045

Qingcsai commented 4 years ago

Thanks @Qingcsai , glad you got it to work. If you are interested, I think it would be great to add your little example to the documentation. If you issue a PR, I would gladly review it and add your contribution to the next release. Cheers!

Hi, in fact I laterly realized

your_mesh.set_texture_image(join(path, 'sample_texture.jpg'))
your_mesh.write_obj('PATH_TO_SAVE_IT/your_mesh.obj')

only save the constant .mtl files for all the .obj, for texture I still need to edit the vt *** and f *** stuffs by myself, so I am afraid it's not enough to add texture to .obj files by the above code, which is the reason why I din't issue a PR. Anyway, thanks again for your great work!

haiderasad commented 2 years ago

hi @Qingcsai any idea on how to add vt and refined f values into an obj with no vt? also any script that automatically generates obj files with vt values