-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompressTargetsASCII.py
74 lines (61 loc) · 2.19 KB
/
compressTargetsASCII.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
**Project Name:** MakeHuman
**Product Home Page:** http://www.makehuman.org/
**Code Home Page:** http://code.google.com/p/makehuman/
**Authors:** Jonas Hauquier
**Copyright(c):** MakeHuman Team 2001-2013
**Licensing:** AGPL3 (see also http://www.makehuman.org/node/318)
**Coding Standards:** See http://www.makehuman.org/node/165
Abstract
--------
Convert ASCII .target files to use minimal space.
This script is useful to run everytime before new targets or modifications are
committed.
"""
import fnmatch
import os
def getTargets(rootPath):
targetFiles = []
for root, dirnames, filenames in os.walk(rootPath):
for filename in fnmatch.filter(filenames, '*.target'):
targetFiles.append(os.path.join(root, filename))
return targetFiles
def formatFloat(f):
"""
Optimally format floats for writing in ASCII .target files.
"""
f = round(f, 3)
if f == 0:
# Make sure -0.0 becomes 0
return "0"
result = "%.3f" % f
result = result.rstrip("0") # Remove trailing zeros
result = result.lstrip("0") # Remove leading zeros
result = result.replace('-0.', '-.') # Special case: one leading zero and negative
result = result.rstrip(".") # Strip ending . if applicable
if not result:
result = "0" # In case it was "0", rstrip makes it an empty string
return result
allTargets = getTargets('data/targets')
for (i, targetPath) in enumerate(allTargets):
f = open(targetPath, 'rb')
target = f.readlines()
f.close()
newTarget = []
for line in target:
fields = line.split()
idx = int(fields[0])
dx = formatFloat( float(fields[1]) )
dy = formatFloat( float(fields[2]) )
dz = formatFloat( float(fields[3]) )
if dx == dy == dz == "0":
continue
newLine = "%d %s %s %s" % (idx, dx, dy, dz)
newTarget.append(newLine)
f = open(targetPath, 'wb') # write binary to enforce unix line-endings on windows
newTarget = "\n".join(newTarget)
f.write(newTarget)
f.close()
print(("[%.0f%% done] Updated file %s" % (100*(float(i)/float(len(allTargets))), targetPath)))