]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/datafile.py
Deprecated the Renderer constructor taking a Camera pointer
[libs/gl.git] / blender / io_mspgl / datafile.py
1 class Statement:
2         def __init__(self, keyword, *args):
3                 self.keyword = keyword
4                 self.args = list(args)
5                 self.sub = []
6
7         def append(self, v):
8                 self.args.append(v)
9
10         def write_to_file(self, f, indent=0):
11                 s = self.keyword
12                 for a in self.args:
13                         if type(a)==float:
14                                 s += " {:#.6g}".format(a)
15                         elif type(a)==bool:
16                                 if a:
17                                         s += " true"
18                                 else:
19                                         s += " false"
20                         elif type(a)==str:
21                                 s += ' "{}"'.format(a)
22                         elif type(a)==Resource:
23                                 s += ' "{}"'.format(a.name)
24                         else:
25                                 s += " {}".format(a)
26
27                 tabs = '\t'*indent
28                 f.write("{}{}".format(tabs, s))
29                 if self.sub:
30                         f.write("\n{}{{\n".format(tabs))
31                         for s in self.sub:
32                                 s.write_to_file(f, indent+1)
33                         f.write("{}}};\n".format(tabs))
34                 else:
35                         f.write(";\n")
36
37
38 class Token:
39         def __init__(self, text):
40                 self.text = text
41
42         def __str__(self):
43                 return self.text
44
45
46 class Resource:
47         def __init__(self, name):
48                 self.name = name
49                 self.statements = []
50                 self.references = []
51
52         def create_reference_statement(self, keyword, *args):
53                 if len(args)<1:
54                         raise TypeError("create_reference_statement expected at least 2 arguments, got {}".format(1+len(args)))
55
56                 resources = []
57                 for a in args:
58                         if type(a)==Resource:
59                                 resources.append(a)
60
61                 if not resources:
62                         raise TypeError("create_reference_statement expected a Resource argument, found none")
63
64                 self.references += [r for r in resources if r not in self.references]
65                 return Statement(keyword, *args)
66
67         def create_embed_statement(self, keyword, *args):
68                 if len(args)<1:
69                         raise TypeError("create_embed_statement expected at least 2 arguments, got {}".format(1+len(args)))
70                 res = args[-1]
71                 if type(res)!=Resource:
72                         raise TypeError("create_embed_statement expected a Resource as last argument, got {}".format(type(args[-1]).__name__))
73
74                 self.references += [r for r in res.references if r not in self.references]
75                 st = Statement(keyword, *args[:-1])
76                 st.sub = res.statements
77                 return st
78
79         def collect_references(self):
80                 refs = []
81                 for r in self.references:
82                         refs += [e for e in r.collect_references() if e not in refs]
83                 refs += [e for e in self.references if e not in refs]
84                 return refs