Welcome to mirror list, hosted at ThFree Co, Russian Federation.

loader.py « vrml « modules « python « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dd53fe49fd3460f9c522773c1995fbc5ff899596 (plain)
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
# The VRML loader
# supports gzipped files
# 
# TODO: better progress monitoring

import parser 

def quiet(txt):
	pass

debug = quiet

def debug1(txt):
	print "Loader:", txt

g_last = 0

def getFileType(file):
	"returns the file type string from 'file'"
	file.seek(0)
	magic = file.readline()
	if magic[:3] == '\037\213\010':
		file.seek(0)
		return "gzip"
	elif magic[:10] == '#VRML V2.0':
		file.seek(0)
		return "vrml"
	else:
		file.seek(0)
		return ""

class Loader:
	def __init__(self, url, progress = None):
		self.url = url
		self.debug = debug
		self.fail = debug
		self.monitor = debug
		self.progress = progress
		self.nodes = 0 # number of nodes parsed

	def getGzipFile(self, file):
		'''Return gzip file (only called when gzip type is recognised)'''
		# we now have the local filename and the headers
		# read the first few bytes, check for gzip magic number
		self.monitor( "gzip-encoded file... loading gzip library")
		try:
			import gzip
			file = gzip.open(file,"rb")
			return file
		except ImportError, value:
			self.fail("Gzip library unavailable, compressed file cannot be read")
		except:
			self.fail("Failed to open Gzip file")

		return None

	def load(self):
		self.debug("try: load file from %s" % self.url)
		url = self.url

		# XXX
		try:
			file = open(url, 'rb')
		except IOError, val:
			self.debug("couldn't open file %s" % url)
			return None

		if getFileType(file) == 'gzip':
			file.close()
			file = self.getGzipFile(url)
		try:
			data = file.read()
		except MemoryError, value:
			self.fail("Insufficient memory to load file as string", value)
			return None
		except IOError, value:
				self.fail("I/O Error while reading data from file %s "% url)
		p = parser.Parser(data)
		if self.progress:
			scenegraph = p.parse(self.progress)
			print "progress"
		else:
			scenegraph = p.parse()
			
		self.nodes = p.progresscount # progress
		del p
		return scenegraph


def load(url, progress = None):
	l = Loader(url, progress)
	return l.load()
	
def test(name = None):	
	if not name:
		name = '/tmp/gna.wrl'
	return load(name)