feat(playcanvas): share dockable scene tools and persistent model editing
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
"""Evaluated v020 geometry, semantic PBR groups and UVs; never saves the source."""
|
||||
import bpy, json, hashlib
|
||||
"""Evaluated geometry and editable PBR groups; never saves the source.
|
||||
|
||||
Blender -b <source.blend> --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
|
||||
@@ -7,6 +11,16 @@ 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:
|
||||
@@ -16,7 +30,14 @@ for inst in dg.object_instances:
|
||||
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)):
|
||||
name=mesh.materials[idx].name if mesh.materials[idx] else '';key=classify(name,src.name)
|
||||
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()))
|
||||
@@ -30,7 +51,7 @@ 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()
|
||||
# Separate by material keeps picking exact while avoiding 1,426 draw calls.
|
||||
# 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
|
||||
@@ -38,8 +59,15 @@ for o in list(scene.objects):
|
||||
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/'apps/control-station/public/rover-scene/dcd006-v020.glb'
|
||||
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':'DCD-006 v020 complete rover','revision':'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()}}
|
||||
(out.parent/'dcd006-v020.json').write_text(json.dumps(manifest,ensure_ascii=False,indent=2)+'\n')
|
||||
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')
|
||||
|
||||
Reference in New Issue
Block a user