Following is a model given by a module.
If you look inside of it, you can check two definitin of functions are defined in a nested depth.
I've never seen this before.
How's it working?
def load(filepath, shape_only=False):
def readpca(f, nv):
nc,nd = struct.unpack('<II', f.read(8))
assert(nd == 3*nv)
funcs = [[struct.unpack('<d', f.read(8))[0] for d in range(nd)]
for c in range(nc)]
(nm,) = struct.unpack('<I', f.read(4))
assert(nm == 3*nv)
# fyi: elements are ordered x1,y1,z1,x2,y2,z2,...xnv,ynv,znv.
vmean = [struct.unpack('<d', f.read(8))[0] for i in range(nm)]
(ne,) = struct.unpack('<I', f.read(4))
values = [struct.unpack('<d', f.read(8))[0] for i in range(ne)]
return PCAModel(vmean, funcs, values)
with open(filepath, 'rb') as f:
nv,nt = struct.unpack('<II', f.read(8))
# fyi: faces is a list of tuples (each tuple contains 3 vertex indices).
faces = [struct.unpack('<III', f.read(12)) for i in range(nt)]
shape = readpca(f, nv)
try:
textr = None if shape_only else readpca(f, nv)
except Exception:
print('No texture data. Returning shape-only model.')
textr = None
return MorphModel(faces, shape, textr)