6 self.selected_only = False
7 self.active_layers = True
8 self.resource_collection = True
9 self.show_progress = True
11 def export_to_file(self, context, out_fn):
12 if self.selected_only:
13 objs = context.selected_objects
15 objs = context.scene.objects
16 if self.active_layers:
17 layers = context.scene.layers
18 objs = [o for o in objs if any(a and b for a, b in zip(layers, o.layers))]
19 objs = [o for o in objs if o.type=="MESH" and not o.lod_for_parent]
20 objs = [o for o in objs if (not o.compound or o.parent not in objs)]
22 path, base = os.path.split(out_fn)
23 base, ext = os.path.splitext(base)
25 from .export_object import ObjectExporter
26 object_export = ObjectExporter()
28 object_prototypes = {}
32 if o.name in object_prototypes:
36 if not any(s.link=="OBJECT" for s in o.material_slots):
40 if u.data.name!=o.data.name:
42 if any(s.link=="OBJECT" for s in u.material_slots):
49 while not c.name.startswith(prefix):
50 pos = max(prefix.rfind(' '), prefix.rfind('.'))
56 export_names[o.name+".object"] = prefix.strip(" .")+".object"
58 unique_objects.append(o)
60 object_prototypes[c.name] = o
62 from .util import Progress
63 progress = Progress(self.show_progress and context)
66 self.export_scene_resources(context, unique_objects, resources, progress)
67 for n, r in resources.items():
68 if r.name in export_names:
69 r.name = export_names[r.name]
71 scene_res = self.export_scene(context, objs, progress, prototypes=object_prototypes, resources=resources)
72 refs = scene_res.collect_references()
74 if self.resource_collection:
75 from .datafile import Statement
76 keywords = { ".mat": "material",
80 ".tex2d": "texture2d" }
81 with open(os.path.join(path, base+"_resources.mdc"), "w") as res_out:
83 st = Statement(keywords[os.path.splitext(r.name)[1]], r.name)
85 st.write_to_file(res_out)
87 res_dir = os.path.join(path, base+"_resources")
88 if not os.path.exists(res_dir):
91 with open(os.path.join(res_dir, r.name), "w") as res_out:
92 for s in r.statements:
93 s.write_to_file(res_out)
95 with open(out_fn, "w") as out_file:
96 for s in scene_res.statements:
97 s.write_to_file(out_file)
99 def export_scene_resources(self, context, objs, resources, progress):
100 from .export_object import ObjectExporter
101 object_export = ObjectExporter()
102 object_export.single_file = False
106 for i, o in enumerate(objs):
107 progress.push_task_slice(o.name, i, len(objs))
108 object_export.export_object_resources(context, o, resources, progress, material_maps=material_maps)
109 obj_name = o.name+".object"
110 resources[obj_name] = object_export.export_object(context, o, progress, resources=resources)
113 def export_scene(self, context, objs, progress, *, prototypes, resources):
114 from .datafile import Resource, Statement
115 scene_res = Resource("scene.scene")
118 obj_res = resources[prototypes[o.name].name+".object"]
119 st = scene_res.create_reference_statement("object", obj_res, o.name)
121 ss = Statement("transform")
123 loc = o.matrix_world.to_translation()
124 ss.sub.append(Statement("position", *tuple(loc)))
126 quat = o.matrix_world.to_quaternion()
127 if o.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
128 angles = [a*180/math.pi for a in quat.to_euler()]
129 ss.sub.append(Statement("euler", *angles));
131 ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
133 scale = o.matrix_world.to_scale()
134 ss.sub.append(Statement("scale", *tuple(scale)))
137 scene_res.statements.append(st)
139 progress.set_progress(1.0)