MCPcopy Create free account
hub / github.com/3DAnimals/3DAnimals / load_obj

Function load_obj

model/render/obj.py:32–122  ·  view source on GitHub ↗
(filename, clear_ks=True, mtl_override=None)

Source from the content-addressed store, hash-verified

30######################################################################################
31
32def load_obj(filename, clear_ks=True, mtl_override=None):
33 obj_path = os.path.dirname(filename)
34
35 # Read entire file
36 with open(filename, 'r') as f:
37 lines = f.readlines()
38
39 # Load materials
40 all_materials = [
41 {
42 'name' : '_default_mat',
43 'bsdf' : 'pbr',
44 'kd' : texture.Texture2D(torch.tensor([0.5, 0.5, 0.5], dtype=torch.float32, device='cuda')),
45 'ks' : texture.Texture2D(torch.tensor([0.0, 0.0, 0.0], dtype=torch.float32, device='cuda'))
46 }
47 ]
48 if mtl_override is None:
49 for line in lines:
50 if len(line.split()) == 0:
51 continue
52 if line.split()[0] == 'mtllib':
53 all_materials += material.load_mtl(os.path.join(obj_path, line.split()[1]), clear_ks) # Read in entire material library
54 else:
55 all_materials += material.load_mtl(mtl_override)
56
57 # load vertices
58 vertices, texcoords, normals = [], [], []
59 for line in lines:
60 if len(line.split()) == 0:
61 continue
62
63 prefix = line.split()[0].lower()
64 if prefix == 'v':
65 vertices.append([float(v) for v in line.split()[1:]])
66 elif prefix == 'vt':
67 val = [float(v) for v in line.split()[1:]]
68 texcoords.append([val[0], 1.0 - val[1]])
69 elif prefix == 'vn':
70 normals.append([float(v) for v in line.split()[1:]])
71
72 # load faces
73 activeMatIdx = None
74 used_materials = []
75 faces, tfaces, nfaces, mfaces = [], [], [], []
76 for line in lines:
77 if len(line.split()) == 0:
78 continue
79
80 prefix = line.split()[0].lower()
81 if prefix == 'usemtl': # Track used materials
82 mat = _find_mat(all_materials, line.split()[1])
83 if not mat in used_materials:
84 used_materials.append(mat)
85 activeMatIdx = used_materials.index(mat)
86 elif prefix == 'f': # Parse face
87 vs = line.split()[1:]
88 nv = len(vs)
89 vv = vs[0].split('/')

Callers

nothing calls this directly

Calls 2

_find_matFunction · 0.85
appendMethod · 0.80

Tested by

no test coverage detected