6 self.selected_only = False
7 self.visible_collections = 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.visible_collections:
18 collections = [c.collection for c in context.view_layer.layer_collection.children if not (c.hide_viewport or c.collection.hide_viewport)]
19 objs = [o for o in objs if any((o.name in c.all_objects) for c in collections)]
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)]
22 objs.sort(key=lambda x:x.name)
24 path, base = os.path.split(out_fn)
25 base, ext = os.path.splitext(base)
27 from .export_object import ObjectExporter
28 object_export = ObjectExporter()
30 object_prototypes = {}
34 if o.name in object_prototypes:
38 if not any(s.link=="OBJECT" for s in o.material_slots):
42 if u.data.name!=o.data.name:
44 if any(s.link=="OBJECT" for s in u.material_slots):
51 while not c.name.startswith(prefix):
52 pos = max(prefix.rfind(' '), prefix.rfind('.'))
58 export_names[o.name+".object"] = prefix.strip(" .")+".object"
60 unique_objects.append(o)
62 object_prototypes[c.name] = o
64 from .util import Progress
65 progress = Progress(self.show_progress and context)
68 self.export_scene_resources(context, unique_objects, resources, progress)
69 for n, r in resources.items():
70 if r.name in export_names:
71 r.name = export_names[r.name]
73 scene_res = self.export_scene(context, objs, progress, prototypes=object_prototypes, resources=resources)
74 refs = scene_res.collect_references()
76 if self.resource_collection:
78 if self.skip_existing:
79 filter = lambda r: not os.path.exists(os.path.join(path, r.name))
80 scene_res.write_collection(os.path.join(path, base+"_resources.mdc"), exclude=self=True, filter)
82 res_dir = os.path.join(path, base+"_resources")
83 if not os.path.exists(res_dir):
86 r.write_to_file(os.path.join(res_dir, r.name))
88 scene_res.write_to_file(out_fn)
90 def export_scene_resources(self, context, objs, resources, progress):
91 from .export_object import ObjectExporter
92 object_export = ObjectExporter()
93 object_export.single_file = False
97 for i, o in enumerate(objs):
98 progress.push_task_slice(o.name, i, len(objs))
99 object_export.export_object_resources(context, o, resources, progress, material_maps=material_maps)
100 obj_name = o.name+".object"
101 resources[obj_name] = object_export.export_object(context, o, progress, resources=resources)
104 def export_scene(self, context, objs, progress, *, prototypes, resources):
105 from .datafile import Resource, Statement
106 scene_res = Resource("scene.scene", "scene")
109 obj_res = resources[prototypes[o.name].name+".object"]
110 st = scene_res.create_reference_statement("object", obj_res, o.name)
112 ss = Statement("transform")
114 loc = o.matrix_world.to_translation()
115 ss.sub.append(Statement("position", *tuple(loc)))
117 quat = o.matrix_world.to_quaternion()
118 if o.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
119 angles = [a*180/math.pi for a in quat.to_euler()]
120 ss.sub.append(Statement("euler", *angles));
122 ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
124 scale = o.matrix_world.to_scale()
125 ss.sub.append(Statement("scale", *tuple(scale)))
128 scene_res.statements.append(st)
130 progress.set_progress(1.0)