]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export.py
9e60f28487f0ccd8b7b42aaad14484b2e72d530f
[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 = ctx.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                 scene_queue = []
93                 sequences = []
94                 for s in ctx.context.blend_data.scenes:
95                         subtask = task.next_slice(s)
96                         if s.export_disposition=='IGNORE':
97                                 continue
98
99                         if s.export_disposition=='SEQUENCE':
100                                 scene = create_scene_chain(s, scenes)
101                                 sequences.append(scene)
102                                 scene_queue.append(scene)
103                         elif s.export_disposition!='IGNORE' and s.name not in scenes:
104                                 scene = create_scene(s)
105                                 scenes[scene.name] = scene
106                                 if s.export_disposition=='SCENE':
107                                         scene_queue.append(scene)
108
109                 all_objects = []
110                 for s in scenes.values():
111                         all_objects += [p.object for p in s.prototypes]
112                         all_objects += s.lights
113                         if s.camera:
114                                 all_objects.append(s.camera)
115
116                 ordered_scenes = []
117                 while scene_queue:
118                         s = scene_queue.pop(0)
119                         if not s.background_set or s.background_set in ordered_scenes:
120                                 ordered_scenes.append(s)
121                         else:
122                                 scene_queue.append(s)
123
124                 from .util import make_unique
125                 all_objects = make_unique(all_objects)
126
127                 from .export_scene import SceneExporter
128                 scene_exporter = SceneExporter()
129                 data_exporter = DataExporter()
130
131                 task = ctx.task("Exporting resources", 1.0)
132                 resources = {}
133                 dummy_res = data_exporter.export_resources(task, all_objects, resources)
134
135                 task = ctx.task("Exporting scenes", 1.0)
136                 for s in ordered_scenes:
137                         subtask = task.task(s, 0.5)
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                         subtask = task.task(s, 0.5)
146                         seq_name = s.name+".seq"
147                         if seq_name not in resources:
148                                 scene_exporter.export_sequence_resources(s, resources)
149                                 seq_res = scene_exporter.export_sequence(s, resources)
150                                 resources[seq_name] = seq_res
151                                 dummy_res.create_reference_statement("ref", seq_res)
152
153                 task = ctx.task("Writing files", 1.0)
154                 refs = dummy_res.collect_references()
155                 for r in refs:
156                         r.write_to_file(os.path.join(out_dir, r.name))