File size: 7,185 Bytes
3f15e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""Extend the existing living-room wood finish to the balcony floor region."""
import argparse
import copy
import json
import math
import sys
from pathlib import Path

import bpy


def main():
    parser = argparse.ArgumentParser()
    for key in ('tree', 'scene', 'output'):
        parser.add_argument('--' + key, type=Path, required=True)
    args = parser.parse_args(sys.argv[sys.argv.index('--') + 1:])
    sys.path[:0] = [str(args.tree), str(args.tree / 'engine'),
                    str(args.tree / 'engine/source/scripts/internal/scene_generation/real_assets')]
    from supplemental_blender_assets import digest, fingerprint
    from apply_showhome_scene import geometry_signature
    from asset_import import material_record
    from real_test.render_wholehouse import snapshot
    from stage_checkpoint import fingerprint as json_fingerprint

    assert not args.output.exists()
    before_files = snapshot(args.scene)
    mp = args.scene / 'composition_manifest.runtime.json'
    manifest = json.loads(mp.read_text())
    source = args.scene / (manifest['scene_id'] + '.blend')
    parent = json.loads((args.scene / 'home_revision_receipt.json').read_text())
    assert parent['passed'] and parent['source_blend_sha256'] == digest(source)
    assert parent['source_manifest_sha256'] == digest(mp)
    sp = args.scene / manifest['structural_material_manifest']['path']
    assert digest(sp) == manifest['structural_material_manifest']['sha256']
    structural = json.loads(sp.read_text())
    rows = [r for r in structural['assignments'] if r['object_name'] == 'floor__whole_house']
    balcony = next(r for r in rows if r['room_id'] == 'balcony')
    living = next(r for r in rows if r['room_id'] == 'living')
    original_assignment = copy.deepcopy(balcony)
    bpy.ops.wm.open_mainfile(filepath=str(source))
    floor = bpy.data.objects['floor__whole_house']
    geometry = geometry_signature(floor)
    protected = {o.name: fingerprint(o) for o in bpy.context.scene.objects if o != floor}
    materials = {s.material.name: material_record(s.material)['signature_sha256']
                 for o in bpy.context.scene.objects for s in o.material_slots if s.material}
    slots = [m.name for m in floor.data.materials]
    index = balcony['material_slot_index']
    assert floor.data.materials[index].name == balcony['material_name']
    wood = floor.data.materials[living['material_slot_index']]
    assert material_record(wood)['signature_sha256'] == living['material_signature_sha256']
    floor.data.materials[index] = wood
    slots[index] = wood.name
    for key in ('role', 'material_id', 'family_id', 'material_name', 'material_signature_sha256',
                'uv_policy', 'texture_scale_m', 'projection', 'projection_blend'):
        balcony[key] = copy.deepcopy(living[key])
    structural['room_floor_roles']['balcony'] = living['role']
    structural['assignment_fingerprint'] = json_fingerprint(structural['assignments'])
    structural['application'] = dict(passed=True, scope='Saved-scene bindings after balcony wood-floor revision',
                                     assignment_sha256=structural['assignment_fingerprint'])
    structural['balcony_floor_parent_assignment'] = original_assignment
    floor_bounds = next(r['bounds_m'] for r in manifest['floor_plan']['rooms'] if r['room_id'] == 'balcony')

    def verify():
        obj = bpy.data.objects['floor__whole_house']
        assert geometry_signature(obj) == geometry
        assert [m.name for m in obj.data.materials] == slots
        assert all(fingerprint(bpy.data.objects[name]) == value for name, value in protected.items())
        assert all(material_record(bpy.data.materials[name])['signature_sha256'] == value for name, value in materials.items())
        top_faces = [p for p in obj.data.polygons if p.material_index == index and p.normal.z > .9]
        assert top_faces
        for polygon in top_faces:
            for vi in polygon.vertices:
                point = obj.matrix_world @ obj.data.vertices[vi].co
                assert floor_bounds[0] - 1e-5 <= point.x <= floor_bounds[2] + 1e-5
                assert floor_bounds[1] - 1e-5 <= point.y <= floor_bounds[3] + 1e-5
                assert abs(point.z) < 1e-5
        area = sum(p.area for p in top_faces)
        assert abs(area - manifest['floor_slab']['room_top_face_area_m2']['balcony']) < 1e-4
        for node in obj.data.materials[index].node_tree.nodes:
            if node.type == 'TEX_IMAGE':
                assert node.image.packed_file and list(node.image.size) == [2048, 2048]
        rig = manifest['balcony_coffee_revision']['articulation']
        root = bpy.data.objects[rig['root_name']]
        for angle in (0, 30, 60, 90):
            for control in rig['controls']:
                root[control['property']] = float(angle)
            root.update_tag()
            bpy.context.view_layer.update()
            for control in rig['controls']:
                assert abs(math.degrees(bpy.data.objects[control['hinge_name']].rotation_euler.z) + angle) < 1e-4
        for control in rig['controls']:
            root[control['property']] = 0.
        root.update_tag()
        bpy.context.view_layer.update()
        assert all(fingerprint(bpy.data.objects[name]) == value for name, value in protected.items())
        return dict(top_face_count=len(top_faces), area_m2=area)

    floor_check = verify()
    args.output.mkdir(parents=True)
    out = args.output / source.name
    bpy.ops.wm.save_as_mainfile(filepath=str(out), relative_remap=False)
    bpy.ops.wm.open_mainfile(filepath=str(out))
    assert verify() == floor_check
    assert snapshot(args.scene) == before_files
    target_sp = args.output / sp.name
    target_sp.write_text(json.dumps(structural, ensure_ascii=False, indent=2) + '\n')
    manifest['structural_material_manifest'] = dict(path=target_sp.name, sha256=digest(target_sp))
    result = dict(schema='rhg.balcony-wood-floor.v1', passed=True, parent_blend_sha256=digest(source),
                  parent_manifest_sha256=digest(mp), source_scene_unchanged=True, saved_reopen_passed=True,
                  protected_object_count=len(protected), floor_geometry_preserved=True,
                  other_floor_slots_preserved=True, existing_materials_preserved=True,
                  changed_floor_slot=index, original_assignment=original_assignment,
                  current_assignment=copy.deepcopy(balcony), floor_check=floor_check,
                  laundry_controls_checked_deg=[0, 30, 60, 90],
                  parent_collision_evidence_retained_by_identical_geometry=True,
                  physics_simulation_tested=False)
    manifest['balcony_floor_revision'] = copy.deepcopy(result)
    target_mp = args.output / mp.name
    target_mp.write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + '\n')
    result.update(source_blend_sha256=digest(out), source_manifest_sha256=digest(target_mp))
    for filename in ('balcony_floor_receipt.json', 'home_revision_receipt.json'):
        (args.output / filename).write_text(json.dumps(result, ensure_ascii=False, indent=2) + '\n')
    print(json.dumps(result), flush=True)


if __name__ == '__main__':
    main()