MTL.__load()   F
last analyzed

Complexity

Conditions 15

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 15
dl 0
loc 58
rs 3.2148
c 2
b 0
f 0

How to fix   Long Method    Complexity   

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:

Complexity

Complex classes like MTL.__load() 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 import material
2
3
class MTL(object):
4
    def __init__(self, filename):
5
        ''' Wavefront .mtl file parser. '''
6
        self.data = {}
7
        self.material = material.Material()
8
9
        # The file name is passed in with the full path via OBJ parser.
10
        self.__load(filename)
11
12
    def __load(self, filename):
13
        materialName = None
14
        value = None
15
        valueType = None
16
17
        for line in open(filename, "r"):
18
            # Avoid lines starting with "#"
19
            if line.startswith('#'):
20
                continue
21
            else:
22
                value = line.split()
23
                # Make sure the line is no empty
24
                if not value:
25
                    continue
26
                else:
27
                    valueType = value[0]
28
29
            # Check for material
30
            if valueType == 'newmtl':
31
                # Store current material and make it active
32
                self.material = self.data[value[1]] = material.Material()
33
                materialName = value[1]
34
                continue
35
            elif self.material is None:
36
                print("Error, material member is None.")
37
                break
38
39
            # Generate a list of floats using the numbers
40
            value = list(map(float, value[1:]))
41
42
            if valueType == 'Ns':
43
                # Material Specular Exponent which is multipled by texture value
44
                self.data[materialName].ispecular = value[0]
45
            elif valueType == 'Ka':
46
                # Ambient color
47
                self.data[materialName].ambient = [value[0], value[1], value[2]]
48
            elif valueType == 'Kd':
49
                # Diffuse color
50
                self.data[materialName].diffuse = [value[0], value[1], value[2]]
51
            elif valueType == 'Ks':
52
                # Specular color
53
                self.data[materialName].specular = [value[0], value[1], value[2]]
54
            elif valueType == 'Ke':
55
                # Emission color
56
                self.data[materialName].emission = [value[0], value[1], value[2]]
57
            elif valueType == 'Ni':
58
                # Optical Denisty or Index of Refraction
59
                self.data[materialName].IOR = value[0]
60
            elif valueType == 'map_Kd':
61
                # Diffuse texture is multiplied by the Kd
62
                self.data[materialName].albedoLayers['test'] = value[0]
63
                # Do texture stuff here
64
            elif valueType == 'map_Ks':
65
                # Specular texture is multiplied by the Ks
66
                self.data[materialName].specularMapLayers['test'] = value[0]
67
            elif valueType == 'map_Ns':
68
                # Texture linked to the specular exponent and is multiplied by Ns
69
                pass
70
71
# NOTE:
72
'''
73
During rendering, the Ka, Kd, and Ks values and the map_Ka, map_Kd, and map_Ks values are blended according to the following formula:
74
75
result_color=tex_color(tv)*decal(tv)+mtl_color*(1.0-decal(tv))
76
77
where tv is the texture vertex.
78
79
"result_color" is the blended Ka, Kd, and Ks values.
80
'''
81