]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export.py
Refactor scene export to use inline scenes instead of a wrapper scene
[libs/gl.git] / blender / io_mspgl / export.py
1 import os
2 import itertools
3
4 class DataExporter:
5         def export_to_file(self, context, out_fn, *, collection=False, shared_resources=False):
6                 from .util import Progress
7                 progress = Progress(context)
8
9                 objects = context.selected_objects
10
11                 resources = {}
12                 material_atlases = {}
13
14                 dummy_res = self.export_resources(context, objects, resources, material_atlases, progress)
15
16                 path, base = os.path.split(out_fn)
17                 base, ext = os.path.splitext(base)
18
19                 refs = dummy_res.collect_references()
20                 if not shared_resources:
21                         numbers = {}
22                         for r in refs:
23                                 res_ext = os.path.splitext(r.name)[1]
24                                 n = numbers.get(res_ext, 0)
25                                 if n>0:
26                                         r.name = "{}_{}{}".format(base, n, res_ext)
27                                 else:
28                                         r.name = base+res_ext
29                                 numbers[res_ext] = n+1
30
31                 if collection:
32                         dummy_res.write_collection(out_fn, exclude_self=True)
33                 else:
34                         for r in refs:
35                                 r.write_to_file(os.path.join(path, r.name))
36
37         def export_resources(self, context, objects, resources, material_atlases, progress):
38                 if material_atlases is None:
39                         material_atlases = {}
40
41                 object_exporter = None
42                 camera_exporter = None
43                 armature_exporter = None
44                 light_exporter = None
45
46                 from .datafile import Resource
47                 dummy_res = Resource("dummy", "dummy")
48
49                 for i, obj in enumerate(objects):
50                         progress.push_task_slice(obj.name, i, len(objects))
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(context, obj, resources, material_atlases, progress)
60                                         res = object_exporter.export_object(obj, resources, progress)
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(context, 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                         progress.pop_task()
88
89                 return dummy_res
90
91 class ProjectExporter:
92         def export_to_directory(self, context, out_dir):
93                 from .util import Progress
94                 progress = Progress(context)
95
96                 from .scene import create_scene_chain
97
98                 scenes = {}
99                 sequences = []
100                 for s in context.blend_data.scenes:
101                         if s.export_disposition=='IGNORE':
102                                 continue
103
104                         if s.export_disposition=='SEQUENCE':
105                                 scene = create_scene_chain(s, scenes)
106                                 sequences.append(scene)
107                         elif s.name not in scenes:
108                                 scene = create_scene(s)
109                                 if s.export_disposition=='SCENE':
110                                         scenes[scene.name] = scene
111
112                 all_objects = []
113                 for s in scenes.values():
114                         all_objects += s.prototypes
115                         all_objects += s.lights
116                         if s.camera:
117                                 all_objects.append(s.camera)
118
119                 scene_queue = list(scenes.values())
120                 ordered_scenes = []
121                 while scene_queue:
122                         s = scene_queue.pop(0)
123                         if not s.background_set or s.background_set in ordered_scenes:
124                                 ordered_scenes.append(s)
125                         else:
126                                 scene_queue.append(s)
127
128                 from .util import make_unique
129                 all_objects = make_unique(all_objects)
130
131                 from .export_scene import SceneExporter
132                 scene_exporter = SceneExporter()
133                 data_exporter = DataExporter()
134
135                 resources = {}
136                 dummy_res = data_exporter.export_resources(context, all_objects, resources, None, progress)
137                 for s in ordered_scenes:
138                         scene_name = s.name+".scene"
139                         if scene_name not in resources:
140                                 scene_res = scene_exporter.export_scene(s, resources)
141                                 resources[scene_name] = scene_res
142                                 dummy_res.create_reference_statement("ref", scene_res)
143
144                 for s in sequences:
145                         seq_name = s.name+".seq"
146                         if seq_name not in resources:
147                                 scene_exporter.export_sequence_resources(s, resources)
148                                 seq_res = scene_exporter.export_sequence(s, resources)
149                                 resources[seq_name] = seq_res
150                                 dummy_res.create_reference_statement("ref", seq_res)
151
152                 refs = dummy_res.collect_references()
153                 for r in refs:
154                         r.write_to_file(os.path.join(out_dir, r.name))