]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_scene.py
Add getters for RenderTarget width and height
[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                 export_names = {}
22                 for o in objs:
23                         if o.name in object_prototypes:
24                                 continue
25
26                         clones = [o]
27                         if not any(s.link=="OBJECT" for s in o.material_slots):
28                                 for u in objs:
29                                         if u is o:
30                                                 continue
31                                         if u.data.name!=o.data.name:
32                                                 continue
33                                         if u.technique!=o.technique:
34                                                 continue
35                                         if any(s.link=="OBJECT" for s in u.material_slots):
36                                                 continue
37
38                                         clones.append(u)
39
40                         prefix = o.name
41                         for c in clones:
42                                 for i in range(min(len(c.name), len(prefix))):
43                                         if c.name[i]!=prefix[i]:
44                                                 prefix = prefix[:i]
45                                                 break
46
47                         if prefix:
48                                 export_names[o.name+".object"] = prefix.strip(" .")+".object"
49
50                         unique_objects.append(o)
51                         for c in clones:
52                                 object_prototypes[c.name] = o
53
54                 from .util import Progress
55                 progress = Progress(self.show_progress and context)
56
57                 resources = {}
58                 self.export_scene_resources(context, unique_objects, resources, progress)
59                 for n, r in resources.items():
60                         if r.name in export_names:
61                                 r.name = export_names[r.name]
62
63                 scene_res = self.export_scene(context, objs, progress, prototypes=object_prototypes, resources=resources)
64                 refs = scene_res.collect_references()
65
66                 from .datafile import Statement
67                 if self.resource_collection:
68                         keywords = { ".mat": "material",
69                                 ".mesh": "mesh",
70                                 ".object": "object",
71                                 ".tech": "technique",
72                                 ".tex2d": "texture2d" }
73                         with open(os.path.join(path, base+"_resources.mdc"), "w") as res_out:
74                                 for r in refs:
75                                         st = Statement(keywords[os.path.splitext(r.name)[1]], r.name)
76                                         st.sub = r.statements
77                                         st.write_to_file(res_out)
78                 else:
79                         res_dir = os.path.join(path, base+"_resources")
80                         if not os.path.exists(res_dir):
81                                 os.makedirs(res_dir)
82                         for r in refs:
83                                 with open(os.path.join(res_dir, r.name), "w") as res_out:
84                                         for s in r.statements:
85                                                 s.write_to_file(res_out)
86
87                 with open(out_fn, "w") as out_file:
88                         for s in scene_res.statements:
89                                 s.write_to_file(out_file)
90
91         def export_scene_resources(self, context, objs, resources, progress):
92                 from .export_object import ObjectExporter
93                 object_export = ObjectExporter()
94                 object_export.single_file = False
95
96                 material_maps = {}
97
98                 for i, o in enumerate(objs):
99                         progress.push_task_slice(o.name, i, len(objs))
100                         object_export.export_object_resources(context, o, resources, progress, material_maps=material_maps)
101                         obj_name = o.name+".object"
102                         resources[obj_name] = object_export.export_object(context, o, progress, resources=resources)
103                         progress.pop_task()
104
105         def export_scene(self, context, objs, progress, *, prototypes, resources):
106                 from .datafile import Resource, Statement
107                 scene_res = Resource("scene.scene")
108
109                 for o in objs:
110                         obj_res = resources[prototypes[o.name].name+".object"]
111                         st = scene_res.create_reference_statement("object", obj_res, o.name)
112                         # XXX Parent relationships screw up the location and rotation
113                         st.sub.append(Statement("position", o.location[0], o.location[1], o.location[2]))
114                         if o.rotation_mode=="AXIS_ANGLE":
115                                 angle = o.rotation_axis_angle[0]
116                                 axis = o.rotation_axis_angle[1:]
117                         else:
118                                 if o.rotation_mode=="QUATERNION":
119                                         q = o.rotation_quaternion
120                                 else:
121                                         q = o.rotation_euler.to_quaternion()
122                                 angle = q.angle
123                                 axis = q.axis
124                         st.sub.append(Statement("rotation", angle*180/math.pi, axis[0], axis[1], axis[2]))
125                         st.sub.append(Statement("scale", o.scale[0], o.scale[1], o.scale[2]))
126                         scene_res.statements.append(st)
127
128                 progress.set_progress(1.0)
129
130                 return scene_res