-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImodView.py
executable file
·114 lines (107 loc) · 4.21 KB
/
ImodView.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import struct
from .utils import is_string, is_integer
class ImodView(object):
def __init__(self,
fid = None,
flags = 0,
red = 0,
green = 0,
blue = 0,
pdrawsize = 0,
linewidth = 1,
linesty = 0,
trans = 0,
clips_count = 0,
clips_flags = 0,
clips_trans = 0,
clips_plane = 0,
clips_normal_x = 0,
clips_normal_y = 0,
clips_normal_z = -1,
clips_points_x = 0,
clips_points_y = 0,
clips_points_z = 0,
ambient = 102,
diffuse = 255,
specular = 127,
shininess = 4,
fillred = 0,
fillgreen = 0,
fillblue = 0,
quality = 0,
mat2 = 0,
valblack = 0,
valwhite = 255,
mat3b2 = 0,
mat3b3 = 0,
clips_normal = [0, 0, -1] * 5,
clips_point = [0] * 15,
**kwargs):
self.__dict__.update(kwargs)
self.__dict__.update(locals())
if self.fid:
self.read_file()
def read_file(self):
fid = self.fid
self.flags = struct.unpack('>I', fid.read(4))[0]
self.red = struct.unpack('>f', fid.read(4))[0]
self.green = struct.unpack('>f', fid.read(4))[0]
self.blue = struct.unpack('>f', fid.read(4))[0]
self.pdrawsize = struct.unpack('>i', fid.read(4))[0]
self.linewidth = struct.unpack('>B', fid.read(1))[0]
self.linesty = struct.unpack('>B', fid.read(1))[0]
self.trans = struct.unpack('>B', fid.read(1))[0]
self.clips_count = struct.unpack('>B', fid.read(1))[0]
self.clips_flags = struct.unpack('>B', fid.read(1))[0]
self.clips_trans = struct.unpack('>B', fid.read(1))[0]
self.clips_plane = struct.unpack('>B', fid.read(1))[0]
self.clips_normal_x = struct.unpack('>f', fid.read(4))[0]
self.clips_normal_y = struct.unpack('>f', fid.read(4))[0]
self.clips_normal_z = struct.unpack('>f', fid.read(4))[0]
self.clips_points_x = struct.unpack('>f', fid.read(4))[0]
self.clips_points_y = struct.unpack('>f', fid.read(4))[0]
self.clips_points_z = struct.unpack('>f', fid.read(4))[0]
self.ambient = struct.unpack('>B', fid.read(1))[0]
self.diffuse = struct.unpack('>B', fid.read(1))[0]
self.specular = struct.unpack('>B', fid.read(1))[0]
self.shininess = struct.unpack('>B', fid.read(1))[0]
self.fillred = struct.unpack('>B', fid.read(1))[0]
self.fillgreen = struct.unpack('>B', fid.read(1))[0]
self.fillblue = struct.unpack('>B', fid.read(1))[0]
self.quality = struct.unpack('>B', fid.read(1))[0]
self.mat2 = struct.unpack('>I', fid.read(4))[0]
self.valblack = struct.unpack('>B', fid.read(1))[0]
self.valwhite = struct.unpack('>B', fid.read(1))[0]
self.mat3b2 = struct.unpack('>B', fid.read(1))[0]
self.mat3b3 = struct.unpack('>B', fid.read(1))[0]
self.clips_normal = []
for i in range(0, 15):
self.clips_normal.append(struct.unpack('>f', fid.read(4))[0])
self.clips_point = []
for i in range(0, 15):
self.clips_point.append(struct.unpack('>f', fid.read(4))[0])
return self
def setColor(self, r, g, b):
"""
Set object color by changing the red, green, and blue variables. The
input color variable must be a string in the format 'R,G,B', where R,
G, and B range either from 0-1 or 0-255.
"""
color = [float(x) for x in [r, g, b]]
if not all(0 <= x <= 255 for x in color):
raise ValueError('Color values must range from 0-1 or 0-255.')
color = [x if x <= 1 else x/255 for x in color]
self.red = color[0]
self.green = color[1]
self.blue = color[2]
def setTransparency(self, transp):
is_integer(transp, 'Transparency')
if not (0 <= transp <= 100):
raise ValueError('Transparency value must range from 0-100.')
self.trans = transp
return self
def dump(self):
from collections import OrderedDict as od
for key, value in od(sorted(self.__dict__.items())).iteritems():
print key, value
print "\n"