(filename_obj, normalization=True, texture_size=4, load_texture=False)
Load Wavefront .obj file.
    This function only supports vertices (v x x x) and faces (f x x x).
Load Wavefront .obj file.
    This function only supports vertices (v x x x) and faces (f x x x).
Load Wavefront . obj file . This function only supports vertices ( v x x x ) and faces ( f x x x ) .
def load_obj(filename_obj, normalization=True, texture_size=4, load_texture=False):
    """
    Load Wavefront .obj file.
    This function only supports vertices (v x x x) and faces (f x x x).
    """

    # load vertices
    vertices = []
    for line in open(filename_obj).readlines():
        if len(line.split()) == 0:
            continue
        if line.split()[0] == 'v':
            vertices.append([float(v) for v in line.split()[1:4]])
    vertices = np.vstack(vertices).astype('float32')

    # load faces
    faces = []
    for line in open(filename_obj).readlines():
        if len(line.split()) == 0:
            continue
        if line.split()[0] == 'f':
            vs = line.split()[1:]
            nv = len(vs)
            v0 = int(vs[0].split('/')[0])
            for i in range(nv - 2):
                v1 = int(vs[i + 1].split('/')[0])
                v2 = int(vs[i + 2].split('/')[0])
                faces.append((v0, v1, v2))
    faces = np.vstack(faces).astype('int32') - 1

    # load textures
    textures = None
    if load_texture:
        for line in open(filename_obj).readlines():
            if line.startswith('mtllib'):
                filename_mtl = os.path.join(os.path.dirname(filename_obj), line.split()[1])
                textures = load_textures(filename_obj, filename_mtl, texture_size)
        if textures is None:
            raise Exception('Failed to load textures.')

    # normalize into a unit cube centered zero
    if normalization:
        vertices -= vertices.min(0)[None, :]
        vertices /= np.abs(vertices).max()
        vertices *= 2
        vertices -= vertices.max(0)[None, :] / 2

    if load_texture:
        return vertices, faces, textures
    else:
        return vertices, faces
def load_obj ( filename_obj , normalization = True , texture_size = 4 , load_texture = False ) : # load vertices vertices = [ ] for line in open ( filename_obj ) . readlines ( ) : if len ( line . split ( ) ) == 0 : continue if line . split ( ) [ 0 ] == 'v' : vertices . append ( [ float ( v ) for v in line . split ( ) [ 1 : 4 ] ] ) vertices = np . vstack ( vertices ) . astype ( 'float32' ) # load faces faces = [ ] for line in open ( filename_obj ) . readlines ( ) : if len ( line . split ( ) ) == 0 : continue if line . split ( ) [ 0 ] == 'f' : vs = line . split ( ) [ 1 : ] nv = len ( vs ) v0 = int ( vs [ 0 ] . split ( '/' ) [ 0 ] ) for i in range ( nv - 2 ) : v1 = int ( vs [ i + 1 ] . split ( '/' ) [ 0 ] ) v2 = int ( vs [ i + 2 ] . split ( '/' ) [ 0 ] ) faces . append ( ( v0 , v1 , v2 ) ) faces = np . vstack ( faces ) . astype ( 'int32' ) - 1 # load textures textures = None if load_texture : for line in open ( filename_obj ) . readlines ( ) : if line . startswith ( 'mtllib' ) : filename_mtl = os . path . join ( os . path . dirname ( filename_obj ) , line . split ( ) [ 1 ] ) textures = load_textures ( filename_obj , filename_mtl , texture_size ) if textures is None : raise Exception ( 'Failed to load textures.' ) # normalize into a unit cube centered zero if normalization : vertices -= vertices . min ( 0 ) [ None , : ] vertices /= np . abs ( vertices ) . max ( ) vertices *= 2 vertices -= vertices . max ( 0 ) [ None , : ] / 2 if load_texture : return vertices , faces , textures else : return vertices , faces