Skip to content

Instantly share code, notes, and snippets.

@satoruhiga
Created June 8, 2019 01:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satoruhiga/bdbcb098200b3a509e0ea45711821655 to your computer and use it in GitHub Desktop.
Save satoruhiga/bdbcb098200b3a509e0ea45711821655 to your computer and use it in GitHub Desktop.
from collections import deque
import os
def main(path, output):
data = None
lines = []
with open(path, 'r') as fp:
for line in fp:
lines.append(line)
N = len(lines)
class Chan:
def __init__(self, name, numSamples):
self.name = name
self.data = [0] * numSamples
def __setitem__(self, key, value):
self.data[key] = value
def __getitem__(self, key):
return self.data[key]
class CHOP:
def __init__(self, name, numSamples):
self.name = name
self.rate = 30
self.numSamples = numSamples
self.chans = []
self.numChans = 0
def appendChan(self, name):
self.chans.append(Chan(name, self.numSamples))
self.numChans = len(self.chans)
def __setitem__(self, key, value):
self.chans[key] = value
def __getitem__(self, key):
return self.chans[key]
def saveClip(self, path):
fp = open(path, 'w')
fp.write('{\n')
fp.write(' rate = ' + str(self.rate) + '\n')
fp.write(' start = ' + str(0) + '\n')
fp.write(' tracklength = ' + str(self.numSamples) + '\n')
fp.write(' tracks = ' + str(self.numChans) + '\n')
for c in self.chans:
fp.write(' {\n')
fp.write(' name = ' + c.name + '\n')
fp.write(' data = ' + " ".join(map(str, c.data)) + '\n')
fp.write(' }\n')
fp.write('}')
fp.close()
MAT1 = CHOP('MAT1', N)
MAT2 = CHOP('MAT2', N)
MAT3 = CHOP('MAT3', N)
MESH = CHOP('MESH', N)
FACIAL_DATA = CHOP('FACIAL_DATA', N)
for i in range(16):
MAT1.appendChan('m%i' % (i + 1))
MAT2.appendChan('m%i' % (i + 1))
for i in range(9):
MAT3.appendChan('m%i' % (i + 1))
for i in range(1220):
MESH.appendChan('tx%i' % i)
MESH.appendChan('ty%i' % i)
MESH.appendChan('tz%i' % i)
for i, line in enumerate(lines):
if not line:
break
data = deque()
for x in line.split('~'):
data.append(x)
N = int(data.popleft())
m1 = [float(x) for x in data.popleft().split(':')]
m2 = [float(x) for x in data.popleft().split(':')]
m3 = [float(x) for x in data.popleft().split(':')]
# mat
for x in range(16):
MAT1[x][i] = m1[x]
MAT2[x][i] = m2[x]
for x in range(9):
MAT3[x][i] = m3[x]
# mesh
for x in range(1220):
v = [float(x) for x in data.popleft().split(':')]
MESH[x * 3 + 0][i] = v[0]
MESH[x * 3 + 1][i] = v[1]
MESH[x * 3 + 2][i] = v[2]
# facial
data.popleft()
facial = []
for x in range(52):
facial.append(data.popleft())
if FACIAL_DATA.numChans == 0:
for x in facial:
x = x.split(':')
FACIAL_DATA.appendChan( x[0] )
for n, x in enumerate(facial):
x = x.split(':')
FACIAL_DATA[n][i] = float(x[1])
chops = [MAT1, MAT2, MAT3, MESH, FACIAL_DATA]
for x in chops:
os.makedirs(output, exist_ok=True)
x.saveClip(os.path.join(output, '%s.clip' % x.name))
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('input', help='input faceData.txt path')
parser.add_argument("-o", "--output", help='output folder path', default='out')
args = parser.parse_args()
main(args.path, args.output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment