Completed
Push — master ( 0dba88...7a21f8 )
by
unknown
56s
created

ed2d.assets.OBJ   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 111
Duplicated Lines 0 %
Metric Value
dl 0
loc 111
rs 10
wmc 21

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __init__() 0 52 6
F __process_in_house() 0 42 11
A get_final_data() 0 14 4
1
2
from ed2d.mesh import MeshBase
3
from ed2d.assets.mtlloader import MTL
4
5
class OBJ(object):
6
    def __init__(self, fileName):
7
        ''' Wavefront .obj file parser.'''
8
        fcount = 0
9
        vcount = 0
10
        vtcount = 0
11
        vncount = 0
12
13
        # Load the file
14
        objfile = open(fileName, "r")
15
16
        # Do a head count
17
        for line in objfile:
18
            value = line[:2]
19
            if value == 'f ':
20
                fcount += 1
21
            elif value == 'v ':
22
                vcount += 1
23
            elif value == 'vt':
24
                vtcount += 1
25
            elif value == 'vn':
26
                vncount += 1
27
28
        fcount *= 3
29
        vcount *= 3
30
        vtcount *= 3
31
        vncount *= 3
32
33
        self.tempVertices = [None] * vcount
34
        self.tempNormals = [None] * vncount
35
36
        self.tempUVs = [None] * fcount
37
38
        self.vertexIndices = [None] * fcount
39
        self.normalIndices = [None] * fcount
40
        self.uvIndices = [None] * fcount
41
42
        self.finalVertices = [None] * fcount
43
        self.finalNormals = [None] * fcount
44
        self.finalUVs = [None] * fcount
45
46
        self.fnumber = 0
47
        self.vnumber = 0
48
        self.vtnumber = 0
49
        self.vnnumber = 0
50
51
        # Process the data
52
        self.__process_in_house(objfile)
53
        # Finalize
54
        self.get_final_data()
55
56
        # Close the file
57
        objfile.close()
58
59
    def __process_in_house(self, objfl):
60
        with open(filename, "r") as objfl:
61
62
            for line in objfl:
63
64
                value = line.split()
65
                valueType = value[0]
66
67
                if valueType not in ['f', 'v', 'vt', 'vn']:
68
                    continue
69
70
                value = value[1:]
71
72
                # Check first and continue on early because of string splitting
73
                if valueType == "f":
74
                    temp = [item.split("/") for item in value]
75
76
                    for i in range(3):
77
                        if temp[i][1] != '':
78
                            self.uvIndices[fnumber] = int(temp[i][1])
79
                        self.vertexIndices[fnumber] = int(temp[i][0])
80
                        self.normalIndices[fnumber] = int(temp[i][2])
81
                        fnumber += 1
82
83
                    continue
84
85
                value = list(map(float, value))
86
87
                if valueType == "v":
88
                    v = [value[0], value[1], value[2]]
89
                    self.tempVertices[vnumber] = v
90
                    self.vnumber += 1
91
92
                elif valueType == "vt":
93
                    vt = [value[0], value[0]]
94
                    self.tempUVs[vtnumber] = vt
95
                    self.vtnumber += 1
96
97
                elif valueType == "vn":
98
                    n = [value[0], value[1], value[2]]
99
                    self.tempNormals[vnnumber] = n
100
                    self.vnnumber += 1
101
102
    def get_final_data(self):
103
        for i in range(fcount):
104
            vertexIndex = int(self.vertexIndices[i]) - 1
105
            vertex = self.tempVertices[vertexIndex]
106
            self.finalVertices[i] = vertex
107
108
            normalIndex = self.normalIndices[i]
109
            normal = self.tempNormals[int(normalIndex) - 1]
110
            self.finalNormals[i] = normal
111
112
            if self.uvIndices[0] is None and vtcount != 0:
113
                uvIndex = self.uvIndices[i]
114
                uv = self.tempUVs[int(uvIndex) - 1]
115
                self.finalUVs[i] = uv
116
117
class ObjMesh(MeshBase):
118
    def __init__(self, filePath, name, program, vertexLoc, normalLoc):
119
        super(ObjMesh, self).__init__(filePath, name, program, vertexLoc, normalLoc)
120