Completed
Push — master ( 454eb4...f83d10 )
by
unknown
07:14
created

ed2d.assets.OBJ.__init__()   C

Complexity

Conditions 7

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 7
dl 0
loc 57
rs 6.6397

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        matcount = 0
13
14
        # Load the file
15
        objfile = open(fileName, "r")
16
17
        # Do a head count
18
        for line in objfile:
19
            value = line[:2]
20
            if value == 'f ':
21
                fcount += 1
22
            elif value == 'v ':
23
                vcount += 1
24
            elif value == 'vt':
25
                vtcount += 1
26
            elif value == 'vn':
27
                vncount += 1
28
            elif value == 'g ':
29
                matcount += 1
30
31
        fcount *= 3
32
        vcount *= 3
33
        vtcount *= 3
34
        vncount *= 3
35
36
        self.tempVertices = [None] * vcount
37
        self.tempNormals = [None] * vncount
38
        self.tempUVs = [None] * fcount
39
        self.tempMaterials = [None] * matcount
40
41
        self.vertexIndices = [None] * fcount
42
        self.normalIndices = [None] * fcount
43
        self.uvIndices = [None] * fcount
44
45
        self.finalVertices = [None] * fcount
46
        self.finalNormals = [None] * fcount
47
        self.finalUVs = [None] * fcount
48
        self.usedMaterials = [None] * matcount
49
50
        self.fnumber = 0
51
        self.vnumber = 0
52
        self.vtnumber = 0
53
        self.vnnumber = 0
54
        self.matnumber = 0
55
56
        # Process the data
57
        self.__process_in_house(objfile)
58
        # Finalize
59
        self.get_final_data()
60
61
        # Close the file
62
        objfile.close()
63
64
    def __process_in_house(self, filename):
65
        with open(filename, "r") as objfl:
66
67
            for line in objfl:
68
69
                value = line.split()
70
                valueType = value[0]
71
72
                if valueType not in ['f', 'v', 'vt', 'vn', 'g', 'usemtl']:
73
                    continue
74
75
                value = value[1:]
76
77
                # Check first and continue on early because of string splitting
78
                if valueType == "f":
79
                    temp = [item.split("/") for item in value]
80
81
                    for i in range(3):
82
                        if temp[i][1] != '':
83
                            self.uvIndices[self.fnumber] = int(temp[i][1])
84
                        self.vertexIndices[self.fnumber] = int(temp[i][0])
85
                        self.normalIndices[self.fnumber] = int(temp[i][2])
86
                        self.fnumber += 1
87
88
                    continue
89
90
                value = list(map(float, value))
91
92
                if valueType == "v":
93
                    v = [value[0], value[1], value[2]]
94
                    self.tempVertices[self.vnumber] = v
95
                    self.vnumber += 1
96
97
                elif valueType == "vt":
98
                    vt = [value[0], value[0]]
99
                    self.tempUVs[self.vtnumber] = vt
100
                    self.vtnumber += 1
101
102
                elif valueType == "vn":
103
                    n = [value[0], value[1], value[2]]
104
                    self.tempNormals[self.vnnumber] = n
105
                    self.vnnumber += 1
106
107
                elif valueType == "g":
108
                    g = value[0]
109
                    self.tempMaterials[self.matnumber] = g
110
                    self.matnumber += 1
111
                elif valueType == "usemtl":
112
                    gs = value[0]
113
                    # Need a way to generate which material per what face
114
                    # In a specific order
115
                    self.usedMaterials[sef.matnumber] = gs
116
117
    def get_final_data(self):
118
        for i in range(self.fcount):
119
            vertexIndex = int(self.vertexIndices[i]) - 1
120
            vertex = self.tempVertices[vertexIndex]
121
            self.finalVertices[i] = vertex
122
123
            normalIndex = self.normalIndices[i]
124
            normal = self.tempNormals[int(normalIndex) - 1]
125
            self.finalNormals[i] = normal
126
127
            if self.uvIndices[0] is None and self.vtcount != 0:
128
                uvIndex = self.uvIndices[i]
129
                uv = self.tempUVs[int(uvIndex) - 1]
130
                self.finalUVs[i] = uv
131
132
class ObjMesh(MeshBase):
133
    def __init__(self, filePath, name, program, vertexLoc, normalLoc):
134
        super(ObjMesh, self).__init__(filePath, name, program, vertexLoc, normalLoc)
135