-
Notifications
You must be signed in to change notification settings - Fork 1
/
readprimitives.py
151 lines (118 loc) · 4.05 KB
/
readprimitives.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- coding:gbk -*-
import struct
import binsec
import dirHandle as dh
import os
temppath = 'temp'
DEBUG = False
def GetAllPrimitives(DirName):
PrimitivesList = []
pathlist = os.listdir(DirName)
for path in pathlist:
#print path
path = os.path.join(DirName, path)
if os.path.isfile(path) and path[-11:] != '.primitives':
continue
if os.path.isfile(path):
PrimitivesList.append(path)
else:
PrimitivesList.extend(GetAllPrimitives(path))
return PrimitivesList
def getIntByStr(bstr):
op = bstr[0]
bstr = bstr[1:]
#positive
if op == '0':
return int(bstr, 2)
#negative
curPos = len(bstr) - 1
while curPos > -1:
if bstr[curPos] == '0':
bstr = bstr[:curPos] + '1' + bstr[curPos + 1:]
else:
bstr = bstr[:curPos] + '0' + bstr[curPos + 1:]
break
curPos -= 1
curPos = len(bstr) - 1
while curPos > -1:
if bstr[curPos] == '0':
bstr = bstr[:curPos] + '1' + bstr[curPos + 1:]
else:
bstr = bstr[:curPos] + '0' + bstr[curPos + 1:]
curPos -= 1
return -int(bstr, 2)
def unpackIntToFloat(packed):
strPacked = bin(packed)[2:]
while len(strPacked) < 32:
strPacked = '0' + strPacked
z = getIntByStr(strPacked[:10])
y = getIntByStr(strPacked[10:21])
x = getIntByStr(strPacked[21:32])
return (float(x) / 1023.0, float(y) / 1023.0, float(z) / 511.0)
def GetInfoFromPrimitives(filename):
indexList = []
vertexList = []
vertexs = []
index = []
groupList = []
indexFormat = ''
dh.cleanDir(temppath)
if os.path.isdir(temppath):
os.rmdir(temppath)
binsec.extract(filename, temppath)
listItems = os.listdir(temppath)
for listItem in listItems:
if listItem.find('vertices') != -1:
vertexList.append(listItem)
elif listItem.find('indices') != -1:
indexList.append(listItem)
#read vertex information
for vertex in vertexList:
f = open(temppath + "\\" + vertex, 'rb')
dataInfo = f.read(64 + 4)
dataValue = struct.unpack('64s1i', dataInfo)
vertexFormat = dataValue[0]
vertexNumber = dataValue[1]
vertexFormatEnd = ''
for char in vertexFormat:
if char >= 'a' and char <= 'z':
vertexFormatEnd += char
else:
break
for i in xrange(0, vertexNumber):
vertexInfo = f.read(32)
vertexValue = struct.unpack('3f1I2f1I1I', vertexInfo)
vPos = (vertexValue[0], vertexValue[1], vertexValue[2])
vUV = (vertexValue[4], vertexValue[5])
vNormal = unpackIntToFloat(vertexValue[3])
vT = unpackIntToFloat(vertexValue[6])
vB = unpackIntToFloat(vertexValue[7])
vertexs.append((vPos, vUV, vNormal, vT, vB))
f.close()
for indice in indexList:
f = open(temppath + '\\' + indice, 'rb')
indiceInfo = f.read(64 + 4 + 4)
indiceValue = struct.unpack('64s2i', indiceInfo)
indiceFormat = indiceValue[0]
indiceNumber = indiceValue[1]
groupNumber = indiceValue[2]
for indicenumber in xrange(0, indiceNumber):
DataInfo = f.read(2)
DataValue = struct.unpack('H', DataInfo)[0]
index.append(DataValue)
for groupnumber in xrange(0, groupNumber):
groupInfo = []
for i in xrange(0, 4):
groupData = f.read(4)
groupValue = struct.unpack('i', groupData)[0]
groupInfo.append(groupValue)
groupList.append(groupInfo)
f.close()
if not DEBUG:
dh.cleanDir(temppath)
if os.path.isdir(temppath):
os.rmdir(temppath)
return (vertexs, index, groupList, vertexFormat, vertexFormatEnd, indexFormat)
if __name__ == '__main__':
print len(GetAllPrimitives('scene'))
#print GetInfoFromPrimitives(GetAllPrimitives()[0])[0]