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