pythonrenderingwavefrontfileparsing

.obj to raw vertices converter with normals in python


Iam asking for your help again. I want to make a script with python that takes an .obj format file of vertices and normals and exports 2 files with the vertex coordinates and another with the normal coordinates of the normal

v 1.900470 2.777519 -1.000000
v 1.900470 2.777519 -3.000000
v 2.095561 2.796734 -1.000000
v 2.095560 2.796734 -3.000000
v 2.283154 2.853640 -1.000000
v 2.283154 2.853640 -3.000000
v 2.456040 2.946049 -1.000000
v 2.456040 2.946049 -3.000000
v 2.607577 3.070412 -1.000000
vn 0.0980 -0.9952 0.0000
vn 0.2903 -0.9569 -0.0000
vn 0.4714 -0.8819 0.0000
f 2//1 3//1 1//1
f 4//2 5//2 3//2
f 6//3 7//3 5//3

and export them in the format of:

vertizes

 verxex1.x,verxex1.y,verxex1.z, verxex2.x,verxex2.y,verxex2.z,verxex3.x,verxex3.y,verxex3.z,

normals:

normal1.x,normal1.y,normal1.z, normal2.x,normal2.y,normal2.z, normal3.x,normal3.y,normal3.z, 

with the sequence of the faces as they appear. Hoping i can get some sample code if someone has already implemented it in order to save some time.

Thank you very much!!


Solution

  • If you want to parse a Wavefront .obj file, then the file can be parsed using a Regular Expression, which gets the lines of the file which contain relevant data:

    import re
    
    reComp = re.compile("(?<=^)(v |vn |vt |f )(.*)(?=$)", re.MULTILINE)
    with open(filename) as f:
        data = [txt.group() for txt in reComp.finditer(f.read())]
    

    The list of strings can be converted to lists of floating point coordinates and attributes and integral indices:

    v_arr, vn_arr, vt_arr, f_arr = [], [], [], []
    for line in data:
        tokens = line.split(' ')
        if tokens[0] == 'v':
            v_arr.append([float(c) for c in tokens[1:]])
        elif tokens[0] == 'vn':
            vn_arr.append([float(c) for c in tokens[1:]])
        elif tokens[0] == 'vt':
            vn_arr.append([float(c) for c in tokens[1:]])
        elif tokens[0] == 'f':
            f_arr.append([[int(i) if len(i) else 0 for i in c.split('/')] for c in tokens[1:]])
    

    Finally generate the linearized lists of vertex coordinates and normal vectors:

    vertices, normals = [], []
    for face in f_arr:
        for tp in face:
            vertices += v_arr[tp[0]-1]
            normals  += vn_arr[tp[2]-1]
    

    Parsing the Wavefront file of the question with the above code will generate the following arrays of vertex coordinates and normal vectors:

    Vertex coordinates:

    [1.90047, 2.777519, -3.0, 2.095561, 2.796734, -1.0, 1.90047, 2.777519, -1.0, 2.09556, 2.796734, -3.0, 2.283154, 2.85364, -1.0, 2.095561, 2.796734, -1.0, 2.283154, 2.85364, -3.0, 2.45604, 2.946049, -1.0, 2.283154, 2.85364, -1.0]

    Normal vectors:

    [0.098, -0.9952, 0.0, 0.098, -0.9952, 0.0, 0.098, -0.9952, 0.0, 0.2903, -0.9569, -0.0, 0.2903, -0.9569, -0.0, 0.2903, -0.9569, -0.0, 0.4714, -0.8819, 0.0, 0.4714, -0.8819, 0.0, 0.4714, -0.8819, 0.0]