159 lines
7.4 KiB
Python
159 lines
7.4 KiB
Python
import bpy
|
|
import hashlib
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
from mathutils import Vector
|
|
|
|
OUT = Path(__file__).resolve().parent
|
|
sys.path.insert(0, str(OUT))
|
|
from export_collector_parts import studio, evaluated_bin
|
|
|
|
MAIN = OUT/'PerfectCoffee_Minex_Mechanism.blend'
|
|
SPECS = {
|
|
'fixed_chassis': {
|
|
'root': 'Fixed_Chassis_Assembly',
|
|
'file': 'PerfectCoffee_Fixed_Chassis.blend',
|
|
'preview': 'preview_fixed_chassis_part.png',
|
|
'camera': ((0.76, 1.30, 0.85), (0, 0, 0.36), 0.980),
|
|
},
|
|
'collection_deck': {
|
|
'root': 'Collection_Deck_Assembly',
|
|
'file': 'PerfectCoffee_Collection_Deck.blend',
|
|
'preview': 'preview_collection_deck_part.png',
|
|
'camera': ((0.46, 0.72, 0.48), (0, 0, 0.12), 0.510),
|
|
},
|
|
'lower_service_covers': {
|
|
'root': 'Lower_Service_Cover_Assembly',
|
|
'file': 'PerfectCoffee_Lower_Service_Covers.blend',
|
|
'preview': 'preview_lower_service_covers_part.png',
|
|
'camera': ((0.50, 0.72, 0.40), (0, 0, 0.17), 0.540),
|
|
},
|
|
'transparent_hood': {
|
|
'root': 'Removable_Transparent_Hood_Assembly',
|
|
'file': 'PerfectCoffee_Transparent_Hood.blend',
|
|
'preview': 'preview_transparent_hood.png',
|
|
'camera': ((0.55, 0.85, 0.60), (0, 0, 0.23), 0.520),
|
|
},
|
|
}
|
|
|
|
|
|
def export_part(kind, spec):
|
|
bpy.ops.wm.open_mainfile(filepath=str(MAIN))
|
|
source_root = bpy.data.objects[spec['root']]
|
|
names = [source_root.name, *[obj.name for obj in source_root.children_recursive]]
|
|
bpy.ops.wm.read_factory_settings(use_empty=True)
|
|
bpy.context.preferences.filepaths.save_version = 0
|
|
with bpy.data.libraries.load(str(MAIN), link=False) as (source, target):
|
|
target.objects = names
|
|
appended = [obj for obj in target.objects if obj is not None]
|
|
assert len(appended) == len(names), f'{kind}: missing source objects'
|
|
for obj in appended:
|
|
bpy.context.scene.collection.objects.link(obj)
|
|
obj.animation_data_clear()
|
|
obj.hide_render = kind == 'fixed_chassis' and obj.name in {'LCD_Status_Processing', 'LCD_Status_Stopped'}
|
|
obj.hide_viewport = obj.hide_render
|
|
assembly = bpy.data.objects[spec['root']]
|
|
assembly.parent = None
|
|
assembly.matrix_parent_inverse.identity()
|
|
assembly.location = (0, 0, 0)
|
|
assembly.rotation_euler = (0, 0, 0)
|
|
bpy.context.view_layer.update()
|
|
points = []
|
|
for obj in assembly.children_recursive:
|
|
if obj.type != 'MESH':
|
|
continue
|
|
evaluated = obj.evaluated_get(bpy.context.evaluated_depsgraph_get())
|
|
data = evaluated.to_mesh()
|
|
points.extend(evaluated.matrix_world @ vertex.co for vertex in data.vertices)
|
|
evaluated.to_mesh_clear()
|
|
assert points, f'{kind}: assembly has no mesh geometry'
|
|
low = Vector(tuple(min(point[i] for point in points) for i in range(3)))
|
|
high = Vector(tuple(max(point[i] for point in points) for i in range(3)))
|
|
offset = Vector((-(low.x+high.x)/2, -(low.y+high.y)/2, 0.039-low.z))
|
|
assembly.location += offset
|
|
studio(kind, spec)
|
|
scene = bpy.context.scene
|
|
scene.frame_set(1)
|
|
bpy.context.view_layer.update()
|
|
scene['acceptance_view'] = kind
|
|
scene['source_model'] = MAIN.name
|
|
scene['source_sha256'] = hashlib.sha256(MAIN.read_bytes()).hexdigest()
|
|
scene['geometry_note'] = 'The complete assembly root and descendants are appended from the main juicer; no replacement geometry is drawn'
|
|
about = bpy.data.texts.new('ABOUT_Assembly_Acceptance')
|
|
about.write(f'Independent {kind} assembly from {MAIN.name}. Source segment: ZMUEX_working_video.mp4 03:53 onward. Animated visibility and transforms are cleared for static acceptance. Studio placement offset: {list(offset)}.\n')
|
|
bpy.ops.object.select_all(action='DESELECT')
|
|
assembly.select_set(True)
|
|
bpy.context.view_layer.objects.active = assembly
|
|
path = OUT/spec['file']
|
|
bpy.ops.wm.save_as_mainfile(filepath=str(path), compress=True)
|
|
bpy.ops.wm.open_mainfile(filepath=str(path))
|
|
scene = bpy.context.scene
|
|
scene.frame_set(1)
|
|
bpy.context.view_layer.update()
|
|
assembly = bpy.data.objects[spec['root']]
|
|
non_manifold = {}
|
|
volumes = {}
|
|
for obj in assembly.children_recursive:
|
|
if obj.type != 'MESH':
|
|
continue
|
|
edges, volume, _ = evaluated_bin(obj)
|
|
non_manifold[obj.name] = edges
|
|
volumes[obj.name] = volume
|
|
other_roots = [item['root'] for item in SPECS.values() if item['root'] != spec['root']]
|
|
visible_lcd_states = [status for status in ['Ready', 'Processing', 'Stopped'] if 'LCD_Status_'+status in bpy.data.objects and not bpy.data.objects['LCD_Status_'+status].hide_render]
|
|
report = {
|
|
'file': spec['file'],
|
|
'preview': spec['preview'],
|
|
'source_blend': MAIN.name,
|
|
'source_blend_sha256': scene['source_sha256'],
|
|
'assembly_root': spec['root'],
|
|
'source_object_count': len(names),
|
|
'appended_object_count': len(appended),
|
|
'root_parent_none': assembly.parent is None,
|
|
'other_assemblies_excluded': all(name not in bpy.data.objects for name in other_roots),
|
|
'control_pod_excluded_from_deck': kind != 'collection_deck' or 'Fixed_Control_Pod_Assembly' not in bpy.data.objects,
|
|
'visible_lcd_states': visible_lcd_states,
|
|
'single_ready_state_for_fixed_chassis': kind != 'fixed_chassis' or visible_lcd_states == ['Ready'],
|
|
'studio_placement_offset_m': list(offset),
|
|
'non_manifold_edges': non_manifold,
|
|
'positive_part_volumes_m3': volumes,
|
|
'linked_library_count': len(bpy.data.libraries),
|
|
'missing_external_assets': [image.filepath for image in bpy.data.images if image.source == 'FILE' and not image.packed_file and not Path(bpy.path.abspath(image.filepath)).exists()],
|
|
'visual_review_passed': False,
|
|
}
|
|
report['geometry_checks_passed'] = bool(volumes) and all(value == 0 for value in non_manifold.values()) and all(value > 0 for value in volumes.values())
|
|
report['technical_checks_passed'] = all([
|
|
report['source_object_count'] == report['appended_object_count'],
|
|
report['root_parent_none'], report['other_assemblies_excluded'],
|
|
report['control_pod_excluded_from_deck'], report['geometry_checks_passed'],
|
|
report['single_ready_state_for_fixed_chassis'],
|
|
report['linked_library_count'] == 0, not report['missing_external_assets'],
|
|
])
|
|
report['passed'] = report['technical_checks_passed']
|
|
assert report['passed'], f'{kind} assembly export failed: {report}'
|
|
scene.render.filepath = str(OUT/spec['preview'])
|
|
bpy.ops.render.render(write_still=True)
|
|
print('ASSEMBLY_EXPORT_COMPLETE '+json.dumps(report), flush=True)
|
|
return report
|
|
|
|
|
|
def main():
|
|
reports = {kind: export_part(kind, spec) for kind, spec in SPECS.items()}
|
|
result = {
|
|
'source_blend': MAIN.name,
|
|
'source_blend_sha256': hashlib.sha256(MAIN.read_bytes()).hexdigest(),
|
|
'reference_video': 'model_assets/reference/juicer/ZMUEX_working_video.mp4',
|
|
'assembly_segment_seconds': [233, 283],
|
|
'parts': reports,
|
|
'technical_checks_passed': all(report['technical_checks_passed'] for report in reports.values()),
|
|
'visual_review_passed': False,
|
|
}
|
|
result['passed'] = result['technical_checks_passed']
|
|
(OUT/'assembly_parts_validation.json').write_text(json.dumps(result, ensure_ascii=False, indent=2)+'\n', encoding='utf-8')
|
|
print('ALL_ASSEMBLY_PARTS_EXPORTED', flush=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|