if "bpy" in locals():
import imp
- for sub in "animation", "armature", "datafile", "export", "export_animation", "export_armature", "export_camera", "export_material", "export_mesh", "export_object", "export_scene", "export_texture", "material", "mesh", "properties", "util":
+ for sub in "animation", "armature", "datafile", "export", "export_animation", "export_armature", "export_camera", "export_material", "export_mesh", "export_object", "export_scene", "export_texture", "material", "mesh", "properties", "scene", "util":
if sub in locals():
imp.reload(locals()[sub])
self.show_progress = True
def export_to_file(self, context, out_fn):
- if self.selected_only:
- objs = context.selected_objects
- else:
- objs = context.scene.objects
- if self.visible_only:
- collections = [c.collection for c in context.view_layer.layer_collection.children if not (c.hide_viewport or c.collection.hide_viewport)]
- objs = [o for o in objs if any((o.name in c.all_objects) for c in collections)]
- objs = [o for o in objs if o.type=="MESH" and not o.lod_for_parent and o.data.vertices]
- objs = [o for o in objs if (not o.compound or o.parent not in objs)]
- objs.sort(key=lambda x:x.name)
+ from .scene import create_scene_from_current
+ scene = create_scene_from_current(context, selected_only=self.selected_only, visible_only=self.visible_only)
path, base = os.path.split(out_fn)
base, ext = os.path.splitext(base)
- object_prototypes = {}
- unique_objects = []
export_names = {}
used_names = set()
- for o in objs:
- if o.name in object_prototypes:
- continue
-
- clones = [o]
- for u in objs:
- if u is o:
- continue
- if u.data.name!=o.data.name:
- continue
- if any(m1.name!=m2.name for m1, m2 in zip(o.material_slots, u.material_slots)):
- continue
-
- clones.append(u)
-
- prefix = o.name
+ for p in scene.prototypes:
+ clones = [i for i in scene.instances if i.prototype==p.name]
+
+ prefix = p.name
for c in clones:
while not c.name.startswith(prefix):
pos = max(prefix.rfind(' '), prefix.rfind('.'))
prefix = prefix[:pos]
if prefix:
- export_names[o.name+".object"] = prefix.strip(" .")
+ export_names[p.name+".object"] = prefix.strip(" .")
else:
- used_names.add(o.name)
-
- unique_objects.append(o)
- for c in clones:
- object_prototypes[c.name] = o
+ used_names.add(p.name)
for n, e in export_names.items():
if e in used_names:
data_exporter = DataExporter()
resources = {}
- data_exporter.export_resources(context, unique_objects, resources, None, progress)
+ data_exporter.export_resources(context, scene.prototypes, resources, None, progress)
for n, r in resources.items():
if r.name in export_names:
r.name = export_names[r.name]
- scene_res = self.export_scene(context, objs, resources, object_prototypes, progress)
+ scene_res = self.export_scene(scene, resources, progress)
refs = scene_res.collect_references()
if self.collection:
for r in refs:
r.write_to_file(os.path.join(path, r.name))
- def export_scene(self, context, objs, resources, prototypes, progress):
+ def export_scene(self, scene, resources, progress):
from .datafile import Resource, Statement, Token
- scene_res = Resource(context.scene.name+".scene", "scene")
+ scene_res = Resource(scene.name+".scene", "scene")
- scene_res.statements.append(Statement("type", Token(context.scene.scene_type.lower())))
+ scene_res.statements.append(Statement("type", Token(scene.scene_type.lower())))
- for o in objs:
- obj_res = resources[prototypes[o.name].name+".object"]
- st = scene_res.create_reference_statement("object", obj_res, o.name)
+ for i in scene.instances:
+ obj_res = resources[i.prototype+".object"]
+ st = scene_res.create_reference_statement("object", obj_res, i.name)
ss = Statement("transform")
- loc = o.matrix_world.to_translation()
+ loc = i.matrix_world.to_translation()
ss.sub.append(Statement("position", *tuple(loc)))
- quat = o.matrix_world.to_quaternion()
- if o.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
+ quat = i.matrix_world.to_quaternion()
+ if i.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
angles = [a*180/math.pi for a in quat.to_euler()]
ss.sub.append(Statement("euler", *angles));
else:
ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
- scale = o.matrix_world.to_scale()
+ scale = i.matrix_world.to_scale()
ss.sub.append(Statement("scale", *tuple(scale)))
st.sub.append(ss)
--- /dev/null
+def is_same_object(obj1, obj2):
+ if obj1.data.name!=obj2.data.name:
+ return False
+ if any(m1.name!=m2.name for m1, m2 in zip(obj1.material_slots, obj2.material_slots)):
+ return False
+
+ return True
+
+class Instance:
+ def __init__(self, obj, prototype):
+ self.name = obj.name
+ self.matrix_world = obj.matrix_world
+ self.rotation_mode = obj.rotation_mode
+ self.prototype = prototype.name
+
+class Scene:
+ def __init__(self, scene, obj_filter=None):
+ self.name = scene.name
+ self.scene_type = scene.scene_type
+ self.prototypes = []
+ self.instances = []
+
+ objects = [o for o in scene.objects if o.type=='MESH']
+ objects.sort(key=lambda o:o.name)
+ if obj_filter:
+ objects = list(filter(obj_filter, objects))
+
+ processed = set()
+ for o in objects:
+ if o.name in processed:
+ continue
+
+ clones = [c for c in objects if is_same_object(o, c)]
+ self.prototypes.append(o)
+ for c in clones:
+ self.instances.append(Instance(c, o))
+ processed.add(c.name)
+
+def get_all_collections(collection):
+ result = [collection]
+ for c in collection.children:
+ result += get_all_collections(c)
+ return result
+
+def create_scene_from_current(context, *, selected_only=False, visible_only=True):
+ obj_filters = []
+
+ if selected_only:
+ obj_filters.append(lambda o: o.select_get())
+
+ if visible_only:
+ visible_names = set()
+ for c in get_all_collections(context.view_layer.layer_collection):
+ if not c.hide_viewport and not c.collection.hide_viewport:
+ visible_names.update(o.name for o in c.collection.objects)
+ obj_filters.append(lambda o: o.name in visible_names)
+
+ obj_filter = None
+ if len(obj_filters)==1:
+ obj_filter = obj_filters[0]
+ if obj_filters:
+ obj_filter = lambda o: all(f(o) for f in obj_filters)
+
+ return Scene(context.scene, obj_filter)