6 self.selected_only = False
7 self.active_layers = True
8 self.resource_collection = True
9 self.skip_existing = True
10 self.show_progress = True
12 def export_to_file(self, context, out_fn):
13 if self.selected_only:
14 objs = context.selected_objects
16 objs = context.scene.objects
17 if self.active_layers:
18 layers = context.scene.layers
19 objs = [o for o in objs if any(a and b for a, b in zip(layers, o.layers))]
20 objs = [o for o in objs if o.type=="MESH" and not o.lod_for_parent]
21 objs = [o for o in objs if (not o.compound or o.parent not in objs)]
23 path, base = os.path.split(out_fn)
24 base, ext = os.path.splitext(base)
26 from .export_object import ObjectExporter
27 object_export = ObjectExporter()
29 object_prototypes = {}
33 if o.name in object_prototypes:
37 if not any(s.link=="OBJECT" for s in o.material_slots):
41 if u.data.name!=o.data.name:
43 if any(s.link=="OBJECT" for s in u.material_slots):
50 while not c.name.startswith(prefix):
51 pos = max(prefix.rfind(' '), prefix.rfind('.'))
57 export_names[o.name+".object"] = prefix.strip(" .")+".object"
59 unique_objects.append(o)
61 object_prototypes[c.name] = o
63 from .util import Progress
64 progress = Progress(self.show_progress and context)
67 self.export_scene_resources(context, unique_objects, resources, progress)
68 for n, r in resources.items():
69 if r.name in export_names:
70 r.name = export_names[r.name]
72 scene_res = self.export_scene(context, objs, progress, prototypes=object_prototypes, resources=resources)
73 refs = scene_res.collect_references()
75 if self.resource_collection:
76 from .datafile import Statement
77 keywords = { ".mat": "material",
81 ".tex2d": "texture2d" }
82 with open(os.path.join(path, base+"_resources.mdc"), "w") as res_out:
84 if self.skip_existing and os.path.exists(os.path.join(path, r.name)):
87 st = Statement(keywords[os.path.splitext(r.name)[1]], r.name)
89 st.write_to_file(res_out)
91 res_dir = os.path.join(path, base+"_resources")
92 if not os.path.exists(res_dir):
95 with open(os.path.join(res_dir, r.name), "w") as res_out:
96 for s in r.statements:
97 s.write_to_file(res_out)
99 with open(out_fn, "w") as out_file:
100 for s in scene_res.statements:
101 s.write_to_file(out_file)
103 def export_scene_resources(self, context, objs, resources, progress):
104 from .export_object import ObjectExporter
105 object_export = ObjectExporter()
106 object_export.single_file = False
110 for i, o in enumerate(objs):
111 progress.push_task_slice(o.name, i, len(objs))
112 object_export.export_object_resources(context, o, resources, progress, material_maps=material_maps)
113 obj_name = o.name+".object"
114 resources[obj_name] = object_export.export_object(context, o, progress, resources=resources)
117 def export_scene(self, context, objs, progress, *, prototypes, resources):
118 from .datafile import Resource, Statement
119 scene_res = Resource("scene.scene")
122 obj_res = resources[prototypes[o.name].name+".object"]
123 st = scene_res.create_reference_statement("object", obj_res, o.name)
125 ss = Statement("transform")
127 loc = o.matrix_world.to_translation()
128 ss.sub.append(Statement("position", *tuple(loc)))
130 quat = o.matrix_world.to_quaternion()
131 if o.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
132 angles = [a*180/math.pi for a in quat.to_euler()]
133 ss.sub.append(Statement("euler", *angles));
135 ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
137 scale = o.matrix_world.to_scale()
138 ss.sub.append(Statement("scale", *tuple(scale)))
141 scene_res.statements.append(st)
143 progress.set_progress(1.0)