"""Export wood and porcelain finishes on the existing iron sphere mesh.""" import bpy import json import runpy from pathlib import Path ROOT = Path(__file__).resolve().parents[4] OUT = ROOT / 'assets/collections/storybook' KINDS = ['wood_ball', 'ceramic_ball'] ONLY_BALL = globals().get('ONLY_BALL') if ONLY_BALL is not None and ONLY_BALL not in KINDS: raise ValueError(ONLY_BALL) apply_material = runpy.run_path(str(Path(__file__).with_name('material_style.py')))['apply_material'] catalog = json.loads((OUT/'catalog.json').read_text()) for kind in ([ONLY_BALL] if ONLY_BALL else KINDS): directory = ROOT/'assets/balls'/kind source = directory/'source/model.blend' input_model = source if source.exists() else ROOT/'assets/balls/iron_ball/source/model.blend' bpy.ops.wm.open_mainfile(filepath=str(input_model)) bpy.context.preferences.filepaths.save_version = 0 obj = bpy.data.objects[kind if input_model == source else 'iron_ball'] obj.name = kind obj.data = obj.data.copy() obj['asset_id'] = kind obj['collision_source'] = 'same sphere geometry as iron_ball' assert obj.data.uv_layers, 'Iron source must retain its authored UVs' apply_material(obj, kind) obj.location = (0,0,0) bpy.ops.object.select_all(action='DESELECT');obj.select_set(True) bpy.context.view_layer.objects.active = obj bpy.ops.export_scene.gltf(filepath=str(directory/'model.glb'),export_format='GLB',use_selection=True, export_yup=True,export_extras=True,export_apply=True) source.parent.mkdir(parents=True,exist_ok=True) bpy.ops.wm.save_as_mainfile(filepath=str(source)) metadata = dict(catalog['models']['iron_ball']) metadata.update(path='res://'+str((directory/'model.glb').relative_to(ROOT)), sourceBlend=str(source.relative_to(ROOT)),role='ball',geometrySource='iron_ball', albedoTexture='res://'+str((directory/'textures/albedo.png').relative_to(ROOT))) catalog['models'][kind] = metadata (OUT/'catalog.json').write_text(json.dumps(catalog,indent=2,ensure_ascii=False)+'\n') if not ONLY_BALL: # Full-family build also keeps the editable collection complete. collection = OUT/'source/collection.blend' bpy.ops.wm.open_mainfile(filepath=str(collection)) bpy.context.preferences.filepaths.save_version = 0 for i,kind in enumerate(KINDS): if kind in bpy.data.objects:bpy.data.objects.remove(bpy.data.objects[kind],do_unlink=True) with bpy.data.libraries.load(str(ROOT/catalog['models'][kind]['sourceBlend']),link=False) as (src,dst): dst.objects = [kind] obj = dst.objects[0] bpy.context.collection.objects.link(obj) obj.location = (5.2+i*2.6,6,0) bpy.ops.wm.save_as_mainfile(filepath=str(collection)) print('MATERIAL_BALLS_READY',ONLY_BALL or KINDS)