]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/context.py
Check the flat qualifier from the correct member
[libs/gl.git] / blender / io_mspgl / context.py
1 class ExportError(Exception):
2         def __init__(self, objs):
3                 super().__init__(" -> ".join(o.name for o in objs))
4                 self.objs = objs
5
6 class ExportContext:
7         def __init__(self, ctx, *args, verbose=False):
8                 self.obj = None
9
10                 if type(ctx)==ExportContext:
11                         self.level = ctx.level+1
12                         self.parent = ctx
13                         self.context = ctx.context
14                         self.verbose = ctx.verbose
15                         self.descr, self.start, self.delta = args
16                         if type(self.descr)!=str:
17                                 self.obj = self.descr
18                                 self.descr = self.descr.name
19                 else:
20                         self.level = 0
21                         self.parent = None
22                         self.context = ctx
23                         self.verbose = verbose
24                         self.descr = None
25                         self.start = 0.0
26                         self.delta = 1.0
27                         self.context.window_manager.progress_begin(self.start, self.delta)
28
29                 if self.verbose and self.descr:
30                         d = self.descr
31                         if self.obj:
32                                 d += " ({})".format(type(self.obj).__name__)
33                         print("{} {}".format("ยท"*self.level, d))
34
35                 self.slice_delta = 0.0
36                 self.progress = self.start
37                 self.child = None
38                 self.context.window_manager.progress_update(self.progress)
39                 self.last_progress_update = self.progress
40
41         def export(self, func, *args, **kwargs):
42                 try:
43                         func(self, *args, **kwargs)
44                 except Exception as e:
45                         tasks = []
46                         c = self
47                         while c:
48                                 tasks.append(c)
49                                 c = c.child
50                         objs = [t.obj for t in tasks if t.obj]
51                         raise ExportError(objs) from e
52
53         def task(self, task, end):
54                 self.child = ExportContext(self, task, self.progress, self.start+end*self.delta-self.progress)
55                 self.progress += self.child.delta
56                 return self.child
57
58         def set_progress(self, progr):
59                 self.progress = self.start+progr*self.delta
60                 self.child = None
61                 if self.progress>self.last_progress_update+0.001:
62                         self.context.window_manager.progress_update(self.progress)
63                         self.last_progress_update = self.progress
64
65         def set_slices(self, count):
66                 if count>0:
67                         self.slice_delta = self.delta/count
68                 else:
69                         self.slice_delta = 0.0
70
71         def next_slice(self, task):
72                 self.child = ExportContext(self, task, self.progress, self.slice_delta)
73                 self.progress += self.slice_delta
74                 return self.child