"""Evaluated geometry and editable PBR groups; never saves the source. Blender -b --python tools/rover-scene/export_rover.py -- \ --revision v022 --equipment-collection "04 ОБОРУДОВАНИЕ • актуальная сборка" """ import bpy, json, hashlib, argparse from collections import defaultdict from pathlib import Path from mathutils import Vector, Matrix root=Path.cwd();source=Path(bpy.data.filepath) import sys sys.path.insert(0,str(Path(__file__).resolve().parent)) from materials import specs, materials, classify from equipment_materials import equipment_material, srgb_hex parser=argparse.ArgumentParser() parser.add_argument('--revision',default='v020') parser.add_argument('--equipment-collection') args=parser.parse_args(sys.argv[sys.argv.index('--')+1:] if '--' in sys.argv else []) if args.revision not in {'v020','v022'}:parser.error('Unsupported rover revision') equipment=set(bpy.data.collections[args.equipment_collection].all_objects) if args.equipment_collection else set() bpy.context.view_layer.update() equipment_cache={};color_sources={};frames={o.name:[list(row) for row in o.matrix_world] for o in bpy.context.scene.objects if o.name.startswith('FRAME_') and o.type=='EMPTY'} bpy.context.scene.frame_set(1);dg=bpy.context.evaluated_depsgraph_get();instances=[];assignment=defaultdict(set) for inst in dg.object_instances: o=inst.object;src=o.original;cols=[c.name for c in src.users_collection] if o.type not in {'MESH','CURVE'} or src.hide_render:continue if any(c.hide_render for c in src.users_collection) or any(any(t in c for t in ['STUDIO','BOOLEAN','DATUM']) for c in cols):continue if not inst.is_instance and not src.visible_get():continue mesh=bpy.data.meshes.new_from_object(o,preserve_all_data_layers=True,depsgraph=dg) for idx in range(len(mesh.materials)): source_material=mesh.materials[idx] name=source_material.name if source_material else '' if src in equipment and source_material: if name not in equipment_cache: key,spec,mat,color_source=equipment_material(source_material) equipment_cache[name]=key;specs[key]=spec;materials[key]=mat;color_sources[key]=color_source key=equipment_cache[name] else:key=classify(name,src.name) mesh.materials[idx]=materials[key];assignment[key].add(src.name) if not mesh.materials:mesh.materials.append(materials['body']);assignment['body'].add(src.name) instances.append((src.name,mesh,inst.matrix_world.copy())) scene=bpy.data.scenes.new('Mission Core rover export');bpy.context.window.scene=scene;objects=[] for name,mesh,matrix in instances: o=bpy.data.objects.new(name,mesh);scene.collection.objects.link(o);o.matrix_world=matrix;objects.append(o) bpy.context.view_layer.update() lo=Vector((min((o.matrix_world@Vector(v))[i] for o in objects for v in o.bound_box) for i in range(3))) hi=Vector((max((o.matrix_world@Vector(v))[i] for o in objects for v in o.bound_box) for i in range(3))) shift=Vector((-(lo.x+hi.x)/2,-(lo.y+hi.y)/2,-lo.z)) for o in objects:o.matrix_world=Matrix.Translation(shift)@o.matrix_world count=len(objects) bpy.ops.object.select_all(action='SELECT');bpy.context.view_layer.objects.active=objects[0];bpy.ops.object.join() # Batch by material to keep exact surface picking with a bounded draw-call count. bpy.ops.object.mode_set(mode='EDIT');bpy.ops.mesh.select_all(action='SELECT');bpy.ops.mesh.separate(type='MATERIAL');bpy.ops.object.mode_set(mode='OBJECT') for o in list(scene.objects): bpy.ops.object.select_all(action='DESELECT');o.select_set(True);bpy.context.view_layer.objects.active=o key=o.data.materials[0].name.removeprefix('rover/');o.name=specs[key][0] for uv in list(o.data.uv_layers):o.data.uv_layers.remove(uv) bpy.ops.object.mode_set(mode='EDIT');bpy.ops.mesh.select_all(action='SELECT');bpy.ops.uv.cube_project(cube_size=.2);bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.select_all(action='SELECT') out=root/f'apps/control-station/public/rover-scene/dcd006-{args.revision}.glb' bpy.ops.export_scene.gltf(filepath=str(out),export_format='GLB',use_selection=True,use_active_scene=True,export_animations=False,export_cameras=False,export_lights=False,export_extras=False,export_yup=True,export_apply=False,export_texcoords=True,export_attributes=False,export_vertex_color='NONE') manifest={'source':source.name,'source_sha256':hashlib.sha256(source.read_bytes()).hexdigest(),'model':f'DCD-006 {args.revision} complete rover','revision':'materials-v2' if args.revision=='v022' else 'materials-v2','source_object_count':count,'render_mesh_count':len(scene.objects),'original_bounds':[list(lo),list(hi)],'dimensions_m':list(hi-lo),'translation_m':list(shift),'glb_bytes':out.stat().st_size,'glb_sha256':hashlib.sha256(out.read_bytes()).hexdigest(),'source_saved':False,'uv':'cube projection, 0.2 m','materials':{k:{'name':v[0],'baseColorLinear':v[1],'metalness':v[2],'roughness':v[3],'sourceObjects':sorted(assignment[k])} for k,v in specs.items()}} # Source anchors remain in engineering coordinates. The explicit transform maps # each original Blender point to the centered Y-up GLB without losing placement. manifest['mount_frames']={'units':'metres','source_axes':'+X forward, +Z up','source_world_matrices':frames,'source_to_gltf_matrix':[[1,0,0,shift.x],[0,0,1,shift.z],[0,-1,0,-shift.y],[0,0,0,1]]} manifest['equipment_color_sources']=color_sources if equipment: equipment_defaults={key:{'name':specs[key][0],'color':srgb_hex(specs[key][1]),'metalness':specs[key][2],'roughness':specs[key][3],'finish':color_sources[key].removeprefix('owner/') if color_sources[key].startswith('owner/') else ''} for key in sorted(color_sources)} (root/'apps/control-station/src/core/roverScene/equipmentMaterials.json').write_text(json.dumps(equipment_defaults,ensure_ascii=False,indent=2)+'\n') (out.with_suffix('.json')).write_text(json.dumps(manifest,ensure_ascii=False,indent=2)+'\n') print('ROVER EXPORT',out.stat().st_size,'bytes;',len(scene.objects),'material groups')