NVlabs / nvdiffmodeling

Differentiable rasterization applied to 3D model simplification tasks
Other
455 stars 30 forks source link

importing obj file that has vertex texture coordinate indices #21

Open chacorp opened 1 year ago

chacorp commented 1 year ago

Hi, great work and thnks for providing the code!

btw, when I tried to import a '.obj' file that has vertex texture coordinate indices, (ex, f 4/1 2/2 1/3 ) the code raised an error.

I think it is due to the line in src/obj.py L101-L102, L106-L107, and L110-L111 https://github.com/NVlabs/nvdiffmodeling/blob/9b2ba2eff83c7d90127f78c20773b06ddc3ae1db/src/obj.py#L96-L111

I slightly changed the code to fix the error as below:

elif prefix == 'f': # Parse face
    vs = line.split()[1:]
    nv = len(vs)
    vv = vs[0].split('/')
    len_vv = len(vv)
    v0 = int(vv[0]) - 1
    t0 = int(vv[1]) - 1 if len_vv >= 2 else -1
    n0 = int(vv[2]) - 1 if len_vv >= 3 else -1
    for i in range(nv - 2): # Triangulate polygons
        vv = vs[i + 1].split('/')
        v1 = int(vv[0]) - 1
        t1 = int(vv[1]) - 1 if len_vv >= 2 else -1
        n1 = int(vv[2]) - 1 if len_vv >= 3 else -1
        vv = vs[i + 2].split('/')
        v2 = int(vv[0]) - 1
        t2 = int(vv[1]) - 1 if len_vv >= 2 else -1
        n2 = int(vv[2]) - 1 if len_vv >= 3 else -1

In addition, the code in lines L170-L174 was also changed too. https://github.com/NVlabs/nvdiffmodeling/blob/9b2ba2eff83c7d90127f78c20773b06ddc3ae1db/src/obj.py#L170-L174 into

if v_nrm is not None:
    print("    writing %d normals" % len(v_nrm))
    assert(len(t_pos_idx) == len(t_nrm_idx))
    for v in v_nrm:
        f.write('vn {} {} {}\n'.format(v[0], v[1], v[2]))

I hope someone who faces a similar error can get help from this