6 self.resource_collection = True
7 self.show_progress = True
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)]
13 path, base = os.path.split(out_fn)
14 base, ext = os.path.splitext(base)
16 from .export_object import ObjectExporter
17 object_export = ObjectExporter()
19 object_prototypes = {}
23 if o.name in object_prototypes:
27 if not any(s.link=="OBJECT" for s in o.material_slots):
31 if u.data.name!=o.data.name:
33 if u.technique!=o.technique:
35 if any(s.link=="OBJECT" for s in u.material_slots):
42 while not c.name.startswith(prefix):
43 pos = max(prefix.rfind(' '), prefix.rfind('.'))
49 export_names[o.name+".object"] = prefix.strip(" .")+".object"
51 unique_objects.append(o)
53 object_prototypes[c.name] = o
55 from .util import Progress
56 progress = Progress(self.show_progress and context)
59 self.export_scene_resources(context, unique_objects, resources, progress)
60 for n, r in resources.items():
61 if r.name in export_names:
62 r.name = export_names[r.name]
64 scene_res = self.export_scene(context, objs, progress, prototypes=object_prototypes, resources=resources)
65 refs = scene_res.collect_references()
67 from .datafile import Statement
68 if self.resource_collection:
69 keywords = { ".mat": "material",
73 ".tex2d": "texture2d" }
74 with open(os.path.join(path, base+"_resources.mdc"), "w") as res_out:
76 st = Statement(keywords[os.path.splitext(r.name)[1]], r.name)
78 st.write_to_file(res_out)
80 res_dir = os.path.join(path, base+"_resources")
81 if not os.path.exists(res_dir):
84 with open(os.path.join(res_dir, r.name), "w") as res_out:
85 for s in r.statements:
86 s.write_to_file(res_out)
88 with open(out_fn, "w") as out_file:
89 for s in scene_res.statements:
90 s.write_to_file(out_file)
92 def export_scene_resources(self, context, objs, resources, progress):
93 from .export_object import ObjectExporter
94 object_export = ObjectExporter()
95 object_export.single_file = False
99 for i, o in enumerate(objs):
100 progress.push_task_slice(o.name, i, len(objs))
101 object_export.export_object_resources(context, o, resources, progress, material_maps=material_maps)
102 obj_name = o.name+".object"
103 resources[obj_name] = object_export.export_object(context, o, progress, resources=resources)
106 def export_scene(self, context, objs, progress, *, prototypes, resources):
107 from .datafile import Resource, Statement
108 scene_res = Resource("scene.scene")
111 obj_res = resources[prototypes[o.name].name+".object"]
112 st = scene_res.create_reference_statement("object", obj_res, o.name)
113 # XXX Parent relationships screw up the location and rotation
114 ss = Statement("transform")
115 ss.sub.append(Statement("position", o.location[0], o.location[1], o.location[2]))
116 if o.rotation_mode=="AXIS_ANGLE":
117 angle = o.rotation_axis_angle[0]
118 axis = o.rotation_axis_angle[1:]
120 if o.rotation_mode=="QUATERNION":
121 q = o.rotation_quaternion
123 q = o.rotation_euler.to_quaternion()
126 ss.sub.append(Statement("rotation", angle*180/math.pi, axis[0], axis[1], axis[2]))
127 ss.sub.append(Statement("scale", o.scale[0], o.scale[1], o.scale[2]))
129 scene_res.statements.append(st)
131 progress.set_progress(1.0)