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