It goes like this
/initial code/
from github repo https://github.com/bharat-b7/MultiGarmentNetwork/dress_smpl.py
…
…
tgt_body = Mesh(smpl.r, smpl.f)
vert_inds = vert_indices[garment_type]
garment_unposed.set_texture_image(garment_tex)
and then i save garment_unposed.write_obj(“filename”) like this
garment_unposed = Mesh(filename=join(path, garment_type + ‘.obj’))
The commands Mesh and set_texture_image come from the github repo https://github.com/MPI-IS/mesh
Here if you go in mesh folder in the repo you will find mesh.py and texture.py
The write_obj is written as
if os.path.dirname(filename) and not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
ff = -1 if flip_faces else 1
def write_face_to_obj_file(face_index, obj_file):
vertex_indices = self.f[face_index][::ff] + 1
if hasattr(self, 'ft'):
texture_indices = self.ft[face_index][::ff] + 1
if not hasattr(self, 'fn'):
self.reset_face_normals()
normal_indices = self.fn[face_index][::ff] + 1
obj_file.write('f %d/%d/%d %d/%d/%d %d/%d/%d\n' % tuple(
np.array([vertex_indices, texture_indices, normal_indices]).T.flatten()))
elif hasattr(self, 'fn'):
normal_indices = self.fn[face_index][::ff] + 1
obj_file.write('f %d//%d %d//%d %d//%d\n' % tuple(np.array([vertex_indices, normal_indices]).T.flatten()))
else:
obj_file.write('f %d %d %d\n' % tuple(vertex_indices))
with open(filename, 'w') as fi:
if comments is not None:
if isinstance(comments, str):
comments = [comments]
for comment in comments:
for line in comment.split("\n"):
fi.write("# %s\n" % line)
if hasattr(self, 'texture_filepath'):
outfolder = os.path.dirname(filename)
outbase = os.path.splitext(os.path.basename(filename))[0]
mtlpath = outbase + '.mtl'
fi.write('mtllib %s\n' % mtlpath)
from shutil import copyfile
texture_name = outbase + os.path.splitext(self.texture_filepath)[1]
if os.path.abspath(self.texture_filepath) != os.path.abspath(os.path.join(outfolder, texture_name)):
copyfile(self.texture_filepath, os.path.join(outfolder, texture_name))
self.write_mtl(os.path.join(outfolder, mtlpath), outbase, texture_name)
for r in self.v:
fi.write('v %f %f %f\n' % (r[0], r[1], r[2]))
if hasattr(self, 'fn') and hasattr(self, 'vn'):
for r in self.vn:
fi.write('vn %f %f %f\n' % (r[0], r[1], r[2]))
if hasattr(self, 'ft'):
for r in self.vt:
if len(r) == 3:
fi.write('vt %f %f %f\n' % (r[0], r[1], r[2]))
else:
fi.write('vt %f %f\n' % (r[0], r[1]))
if hasattr(self, 'segm') and self.segm and not group:
for p in self.segm.keys():
fi.write('g %s\n' % p)
for face_index in self.segm[p]:
write_face_to_obj_file(face_index, fi)
else:
if hasattr(self, 'f'):
for face_index in range(len(self.f)):
write_face_to_obj_file(face_index, fi)
and write_mtl as
def write_mtl(self, path, material_name, texture_name):
“”“Material attribute file serialization”""
with open(path, ‘w’) as f:
f.write('newmtl s\n' material_name)
# copied from another obj, no idea about what it does
f.write(‘ka 0.329412 0.223529 0.027451\n’)
f.write(‘kd 0.780392 0.568627 0.113725\n’)
f.write(‘ks 0.992157 0.941176 0.807843\n’)
f.write(‘illum 0\n’)
f.write('map_Ka s\n' texture_name)
f.write('map_Kd s\n' texture_name)
f.write('map_Ks s\n' texture_name)