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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
#!BPY
# flt_filewalker.py is an utility module for OpenFlight IO scripts for blender.
# Copyright (C) 2005 Greg MacDonald
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
__bpydoc__ ="""\
File read/write module used by OpenFlight I/O and tool scripts. OpenFlight is a
registered trademark of MultiGen-Paradigm, Inc.
"""
import Blender
from struct import *
import re
class FltIn:
def __init__(self, filename):
self.file = open(filename, 'rb')
self.position = 0
self.next_position = 100000
self.opcode = 0
self.length = 0
self.level = 0
self.repeat = False # Repeat the last record.
def begin_record(self):
if self.repeat == True:
self.repeat = False
else:
self.position += self.length
try:
self.file.seek(self.position)
input = self.file.read(4)
except:
print 'Parse Error!'
return False
if not input:
self.close_file()
return False
self.opcode = unpack('>h', input[:2])[0]
self.length = unpack('>H', input[-2:])[0]
self.next_position = self.position + self.length
return True
def repeat_record(self):
self.repeat = True
def get_opcode(self):
return self.opcode
def get_level(self):
return self.level
def up_level(self):
self.level += 1
def down_level(self):
self.level -= 1
def read_string(self, length):
s = ''
if self.file.tell() + length <= self.next_position:
start = self.file.tell()
for i in xrange(length):
char = self.file.read(1)
if char == '\x00':
break
s = s + char
self.file.seek(start+length)
# else:
# print 'Warning: string truncated'
return s
def read_int(self):
if self.file.tell() + 4 <= self.next_position:
return unpack('>i', self.file.read(4))[0]
else:
#print 'Warning: int truncated'
return 0
def read_uint(self):
if self.file.tell() + 4 <= self.next_position:
return unpack('>I', self.file.read(4))[0]
else:
#print 'Warning: uint truncated'
return 0
def read_double(self):
if self.file.tell() + 8 <= self.next_position:
return unpack('>d', self.file.read(8))[0]
else:
#print 'Warning: double truncated'
return 0.0
def read_float(self):
if self.file.tell() + 4 <= self.next_position:
return unpack('>f', self.file.read(4))[0]
else:
#print 'Warning: float truncated'
return 0.0
def read_ushort(self):
if self.file.tell() + 2 <= self.next_position:
return unpack('>H', self.file.read(2))[0]
else:
#print 'Warning: ushort truncated'
return 0
def read_short(self):
if self.file.tell() + 2 <= self.next_position:
return unpack('>h', self.file.read(2))[0]
else:
#print 'Warning: short trunated'
return 0
def read_uchar(self):
if self.file.tell() + 1 <= self.next_position:
return unpack('>B', self.file.read(1))[0]
else:
#print 'Warning: uchar truncated'
return 0
def read_char(self):
if self.file.tell() + 1 <= self.next_position:
return unpack('>b', self.file.read(1))[0]
else:
#print 'Warning: char truncated'
return 0
def read_ahead(self, i):
if self.file.tell() + i <= self.next_position:
self.file.seek(i, 1)
# else:
# print 'Warning: attempt to seek past record'
def get_length(self):
return self.length
def close_file(self):
self.file.close()
class FltOut:
# Length includes terminating null
def write_string(self, string, length):
if len(string) > length - 1:
str_len = length - 1
else:
str_len = len(string)
pad_len = length - str_len
self.file.write(string[:str_len])
self.pad(pad_len)
def write_int(self, a):
self.file.write( pack('>i', a) )
def write_uint(self, a):
self.file.write( pack('>I', a) )
def write_double(self, a):
self.file.write( pack('>d', a) )
def write_float(self, a):
self.file.write( pack('>f', a) )
def write_ushort(self, a):
self.file.write( pack('>H', a) )
def write_short(self, a):
self.file.write( pack('>h', a) )
def write_uchar(self, a):
self.file.write( pack('>B', a) )
def write_char(self, a):
self.file.write( pack('>b', a) )
def pad(self, reps):
for i in xrange(reps):
self.file.write('\x00')
def close_file(self):
self.file.close()
def __init__(self, filename):
self.file = open(filename, 'wb')
self.filename = filename
class FileFinder:
def add_file_to_search_path(self, filename):
dir = Blender.sys.dirname(filename)
if dir != None and dir != '':
self.search_dirs.append(dir)
def strip_path(self, full_path):
# One of my flt files had a windows path with unix seperation. Basename
# returned the whole path + filename, which isn't expected. So my
# attempt to fix it is to replace all / or \ with the platform specific
# dir seperator.
#
# note: \\\\ is actually just one \ indirected twice, once for python
# then again for re.sub
if Blender.sys.sep == '\\':
full_path = re.sub('/', '\\\\', full_path)
elif Blender.sys.sep == '/':
full_path = re.sub('\\\\', '/', full_path)
filename = Blender.sys.basename(full_path)
return filename
def find(self, full_path):
if full_path == '':
return None
# Seperate out the path.
dirname = Blender.sys.dirname(full_path)
# Try it first.
if Blender.sys.exists(full_path):
if not dirname in self.search_dirs:
self.search_dirs.append(dirname)
return full_path
# Maybe it's relative.
for path in self.search_dirs:
rel_full_path = Blender.sys.join(path, full_path)
if Blender.sys.exists(rel_full_path):
return rel_full_path
# Search previous directories that have worked.
filename = self.strip_path(full_path)
for path in self.search_dirs:
t = Blender.sys.join(path, filename)
if Blender.sys.exists(t):
return t
# Ask user where it is.
self.user_input = Blender.Draw.PupStrInput(filename + "? ", '', 100)
#self.user_input = None
if self.user_input != None:
t = Blender.sys.join(self.user_input, filename)
if Blender.sys.exists(t):
user_dirname = Blender.sys.dirname(t)
if not user_dirname in self.search_dirs:
self.search_dirs.append(user_dirname)
return t
# Couldn't find it.
return None
def __init__(self):
self.user_input = ''
self.current_file = ''
self.search_dirs = []
dir = Blender.Get('texturesdir')
if dir != None and dir != '':
self.search_dirs.append(dir)
dir = Blender.sys.dirname(Blender.Get('filename'))
if dir != None and dir != '':
print dir
self.search_dirs.append(dir)
|