]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_scene.py
Add a missing file
[libs/gl.git] / blender / io_mspgl / export_scene.py
1 import math
2 import os
3
4 class SceneExporter:
5         def __init__(self):
6                 self.resource_collection = True
7                 self.show_progress = True
8
9         def export_to_file(self, context, out_fn):
10                 objs = [o for o in context.selected_objects if o.type=="MESH" and not o.lod_for_parent]
11                 objs = [o for o in objs if (not o.compound or o.parent not in objs)]
12
13                 path, base = os.path.split(out_fn)
14                 base, ext = os.path.splitext(base)
15
16                 from .export_object import ObjectExporter
17                 object_export = ObjectExporter()
18
19                 object_prototypes = {}
20                 unique_objects = []
21                 for o in objs:
22                         if o.name in object_prototypes:
23                                 continue
24
25                         clones = [o]
26                         if not any(s.link=="OBJECT" for s in o.material_slots):
27                                 for u in objs:
28                                         if u is o:
29                                                 continue
30                                         if u.data.name!=o.data.name:
31                                                 continue
32                                         if u.technique!=o.technique:
33                                                 continue
34                                         if any(s.link=="OBJECT" for s in u.material_slots):
35                                                 continue
36
37                                         clones.append(u)
38
39                         unique_objects.append(o)
40                         for c in clones:
41                                 object_prototypes[c.name] = o
42
43                 from .util import Progress
44                 progress = Progress(self.show_progress and context)
45
46                 resources = {}
47                 self.export_scene_resources(context, unique_objects, resources, progress)
48
49                 scene_res = self.export_scene(context, objs, progress, prototypes=object_prototypes, resources=resources)
50                 refs = scene_res.collect_references()
51
52                 from .datafile import Statement
53                 if self.resource_collection:
54                         keywords = { ".mat": "material",
55                                 ".mesh": "mesh",
56                                 ".object": "object",
57                                 ".tech": "technique",
58                                 ".tex2d": "texture2d" }
59                         with open(os.path.join(path, base+"_resources.mdc"), "w") as res_out:
60                                 for r in refs:
61                                         st = Statement(keywords[os.path.splitext(r.name)[1]], r.name)
62                                         st.sub = r.statements
63                                         st.write_to_file(res_out)
64                 else:
65                         res_dir = os.path.join(path, base+"_resources")
66                         if not os.path.exists(res_dir):
67                                 os.makedirs(res_dir)
68                         for r in refs:
69                                 with open(os.path.join(res_dir, r.name), "w") as res_out:
70                                         for s in r.statements:
71                                                 s.write_to_file(res_out)
72
73                 with open(out_fn, "w") as out_file:
74                         for s in scene_res.statements:
75                                 s.write_to_file(out_file)
76
77         def export_scene_resources(self, context, objs, resources, progress):
78                 from .export_object import ObjectExporter
79                 object_export = ObjectExporter()
80                 object_export.single_file = False
81
82                 material_maps = {}
83
84                 for i, o in enumerate(objs):
85                         progress.push_task_slice(o.name, i, len(objs))
86                         object_export.export_object_resources(context, o, resources, progress, material_maps=material_maps)
87                         obj_name = o.name+".object"
88                         resources[obj_name] = object_export.export_object(context, o, progress, resources=resources)
89                         progress.pop_task()
90
91         def export_scene(self, context, objs, progress, *, prototypes, resources):
92                 from .datafile import Resource, Statement
93                 scene_res = Resource("scene.scene")
94
95                 for o in objs:
96                         obj_res = resources[prototypes[o.name].name+".object"]
97                         st = scene_res.create_reference_statement("object", obj_res)
98                         # XXX Parent relationships screw up the location and rotation
99                         st.sub.append(Statement("position", o.location[0], o.location[1], o.location[2]))
100                         if o.rotation_mode=="AXIS_ANGLE":
101                                 angle = o.rotation_axis_angle[0]
102                                 axis = o.rotation_axis_angle[1:]
103                         else:
104                                 if o.rotation_mode=="QUATERNION":
105                                         q = o.rotation_quaternion
106                                 else:
107                                         q = o.rotation_euler.to_quaternion()
108                                 angle = q.angle
109                                 axis = q.axis
110                         st.sub.append(Statement("rotation", angle*180/math.pi, axis[0], axis[1], axis[2]))
111                         st.sub.append(Statement("scale", o.scale[0], o.scale[1], o.scale[2]))
112                         scene_res.statements.append(st)
113
114                 progress.set_progress(1.0)
115
116                 return scene_res