Initial revision
[blender.git] / intern / python / modules / vrml / loader.py
1 # The VRML loader
2 # supports gzipped files
3
4 # TODO: better progress monitoring
5
6 import parser 
7
8 def quiet(txt):
9         pass
10
11 debug = quiet
12
13 def debug1(txt):
14         print "Loader:", txt
15
16 g_last = 0
17
18 def getFileType(file):
19         "returns the file type string from 'file'"
20         file.seek(0)
21         magic = file.readline()
22         if magic[:3] == '\037\213\010':
23                 file.seek(0)
24                 return "gzip"
25         elif magic[:10] == '#VRML V2.0':
26                 file.seek(0)
27                 return "vrml"
28         else:
29                 file.seek(0)
30                 return ""
31
32 class Loader:
33         def __init__(self, url, progress = None):
34                 self.url = url
35                 self.debug = debug
36                 self.fail = debug
37                 self.monitor = debug
38                 self.progress = progress
39                 self.nodes = 0 # number of nodes parsed
40
41         def getGzipFile(self, file):
42                 '''Return gzip file (only called when gzip type is recognised)'''
43                 # we now have the local filename and the headers
44                 # read the first few bytes, check for gzip magic number
45                 self.monitor( "gzip-encoded file... loading gzip library")
46                 try:
47                         import gzip
48                         file = gzip.open(file,"rb")
49                         return file
50                 except ImportError, value:
51                         self.fail("Gzip library unavailable, compressed file cannot be read")
52                 except:
53                         self.fail("Failed to open Gzip file")
54
55                 return None
56
57         def load(self):
58                 self.debug("try: load file from %s" % self.url)
59                 url = self.url
60
61                 # XXX
62                 try:
63                         file = open(url, 'rb')
64                 except IOError, val:
65                         self.debug("couldn't open file %s" % url)
66                         return None
67
68                 if getFileType(file) == 'gzip':
69                         file.close()
70                         file = self.getGzipFile(url)
71                 try:
72                         data = file.read()
73                 except MemoryError, value:
74                         self.fail("Insufficient memory to load file as string", value)
75                         return None
76                 except IOError, value:
77                                 self.fail("I/O Error while reading data from file %s "% url)
78                 p = parser.Parser(data)
79                 if self.progress:
80                         scenegraph = p.parse(self.progress)
81                         print "progress"
82                 else:
83                         scenegraph = p.parse()
84                         
85                 self.nodes = p.progresscount # progress
86                 del p
87                 return scenegraph
88
89
90 def load(url, progress = None):
91         l = Loader(url, progress)
92         return l.load()
93         
94 def test(name = None):  
95         if not name:
96                 name = '/tmp/gna.wrl'
97         return load(name)