]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export.py
Redesign progress and error reporting in the Blender exporter
[libs/gl.git] / blender / io_mspgl / export.py
1 import os
2 import itertools
3
4 class DataExporter:
5         def export_to_file(self, ctx, out_fn, *, collection=False, shared_resources=False):
6                 objects = context.context.selected_objects
7
8                 resources = {}
9                 material_atlases = {}
10
11                 task = ctx.task("Exporting resources", 1.0)
12                 dummy_res = self.export_resources(task, objects, resources, material_atlases)
13
14                 path, base = os.path.split(out_fn)
15                 base, ext = os.path.splitext(base)
16
17                 task = ctx.task("Writing files", 1.0)
18                 refs = dummy_res.collect_references()
19                 if not shared_resources:
20                         numbers = {}
21                         for r in refs:
22                                 res_ext = os.path.splitext(r.name)[1]
23                                 n = numbers.get(res_ext, 0)
24                                 if n>0:
25                                         r.name = "{}_{}{}".format(base, n, res_ext)
26                                 else:
27                                         r.name = base+res_ext
28                                 numbers[res_ext] = n+1
29
30                 if collection:
31                         dummy_res.write_collection(out_fn, exclude_self=True)
32                 else:
33                         for r in refs:
34                                 r.write_to_file(os.path.join(path, r.name))
35
36         def export_resources(self, ctx, objects, resources, material_atlases):
37                 if material_atlases is None:
38                         material_atlases = {}
39
40                 object_exporter = None
41                 camera_exporter = None
42                 armature_exporter = None
43                 light_exporter = None
44
45                 from .datafile import Resource
46                 dummy_res = Resource("dummy", "dummy")
47
48                 ctx.set_slices(len(objects))
49                 for obj in objects:
50                         task = ctx.next_slice(obj)
51                         res_name = None
52                         res = None
53                         if obj.type=='MESH':
54                                 res_name = obj.name+".object"
55                                 if res_name not in resources:
56                                         if not object_exporter:
57                                                 from .export_object import ObjectExporter
58                                                 object_exporter = ObjectExporter()
59                                         object_exporter.export_object_resources(task, obj, resources, material_atlases)
60                                         res = object_exporter.export_object(obj, resources)
61                         elif obj.type=='CAMERA':
62                                 res_name = obj.name+".camera"
63                                 if res_name not in resources:
64                                         if not camera_exporter:
65                                                 from .export_camera import CameraExporter
66                                                 camera_exporter = CameraExporter()
67                                         res = camera_exporter.export_camera(obj)
68                         elif obj.type=='ARMATURE':
69                                 res_name = obj.name+".arma"
70                                 if res_name not in resources:
71                                         if not armature_exporter:
72                                                 from .export_armature import ArmatureExporter
73                                                 armature_exporter = ArmatureExporter()
74                                         res = armature_exporter.export_armature(obj)
75                         elif obj.type=='LIGHT':
76                                 res_name = obj.name+".light"
77                                 if res_name not in resources:
78                                         if not light_exporter:
79                                                 from .export_light import LightExporter
80                                                 light_exporter = LightExporter()
81                                         res = light_exporter.export_light(obj)
82
83                         if res:
84                                 resources[res_name] = res
85                                 dummy_res.create_reference_statement("ref", res)
86
87                 return dummy_res
88
89 class ProjectExporter:
90         def export_to_directory(self, ctx, out_dir):
91                 from .scene import create_scene_chain
92
93                 task = ctx.task("Preparing scenes", 0.0)
94                 task.set_slices(len(ctx.context.blend_data.scenes))
95
96                 scenes = {}
97                 sequences = []
98                 for s in ctx.context.blend_data.scenes:
99                         subtask = task.next_slice(s)
100                         if s.export_disposition=='IGNORE':
101                                 continue
102
103                         if s.export_disposition=='SEQUENCE':
104                                 scene = create_scene_chain(s, scenes)
105                                 sequences.append(scene)
106                         elif s.export_disposition!='IGNORE' and s.name not in scenes:
107                                 scene = create_scene(s)
108                                 if s.export_disposition=='SCENE':
109                                         scenes[scene.name] = scene
110
111                 all_objects = []
112                 for s in scenes.values():
113                         all_objects += s.prototypes
114                         all_objects += s.lights
115                         if s.camera:
116                                 all_objects.append(s.camera)
117
118                 scene_queue = list(scenes.values())
119                 ordered_scenes = []
120                 while scene_queue:
121                         s = scene_queue.pop(0)
122                         if not s.background_set or s.background_set in ordered_scenes:
123                                 ordered_scenes.append(s)
124                         else:
125                                 scene_queue.append(s)
126
127                 from .util import make_unique
128                 all_objects = make_unique(all_objects)
129
130                 from .export_scene import SceneExporter
131                 scene_exporter = SceneExporter()
132                 data_exporter = DataExporter()
133
134                 task = ctx.task("Exporting resources", 1.0)
135                 resources = {}
136                 dummy_res = data_exporter.export_resources(task, all_objects, resources, None)
137
138                 task = ctx.task("Exporting scenes", 1.0)
139                 for s in ordered_scenes:
140                         subtask = task.task(s, 0.5)
141                         scene_name = s.name+".scene"
142                         if scene_name not in resources:
143                                 scene_res = scene_exporter.export_scene(s, resources)
144                                 resources[scene_name] = scene_res
145                                 dummy_res.create_reference_statement("ref", scene_res)
146
147                 for s in sequences:
148                         subtask = task.task(s, 0.5)
149                         seq_name = s.name+".seq"
150                         if seq_name not in resources:
151                                 scene_exporter.export_sequence_resources(s, resources)
152                                 seq_res = scene_exporter.export_sequence(s, resources)
153                                 resources[seq_name] = seq_res
154                                 dummy_res.create_reference_statement("ref", seq_res)
155
156                 task = ctx.task("Writing files", 1.0)
157                 refs = dummy_res.collect_references()
158                 for r in refs:
159                         r.write_to_file(os.path.join(out_dir, r.name))