"""Export the current authored Portfolio Evolution scene. Run inside Blender.""" import bpy from pathlib import Path root_path = Path(__file__).resolve().parents[3] scene = bpy.data.scenes["Portfolio Evolution"] # A separate export scene combines geometry by material: eight runtime draw groups. export_scene = bpy.data.scenes.new('Evolution Export') bpy.context.window.scene = export_scene root = bpy.data.objects.new('evolution-chase-car', None) export_scene.collection.objects.link(root) names = {'M_Body_Gunmetal': 'car-body', 'M_Body_Haunch': 'body-haunches', 'M_Glass_Smoke': 'rear-glass', 'M_Rear_Light': 'rear-light-bar', 'M_Cyan_Accent': 'car-underglow-source', 'M_Lower_Graphite': 'body-trim', 'M_Wheel': 'wheel-machining', 'M_Tire': 'tires'} bpy.context.window.scene = scene depsgraph = bpy.context.evaluated_depsgraph_get() groups = {} for obj in scene.objects: if obj.type != 'MESH' or obj.name.startswith('LOOKDEV'): continue evaluated = obj.evaluated_get(depsgraph) mesh = bpy.data.meshes.new_from_object(evaluated, depsgraph=depsgraph) mesh.transform(obj.matrix_world) clone = bpy.data.objects.new('export-' + obj.name, mesh) export_scene.collection.objects.link(clone) key = obj.data.materials[0].name.split('.')[0] groups.setdefault(key, []).append(clone) bpy.context.window.scene = export_scene for key, objects in groups.items(): bpy.ops.object.select_all(action='DESELECT') for obj in objects: obj.select_set(True) bpy.context.view_layer.objects.active = objects[0] if len(objects) > 1: bpy.ops.object.join() joined = bpy.context.object joined.name = 'evolution-' + names[key] joined.parent = root bpy.ops.object.select_all(action='SELECT') bpy.ops.export_scene.gltf(filepath=str(root_path / 'src/assets/nightdrive-grand-tourer-evolution.glb'), export_format='GLB', use_selection=True, use_active_scene=True, export_apply=True, export_animations=False, export_cameras=False, export_lights=False) print('Exported', len(groups), 'material groups') bpy.context.window.scene = scene