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