Conditions | 17 |
Total Lines | 85 |
Lines | 0 |
Ratio | 0 % |
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:
If many parameters/temporary variables are present:
Complex classes like ed2d.assets.OBJ.__init__() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | |||
7 | def __init__(self, fileName): |
||
8 | ''' Wavefront .obj file parser.''' |
||
9 | fcount = 0 |
||
10 | vcount = 0 |
||
11 | vtcount = 0 |
||
12 | vncount = 0 |
||
13 | gcount = 0 |
||
14 | |||
15 | self.tmvnig = {} # Indices dictionary |
||
16 | self.fmvnig = {} # Final data dictionary |
||
17 | |||
18 | __objpath = files.resolve_path('data', 'models', fileName + '.obj') |
||
19 | __mtlpath = files.resolve_path('data', 'models', fileName + '.mtl') |
||
20 | __txtpath = files.resolve_path('data', 'models', fileName + '.txt') |
||
21 | |||
22 | # Load the mtl file |
||
23 | self.mtlfile = MTL(__mtlpath) |
||
24 | |||
25 | # Load the obj file |
||
26 | objfile = open(__objpath, "r") |
||
27 | |||
28 | lines = objfile.readlines() |
||
29 | lineslen = len(lines) |
||
30 | |||
31 | # Do a head count and setup the storage layout using dictionaries |
||
32 | for i in range(lineslen): |
||
33 | value = lines[i][:2] |
||
34 | if value == 'v ': |
||
35 | vcount += 1 |
||
36 | elif value == 'vt': |
||
37 | vtcount += 1 |
||
38 | elif value == 'vn': |
||
39 | vncount += 1 |
||
40 | elif value == 'g ': |
||
41 | gcount += 1 |
||
42 | # The material used currently |
||
43 | materialName = None |
||
44 | if lines[i+1][:6] == 'usemtl': |
||
45 | materialName = lines[i+1].split()[1] |
||
46 | # Initialize the two dictionaries, indices and final data |
||
47 | self.tmvnig[materialName] = [ [], [], [] ] |
||
48 | self.fmvnig[materialName] = [ [], [], [] ] |
||
49 | |||
50 | for j in range(i+2, lineslen, 1): |
||
51 | fval = lines[j][:2] |
||
52 | |||
53 | if fval == 'f ': |
||
54 | fcount += 1 |
||
55 | # Vertices |
||
56 | self.tmvnig[materialName][0].extend(None for _ in range(3)) |
||
57 | self.fmvnig[materialName][0].extend([None] for _ in range(3)) |
||
58 | # Normals |
||
59 | self.tmvnig[materialName][2].extend(None for _ in range(3)) |
||
60 | self.fmvnig[materialName][2].extend([None] for _ in range(3)) |
||
61 | # UV Coordinates (usually these are not always generated) |
||
62 | if vtcount != 0: |
||
63 | self.tmvnig[materialName][1].extend(None for _ in range(3)) |
||
64 | self.fmvnig[materialName][1].extend([None] for _ in range(3)) |
||
65 | elif fval == 'g ': |
||
66 | # We need this here for any material that is not the last |
||
67 | # Otherwise it will not know when to stop |
||
68 | break |
||
69 | |||
70 | # Close the file |
||
71 | objfile.close() |
||
72 | |||
73 | fcount *= 3 |
||
74 | vcount *= 3 |
||
75 | vtcount *= 3 |
||
76 | vncount *= 3 |
||
77 | |||
78 | self.tempVertices = [None] * vcount |
||
79 | self.tempNormals = [None] * vncount |
||
80 | self.tempUVs = [None] * fcount |
||
81 | |||
82 | self.fnumber = 0 |
||
83 | self.vnumber = 0 |
||
84 | self.vtnumber = 0 |
||
85 | self.vnnumber = 0 |
||
86 | self.matnumber = 0 |
||
87 | |||
88 | # Process the data |
||
89 | self.__process_in_house(__objpath) |
||
90 | # Finalize |
||
91 | self.get_final_data() |
||
92 | |||
189 |