diff --git a/custom_utils/supersmpl_io.py b/custom_utils/supersmpl_io.py index 179a6bb..400d2bf 100644 --- a/custom_utils/supersmpl_io.py +++ b/custom_utils/supersmpl_io.py @@ -135,4 +135,41 @@ def readOBJ(file): Vt = np.array(Vt, np.float32) if Ft: assert len(F) == len(Ft), 'Inconsistent .obj file, mesh and UV map do not have the same number of faces' else: Vt, Ft = None, None - return V, F, Vt, Ft \ No newline at end of file + return V, F, Vt, Ft + + + +""" +Writes OBJ files +Only handles vertices, faces and UV maps +Inputs: +- file: path to .obj file (overwrites if exists) +- V: 3D vertices +- F: 3D faces +- Vt: UV vertices +- Ft: UV faces +Correspondence between mesh and UV map is implicit in F to Ft correspondences +If no UV map data as input, it will write only 3D data in .obj file +""" +def writeOBJ(file, V, F, Vt=None, Ft=None): + if not Vt is None: + assert len(F) == len(Ft), 'Inconsistent data, mesh and UV map do not have the same number of faces' + + with open(file, 'w') as file: + # Vertices + for v in V: + line = 'v ' + ' '.join([str(_) for _ in v]) + '\n' + file.write(line) + # UV verts + if not Vt is None: + for v in Vt: + line = 'vt ' + ' '.join([str(_) for _ in v]) + '\n' + file.write(line) + # 3D Faces / UV faces + if Ft: + F = [[str(i+1)+'/'+str(j+1) for i,j in zip(f,ft)] for f,ft in zip(F,Ft)] + else: + F = [[str(i + 1) for i in f] for f in F] + for f in F: + line = 'f ' + ' '.join(f) + '\n' + file.write(line) \ No newline at end of file