Conditions | 17 |
Total Lines | 82 |
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.OBJObject.__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 | from ed2d.assets.mtlloader import MTL |
||
43 | def __init__(self, data): |
||
44 | # Store the lines that represent one object from obj file. |
||
45 | self.object = data |
||
46 | # Size of the object |
||
47 | self.objectlen = len(self.object) |
||
48 | # Store number of faces/indices |
||
49 | self.fcount = 0 |
||
50 | # Store number of vertices |
||
51 | self.vcount = 0 |
||
52 | # Store numver of uv indices |
||
53 | self.vtcount = 0 |
||
54 | # Store number of normals |
||
55 | self.vncount = 0 |
||
56 | # Store number of materials used |
||
57 | self.gcount = 0 |
||
58 | |||
59 | # Indices dict |
||
60 | self.tmvnig = {} |
||
61 | # Final parsed data dict |
||
62 | self.fmvnig = {} |
||
63 | |||
64 | # Material list |
||
65 | self.matList = [] |
||
66 | |||
67 | # Do a head count and setup the storage layout using dictionaries |
||
68 | for i in range(self.objectlen): |
||
69 | value = self.object[i][:2] |
||
70 | if value == 'v ': |
||
71 | self.vcount += 1 |
||
72 | elif value == 'vt': |
||
73 | self.vtcount += 1 |
||
74 | elif value == 'vn': |
||
75 | self.vncount += 1 |
||
76 | elif value == 'g ': |
||
77 | self.gcount += 1 |
||
78 | # The material used currently |
||
79 | materialName = None |
||
80 | if self.object[i+1][:6] == 'usemtl': |
||
81 | materialName = self.object[i+1].split()[1] |
||
82 | self.matList.append(materialName) |
||
83 | # Initialize the two dictionaries, indices and final data |
||
84 | self.tmvnig[materialName] = [ [], [], [] ] |
||
85 | self.fmvnig[materialName] = [ [], [], [] ] |
||
86 | |||
87 | |||
88 | for j in range(i+2, self.objectlen, 1): |
||
89 | fval = self.object[j][:2] |
||
90 | |||
91 | if fval == 'f ': |
||
92 | self.fcount += 1 |
||
93 | # Vertices |
||
94 | self.tmvnig[materialName][0].extend(None for _ in range(3)) |
||
95 | self.fmvnig[materialName][0].extend([None] for _ in range(3)) |
||
96 | # Normals |
||
97 | self.tmvnig[materialName][2].extend(None for _ in range(3)) |
||
98 | self.fmvnig[materialName][2].extend([None] for _ in range(3)) |
||
99 | # UV Coordinates (usually these are not always generated) |
||
100 | if self.vtcount != 0: |
||
101 | self.tmvnig[materialName][1].extend(None for _ in range(3)) |
||
102 | self.fmvnig[materialName][1].extend([None] for _ in range(3)) |
||
103 | elif fval == 'g ': |
||
104 | # We need this here for any material that is not the last |
||
105 | # Otherwise it will not know when to stop |
||
106 | break |
||
107 | |||
108 | # Faces, Vertices, UVs, Normals each store 3 values per line |
||
109 | self.fcount *= 3 |
||
110 | self.vcount *= 3 |
||
111 | self.vtcount *= 3 |
||
112 | self.vncount *= 3 |
||
113 | |||
114 | # Create temporary storage |
||
115 | self.tempVertices = [None] * self.vcount |
||
116 | self.tempNormals = [None] * self.vncount |
||
117 | self.tempUVs = [None] * self.fcount |
||
118 | |||
119 | # Store the final counts |
||
120 | self.fnumber = 0 |
||
121 | self.vnumber = 0 |
||
122 | self.vtnumber = 0 |
||
123 | self.vnnumber = 0 |
||
124 | self.matnumber = 0 |
||
125 | |||
293 |